@aegis-framework/artemis 0.3.21 → 0.3.25

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,18 +317,32 @@ 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) {
274
332
  if (this.length > 0) {
275
333
  return new DOM (this.collection[0].querySelector (selector));
276
334
  }
335
+
336
+ return new DOM (null);
337
+ }
338
+
339
+ /**
340
+ * Check if there are any elements that match the selector.
341
+ *
342
+ * @return {boolean} - Whether elements matching the selector existed or not
343
+ */
344
+ exists () {
345
+ return this.length > 0;
277
346
  }
278
347
 
279
348
  /**
@@ -281,13 +350,17 @@ export class DOM {
281
350
  *
282
351
  * @param {string} name - Name of the data property
283
352
  * @param {string} [value] - Value of the property
284
- * @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
285
356
  */
286
357
  data (name, value) {
287
358
  if (typeof value !== 'undefined') {
288
359
  for (const element of this.collection) {
289
360
  element.dataset[name] = value;
290
361
  }
362
+
363
+ return this;
291
364
  } else {
292
365
  if (this.length > 0) {
293
366
  return this.collection[0].dataset[name];
@@ -301,26 +374,32 @@ export class DOM {
301
374
  *
302
375
  * @param {string} name - Name of the data property to remove
303
376
  *
304
- * @return {void}
377
+ * @return {DOM} - Current instance
305
378
  */
306
379
  removeData (name) {
307
380
  for (const element of this.collection) {
308
381
  delete element.dataset[name];
309
382
  }
383
+
384
+ return this;
310
385
  }
311
386
 
312
387
  /**
313
388
  * Get or set the text of the first element matching the selector
314
389
  *
315
390
  * @param {string} [value] - Value to set the text to
316
- * @return {type} - If no value is present, this function returns its the
317
- * 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.
318
395
  */
319
396
  text (value) {
320
397
  if (typeof value !== 'undefined') {
321
398
  for (const element of this.collection) {
322
399
  element.textContent = value;
323
400
  }
401
+
402
+ return this;
324
403
  } else {
325
404
  if (this.length > 0) {
326
405
  return this.collection[0].textContent;
@@ -332,14 +411,17 @@ export class DOM {
332
411
  * Get or set the inner HTML of the first element matching the selector
333
412
  *
334
413
  * @param {string} [value] - Value to set the HTML to
335
- * @return {type} - If no value is present, this function returns its the
336
- * 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
337
417
  */
338
418
  html (value) {
339
419
  if (typeof value !== 'undefined') {
340
420
  for (const element of this.collection) {
341
421
  element.innerHTML = value;
342
422
  }
423
+
424
+ return this;
343
425
  } else {
344
426
  if (this.length > 0) {
345
427
  return this.collection[0].innerHTML;
@@ -351,6 +433,8 @@ export class DOM {
351
433
  * Append an element to the first element matching the selector
352
434
  *
353
435
  * @param {string} element - String representation of the element to add
436
+ *
437
+ * @return {DOM} - Current instance
354
438
  */
355
439
  append (element) {
356
440
  if (this.length > 0) {
@@ -366,12 +450,16 @@ export class DOM {
366
450
  this.collection[0].appendChild (element);
367
451
  }
368
452
  }
453
+
454
+ return this;
369
455
  }
370
456
 
371
457
  /**
372
458
  * Prepend an element to the first element matching the selector
373
459
  *
374
460
  * @param {string} element - String representation of the element to add
461
+ *
462
+ * @return {DOM} - Current instance
375
463
  */
376
464
  prepend (element) {
377
465
  if (this.length > 0) {
@@ -395,23 +483,30 @@ export class DOM {
395
483
  }
396
484
  }
397
485
  }
486
+
487
+ return this;
398
488
  }
399
489
 
400
490
  /**
401
491
  * Iterate over the collection of elements matching the selector
402
492
  *
403
493
  * @param {function} callback - Callback to run for every element
494
+ *
495
+ * @return {DOM} - Current instance
404
496
  */
405
497
  each (callback) {
406
498
  for (const element of this.collection) {
407
499
  callback (element);
408
500
  }
501
+
502
+ return this;
409
503
  }
410
504
 
411
505
  /**
412
506
  * Get an element from the collection given it's index
413
507
  *
414
508
  * @param {int} index - Index of the element to retrieve
509
+ *
415
510
  * @return {HTMLElement} - HTML Element in the position indicated by the index
416
511
  */
417
512
  get (index) {
@@ -427,6 +522,8 @@ export class DOM {
427
522
  if (this.length > 0) {
428
523
  return new DOM (this.collection[0]);
429
524
  }
525
+
526
+ return new DOM (null);
430
527
  }
431
528
 
432
529
  /**
@@ -438,6 +535,8 @@ export class DOM {
438
535
  if (this.length > 0) {
439
536
  return new DOM (this.collection[this.collection.length - 1]);
440
537
  }
538
+
539
+ return new DOM (null);
441
540
  }
442
541
 
443
542
  /**
@@ -464,24 +563,29 @@ export class DOM {
464
563
  if (this.length > 0) {
465
564
  return new DOM (this.collection[0].parentElement);
466
565
  }
566
+
567
+ return new DOM (null);
467
568
  }
468
569
 
469
570
  /**
470
571
  * Find an element that matches the given selector in the first element of the collection
471
572
  *
472
573
  * @param {string} selector - Selector to find the element with
473
- * @return {DOM} - Aegis instance with the element if found
574
+ *
575
+ * @return {DOM} - DOM instance with the element if found
474
576
  */
475
577
  find (selector) {
476
578
  if (this.length > 0) {
477
579
  return new DOM (this.collection[0].querySelectorAll (selector));
478
580
  }
581
+
582
+ return new DOM (null);
479
583
  }
480
584
 
481
585
  /**
482
586
  * Get the top and left offsets of the first element matching the selector
483
587
  *
484
- * @return {Object} - Object with `top` and `left` offsets
588
+ * @return {object|void} - Object with `top` and `left` offsets
485
589
  */
486
590
  offset () {
487
591
  if (this.length > 0) {
@@ -498,12 +602,13 @@ export class DOM {
498
602
  * from the initial object and then follows to its parents.
499
603
  *
500
604
  * @param {string} selector - Selector to match the closest element with
605
+ *
501
606
  * @return {DOM} - DOM instance with the closest HTML element matching the selector
502
607
  */
503
608
  closest (selector) {
504
609
  let found = null;
505
610
  let element = this;
506
- while (typeof element !== 'undefined' && found === null) {
611
+ while (element.exists () && found === null) {
507
612
  // Check if the current element matches the selector
508
613
  const matches = element.matches (selector);
509
614
 
@@ -527,9 +632,20 @@ export class DOM {
527
632
  return element;
528
633
  }
529
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
+ */
530
646
  closestParent (selector, limit) {
531
647
  let element = this;
532
- while (typeof element !== 'undefined') {
648
+ while (element.exists ()) {
533
649
 
534
650
  // Check if the current element matches the selector
535
651
  const matches = element.matches (selector);
@@ -546,6 +662,8 @@ export class DOM {
546
662
 
547
663
  element = element.parent ();
548
664
  }
665
+
666
+ return new DOM (null);
549
667
  }
550
668
 
551
669
  /**
@@ -553,7 +671,8 @@ export class DOM {
553
671
  *
554
672
  * @param {string} attribute - Attribute's name
555
673
  * @param {string|Number} [value] - Value to set the attribute to
556
- * @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
557
676
  * value of the provided attribute
558
677
  */
559
678
  attribute (attribute, value) {
@@ -561,6 +680,8 @@ export class DOM {
561
680
  for (const element of this.collection) {
562
681
  element.setAttribute (attribute, value);
563
682
  }
683
+
684
+ return this;
564
685
  } else {
565
686
  if (this.length > 0) {
566
687
  return this.collection[0].getAttribute (attribute);
@@ -572,6 +693,7 @@ export class DOM {
572
693
  * Check whether an element has an attribute or not
573
694
  *
574
695
  * @param {string} attribute - The name of the attribute to check existance for
696
+ *
575
697
  * @returns {boolean} - Whether or not the attribute is present
576
698
  */
577
699
  hasAttribute (attribute) {
@@ -587,22 +709,30 @@ export class DOM {
587
709
  * Insert content to the `after` property of an element
588
710
  *
589
711
  * @param {string} content - String representation of the content to add
712
+ *
713
+ * @return {DOM} - Current instance
590
714
  */
591
715
  after (content) {
592
716
  for (const element of this.collection) {
593
717
  element.insertAdjacentHTML ('afterend', content);
594
718
  }
719
+
720
+ return this;
595
721
  }
596
722
 
597
723
  /**
598
724
  * Insert content to the `before` property of an element
599
725
  *
600
726
  * @param {string} content - String representation of the content to add
727
+ *
728
+ * @return {DOM} - Current instance
601
729
  */
602
730
  before (content) {
603
731
  for (const element of this.collection) {
604
732
  element.insertAdjacentHTML ('beforebegin', content);
605
733
  }
734
+
735
+ return this;
606
736
  }
607
737
 
608
738
  /**
@@ -612,19 +742,24 @@ export class DOM {
612
742
  * either an individual property or a JSON object with key-value pairs
613
743
  * @param {string} [value] - Value to set the property to when only changing
614
744
  * one property
615
- * @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
616
747
  * function will return its current value
617
748
  */
618
749
  style (properties, value) {
619
750
  for (let i = 0; i < this.collection.length; i++) {
620
751
  if (typeof properties === 'string' && value !== 'undefined') {
621
752
  this.collection[i].style[properties] = value;
753
+
754
+ return this;
622
755
  } else if (typeof properties === 'string' && value === 'undefined') {
623
756
  return this.collection[i].style[properties];
624
757
  } else if (typeof properties === 'object') {
625
758
  for (const property in properties) {
626
759
  this.collection[i].style[property] = properties[property];
627
760
  }
761
+
762
+ return this;
628
763
  }
629
764
  }
630
765
  }
@@ -637,6 +772,8 @@ export class DOM {
637
772
  * to animate
638
773
  * @param {int} time - Time in milliseconds during which the properties will
639
774
  * be animated
775
+ *
776
+ * @return {DOM} - Current instance
640
777
  */
641
778
  animate (style, time) {
642
779
  for (let i = 0; i < this.collection.length; i++) {
@@ -676,6 +813,8 @@ export class DOM {
676
813
  }
677
814
  }
678
815
  }
816
+
817
+ return this;
679
818
  }
680
819
 
681
820
  /**
@@ -683,6 +822,8 @@ export class DOM {
683
822
  *
684
823
  * @param {type} [time=400] - Time duration for the animation
685
824
  * @param {type} callback - Callback function to run once the animation is over
825
+ *
826
+ * @return {DOM} - Current instance
686
827
  */
687
828
  fadeIn (time = 400, callback) {
688
829
  if (this.length > 0) {
@@ -706,6 +847,8 @@ export class DOM {
706
847
 
707
848
  tick();
708
849
  }
850
+
851
+ return this;
709
852
  }
710
853
 
711
854
  /**
@@ -713,6 +856,8 @@ export class DOM {
713
856
  *
714
857
  * @param {type} [time=400] - Time duration for the animation
715
858
  * @param {type} callback - Callback function to run once the animation is over
859
+ *
860
+ * @return {DOM} - Current instance
716
861
  */
717
862
  fadeOut (time = 400, callback) {
718
863
  if (this.length > 0) {
@@ -732,11 +877,14 @@ export class DOM {
732
877
  };
733
878
  tick ();
734
879
  }
880
+
881
+ return this;
735
882
  }
736
883
 
737
884
  /** Check if the first element in the collection matches a given selector
738
885
  *
739
886
  * @param {string} selector - Selector to match
887
+ *
740
888
  * @return {boolean} - Whether the element matches the selector or not
741
889
  */
742
890
  matches (selector) {
@@ -754,15 +902,21 @@ export class DOM {
754
902
 
755
903
  /**
756
904
  * Remove all elements in the collection
905
+ *
906
+ * @return {DOM} - Current instance
757
907
  */
758
908
  remove () {
759
909
  for (const element of this.collection) {
760
910
  element.parentNode.removeChild (element);
761
911
  }
912
+
913
+ return this;
762
914
  }
763
915
 
764
916
  /**
765
917
  * Replace the first element in the collection with a new one
918
+ *
919
+ * @return {DOM} - Current instance
766
920
  */
767
921
  replaceWith (newElement) {
768
922
  let replaceElement = newElement;
@@ -776,15 +930,21 @@ export class DOM {
776
930
  for (const element of this.collection) {
777
931
  element.parentElement.replaceChild (replaceElement, element);
778
932
  }
933
+
934
+ return this;
779
935
  }
780
936
 
781
937
  /**
782
938
  * Reset every element in the collection
939
+ *
940
+ * @return {DOM} - Current instance
783
941
  */
784
942
  reset () {
785
943
  for (const element of this.collection) {
786
944
  element.reset ();
787
945
  }
946
+
947
+ return this;
788
948
  }
789
949
 
790
950
  /**
@@ -792,6 +952,7 @@ export class DOM {
792
952
  *
793
953
  * @param {string} property - Property name to set or get
794
954
  * @param {string|Number} [value] - Value to set the property to
955
+ *
795
956
  * @return {string|Number} - If no value is provided, this function will return the
796
957
  * current value of the indicated property
797
958
  */
@@ -800,6 +961,8 @@ export class DOM {
800
961
  for (const element of this.collection) {
801
962
  element[property] = value;
802
963
  }
964
+
965
+ return this;
803
966
  } else {
804
967
  if (this.length > 0) {
805
968
  return this.collection[0][property];
@@ -812,6 +975,7 @@ export class DOM {
812
975
  * Simple wrapper function to use the DOM library
813
976
  *
814
977
  * @param {string|Object|array} selector - Selector or DOM element to use
978
+ *
815
979
  * @return {DOM} - DOM instance or class if no selector is used
816
980
  */
817
981
  export function $_ (selector) {