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