objs-core 1.1.1 → 2.0.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.
package/objs.1.1.js DELETED
@@ -1,1205 +0,0 @@
1
- /**
2
- * @function Objs
3
- * DOM Modify and state control
4
- *
5
- * @version 1.1
6
- * @author Roman Torshin
7
- * @copyright
8
- * Apache-2.0 license *
9
- *
10
- * @param {any} query Selector, DOM element to use, an array of elements, inited ID or nothing for creating an element
11
- * @return {object} Objs
12
- */
13
-
14
- const o = (query) => {
15
- let result = {els: [], ie: {}},
16
- ZERO = 0,
17
- ONE = 1,
18
- TWO = 2,
19
- THREE = 3,
20
- objectType = 'object',
21
- functionType = 'function',
22
- reactProp = 'dangerouslySetInnerHTML',
23
- u = undefined,
24
- D = document,
25
- start = -1,
26
- finish = 0,
27
- select = 0,
28
- initedStates = [],
29
- i = 0, j = 0;
30
-
31
-
32
-
33
-
34
-
35
- // shorten typeof
36
- const type = obj => typeof obj;
37
- // cycle from object
38
- const cycleObj = (obj, func) => {for (const item in obj) if (Object.hasOwnProperty.call(obj, item)) func(item, obj)};
39
- // error function
40
- const error = o.onError || (() => {return});
41
- // values returner
42
- const returner = (f) => {
43
- return (...a) => {
44
- try {
45
- const res = f(a[ZERO], a[ONE], a[TWO], a[THREE]);
46
- return res !== u ? res : result
47
- } catch (err) {
48
- error(err)
49
- }
50
- };
51
- };
52
- // running for each element
53
- const iterator = (f) => {for (i = finish; i <= start; i++) f()};
54
- // getting element from string
55
- const toEl = (el) => {
56
- if (type(el) !== objectType)
57
- el = o.first(el).el;
58
- return el;
59
- };
60
- // setting properties
61
- const setResultVals = (clearStates = true, els = result.els) => {
62
- const ln = els.length;
63
- result.length = ln;
64
- start = ln - ONE;
65
- finish = ZERO;
66
- result.el = ln ? els[ZERO] : u;
67
- result.last = ln ? els[start] : u;
68
- if (clearStates) {
69
- cycleObj(initedStates, (i, state) => {
70
- delete result[state[i]];
71
- });
72
- initedStates = [];
73
- result.ie = {};
74
- }
75
- };
76
- // getting element by query
77
- const getObjs = (innerQuery = '') => {return Array.from(D.querySelectorAll(innerQuery))};
78
- // sets new objects to operate
79
- result.reset = o;
80
-
81
-
82
-
83
-
84
-
85
- /**
86
- * Transformation of DOM elements
87
- *
88
- * @param {object} el DOM element for transformation
89
- * @param {object} state States data
90
- * @param {object} props additional props and dynamic content
91
- */
92
- const transform = (el, state, props) => {
93
- cycleObj(state, (s) => {
94
- let value = state[s];
95
-
96
- // eval functions in attributes
97
- if (type(value) === functionType) {
98
- value = value(props);
99
- }
100
-
101
- // prepare objs to append
102
- if (s === 'append' && type(value) === objectType) {
103
- if (value.els) {
104
- value = [value];
105
- }
106
- if (value[ZERO] && value[ZERO].els) {
107
- valueBuff = [];
108
- cycleObj(value, (i) => {
109
- valueBuff.push(...value[i].els);
110
- });
111
- value = valueBuff;
112
- }
113
- }
114
-
115
- if (value !== u) {
116
- // skip these
117
- ['tag','sample','state'].includes(s) ? '' :
118
- // insert html
119
- ['html','innerHTML'].includes(s) ? el.innerHTML = value :
120
- // attach dataset
121
- s === 'dataset' && type(value) === objectType ?
122
- cycleObj(value, (data) => {
123
- el.dataset[data] = value[data];
124
- }) :
125
- // classes
126
- s === 'toggleClass' ? el.classList.toggle(value) :
127
- s === 'addClass' ? (type(value) === objectType ? el.classList.add(...value) : el.classList.add(value)) :
128
- s === 'removeClass' ? el.classList.remove(value) :
129
- // style attribute
130
- s === 'style' && type(value) === objectType ?
131
- cycleObj(value, (data) => {
132
- el.style[data] = value[data];
133
- }) :
134
- // append DOM objects
135
- s === 'append' && type(value) === objectType ?
136
- cycleObj(value.length ? value : [value], (j) => {
137
- el.appendChild(value[j]);
138
- }) :
139
- // set attributes
140
- el.setAttribute(s, value);
141
- }
142
- });
143
-
144
- el.dataset['oState'] = state.state;
145
- };
146
-
147
- /**
148
- * Creates states functions
149
- *
150
- * @param {object} states States object
151
- */
152
- result.init = returner((states) => {
153
- const initN = result.initID || o.inits.length || ZERO;
154
- result.initID = initN;
155
- setResultVals();
156
- o.inits[result.initID] = result;
157
-
158
- // fast initialisation
159
- if (type(states) !== objectType || states.render === u) {
160
- states = {
161
- render: states,
162
- };
163
- }
164
-
165
- // cycle threw states
166
- cycleObj(states, (state) => {
167
- // save state name to clear object by reset();
168
- initedStates.push(state);
169
- // add method named as state
170
- result[state] = returner((props = [{}]) => {
171
- let data = states[state] || {tag: 'div'};
172
- const els = result.els.slice(finish, start + ONE);
173
-
174
- if (type(data) === objectType) {
175
- data.state = state;
176
- data['data-o-init'] = initN;
177
- }
178
-
179
- // creation elements for prop in props
180
- const newEl = (n, prop = {}) => {
181
- if (type(data) === objectType) {
182
- return D.createElement(data.tag || 'div');
183
- } else {
184
- i = D.createElement('div');
185
- i.innerHTML = type(data) === functionType ? data(prop) : data;
186
- if (i.children.length > ONE || !i.firstElementChild) {
187
- i.dataset.oInit = n;
188
- return i;
189
- } else {
190
- i.firstElementChild.dataset.oInit = n;
191
- return i.firstElementChild;
192
- }
193
- }
194
- };
195
-
196
- // properties creation
197
- !props.length ? props = [props] : '';
198
-
199
- // creating elements if no one was selected
200
- const creation = !els[ZERO] && state === 'render';
201
- props = props.map((prop, i) => {
202
- prop.self = result;
203
- prop.o = o;
204
- prop.i = prop.i === u ? i : prop.i;
205
- if (creation) {
206
- els.push(newEl(initN, prop));
207
- }
208
- return prop;
209
- });
210
- if (creation) {
211
- result.els = els;
212
- setResultVals(false);
213
- }
214
-
215
- // changing element if there is data object
216
- if (els) {
217
- j = els.length === props.length;
218
- els.map((el, i) => {
219
- props[j ? i : ZERO].i = i + finish;
220
- const buff = type(data) === functionType ? data(props[j ? i : ZERO]) : data;
221
- if (type(buff) === objectType) {
222
- transform(
223
- el,
224
- buff,
225
- props[j ? i : ZERO]
226
- );
227
- }
228
- });
229
- }
230
- });
231
- });
232
- });
233
-
234
- result.initState = returner((state, props) => {
235
- result.init(state).render(props);
236
- });
237
-
238
- /**
239
- * Gets state object from existing DOM element
240
- *
241
- * @param {string} state title, optional
242
- *
243
- * @return {object} state
244
- */
245
- result.sample = returner((state = 'render') => {
246
- const attrs = result.els[finish].attributes,
247
- ds = result.els[finish].dataset,
248
- res = {
249
- tag: result.els[finish].tagName,
250
- html: result.els[finish].innerHTML,
251
- dataset: {},
252
- };
253
-
254
- for (const attr of attrs) {
255
- if (attr.nodeName.substring(ZERO, 5) !== 'data-') {
256
- res[attr.nodeName] = attr.value;
257
- }
258
- }
259
-
260
- cycleObj(ds, (data) => {
261
- res.dataset[data] = ds[data];
262
- });
263
-
264
- return {[state]: res};
265
- });
266
-
267
- /**
268
- * Select element to control
269
- *
270
- * @param {number} i index, last one if undefined
271
- */
272
- result.select = returner((i) => {
273
- if (i === u) {
274
- i = result.length - ONE;
275
- }
276
- start = i;
277
- finish = i;
278
- result.el = result.els[i];
279
- select = ONE;
280
- });
281
-
282
- /**
283
- * Select all elements to control
284
- */
285
- result.all = returner(() => {
286
- start = result.length - ONE;
287
- finish = ZERO;
288
- result.el = result.els[ZERO];
289
- select = ZERO;
290
- });
291
-
292
- /**
293
- * Remove selected element or all from DOM
294
- *
295
- * @param {number} j index, removes all if undefined
296
- */
297
- result.remove = returner((j) => {
298
- if (j === u && select) {
299
- j = finish;
300
- }
301
-
302
- if (j !== u) {
303
- result.els[j].parentNode.removeChild(result.els[j]);
304
- } else {
305
- iterator(() => {
306
- result.els[i].parentNode.removeChild(result.els[i]);
307
- });
308
- }
309
- setResultVals(false);
310
- });
311
-
312
- /**
313
- * Delete j, selected or the first element from control list
314
- */
315
- result.skip = returner((j) => {
316
- if (j === u) {
317
- j = finish;
318
- }
319
-
320
- result.els.splice(i, ONE);
321
- setResultVals();
322
- });
323
-
324
- /**
325
- * Add element to control list
326
- */
327
- result.add = returner((el,l) => {
328
- if (type(el) === 'string' && el !== '') {
329
- result.els.push(...getObjs(el));
330
- } else if (type(el) === objectType) {
331
- if (el.tagName) {
332
- result.els.push(el);
333
- } else if (el.els) {
334
- result.els.push(...el.els);
335
- } else if (el.length && el[ZERO].tagName) {
336
- result.els.push(...el);
337
- }
338
- } else if (type(el) === 'number' && o.inits[el]) {
339
- result = o.inits[el];
340
- }
341
-
342
- setResultVals(false);
343
-
344
- if (result.initID !== u) {
345
- result.dataset({'oInit': result.initID});
346
- }
347
- });
348
-
349
- /**
350
- * Functions to insert elements into DOM
351
- */
352
- result.appendInside = returner((el) => {
353
- iterator(() => {
354
- toEl(el).appendChild(result.els[i]);
355
- });
356
- });
357
- result.appendBefore = returner((el) => {
358
- iterator(() => {
359
- toEl(el).parentNode.insertBefore(result.els[i], toEl(el));
360
- });
361
- });
362
- result.appendAfter= returner((el) => {
363
- iterator(() => {
364
- toEl(el).after(...result.els);
365
- });
366
- });
367
-
368
- /**
369
- * Find child elements
370
- */
371
- result.find = returner((innerQuery = '') => {
372
- const newEls = [];
373
-
374
- iterator(() => {
375
- newEls.push(...Array.from(result.els[i].querySelectorAll(':scope ' + innerQuery)));
376
- });
377
-
378
- return o(newEls);
379
- });
380
-
381
- /**
382
- * Find the first child element by query
383
- */
384
- result.first = returner((innerQuery = '') => {
385
- let buff = u;
386
- const newEls = [];
387
-
388
- iterator(() => {
389
- buff = result.els[i].querySelector(innerQuery);
390
- if (buff) {
391
- newEls.push(buff);
392
- }
393
- });
394
-
395
- return o(newEls);
396
- });
397
-
398
- /**
399
- * Set, delete or get attribute
400
- */
401
- result.attr = returner((attr, val) => {
402
- if (attr) {
403
- if (val === u) {
404
- const attrs = [];
405
- iterator(() => {
406
- attrs[i] = result.els[i].getAttribute(attr);
407
- });
408
- return select ? attrs[ZERO] : attrs;
409
- } else if (val !== '') {
410
- iterator(() => {
411
- result.els[i].setAttribute(attr, val);
412
- });
413
- } else {
414
- iterator(() => {
415
- result.els[i].removeAttribute(attr);
416
- });
417
- }
418
- }
419
- });
420
- /**
421
- * Get all attributes
422
- */
423
- result.attrs = returner(() => {
424
- const res = [];
425
- iterator(() => {
426
- const obj = {};
427
- [...result.els[i].attributes].forEach((attr) => {
428
- obj[attr.nodeName] = attr.nodeValue;
429
- });
430
- res.push(obj);
431
- });
432
- return select ? res[ZERO] : res;
433
- });
434
- /**
435
- * Dataset control
436
- */
437
- result.dataset = returner((values) => {
438
- if (typeof values === objectType) {
439
- iterator(() => {
440
- cycleObj(values, (data) => {
441
- result.els[i].dataset[data] = values[data];
442
- });
443
- });
444
- } else {
445
- const res = [];
446
- iterator(() => {
447
- res.push({...result.els[i].dataset});
448
- });
449
- return select ? res[ZERO] : res;
450
- }
451
- });
452
- /**
453
- * Style attribute
454
- */
455
- result.style = returner((val) => {
456
- result.attr('style', val);
457
- });
458
- /**
459
- * CSS as object to create style attribute
460
- */
461
- result.css = returner((styles = {}) => {
462
- let val = '';
463
-
464
- cycleObj(styles, (style) => {
465
- val += style + ':' + styles[style].replace('"', "'") + ';'
466
- });
467
-
468
- result.style(val)
469
- });
470
- /**
471
- * Class control
472
- */
473
- result.setClass = returner((cl) => {
474
- iterator(() => {
475
- result.els[i].setAttribute('class', cl)
476
- })
477
- });
478
- result.addClass = returner((cl) => {
479
- iterator(() => {
480
- result.els[i].classList.add(cl)
481
- })
482
- });
483
- result.removeClass = returner((cl) => {
484
- iterator(() => {
485
- result.els[i].classList.remove(cl)
486
- })
487
- });
488
- result.toggleClass = returner((cl, check) => {
489
- iterator(() => {
490
- result.els[i].classList.toggle(cl, check)
491
- })
492
- });
493
- result.haveClass = (cl) => {
494
- let res = true;
495
- iterator(() => {
496
- if (!result.els[i].classList.contains(cl)) {
497
- res = false;
498
- }
499
- });
500
- return res;
501
- };
502
- /**
503
- * Inner content control
504
- */
505
- result.innerHTML = returner((html) => {
506
- if (html !== u) {
507
- iterator(() => {
508
- result.els[i].innerHTML = html;
509
- });
510
- } else {
511
- let res = '';
512
- iterator(() => {
513
- res += result.els[i].innerHTML;
514
- });
515
- return res;
516
- }
517
- });
518
- result.innerText = returner((text = '') => {
519
- iterator(() => {
520
- result.els[i].innerText = text;
521
- });
522
- });
523
- result.textContent = returner((text = '') => {
524
- iterator(() => {
525
- result.els[i].textContent = text;
526
- });
527
- });
528
-
529
- /**
530
- * Gets HTML of DOM operated elements
531
- *
532
- * @returns {string}
533
- */
534
- result.html = returner((value) => {
535
- if (value) {
536
- result.innerHTML(value);
537
- } else {
538
- let html = '';
539
-
540
- iterator(() => {
541
- html += result.els[i].outerHTML;
542
- });
543
-
544
- return html;
545
- }
546
- });
547
-
548
- result.forEach = returner((f) => {
549
- iterator(() => {
550
- f({self: result, i, o, el: result.els[i]});
551
- });
552
- });
553
-
554
- /**
555
- * Transform to React Element or Component (if defined)
556
- *
557
- * @param {object} React or React.createElement to get Element
558
- * @param {object} React.Component to get Component
559
- * @returns {object}
560
- */
561
- result.prepareFor = returner((createElement, ReactComp) => {
562
- if (createElement.createElement) {
563
- createElement = createElement.createElement;
564
- }
565
- const getElement = () => {
566
- if (result.length === ONE) {
567
- return createElement(result.el.tagName.toLowerCase(), {
568
- ...result.attrs()[ZERO],
569
- [reactProp]: {__html: result.innerHTML()},
570
- });
571
- } else {
572
- return createElement('div', {[reactProp]: {__html: result.html()}});
573
- }
574
- };
575
- if (!ReactComp) {
576
- return getElement();
577
- } else {
578
- return class extends ReactComp {
579
- render() {
580
- return getElement();
581
- }
582
- };
583
- }
584
- });
585
-
586
-
587
-
588
-
589
-
590
- /**
591
- * Event functions
592
- */
593
- result.on = returner((a, b, c, d) => {
594
- a.split(', ').forEach((ev) => {
595
- iterator(() => {
596
- result.els[i].addEventListener(ev, b, c, d);
597
- });
598
- if (!result.ie[ev]) {
599
- result.ie[ev] = [];
600
- }
601
- result.ie[ev].push([b, c, d]);
602
- });
603
- });
604
- result.off = returner((a, b, c) => {
605
- a.split(', ').forEach((ev) => {
606
- iterator(() => {
607
- result.els[i].removeEventListener(ev, b, c);
608
- });
609
- if (result.ie[ev]) {
610
- result.ie[ev] = result.ie[ev].filter(f => f[ZERO] !== b);
611
- }
612
- });
613
- });
614
- /**
615
- * On and off listeners
616
- */
617
- result.onAll = returner((type, off) => {
618
- cycleObj(result.ie, (ev, events) => {
619
- if (!type || type === ev) {
620
- events[ev].forEach((data) => {
621
- iterator(() => {
622
- if (off) {
623
- result.els[i].removeEventListener(ev, data[ZERO]);
624
- } else {
625
- result.els[i].addEventListener(ev, data[ZERO], data[ONE], data[TWO]);
626
- }
627
- });
628
- });
629
- }
630
- });
631
- });
632
- result.offAll = returner((type) => {
633
- result.onAll(type, ONE);
634
- });
635
-
636
- /**
637
- * Making result object
638
- */
639
- if (query)
640
- result.add(query);
641
-
642
- result.take = (innerQuery) => {
643
- result.add(innerQuery);
644
-
645
- if (result.el) {
646
- const initID = result.el.dataset['oInit'];
647
-
648
- if (initID !== u && o.inits[initID]) {
649
- if (result.length === ONE) {
650
- j = result.els[ZERO];
651
- Object.assign(result, o.inits[initID]);
652
- result.els = [j];
653
- } else {
654
- result = o.inits[initID];
655
- }
656
- setResultVals(false, result.els);
657
- return result;
658
- }
659
- }
660
- };
661
-
662
- return result;
663
- };
664
-
665
-
666
-
667
-
668
-
669
-
670
-
671
-
672
-
673
-
674
-
675
-
676
-
677
-
678
-
679
-
680
-
681
-
682
-
683
- o.first = (query) => {
684
- return o(document.querySelector(query) || '');
685
- };
686
-
687
- o.inits = [];
688
- o.errors = [];
689
- o.showErrors = false;// on and off errors
690
- o.logErrors = () => {o.errors.length ? o.errors.forEach((e) => console.log(e)) : console.log('No errors')};
691
- o.onError = (e) => {o.showErrors ? console.log(e) : o.errors.push(e)};// function for errors
692
-
693
- /**
694
- * Creating elements from state
695
- *
696
- * @param {object} states State, states or function/string
697
- */
698
- o.init = (states) => {
699
- return o().init(states);
700
- };
701
- o.initState = (state, props) => {
702
- return o().init(state).render(props);
703
- };
704
- o.take = (query) => {
705
- return o().take(query);
706
- };
707
-
708
- // Short values
709
- o.Z = 0; o.N = 1; o.W = 2; o.H = 100; o.F = false;
710
- o.C = (a, b) => {
711
- return Object.hasOwnProperty.call(a, b);
712
- };
713
-
714
-
715
-
716
-
717
-
718
-
719
-
720
-
721
-
722
-
723
-
724
-
725
-
726
-
727
-
728
-
729
-
730
-
731
- /**
732
- * GET and POST
733
- *
734
- * @argument {string} url link
735
- * @argument {object} props parameters
736
- *
737
- * @returns {promise}
738
- */
739
- o.ajax = (url, props = {}) => {
740
- let row = new URLSearchParams();
741
- if (props.data && typeof props.data === 'object') {
742
- for (const param in props.data) {
743
- if (o.C(props.data, param)) {
744
- if (typeof props.data[param] === 'object') {
745
- row.set(param, encodeURIComponent(JSON.stringify(props.data[param])));
746
- } else {
747
- row.set(param, props.data[param]);
748
- }
749
- }
750
- }
751
- if (props.method.toLowerCase() === 'get') {
752
- url += '?' + row.toString();
753
- } else if (!props.body) {
754
- props.body = row;
755
- }
756
- delete props.data;
757
- }
758
- if (!props.headers) {
759
- props.headers = {'X-Requested-With': 'XMLHttpRequest'};
760
- }
761
-
762
- return fetch(url, props);
763
- };
764
- o.get = (url, props = {}) => {
765
- return o.ajax(url, {...props, method: 'GET'});
766
- };
767
- o.post = (url, props = {}) => {
768
- return o.ajax(url, {...props, method: 'POST'});
769
- };
770
- o.getParams = (key) => {
771
- const params = {};
772
- const paramsRaw = new URLSearchParams(window.location.search).entries();
773
-
774
- for (const entry of paramsRaw) {
775
- params[entry[o.Z]] = entry[o.N];
776
- }
777
-
778
- return key ? params[key] : params;
779
- };
780
-
781
-
782
-
783
-
784
-
785
-
786
-
787
-
788
-
789
-
790
-
791
-
792
-
793
-
794
-
795
-
796
-
797
-
798
- /**
799
- * Include functions
800
- *
801
- * @param {object} sources array of name:src values
802
- * @param {function} callBack function for success
803
- * @param {function} callBad function for error
804
- *
805
- * @returns {bool}
806
- */
807
-
808
- /* parameters */
809
- o.incCache = true;// cache in localStorage
810
- o.incCacheExp = 1000 * 60 * 60 * 24;// cache time
811
- o.incTimeout = 6000;// ms for timing to load function
812
- o.incSource = '';// link to source folder
813
- o.incForce = o.F;// reload loaded files or not
814
- o.incAsync = true;// async or in order loading
815
- o.incCors = o.F;// allow loading from other domains
816
-
817
- /* service values */
818
- o.incFns = {};// array of name:status for all functions
819
- o.incSet = [o.Z];// saving callbacks and change them for 1 value if executed
820
- o.incReady = [o.Z];// array of all included sets and statuses
821
- o.incN = o.Z;// index of the next set
822
-
823
- /**
824
- * Check the state status or a function in it, also checks its status to true
825
- *
826
- * @param {number} set index
827
- * @param {number} fnId index
828
- * @returns {boolean}
829
- */
830
- o.incCheck = (set = 0, fnId, loaded = 0) => {
831
- if (!loaded && set && fnId === o.U && o.incReady[set]) {
832
- return o.incSet[set] === o.N;
833
- }
834
-
835
- if (o.incReady[set] === o.U || o.incReady[set][fnId] === o.U) {
836
- return o.F;
837
- }
838
-
839
- o.incReady[set][fnId].loaded = loaded;
840
- o.incFns[o.incReady[set][fnId].name] = loaded;
841
- o.incReady[set][o.Z] += loaded;
842
-
843
- if (set && o.incReady[set].length === o.incReady[set][o.Z]) {
844
- if (typeof o.incSet[set] === 'function') {
845
- o.incSet[set](set);
846
- }
847
- o.incSet[set] = o.N;
848
- }
849
-
850
- return o.incSet[set] === o.N;
851
- };
852
- /* Clear all cache and all loaded sets info */
853
- o.incCacheClear = (all = o.F) => {
854
- for (const name in o.incFns) {
855
- if (o.C(o.incFns, name)) {
856
- localStorage.removeItem('inc-' + name);
857
- localStorage.removeItem('inc-' + name + 'Expires');
858
- }
859
- }
860
- if (all) {
861
- o.incReady.forEach((val, i) => {
862
- if (i) {
863
- val.forEach((a, j) => {
864
- if (j) {
865
- o('#oInc-' + i + '-' + j).remove();
866
- }
867
- });
868
- }
869
- });
870
-
871
- o.incN = o.Z
872
- o.incFns = {};
873
- o.incSet = [o.Z];
874
- o.incReady = [o.Z];
875
- }
876
- return true;
877
- };
878
- /* Main function to include */
879
- o.inc = (sources, callBack, callBad) => {
880
- let sourcesN = o.Z,
881
- sourcesReady = o.Z;
882
- const f = 'function',
883
- FOUR = 4,
884
- no = -1;
885
-
886
- if (typeof sources !== 'object' || !sources) {
887
- return o.incSet[o.Z];
888
- }
889
-
890
- o.incSet[o.Z]++;
891
- const setN = o.incSet[o.Z];
892
- o.incSet[setN] = callBack || o.Z;
893
- o.incReady[setN] = [];
894
- const fnsStatus = o.incReady[setN];
895
- fnsStatus[o.Z] = o.N;
896
- const fnId = {};
897
-
898
- for (let name in sources) {
899
- if (o.C(sources, name)) {
900
- sourcesN++;
901
- o.incN++;
902
-
903
- let tag = sources[name].indexOf('.css') > no ? 'style' : 'script';
904
- sources[name] = (o.incSource ? o.incSource + '/' : '') + sources[name];
905
- // skip loading if already loaded and not forced
906
- if (isNaN(name) && o.C(o.incFns, name) && o.incFns[name] && !o.incForce) {
907
- fnsStatus[sourcesN] = {
908
- name,
909
- loaded: o.N,
910
- };
911
- sourcesReady++;
912
- continue;
913
- }
914
-
915
- // fixing if loaded needed
916
- fnsStatus[sourcesN] = {
917
- name,
918
- loaded: o.Z,
919
- };
920
-
921
- if (isNaN(name)) {
922
- o.incFns[name] = o.Z;
923
- }
924
-
925
- if (
926
- isNaN(name) &&
927
- o.incCache &&
928
- sources[name].substring(o.Z, FOUR) !== 'http' &&
929
- window.location.protocol !== 'file:' &&
930
- (sources[name].indexOf('.css') > no || sources[name].indexOf('.js') > no)
931
- ) {
932
- // if cache is enabled
933
- const ls = localStorage,
934
- script = ls.getItem('inc-' + name),
935
- cacheSavedTill = ls.getItem('inc-' + name + 'Expires');
936
-
937
- if (
938
- script &&
939
- cacheSavedTill &&
940
- new Date().getTime() < cacheSavedTill
941
- ) {
942
- // load from cache
943
- o.initState({
944
- tag,
945
- id: 'oInc-' + setN + '-' + sourcesN,
946
- innerHTML: script,
947
- 'data-o-inc': setN,
948
- }).appendInside('head');
949
- fnsStatus[sourcesN].loaded = o.N;
950
- o.incFns[name] = o.N;
951
- sourcesReady++;
952
- } else {
953
- // loading and caching new files
954
- fnId[name] = sourcesN;
955
- o.get(sources[name], {mode: o.incCors ? 'cors' : 'same-origin'}).then((response) => {
956
- if (response.status !== 200) {
957
- o.onError ? o.onError({message: o.incSource + sources[name] + ' was not loaded'}) : '';
958
- return;
959
- }
960
- response.text().then((script) => {
961
- ls.setItem('inc-' + name, script);
962
- ls.setItem('inc-' + name + 'Expires', new Date().getTime() + o.incCacheExp);
963
- o.initState({
964
- tag,
965
- id: 'oInc-' + setN + '-' + fnId[name],
966
- innerHTML: script,
967
- 'data-o-inc': setN,
968
- }).appendInside('head');
969
- o.incCheck(setN, fnId[name], o.N);
970
- });
971
- });
972
- }
973
- } else {
974
- // standart loading without caching
975
- const state = {
976
- tag,
977
- id: 'oInc-' + setN + '-' + sourcesN,
978
- 'data-o-inc': setN,
979
- async: o.incAsync,
980
- onload: 'o.incCheck(' + setN + ',' + sourcesN + ',1)',
981
- };
982
- if (sources[name].indexOf('.css') > no) {
983
- state.tag = 'link';
984
- state.rel = 'stylesheet';
985
- state.href = sources[name];
986
- } else if (sources[name].indexOf('.js') > no) {
987
- state.src = sources[name];
988
- } else {
989
- state.tag = 'img';
990
- state.style = 'display:none;';
991
- state.src = sources[name];
992
- }
993
- o.initState(state).appendInside(state.style ? 'body' : 'head');
994
- }
995
- }
996
- }
997
-
998
- fnsStatus[o.Z] += sourcesReady;
999
-
1000
- if (sourcesN !== o.Z) {
1001
- if (sourcesReady === sourcesN) {
1002
- // if everything included
1003
- if (typeof callBack === f) {
1004
- callBack(setN);
1005
- }
1006
- } else {
1007
- // starting timeout for loading
1008
- setTimeout((set) => {
1009
- if (o.incReady[set] && o.incReady[set].length < o.incReady[set][o.Z]) {
1010
- o.incSet[set] = o.Z;
1011
- if (typeof callBad === f) {
1012
- callBad(setN);
1013
- }
1014
- }
1015
- }, o.incTimeout, setN);
1016
- }
1017
- }
1018
-
1019
- return o.incSet[o.Z];
1020
- };
1021
-
1022
-
1023
-
1024
-
1025
-
1026
-
1027
-
1028
-
1029
-
1030
-
1031
-
1032
-
1033
-
1034
-
1035
-
1036
-
1037
-
1038
-
1039
-
1040
-
1041
-
1042
-
1043
- /**
1044
- * Test function
1045
- *
1046
- * @param {string} title
1047
- * @param {array} tests array with name:function to test - true is success, false/string for failure
1048
- *
1049
- * @returns {number} test session number in o.tLog
1050
- */
1051
-
1052
- /* parameters */
1053
- o.tLog = [];// test sessions and results
1054
- o.tRes = [];// test results
1055
- o.tStatus = [];// test statuses
1056
- o.tFns = [];// callbacks
1057
- o.tShowOk = o.F;// show success tests or only errors
1058
- o.tStyled = o.F;// styled HTML results or plain style
1059
- o.tTime = 2000;// timeout for async tests
1060
-
1061
- // service
1062
- o.tPre = '<div style="font-family:monospace;text-align:left;">';
1063
- o.tOk = '<span style="background:#cfc;padding: 0 15px;">OK</span> ';
1064
- o.tXx = '<div style="background:#fcc;padding:3px;">';
1065
- o.tDc = '</div>';
1066
-
1067
- /* Main function for testing */
1068
- o.test = (title = '', ...tests) => {
1069
- const testN = o.tLog.length;
1070
- let waits = 0,
1071
- preOk = '├ OK: ',
1072
- preXx = '├ ✘ ',
1073
- posOk = '\n',
1074
- posXx = '\n',
1075
- num = tests.length,
1076
- done = o.Z;
1077
-
1078
- if (typeof tests[num - o.N] === 'function') {
1079
- o.tFns[testN] = tests[num - o.N];
1080
- num--;
1081
- }
1082
-
1083
- if (o.tStyled) {
1084
- o.tLog[testN] = '<div><b>' + title + ' #' + testN + '</b></div>';
1085
- preOk = o.tPre + o.tOk;
1086
- preXx = o.tPre + o.tXx;
1087
- posOk = o.tDc;
1088
- posXx = posOk + posOk;
1089
- } else {
1090
- o.tLog[testN] = title + ' #' + testN + '\n';
1091
- }
1092
-
1093
- o.tRes[testN] = o.F;
1094
- o.tStatus[testN] = [];
1095
-
1096
- for (let i = o.Z; i < num; i++) {
1097
- const testInfo = {
1098
- n: testN,
1099
- i,
1100
- title: tests[i][o.Z],
1101
- tShowOk: o.tShowOk,
1102
- tStyled: o.tStyled,
1103
- };
1104
- let res = tests[i][o.N];
1105
- if (typeof res === 'function') {
1106
- try {
1107
- res = res(testInfo);
1108
- } catch (error) {
1109
- res = error.message;
1110
- if (o.onError) {
1111
- o.onError(error);
1112
- }
1113
- }
1114
- }
1115
- o.tStatus[testN][i] = typeof res === 'string' ? o.F : res;
1116
- if (res === true) {
1117
- done++;
1118
- if (o.tShowOk) {
1119
- o.tLog[testN] += preOk + tests[i][o.Z] + posOk;
1120
- }
1121
- } else if (res !== o.U) {
1122
- o.tLog[testN] += preXx + tests[i][o.Z] + (res !== o.F ? ': <i>' + res + '</i>' : '') + posXx;
1123
- } else {
1124
- waits++;
1125
- setTimeout((info) => {
1126
- info.title += ' (timeout)';
1127
- o.testUpdate(info);
1128
- }, o.tTime, testInfo);
1129
- }
1130
- }
1131
-
1132
- o.tRes[testN] = done === num;
1133
-
1134
- if (o.tStyled) {
1135
- o.tLog[testN] += o.tPre + '<div style="color:' + (done + waits !== num ? 'red' : 'green') + ';"><b>';
1136
- } else {
1137
- o.tLog[testN] += waits ? '├' : '└ ';
1138
- }
1139
-
1140
- o.tLog[testN] += 'DONE ' + done + '/' + (num - waits);
1141
-
1142
- if (waits) {
1143
- o.tLog[testN] += ', waiting: ' + waits;
1144
- }
1145
-
1146
- if (o.tStyled) {
1147
- o.tLog[testN] += '</b>' + o.tDc + o.tDc;
1148
- } else {
1149
- o.tLog[testN] += '\n';
1150
- }
1151
-
1152
- if (!waits && typeof o.tFns[testN] === 'function') {
1153
- o.tFns[testN](testN);
1154
- }
1155
-
1156
- return testN;
1157
- };
1158
-
1159
- /* Function to update test state for async tests */
1160
- o.testUpdate = (info, res = o.F, suff = '') => {
1161
- if (o.tStatus[info.n][info.i] === o.U) {
1162
- o.tStatus[info.n][info.i] = res === true;
1163
- if (res === true) {
1164
- if (info.tShowOk) {
1165
- if (info.tStyled) {
1166
- o.tLog[info.n] += o.tPre + o.tOk + info.title + suff + o.tDc;
1167
- } else {
1168
- o.tLog[info.n] += '└ OK: ' + info.title + suff + '\n';
1169
- }
1170
- }
1171
- } else {
1172
- o.tRes[info.n] = o.F;
1173
- if (info.tStyled) {
1174
- o.tLog[info.n] += o.tPre + o.tXx + info.title + suff + (res ? ': ' + res : '') + o.tDc + o.tDc;
1175
- } else {
1176
- o.tLog[info.n] += '└ ✘ ' + info.title + (res ? ': ' + res : '') + suff + '\n';
1177
- }
1178
- }
1179
-
1180
- let fails = o.Z,
1181
- n = fails;
1182
-
1183
- for (let s of o.tStatus[info.n]) {
1184
- if (s === o.U) {
1185
- return;
1186
- }
1187
- if (!s) {
1188
- fails++;
1189
- }
1190
- n++;
1191
- }
1192
-
1193
- o.tRes[info.n] = !fails;
1194
- const text = fails ? 'FAILED ' + fails + '/' + n : 'DONE ' + n + '/' + n;
1195
- if (info.tStyled) {
1196
- o.tLog[info.n] += o.tPre + '<b style="color:' + (!fails ? 'green' : 'red') + ';">' + text + '</b>' + o.tDc;
1197
- } else {
1198
- o.tLog[info.n] += '└ ' + text;
1199
- }
1200
-
1201
- if (typeof o.tFns[info.n] === 'function') {
1202
- o.tFns[info.n](info.n);
1203
- }
1204
- }
1205
- };