eyjs 6.2.0 → 7.1.0

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/dist/eye.umd.js CHANGED
@@ -14,6 +14,11 @@
14
14
  * @property {String} html - set element html
15
15
  */
16
16
 
17
+ const unitlessProps = new Set([
18
+ "opacity", "zIndex", "fontWeight", "lineHeight",
19
+ "flex", "flexGrow", "flexShrink", "order"
20
+ ]);
21
+
17
22
  /**
18
23
  * Returns the associated class event of that event
19
24
  * example: for click event it returns new MouseEvent("click")
@@ -381,7 +386,7 @@
381
386
  class EyeElement {
382
387
  /**
383
388
  * Raw html element
384
- * @type {HTMLElement}
389
+ * @type {Array<HTMLElement>}
385
390
  */
386
391
  #raw = null;
387
392
 
@@ -412,17 +417,14 @@
412
417
  constructor(selector, attrs, css) {
413
418
  let _this = this;
414
419
  if (selector instanceof HTMLElement) {
415
- this.#raw = selector;
420
+ this.#raw = [selector];
416
421
  } else if (htmlElements.includes(selector)) {
417
422
  // creating a new element
418
- this.#raw = document.createElement(selector.substring(1, selector.length - 1));
423
+ this.#raw = [document.createElement(selector.substring(1, selector.length - 1))];
419
424
  } else {
420
425
  // selecting
421
426
  let s = selector.slice(-1) === "!";
422
- this.#raw = document.querySelectorAll(s ? selector.slice(0, -1) : selector);
423
-
424
- if (this.#raw.length == 0) return null; // we stop everything here
425
- if (this.length == 1 || s) this.#raw = this.#raw.item(0);
427
+ this.#raw = [...document.querySelectorAll(s ? selector.slice(0, -1) : selector)];
426
428
  }
427
429
 
428
430
  /**
@@ -486,7 +488,6 @@
486
488
  };
487
489
  });
488
490
 
489
-
490
491
  return this;
491
492
  }
492
493
 
@@ -495,7 +496,7 @@
495
496
  * @type {Number}
496
497
  */
497
498
  get length() {
498
- return this.#raw instanceof NodeList ? this.#raw.length : 1;
499
+ return this.#raw.length;
499
500
  }
500
501
 
501
502
  /**
@@ -513,10 +514,9 @@
513
514
  * @returns {EyeElement}
514
515
  */
515
516
  each(cb) {
516
- let list = (this.#raw instanceof NodeList ? [...this.#raw.entries()] : [[0, this.#raw]]);
517
- for (let i = 0; i < list.length; i++) {
518
- const [idx, elm] = list[i];
519
- let exit = cb(elm, idx, this);
517
+ for (let i = 0; i < this.#raw.length; i++) {
518
+ const elm = this.#raw[i];
519
+ let exit = cb(elm, i, this);
520
520
  if (exit === false) break;
521
521
  }
522
522
  return this;
@@ -682,46 +682,39 @@
682
682
  return this;
683
683
  }
684
684
  /**
685
- * Append one or more elements to the current element
685
+ * Append one or more elements to the current element, only effect the first element in the selected list
686
686
  * @method EyeElement#append
687
687
  * @param {HTMLElement|Array<Node|EyeElement>} elm
688
688
  * @param {"next" | "after" | "previous" | "before" | "first" | "afterbegin" | "last" | "beforeend"} [pos] [optional]
689
689
  * @returns {EyeElement}
690
690
  */
691
691
  append(elm, pos) {
692
- let nodes = [];
693
692
  (Array.isArray(elm) ? elm : [elm]).forEach(item => {
694
- if (item instanceof EyeElement) nodes.push(item.#raw);
695
- else if (item instanceof HTMLElement) nodes.push(item);
696
- });
697
- if (this.#raw instanceof NodeList) {
698
- console.warn(`[EyeJS] beware while using .append with multi selected elements`);
699
- this.#raw.forEach((itm, idx) => {
700
- if (!nodes[idx]) return;
701
- itm.append(nodes[idx]);
693
+ let nodes = [];
694
+ if (item instanceof EyeElement) nodes = [...item.raw];
695
+ else if (item instanceof HTMLElement) nodes = [item];
696
+
697
+ nodes.forEach(node => {
698
+ switch (pos) {
699
+ case "next":
700
+ case "after":
701
+ this.#raw[0].after(node);
702
+ break;
703
+ case "previous":
704
+ case "before":
705
+ this.#raw[0].before(node);
706
+ break;
707
+ case "afterbegin":
708
+ case "first":
709
+ this.#raw[0].prepend(node);
710
+ break;
711
+ case "beforeend":
712
+ case "last":
713
+ default:
714
+ this.#raw[0].append(node);
715
+ }
702
716
  });
703
- return this;
704
- }
705
- if (!pos) this.#raw.append(...nodes);
706
- else
707
- switch (pos) {
708
- case "next":
709
- case "after":
710
- this.#raw.after(...nodes);
711
- break;
712
- case "previous":
713
- case "before":
714
- this.#raw.before(...nodes);
715
- break;
716
- case "afterbegin":
717
- case "first":
718
- this.#raw.prepend(...nodes);
719
- break;
720
- case "beforeend":
721
- case "last":
722
- this.#raw.append(...nodes);
723
-
724
- }
717
+ });
725
718
  return this;
726
719
  }
727
720
  /**
@@ -731,7 +724,7 @@
731
724
  * @returns {EyeElement}
732
725
  */
733
726
  after(elm) {
734
- (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw).after(elm instanceof EyeElement ? elm.raw : elm);
727
+ this.#raw[0].after(elm instanceof EyeElement ? elm.raw : elm);
735
728
  return this;
736
729
  }
737
730
  /**
@@ -741,7 +734,7 @@
741
734
  * @returns {EyeElement}
742
735
  */
743
736
  before(elm) {
744
- (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw).before(elm instanceof EyeElement ? elm.raw : elm);
737
+ this.#raw[0].before(elm instanceof EyeElement ? elm.raw : elm);
745
738
  return this;
746
739
  }
747
740
  /**
@@ -754,57 +747,47 @@
754
747
  replaceWith(...elms) {
755
748
  let nodes = [];
756
749
  (Array.isArray(elms) ? elms : [elms]).forEach(item => {
757
- if (item instanceof EyeElement) nodes.push(item.#raw);
750
+ if (item instanceof EyeElement) nodes = nodes.concat(...item.raw);
758
751
  else if (item instanceof HTMLElement) nodes.push(item);
759
752
  });
760
- if (this.#raw instanceof NodeList) {
761
- [...this.#raw.entries()].forEach(([idx, elm]) => {
762
- if (!nodes[idx]) return;
763
- elm.replaceWith(nodes[idx]);
764
- });
765
- } else {
766
- this.#raw.replaceWith(...nodes);
767
- }
753
+ this.#raw[0].replaceWith(...nodes);
768
754
  return this;
769
755
  }
770
756
  /**
771
757
  * Get current element parent or append it to one
772
758
  * @method EyeElement#parent
773
- * @param {HTMLElement|EyeElement} par
759
+ * @param {HTMLElement|EyeElement} parent
760
+ * @param {boolean} clone [true] append clones of the elements
774
761
  * @returns {EyeElement}
775
762
  */
776
- parent(par) {
777
- if (par) {
778
- if (!(par instanceof HTMLElement) && !(par instanceof EyeElement))
763
+ parent(parent, clone) {
764
+ if (parent) {
765
+ if (!(parent instanceof HTMLElement) && !(parent instanceof EyeElement))
779
766
  throw new Error(
780
767
  "[EyeJS] Unable to append current element to parent because it's not HTMLElement"
781
768
  );
782
- this.each((elm, idx) => {
783
- par.append(elm);
769
+ this.each(elm => {
770
+ parent.append(clone === true ? elm.cloneNode(true) : elm);
784
771
  });
785
772
  return this;
786
773
  }
787
- return e(this.#raw instanceof NodeList ? this.#raw.item(0).parentElement : this.#raw.parentElement);
774
+ return e(this.#raw[0].parentElement);
788
775
  }
789
776
  /**
790
- * Returns whether current node is the same/equal(depending on `check`) as the passed node or not
777
+ * Returns whether current node is the same/equal (depending on `check`) as the passed node or not
791
778
  * @method EyeElement#is
792
779
  * @param {HTMLElement|EyeElement} node
793
780
  * @param {"connected" | "same" | "equal"} [check] check type `same`, `equal`
794
781
  * @returns {boolean}
795
782
  */
796
783
  is(node, check) {
797
- node = node instanceof EyeElement ? node.#raw : node;
798
- if (this.#raw instanceof NodeList) {
799
- console.warn(`[EyeJS] .is is not functional with multi selected elements`);
800
- return this;
801
- }
802
- if (node === "connected") return this.#raw.isConnected;
784
+ node = node instanceof EyeElement ? node.#raw[0] : node;
785
+ if (node === "connected") return this.#raw[0].isConnected;
803
786
  switch (check) {
804
787
  case "same":
805
- return this.#raw.isSameNode(node);
788
+ return this.#raw[0].isSameNode(node);
806
789
  case "equal":
807
- return this.#raw.isEqualNode(node);
790
+ return this.#raw[0].isEqualNode(node);
808
791
  default:
809
792
  console.error(
810
793
  `[EyeJS] Unknown check "${check}", possible values are ["same","equal","connected"]`
@@ -821,6 +804,7 @@
821
804
  */
822
805
  css(attr, value) {
823
806
  if (attr) {
807
+ if (!unitlessProps.has(attr) && typeof value === "number" && value != 0) value = `${value}px`;
824
808
  let out = undefined;
825
809
  attr = flat(attr);
826
810
  this.each((elm, idx) => {
@@ -828,7 +812,7 @@
828
812
  elm.style[attr] = value;
829
813
  });
830
814
  return out != undefined ? out : this;
831
- } else return console.error(`[EyeJS] mission argument "attr" in function .css`);
815
+ } else return console.error(`[EyeJS] missing argument "attr" in function .css`);
832
816
  }
833
817
  /**
834
818
  * Remove current element
@@ -871,8 +855,6 @@
871
855
  "[EyeJS] .on method is missing the actuall callback `cb` or not of type function"
872
856
  );
873
857
 
874
- let elms = (this.#raw instanceof NodeList ? [...this.#raw.entries()] : [[0, this.#raw]]);
875
-
876
858
  let outsider = null;
877
859
  ev.forEach(evt => {
878
860
  if (target) {
@@ -883,7 +865,7 @@
883
865
  _this.#dlgListeners.set(evt, new Set());
884
866
  _this.#dlgListeners.get(evt).add({ callback: cb, target });
885
867
  } else {
886
- elms.forEach(([idx, elm]) => {
868
+ _this.each((elm) => {
887
869
  elm.addEventListener(evt, cb);
888
870
  });
889
871
  }
@@ -955,21 +937,15 @@
955
937
  * Returns a clone of current selected element/s
956
938
  * @method EyeElement#clone
957
939
  * @param {HTMLElement} [parent] optionally append new clone to a parent
958
- * @returns {Array<EyeElement>|EyeElement}
940
+ * @returns {Array<EyeElement>}
959
941
  */
960
942
  clone(parent) {
961
- if (this.#raw instanceof NodeList) {
962
- let list = [];
963
- this.#raw.forEach(nd => {
964
- list.push(nd.cloneNode(true));
965
- });
966
- if (parent instanceof HTMLElement || parent instanceof EyeElement) list.forEach(el => parent.append(el));
967
- return list;
968
- } else {
969
- let clone = this.#raw.cloneNode(true);
970
- if (parent instanceof HTMLElement || parent instanceof EyeElement) parent.append(clone);
971
- return clone;
972
- }
943
+ let list = [];
944
+ this.each((nd) => {
945
+ list.push(nd.cloneNode(true));
946
+ });
947
+ if (parent instanceof HTMLElement || parent instanceof EyeElement) list.forEach(el => parent.append(el));
948
+ return list;
973
949
  }
974
950
  /**
975
951
  * Compute DOMRect or style declaration of current element
@@ -980,10 +956,10 @@
980
956
  compute(type) {
981
957
  type = type || "bounds";
982
958
  if (type === "bounds")
983
- return (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw).getBoundingClientRect();
959
+ return (this.#raw[0]).getBoundingClientRect();
984
960
  else if (type == "style")
985
- return getComputedStyle(this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw)
986
- console.error(`[EyeJS] unkown type "${type}" in function .compute, possible values are "bounds" "style"`);
961
+ return getComputedStyle(this.#raw[0])
962
+ console.error(`[EyeJS] unknown type "${type}" in function .compute, possible values are "bounds" "style"`);
987
963
  }
988
964
 
989
965
  /**
@@ -993,7 +969,7 @@
993
969
  */
994
970
  client(attr) {
995
971
  if (['width', 'height', 'left', 'top'].includes(attr))
996
- return (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw)[`client${attr[0].toUpperCase()}${attr.substring(1, attr.length)}`];
972
+ return this.#raw[0][`client${attr[0].toUpperCase()}${attr.substring(1, attr.length)}`];
997
973
  else console.error(`[EyeJS] Unknown client* attribute "${attr}" in .client(attr)`);
998
974
  return this;
999
975
  }
@@ -1031,9 +1007,8 @@
1031
1007
  * @returns {EyeElement|null}
1032
1008
  */
1033
1009
  child(index) {
1034
- let it = (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw);
1035
- if (index === undefined) return it.children.length;
1036
- if (it.children[index]) return e(it.children[index]);
1010
+ if (index === undefined) return this.#raw[0].children.length;
1011
+ if (this.#raw[0].children[index]) return e(this.#raw[0].children[index]);
1037
1012
  return null;
1038
1013
  }
1039
1014
  /**
@@ -1043,11 +1018,8 @@
1043
1018
  * @returns
1044
1019
  */
1045
1020
  val(value) {
1046
- if (value != undefined) (this.#raw instanceof NodeList ? [...this.#raw.entries()] : [[0, this.#raw]]).forEach(([idx, a]) => a.value = this.#customSet.value("set", value, a));
1047
- else {
1048
- let it = (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw);
1049
- return this.#customSet.value("get", it.value, it);
1050
- }
1021
+ if (value != undefined) this.each((a) => a.value = this.#customSet.value("set", value, a));
1022
+ else return this.#customSet.value("get", this.#raw[0].value, this.#raw[0]);
1051
1023
  return this;
1052
1024
  }
1053
1025
  /**
@@ -1061,31 +1033,29 @@
1061
1033
  let {
1062
1034
  inputs = ["input", "textarea", "select"],
1063
1035
  } = opts;
1064
- if (this.#raw instanceof HTMLElement) {
1065
- let out = {
1066
- json: {},
1067
- url: "",
1068
- fd: new FormData()
1069
- };
1070
- this.#raw.querySelectorAll(inputs.join(','))
1071
- .forEach((inp, i) => {
1072
- let name = inp.name || inp.dataset.name;
1073
- let value = inp.value || inp.textContent;
1074
- if (typeof opts[name] === "function") value = opts[name](inp);
1075
-
1076
- if (inp.type == "file")
1077
- inp.files.forEach(file => {
1078
- out.fd.append(name, file);
1079
- });
1080
- else {
1081
- out.json[name] = value;
1082
- out.fd.append(name, value);
1083
- }
1084
- });
1036
+ let out = {
1037
+ json: {},
1038
+ url: "",
1039
+ fd: new FormData()
1040
+ };
1041
+ this.#raw[0].querySelectorAll(inputs.join(','))
1042
+ .forEach((inp, i) => {
1043
+ let name = inp.name || inp.dataset.name;
1044
+ let value = inp.value || inp.textContent;
1045
+ if (typeof opts[name] === "function") value = opts[name](inp);
1046
+
1047
+ if (inp.type == "file")
1048
+ inp.files.forEach(file => {
1049
+ out.fd.append(name, file);
1050
+ });
1051
+ else {
1052
+ out.json[name] = value;
1053
+ out.fd.append(name, value);
1054
+ }
1055
+ });
1085
1056
 
1086
- out.url = new URLSearchParams(out.json).toString();
1087
- return out;
1088
- } else console.warn(`[EyeJS] this is a multi selection, it's not serializable!`);
1057
+ out.url = new URLSearchParams(out.json).toString();
1058
+ return out;
1089
1059
  }
1090
1060
  /**
1091
1061
  * Redefine the way `.text` or `.val` set or get data to and from this element.
@@ -1119,20 +1089,20 @@
1119
1089
  }
1120
1090
 
1121
1091
  /**
1122
- * Find element/s position/s within parent element
1092
+ * Find first element position within parent element
1123
1093
  * @method EyeElement#position
1124
1094
  * @returns {number}
1125
1095
  */
1126
1096
  position() {
1127
- let poses = [];
1128
- this.each((elm) => {
1129
- for (let i = 0; i < elm.parentNode.children.length; i++) {
1130
- const child = elm.parentNode.children[i];
1131
- if (elm.isSameNode(child)) poses.push(i);
1097
+ let pos = -1;
1098
+ for (let i = 0; i < this.#raw[0].parentNode.children.length; i++) {
1099
+ const child = this.#raw[0].parentNode.children[i];
1100
+ if (this.#raw[0].isSameNode(child)) {
1101
+ pos = i;
1102
+ break;
1132
1103
  }
1133
-
1134
- });
1135
- return poses.length == 0 ? poses[0] : poses;
1104
+ }
1105
+ return pos;
1136
1106
  }
1137
1107
  }
1138
1108
  /**
@@ -1174,10 +1144,12 @@
1174
1144
  };
1175
1145
  return copy.refresh(attrs);
1176
1146
  };
1177
- } else return new EyeElement(tag, attrs, css);
1147
+ } else {
1148
+ let ne = new EyeElement(tag, attrs, css);
1149
+ return ne.length === 0 ? null : ne;
1150
+ }
1178
1151
  }
1179
1152
 
1180
-
1181
1153
  // gloablly exposed
1182
1154
  window.e = e;
1183
1155
  window.EyeElement = EyeElement;