@cocreate/elements 1.17.15 → 1.17.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js CHANGED
@@ -1,206 +1,206 @@
1
- import observer from '@cocreate/observer';
2
- import action from '@cocreate/actions';
3
- import crud from '@cocreate/crud-client';
4
- import ccfilter from '@cocreate/filter';
5
- import '@cocreate/element-prototype';
6
- import './fetchSrc';
7
-
8
-
9
- const selector = "[collection][name]:not(cocreate-select, link), input, textarea, select, [contenteditable]";
10
- const initializing = new Map();
11
- const els = new Map();
12
-
13
- function init() {
14
- let elements = document.querySelectorAll(selector);
15
- __initSocket();
16
- initElements(elements);
17
- }
18
-
19
- function initElements(elements) {
20
- let documents = new Map();
21
- // let els = []
22
- for (let element of elements){
23
- let doc = initElement(element);
24
- if (doc) {
25
- let key = JSON.stringify(doc)
26
- let item = documents.get(key);
27
- if (item)
28
- doc.elements = [...item.elements, element]
29
- else
30
- doc.elements = [element]
31
-
32
- // let collection = doc.collection;
33
- // let document_id = doc.document_id;
34
- // let filter = doc.filter;
35
- let initialize = initializing.get(element)
36
- if (!initialize){
37
- initializing.set(element, key);
38
- documents.set(key, doc);
39
- // els.set(key, doc);
40
- // els.push(element)
41
- } else {
42
- // if (document_id && initialize.document_id != document_id || filter && JSON.stringify(initialize.filter) != JSON.stringify(filter)){
43
- if (initialize != key){
44
- initializing.set(element, key);
45
- documents.set(key, doc);
46
- // els.set(key, doc);
47
- // els.push(element)
48
- }
49
- }
50
- }
51
- }
52
-
53
- read(documents);
54
- }
55
-
56
- function initElement(el) {
57
- __initEvents(el);
58
-
59
- if (el.closest('.template')) return;
60
-
61
- // if rendered in server side skip
62
- if (el.hasAttribute('rendered')) {
63
- el.removeAttribute('rendered');
64
- return;
65
- }
66
-
67
- const { collection, document_id, name, isRead } = crud.getAttributes(el);
68
- if (!collection || !name) return;
69
-
70
- if (document_id)
71
- if (!document_id.match(/^[0-9a-fA-F]{24}$/)) return;
72
-
73
- let filter
74
- if (el.hasAttribute('filter_id') || el.hasAttribute('filter-name' ) || el.hasAttribute('filter-sort-name')) {
75
- filter = ccfilter.init(el, "filter_id");
76
- }
77
-
78
- if (!crud.checkValue(collection) || !crud.checkValue(name)) return;
79
-
80
- if (isRead == 'false') return;
81
- if (!document_id && !filter) return;
82
-
83
- return {collection, document_id, filter};
84
- }
85
-
86
- async function read(documents) {
87
- if (documents && documents.size > 0) {
88
- for (let [key, {collection, document_id, filter, elements}] of documents) {
89
- documents.delete(key);
90
-
91
- let data = {collection}
92
- if (document_id)
93
- data.document = {_id: document_id}
94
- if (filter)
95
- data.filter = filter
96
-
97
- let response = await crud.readDocument(data);
98
- setData(elements, response);
99
- }
100
- }
101
- }
102
-
103
- function setData(elements, data) {
104
- if (!data.document || !data.document[0]) return;
105
- let key = getKey(data)
106
- if (!elements) {
107
- elements = els.get(key)
108
- } else {
109
- let eles = els.get(key)
110
- if (eles && eles.length)
111
- els.set(key, [...eles, ...elements])
112
- else
113
- els.set(key, elements)
114
- }
115
- if (!elements || elements.length == 0)
116
- return
117
-
118
- for (let el of elements) {
119
- const { collection, document_id, name, isRead, isUpdate, isCrdt } = crud.getAttributes(el);
120
-
121
- if (el.hasAttribute('actions') || el.tagName === 'DIV' && !el.classList.contains('domEditor')) continue;
122
- if (!name || isRead == "false" || isUpdate == "false" || isCrdt == "true") continue;
123
-
124
- if (data.document[0]['collection'] == collection && data.document[0]['_id'] == document_id) {
125
- let value = crud.getValueFromObject(data.document[0], name);
126
- el.setValue(value);
127
- initializing.delete(el)
128
- }
129
- }
130
- }
131
-
132
- function getKey(data) {
133
- let key = {};
134
- let attributes = ["db", "database", "collection", "index", "document"]
135
-
136
- for (let attribute of attributes) {
137
- let value = data[attribute]
138
- if (value) {
139
- if (attribute == 'document')
140
- key[attribute] = value
141
- }
142
- }
143
- key.document = data.document[0]._id
144
- return JSON.stringify(key)
145
- }
146
-
147
- async function save(element) {
148
- let value = element.getValue();
149
- crud.save(element, value);
150
- }
151
-
152
- function __initSocket() {
153
- crud.listen('updateDocument', function(data) {
154
- setData(null, data);
155
- });
156
- }
157
-
158
- const elementEvents = new Map();
159
- function __initEvents(el) {
160
- if (!elementEvents.has(el)) {
161
- if (el.tagName == 'INPUT' || el.tagName == 'TEXTAREA' || el.tagName == 'SELECT' || el.hasAttribute('contenteditable')) {
162
- el.addEventListener('input', function(e) {
163
- const {document_id, name, isRealtime, isCrdt} = crud.getAttributes(el);
164
- if (isCrdt == "true" && document_id && document_id != 'pending' || isRealtime == "false" || name == "_id") return;
165
- if (document_id && e.detail && e.detail.skip == true) return;
166
- save(el);
167
- });
168
-
169
- elementEvents.set(el, true)
170
- }
171
- }
172
- }
173
-
174
- observer.init({
175
- name: 'CoCreateElementsChildList',
176
- observe: ['childList'],
177
- target: selector,
178
- callback: function(mutation) {
179
- initElements(mutation.addedNodes);
180
- }
181
- });
182
-
183
- observer.init({
184
- name: 'CoCreateElementsAttributes',
185
- observe: ['attributes'],
186
- attributeName: crud.getAttributeNames(['collection', 'document_id', 'name']),
187
- target: selector,
188
- callback: function(mutation) {
189
- let {collection, document_id, name} = crud.getAttributes(mutation.target);
190
- if (collection && document_id && name)
191
- initElements([mutation.target]);
192
- }
193
- });
194
-
195
- action.init({
196
- name: "saveHtml",
197
- endEvent: "changed-element",
198
- callback: (btn, data) => {
199
- let form = btn.closet('form');
200
- save(form);
201
- },
202
- });
203
-
204
- init();
205
-
206
- export default {initElements, initElement, save};
1
+ import observer from '@cocreate/observer';
2
+ import action from '@cocreate/actions';
3
+ import crud from '@cocreate/crud-client';
4
+ import ccfilter from '@cocreate/filter';
5
+ import '@cocreate/element-prototype';
6
+ import './fetchSrc';
7
+
8
+
9
+ const selector = "[collection][name]:not(cocreate-select, link), input, textarea, select, [contenteditable]";
10
+ const initializing = new Map();
11
+ const els = new Map();
12
+
13
+ function init() {
14
+ let elements = document.querySelectorAll(selector);
15
+ __initSocket();
16
+ initElements(elements);
17
+ }
18
+
19
+ function initElements(elements) {
20
+ let documents = new Map();
21
+ // let els = []
22
+ for (let element of elements){
23
+ let doc = initElement(element);
24
+ if (doc) {
25
+ let key = JSON.stringify(doc)
26
+ let item = documents.get(key);
27
+ if (item)
28
+ doc.elements = [...item.elements, element]
29
+ else
30
+ doc.elements = [element]
31
+
32
+ // let collection = doc.collection;
33
+ // let document_id = doc.document_id;
34
+ // let filter = doc.filter;
35
+ let initialize = initializing.get(element)
36
+ if (!initialize){
37
+ initializing.set(element, key);
38
+ documents.set(key, doc);
39
+ // els.set(key, doc);
40
+ // els.push(element)
41
+ } else {
42
+ // if (document_id && initialize.document_id != document_id || filter && JSON.stringify(initialize.filter) != JSON.stringify(filter)){
43
+ if (initialize != key){
44
+ initializing.set(element, key);
45
+ documents.set(key, doc);
46
+ // els.set(key, doc);
47
+ // els.push(element)
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ read(documents);
54
+ }
55
+
56
+ function initElement(el) {
57
+ __initEvents(el);
58
+
59
+ if (el.closest('.template')) return;
60
+
61
+ // if rendered in server side skip
62
+ if (el.hasAttribute('rendered')) {
63
+ el.removeAttribute('rendered');
64
+ return;
65
+ }
66
+
67
+ const { collection, document_id, name, isRead } = crud.getAttributes(el);
68
+ if (!collection || !name) return;
69
+
70
+ if (document_id)
71
+ if (!document_id.match(/^[0-9a-fA-F]{24}$/)) return;
72
+
73
+ let filter
74
+ if (el.hasAttribute('filter_id') || el.hasAttribute('filter-name' ) || el.hasAttribute('filter-sort-name')) {
75
+ filter = ccfilter.init(el, "filter_id");
76
+ }
77
+
78
+ if (!crud.checkValue(collection) || !crud.checkValue(name)) return;
79
+
80
+ if (isRead == 'false') return;
81
+ if (!document_id && !filter) return;
82
+
83
+ return {collection, document_id, filter};
84
+ }
85
+
86
+ async function read(documents) {
87
+ if (documents && documents.size > 0) {
88
+ for (let [key, {collection, document_id, filter, elements}] of documents) {
89
+ documents.delete(key);
90
+
91
+ let data = {collection}
92
+ if (document_id)
93
+ data.document = {_id: document_id}
94
+ if (filter)
95
+ data.filter = filter
96
+
97
+ let response = await crud.readDocument(data);
98
+ setData(elements, response);
99
+ }
100
+ }
101
+ }
102
+
103
+ function setData(elements, data) {
104
+ if (!data.document || !data.document[0]) return;
105
+ let key = getKey(data)
106
+ if (!elements) {
107
+ elements = els.get(key)
108
+ } else {
109
+ let eles = els.get(key)
110
+ if (eles && eles.length)
111
+ els.set(key, [...eles, ...elements])
112
+ else
113
+ els.set(key, elements)
114
+ }
115
+ if (!elements || elements.length == 0)
116
+ return
117
+
118
+ for (let el of elements) {
119
+ const { collection, document_id, name, isRead, isUpdate, isCrdt } = crud.getAttributes(el);
120
+
121
+ if (el.hasAttribute('actions') || el.tagName === 'DIV' && !el.classList.contains('domEditor')) continue;
122
+ if (!name || isRead == "false" || isUpdate == "false" || isCrdt == "true") continue;
123
+
124
+ if (data.document[0]['collection'] == collection && data.document[0]['_id'] == document_id) {
125
+ let value = crud.getValueFromObject(data.document[0], name);
126
+ el.setValue(value);
127
+ initializing.delete(el)
128
+ }
129
+ }
130
+ }
131
+
132
+ function getKey(data) {
133
+ let key = {};
134
+ let attributes = ["db", "database", "collection", "index", "document"]
135
+
136
+ for (let attribute of attributes) {
137
+ let value = data[attribute]
138
+ if (value) {
139
+ if (attribute == 'document')
140
+ key[attribute] = value
141
+ }
142
+ }
143
+ key.document = data.document[0]._id
144
+ return JSON.stringify(key)
145
+ }
146
+
147
+ async function save(element) {
148
+ let value = element.getValue();
149
+ crud.save(element, value);
150
+ }
151
+
152
+ function __initSocket() {
153
+ crud.listen('updateDocument', function(data) {
154
+ setData(null, data);
155
+ });
156
+ }
157
+
158
+ const elementEvents = new Map();
159
+ function __initEvents(el) {
160
+ if (!elementEvents.has(el)) {
161
+ if (el.tagName == 'INPUT' || el.tagName == 'TEXTAREA' || el.tagName == 'SELECT' || el.hasAttribute('contenteditable')) {
162
+ el.addEventListener('input', function(e) {
163
+ const {document_id, name, isRealtime, isCrdt} = crud.getAttributes(el);
164
+ if (isCrdt == "true" && document_id && document_id != 'pending' || isRealtime == "false" || name == "_id") return;
165
+ if (document_id && e.detail && e.detail.skip == true) return;
166
+ save(el);
167
+ });
168
+
169
+ elementEvents.set(el, true)
170
+ }
171
+ }
172
+ }
173
+
174
+ observer.init({
175
+ name: 'CoCreateElementsChildList',
176
+ observe: ['childList'],
177
+ target: selector,
178
+ callback: function(mutation) {
179
+ initElements(mutation.addedNodes);
180
+ }
181
+ });
182
+
183
+ observer.init({
184
+ name: 'CoCreateElementsAttributes',
185
+ observe: ['attributes'],
186
+ attributeName: crud.getAttributeNames(['collection', 'document_id', 'name']),
187
+ target: selector,
188
+ callback: function(mutation) {
189
+ let {collection, document_id, name} = crud.getAttributes(mutation.target);
190
+ if (collection && document_id && name)
191
+ initElements([mutation.target]);
192
+ }
193
+ });
194
+
195
+ action.init({
196
+ name: "saveHtml",
197
+ endEvent: "changed-element",
198
+ callback: (btn, data) => {
199
+ let form = btn.closet('form');
200
+ save(form);
201
+ },
202
+ });
203
+
204
+ init();
205
+
206
+ export default {initElements, initElement, save};
package/webpack.config.js CHANGED
@@ -1,84 +1,84 @@
1
- const path = require("path")
2
- const TerserPlugin = require("terser-webpack-plugin")
3
- const MiniCssExtractPlugin = require("mini-css-extract-plugin")
4
- let isProduction = process.env.NODE_ENV === "production"
5
- const { CleanWebpackPlugin } = require("clean-webpack-plugin")
6
-
7
- module.exports = {
8
- entry: {
9
- "CoCreate-elements": "./src/index.js",
10
- },
11
- output: {
12
- path: path.resolve(__dirname, "dist"),
13
- filename: isProduction ? "[name].min.js" : "[name].js",
14
- libraryTarget: "umd",
15
- libraryExport: "default",
16
- library: ["CoCreate", "elements"],
17
- globalObject: "this",
18
- },
19
-
20
- plugins: [
21
- new CleanWebpackPlugin(),
22
- new MiniCssExtractPlugin({
23
- filename: "[name].css",
24
- }),
25
- ],
26
- // Default mode for Webpack is production.
27
- // Depending on mode Webpack will apply different things
28
- // on final bundle. For now we don't need production's JavaScript
29
- // minifying and other thing so let's set mode to development
30
- mode: isProduction ? "production" : "development",
31
- module: {
32
- rules: [
33
- {
34
- test: /.js$/,
35
- exclude: /(node_modules)/,
36
- use: {
37
- loader: "babel-loader",
38
- options: {
39
- plugins: ["@babel/plugin-transform-modules-commonjs"],
40
- },
41
- },
42
- },
43
- {
44
- test: /.css$/i,
45
- use: [
46
- { loader: "style-loader", options: { injectType: "linkTag" } },
47
- "file-loader",
48
- ],
49
- },
50
- ],
51
- },
52
-
53
- // add source map
54
- ...(isProduction ? {} : { devtool: "eval-source-map" }),
55
-
56
- optimization: {
57
- minimize: true,
58
- minimizer: [
59
- new TerserPlugin({
60
- extractComments: true,
61
- // cache: true,
62
- parallel: true,
63
- // sourceMap: true, // Must be set to true if using source-maps in production
64
- terserOptions: {
65
- // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
66
- // extractComments: 'all',
67
- compress: {
68
- drop_console: true,
69
- },
70
- },
71
- }),
72
- ],
73
- splitChunks: {
74
- chunks: "all",
75
- minSize: 200,
76
- // maxSize: 99999,
77
- //minChunks: 1,
78
-
79
- cacheGroups: {
80
- defaultVendors: false,
81
- },
82
- },
83
- },
84
- }
1
+ const path = require("path")
2
+ const TerserPlugin = require("terser-webpack-plugin")
3
+ const MiniCssExtractPlugin = require("mini-css-extract-plugin")
4
+ let isProduction = process.env.NODE_ENV === "production"
5
+ const { CleanWebpackPlugin } = require("clean-webpack-plugin")
6
+
7
+ module.exports = {
8
+ entry: {
9
+ "CoCreate-elements": "./src/index.js",
10
+ },
11
+ output: {
12
+ path: path.resolve(__dirname, "dist"),
13
+ filename: isProduction ? "[name].min.js" : "[name].js",
14
+ libraryTarget: "umd",
15
+ libraryExport: "default",
16
+ library: ["CoCreate", "elements"],
17
+ globalObject: "this",
18
+ },
19
+
20
+ plugins: [
21
+ new CleanWebpackPlugin(),
22
+ new MiniCssExtractPlugin({
23
+ filename: "[name].css",
24
+ }),
25
+ ],
26
+ // Default mode for Webpack is production.
27
+ // Depending on mode Webpack will apply different things
28
+ // on final bundle. For now we don't need production's JavaScript
29
+ // minifying and other thing so let's set mode to development
30
+ mode: isProduction ? "production" : "development",
31
+ module: {
32
+ rules: [
33
+ {
34
+ test: /.js$/,
35
+ exclude: /(node_modules)/,
36
+ use: {
37
+ loader: "babel-loader",
38
+ options: {
39
+ plugins: ["@babel/plugin-transform-modules-commonjs"],
40
+ },
41
+ },
42
+ },
43
+ {
44
+ test: /.css$/i,
45
+ use: [
46
+ { loader: "style-loader", options: { injectType: "linkTag" } },
47
+ "file-loader",
48
+ ],
49
+ },
50
+ ],
51
+ },
52
+
53
+ // add source map
54
+ ...(isProduction ? {} : { devtool: "eval-source-map" }),
55
+
56
+ optimization: {
57
+ minimize: true,
58
+ minimizer: [
59
+ new TerserPlugin({
60
+ extractComments: true,
61
+ // cache: true,
62
+ parallel: true,
63
+ // sourceMap: true, // Must be set to true if using source-maps in production
64
+ terserOptions: {
65
+ // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
66
+ // extractComments: 'all',
67
+ compress: {
68
+ drop_console: true,
69
+ },
70
+ },
71
+ }),
72
+ ],
73
+ splitChunks: {
74
+ chunks: "all",
75
+ minSize: 200,
76
+ // maxSize: 99999,
77
+ //minChunks: 1,
78
+
79
+ cacheGroups: {
80
+ defaultVendors: false,
81
+ },
82
+ },
83
+ },
84
+ }