@aegis-framework/artemis 0.3.22 → 0.3.26

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/DOM.js CHANGED
@@ -58,33 +58,45 @@ export class DOM {
58
58
 
59
59
  /**
60
60
  * Hide elements by setting their `display` property to 'none'.
61
+ *
62
+ * @return {DOM} - Current instance
61
63
  */
62
64
  hide () {
63
65
  for (const element of this.collection) {
64
66
  element.style.display = 'none';
65
67
  }
68
+
69
+ return this;
66
70
  }
67
71
 
68
72
  /**
69
73
  * Show elements by setting their `display` property to the given value.
70
74
  *
71
75
  * @param {string} [display='block'] - Display property to set
76
+ *
77
+ * @return {DOM} - Current instance
72
78
  */
73
79
  show (display = 'block') {
74
80
  for (const element of this.collection) {
75
81
  element.style.display = display;
76
82
  }
83
+
84
+ return this;
77
85
  }
78
86
 
79
87
  /**
80
88
  * Add a class to the classList object
81
89
  *
82
90
  * @param {string} newClass - Class name to add
91
+ *
92
+ * @return {DOM} - Current instance
83
93
  */
84
94
  addClass (newClass) {
85
95
  for (const element of this.collection) {
86
96
  element.classList.add (newClass);
87
97
  }
98
+
99
+ return this;
88
100
  }
89
101
 
90
102
  /**
@@ -92,6 +104,8 @@ export class DOM {
92
104
  *
93
105
  * @param {string} [oldClass=null] - Class to remove. If it's empty or null,
94
106
  * all classes will be removed
107
+ *
108
+ * @return {DOM} - Current instance
95
109
  */
96
110
  removeClass (oldClass = null) {
97
111
  if (oldClass !== null) {
@@ -105,12 +119,16 @@ export class DOM {
105
119
  }
106
120
  }
107
121
  }
122
+
123
+ return this;
108
124
  }
109
125
 
110
126
  /**
111
127
  * Toggle between two classes
112
128
  *
113
129
  * @param {string} classes - Space separated class names
130
+ *
131
+ * @return {DOM} - Current instance
114
132
  */
115
133
  toggleClass (classes) {
116
134
  classes = classes.split (' ');
@@ -119,12 +137,15 @@ export class DOM {
119
137
  element.classList.toggle (classes[j]);
120
138
  }
121
139
  }
140
+
141
+ return this;
122
142
  }
123
143
 
124
144
  /**
125
145
  * Check if the first element matching the selector has the given class
126
146
  *
127
147
  * @param {string} classToCheck - Class name to check for
148
+ *
128
149
  * @return {boolean} - Whether the class is present or not
129
150
  */
130
151
  hasClass (classToCheck) {
@@ -140,14 +161,18 @@ export class DOM {
140
161
  * Get or set the value from the first element matching the selector
141
162
  *
142
163
  * @param {string} value - Value to set to the element.
143
- * @return {string} - If no value was provided, this returns the value of the
144
- * element instead of setting it
164
+ *
165
+ * @return {string|DOM} - If a value is provided, this returns the current
166
+ * instance, otherwise it returns the value of the element instead of
167
+ * setting it
145
168
  */
146
169
  value (value) {
147
170
  if (typeof value !== 'undefined') {
148
171
  for (const element of this.collection) {
149
172
  element.value = value;
150
173
  }
174
+
175
+ return this;
151
176
  } else {
152
177
  if (this.length > 0) {
153
178
  return this.collection[0].value;
@@ -157,77 +182,105 @@ export class DOM {
157
182
 
158
183
  /**
159
184
  * Focus on the first element matching the selector
185
+ *
186
+ * @return {DOM} - Current instance
160
187
  */
161
188
  focus () {
162
189
  if (this.length > 0) {
163
190
  this.collection[0].focus ();
164
191
  }
192
+
193
+ return this;
165
194
  }
166
195
 
167
196
  /**
168
197
  * Add a callback for the 'click' event on every element matching the selector
169
198
  *
170
199
  * @param {function} callback - Callback function to run when the event is triggered
200
+ *
201
+ * @return {DOM} - Current instance
171
202
  */
172
203
  click (callback) {
173
204
  for (const element of this.collection) {
174
205
  element.addEventListener ('click', callback, false);
175
206
  }
207
+
208
+ return this;
176
209
  }
177
210
 
178
211
  /**
179
212
  * Add a callback for the 'keyup' event on every element matching the selector
180
213
  *
181
214
  * @param {function} callback - Callback function to run when the event is triggered
215
+ *
216
+ * @return {DOM} - Current instance
182
217
  */
183
218
  keyup (callback) {
184
219
  for (const element of this.collection) {
185
220
  element.addEventListener ('keyup', callback, false);
186
221
  }
222
+
223
+ return this;
187
224
  }
188
225
 
189
226
  /**
190
227
  * Add a callback for the 'keydown' event on every element matching the selector
191
228
  *
192
229
  * @param {function} callback - Callback function to run when the event is triggered
230
+ *
231
+ * @return {DOM} - Current instance
193
232
  */
194
233
  keydown (callback) {
195
234
  for (const element of this.collection) {
196
235
  element.addEventListener ('keydown', callback, false);
197
236
  }
237
+
238
+ return this;
198
239
  }
199
240
 
200
241
  /**
201
242
  * Add a callback for the 'submit' event on every element matching the selector
202
243
  *
203
244
  * @param {function} callback - Callback function to run when the event is triggered
245
+ *
246
+ * @return {DOM} - Current instance
204
247
  */
205
248
  submit (callback) {
206
249
  for (const element of this.collection) {
207
250
  element.addEventListener ('submit', callback, false);
208
251
  }
252
+
253
+ return this;
209
254
  }
210
255
 
211
256
  /**
212
257
  * Add a callback for the 'change' event on every element matching the selector
213
258
  *
214
259
  * @param {function} callback - Callback function to run when the event is triggered
260
+ *
261
+ * @return {DOM} - Current instance
215
262
  */
216
263
  change (callback) {
217
264
  for (const element of this.collection) {
218
265
  element.addEventListener ('change', callback, false);
219
266
  }
267
+
268
+ return this;
220
269
  }
221
270
 
222
271
  /**
223
272
  * Add a callback for the 'scroll' event on every element matching the selector
224
273
  *
225
274
  * @param {function} callback - Callback function to run when the event is triggered
275
+ *
276
+ * @return {DOM} - Current instance
226
277
  */
227
278
  scroll (callback) {
228
279
  for (const element of this.collection) {
229
280
  element.addEventListener ('scroll', callback, false);
230
281
  }
282
+
283
+ return this;
231
284
  }
232
285
 
233
286
  /**
@@ -236,6 +289,8 @@ export class DOM {
236
289
  * @param {string} event - Event to add the listener to
237
290
  * @param {string} target - Target element on which to detect the event
238
291
  * @param {function} callback - Callback function to run when the event is triggered
292
+ *
293
+ * @return {DOM} - Current instance
239
294
  */
240
295
  on (event, target, callback) {
241
296
  event = event.split(' ');
@@ -253,7 +308,7 @@ export class DOM {
253
308
 
254
309
  const targetElement = $_(e.target).closestParent (target, this._selector);
255
310
 
256
- if (typeof targetElement !== 'undefined') {
311
+ if (targetElement.exists ()) {
257
312
  callback.call (targetElement.get (0), e);
258
313
  } else {
259
314
  return;
@@ -262,12 +317,15 @@ export class DOM {
262
317
  }
263
318
  }
264
319
  }
320
+
321
+ return this;
265
322
  }
266
323
 
267
324
  /**
268
325
  * Filter from the current collection to only those matching the new selector
269
326
  *
270
327
  * @param {string} element - Selector to filter the collection with
328
+ *
271
329
  * @return {DOM} - New DOM instance with the filtered collection
272
330
  */
273
331
  filter (selector) {
@@ -292,13 +350,17 @@ export class DOM {
292
350
  *
293
351
  * @param {string} name - Name of the data property
294
352
  * @param {string} [value] - Value of the property
295
- * @return {string} - If no value is set, this function returns it's current value
353
+ *
354
+ * @return {string|DOM} - If no value is provided, this function returns
355
+ * the first matching element value, otherwise it returns the current instance
296
356
  */
297
357
  data (name, value) {
298
358
  if (typeof value !== 'undefined') {
299
359
  for (const element of this.collection) {
300
360
  element.dataset[name] = value;
301
361
  }
362
+
363
+ return this;
302
364
  } else {
303
365
  if (this.length > 0) {
304
366
  return this.collection[0].dataset[name];
@@ -312,26 +374,32 @@ export class DOM {
312
374
  *
313
375
  * @param {string} name - Name of the data property to remove
314
376
  *
315
- * @return {void}
377
+ * @return {DOM} - Current instance
316
378
  */
317
379
  removeData (name) {
318
380
  for (const element of this.collection) {
319
381
  delete element.dataset[name];
320
382
  }
383
+
384
+ return this;
321
385
  }
322
386
 
323
387
  /**
324
388
  * Get or set the text of the first element matching the selector
325
389
  *
326
390
  * @param {string} [value] - Value to set the text to
327
- * @return {type} - If no value is present, this function returns its the
328
- * element's current text.
391
+ *
392
+ * @return {string|DOM} - If no value is provided, this function returns the
393
+ * inner text of the first matching element. Otherwise it returns the current
394
+ * instance.
329
395
  */
330
396
  text (value) {
331
397
  if (typeof value !== 'undefined') {
332
398
  for (const element of this.collection) {
333
399
  element.textContent = value;
334
400
  }
401
+
402
+ return this;
335
403
  } else {
336
404
  if (this.length > 0) {
337
405
  return this.collection[0].textContent;
@@ -343,14 +411,17 @@ export class DOM {
343
411
  * Get or set the inner HTML of the first element matching the selector
344
412
  *
345
413
  * @param {string} [value] - Value to set the HTML to
346
- * @return {type} - If no value is present, this function returns its the
347
- * element's current HTML.
414
+ *
415
+ * @return {string|DOM} - If no value is provided, this function returns the
416
+ * first matching element HTML. Otherwise it returns the current instance
348
417
  */
349
418
  html (value) {
350
419
  if (typeof value !== 'undefined') {
351
420
  for (const element of this.collection) {
352
421
  element.innerHTML = value;
353
422
  }
423
+
424
+ return this;
354
425
  } else {
355
426
  if (this.length > 0) {
356
427
  return this.collection[0].innerHTML;
@@ -362,6 +433,8 @@ export class DOM {
362
433
  * Append an element to the first element matching the selector
363
434
  *
364
435
  * @param {string} element - String representation of the element to add
436
+ *
437
+ * @return {DOM} - Current instance
365
438
  */
366
439
  append (element) {
367
440
  if (this.length > 0) {
@@ -377,12 +450,16 @@ export class DOM {
377
450
  this.collection[0].appendChild (element);
378
451
  }
379
452
  }
453
+
454
+ return this;
380
455
  }
381
456
 
382
457
  /**
383
458
  * Prepend an element to the first element matching the selector
384
459
  *
385
460
  * @param {string} element - String representation of the element to add
461
+ *
462
+ * @return {DOM} - Current instance
386
463
  */
387
464
  prepend (element) {
388
465
  if (this.length > 0) {
@@ -406,23 +483,30 @@ export class DOM {
406
483
  }
407
484
  }
408
485
  }
486
+
487
+ return this;
409
488
  }
410
489
 
411
490
  /**
412
491
  * Iterate over the collection of elements matching the selector
413
492
  *
414
493
  * @param {function} callback - Callback to run for every element
494
+ *
495
+ * @return {DOM} - Current instance
415
496
  */
416
497
  each (callback) {
417
498
  for (const element of this.collection) {
418
499
  callback (element);
419
500
  }
501
+
502
+ return this;
420
503
  }
421
504
 
422
505
  /**
423
506
  * Get an element from the collection given it's index
424
507
  *
425
508
  * @param {int} index - Index of the element to retrieve
509
+ *
426
510
  * @return {HTMLElement} - HTML Element in the position indicated by the index
427
511
  */
428
512
  get (index) {
@@ -487,7 +571,8 @@ export class DOM {
487
571
  * Find an element that matches the given selector in the first element of the collection
488
572
  *
489
573
  * @param {string} selector - Selector to find the element with
490
- * @return {DOM} - Aegis instance with the element if found
574
+ *
575
+ * @return {DOM} - DOM instance with the element if found
491
576
  */
492
577
  find (selector) {
493
578
  if (this.length > 0) {
@@ -500,7 +585,7 @@ export class DOM {
500
585
  /**
501
586
  * Get the top and left offsets of the first element matching the selector
502
587
  *
503
- * @return {Object} - Object with `top` and `left` offsets
588
+ * @return {object|void} - Object with `top` and `left` offsets
504
589
  */
505
590
  offset () {
506
591
  if (this.length > 0) {
@@ -517,12 +602,13 @@ export class DOM {
517
602
  * from the initial object and then follows to its parents.
518
603
  *
519
604
  * @param {string} selector - Selector to match the closest element with
605
+ *
520
606
  * @return {DOM} - DOM instance with the closest HTML element matching the selector
521
607
  */
522
608
  closest (selector) {
523
609
  let found = null;
524
610
  let element = this;
525
- while (typeof element !== 'undefined' && found === null) {
611
+ while (element.exists () && found === null) {
526
612
  // Check if the current element matches the selector
527
613
  const matches = element.matches (selector);
528
614
 
@@ -546,9 +632,20 @@ export class DOM {
546
632
  return element;
547
633
  }
548
634
 
635
+ /**
636
+ * Find the closest parent element matching the given selector. This bubbles up
637
+ * from the initial object and then follows to its parents.
638
+ *
639
+ * @param {string} selector - Selector to match the closest element with
640
+ * @param {string} limit - Selector for limit element. If the limit is reached
641
+ * and no element matching the provided selector was found, it will cause an
642
+ * early return.
643
+ *
644
+ * @return {DOM} - DOM instance with the closest HTML element matching the selector
645
+ */
549
646
  closestParent (selector, limit) {
550
647
  let element = this;
551
- while (typeof element !== 'undefined') {
648
+ while (element.exists ()) {
552
649
 
553
650
  // Check if the current element matches the selector
554
651
  const matches = element.matches (selector);
@@ -565,6 +662,8 @@ export class DOM {
565
662
 
566
663
  element = element.parent ();
567
664
  }
665
+
666
+ return new DOM (null);
568
667
  }
569
668
 
570
669
  /**
@@ -572,7 +671,8 @@ export class DOM {
572
671
  *
573
672
  * @param {string} attribute - Attribute's name
574
673
  * @param {string|Number} [value] - Value to set the attribute to
575
- * @return {type} - If no value is provided, this function returns the current
674
+ *
675
+ * @return {string|number|DOM} - If no value is provided, this function returns the current
576
676
  * value of the provided attribute
577
677
  */
578
678
  attribute (attribute, value) {
@@ -580,6 +680,8 @@ export class DOM {
580
680
  for (const element of this.collection) {
581
681
  element.setAttribute (attribute, value);
582
682
  }
683
+
684
+ return this;
583
685
  } else {
584
686
  if (this.length > 0) {
585
687
  return this.collection[0].getAttribute (attribute);
@@ -591,6 +693,7 @@ export class DOM {
591
693
  * Check whether an element has an attribute or not
592
694
  *
593
695
  * @param {string} attribute - The name of the attribute to check existance for
696
+ *
594
697
  * @returns {boolean} - Whether or not the attribute is present
595
698
  */
596
699
  hasAttribute (attribute) {
@@ -606,22 +709,30 @@ export class DOM {
606
709
  * Insert content to the `after` property of an element
607
710
  *
608
711
  * @param {string} content - String representation of the content to add
712
+ *
713
+ * @return {DOM} - Current instance
609
714
  */
610
715
  after (content) {
611
716
  for (const element of this.collection) {
612
717
  element.insertAdjacentHTML ('afterend', content);
613
718
  }
719
+
720
+ return this;
614
721
  }
615
722
 
616
723
  /**
617
724
  * Insert content to the `before` property of an element
618
725
  *
619
726
  * @param {string} content - String representation of the content to add
727
+ *
728
+ * @return {DOM} - Current instance
620
729
  */
621
730
  before (content) {
622
731
  for (const element of this.collection) {
623
732
  element.insertAdjacentHTML ('beforebegin', content);
624
733
  }
734
+
735
+ return this;
625
736
  }
626
737
 
627
738
  /**
@@ -631,19 +742,24 @@ export class DOM {
631
742
  * either an individual property or a JSON object with key-value pairs
632
743
  * @param {string} [value] - Value to set the property to when only changing
633
744
  * one property
634
- * @return {string} - If a property is given but not a value for it, this
745
+ *
746
+ * @return {string|DOM} - If a property is given but not a value for it, this
635
747
  * function will return its current value
636
748
  */
637
749
  style (properties, value) {
638
750
  for (let i = 0; i < this.collection.length; i++) {
639
751
  if (typeof properties === 'string' && value !== 'undefined') {
640
752
  this.collection[i].style[properties] = value;
753
+
754
+ return this;
641
755
  } else if (typeof properties === 'string' && value === 'undefined') {
642
756
  return this.collection[i].style[properties];
643
757
  } else if (typeof properties === 'object') {
644
758
  for (const property in properties) {
645
759
  this.collection[i].style[property] = properties[property];
646
760
  }
761
+
762
+ return this;
647
763
  }
648
764
  }
649
765
  }
@@ -656,6 +772,8 @@ export class DOM {
656
772
  * to animate
657
773
  * @param {int} time - Time in milliseconds during which the properties will
658
774
  * be animated
775
+ *
776
+ * @return {DOM} - Current instance
659
777
  */
660
778
  animate (style, time) {
661
779
  for (let i = 0; i < this.collection.length; i++) {
@@ -695,6 +813,8 @@ export class DOM {
695
813
  }
696
814
  }
697
815
  }
816
+
817
+ return this;
698
818
  }
699
819
 
700
820
  /**
@@ -702,6 +822,8 @@ export class DOM {
702
822
  *
703
823
  * @param {type} [time=400] - Time duration for the animation
704
824
  * @param {type} callback - Callback function to run once the animation is over
825
+ *
826
+ * @return {DOM} - Current instance
705
827
  */
706
828
  fadeIn (time = 400, callback) {
707
829
  if (this.length > 0) {
@@ -725,6 +847,8 @@ export class DOM {
725
847
 
726
848
  tick();
727
849
  }
850
+
851
+ return this;
728
852
  }
729
853
 
730
854
  /**
@@ -732,6 +856,8 @@ export class DOM {
732
856
  *
733
857
  * @param {type} [time=400] - Time duration for the animation
734
858
  * @param {type} callback - Callback function to run once the animation is over
859
+ *
860
+ * @return {DOM} - Current instance
735
861
  */
736
862
  fadeOut (time = 400, callback) {
737
863
  if (this.length > 0) {
@@ -751,11 +877,14 @@ export class DOM {
751
877
  };
752
878
  tick ();
753
879
  }
880
+
881
+ return this;
754
882
  }
755
883
 
756
884
  /** Check if the first element in the collection matches a given selector
757
885
  *
758
886
  * @param {string} selector - Selector to match
887
+ *
759
888
  * @return {boolean} - Whether the element matches the selector or not
760
889
  */
761
890
  matches (selector) {
@@ -773,15 +902,21 @@ export class DOM {
773
902
 
774
903
  /**
775
904
  * Remove all elements in the collection
905
+ *
906
+ * @return {DOM} - Current instance
776
907
  */
777
908
  remove () {
778
909
  for (const element of this.collection) {
779
910
  element.parentNode.removeChild (element);
780
911
  }
912
+
913
+ return this;
781
914
  }
782
915
 
783
916
  /**
784
917
  * Replace the first element in the collection with a new one
918
+ *
919
+ * @return {DOM} - Current instance
785
920
  */
786
921
  replaceWith (newElement) {
787
922
  let replaceElement = newElement;
@@ -795,15 +930,21 @@ export class DOM {
795
930
  for (const element of this.collection) {
796
931
  element.parentElement.replaceChild (replaceElement, element);
797
932
  }
933
+
934
+ return this;
798
935
  }
799
936
 
800
937
  /**
801
938
  * Reset every element in the collection
939
+ *
940
+ * @return {DOM} - Current instance
802
941
  */
803
942
  reset () {
804
943
  for (const element of this.collection) {
805
944
  element.reset ();
806
945
  }
946
+
947
+ return this;
807
948
  }
808
949
 
809
950
  /**
@@ -811,6 +952,7 @@ export class DOM {
811
952
  *
812
953
  * @param {string} property - Property name to set or get
813
954
  * @param {string|Number} [value] - Value to set the property to
955
+ *
814
956
  * @return {string|Number} - If no value is provided, this function will return the
815
957
  * current value of the indicated property
816
958
  */
@@ -819,6 +961,8 @@ export class DOM {
819
961
  for (const element of this.collection) {
820
962
  element[property] = value;
821
963
  }
964
+
965
+ return this;
822
966
  } else {
823
967
  if (this.length > 0) {
824
968
  return this.collection[0][property];
@@ -831,6 +975,7 @@ export class DOM {
831
975
  * Simple wrapper function to use the DOM library
832
976
  *
833
977
  * @param {string|Object|array} selector - Selector or DOM element to use
978
+ *
834
979
  * @return {DOM} - DOM instance or class if no selector is used
835
980
  */
836
981
  export function $_ (selector) {
package/src/FileSystem.js CHANGED
@@ -40,6 +40,7 @@ export class FileSystem {
40
40
  * @param {File|Blob} file - File to read
41
41
  * @param {string} [type = 'text'] - Type of data to be read, values can be
42
42
  * 'text', 'base64' and 'buffer'.
43
+ *
43
44
  * @return {Promise<Event, ArrayBuffer|string>} - Promise that resolves to
44
45
  * the Load event and content of the file. The format depends on the type
45
46
  * parameter used.
@@ -75,6 +76,7 @@ export class FileSystem {
75
76
  * @param {string} file - Name of the file (Including extension)
76
77
  * @param {ArrayBuffer|ArrayBufferView|Blob|string} content - Content to save in the file
77
78
  * @param {string} [type = 'text/plain'] - Mime Type for the file
79
+ *
78
80
  * @return {Promise<File>}
79
81
  */
80
82
  static create (name, content, type = 'text/plain') {
@@ -85,6 +87,7 @@ export class FileSystem {
85
87
  * @static extension - Returns the extension of a file given its file name.
86
88
  *
87
89
  * @param {string} name - Name or full path of the file
90
+ *
88
91
  * @return {string} - File extension without the leading dot (.)
89
92
  */
90
93
  static extension (name) {
@@ -95,6 +98,7 @@ export class FileSystem {
95
98
  * @static isImage - Check if a file is an image by its extension.
96
99
  *
97
100
  * @param {string} name - Name or full path of the file
101
+ *
98
102
  * @return {boolean}
99
103
  */
100
104
  static isImage (name) {
package/src/Platform.js CHANGED
@@ -53,6 +53,7 @@ export class Platform {
53
53
  }
54
54
 
55
55
  // Main process
56
+ // eslint-disable-next-line no-undef
56
57
  if (typeof process !== 'undefined' && typeof process.versions === 'object' && !!process.versions.electron) {
57
58
  return true;
58
59
  }
@@ -90,7 +90,7 @@ export class IndexedDB {
90
90
  } else {
91
91
  // Check what upgrade functions have been declared in their respective order
92
92
  const availableUpgrades = Object.keys (this.upgrades).sort ();
93
-
93
+
94
94
  // Find the first update that needs to be applied to the database given
95
95
  // the old version it currently has.
96
96
  const startFrom = availableUpgrades.findIndex (u => {
@@ -187,7 +187,14 @@ export class IndexedDB {
187
187
  const transaction = this.storage.transaction (this.store).objectStore (this.store);
188
188
  const op = transaction.get (key);
189
189
 
190
- op.addEventListener ('success', (event) => {resolve (event.target.result);});
190
+ op.addEventListener ('success', (event) => {
191
+ const value = event.target.result;
192
+ if (typeof value !== 'undefined' && value !== null) {
193
+ resolve (value);
194
+ } else {
195
+ reject ();
196
+ }
197
+ });
191
198
  op.addEventListener ('error', (event) => {reject (event);});
192
199
  });
193
200
  });