@dile/crud 2.12.2 → 2.13.0

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.
@@ -0,0 +1,5 @@
1
+ import { DileAjaxInputSuggestions } from './src/DileAjaxInputSuggestions.js';
2
+
3
+ if (!customElements.get('dile-ajax-input-suggestions')) {
4
+ customElements.define('dile-ajax-input-suggestions', DileAjaxInputSuggestions);
5
+ }
@@ -0,0 +1 @@
1
+ export { DileAjaxInputSuggestions } from './src/DileAjaxInputSuggestions.js';
@@ -0,0 +1,59 @@
1
+ import { DileInputSuggestionsBase } from '@dile/ui/components/input-suggestions/index.js';
2
+ import { DileAxios } from '../../../lib/DileAxios.js';
3
+
4
+ /**
5
+ * # dile-ajax-input-suggestions
6
+ *
7
+ * Ajax variant of DileInputSuggestionsBase (`@dile/ui`), with the same request API as
8
+ * dile-ajax-select-crud-overlay. It requests the suggestions to `endpoint` with Axios
9
+ * (DileAxios), sending the typed text in `queryStringVariable`.
10
+ *
11
+ * How the list is extracted from the response, by priority:
12
+ * 1. `getSelectResultList(response)`, which receives the whole body;
13
+ * 2. `resultDataProperty`, name of the first-level property holding the array;
14
+ * 3. the whole body, when it already is the array.
15
+ */
16
+ export class DileAjaxInputSuggestions extends DileAxios(DileInputSuggestionsBase) {
17
+ static get properties() {
18
+ return {
19
+ endpoint: { type: String },
20
+ queryStringVariable: { type: String },
21
+ additionalQueryString: { type: Object },
22
+ resultDataProperty: { type: String },
23
+ maxResults: { type: Number },
24
+ pageParamName: { type: String },
25
+ getSelectResultList: { type: Object },
26
+ };
27
+ }
28
+
29
+ constructor() {
30
+ super();
31
+ this.queryStringVariable = 'q';
32
+ }
33
+
34
+ loadData() {
35
+ const requestId = this._requestId;
36
+ const params = { ...(this.additionalQueryString || {}) };
37
+ if (this.pageParamName && this.maxResults) {
38
+ params[this.pageParamName] = this.maxResults;
39
+ }
40
+ params[this.queryStringVariable] = this.keyword;
41
+ this.axiosInstance
42
+ .get(this.endpoint, { params })
43
+ .then((response) => {
44
+ const body = response.data;
45
+ this.setResults(body?.error ? [] : this.getResultList(body), requestId);
46
+ })
47
+ .catch(() => this.setResults([], requestId));
48
+ }
49
+
50
+ getResultList(body) {
51
+ if (typeof this.getSelectResultList === 'function') {
52
+ return this.getSelectResultList(body);
53
+ }
54
+ if (this.resultDataProperty) {
55
+ return body?.[this.resultDataProperty];
56
+ }
57
+ return body;
58
+ }
59
+ }
@@ -0,0 +1,5 @@
1
+ import { DileAjaxInputTags } from './src/DileAjaxInputTags.js';
2
+
3
+ if (!customElements.get('dile-ajax-input-tags')) {
4
+ customElements.define('dile-ajax-input-tags', DileAjaxInputTags);
5
+ }
@@ -0,0 +1 @@
1
+ export { DileAjaxInputTags } from './src/DileAjaxInputTags.js';
@@ -0,0 +1,10 @@
1
+ import { DileInputTagsMixin } from '@dile/ui/components/input-tags/index.js';
2
+ import { DileAjaxInputSuggestions } from '../../ajax-input-suggestions/src/DileAjaxInputSuggestions.js';
3
+
4
+ /**
5
+ * # dile-ajax-input-tags
6
+ *
7
+ * Tags editor that requests its suggestions to `endpoint`, with the same API as
8
+ * dile-ajax-input-suggestions. See DileInputTagsMixin in `@dile/ui`.
9
+ */
10
+ export class DileAjaxInputTags extends DileInputTagsMixin(DileAjaxInputSuggestions) {}
@@ -295,6 +295,7 @@ export class DileCrud extends DileI18nMixin(DileCrudMixin(LitElement)) {
295
295
  class="action-controller"
296
296
  id="elfilters"
297
297
  @filters-changed=${this.filtersChanged}
298
+ @filters-display-changed=${this.filtersDisplayChanged}
298
299
  .filters=${this.config.availableFilters || []}
299
300
  language="${this.language}"
300
301
  ></dile-crud-filters>
@@ -369,6 +370,7 @@ export class DileCrud extends DileI18nMixin(DileCrudMixin(LitElement)) {
369
370
  <dile-crud-filters-inline
370
371
  id="elfilters"
371
372
  @filters-changed=${this.filtersChanged}
373
+ @filters-display-changed=${this.filtersDisplayChanged}
372
374
  .filters=${this.config.availableFilters || []}
373
375
  language="${this.language}"
374
376
  ></dile-crud-filters-inline>
@@ -525,6 +527,10 @@ export class DileCrud extends DileI18nMixin(DileCrudMixin(LitElement)) {
525
527
  this.listElement.setFilters(e.detail.filters);
526
528
  }
527
529
 
530
+ filtersDisplayChanged() {
531
+ this.listElement?.refreshFiltersList?.();
532
+ }
533
+
528
534
  removeFilter(e) {
529
535
  if(e.detail.name == 'keyword') {
530
536
  const searchInput = this.searchInputElement;
@@ -440,6 +440,14 @@ export class DileCrudList extends DileI18nMixin(DileLoading(LitElement)) {
440
440
  this.elservice.setPageSize(size);
441
441
  }
442
442
 
443
+ /**
444
+ * Re-renders the active filters chips without reloading the list
445
+ * (e.g. when the label of a select_ajax filter value becomes known).
446
+ */
447
+ refreshFiltersList() {
448
+ this.shadowRoot.querySelector('dile-crud-filters-list')?.requestUpdate();
449
+ }
450
+
443
451
  setFilters(filters) {
444
452
  this.loading = true;
445
453
  this.filters = filters;
@@ -106,6 +106,7 @@ export class DileSelectAjaxSimple extends DileAxios(DileEmmitChange(LitElement))
106
106
  if (this.value) {
107
107
  this.selectCurrentValue();
108
108
  }
109
+ this.dispatchDataLoaded();
109
110
  });
110
111
  } else {
111
112
  this.registerError();
@@ -124,6 +125,51 @@ export class DileSelectAjaxSimple extends DileAxios(DileEmmitChange(LitElement))
124
125
  return Array.isArray(json) ? json : [];
125
126
  }
126
127
 
128
+ /**
129
+ * The item in `data` whose `idProperty` matches the current `value`, or null.
130
+ */
131
+ get selectedItem() {
132
+ if (this.value === undefined || this.value === null || this.value === '') {
133
+ return null;
134
+ }
135
+ return (this.data || []).find(item => String(item[this.idProperty]) === String(this.value)) ?? null;
136
+ }
137
+
138
+ /**
139
+ * The `displayProperty` of the selected item, or an empty string when unknown.
140
+ */
141
+ get selectedLabel() {
142
+ return this.selectedItem?.[this.displayProperty] ?? '';
143
+ }
144
+
145
+ emmitChange() {
146
+ this.dispatchEvent(new CustomEvent('element-changed', {
147
+ bubbles: true,
148
+ composed: true,
149
+ detail: {
150
+ name: this.name,
151
+ value: this.value,
152
+ selectedItem: this.selectedItem,
153
+ selectedLabel: this.selectedLabel,
154
+ }
155
+ }));
156
+ }
157
+
158
+ // Notifies that options are available, without emitting a value change
159
+ dispatchDataLoaded() {
160
+ this.dispatchEvent(new CustomEvent('dile-select-ajax-data-loaded', {
161
+ bubbles: true,
162
+ composed: true,
163
+ detail: {
164
+ name: this.name,
165
+ value: this.value,
166
+ data: this.data,
167
+ selectedItem: this.selectedItem,
168
+ selectedLabel: this.selectedLabel,
169
+ }
170
+ }));
171
+ }
172
+
127
173
  registerError() {
128
174
  this.ajaxError = true;
129
175
  this.loading = false;
@@ -171,6 +217,7 @@ export class DileSelectAjaxSimple extends DileAxios(DileEmmitChange(LitElement))
171
217
  : html`
172
218
  <dile-select
173
219
  name="${this.name}"
220
+ value="${this.value ?? ''}"
174
221
  ?disabled=${this.disabled}
175
222
  ?errored=${this.errored}
176
223
  message="${this.message || ''}"
@@ -30,6 +30,7 @@ export class DileCrudFilters extends DileI18nMixin(LitElement) {
30
30
  id="elform"
31
31
  .filters=${this.filters}
32
32
  @dile-form-changed=${this.filtersChanged}
33
+ @dile-select-ajax-data-loaded=${this.selectAjaxDataLoaded}
33
34
  ></dile-crud-filters-form>
34
35
  `;
35
36
  }
@@ -52,21 +53,29 @@ export class DileCrudFilters extends DileI18nMixin(LitElement) {
52
53
 
53
54
  filtersChanged(e) {
54
55
  const data = e.detail.data;
56
+ const form = this.shadowRoot.getElementById('elform');
55
57
  this.filters = this.filters.map(filter => {
56
58
  if (filter.name in data) {
57
- this.setFilterValue(filter, data[filter.name]);
59
+ const valueLabel = filter.type === 'select_ajax' && form
60
+ ? form.getSelectedLabel(filter.name)
61
+ : '';
62
+ this.setFilterValue(filter, data[filter.name], valueLabel);
58
63
  }
59
64
  return filter;
60
65
  });
61
66
  this.dispatchFiltersChanged();
62
67
  }
63
68
 
64
- setFilterValue(filter, value) {
69
+ setFilterValue(filter, value, valueLabel = '') {
65
70
  switch (filter.type) {
66
71
  case 'select':
72
+ filter.active = value !== '';
73
+ filter.value = value;
74
+ break;
67
75
  case 'select_ajax':
68
76
  filter.active = value !== '';
69
77
  filter.value = value;
78
+ filter.valueLabel = value !== '' ? valueLabel : '';
70
79
  break;
71
80
  default:
72
81
  filter.active = value;
@@ -74,6 +83,26 @@ export class DileCrudFilters extends DileI18nMixin(LitElement) {
74
83
  }
75
84
  }
76
85
 
86
+ // Options of a select_ajax filter were loaded: resolve the chip label without
87
+ // dispatching filters-changed (that would reload the list).
88
+ selectAjaxDataLoaded(e) {
89
+ const { name, value, selectedLabel } = e.detail;
90
+ const filter = this.filters.find(f => f.name === name && f.type === 'select_ajax');
91
+ if (!filter || !filter.active || String(filter.value) !== String(value ?? '')) {
92
+ return;
93
+ }
94
+ if ((filter.valueLabel || '') !== (selectedLabel || '')) {
95
+ filter.valueLabel = selectedLabel || '';
96
+ this.dispatchEvent(new CustomEvent('filters-display-changed', {
97
+ bubbles: true,
98
+ composed: true,
99
+ detail: {
100
+ filters: this.filters,
101
+ }
102
+ }));
103
+ }
104
+ }
105
+
77
106
  dispatchFiltersChanged() {
78
107
  this.dispatchEvent(new CustomEvent('filters-changed', {
79
108
  bubbles: true,
@@ -36,6 +36,16 @@ export class DileCrudFiltersForm extends DileFormChangeDetect(DileForm(LitElemen
36
36
  `;
37
37
  }
38
38
 
39
+ /**
40
+ * Returns the label of the option selected in a select_ajax filter field,
41
+ * or an empty string when it is not known (yet).
42
+ */
43
+ getSelectedLabel(name) {
44
+ const field = Array.from(this.shadowRoot?.querySelectorAll('dile-select-ajax-simple') || [])
45
+ .find(el => el.name === name);
46
+ return field?.selectedLabel ?? '';
47
+ }
48
+
39
49
  getFilterField(filter) {
40
50
  if (filter.hidden) {
41
51
  return '';
@@ -44,21 +44,32 @@ export class DileCrudFiltersList extends LitElement {
44
44
 
45
45
  filterItemTemplate(filter) {
46
46
  if(! filter.hidden && filter.active) {
47
- switch(filter.type) {
48
- case 'select':
49
- return html`
50
- <dile-crud-filters-list-item name="${filter.name}" label="${this.findLabelInOption(filter)}" ?active="${filter.active}"></dile-crud-filters-list-item>
51
- `;
52
- default:
53
- return html`
54
- <dile-crud-filters-list-item name="${filter.name}" label="${filter.label}" ?active="${filter.active}"></dile-crud-filters-list-item>
55
- `
56
- }
47
+ return html`
48
+ <dile-crud-filters-list-item name="${filter.name}" label="${this.chipLabel(filter)}" ?active="${filter.active}"></dile-crud-filters-list-item>
49
+ `;
50
+ }
51
+ }
52
+
53
+ chipLabel(filter) {
54
+ let valueLabel;
55
+ switch(filter.type) {
56
+ case 'select':
57
+ valueLabel = this.findLabelInOption(filter);
58
+ break;
59
+ case 'select_ajax':
60
+ valueLabel = filter.valueLabel || '';
61
+ if (!valueLabel) {
62
+ return filter.label;
63
+ }
64
+ break;
65
+ default:
66
+ return filter.label;
57
67
  }
68
+ return filter.showLabelInChip && valueLabel ? `${filter.label}: ${valueLabel}` : valueLabel;
58
69
  }
59
70
 
60
71
  findLabelInOption(filter) {
61
- let option = filter.options.find((option) => option.value == filter.value);
72
+ let option = (filter.options || []).find((option) => option.value == filter.value);
62
73
  if(option) {
63
74
  return option.label;
64
75
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/crud",
3
- "version": "2.12.2",
3
+ "version": "2.13.0",
4
4
  "description": "Components to create a generic crud system based on Web Components and Lit",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -26,12 +26,12 @@
26
26
  "homepage": "https://dile-components.com/",
27
27
  "dependencies": {
28
28
  "@dile/image-cropper": "^0.1.1",
29
- "@dile/ui": "^3.20.1",
29
+ "@dile/ui": "^3.21.0",
30
30
  "axios": "^1.19.0",
31
31
  "lit": "^2.7.0 || ^3.0.0"
32
32
  },
33
33
  "publishConfig": {
34
34
  "access": "public"
35
35
  },
36
- "gitHead": "2312fc5bacc0b5499c99cd25be6ae3509fd026c1"
36
+ "gitHead": "425f1cf93f61a2703365205c699b8a6fc64e538d"
37
37
  }