neo.mjs 5.13.10 → 5.14.1

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.
@@ -6,38 +6,6 @@ import Base from '../core/Base.mjs';
6
6
  * @singleton
7
7
  */
8
8
  class Logger extends Base {
9
- /**
10
- * Colors
11
- * @property {Object} colors
12
- */
13
- logColors = {
14
- error: 'indianred',
15
- info : '#acacac',
16
- log : '#448888',
17
- warn : '#6d6d00'
18
- }
19
- /**
20
- * Character
21
- * @property {Object} logChar
22
- */
23
- logChars = {
24
- error: 'E',
25
- info : 'I',
26
- log : 'L',
27
- warn : 'W'
28
- }
29
- /**
30
- * LogLevels
31
- * @property {String[]} logLevels
32
- */
33
- logLevels = ['info', 'log', 'warn', 'error']
34
-
35
- /**
36
- * Timeout
37
- * @property {Number} timeToStart in ms
38
- */
39
- timeToStartComponentLogger = 1500
40
-
41
9
  static config = {
42
10
  /**
43
11
  * @member {String} className='Neo.util.Logger'
@@ -50,7 +18,7 @@ class Logger extends Base {
50
18
  *
51
19
  * Neo.util.Logger.enableLogsInProduction = true;
52
20
  *
53
- * @member {boolean} enableLogsInProduction=true
21
+ * @member {Boolean} enableLogsInProduction=true
54
22
  */
55
23
  enableLogsInProduction: false,
56
24
  /**
@@ -59,9 +27,9 @@ class Logger extends Base {
59
27
  *
60
28
  * Neo.util.Logger.enableComponentLogger = true;
61
29
  *
62
- * @member {boolean} enableComponentLogger_=true
30
+ * @member {Boolean} enableComponentLogger=true
63
31
  */
64
- enableComponentLogger_: true,
32
+ enableComponentLogger: true,
65
33
  /**
66
34
  * Set the minimum level, which you want to output.
67
35
  * Change this at any time using a value of logLevels: ['info', 'log', 'warn', 'error']
@@ -73,19 +41,43 @@ class Logger extends Base {
73
41
  */
74
42
  level_: 'info',
75
43
  /**
76
- * @member {boolean} enableLogs=true
44
+ * @member {Boolean} enableLogs=true
77
45
  * @protected
78
46
  */
79
47
  singleton: true
80
48
  }
81
49
 
50
+ /**
51
+ * @member {Object} logChar
52
+ */
53
+ logChars = {
54
+ error: 'E',
55
+ info : 'I',
56
+ log : 'L',
57
+ warn : 'W'
58
+ }
59
+ /**
60
+ * @member {Object} colors
61
+ */
62
+ logColors = {
63
+ error: 'indianred',
64
+ info : '#acacac',
65
+ log : '#448888',
66
+ warn : '#6d6d00'
67
+ }
68
+ /**
69
+ * LogLevels
70
+ * @member {String[]} logLevels
71
+ */
72
+ logLevels = ['info', 'log', 'warn', 'error']
73
+
82
74
  /**
83
75
  * @param config
84
76
  */
85
77
  construct(config) {
86
78
  super.construct(config);
87
79
 
88
- const me = this;
80
+ let me = this;
89
81
 
90
82
  // aliases
91
83
  Neo.applyFromNs(Neo, me, {
@@ -98,188 +90,185 @@ class Logger extends Base {
98
90
 
99
91
  setTimeout(() => {
100
92
  if (!me.enableLogsInProduction && Neo.config.environment === 'dist/production') {
101
- me.write = Neo.emptyFn;
93
+ me.write = Neo.emptyFn
102
94
  }
103
- }, 50);
95
+ }, 50)
104
96
  }
105
97
 
106
98
  /**
107
99
  * Ctrl-Right Click will show the current component
108
- * @param {Boolean} value
109
- * @param {Boolean} oldValue
100
+ * @param {Neo.component.Base} view
110
101
  */
111
- afterSetEnableComponentLogger(value, oldValue) {
112
- setTimeout(() => {
113
- if (value) {
114
- if (Neo.workerId !== 'app' || Neo.config.environment === 'dist/production') return;
115
-
116
- const viewport = Neo.getComponent('neo-viewport-1') || Neo.getComponent('neo-configuration-viewport-1');
117
- if (!viewport) {
118
- console.warn('[LOGGER] could not find viewport.');
119
- return;
120
- }
121
-
122
- viewport.addDomListeners({
123
- contextmenu: (data) => {
124
- if (data.ctrlKey) {
125
- let isGroupSet = false;
126
-
127
- data.path.forEach((item) => {
128
- const component = Neo.getComponent(item.id);
129
-
130
- if (component) {
131
- if (!isGroupSet) {
132
- isGroupSet = true;
133
- console.group(item.id);
134
- }
135
- console.log(component);
136
- }
137
- });
138
-
139
- if (isGroupSet) {
140
- console.groupEnd();
141
- }
142
- }
143
- }
144
- });
145
- }
146
- }, this.timeToStartComponentLogger);
102
+ addContextMenuListener(view) {
103
+ view.addDomListeners({
104
+ contextmenu: this.onContextMenu,
105
+ scope : this
106
+ })
147
107
  }
148
108
 
149
109
  /**
150
110
  * Set level to number based on position in logLevels
151
111
  * @param {String} value
152
112
  * @param {String|Number} oldValue
153
- * @returns {number}
113
+ * @returns {Number}
154
114
  */
155
115
  beforeSetLevel(value, oldValue) {
156
- return this.logLevels.indexOf(value);
116
+ return this.logLevels.indexOf(value)
157
117
  }
158
118
 
159
119
  /**
160
- * @param value
120
+ * @param {String} value
161
121
  */
162
122
  error(value) {
163
- throw new Error(value);
123
+ throw new Error(value)
164
124
  }
165
125
 
166
126
  /**
167
- * @param args
127
+ * internal helper to catch caller
128
+ * no known native way in modern JS to know what file that triggered the current method
129
+ * therefore we use Error, we can get the caller file from the stack trace string.
130
+ * @protected
131
+ * @returns {String}
168
132
  */
169
- info(...args) {
170
- args = this.resolveArgs(...args);
171
- this.write(args, 'info');
172
- }
133
+ getCaller() {
134
+ let caller_path = undefined,
135
+ err = new Error(),
136
+ stack_lines = err.stack.split('\n'),
137
+ found_this = false,
138
+ i, line;
139
+
140
+ for (i in stack_lines) {
141
+ line = stack_lines[i];
142
+
143
+ if (!found_this && /Logger\.mjs/.test(line)) {
144
+ found_this = true
145
+ } else if (found_this) {
146
+ if (!/Logger\.mjs/.test(line)) {
147
+ // remove the closing )
148
+ line = line.replace(')', '');
149
+ // get the part after the last /
150
+ caller_path = line.match(/([^\/]+)$/)[1].match(/([^ ]+)$/)[1];
151
+
152
+ break;
153
+ }
154
+ }
155
+ }
173
156
 
174
- /**
175
- * @param args
176
- */
177
- log(...args) {
178
- args = this.resolveArgs(...args);
179
- this.write(args, 'log');
157
+ return caller_path
180
158
  }
181
159
 
182
160
  /**
183
161
  * @param args
184
162
  */
185
- logError(...args) {
163
+ info(...args) {
186
164
  args = this.resolveArgs(...args);
187
- this.write(args, 'error');
165
+ this.write(args, 'info')
188
166
  }
189
167
 
190
168
  /**
191
169
  * @param args
192
170
  */
193
- warn(...args) {
171
+ log(...args) {
194
172
  args = this.resolveArgs(...args);
195
- this.write(args, 'warn');
173
+ this.write(args, 'log')
196
174
  }
197
175
 
198
176
  /**
199
- * Output method
200
177
  * @param args
201
- * @param {String} level
202
- * @protected
203
178
  */
204
- write(args, level) {
205
- const me = this;
206
- if (me.beforeSetLevel(level) < me.level) return;
207
-
208
- const logColor = me.logColors[level],
209
- logChar = me.logChars[level],
210
- bg = `background-color:${logColor}; color: white; font-weight: 900;`,
211
- color = `color:${logColor};`,
212
- msg = `[${me.getCaller()}] ${args.msg}`;
213
-
214
- if (args.data) {
215
- console.groupCollapsed(`%c ${logChar} %c ${msg}`, bg, color)
216
- console.log(args.data);
217
- console.groupEnd();
218
- } else {
219
- console.log(`%c ${logChar} %c ${msg}`, bg, color)
220
- }
179
+ logError(...args) {
180
+ args = this.resolveArgs(...args);
181
+ this.write(args, 'error')
221
182
  }
222
183
 
223
184
  /**
224
- * HELPER TO CATCH CALLER
225
- * no known native way in modern JS to know what file that triggered the current method
226
- * therefore we use Error, we can get the caller file from the stack trace string.
185
+ * @param {Object} data
227
186
  */
228
- getCaller() {
229
- let caller_path = undefined;
230
-
231
- try {
232
- throw Error();
233
-
234
- } catch (err) {
235
- const stack_lines = err.stack.split('\n');
236
- let found_this = false;
237
-
238
- for (let i in stack_lines) {
239
- let line = stack_lines[i];
240
-
241
- if (!found_this && /Logger\.mjs/.test(line)) {
242
- found_this = true
243
-
244
- } else if (found_this) {
245
- if (!/Logger\.mjs/.test(line)) {
246
- // remove the closing )
247
- line = line.replace(')', '');
248
- // get the part after the last /
249
- caller_path = line.match(/([^\/]+)$/)[1].match(/([^ ]+)$/)[1];
250
-
251
- break;
187
+ onContextMenu(data) {
188
+ if (
189
+ data.ctrlKey
190
+ && this.enableComponentLogger
191
+ && !(Neo.config.env === 'dist/production' && this.enableLogsInProduction)
192
+ ) {
193
+ let isGroupSet = false,
194
+ component;
195
+
196
+ data.path.forEach(item => {
197
+ component = Neo.getComponent(item.id);
198
+
199
+ if (component) {
200
+ if (!isGroupSet) {
201
+ isGroupSet = true;
202
+ console.group(item.id)
252
203
  }
253
- }
254
204
 
255
- }
205
+ console.log(component)
206
+ }
207
+ });
256
208
 
257
- return caller_path
209
+ isGroupSet && console.groupEnd()
258
210
  }
259
211
  }
260
212
 
261
213
  /**
262
- * HELPER FOR ARGS
214
+ * Internal helper for args
263
215
  * @param {Array} args
264
- * @return {Object}
216
+ * @returns {Object}
217
+ * @protected
265
218
  */
266
219
  resolveArgs(...args) {
267
220
  const identifier = args[0];
268
- let argsObject = {};
221
+ let argsObject = {};
269
222
 
270
223
  if (args.length === 1) {
271
224
  if (Neo.isString(identifier)) {
272
- argsObject.msg = args[0];
225
+ argsObject.msg = args[0]
273
226
  } else if (Neo.isObject(identifier)) {
274
- argsObject = identifier;
227
+ argsObject = identifier
275
228
  }
276
229
  } else if (args.length === 2) {
277
- argsObject.msg = args[0];
278
- argsObject.data = args[1];
230
+ argsObject.msg = args[0];
231
+ argsObject.data = args[1]
279
232
  }
280
233
 
281
234
  return argsObject
282
235
  }
236
+
237
+ /**
238
+ * @param args
239
+ */
240
+ warn(...args) {
241
+ args = this.resolveArgs(...args);
242
+ this.write(args, 'warn')
243
+ }
244
+
245
+ /**
246
+ * Output method
247
+ * @param {Object} args
248
+ * @param {String} level
249
+ * @protected
250
+ */
251
+ write(args, level) {
252
+ let me = this;
253
+
254
+ if (me.beforeSetLevel(level) < me.level) {
255
+ return
256
+ }
257
+
258
+ let logColor = me.logColors[level],
259
+ logChar = me.logChars[level],
260
+ bg = `background-color:${logColor}; color: white; font-weight: 900;`,
261
+ color = `color:${logColor};`,
262
+ msg = `[${me.getCaller()}] ${args.msg}`;
263
+
264
+ if (args.data) {
265
+ console.groupCollapsed(`%c ${logChar} %c ${msg}`, bg, color)
266
+ console.log(args.data);
267
+ console.groupEnd()
268
+ } else {
269
+ console.log(`%c ${logChar} %c ${msg}`, bg, color)
270
+ }
271
+ }
283
272
  }
284
273
 
285
274
  let instance = Neo.applyClassConfig(Logger);