@danielgindi/selectbox 1.0.63 → 1.0.66

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/dist/lib.es6.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @danielgindi/selectbox 1.0.63
2
+ * @danielgindi/selectbox 1.0.66
3
3
  * git://github.com/danielgindi/selectbox.git
4
4
  */
5
5
  import { createElement, closestUntil, setElementAttrs, next, prev } from '@danielgindi/dom-utils/lib/Dom';
@@ -1937,17 +1937,17 @@ class DropList {
1937
1937
  _updateGroupStateForItem(item) {
1938
1938
  const p = this._p;
1939
1939
 
1940
- if (p.multi && p.autoCheckGroupChildren) {
1941
-
1942
- let items, groupIndex, itemIndex;
1940
+ if (!p.multi)
1941
+ return this;
1943
1942
 
1944
- if (item._group) {
1945
- // Now loop through children below the group
1943
+ if (item._group) {
1944
+ // Now loop through children below the group
1946
1945
 
1947
- items = p.items;
1948
- groupIndex = items.indexOf(item);
1946
+ let affectedItems = 0;
1949
1947
 
1950
- let affectedItems = 0;
1948
+ if (p.autoCheckGroupChildren) {
1949
+ let items = p.items;
1950
+ let groupIndex = items.indexOf(item);
1951
1951
 
1952
1952
  for (let i = groupIndex + 1, len = items.length; i < len; i++) {
1953
1953
  let next = items[i];
@@ -1980,30 +1980,30 @@ class DropList {
1980
1980
  isCheckingGroup: true,
1981
1981
  });
1982
1982
  }
1983
+ }
1983
1984
 
1984
- // Fire event
1985
- this._trigger('groupcheck', {
1986
- value: item.value,
1987
- item: item[ItemSymbol$1] ?? item,
1988
- affectedItems: affectedItems,
1989
- });
1990
- } else if (p.groupCount > 0) {
1991
- items = p.items;
1992
- itemIndex = items.indexOf(item);
1993
- groupIndex = -1;
1994
-
1995
- // Find the group index
1996
- for (let i = itemIndex - 1; i >= 0; i--) {
1997
- if (items[i]._group) {
1998
- groupIndex = i;
1999
- break;
2000
- }
2001
- }
1985
+ // Fire event
1986
+ this._trigger('groupcheck', {
1987
+ value: item.value,
1988
+ item: item[ItemSymbol$1] ?? item,
1989
+ affectedItems: affectedItems,
1990
+ });
1991
+ } else if (p.groupCount > 0 && p.autoCheckGroupChildren) {
1992
+ let items = p.items;
1993
+ let itemIndex = items.indexOf(item);
1994
+ let groupIndex = -1;
2002
1995
 
2003
- if (groupIndex > -1) {
2004
- this._updateGroupCheckedState(groupIndex, true);
1996
+ // Find the group index
1997
+ for (let i = itemIndex - 1; i >= 0; i--) {
1998
+ if (items[i]._group) {
1999
+ groupIndex = i;
2000
+ break;
2005
2001
  }
2006
2002
  }
2003
+
2004
+ if (groupIndex > -1) {
2005
+ this._updateGroupCheckedState(groupIndex, true);
2006
+ }
2007
2007
  }
2008
2008
 
2009
2009
  return this;
@@ -2012,50 +2012,50 @@ class DropList {
2012
2012
  _updateGroupCheckedState(groupIndex, fireEvents) {
2013
2013
  const p = this._p;
2014
2014
 
2015
- if (p.multi && p.autoCheckGroupChildren && groupIndex > -1) {
2015
+ if (!(p.multi && p.autoCheckGroupChildren && groupIndex > -1))
2016
+ return this;
2016
2017
 
2017
- let items = p.items;
2018
- let groupItem = items[groupIndex];
2019
- if (!groupItem || !groupItem._group) return this;
2018
+ let items = p.items;
2019
+ let groupItem = items[groupIndex];
2020
2020
 
2021
- let item, hasChecked = false, hasUnchecked = false;
2021
+ if (!groupItem || !groupItem._group) return this;
2022
2022
 
2023
- for (let i = groupIndex + 1, len = items.length; i < len; i++) {
2024
- item = items[i];
2023
+ let item, hasChecked = false, hasUnchecked = false;
2025
2024
 
2026
- // Hit the next group, break out
2027
- if (item._group || (!item._child && items[i - 1]._child))
2028
- break;
2025
+ for (let i = groupIndex + 1, len = items.length; i < len; i++) {
2026
+ item = items[i];
2029
2027
 
2030
- if (item._checked) {
2031
- hasChecked = true;
2032
- } else if (!item._checked) {
2033
- hasUnchecked = true;
2034
- }
2028
+ // Hit the next group, break out
2029
+ if (item._group || (!item._child && items[i - 1]._child))
2030
+ break;
2031
+
2032
+ if (item._checked) {
2033
+ hasChecked = true;
2034
+ } else if (!item._checked) {
2035
+ hasUnchecked = true;
2035
2036
  }
2037
+ }
2036
2038
 
2037
- // See if we need to update the group
2038
- let shouldCheckGroup = hasChecked && !hasUnchecked;
2039
- if (!!groupItem._checked !== shouldCheckGroup) {
2040
- // Update state
2041
- groupItem._checked = shouldCheckGroup;
2039
+ let shouldCheckGroup = hasChecked && !hasUnchecked;
2040
+ if (!!groupItem._checked !== shouldCheckGroup) {
2041
+ // Update state
2042
+ groupItem._checked = shouldCheckGroup;
2042
2043
 
2043
- // Update DOM
2044
- let nextEl = p.virtualListHelper.getItemElementAt(groupIndex);
2045
- if (nextEl) {
2046
- toggleClass(nextEl, `${p.baseClassName}__item_checked`, groupItem._checked);
2047
- }
2044
+ // Update DOM
2045
+ let nextEl = p.virtualListHelper.getItemElementAt(groupIndex);
2046
+ if (nextEl) {
2047
+ toggleClass(nextEl, `${p.baseClassName}__item_checked`, groupItem._checked);
2048
+ }
2048
2049
 
2049
- if (fireEvents) {
2050
- // Fire event
2051
- this._trigger('check', {
2052
- value: groupItem.value,
2053
- item: groupItem[ItemSymbol$1] ?? groupItem,
2054
- checked: groupItem._checked,
2055
- isGroup: groupItem._group,
2056
- isCheckingGroup: false,
2057
- });
2058
- }
2050
+ if (fireEvents) {
2051
+ // Fire event
2052
+ this._trigger('check', {
2053
+ value: groupItem.value,
2054
+ item: groupItem[ItemSymbol$1] ?? groupItem,
2055
+ checked: groupItem._checked,
2056
+ isGroup: groupItem._group,
2057
+ isCheckingGroup: false,
2058
+ });
2059
2059
  }
2060
2060
  }
2061
2061
 
@@ -2299,6 +2299,7 @@ const inputBackbufferCssProps = [
2299
2299
  * @property {boolean} [sortSelectedItems=true] Should the selected items be sorted?
2300
2300
  * @property {boolean} [sortListItems=false] Sort list items
2301
2301
  * @property {boolean} [sortListCheckedFirst=true] When sorting - put checked items first (applicable to `multi` mode only)
2302
+ * @property {boolean} [treatGroupSelectionAsItems=false] Treat group items as normal items
2302
2303
  * @property {*[]} [stickyValues]
2303
2304
  * @property {function(a: DropList.ItemBase, b: DropList.ItemBase):number} [sortItemComparator]
2304
2305
  * @property {boolean} [splitListCheckedGroups=true] Split groups to "checked" and "unchecked", works with `sortCheckedFirst` only
@@ -2344,6 +2345,7 @@ const defaultOptions = {
2344
2345
  stickyValues: null,
2345
2346
  sortItemComparator: null,
2346
2347
  splitListCheckedGroups: true,
2348
+ treatGroupSelectionAsItems: false,
2347
2349
  blurOnSingleSelection: 'touch',
2348
2350
  multi: false,
2349
2351
  showSelection: true,
@@ -2433,6 +2435,7 @@ class SelectBox {
2433
2435
  stickyValues: Array.isArray(o.stickyValues) ? new Set(o.stickyValues) : null,
2434
2436
  sortItemComparator: o.sortItemComparator,
2435
2437
  splitListCheckedGroups: !!o.splitListCheckedGroups,
2438
+ treatGroupSelectionAsItems: o.treatGroupSelectionAsItems,
2436
2439
  blurOnSingleSelection: o.blurOnSingleSelection,
2437
2440
  multi: o.multi,
2438
2441
  showSelection: o.showSelection,
@@ -2978,6 +2981,29 @@ class SelectBox {
2978
2981
  return this._p.sortItemComparator;
2979
2982
  }
2980
2983
 
2984
+ /**
2985
+ * @param {boolean} treatGroupSelectionAsItems
2986
+ * @returns {SelectBox}
2987
+ */
2988
+ setTreatGroupSelectionAsItems(treatGroupSelectionAsItems) {
2989
+ const p = this._p;
2990
+ treatGroupSelectionAsItems = !!treatGroupSelectionAsItems;
2991
+ if (p.treatGroupSelectionAsItems === treatGroupSelectionAsItems)
2992
+ return this;
2993
+
2994
+ p.treatGroupSelectionAsItems = treatGroupSelectionAsItems;
2995
+ p.itemsChanged = true;
2996
+ this._scheduleSync();
2997
+ return this;
2998
+ }
2999
+
3000
+ /**
3001
+ * @returns {boolean}
3002
+ */
3003
+ isTreatGroupSelectionAsItemsEnabled() {
3004
+ return !this._p.treatGroupSelectionAsItems;
3005
+ }
3006
+
2981
3007
  /**
2982
3008
  * @param {boolean} splitListCheckedGroups
2983
3009
  * @returns {SelectBox}
@@ -3929,7 +3955,7 @@ class SelectBox {
3929
3955
  const value = event.value;
3930
3956
 
3931
3957
  let checked = event.checked;
3932
- if (event.isGroup) return; // Ignore groups
3958
+ if (event.isGroup && !p.treatGroupSelectionAsItems) return; // Ignore groups
3933
3959
 
3934
3960
  let selEvt = { value: value, item: item, cancel: false };
3935
3961
  this._trigger((checked ? 'addsel' : 'removesel') + ':before', selEvt);
@@ -3965,7 +3991,7 @@ class SelectBox {
3965
3991
  this._scheduleSync('full');
3966
3992
  } else {
3967
3993
  if (p.maxMultiItems != null &&
3968
- p.selectedItems.filter(x => !x._group).length > p.maxMultiItems) {
3994
+ (p.treatGroupSelectionAsItems ? p.selectedItems : p.selectedItems.filter(x => !x._group)).length > p.maxMultiItems) {
3969
3995
  this._scheduleSync('addOrUpdateMultiItemRestElement');
3970
3996
  } else {
3971
3997
  this._scheduleSync('addMultiItemElement', item);
@@ -3976,7 +4002,7 @@ class SelectBox {
3976
4002
  }
3977
4003
  } else {
3978
4004
  if (p.maxMultiItems != null &&
3979
- p.selectedItems.filter(x => !x._group).length === p.maxMultiItems) {
4005
+ (p.treatGroupSelectionAsItems ? p.selectedItems : p.selectedItems.filter(x => !x._group)).length === p.maxMultiItems) {
3980
4006
  this._scheduleSync('removeMultiItemRestElement');
3981
4007
  } else {
3982
4008
  this._scheduleSync('removeMultiItemElement', item);
@@ -4428,6 +4454,7 @@ class SelectBox {
4428
4454
  const p = this._p;
4429
4455
 
4430
4456
  const term = p.filterTerm;
4457
+ const treatGroupSelectionAsItems = p.treatGroupSelectionAsItems;
4431
4458
 
4432
4459
  if (term || (p.filterOnEmptyTerm && p.filterFn)) {
4433
4460
  let fn = p.filterFn;
@@ -4446,7 +4473,7 @@ class SelectBox {
4446
4473
  multiItemLabelProp = p.multiItemLabelProp;
4447
4474
 
4448
4475
  filteredItems = p.items.filter(x => {
4449
- if (x._group) return true;
4476
+ if (!treatGroupSelectionAsItems && x._group) return true;
4450
4477
  return matcher.test(x[labelProp] || x[multiItemLabelProp]);
4451
4478
  });
4452
4479
  } else {
@@ -4456,7 +4483,7 @@ class SelectBox {
4456
4483
 
4457
4484
  p.filteredItems = filteredItems;
4458
4485
 
4459
- if (filteredItems) {
4486
+ if (filteredItems && !treatGroupSelectionAsItems) {
4460
4487
  // Clean up groups without children
4461
4488
 
4462
4489
  let lastGroup = -1;
@@ -4698,15 +4725,18 @@ class SelectBox {
4698
4725
 
4699
4726
  /**
4700
4727
  * @param {DropList.ItemBase} item
4728
+ * @returns {boolean} true if rendered, false if not
4701
4729
  * @private
4702
4730
  */
4703
4731
  _addMultiItemElement(item) {
4704
4732
  const p = this._p;
4705
4733
  const itemEl = this._renderMultiItem(item);
4706
- if (!itemEl) return;
4734
+ if (!itemEl) return false;
4707
4735
 
4708
4736
  before(p.inputWrapper, itemEl);
4709
4737
  p.multiItemEls.push(itemEl);
4738
+
4739
+ return true;
4710
4740
  }
4711
4741
 
4712
4742
  /** @private */
@@ -4714,7 +4744,7 @@ class SelectBox {
4714
4744
  const p = this._p;
4715
4745
 
4716
4746
  let items = p.selectedItems.slice(0);
4717
- let count = items.filter(x => !x._group).length - p.maxMultiItems;
4747
+ let count = (p.treatGroupSelectionAsItems ? items : items.filter(x => !x._group)).length - p.maxMultiItems;
4718
4748
  let label = p.multiItemsRestLabelProvider
4719
4749
  ? p.multiItemsRestLabelProvider(count, items)
4720
4750
  : `+ ${count}`;
@@ -4728,10 +4758,13 @@ class SelectBox {
4728
4758
 
4729
4759
  /** @private */
4730
4760
  _syncClearButton() {
4731
- const p = this._p;
4761
+ const p = this._p,
4762
+ multiItemLabelProp = p.multiItemLabelProp;
4732
4763
 
4733
4764
  // Set clear button
4734
- if (p.selectedItems.length > 0 && p.clearable && p.showSelection) {
4765
+ if (p.selectedItems.length > 0 &&
4766
+ p.selectedItems.some(x => x[multiItemLabelProp] !== false) &&
4767
+ p.clearable && p.showSelection) {
4735
4768
  if (!p.clearButtonWrapper) {
4736
4769
  p.clearButtonWrapper = createElement(
4737
4770
  p.multi ? 'li' : 'span',
@@ -4761,7 +4794,8 @@ class SelectBox {
4761
4794
 
4762
4795
  /** @private */
4763
4796
  _syncPlaceholder() {
4764
- const p = this._p;
4797
+ const p = this._p,
4798
+ multiItemLabelProp = p.multiItemLabelProp;
4765
4799
 
4766
4800
  let placeholder = '';
4767
4801
 
@@ -4771,7 +4805,9 @@ class SelectBox {
4771
4805
  } else {
4772
4806
  placeholder = defaultMultiPlaceholderFormatter(p.selectedItems, p.labelProp);
4773
4807
  }
4774
- } else if (p.selectedItems.length === 0 || !p.showSelection) {
4808
+ } else if (p.selectedItems.length === 0 ||
4809
+ !p.showSelection ||
4810
+ p.selectedItems.every(x => x[multiItemLabelProp] === false)) {
4775
4811
  placeholder = p.placeholder == null ? '' : (p.placeholder + '');
4776
4812
  }
4777
4813
 
@@ -4800,13 +4836,16 @@ class SelectBox {
4800
4836
  * @returns {SelectBox}
4801
4837
  */
4802
4838
  _syncFull(fullItemsRender) {
4803
- const p = this._p;
4839
+ const p = this._p,
4840
+ multiItemLabelProp = p.multiItemLabelProp;
4804
4841
 
4805
4842
  this._renderBase();
4806
4843
  this._syncClearButton();
4807
4844
  this._syncPlaceholder();
4808
4845
 
4809
- fullItemsRender = p.multi && p.showSelection && (fullItemsRender || p.selectedItems.length !== p.multiItemEls.length);
4846
+ fullItemsRender = p.multi &&
4847
+ p.showSelection &&
4848
+ (fullItemsRender || p.selectedItems.filter(x => x[multiItemLabelProp] !== false).length !== p.multiItemEls.length);
4810
4849
 
4811
4850
  if (fullItemsRender || !p.showSelection || !p.multi) {
4812
4851
  // Remove all item elements
@@ -4819,6 +4858,7 @@ class SelectBox {
4819
4858
  if (p.multi) {
4820
4859
  if (fullItemsRender) {
4821
4860
  const items = p.selectedItems;
4861
+ const treatGroupSelectionAsItems = p.treatGroupSelectionAsItems;
4822
4862
 
4823
4863
  // Sort these
4824
4864
  if (p.sortSelectedItems) {
@@ -4851,15 +4891,16 @@ class SelectBox {
4851
4891
 
4852
4892
  // Add item elements
4853
4893
  for (let i = 0; i < items.length; i++) {
4854
- if (items[i]._group) continue;
4894
+ if (!treatGroupSelectionAsItems && items[i]._group) continue;
4855
4895
 
4856
4896
  if (max != null && actualItemCount === max) {
4857
4897
  addRestItem = true;
4858
4898
  break;
4859
4899
  }
4860
4900
 
4861
- actualItemCount++;
4862
- this._addMultiItemElement(items[i]);
4901
+ if (this._addMultiItemElement(items[i])) {
4902
+ actualItemCount++;
4903
+ }
4863
4904
  }
4864
4905
 
4865
4906
  if (addRestItem) {
@@ -4964,7 +5005,8 @@ class SelectBox {
4964
5005
 
4965
5006
  if (item !== undefined) {
4966
5007
  if (value === RestMultiItemsSymbol) {
4967
- let items = p.selectedItems.filter(x => !x._group).slice(p.maxMultiItems);
5008
+ let items = (p.treatGroupSelectionAsItems ? p.selectedItems : p.selectedItems.filter(x => !x._group))
5009
+ .slice(p.maxMultiItems);
4968
5010
  let itemsToRemove = [];
4969
5011
 
4970
5012
  for (let item of items) {