wj-elements 0.1.107 → 0.1.108

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.
@@ -619,11 +619,12 @@ const _WJElement = class _WJElement extends HTMLElement {
619
619
  setUpAccessors() {
620
620
  let attrs = this.getAttributeNames();
621
621
  attrs.forEach((name) => {
622
+ var _a, _b;
622
623
  const sanitizedName = this.sanitizeName(name);
623
624
  if (this[sanitizedName] == void 0) {
624
625
  Object.defineProperty(this, sanitizedName, {
625
- set: (value) => this.setAttribute(name, value),
626
- get: (_) => {
626
+ set: Object.getOwnPropertyDescriptors(this.__proto__).hasOwnProperty(sanitizedName) ? (_a = Object.getOwnPropertyDescriptors(this.__proto__)) == null ? void 0 : _a[sanitizedName].set : (value) => this.setAttribute(name, value),
627
+ get: Object.getOwnPropertyDescriptors(this.__proto__).hasOwnProperty(sanitizedName) ? (_b = Object.getOwnPropertyDescriptors(this.__proto__)) == null ? void 0 : _b[sanitizedName].get : (_) => {
627
628
  return this.getAttribute(name);
628
629
  }
629
630
  });
@@ -29,7 +29,7 @@ class Option extends WJElement {
29
29
  * @param {boolean} value - The value to set.
30
30
  */
31
31
  set selected(value) {
32
- return value ? this.setAttribute("selected", "") : this.removeAttribute("selected");
32
+ value ? this.setAttribute("selected", "") : this.removeAttribute("selected");
33
33
  }
34
34
  /**
35
35
  * Sets the value attribute of the option.
@@ -134,15 +134,17 @@ class Options extends WJElement {
134
134
  infiniteScroll.dataToHtml = this.htmlItem;
135
135
  infiniteScroll.setCustomData = async (page, signal) => {
136
136
  let res = await this.service.get(`${this.url}${this.search ? `/${this.search}` : ""}?page=${page}&size=${this.lazyLoadSize}`, null, false, signal);
137
- this._loadedOptions.push(this.processData(res));
138
- return res;
137
+ const filteredOptions = this.filterOutDrawnOptions(res);
138
+ this._loadedOptions.push(...this.processData(filteredOptions));
139
+ return filteredOptions;
139
140
  };
140
141
  this.contains(infiniteScroll) || this.appendChild(infiniteScroll);
141
142
  this.infiniteScroll = infiniteScroll;
142
143
  } else {
143
144
  this.response = await this.getPages();
144
- const optionsData = this.processData(this.response);
145
- this._loadedOptions.push(optionsData);
145
+ let optionsData = this.filterOutDrawnOptions(this.response);
146
+ optionsData = this.processData(optionsData);
147
+ this._loadedOptions.push(...optionsData);
146
148
  this.append(...optionsData.map(this.htmlItem));
147
149
  }
148
150
  fragment.appendChild(slot);
@@ -161,7 +163,27 @@ class Options extends WJElement {
161
163
  splittedOptionArrayPath == null ? void 0 : splittedOptionArrayPath.forEach((path) => {
162
164
  options = options[path];
163
165
  });
164
- return options;
166
+ return options ?? [];
167
+ }
168
+ /**
169
+ * Filters out drawn options from the response.
170
+ *
171
+ * @param {any} response - The response to filter.
172
+ * @returns {any} - The filtered response.
173
+ */
174
+ filterOutDrawnOptions(response) {
175
+ var _a;
176
+ const splittedOptionArrayPath = this.optionArrayPath ? (_a = this.optionArrayPath) == null ? void 0 : _a.split(".") : [];
177
+ let filteredResponse = structuredClone(response);
178
+ const recursiveUpdate = (object, pathToProperty) => {
179
+ if (pathToProperty.length > 1) {
180
+ recursiveUpdate(object[pathToProperty[0]], pathToProperty.slice(1));
181
+ } else {
182
+ object[pathToProperty[0]] = object[pathToProperty[0]].filter((option) => !this._loadedOptions.some((loadedOption) => loadedOption[this.itemValue] === option[this.itemValue]));
183
+ }
184
+ };
185
+ recursiveUpdate(filteredResponse, splittedOptionArrayPath);
186
+ return filteredResponse;
165
187
  }
166
188
  /**
167
189
  * Fetches the pages from the provided URL.
@@ -178,9 +200,36 @@ class Options extends WJElement {
178
200
  const data = await response.json();
179
201
  return data;
180
202
  }
203
+ /**
204
+ * Finds the selected option data based on the given selected option values.
205
+ *
206
+ * @param {Array} selectedOptionValues - The array of selected option values.
207
+ * @returns {Array} - The array of option data that matches the selected option values.
208
+ */
181
209
  findSelectedOptionData(selectedOptionValues = []) {
182
210
  return this.options.filter((option) => selectedOptionValues.includes(option[this.itemValue]));
183
211
  }
212
+ /**
213
+ * Adds an option to the element.
214
+ *
215
+ * @param {Object} optionData - The data of the option to be added.
216
+ */
217
+ addOption(optionData) {
218
+ if (this._loadedOptions.some((option) => option[this.itemValue] === optionData[this.itemValue])) {
219
+ return;
220
+ }
221
+ this.appendChild(this.htmlItem(optionData));
222
+ this._loadedOptions.push(optionData);
223
+ }
224
+ /**
225
+ * Adds options to the element.
226
+ *
227
+ * @param {Array} optionsData - The array of option data to be added.
228
+ * @param {boolean} [silent=false] - Whether to suppress events triggered by adding options.
229
+ */
230
+ addOptions(optionsData = [], silent = false) {
231
+ Array.isArray(optionsData) ? optionsData == null ? void 0 : optionsData.forEach((optionData) => this.addOption(optionData, silent)) : this.addOption(optionsData, silent);
232
+ }
184
233
  }
185
234
  Options.define("wje-options", Options);
186
235
  export {
@@ -61,7 +61,7 @@ class Select extends WJElement {
61
61
  option.selected = false;
62
62
  option.removeAttribute("selected");
63
63
  e.target.parentNode.removeChild(e.target);
64
- this.selections(null, 0);
64
+ this.selections();
65
65
  });
66
66
  this._selected = [];
67
67
  this.counterEl = null;
@@ -498,6 +498,95 @@ class Select extends WJElement {
498
498
  chip.appendChild(label);
499
499
  return chip;
500
500
  }
501
+ /**
502
+ * Generates an HTML option element based on the provided item and mapping.
503
+ *
504
+ * @param {Object} item - The item to generate the option for.
505
+ * @param {Object} [map] - The mapping object that specifies the properties of the item to use for the option's value and text.
506
+ * @param {string} [map.value="value"] - The property of the item to use for the option's value.
507
+ * @param {string} [map.text="text"] - The property of the item to use for the option's text.
508
+ * @returns {HTMLElement} The generated HTML option element.
509
+ */
510
+ htmlOption(item, map = { value: "value", text: "text" }) {
511
+ let option = document.createElement("wje-option");
512
+ if (item[map.value] == null) {
513
+ console.warn(`The item ${JSON.stringify(item)} does not have the property ${map.value}`);
514
+ }
515
+ if (item[map.text] == null) {
516
+ console.warn(`The item ${JSON.stringify(item)} does not have the property ${map.text}`);
517
+ }
518
+ option.setAttribute("value", item[map.value] ?? "");
519
+ option.innerText = item[map.text] ?? "";
520
+ return option;
521
+ }
522
+ /**
523
+ * Adds an option to the select element.
524
+ *
525
+ * @param {any} optionData - The data for the option to be added.
526
+ * @param {boolean} [silent=false] - Whether to suppress any events triggered by the addition of the option.
527
+ * @param {object} [map={ value: "value", text: "text" }] - The mapping object specifying the properties of the option data to be used for the value and text of the option.
528
+ */
529
+ addOption(optionData, silent = false, map = { value: "value", text: "text" }) {
530
+ if (!optionData)
531
+ return;
532
+ const optionsElement = this.querySelector("wje-options");
533
+ if (optionsElement) {
534
+ optionsElement.addOption(optionData, silent, map);
535
+ return;
536
+ }
537
+ let option = this.htmlOption(optionData, map);
538
+ this.appendChild(option);
539
+ }
540
+ /**
541
+ * Adds options to the select element.
542
+ *
543
+ * @param {Array|Object} optionsData - The options data to be added. Can be an array of objects or a single object.
544
+ * @param {boolean} [silent=false] - Indicates whether to trigger events when adding options. Default is false.
545
+ * @param {Object} [map] - The mapping object that specifies the properties of the options data object. Default is { value: "value", text: "text" }.
546
+ */
547
+ addOptions(optionsData, silent = false, map = { value: "value", text: "text" }) {
548
+ if (!Array.isArray(optionsData)) {
549
+ this.addOption(optionsData, silent, map);
550
+ } else {
551
+ const optionsElement = this.querySelector("wje-options");
552
+ if (optionsElement) {
553
+ optionsElement.addOptions(optionsData, silent, map);
554
+ return;
555
+ }
556
+ optionsData.forEach((item) => {
557
+ this.addOption(item, silent, map);
558
+ });
559
+ }
560
+ }
561
+ /**
562
+ * Selects an option with the specified value.
563
+ *
564
+ * @param {string} value - The value of the option to be selected.
565
+ * @param {boolean} [silent=false] - Whether to suppress firing events.
566
+ */
567
+ selectOption(value, silent = false) {
568
+ if (!value)
569
+ return;
570
+ let option = this.querySelector(`wje-option[value="${value}"]`);
571
+ if (option) {
572
+ option.selected = true;
573
+ }
574
+ }
575
+ /**
576
+ * Selects one or multiple options in the select element.
577
+ *
578
+ * @param {Array|any} values - The value(s) of the option(s) to be selected.
579
+ * @param {boolean} [silent=false] - Whether to trigger the change event or not.
580
+ */
581
+ selectOptions(values, silent = false) {
582
+ if (!Array.isArray(values)) {
583
+ this.selectOption(values, silent);
584
+ } else {
585
+ values.forEach((value) => {
586
+ this.selectOption(value, silent);
587
+ });
588
+ }
589
+ }
501
590
  /**
502
591
  * @summary Callback function that is called when the custom element is associated with a form.
503
592
  * This function adds an event listener to the form's submit event, which validates the input and propagates the validation.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wj-elements",
3
3
  "description": "WebJET Elements is a modern set of user interface tools harnessing the power of web components designed to simplify web application development.",
4
- "version": "0.1.107",
4
+ "version": "0.1.108",
5
5
  "homepage": "https://github.com/lencys/wj-elements",
6
6
  "author": "Lukáš Ondrejček <lukas.ondrejcek@gmail.com>",
7
7
  "license": "MIT",