jqx-es 1.2.0 → 1.2.2

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/Utilities.js CHANGED
@@ -1,65 +1,141 @@
1
1
  import {default as tagFNFactory} from "./tinyDOM.js";
2
2
  import {default as IS, maybe} from "./TypeofAnything.js";
3
3
  import styleFactory from "./LifeCSS.js";
4
+
4
5
  const characters4RandomString = [...Array(26)]
5
6
  .map((x, i) => String.fromCharCode(i + 65))
6
7
  .concat([...Array(26)].map((x, i) => String.fromCharCode(i + 97)))
7
8
  .concat([...Array(10)].map((x, i) => `${i}`));
8
- const pad0 = (nr, n=2) => `${nr}`.padStart(n, `0`);
9
- const randomNr = (max, min = 0) => {
9
+ const systemLog = systemLogFactory();
10
+
11
+ function pad0(nr, n=2) {
12
+ return `${nr}`.padStart(n, `0`);
13
+ }
14
+
15
+ function randomNr(max, min = 0) {
10
16
  [max, min] = [Math.floor(max), Math.ceil(min)];
11
17
  return Math.floor( ([...crypto.getRandomValues(new Uint32Array(1))].shift() / 2 ** 32 ) * (max - min + 1) + min );
12
- };
13
- const isNonEmptyString = function(str, minlen = 1) {
18
+ }
19
+
20
+ function isNonEmptyString(str, minlen = 1) {
14
21
  minlen = IS(minlen, Number) && minlen || 1;
15
22
  return IS(str, String) && str.length >= minlen;
16
- };
17
- const resolveEventTypeParameter = function(maybeTypes) {
23
+ }
24
+
25
+ function resolveEventTypeParameter (maybeTypes) {
18
26
  maybeTypes = IS(maybeTypes, String) && /,/.test(maybeTypes) ? maybeTypes.split(`,`) : maybeTypes;
19
27
  return IS(maybeTypes, Array)
20
28
  ? maybeTypes.filter(t => isNonEmptyString(t)).map(t => t.trim().toLowerCase())
21
29
  : IS(maybeTypes, String) && maybeTypes?.trim().toLowerCase() || ``;
22
- };
23
- const shuffle = array => {
30
+ }
31
+
32
+ function shuffle(array) {
24
33
  let i = array.length;
25
34
  while (i--) {
26
35
  const ri = randomNr(i);
27
36
  [array[i], array[ri]] = [array[ri], array[i]];
28
37
  }
29
38
  return array;
30
- };
31
- const hex2Full = hex => {
39
+ }
40
+
41
+ function hex2Full(hex) {
32
42
  hex = (hex.trim().startsWith("#") ? hex.slice(1) : hex).trim();
33
43
  return hex.length === 3 ? [...hex].map(v => v + v).join("") : hex;
34
- };
35
- const truncateHtmlStr = (str, maxLength = 120) => `${str}`.trim()
36
- .slice(0, maxLength)
37
- .replace(/>\s+</g, `><`)
38
- .replace(/</g, `&lt;`)
39
- .replace(/\s{2,}/g, ` `)
40
- .replace(/\n/g, `\\n`) + (str.length > maxLength ? ` &hellip;` : ``).trim();
41
- const toDashedNotation = str2Convert =>str2Convert.replace(/[A-Z]/g, a => `-${a.toLowerCase()}`).replace(/^-|-$/, ``);
42
- const ucFirst = ([first, ...theRest]) => `${first.toUpperCase()}${theRest.join(``)}`;
43
- const toCamelcase = str2Convert =>
44
- IS(str2Convert, String) ? str2Convert.toLowerCase()
45
- .split(`-`)
46
- .map( (str, i) => i && `${ucFirst(str)}` || str)
47
- .join(``) : str2Convert;
48
- const randomString = () => `_${shuffle(characters4RandomString).slice(0, 8).join(``)}`;
49
- const truncate2SingleStr = (str, maxLength = 120) =>
50
- truncateHtmlStr(str, maxLength).replace(/&lt;/g, `<`);
51
- const logTime = () => ((d) =>
52
- `[${pad0(d.getHours())}:${pad0(d.getMinutes())}:${
53
- pad0(d.getSeconds())}.${pad0(d.getMilliseconds(), 3)}]`)(new Date());
54
- const hex2RGBA = function (hex, opacity = 100) {
44
+ }
45
+
46
+ function truncateHtmlStr(str, maxLength = 120) {
47
+ return `${str}`
48
+ .trim()
49
+ .slice(0, maxLength)
50
+ .replace(/>\s+</g, `><`)
51
+ .replace(/</g, `&lt;`)
52
+ .replace(/\s{2,}/g, ` `)
53
+ .replace(/\n/g, `\\n`) + (str.length > maxLength ? ` &hellip;` : ``).trim();
54
+ }
55
+
56
+ function toDashedNotation(str2Convert) {
57
+ return str2Convert.replace(/[A-Z]/g, a => `-${a.toLowerCase()}`).replace(/^-|-$/, ``);
58
+ }
59
+
60
+ function ucFirst([first, ...theRest]) {
61
+ return `${first.toUpperCase()}${theRest.join(``)}`;
62
+ }
63
+
64
+ function toCamelcase(str2Convert) {
65
+ return IS(str2Convert, String)
66
+ ? str2Convert.toLowerCase()
67
+ .split(`-`)
68
+ .map( (str, i) => i && `${ucFirst(str)}` || str)
69
+ .join(``)
70
+ : str2Convert;
71
+ }
72
+
73
+ function randomString() {
74
+ return `_${shuffle(characters4RandomString).slice(0, 8).join(``)}`;
75
+ }
76
+
77
+ function truncate2SingleStr(str, maxLength = 120) {
78
+ return truncateHtmlStr(str, maxLength).replace(/&lt;/g, `<`);
79
+ }
80
+
81
+ function logTime() {
82
+ return ((d) =>
83
+ `[${pad0(d.getHours())}:${pad0(d.getMinutes())}:${
84
+ pad0(d.getSeconds())}.${pad0(d.getMilliseconds(), 3)}]`)(new Date());
85
+ }
86
+
87
+ function hex2RGBA(hex, opacity = 100) {
55
88
  hex = hex2Full(hex.slice(1));
56
89
  const op = opacity % 100 !== 0;
57
90
  return `rgb${op ? "a" : ""}(${
58
91
  parseInt(hex.slice(0, 2), 16)}, ${
59
92
  parseInt(hex.slice(2, 4), 16)}, ${
60
93
  parseInt(hex.slice(-2), 16)}${op ? `, ${opacity / 100}` : ""})`;
61
- };
62
- const escHtml = html => html.replace(/</g, `&lt;`);
94
+ }
95
+
96
+ function escHtml(html) {
97
+ return html.replace(/</g, `&lt;`).replace(/>/g, `&gt;`);
98
+ }
99
+
100
+ function extensionHelpers() {
101
+ const isCommentOrTextNode = elem => IS(elem, Comment, Text);
102
+ const isNode = input => IS(input, Text, HTMLElement, Comment);
103
+ const isComment = input => IS(input, Comment);
104
+ const isText = input => IS(input, Text);
105
+ const isHtmlString = input => IS(input, String) && /^<|>$/.test(`${input}`.trim());
106
+ const isArrayOfHtmlStrings = input => IS(input, Array) && !input?.find(s => !isHtmlString(s));
107
+ const isArrayOfHtmlElements = input => IS(input, Array) && !input?.find(el => !isNode(el));
108
+ const ElemArray2HtmlString = elems => elems?.filter(el => el).reduce((acc, el) =>
109
+ acc.concat(isComment(el) ? `<!--${el.data}-->`
110
+ : isCommentOrTextNode(el) ? el.textContent
111
+ : el.outerHTML), ``);
112
+ const input2Collection = input =>
113
+ !input ? []
114
+ : IS(input, Proxy) ? [input.EL]
115
+ : IS(input, NodeList) ? [...input]
116
+ : isNode(input) ? [input]
117
+ : isArrayOfHtmlElements(input) ? input
118
+ : input.isJQx ? input.collection : undefined;
119
+ const setCollectionFromCssSelector = (input, root, self) => {
120
+ const selectorRoot = root !== document.body && (IS(input, String) && input.toLowerCase() !== "body") ? root : document;
121
+ let errorStr = undefined;
122
+
123
+ try { self.collection = [...selectorRoot.querySelectorAll(input)]; }
124
+ catch (err) { errorStr = `Invalid CSS querySelector. [${!IS(input, String) ? `Nothing valid given!` : input}]`; }
125
+
126
+ return errorStr ?? `CSS querySelector "${input}", output ${self.collection.length} element(s)`;
127
+ };
128
+ const addHandlerId = instance => {
129
+ const handleId = instance.data.get(`hid`) || `HID${randomString()}`;
130
+ instance.data.add({hid: handleId});
131
+ return `[data-hid="${handleId}"]`;
132
+ };
133
+
134
+ return {
135
+ isCommentOrTextNode, isNode, isComment, isText, isHtmlString, isArrayOfHtmlElements,
136
+ isArrayOfHtmlStrings, ElemArray2HtmlString, input2Collection, setCollectionFromCssSelector,
137
+ addHandlerId };
138
+ }
63
139
 
64
140
  function ExamineElementFeatureFactory() {
65
141
  const isVisible = function(el) {
@@ -116,6 +192,41 @@ function ExamineElementFeatureFactory() {
116
192
  };
117
193
  }
118
194
 
195
+ function decodeForConsole(something) {
196
+ return IS(something, String) &&
197
+ Object.assign(document.createElement(`textarea`), {innerHTML: something}).textContent || something;
198
+ }
199
+
200
+ function systemLogFactory() {
201
+ let on = false;
202
+ const backLog = [];
203
+ const systemLogger = {
204
+ get on() { on = true; return systemLogger; },
205
+ get off() { on = false; return systemLogger; },
206
+ get backLog() { return backLog; },
207
+ };
208
+
209
+ function error(...args) {
210
+ backLog.unshift(...args.map(arg => `${logTime()} ⨻ ${decodeForConsole(arg)}`));
211
+ console.error(backLog.slice(0, args.length).join(`\n`));
212
+ return systemLogger;
213
+ }
214
+
215
+ function log(...args) {
216
+ backLog.unshift(...args.map(arg => `${logTime()} ✔ ${decodeForConsole(arg)}`));
217
+ if (!on) { return systemLogger; }
218
+ console.log(backLog.slice(0, args.length).join(`\n`));
219
+ return systemLogger;
220
+ }
221
+
222
+ Object.defineProperties(systemLogger, {
223
+ log: {value: log, enumerable: false},
224
+ error: {value: error, enumerable: false},
225
+ });
226
+
227
+ return Object.freeze(systemLogger);
228
+ }
229
+
119
230
  export {
120
231
  IS,
121
232
  maybe,
@@ -133,4 +244,6 @@ export {
133
244
  styleFactory,
134
245
  tagFNFactory,
135
246
  resolveEventTypeParameter,
247
+ extensionHelpers,
248
+ systemLog,
136
249
  };
package/src/JQxLog.js DELETED
@@ -1,172 +0,0 @@
1
- import { logTime } from "./Utilities.js";
2
- import { createElementFromHtmlString, element2DOM, insertPositions } from "./DOM.js";
3
- import { logStyling } from "./EmbedResources.js";
4
-
5
- function logFactory(jqx) {
6
- let logSystem = false;
7
- let useLogging = false;
8
- let log2Console = true;
9
- let reverseLogging = false;
10
- let useHtml = true;
11
- let editLogRules;
12
- const getLogBox = () => jqx(`#logBox`);
13
- const logBoxTextBoxId = `#jqx_logger`;
14
- const setStyling4Log = setStyle => {
15
- logStyling?.forEach(selector => setStyle(selector));
16
- };
17
-
18
- const decodeForConsole = something => jqx.IS(something, String) &&
19
- Object.assign(document.createElement(`textarea`), {innerHTML: something}).textContent || something;
20
-
21
- function createLogElement(jqx) {
22
- // note: jqx is not fully proxified here ...
23
- const jqx_logger_element_name = useHtml ? `div` : `pre`;
24
- const loggingFieldSet = `
25
- <div id="logBox">
26
- <div class="legend">
27
- <div><!--legend text--></div>
28
- </div>
29
- <${jqx_logger_element_name} id="jqx_logger"></${jqx_logger_element_name}>`;
30
- const logBoxElement = createElementFromHtmlString(loggingFieldSet);
31
- element2DOM(logBoxElement, undefined, insertPositions.AfterBegin);
32
- return jqx.node(logBoxTextBoxId);
33
- }
34
-
35
- const Log = (...args) => {
36
- const isInstanceLog = args[0] === `fromStatic`;
37
-
38
- if (!jqx.node(`#JQxLogCSS`)) {
39
- editLogRules = jqx.editStylesheet(`JQxLogCSS`);
40
- for (const rule of logStyling) { editLogRules(rule); }
41
- }
42
-
43
- args = isInstanceLog ? args.slice(1) : args;
44
-
45
- if (isInstanceLog && !useLogging) {
46
- return args.forEach(arg => console.info(`${logTime()} ✔ ${decodeForConsole(arg)}`));
47
- }
48
-
49
- if (!useLogging) { return; }
50
-
51
- if (!log2Console && !jqx.node(`#logBox`)) { createLogElement(jqx); }
52
-
53
- const logLine = arg => `${jqx.IS(arg, Object) ? JSON.stringify(arg, null, 2) : arg}\n`;
54
-
55
- args.forEach(arg => log2Console
56
- ? console.info(`${logTime()} ✔ ${decodeForConsole(arg)}`)
57
- : jqx.node(`#jqx_logger`).insertAdjacentHTML(
58
- reverseLogging ? `afterbegin` : `beforeend`,
59
- `<div class="entry">${logTime()} ${logLine(arg.replace(/\n/g, `<br>`))}</div>`)
60
- );
61
- };
62
-
63
- const logActive = {
64
- on() {
65
- useLogging = true;
66
- Log(`Logging activated (CSS in style#JQxLogCSS)`);
67
- },
68
- off() {
69
- useLogging = false;
70
- console.log(`${logTime()} ✔ Logging deactivated`)
71
- },
72
- }
73
-
74
- const setSystemLog = {
75
- on() {
76
- logSystem = true;
77
- },
78
- off() {
79
- logSystem = false;
80
- },
81
- };
82
-
83
- const systemLog = (...logTxt) => logSystem && Log(...logTxt);
84
-
85
- const debugLog = {
86
- get isConsole() {
87
- return log2Console === true;
88
- },
89
- get isOn() {
90
- return useLogging;
91
- },
92
- isVisible: function () {
93
- return jqx(`#jqx_logger`).is(`visible`);
94
- },
95
- on() {
96
- logActive.on();
97
- setSystemLog.on();
98
- if (!log2Console) {
99
- getLogBox()?.addClass(`visible`);
100
- }
101
- Log(`Debug logging started. Every call to [jqx instance] is logged`);
102
- return debugLog;
103
- },
104
- off() {
105
- if (!getLogBox().isEmpty) {
106
- setSystemLog.off();
107
- Log(`Debug logging stopped`);
108
- getLogBox()?.removeClass(`visible`);
109
- }
110
- logActive.off();
111
- return debugLog;
112
- },
113
- toConsole: {
114
- on() {
115
- log2Console = true;
116
- Log(`Started logging to console`);
117
- return debugLog;
118
- },
119
- off() {
120
- Log(`Stopped logging to console (except error messages)`);
121
- log2Console = false;
122
- return debugLog;
123
- }
124
- },
125
- remove() {
126
- logActive.off();
127
- setSystemLog.off();
128
- getLogBox()?.remove();
129
- console.clear();
130
- console.log(`${logTime()} logging completely disabled and all entries removed`);
131
- return debugLog;
132
- },
133
- log: function (...args) {
134
- Log(...args);
135
- return debugLog;
136
- },
137
- hide() {
138
- getLogBox()?.removeClass(`visible`);
139
- return debugLog;
140
- },
141
- show: () => {
142
- getLogBox()?.addClass(`visible`);
143
- return debugLog;
144
- },
145
- get reversed() {
146
- return {
147
- on: () => {
148
- reverseLogging = true;
149
- Log(`Reverse logging set: now logging bottom to top (latest first)`);
150
- jqx(`#logBox .legend`).addClass(`reversed`);
151
- return debugLog;
152
- },
153
- off: () => {
154
- reverseLogging = false;
155
- jqx(`#logBox .legend`).removeClass(`reversed`);
156
- Log(`Reverse logging reset: now logging chronological (latest last)`);
157
- return debugLog;
158
- },
159
- };
160
- },
161
- clear() {
162
- jqx(logBoxTextBoxId).text(``);
163
- console.clear();
164
- Log(`Logging cleared`);
165
- return debugLog;
166
- }
167
- };
168
-
169
- return { Log, debugLog, systemLog };
170
- }
171
-
172
- export default logFactory;