PageSourceSearch

https://www.trade.gov/modules/contrib/cshs/build/jquery.simpler-select.js?v=11.4.4

js trade.gov collected 2026-09-24 06:44:34 UTC 11,530 bytes, 327 lines download raw bytes

1/******/ (function() { // webpackBootstrap
2/******/ 	"use strict";
3var __webpack_exports__ = {};
4
5;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/classCallCheck.js
6function _classCallCheck(instance, Constructor) {
7  if (!(instance instanceof Constructor)) {
8    throw new TypeError("Cannot call a class as a function");
9  }
10}
11;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/createClass.js
12function _defineProperties(target, props) {
13  for (var i = 0; i < props.length; i++) {
14    var descriptor = props[i];
15    descriptor.enumerable = descriptor.enumerable || false;
16    descriptor.configurable = true;
17    if ("value" in descriptor) descriptor.writable = true;
18    Object.defineProperty(target, descriptor.key, descriptor);
19  }
20}
21
22function _createClass(Constructor, protoProps, staticProps) {
23  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
24  if (staticProps) _defineProperties(Constructor, staticProps);
25  return Constructor;
26}
27;// CONCATENATED MODULE: ./src/jquery.simpler-select.ts
28
29/**
30 * @file
31 * Render standard select with hierarchical options: as set of selects, one for each level of the hierarchy.
32 */
33
34
35
36
37(function ($, wrapperClass, pluginName) {
38  'use strict';
39
40  var pluginId = 'plugin_' + pluginName;
41
42  function $createElement(name) {
43    return $(document.createElement(name));
44  }
45
46  var ClientSideHierarchicalSelect = /*#__PURE__*/function () {
47    function ClientSideHierarchicalSelect(element, settings) {
48      _classCallCheck(this, ClientSideHierarchicalSelect);
49
50      this.options = [];
51      this.groups = {};
52      this.currentLevel = -1;
53      this.$element = $(element);
54      this.disabled = element.disabled;
55      this.elementId = element.id;
56      this.isMultiple = element.multiple;
57      this.elementClasses = element.className.replace('simpler-select-root', 'simpler-select'); // Use `$.extend()` to not care about `Object.assign()` polyfill for IE11.
58
59      this.settings = $.extend({
60        noFirstLevelNone: false,
61        noneLabel: '- Please choose -',
62        noneValue: '_none',
63        labels: []
64      }, settings);
65      this.init();
66    }
67
68    _createClass(ClientSideHierarchicalSelect, [{
69      key: "init",
70      value: function init() {
71        // Ensure that we'll clearly initiate a new instance.
72        this.destroy();
73        this.buildOptions();
74        var $currentWrapper = this.createSelectElement(this.$element, this.rootValue, !this.settings.noFirstLevelNone);
75
76        if ($currentWrapper !== this.$element) {
77          var lastSelectedValue = this.$element.val();
78
79          if (lastSelectedValue instanceof Array) {
80            lastSelectedValue = lastSelectedValue.pop();
81          } else if (typeof lastSelectedValue === 'number') {
82            lastSelectedValue = lastSelectedValue.toString();
83          }
84
85          if (lastSelectedValue) {
86            // Reverse since we started from the last value.
87            for (var _i2 = 0, _this$getLineage$reve2 = this.getLineage(lastSelectedValue).reverse(); _i2 < _this$getLineage$reve2.length; _i2++) {
88              var value = _this$getLineage$reve2[_i2];
89              $currentWrapper.find('select').val(value);
90              $currentWrapper = this.createSelectElement($currentWrapper, value);
91            }
92          } // Hide the original.
93
94
95          this.$element.hide();
96        }
97      }
98    }, {
99      key: "destroy",
100      value: function destroy() {
101        // Allow reinitialization here.
102        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
103        // @ts-ignore
104        this['groups'] = {};
105        this.options.length = 0;
106        this.removeSubsequentSelects(this.$element.show());
107      }
108    }, {
109      key: "buildOptions",
110      value: function buildOptions() {
111        var _a, _b;
112
113        for (var _i4 = 0, _this$$element$0$opti2 = this.$element[0].options; _i4 < _this$$element$0$opti2.length; _i4++) {
114          var option = _this$$element$0$opti2[_i4];
115          var group = option.parentNode instanceof HTMLOptGroupElement ? option.parentNode.label : undefined;
116
117          if (group) {
118            // Create the mapping as we'll need the total count later.
119            this.groups[group] = undefined;
120          }
121
122          this.options.push({
123            group: group,
124            value: option.value,
125            label: option.text.trim(),
126            // `undefined` and `''` results in an `undefined`.
127            parent: option.dataset.parent || undefined
128          });
129        } // If the first option is a `_none`, then the next one is the
130        // real where we should look for a parent to determine whether
131        // the hierarchy starts from a specific item.
132
133
134        this.rootValue = (_b = this.options[((_a = this.options[0]) === null || _a === void 0 ? void 0 : _a.value) === this.settings.noneValue ? 1 : 0]) === null || _b === void 0 ? void 0 : _b.parent;
135      }
136    }, {
137      key: "createSelectElement",
138      value: function createSelectElement($element, parent) {
139        var _this = this;
140
141        var addNoneOption = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
142        var options = this.getChildren(parent);
143
144        if (!options.length) {
145          return $element;
146        }
147
148        this.currentLevel++;
149        var levelPrefix = '--level-' + this.currentLevel;
150        var selectId = this.elementId + levelPrefix;
151        var $wrapper = $createElement('div') // Add level-specific class to ease the styling.
152        .addClass([wrapperClass, wrapperClass + levelPrefix]) // Provide the read-only attribute for those who may need to query it.
153        .attr('data-level', this.currentLevel);
154        var $select = $createElement('select').addClass(this.elementClasses).attr('id', selectId) // Suppress `Argument of type 'boolean' is not assignable to
155        // parameter of type 'string | number` because it's not true.
156        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
157        // @ts-ignore TS2345.
158        .attr('disabled', this.disabled);
159
160        if (addNoneOption) {
161          $createElement('option').attr('value', this.settings.noneValue).attr('data-parent-value', parent || null).text(this.settings.noneLabel).appendTo($select);
162        }
163
164        for (var _i6 = 0; _i6 < options.length; _i6++) {
165          var option = options[_i6];
166
167          // Do not add the `_none` option 'cause it's already added above.
168          if (option.value !== this.settings.noneValue) {
169            this.createOptionElement($select, option);
170          }
171        }
172
173        $select.on('change', function (event) {
174          var _a;
175
176          var selectedOption = event.target.options[event.target.selectedIndex];
177
178          _this.removeSubsequentSelects($wrapper);
179
180          _this.$element // The `data-parent-value` exists only on the `_none` option, meaning
181          // we should refer to the selection picked in the previous select.
182          .val(_this.getValue((_a = selectedOption.dataset.parentValue) !== null && _a !== void 0 ? _a : selectedOption.value)).trigger(event.type);
183
184          if (selectedOption.value !== _this.settings.noneValue) {
185            _this.createSelectElement($wrapper, selectedOption.value);
186          }
187        });
188
189        if (this.settings.labels[this.currentLevel]) {
190          $createElement('label').attr('for', selectId).text(this.settings.labels[this.currentLevel]).appendTo($wrapper);
191        }
192
193        this.$element.trigger(jQuery.Event("".concat(pluginName, "ChildCreated"), {
194          $wrapper: $wrapper
195        }));
196        return $wrapper.append($select).insertAfter($element);
197      }
198    }, {
199      key: "createOptionElement",
200      value: function createOptionElement($select, option) {
201        var _a;
202
203        var _b, _c;
204
205        var $option = $createElement('option').attr('value', option.value).text(option.label);
206
207        if (this.getChildren(option.value).length) {
208          $option.addClass('has-children');
209        } // Group options on the first level only if there are at least 2 groups.
210
211
212        if (!option.parent && option.group && Object.keys(this.groups).length > 1) {
213          (_a = (_b = this.groups)[_c = option.group]) !== null && _a !== void 0 ? _a : _b[_c] = $createElement('optgroup').attr('label', option.group).appendTo($select); // The group is no longer `undefined`.
214          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
215          // @ts-ignore
216
217          $option.appendTo(this.groups[option.group]);
218        } else {
219          $option.appendTo($select);
220        }
221
222        return $option;
223      }
224      /**
225       * Returns the value for a given `option` element.
226       */
227
228    }, {
229      key: "getValue",
230      value: function getValue(value) {
231        return this.isMultiple ? this.getLineage(value) : value;
232      }
233      /**
234       * Removes subsequent `select` elements after a given element.
235       */
236
237    }, {
238      key: "removeSubsequentSelects",
239      value: function removeSubsequentSelects($element) {
240        var $wrappers = $element.nextAll('.' + wrapperClass);
241        this.currentLevel -= $wrappers.length;
242
243        if ($wrappers.length) {
244          this.$element.trigger(jQuery.Event("".concat(pluginName, "ChildrenDeleted"), {
245            $wrappers: $wrappers
246          }));
247        }
248
249        $wrappers.remove();
250      }
251      /**
252       * Returns the first found option with a given value.
253       */
254
255    }, {
256      key: "getOptionByValue",
257      value: function getOptionByValue(value) {
258        for (var _i8 = 0, _this$options2 = this.options; _i8 < _this$options2.length; _i8++) {
259          var option = _this$options2[_i8];
260
261          if (option.value === value) {
262            return option;
263          }
264        }
265
266        return undefined;
267      }
268      /**
269       * Returns the options that are children of a given parent.
270       */
271
272    }, {
273      key: "getChildren",
274      value: function getChildren(parent) {
275        return this.options.filter(function (option) {
276          return option.parent === parent;
277        });
278      }
279      /**
280       * Returns the values tree for a given value.
281       */
282
283    }, {
284      key: "getLineage",
285      value: function getLineage(value) {
286        var parents = [value]; // eslint-disable-next-line no-constant-condition
287
288        while (true) {
289          if (value !== this.settings.noneValue) {
290            var option = this.getOptionByValue(value); // Do not allow going beyond the configured root.
291
292            if ((option === null || option === void 0 ? void 0 : option.parent) && option.parent !== this.rootValue) {
293              parents.push(option.parent);
294              var child = this.getOptionByValue(option.parent);
295
296              if (child === null || child === void 0 ? void 0 : child.value) {
297                value = child.value;
298                continue;
299              }
300            }
301          }
302
303          break;
304        }
305
306        return parents;
307      }
308    }]);
309
310    return ClientSideHierarchicalSelect;
311  }();
312
313  $.fn[pluginName] = function (settings) {
314    return this.each(function (index, element) {
315      if (!$.data(element, pluginId)) {
316        if (element instanceof HTMLSelectElement) {
317          $.data(element, pluginId, new ClientSideHierarchicalSelect(element, settings));
318        } else {
319          throw new Error("The \"".concat(element.id, "\" must be an instance of HTMLSelectElement."));
320        }
321      }
322    });
323  };
324})(jQuery, 'select-wrapper', 'simplerSelect');
325/******/ })()
326;
327//# sourceMappingURL=jquery.simpler-select.js.map

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.