@vaadin/vaadin-crud 21.0.1 → 22.0.0-alpha10

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.
@@ -3,978 +3,11 @@
3
3
  * Copyright (c) 2017 - 2021 Vaadin Ltd.
4
4
  * This program is available under Commercial Vaadin Developer License 4.0, available at https://vaadin.com/license/cvdl-4.0.
5
5
  */
6
- import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
7
- import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
8
- import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
9
- import '@polymer/iron-media-query/iron-media-query.js';
10
- import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
11
- import { ElementMixin } from '@vaadin/vaadin-element-mixin/vaadin-element-mixin.js';
12
- import '@vaadin/vaadin-button/src/vaadin-button.js';
13
- import '@vaadin/vaadin-dialog/src/vaadin-dialog.js';
14
- import '@vaadin/vaadin-confirm-dialog/src/vaadin-confirm-dialog.js';
15
- import '@vaadin/vaadin-license-checker/vaadin-license-checker.js';
16
- import './vaadin-dialog-layout.js';
17
- import './vaadin-crud-grid.js';
18
- import './vaadin-crud-form.js';
6
+ import { Crud } from '@vaadin/crud/src/vaadin-crud.js';
19
7
 
20
8
  /**
21
- * `<vaadin-crud>` is a Web Component for [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operations.
22
- *
23
- * ### Quick Start
24
- *
25
- * Assign an array to the [`items`](#/elements/vaadin-crud#property-items) property.
26
- *
27
- * A grid and an editor will be automatically generated and configured based on the data structure provided.
28
- *
29
- * #### Example:
30
- * ```html
31
- * <vaadin-crud items='[{"name": "John", "surname": "Lennon", "role": "singer"},
32
- * {"name": "Ringo", "surname": "Starr", "role": "drums"}]'></vaadin-crud>
33
- * ```
34
- *
35
- * ### Data Provider Function
36
- *
37
- * Otherwise, you can provide a [`dataProvider`](#/elements/vaadin-crud#property-dataProvider) function.
38
- * #### Example:
39
- * ```html
40
- * <vaadin-crud></vaadin-crud>
41
- * ```
42
- * ```js
43
- * const crud = document.querySelector('vaadin-crud');
44
- * const users = [{'name': 'John', 'surname': 'Lennon', 'role': 'singer'}, ...];
45
- * crud.dataProvider = function(params, callback) {
46
- * const chunk = users.slice(params.page * params.pageSize, params.page * params.pageSize + params.pageSize);
47
- * callback(chunk, people.length);
48
- * };
49
- * ```
50
- *
51
- * NOTE: The auto-generated editor only supports string types. If you need to handle special cases
52
- * customizing the editor is discussed below.
53
- *
54
- * ### Customization
55
- *
56
- * Alternatively you can fully configure the component by using `slot` names.
57
- *
58
- * Slot name | Description
59
- * ----------------|----------------
60
- * `grid` | To replace the auto-generated grid with a custom one.
61
- * `form` | To replace the auto-generated form.
62
- * `toolbar` | To replace the toolbar content. Add an element with the attribute `new-button` for the new item action.
63
- *
64
- * #### Example:
65
- *
66
- * ```html
67
- * <vaadin-crud
68
- * id="crud"
69
- * items='[{"name": "John", "surname": "Lennon", "role": "singer"},
70
- * {"name": "Ringo", "surname": "Starr", "role": "drums"}]'
71
- * >
72
- * <vaadin-grid slot="grid">
73
- * <vaadin-crud-edit-column></vaadin-crud-edit-column>
74
- * <vaadin-grid-column id="column1"></vaadin-grid-column>
75
- * <vaadin-grid-column id="column2"></vaadin-grid-column>
76
- * </vaadin-grid>
77
- *
78
- * <vaadin-form-layout slot="form">
79
- * <vaadin-text-field label="First" path="name"></vaadin-text-field>
80
- * <vaadin-text-field label="Surname" path="surname"></vaadin-text-field>
81
- * </vaadin-form-layout>
82
- *
83
- * <div slot="toolbar">
84
- * Total singers: [[size]]
85
- * <button new-button>New singer</button>
86
- * </div>
87
- * </vaadin-crud>
88
- * ```
89
- * ```js
90
- * const crud = document.querySelector('#crud');
91
- *
92
- * const column1 = document.querySelector('#column1');
93
- * column1.headerRenderer = (root, column) => {
94
- * root.textContent = 'Name';
95
- * };
96
- * column1.renderer = (root, column, model) => {
97
- * root.textContent = model.item.name;
98
- * };
99
- *
100
- * const column2 = document.querySelector('#column2');
101
- * column2.headerRenderer = (root, column) => {
102
- * root.textContent = 'Surname';
103
- * };
104
- * column2.renderer = (root, column, model) => {
105
- * root.textContent = model.item.surname;
106
- * };
107
- * ```
108
- *
109
- * ### Helpers
110
- *
111
- * The following elements are used to auto-configure the grid and the editor
112
- * - [`<vaadin-crud-edit-column>`](#/elements/vaadin-crud-edit-column)
113
- * - `<vaadin-crud-grid>` - can be replaced with custom [`<vaadin-grid>`](#/elements/vaadin-grid)
114
- * - `<vaadin-crud-form>` - can be replaced with custom [`<vaadin-form-layout>`](#/elements/vaadin-form-layout)
115
- *
116
- * ### Styling
117
- *
118
- * The following shadow DOM parts are available for styling:
119
- *
120
- * Part name | Description
121
- * ----------------|----------------
122
- * `toolbar` | Toolbar container at the bottom. By default it contains the the `new` button
123
- *
124
- * The following custom properties are available:
125
- *
126
- * Custom Property | Description | Default
127
- * ----------------|----------------
128
- * --vaadin-crud-editor-max-height | max height of editor when opened on the bottom | 40%
129
- * --vaadin-crud-editor-max-width | max width of editor when opened on the side | 40%
130
- *
131
- * See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
132
- *
133
- * @fires {CustomEvent} editor-opened-changed - Fired when the `editorOpened` property changes.
134
- * @fires {CustomEvent} edited-item-changed - Fired when `editedItem` property changes.
135
- * @fires {CustomEvent} items-changed - Fired when the `items` property changes.
136
- * @fires {CustomEvent} size-changed - Fired when the `size` property changes.
137
- * @fires {CustomEvent} new - Fired when user wants to create a new item.
138
- * @fires {CustomEvent} edit - Fired when user wants to edit an existing item.
139
- * @fires {CustomEvent} delete - Fired when user wants to delete item.
140
- * @fires {CustomEvent} save - Fired when user wants to save a new or an existing item.
141
- * @fires {CustomEvent} cancel - Fired when user discards edition.
142
- *
143
- * @extends HTMLElement
144
- * @mixes ElementMixin
145
- * @mixes ThemableMixin
9
+ * @deprecated Import `Crud` from `@vaadin/crud` instead.
146
10
  */
147
- class CrudElement extends ElementMixin(ThemableMixin(PolymerElement)) {
148
- static get template() {
149
- return html`
150
- <style>
151
- :host {
152
- --vaadin-crud-editor-max-height: 40%;
153
- --vaadin-crud-editor-max-width: 40%;
154
- }
11
+ export const CrudElement = Crud;
155
12
 
156
- :host,
157
- #main {
158
- display: flex;
159
- flex-direction: column;
160
- height: 400px;
161
- flex: 1 1 100%;
162
- align-self: stretch;
163
- position: relative;
164
- overflow: hidden;
165
- }
166
-
167
- #main {
168
- height: 100%;
169
- }
170
-
171
- :host([hidden]) {
172
- display: none !important;
173
- }
174
-
175
- [part='toolbar'] {
176
- display: flex;
177
- flex-shrink: 0;
178
- align-items: baseline;
179
- justify-content: flex-end;
180
- }
181
-
182
- #container {
183
- display: flex;
184
- height: 100%;
185
- }
186
-
187
- :host([editor-position='bottom']) #container {
188
- flex-direction: column;
189
- }
190
- </style>
191
-
192
- <div id="container">
193
- <div id="main">
194
- <slot name="grid">
195
- <vaadin-crud-grid
196
- theme$="[[theme]]"
197
- id="grid"
198
- include="[[include]]"
199
- exclude="[[exclude]]"
200
- no-sort="[[noSort]]"
201
- no-filter="[[noFilter]]"
202
- no-head="[[noHead]]"
203
- hide-edit-column="[[editOnClick]]"
204
- ></vaadin-crud-grid>
205
- </slot>
206
-
207
- <div id="toolbar" part="toolbar" on-click="__new">
208
- <slot name="toolbar">
209
- <vaadin-button new-button="" id="new" theme="primary">[[i18n.newItem]]</vaadin-button>
210
- </slot>
211
- </div>
212
- </div>
213
- <vaadin-dialog-layout
214
- theme$="[[theme]]"
215
- form="[[_form]]"
216
- id="dialog"
217
- no-close-on-outside-click="[[__isDirty]]"
218
- no-close-on-esc="[[__isDirty]]"
219
- opened="{{editorOpened}}"
220
- editor-position$="{{editorPosition}}"
221
- mobile="[[__mobile]]"
222
- theme="crud"
223
- >
224
- <h3 slot="header">[[__computeEditorHeader(__isNew, i18n.newItem, i18n.editItem)]]</h3>
225
- <div id="editor">
226
- <slot name="form">
227
- <vaadin-crud-form
228
- theme$="[[theme]]"
229
- id="form"
230
- include="[[include]]"
231
- exclude="[[exclude]]"
232
- ></vaadin-crud-form>
233
- </slot>
234
- </div>
235
-
236
- <vaadin-button slot="footer" on-click="__save" theme="primary" disabled="[[!__isDirty]]"
237
- >[[i18n.saveItem]]</vaadin-button
238
- >
239
- <vaadin-button slot="footer" on-click="__cancel" theme="tertiary">[[i18n.cancel]]</vaadin-button>
240
- <div slot="footer" style="flex: auto;"></div>
241
- <vaadin-button slot="footer" on-click="__delete" theme="tertiary error" hidden="[[__isNew]]"
242
- >[[i18n.deleteItem]]</vaadin-button
243
- >
244
- </vaadin-dialog-layout>
245
- </div>
246
-
247
- <vaadin-confirm-dialog
248
- theme$="[[theme]]"
249
- id="confirmCancel"
250
- on-confirm="__confirmCancel"
251
- cancel
252
- confirm-text="[[i18n.confirm.cancel.button.confirm]]"
253
- cancel-text="[[i18n.confirm.cancel.button.dismiss]]"
254
- header="[[i18n.confirm.cancel.title]]"
255
- message="[[i18n.confirm.cancel.content]]"
256
- confirm-theme="primary"
257
- ></vaadin-confirm-dialog>
258
- <vaadin-confirm-dialog
259
- theme$="[[theme]]"
260
- id="confirmDelete"
261
- on-confirm="__confirmDelete"
262
- cancel
263
- confirm-text="[[i18n.confirm.delete.button.confirm]]"
264
- cancel-text="[[i18n.confirm.delete.button.dismiss]]"
265
- header="[[i18n.confirm.delete.title]]"
266
- message="[[i18n.confirm.delete.content]]"
267
- confirm-theme="primary error"
268
- ></vaadin-confirm-dialog>
269
-
270
- <iron-media-query query="[[__mobileMediaQuery]]" query-matches="{{__mobile}}"></iron-media-query>
271
- `;
272
- }
273
-
274
- static get is() {
275
- return 'vaadin-crud';
276
- }
277
-
278
- static get version() {
279
- return '21.0.1';
280
- }
281
-
282
- static get properties() {
283
- return {
284
- /**
285
- * A reference to the grid used for displaying the item list
286
- * @private
287
- */
288
- _grid: {
289
- type: HTMLElement,
290
- observer: '__onGridChange'
291
- },
292
-
293
- /**
294
- * A reference to the editor component which will be teleported to the dialog
295
- * @private
296
- */
297
- _form: {
298
- type: HTMLElement,
299
- observer: '__onFormChange'
300
- },
301
-
302
- /**
303
- * An array containing the items which will be stamped to the column template instances.
304
- * @type {Array<unknown> | undefined}
305
- */
306
- items: {
307
- type: Array,
308
- notify: true,
309
- observer: '__onItemsChange'
310
- },
311
-
312
- /**
313
- * The item being edited in the dialog.
314
- * @type {unknown}
315
- */
316
- editedItem: {
317
- type: Object,
318
- observer: '__onItemChange',
319
- notify: true
320
- },
321
-
322
- /**
323
- * Sets how editor will be presented on desktop screen.
324
- *
325
- * Accepted values are:
326
- * - `` (default) - form will open as overlay
327
- * - `bottom` - form will open below the grid
328
- * - `aside` - form will open on the grid side (_right_, if lft and _left_ if rtl)
329
- * @attr {bottom|aside} editor-position
330
- * @type {!CrudEditorPosition}
331
- */
332
- editorPosition: {
333
- type: String,
334
- value: '',
335
- reflectToAttribute: true,
336
- observer: '__onEditorPositionChange'
337
- },
338
-
339
- /**
340
- * Enables user to click on row to edit it.
341
- * Note: When enabled, auto-generated grid won't show the edit column.
342
- * @attr {boolean} edit-on-click
343
- * @type {boolean}
344
- */
345
- editOnClick: {
346
- type: Boolean,
347
- value: false
348
- },
349
-
350
- /**
351
- * Function that provides items lazily. Receives arguments `params`, `callback`
352
- *
353
- * `params.page` Requested page index
354
- * `params.pageSize` Current page size
355
- * `params.filters` Currently applied filters
356
- * `params.sortOrders` Currently applied sorting orders
357
- *
358
- * `callback(items, size)` Callback function with arguments:
359
- * - `items` Current page of items
360
- * - `size` Total number of items
361
- * @type {CrudDataProvider | undefined}
362
- */
363
- dataProvider: {
364
- type: Function,
365
- observer: '__onDataProviderChange'
366
- },
367
-
368
- /**
369
- * Disable filtering when grid is autoconfigured.
370
- * @attr {boolean} no-filter
371
- */
372
- noFilter: Boolean,
373
-
374
- /**
375
- * Disable sorting when grid is autoconfigured.
376
- * @attr {boolean} no-sort
377
- */
378
- noSort: Boolean,
379
-
380
- /**
381
- * Remove grid headers when it is autoconfigured.
382
- * @attr {boolean} no-head
383
- */
384
- noHead: Boolean,
385
-
386
- /**
387
- * A comma-separated list of fields to include in the generated grid and the generated editor.
388
- *
389
- * It can be used to explicitly define the field order.
390
- *
391
- * When it is defined [`exclude`](#/elements/vaadin-crud#property-exclude) is ignored.
392
- *
393
- * Default is undefined meaning that all properties in the object should be mapped to fields.
394
- */
395
- include: String,
396
-
397
- /**
398
- * A comma-separated list of fields to be excluded from the generated grid and the generated editor.
399
- *
400
- * When [`include`](#/elements/vaadin-crud#property-include) is defined, this parameter is ignored.
401
- *
402
- * Default is to exclude all private fields (those properties starting with underscore)
403
- */
404
- exclude: String,
405
-
406
- /**
407
- * Reflects the opened status of the editor.
408
- */
409
- editorOpened: {
410
- type: Boolean,
411
- notify: true,
412
- observer: '__onOpenedChanged'
413
- },
414
-
415
- /**
416
- * Number of items in the data set which is reported by the grid.
417
- * Typically it reflects the number of filtered items displayed in the grid.
418
- */
419
- size: {
420
- type: Number,
421
- readOnly: true,
422
- notify: true
423
- },
424
-
425
- /**
426
- * The object used to localize this component.
427
- * For changing the default localization, change the entire
428
- * _i18n_ object or just the property you want to modify.
429
- *
430
- * The object has the following JSON structure and default values:
431
- *
432
- * ```
433
- * {
434
- * newItem: 'New item',
435
- * editItem: 'Edit item',
436
- * saveItem: 'Save',
437
- * cancel: 'Cancel',
438
- * deleteItem: 'Delete...',
439
- * editLabel: 'Edit',
440
- * confirm: {
441
- * delete: {
442
- * title: 'Confirm delete',
443
- * content: 'Are you sure you want to delete the selected item? This action cannot be undone.',
444
- * button: {
445
- * confirm: 'Delete',
446
- * dismiss: 'Cancel'
447
- * }
448
- * },
449
- * cancel: {
450
- * title: 'Unsaved changes',
451
- * content: 'There are unsaved modifications to the item.',
452
- * button: {
453
- * confirm: 'Discard',
454
- * dismiss: 'Continue editing'
455
- * }
456
- * }
457
- * }
458
- * }
459
- * ```
460
- *
461
- * @type {!CrudI18n}
462
- * @default {English/US}
463
- */
464
- i18n: {
465
- type: Object,
466
- value: function () {
467
- return {
468
- newItem: 'New item',
469
- editItem: 'Edit item',
470
- saveItem: 'Save',
471
- cancel: 'Cancel',
472
- deleteItem: 'Delete...',
473
- editLabel: 'Edit',
474
- confirm: {
475
- delete: {
476
- title: 'Delete item',
477
- content: 'Are you sure you want to delete this item? This action cannot be undone.',
478
- button: {
479
- confirm: 'Delete',
480
- dismiss: 'Cancel'
481
- }
482
- },
483
- cancel: {
484
- title: 'Discard changes',
485
- content: 'There are unsaved changes to this item.',
486
- button: {
487
- confirm: 'Discard',
488
- dismiss: 'Cancel'
489
- }
490
- }
491
- }
492
- };
493
- }
494
- },
495
-
496
- /** @private */
497
- __isDirty: Boolean,
498
-
499
- /** @private */
500
- __isNew: Boolean,
501
-
502
- /** @private */
503
- __mobileMediaQuery: {
504
- value: '(max-width: 600px), (max-height: 600px)'
505
- },
506
-
507
- /** @private */
508
- __mobile: {
509
- type: Boolean,
510
- observer: '__mobileChanged'
511
- }
512
- };
513
- }
514
-
515
- static get observers() {
516
- return ['__onI18Change(i18n, _grid)', '__onEditOnClickChange(editOnClick, _grid)'];
517
- }
518
-
519
- /** @protected */
520
- static _finalizeClass() {
521
- super._finalizeClass();
522
-
523
- const devModeCallback = window.Vaadin.developmentModeCallback;
524
- const licenseChecker = devModeCallback && devModeCallback['vaadin-license-checker'];
525
- if (typeof licenseChecker === 'function') {
526
- licenseChecker(CrudElement);
527
- }
528
- }
529
-
530
- constructor() {
531
- super();
532
- this._observer = new FlattenedNodesObserver(this, (info) => {
533
- this.__onDomChange(info.addedNodes);
534
- });
535
- }
536
-
537
- /** @protected */
538
- ready() {
539
- super.ready();
540
- this.__editListener = (e) => this.__onCrudGridEdit(e);
541
- this.__changeListener = (e) => this.__onFormChanges(e);
542
- this.__gridSizeListener = this.__onGridSizeChanges.bind(this);
543
- this.__boundEditOnClickListener = this.__editOnClickListener.bind(this);
544
- this._grid = this.$.grid;
545
- this._form = this.$.form;
546
- this.$.dialog.$.dialog.$.overlay.addEventListener('vaadin-overlay-outside-click', this.__cancel.bind(this));
547
- this.$.dialog.$.dialog.$.overlay.addEventListener('vaadin-overlay-escape-press', this.__cancel.bind(this));
548
- }
549
-
550
- /** @private */
551
- __computeEditorHeader(isNew, newItem, editItem) {
552
- return isNew ? newItem : editItem;
553
- }
554
-
555
- /** @private */
556
- __onI18Change(i18n, grid) {
557
- if (!grid) {
558
- return;
559
- }
560
-
561
- afterNextRender(grid, () => {
562
- Array.from(grid.querySelectorAll('vaadin-crud-edit-column')).forEach((column) => {
563
- column.ariaLabel = i18n.editLabel;
564
- });
565
- });
566
- }
567
-
568
- /** @private */
569
- __onEditorPositionChange(editorPosition) {
570
- if (CrudElement._isValidEditorPosition(editorPosition)) {
571
- return;
572
- }
573
- this.editorPosition = '';
574
- }
575
-
576
- /** @private */
577
- static _isValidEditorPosition(editorPosition) {
578
- return ['bottom', 'aside'].indexOf(editorPosition) != -1;
579
- }
580
-
581
- /** @private */
582
- __onOpenedChanged(opened, old) {
583
- if (!opened && old) {
584
- this.__closeEditor();
585
- } else {
586
- this.__onFormChange(this._form);
587
- }
588
-
589
- this.__toggleToolbar();
590
- }
591
-
592
- /** @private */
593
- __mobileChanged() {
594
- this.__toggleToolbar();
595
- }
596
-
597
- /** @private */
598
- __toggleToolbar() {
599
- // Hide toolbar to give more room for the editor when it's positioned below the grid
600
- if (this.editorPosition === 'bottom' && !this.__mobile) {
601
- this.$.toolbar.style.display = this.editorOpened ? 'none' : '';
602
- }
603
- }
604
-
605
- /** @private */
606
- __onDomChange(nodes) {
607
- nodes.forEach((node) => {
608
- if (node.getAttribute) {
609
- if (node.getAttribute('slot') == 'grid') {
610
- // Force to remove listener on previous grid first
611
- this.__onEditOnClickChange(false, this._grid);
612
- this._grid = node;
613
- this.__onEditOnClickChange(this.editOnClick, this._grid);
614
- } else if (node.getAttribute('slot') == 'form') {
615
- this._form = node;
616
- }
617
- }
618
- });
619
- }
620
-
621
- /** @private */
622
- __onCrudGridEdit(e) {
623
- e.stopPropagation();
624
- this.__confirmBeforeChangingEditedItem(e.detail.item);
625
- }
626
-
627
- /** @private */
628
- __onFormChanges() {
629
- this.__isDirty = true;
630
- }
631
-
632
- /** @private */
633
- __onGridSizeChanges() {
634
- this._setSize(this._grid.size);
635
- }
636
-
637
- /** @private */
638
- __onGridChange(grid, old) {
639
- if (old) {
640
- old.removeEventListener('edit', this.__editListener);
641
- old.removeEventListener('size-changed', this.__gridSizeListener);
642
- }
643
- if (this.items) {
644
- this.__onItemsChange(this.items);
645
- }
646
- if (this.editedItem) {
647
- this.__onItemChange(this.editedItem);
648
- }
649
- grid.addEventListener('edit', this.__editListener);
650
- grid.addEventListener('size-changed', this.__gridSizeListener);
651
- this.__onGridSizeChanges();
652
- }
653
-
654
- /** @private */
655
- __onFormChange(form, old) {
656
- if (old && old.parentElement) {
657
- old.parentElement && old.parentElement.removeChild(old);
658
- old.removeEventListener('change', this.__changeListener);
659
- old.removeEventListener('input', this.__changeListener);
660
- }
661
- if (!form) {
662
- return;
663
- }
664
- if (this.items) {
665
- this.__onItemsChange(this.items);
666
- }
667
- if (this.editedItem) {
668
- this.__onItemChange(this.editedItem);
669
- }
670
- form.addEventListener('change', this.__changeListener);
671
- form.addEventListener('input', this.__changeListener);
672
- }
673
-
674
- /** @private */
675
- __onDataProviderChange(dataProvider) {
676
- if (this._grid) {
677
- this._grid.dataProvider = this.__createDataProviderProxy(dataProvider);
678
- }
679
- }
680
-
681
- /** @private */
682
- __onEditOnClickChange(rowToEditChange, _grid) {
683
- if (!_grid) {
684
- return;
685
- }
686
-
687
- if (rowToEditChange) {
688
- _grid.addEventListener('active-item-changed', this.__boundEditOnClickListener);
689
- } else {
690
- _grid.removeEventListener('active-item-changed', this.__boundEditOnClickListener);
691
- }
692
- }
693
-
694
- /** @private */
695
- __editOnClickListener(event) {
696
- const item = event.detail.value;
697
- if (this.editorOpened && this.__isDirty) {
698
- this.__confirmBeforeChangingEditedItem(item);
699
- return;
700
- }
701
- if (item) {
702
- this.__edit(item);
703
- } else if (!this.__keepOpened) {
704
- this.__closeEditor();
705
- }
706
- }
707
-
708
- /** @private */
709
- __confirmBeforeChangingEditedItem(item, keepOpened) {
710
- if (
711
- this.editorOpened && // Editor opened
712
- this.__isDirty && // Form change has been made
713
- this.editedItem !== item // Item is different
714
- ) {
715
- this.$.confirmCancel.opened = true;
716
- const listenOnce = (event) => {
717
- event.preventDefault(); // prevent editor to get closed
718
- if (item || keepOpened) {
719
- this.__edit(item);
720
- this.__clearItemAndKeepEditorOpened(item, keepOpened);
721
- } else {
722
- this.__closeEditor();
723
- }
724
- this.removeEventListener('cancel', listenOnce);
725
- };
726
- this.addEventListener('cancel', listenOnce);
727
- } else {
728
- this.__edit(item);
729
- this.__clearItemAndKeepEditorOpened(item, keepOpened);
730
- }
731
- }
732
-
733
- /** @private */
734
- __clearItemAndKeepEditorOpened(item, keepOpened) {
735
- if (!item) {
736
- setTimeout(() => {
737
- this.__keepOpened = keepOpened;
738
- this.editedItem = this._grid.activeItem = undefined;
739
- });
740
- }
741
- }
742
-
743
- /** @private */
744
- __createDataProviderProxy(dataProvider) {
745
- return (params, callback) => {
746
- const callbackProxy = (chunk, size) => {
747
- if (chunk && chunk[0]) {
748
- this.__model = chunk[0];
749
- }
750
-
751
- callback(chunk, size);
752
- };
753
-
754
- dataProvider(params, callbackProxy);
755
- };
756
- }
757
-
758
- /** @private */
759
- __onItemsChange(items) {
760
- if (this.items && this.items[0]) {
761
- this.__model = items[0];
762
- }
763
-
764
- if (this._grid) {
765
- this._grid.items = items;
766
- }
767
- }
768
-
769
- /** @private */
770
- __onItemChange(item) {
771
- if (!this._form) {
772
- return;
773
- }
774
- if (item) {
775
- if (!this._fields.length && this._form._configure) {
776
- if (this.__model) {
777
- this._form._configure(this.__model);
778
- } else {
779
- console.warn(
780
- '<vaadin-crud> Unable to autoconfigure form because the data structure is unknown. ' +
781
- 'Either specify `include` or ensure at least one item is available beforehand.'
782
- );
783
- }
784
- }
785
- this._form.item = item;
786
- this._fields.forEach((e) => {
787
- const path = e.path || e.getAttribute('path');
788
- path && (e.value = this.get(path, item));
789
- });
790
-
791
- this.__isNew = this.__isNew || (this.items && this.items.indexOf(item) < 0);
792
- this.editorOpened = true;
793
- }
794
- }
795
-
796
- /**
797
- * A reference to all fields inside the [`_form`](#/elements/vaadin-crud#property-_form) element
798
- * @return {!Array<!HTMLElement>}
799
- * @protected
800
- */
801
- get _fields() {
802
- if (!this.__fields || !this.__fields.length) {
803
- this.__fields = Array.from(this._form.querySelectorAll('*')).filter((e) => e.validate || e.checkValidity);
804
- }
805
- return this.__fields;
806
- }
807
-
808
- /** @private */
809
- __validate() {
810
- return this._fields.every((e) => (e.validate || e.checkValidity).call(e));
811
- }
812
-
813
- /** @private */
814
- __setHighlightedItem(item) {
815
- if (this._grid === this.$.grid) {
816
- this._grid.selectedItems = item ? [item] : [];
817
- }
818
- }
819
-
820
- /** @private */
821
- __closeEditor() {
822
- this.editorOpened = false;
823
- this.__isDirty = false;
824
- this.__setHighlightedItem(null);
825
-
826
- // Delay changing the item in order not to modify editor while closing
827
- setTimeout(() => this.__clearItemAndKeepEditorOpened(null, false));
828
- }
829
-
830
- /** @private */
831
- __new(event) {
832
- // This allows listening to parent element and fire only when clicking on default or custom new-button.
833
- if (event.composedPath().filter((e) => e.nodeType == 1 && e.hasAttribute('new-button'))[0]) {
834
- this.__confirmBeforeChangingEditedItem(null, true);
835
- }
836
- }
837
-
838
- /** @private */
839
- __edit(item) {
840
- if (this.editedItem === item) {
841
- return;
842
- }
843
- this.__setHighlightedItem(item);
844
- this.__openEditor('edit', item);
845
- }
846
-
847
- /** @private */
848
- __openEditor(type, item) {
849
- this.__isDirty = false;
850
- this.__isNew = !item;
851
- const evt = this.dispatchEvent(
852
- new CustomEvent(this.__isNew ? 'new' : 'edit', { detail: { item: item }, cancelable: true })
853
- );
854
- if (evt) {
855
- this.editedItem = item || {};
856
- } else {
857
- this.editorOpened = true;
858
- }
859
- }
860
-
861
- /** @private */
862
- __save() {
863
- if (!this.__validate()) {
864
- return;
865
- }
866
-
867
- const item = Object.assign({}, this.editedItem);
868
- this._fields.forEach((e) => {
869
- const path = e.path || e.getAttribute('path');
870
- path && this.__set(path, e.value, item);
871
- });
872
- const evt = this.dispatchEvent(new CustomEvent('save', { detail: { item: item }, cancelable: true }));
873
- if (evt) {
874
- if (this.__isNew && !this.dataProvider) {
875
- if (!this.items) {
876
- this.items = [item];
877
- } else {
878
- this.items.push(item);
879
- }
880
- } else {
881
- Object.assign(this.editedItem, item);
882
- }
883
- this._grid.clearCache();
884
- this.__closeEditor();
885
- }
886
- }
887
-
888
- /** @private */
889
- __cancel() {
890
- if (this.__isDirty) {
891
- this.$.confirmCancel.opened = true;
892
- } else {
893
- this.__confirmCancel();
894
- }
895
- }
896
-
897
- /** @private */
898
- __confirmCancel() {
899
- const evt = this.dispatchEvent(new CustomEvent('cancel', { detail: { item: this.editedItem }, cancelable: true }));
900
- if (evt) {
901
- this.__closeEditor();
902
- }
903
- }
904
-
905
- /** @private */
906
- __delete() {
907
- this.$.confirmDelete.opened = true;
908
- }
909
-
910
- /** @private */
911
- __confirmDelete() {
912
- const evt = this.dispatchEvent(new CustomEvent('delete', { detail: { item: this.editedItem }, cancelable: true }));
913
- if (evt) {
914
- if (this.items && this.items.indexOf(this.editedItem) >= 0) {
915
- this.items.splice(this.items.indexOf(this.editedItem), 1);
916
- }
917
- this._grid.clearCache();
918
- this.__closeEditor();
919
- }
920
- }
921
-
922
- /**
923
- * Utility method for setting nested values in JSON objects but initializing empty keys unless `Polymer.Base.set`
924
- * @private
925
- */
926
- __set(path, val, obj) {
927
- if (obj && path) {
928
- path
929
- .split('.')
930
- .slice(0, -1)
931
- .reduce((o, p) => (o[p] = o[p] || {}), obj);
932
- this.set(path, val, obj);
933
- }
934
- }
935
-
936
- /**
937
- * Fired when user wants to edit an existing item. If the default is prevented, then
938
- * a new item is not assigned to the form, giving that responsibility to the app, though
939
- * dialog is always opened.
940
- *
941
- * @event edit
942
- * @param {Object} detail.item the item to edit
943
- */
944
-
945
- /**
946
- * Fired when user wants to create a new item.
947
- *
948
- * @event new
949
- */
950
-
951
- /**
952
- * Fired when user wants to delete item. If the default is prevented, then
953
- * no action is performed, items array is not modified nor dialog closed
954
- *
955
- * @event delete
956
- * @param {Object} detail.item the item to delete
957
- */
958
-
959
- /**
960
- * Fired when user discards edition. If the default is prevented, then
961
- * no action is performed, user is responsible to close dialog and reset
962
- * item and grid.
963
- *
964
- * @event cancel
965
- * @param {Object} detail.item the item to delete
966
- */
967
-
968
- /**
969
- * Fired when user wants to save a new or an existing item. If the default is prevented, then
970
- * no action is performed, items array is not modified nor dialog closed
971
- *
972
- * @event save
973
- * @param {Object} detail.item the item to save
974
- * @param {Object} detail.new whether the item is a new one
975
- */
976
- }
977
-
978
- customElements.define(CrudElement.is, CrudElement);
979
-
980
- export { CrudElement };
13
+ export * from '@vaadin/crud/src/vaadin-crud.js';