eyjs 7.0.0 → 7.1.1

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.1] - 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,20 @@ 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))];
422
+ } else if (Array.isArray(selector)) {
423
+ this.#raw = [];
424
+ selector.forEach((a) => {
425
+ if (a instanceof EyeElement) _this.#raw = _this.#raw.concat(...a.raw);
426
+ else if (a instanceof HTMLElement) _this.#raw.push(a);
427
+ });
417
428
  } else {
418
429
  // selecting
419
430
  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);
431
+ this.#raw = [...document.querySelectorAll(s ? selector.slice(0, -1) : selector)];
424
432
  }
425
433
 
426
434
  /**
@@ -484,7 +492,6 @@ class EyeElement {
484
492
  };
485
493
  });
486
494
 
487
-
488
495
  return this;
489
496
  }
490
497
 
@@ -493,7 +500,7 @@ class EyeElement {
493
500
  * @type {Number}
494
501
  */
495
502
  get length() {
496
- return this.#raw instanceof NodeList ? this.#raw.length : 1;
503
+ return this.#raw.length;
497
504
  }
498
505
 
499
506
  /**
@@ -511,10 +518,9 @@ class EyeElement {
511
518
  * @returns {EyeElement}
512
519
  */
513
520
  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);
521
+ for (let i = 0; i < this.#raw.length; i++) {
522
+ const elm = this.#raw[i];
523
+ let exit = cb(elm, i, this);
518
524
  if (exit === false) break;
519
525
  }
520
526
  return this;
@@ -680,46 +686,39 @@ class EyeElement {
680
686
  return this;
681
687
  }
682
688
  /**
683
- * Append one or more elements to the current element
689
+ * Append one or more elements to the current element, only effect the first element in the selected list
684
690
  * @method EyeElement#append
685
691
  * @param {HTMLElement|Array<Node|EyeElement>} elm
686
692
  * @param {"next" | "after" | "previous" | "before" | "first" | "afterbegin" | "last" | "beforeend"} [pos] [optional]
687
693
  * @returns {EyeElement}
688
694
  */
689
695
  append(elm, pos) {
690
- let nodes = [];
691
696
  (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]);
697
+ let nodes = [];
698
+ if (item instanceof EyeElement) nodes = [...item.raw];
699
+ else if (item instanceof HTMLElement) nodes = [item];
700
+
701
+ nodes.forEach(node => {
702
+ switch (pos) {
703
+ case "next":
704
+ case "after":
705
+ this.#raw[0].after(node);
706
+ break;
707
+ case "previous":
708
+ case "before":
709
+ this.#raw[0].before(node);
710
+ break;
711
+ case "afterbegin":
712
+ case "first":
713
+ this.#raw[0].prepend(node);
714
+ break;
715
+ case "beforeend":
716
+ case "last":
717
+ default:
718
+ this.#raw[0].append(node);
719
+ }
700
720
  });
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
- }
721
+ });
723
722
  return this;
724
723
  }
725
724
  /**
@@ -729,7 +728,7 @@ class EyeElement {
729
728
  * @returns {EyeElement}
730
729
  */
731
730
  after(elm) {
732
- (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw).after(elm instanceof EyeElement ? elm.raw : elm);
731
+ this.#raw[0].after(elm instanceof EyeElement ? elm.raw : elm);
733
732
  return this;
734
733
  }
735
734
  /**
@@ -739,7 +738,7 @@ class EyeElement {
739
738
  * @returns {EyeElement}
740
739
  */
741
740
  before(elm) {
742
- (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw).before(elm instanceof EyeElement ? elm.raw : elm);
741
+ this.#raw[0].before(elm instanceof EyeElement ? elm.raw : elm);
743
742
  return this;
744
743
  }
745
744
  /**
@@ -752,17 +751,10 @@ class EyeElement {
752
751
  replaceWith(...elms) {
753
752
  let nodes = [];
754
753
  (Array.isArray(elms) ? elms : [elms]).forEach(item => {
755
- if (item instanceof EyeElement) nodes.push(item.#raw);
754
+ if (item instanceof EyeElement) nodes = nodes.concat(...item.raw);
756
755
  else if (item instanceof HTMLElement) nodes.push(item);
757
756
  });
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
- }
757
+ this.#raw[0].replaceWith(...nodes);
766
758
  return this;
767
759
  }
768
760
  /**
@@ -783,27 +775,23 @@ class EyeElement {
783
775
  });
784
776
  return this;
785
777
  }
786
- return e(this.#raw instanceof NodeList ? this.#raw.item(0).parentElement : this.#raw.parentElement);
778
+ return e(this.#raw[0].parentElement);
787
779
  }
788
780
  /**
789
- * Returns whether current node is the same/equal(depending on `check`) as the passed node or not
781
+ * Returns whether current node is the same/equal (depending on `check`) as the passed node or not
790
782
  * @method EyeElement#is
791
783
  * @param {HTMLElement|EyeElement} node
792
784
  * @param {"connected" | "same" | "equal"} [check] check type `same`, `equal`
793
785
  * @returns {boolean}
794
786
  */
795
787
  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;
788
+ node = node instanceof EyeElement ? node.#raw[0] : node;
789
+ if (node === "connected") return this.#raw[0].isConnected;
802
790
  switch (check) {
803
791
  case "same":
804
- return this.#raw.isSameNode(node);
792
+ return this.#raw[0].isSameNode(node);
805
793
  case "equal":
806
- return this.#raw.isEqualNode(node);
794
+ return this.#raw[0].isEqualNode(node);
807
795
  default:
808
796
  console.error(
809
797
  `[EyeJS] Unknown check "${check}", possible values are ["same","equal","connected"]`
@@ -820,6 +808,7 @@ class EyeElement {
820
808
  */
821
809
  css(attr, value) {
822
810
  if (attr) {
811
+ if (!unitlessProps.has(attr) && typeof value === "number" && value != 0) value = `${value}px`;
823
812
  let out = undefined;
824
813
  attr = flat(attr);
825
814
  this.each((elm, idx) => {
@@ -827,7 +816,7 @@ class EyeElement {
827
816
  elm.style[attr] = value;
828
817
  });
829
818
  return out != undefined ? out : this;
830
- } else return console.error(`[EyeJS] mission argument "attr" in function .css`);
819
+ } else return console.error(`[EyeJS] missing argument "attr" in function .css`);
831
820
  }
832
821
  /**
833
822
  * Remove current element
@@ -870,8 +859,6 @@ class EyeElement {
870
859
  "[EyeJS] .on method is missing the actuall callback `cb` or not of type function"
871
860
  );
872
861
 
873
- let elms = (this.#raw instanceof NodeList ? [...this.#raw.entries()] : [[0, this.#raw]]);
874
-
875
862
  let outsider = null;
876
863
  ev.forEach(evt => {
877
864
  if (target) {
@@ -882,7 +869,7 @@ class EyeElement {
882
869
  _this.#dlgListeners.set(evt, new Set());
883
870
  _this.#dlgListeners.get(evt).add({ callback: cb, target });
884
871
  } else {
885
- elms.forEach(([idx, elm]) => {
872
+ _this.each((elm) => {
886
873
  elm.addEventListener(evt, cb);
887
874
  });
888
875
  }
@@ -954,21 +941,15 @@ class EyeElement {
954
941
  * Returns a clone of current selected element/s
955
942
  * @method EyeElement#clone
956
943
  * @param {HTMLElement} [parent] optionally append new clone to a parent
957
- * @returns {Array<EyeElement>|EyeElement}
944
+ * @returns {Array<EyeElement>}
958
945
  */
959
946
  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
- }
947
+ let list = [];
948
+ this.each((nd) => {
949
+ list.push(nd.cloneNode(true));
950
+ });
951
+ if (parent instanceof HTMLElement || parent instanceof EyeElement) list.forEach(el => parent.append(el));
952
+ return list;
972
953
  }
973
954
  /**
974
955
  * Compute DOMRect or style declaration of current element
@@ -979,10 +960,10 @@ class EyeElement {
979
960
  compute(type) {
980
961
  type = type || "bounds";
981
962
  if (type === "bounds")
982
- return (this.#raw instanceof NodeList ? this.#raw.item(0) : this.#raw).getBoundingClientRect();
963
+ return (this.#raw[0]).getBoundingClientRect();
983
964
  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"`);
965
+ return getComputedStyle(this.#raw[0])
966
+ console.error(`[EyeJS] unknown type "${type}" in function .compute, possible values are "bounds" "style"`);
986
967
  }
987
968
 
988
969
  /**
@@ -992,7 +973,7 @@ class EyeElement {
992
973
  */
993
974
  client(attr) {
994
975
  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)}`];
976
+ return this.#raw[0][`client${attr[0].toUpperCase()}${attr.substring(1, attr.length)}`];
996
977
  else console.error(`[EyeJS] Unknown client* attribute "${attr}" in .client(attr)`);
997
978
  return this;
998
979
  }
@@ -1030,9 +1011,8 @@ class EyeElement {
1030
1011
  * @returns {EyeElement|null}
1031
1012
  */
1032
1013
  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]);
1014
+ if (index === undefined) return this.#raw[0].children.length;
1015
+ if (this.#raw[0].children[index]) return e(this.#raw[0].children[index]);
1036
1016
  return null;
1037
1017
  }
1038
1018
  /**
@@ -1042,11 +1022,8 @@ class EyeElement {
1042
1022
  * @returns
1043
1023
  */
1044
1024
  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
- }
1025
+ if (value != undefined) this.each((a) => a.value = this.#customSet.value("set", value, a));
1026
+ else return this.#customSet.value("get", this.#raw[0].value, this.#raw[0]);
1050
1027
  return this;
1051
1028
  }
1052
1029
  /**
@@ -1060,31 +1037,29 @@ class EyeElement {
1060
1037
  let {
1061
1038
  inputs = ["input", "textarea", "select"],
1062
1039
  } = 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
- });
1040
+ let out = {
1041
+ json: {},
1042
+ url: "",
1043
+ fd: new FormData()
1044
+ };
1045
+ this.#raw[0].querySelectorAll(inputs.join(','))
1046
+ .forEach((inp, i) => {
1047
+ let name = inp.name || inp.dataset.name;
1048
+ let value = inp.value || inp.textContent;
1049
+ if (typeof opts[name] === "function") value = opts[name](inp);
1050
+
1051
+ if (inp.type == "file")
1052
+ inp.files.forEach(file => {
1053
+ out.fd.append(name, file);
1054
+ });
1055
+ else {
1056
+ out.json[name] = value;
1057
+ out.fd.append(name, value);
1058
+ }
1059
+ });
1084
1060
 
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!`);
1061
+ out.url = new URLSearchParams(out.json).toString();
1062
+ return out;
1088
1063
  }
1089
1064
  /**
1090
1065
  * Redefine the way `.text` or `.val` set or get data to and from this element.
@@ -1118,20 +1093,20 @@ class EyeElement {
1118
1093
  }
1119
1094
 
1120
1095
  /**
1121
- * Find element/s position/s within parent element
1096
+ * Find first element position within parent element
1122
1097
  * @method EyeElement#position
1123
1098
  * @returns {number}
1124
1099
  */
1125
1100
  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);
1101
+ let pos = -1;
1102
+ for (let i = 0; i < this.#raw[0].parentNode.children.length; i++) {
1103
+ const child = this.#raw[0].parentNode.children[i];
1104
+ if (this.#raw[0].isSameNode(child)) {
1105
+ pos = i;
1106
+ break;
1131
1107
  }
1132
-
1133
- });
1134
- return poses.length == 0 ? poses[0] : poses;
1108
+ }
1109
+ return pos;
1135
1110
  }
1136
1111
  }
1137
1112
  /**
@@ -1179,7 +1154,6 @@ function e(tag, attrs, css) {
1179
1154
  }
1180
1155
  }
1181
1156
 
1182
-
1183
1157
  // gloablly exposed
1184
1158
  window.e = e;
1185
1159
  window.EyeElement = EyeElement;