@danielgindi/selectbox 2.0.52 → 2.0.53

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/eslint.config.mjs CHANGED
@@ -131,7 +131,7 @@ export default [
131
131
  },
132
132
  },
133
133
  {
134
- files: ["scripts/**/*.js", "**/eslint.config.mjs"],
134
+ files: ["scripts/**/*.js", "benchmarks/**/*.mjs", "**/eslint.config.mjs"],
135
135
 
136
136
  languageOptions: {
137
137
  globals: {
package/lib/SelectBox.js CHANGED
@@ -624,7 +624,7 @@ class SelectBox {
624
624
  p.items = items.slice(0);
625
625
  p.itemsChanged = true;
626
626
 
627
- this._updateItemByValueMap();
627
+ this._refreshItemByValueMap();
628
628
 
629
629
  if (resetValues) {
630
630
  this.setSelectedValues(this.getSelectedValues());
@@ -655,10 +655,17 @@ class SelectBox {
655
655
  updateItemByValue(value, newItem) {
656
656
  const p = this._p;
657
657
 
658
- let existingItem = p.itemByValueMap.get(value);
659
- if (existingItem)
658
+ let existingItem = this._getItemByValue(value);
659
+ if (existingItem) {
660
+ const valueProp = existingItem[p.valueProp];
661
+ if (p.itemByValueMap?.has(valueProp))
662
+ p.itemByValueMap.delete(valueProp);
663
+
660
664
  Object.assign(existingItem, newItem);
661
665
 
666
+ p.itemByValueMap.set(existingItem[p.valueProp], existingItem);
667
+ }
668
+
662
669
  if (p.dropList) {
663
670
  p.dropList.updateItemByValue(value, newItem);
664
671
  }
@@ -1228,7 +1235,7 @@ class SelectBox {
1228
1235
  if (p.dropList)
1229
1236
  p.dropList.setValueProp(prop);
1230
1237
 
1231
- this._updateItemByValueMap();
1238
+ this._refreshItemByValueMap();
1232
1239
 
1233
1240
  return this;
1234
1241
  }
@@ -1414,7 +1421,7 @@ class SelectBox {
1414
1421
 
1415
1422
  selectedValues.push(value);
1416
1423
 
1417
- let item = p.itemByValueMap.get(value);
1424
+ let item = this._getItemByValue(value);
1418
1425
  if (item !== undefined) {
1419
1426
  selectedItems.push(item);
1420
1427
  } else {
@@ -1709,7 +1716,7 @@ class SelectBox {
1709
1716
  * This reduces search time greatly (i.e when checking items), especially when Vue proxies are in place.
1710
1717
  * @private
1711
1718
  */
1712
- _updateItemByValueMap() {
1719
+ _refreshItemByValueMap() {
1713
1720
  const p = this._p;
1714
1721
 
1715
1722
  const itemByValueMap = p.itemByValueMap = new Map();
@@ -1717,6 +1724,52 @@ class SelectBox {
1717
1724
  for (let item of p.items) {
1718
1725
  itemByValueMap.set(item[valueProp], item);
1719
1726
  }
1727
+
1728
+ p.subitemByValueMap = null;
1729
+ }
1730
+
1731
+ /**
1732
+ * @param {*} value
1733
+ * @returns {DropList.ItemBase|undefined}
1734
+ * @private
1735
+ */
1736
+ _getItemByValue(value) {
1737
+ const p = this._p;
1738
+ let item = p.itemByValueMap.get(value);
1739
+ if (item !== undefined)
1740
+ return item;
1741
+
1742
+ if (!p.subitemByValueMap)
1743
+ this._refreshSubitemByValueMap();
1744
+
1745
+ return p.subitemByValueMap.get(value);
1746
+ }
1747
+
1748
+ /** @private */
1749
+ _refreshSubitemByValueMap() {
1750
+ const p = this._p;
1751
+ const subitemByValueMap = p.subitemByValueMap = new Map();
1752
+ this._addSubitemsToValueMap(p.items, subitemByValueMap, p.valueProp);
1753
+ }
1754
+
1755
+ /**
1756
+ * @param {DropList.ItemBase[]} items
1757
+ * @param {Map<*, DropList.ItemBase>} itemByValueMap
1758
+ * @param {string} valueProp
1759
+ * @private
1760
+ */
1761
+ _addSubitemsToValueMap(items, itemByValueMap, valueProp) {
1762
+ for (let item of items) {
1763
+ if (!item._subitems?.length)
1764
+ continue;
1765
+
1766
+ for (let subitem of item._subitems) {
1767
+ if (!itemByValueMap.has(subitem[valueProp]))
1768
+ itemByValueMap.set(subitem[valueProp], subitem);
1769
+ }
1770
+
1771
+ this._addSubitemsToValueMap(item._subitems, itemByValueMap, valueProp);
1772
+ }
1720
1773
  }
1721
1774
 
1722
1775
  /** @private */
@@ -2101,7 +2154,8 @@ class SelectBox {
2101
2154
  }
2102
2155
  break;
2103
2156
 
2104
- case 'select': {
2157
+ case 'select':
2158
+ case 'subitems:select': {
2105
2159
  if (p.multi) return;
2106
2160
 
2107
2161
  const item = event.item;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielgindi/selectbox",
3
- "version": "2.0.52",
3
+ "version": "2.0.53",
4
4
  "description": "A collection of dom utilities. So you can work natively with the dom without dom frameworks.",
5
5
  "main": "dist/lib.cjs.min.js",
6
6
  "module": "lib/index.js",
@@ -18,6 +18,7 @@
18
18
  "lib": "lib"
19
19
  },
20
20
  "scripts": {
21
+ "benchmark": "node ./benchmarks/selectbox-value-lookup.mjs",
21
22
  "build": "npm run lint && node ./scripts/build.js",
22
23
  "lint": "eslint -f codeframe ./",
23
24
  "lint-fix": "eslint -f codeframe --fix ./",