eyjs 7.0.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,18 @@
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
+
3
16
  ## [7.0.0] - 2025-09-01
4
17
 
5
18
  ### 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,17 +745,10 @@ 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
  /**
@@ -783,27 +769,23 @@ class EyeElement {
783
769
  });
784
770
  return this;
785
771
  }
786
- return e(this.#raw instanceof NodeList ? this.#raw.item(0).parentElement : this.#raw.parentElement);
772
+ return e(this.#raw[0].parentElement);
787
773
  }
788
774
  /**
789
- * 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
790
776
  * @method EyeElement#is
791
777
  * @param {HTMLElement|EyeElement} node
792
778
  * @param {"connected" | "same" | "equal"} [check] check type `same`, `equal`
793
779
  * @returns {boolean}
794
780
  */
795
781
  is(node, check) {
796
- node = node instanceof EyeElement ? node.#raw : node;
797
- if (this.#raw instanceof NodeList) {
798
- console.warn(`[EyeJS] .is is not functional with multi selected elements`);
799
- return this;
800
- }
801
- 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;
802
784
  switch (check) {
803
785
  case "same":
804
- return this.#raw.isSameNode(node);
786
+ return this.#raw[0].isSameNode(node);
805
787
  case "equal":
806
- return this.#raw.isEqualNode(node);
788
+ return this.#raw[0].isEqualNode(node);
807
789
  default:
808
790
  console.error(
809
791
  `[EyeJS] Unknown check "${check}", possible values are ["same","equal","connected"]`
@@ -820,6 +802,7 @@ class EyeElement {
820
802
  */
821
803
  css(attr, value) {
822
804
  if (attr) {
805
+ if (!unitlessProps.has(attr) && typeof value === "number" && value != 0) value = `${value}px`;
823
806
  let out = undefined;
824
807
  attr = flat(attr);
825
808
  this.each((elm, idx) => {
@@ -827,7 +810,7 @@ class EyeElement {
827
810
  elm.style[attr] = value;
828
811
  });
829
812
  return out != undefined ? out : this;
830
- } else return console.error(`[EyeJS] mission argument "attr" in function .css`);
813
+ } else return console.error(`[EyeJS] missing argument "attr" in function .css`);
831
814
  }
832
815
  /**
833
816
  * Remove current element
@@ -870,8 +853,6 @@ class EyeElement {
870
853
  "[EyeJS] .on method is missing the actuall callback `cb` or not of type function"
871
854
  );
872
855
 
873
- let elms = (this.#raw instanceof NodeList ? [...this.#raw.entries()] : [[0, this.#raw]]);
874
-
875
856
  let outsider = null;
876
857
  ev.forEach(evt => {
877
858
  if (target) {
@@ -882,7 +863,7 @@ class EyeElement {
882
863
  _this.#dlgListeners.set(evt, new Set());
883
864
  _this.#dlgListeners.get(evt).add({ callback: cb, target });
884
865
  } else {
885
- elms.forEach(([idx, elm]) => {
866
+ _this.each((elm) => {
886
867
  elm.addEventListener(evt, cb);
887
868
  });
888
869
  }
@@ -954,21 +935,15 @@ class EyeElement {
954
935
  * Returns a clone of current selected element/s
955
936
  * @method EyeElement#clone
956
937
  * @param {HTMLElement} [parent] optionally append new clone to a parent
957
- * @returns {Array<EyeElement>|EyeElement}
938
+ * @returns {Array<EyeElement>}
958
939
  */
959
940
  clone(parent) {
960
- if (this.#raw instanceof NodeList) {
961
- let list = [];
962
- this.#raw.forEach(nd => {
963
- list.push(nd.cloneNode(true));
964
- });
965
- if (parent instanceof HTMLElement || parent instanceof EyeElement) list.forEach(el => parent.append(el));
966
- return list;
967
- } else {
968
- let clone = this.#raw.cloneNode(true);
969
- if (parent instanceof HTMLElement || parent instanceof EyeElement) parent.append(clone);
970
- return clone;
971
- }
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;
972
947
  }
973
948
  /**
974
949
  * Compute DOMRect or style declaration of current element
@@ -979,10 +954,10 @@ class EyeElement {
979
954
  compute(type) {
980
955
  type = type || "bounds";
981
956
  if (type === "bounds")
982
- return (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw).getBoundingClientRect();
957
+ return (this.#raw[0]).getBoundingClientRect();
983
958
  else if (type == "style")
984
- return getComputedStyle(this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw)
985
- 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"`);
986
961
  }
987
962
 
988
963
  /**
@@ -992,7 +967,7 @@ class EyeElement {
992
967
  */
993
968
  client(attr) {
994
969
  if (['width', 'height', 'left', 'top'].includes(attr))
995
- 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)}`];
996
971
  else console.error(`[EyeJS] Unknown client* attribute "${attr}" in .client(attr)`);
997
972
  return this;
998
973
  }
@@ -1030,9 +1005,8 @@ class EyeElement {
1030
1005
  * @returns {EyeElement|null}
1031
1006
  */
1032
1007
  child(index) {
1033
- let it = (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw);
1034
- if (index === undefined) return it.children.length;
1035
- 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]);
1036
1010
  return null;
1037
1011
  }
1038
1012
  /**
@@ -1042,11 +1016,8 @@ class EyeElement {
1042
1016
  * @returns
1043
1017
  */
1044
1018
  val(value) {
1045
- if (value != undefined) (this.#raw instanceof NodeList ? [...this.#raw.entries()] : [[0, this.#raw]]).forEach(([idx, a]) => a.value = this.#customSet.value("set", value, a));
1046
- else {
1047
- let it = (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw);
1048
- return this.#customSet.value("get", it.value, it);
1049
- }
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]);
1050
1021
  return this;
1051
1022
  }
1052
1023
  /**
@@ -1060,31 +1031,29 @@ class EyeElement {
1060
1031
  let {
1061
1032
  inputs = ["input", "textarea", "select"],
1062
1033
  } = opts;
1063
- if (this.#raw instanceof HTMLElement) {
1064
- let out = {
1065
- json: {},
1066
- url: "",
1067
- fd: new FormData()
1068
- };
1069
- this.#raw.querySelectorAll(inputs.join(','))
1070
- .forEach((inp, i) => {
1071
- let name = inp.name || inp.dataset.name;
1072
- let value = inp.value || inp.textContent;
1073
- if (typeof opts[name] === "function") value = opts[name](inp);
1074
-
1075
- if (inp.type == "file")
1076
- inp.files.forEach(file => {
1077
- out.fd.append(name, file);
1078
- });
1079
- else {
1080
- out.json[name] = value;
1081
- out.fd.append(name, value);
1082
- }
1083
- });
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
+ });
1084
1054
 
1085
- out.url = new URLSearchParams(out.json).toString();
1086
- return out;
1087
- } else console.warn(`[EyeJS] this is a multi selection, it's not serializable!`);
1055
+ out.url = new URLSearchParams(out.json).toString();
1056
+ return out;
1088
1057
  }
1089
1058
  /**
1090
1059
  * Redefine the way `.text` or `.val` set or get data to and from this element.
@@ -1118,20 +1087,20 @@ class EyeElement {
1118
1087
  }
1119
1088
 
1120
1089
  /**
1121
- * Find element/s position/s within parent element
1090
+ * Find first element position within parent element
1122
1091
  * @method EyeElement#position
1123
1092
  * @returns {number}
1124
1093
  */
1125
1094
  position() {
1126
- let poses = [];
1127
- this.each((elm) => {
1128
- for (let i = 0; i < elm.parentNode.children.length; i++) {
1129
- const child = elm.parentNode.children[i];
1130
- 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;
1131
1101
  }
1132
-
1133
- });
1134
- return poses.length == 0 ? poses[0] : poses;
1102
+ }
1103
+ return pos;
1135
1104
  }
1136
1105
  }
1137
1106
  /**
@@ -1179,7 +1148,6 @@ function e(tag, attrs, css) {
1179
1148
  }
1180
1149
  }
1181
1150
 
1182
-
1183
1151
  // gloablly exposed
1184
1152
  window.e = e;
1185
1153
  window.EyeElement = EyeElement;