-
Notifications
You must be signed in to change notification settings - Fork 224
/
crudToolbar.ts
194 lines (168 loc) · 5.36 KB
/
crudToolbar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
/* eslint-disable require-jsdoc */
import * as Utils from '../utils.js';
import crudToolbarHTML from './crudToolbar.html';
export enum CrudOperation {
CREATE,
UPDATE,
DELETE,
}
export class CrudToolbar extends HTMLElement {
isEditing = false;
isEditDisabled = false;
isCreateDisabled = false;
isDeleteDisabled = false;
dom = {
label: null,
inputIdValue: null,
buttonEdit: null,
buttonCreate: null,
buttonUpdate: null,
buttonDelete: null,
buttonCancel: null,
divRoot: null,
};
static get observedAttributes() {
return [`extraeditclass`];
}
private _extraEditClass: string;
get extraeditclass() {
return this._extraEditClass;
}
set extraeditclass(val: string) {
if (val == null) { // check for null and undefined
this.removeAttribute('extraeditclass');
}
else {
this.setAttribute('extraeditclass', val);
}
}
get idValue() {
return this.dom.inputIdValue.value;
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === `extraeditclass`) {
this._extraEditClass = newValue;
}
}
set idValue(newValue) {
const domInput: HTMLInputElement = this.shadowRoot.getElementById('inputIdValue') as HTMLInputElement;
domInput.value = newValue;
domInput.classList.remove('is-invalid');
const buttonEdit = this.shadowRoot.getElementById('buttonEdit');
if (newValue && newValue !== '') {
buttonEdit.innerText = 'Edit';
} else {
buttonEdit.innerText = 'Create'
}
const buttonDelete = this.shadowRoot.getElementById('buttonDelete');
if (!this.isEditing && !this.isDeleteDisabled && newValue && newValue !== '') {
buttonDelete.removeAttribute('hidden');
} else {
buttonDelete.setAttribute('hidden', '');
}
}
get editDisabled() {
return this.isEditDisabled;
}
set createDisabled(newValue: boolean) {
this.isCreateDisabled = newValue;
this.setButtonState('buttonCreate', newValue);
}
set deleteDisabled(newValue: boolean) {
this.isDeleteDisabled = newValue;
this.setButtonState('buttonDelete', newValue);
}
set editDisabled(newValue: boolean) {
this.isEditDisabled = newValue;
if (!this.isEditing) {
this.setButtonState('buttonEdit', newValue);
}
}
setButtonState(buttonId, isDisabled) {
const button = this.shadowRoot.getElementById(buttonId);
if (isDisabled) {
button.setAttribute('hidden', '');
} else {
button.removeAttribute('hidden');
}
}
get validationElement() {
return this.dom.inputIdValue;
}
constructor() {
super();
this.attachShadow({mode: 'open'});
}
connectedCallback() {
this.shadowRoot.innerHTML = crudToolbarHTML;
setTimeout(() => {
Utils.getAllElementsById(this.dom, this.shadowRoot);
this.dom.buttonEdit.onclick = () => this.toggleEdit(false);
this.dom.buttonCancel.onclick = () => this.toggleEdit(true);
this.dom.label.innerText = this.getAttribute('label') || 'Label';
this.dom.buttonCreate.onclick = this.eventDispatcher('onCreateClick');
this.dom.buttonUpdate.onclick = this.eventDispatcher('onUpdateClick');
this.dom.buttonDelete.onclick = this.eventDispatcher('onDeleteClick');
this.dom.inputIdValue.addEventListener('change', (event) => {
(event.target as HTMLElement).classList.remove('is-invalid');
this.dispatchEvent(new CustomEvent('onIdValueChange', { composed: true }));
});
});
};
eventDispatcher(eventName) {
return () => {
this.dispatchEvent(new CustomEvent(eventName, {
composed: true,
}));
};
}
toggleEdit(isCancel) {
this.isEditing = !this.isEditing;
document.getElementById('modalCrudEdit').classList.toggle('editBackground');
this.dom.divRoot.classList.toggle('editForground');
if (this._extraEditClass) {
this.dom.divRoot.classList.toggle(this._extraEditClass);
}
if (this.isEditing || this.isEditDisabled) {
this.dom.buttonEdit.setAttribute('hidden', '');
} else {
this.dom.buttonEdit.removeAttribute('hidden');
}
this.dom.buttonCancel.toggleAttribute('hidden');
if (this.isEditing) {
if (this.dom.inputIdValue.value) {
this.dom.buttonUpdate.toggleAttribute('hidden');
} else if (!this.isCreateDisabled) {
this.dom.buttonCreate.toggleAttribute('hidden');
}
} else {
this.dom.buttonCreate.setAttribute('hidden', '');
this.dom.buttonUpdate.setAttribute('hidden', '');
}
if (this.isEditing || !this.dom.inputIdValue.value) {
this.dom.buttonDelete.setAttribute('hidden', '');
}
const allowIdChange = this.isEditing && (!this.dom.inputIdValue.value || this.hasAttribute('allowIdChange'));
this.dom.inputIdValue.disabled = !allowIdChange;
this.dispatchEvent(new CustomEvent('onEditToggle', {
composed: true,
detail: {
isEditing: this.isEditing,
isCancel: isCancel,
},
}));
}
}
customElements.define('crud-toolbar', CrudToolbar);