@danielgindi/selectbox 1.0.28 → 1.0.32

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/lib/SelectBox.js CHANGED
@@ -100,7 +100,9 @@ const inputBackbufferCssProps = [
100
100
  * @property {string} [multiItemLabelProp='short_label']
101
101
  * @property {number} [maxMultiItems] maximum number of multi items. The rest will get a single item to represent.
102
102
  * @property {function(count: number, items: DropList.ItemBase[]):string} [multiItemsRestLabelProvider] label for the item representing the rest of the items.
103
- * @property {DropList.ItemBase[]|null} [items]
103
+ * @property {DropList.ItemBase[]|null} [items] initial items
104
+ * @property {*[]|null} [selectedValues] initial selected values
105
+ * @property {*|*[]|null} [value] initial selected value
104
106
  * @property {function(item: DropList.ItemBase, itemEl: Element):(*|false)} [renderSingleItem]
105
107
  * @property {function(item: DropList.ItemBase, itemEl: Element)} [unrenderSingleItem]
106
108
  * @property {function(item: DropList.ItemBase, itemEl: Element):(*|false)} [renderMultiItem]
@@ -112,6 +114,7 @@ const inputBackbufferCssProps = [
112
114
  * @property {function(items: DropList.ItemBase[], term: string):(DropList.ItemBase[]|null)} [filterFn]
113
115
  * @property {function(name: string, ...args)} [on]
114
116
  * @property {string|string[]} [additionalClasses]
117
+ * @property {boolean} [isLoadingMode]
115
118
  * */
116
119
  const defaultOptions = {
117
120
  el: null,
@@ -140,6 +143,9 @@ const defaultOptions = {
140
143
  maxMultiItems: null,
141
144
  multiItemsRestLabelProvider: null,
142
145
  items: [],
146
+ selectedValues: undefined,
147
+ value: undefined,
148
+ isLoadingMode: false,
143
149
  };
144
150
 
145
151
  /**
@@ -237,12 +243,12 @@ class SelectBox {
237
243
  renderNoResultsItem: o.renderNoResultsItem,
238
244
  unrenderNoResultsItem: o.unrenderNoResultsItem,
239
245
  filterFn: o.filterFn,
240
- on: o.on,
246
+ on: null,
241
247
  additionalClasses: o.additionalClasses,
242
248
 
243
- isLoadingMode: false,
249
+ isLoadingMode: !!o.isLoadingMode,
244
250
 
245
- items: o.items || [],
251
+ items: [],
246
252
  filteredItems: null,
247
253
  itemsChanged: true,
248
254
 
@@ -336,7 +342,16 @@ class SelectBox {
336
342
  p.sink.add(window, 'resize', () => this._resizeInput());
337
343
  p.sink.add(window, 'orientationchange', () => this._resizeInput());
338
344
 
339
- this.setItems(p.items);
345
+ this.setItems(o.items);
346
+
347
+ if (o.multi && Array.isArray(o.selectedValues)) {
348
+ this.setSelectedValues(o.selectedValues);
349
+ } else if (o.value != null) {
350
+ this.setValue(o.value);
351
+ }
352
+
353
+ // Start firing events
354
+ p.on = o.on || null;
340
355
 
341
356
  this._scheduleSync();
342
357
 
@@ -1304,13 +1319,22 @@ class SelectBox {
1304
1319
  * @returns {SelectBox}
1305
1320
  */
1306
1321
  toggleLoading(on) {
1322
+ return this.setIsLoadingMode(on === undefined ? !this.getIsLoadingMode() : !!on);
1323
+ }
1324
+
1325
+ /**
1326
+ * @param {boolean} isLoadingMode
1327
+ * @returns {SelectBox}
1328
+ */
1329
+ setIsLoadingMode(isLoadingMode) {
1307
1330
  const p = this._p;
1308
- let loading = on === undefined ? !this._p.isLoadingMode : !!on;
1309
1331
 
1310
- if (p.isLoadingMode === loading)
1332
+ isLoadingMode = isLoadingMode === undefined ? true : !!isLoadingMode;
1333
+
1334
+ if (p.isLoadingMode === isLoadingMode)
1311
1335
  return this;
1312
1336
 
1313
- p.isLoadingMode = loading;
1337
+ p.isLoadingMode = isLoadingMode;
1314
1338
 
1315
1339
  if (p.isLoadingMode && p.items.length === 0 && this.isListOpen()) {
1316
1340
  this.closeList();
@@ -1324,6 +1348,12 @@ class SelectBox {
1324
1348
  return this;
1325
1349
  }
1326
1350
 
1351
+ /**
1352
+ * @returns {boolean}
1353
+ */
1354
+ getIsLoadingMode() {
1355
+ return this._p.isLoadingMode;
1356
+ }
1327
1357
 
1328
1358
  /**
1329
1359
  * Can be called in case that the selectbox was attached to the dom late and has a weird size.
@@ -2960,7 +2990,7 @@ class SelectBox {
2960
2990
  return 0;
2961
2991
  });
2962
2992
 
2963
- let group = [];
2993
+ let group = [], stickyGroup = null;
2964
2994
  let groups = [group];
2965
2995
  const selectedValuesSet = new Set(p.selectedValues);
2966
2996
  let item, i, len;
@@ -2980,12 +3010,27 @@ class SelectBox {
2980
3010
  groups.length = 0;
2981
3011
  }
2982
3012
 
3013
+ // Non-grouped sticky should be first
3014
+ if (stickyValues && items.length > 0 && !items[0]._group) {
3015
+ stickyGroup = groups[0].filter(x => stickyValues.has(x[valueProp]));
3016
+ if (stickyGroup.length > 0) {
3017
+ groups[0] = groups[0].filter(x => !stickyValues.has(x[valueProp]));
3018
+ }
3019
+ }
3020
+
2983
3021
  if (sort) {
2984
3022
  // Sort the groups
2985
3023
  groups.sort((a, b) => {
2986
3024
  a = a[0];
2987
3025
  b = b[0];
2988
3026
 
3027
+ if (stickyValues !== null) {
3028
+ let sa = stickyValues.has(a[valueProp]);
3029
+ let sb = stickyValues.has(b[valueProp]);
3030
+ if (sa && !sb) return -1;
3031
+ if (!sa && sb) return 1;
3032
+ }
3033
+
2989
3034
  // A "group" without a group item will come first
2990
3035
  if (!a._group && b._group) return -1;
2991
3036
  if (a._group && !b._group) return 1;
@@ -2994,6 +3039,10 @@ class SelectBox {
2994
3039
  });
2995
3040
  }
2996
3041
 
3042
+ if (stickyGroup && stickyGroup.length > 0) {
3043
+ groups.unshift(stickyGroup);
3044
+ }
3045
+
2997
3046
  // Now we have an array of groups, possibly sorted.
2998
3047
  // Each group is an array that begins with the group item (group name/id).
2999
3048
  // A group could possible start with a normal item, if it's a "default group", which had no group item.
@@ -3007,7 +3056,7 @@ class SelectBox {
3007
3056
  // Sort each group
3008
3057
  group.sort((a, b) => {
3009
3058
 
3010
- // Grouped items come first
3059
+ // Grouping items come first
3011
3060
  if (a._group && !b._group) return -1;
3012
3061
  if (!a._group && b._group) return 1;
3013
3062
 
@@ -3037,6 +3086,15 @@ class SelectBox {
3037
3086
  for (let g = 0, glen = groups.length; g < glen; g++) {
3038
3087
  group = groups[g];
3039
3088
 
3089
+ if (group === stickyGroup) {
3090
+ checkedGroups.push(stickyGroup);
3091
+ let sgi = uncheckedGroups.indexOf(stickyGroup);
3092
+ if (sgi !== -1) {
3093
+ uncheckedGroups.splice(sgi, 1);
3094
+ }
3095
+ continue;
3096
+ }
3097
+
3040
3098
  virtualGroup = null;
3041
3099
 
3042
3100
  for (let gi = 0, gilen = group.length; gi < gilen; gi++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielgindi/selectbox",
3
- "version": "1.0.28",
3
+ "version": "1.0.32",
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",
@@ -31,21 +31,21 @@
31
31
  "homepage": "https://github.com/danielgindi/selectbox#readme",
32
32
  "license": "MIT",
33
33
  "devDependencies": {
34
- "@babel/core": "^7.14.8",
35
- "@babel/preset-env": "^7.14.9",
36
- "@babel/runtime": "^7.14.8",
34
+ "@babel/core": "^7.15.5",
35
+ "@babel/preset-env": "^7.15.6",
36
+ "@babel/runtime": "^7.15.4",
37
37
  "@rollup/plugin-babel": "^5.3.0",
38
38
  "@rollup/plugin-commonjs": "^20.0.0",
39
39
  "@rollup/plugin-node-resolve": "^13.0.4",
40
- "core-js": "^3.16.0",
40
+ "core-js": "^3.17.3",
41
41
  "eslint": "^7.32.0",
42
- "eslint-plugin-vue": "^7.15.1",
42
+ "eslint-plugin-vue": "^7.17.0",
43
43
  "fs-extra": "^10.0.0",
44
- "husky": "^7.0.1",
44
+ "husky": "^7.0.2",
45
45
  "pinst": "^2.1.6",
46
- "rollup": "^2.55.1",
46
+ "rollup": "^2.56.3",
47
47
  "rollup-plugin-terser": "^7.0.2",
48
- "sass": "^1.37.2"
48
+ "sass": "^1.39.2"
49
49
  },
50
50
  "dependencies": {
51
51
  "@danielgindi/dom-utils": "^1.0.3",
package/vue/SelectBox.vue CHANGED
@@ -226,6 +226,10 @@
226
226
  type: Boolean,
227
227
  default: false,
228
228
  },
229
+ isLoadingMode: {
230
+ type: Boolean,
231
+ default: false,
232
+ },
229
233
  },
230
234
 
231
235
  data() {
@@ -481,6 +485,11 @@
481
485
  if (this._box)
482
486
  this._box.setAdditionalClasses(this.additionalClassesList);
483
487
  },
488
+
489
+ isLoadingMode() {
490
+ if (this._box)
491
+ this._box.setIsLoadingMode(!!this.isLoadingMode);
492
+ },
484
493
  },
485
494
 
486
495
  mounted() {
@@ -652,6 +661,7 @@
652
661
  filterFn: this.filterFn,
653
662
  on: this._handleBoxEvents.bind(this),
654
663
  additionalClasses: this.additionalClassesList,
664
+ isLoadingMode: this.isLoadingMode,
655
665
  });
656
666
 
657
667
  box.setValue(this.value === null ? undefined : this.value);