-
Notifications
You must be signed in to change notification settings - Fork 218
/
crudToolbar.ts
183 lines (156 loc) · 5.55 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
/*
* 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';
const CLASS_BG = 'editBackground'
const CLASS_FG = 'editForground';
const CLASS_FG_FULLSCREEN = 'editForgroundBig';
let ICON_CLASS_FS = 'bi-arrows-fullscreen';
let ICON_CLASS_FS_EXIT = 'bi-fullscreen-exit';
const ATTR_FULLSCREEN = 'fullscreen';
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,
buttonFullscreen: null,
divRoot: null,
};
get idValue() {
return this.dom.inputIdValue.value;
}
set idValue(newValue) {
(this.shadowRoot.getElementById('inputIdValue') as HTMLInputElement).value = newValue;
const buttonDelete = this.shadowRoot.getElementById('buttonDelete');
if (!this.isDeleteDisabled && newValue && newValue !== '') {
buttonDelete.removeAttribute('hidden');
} else {
buttonDelete.setAttribute('hidden', '');
}
}
get editDisabled() {
return this.isEditDisabled;
}
set createDisabled(newValue) {
this.isCreateDisabled = newValue;
this.lazyInit(this.dom.buttonCreate);
this.setButtonState(this.dom.buttonCreate, newValue);
}
set deleteDisabled(newValue) {
this.isDeleteDisabled = newValue;
this.lazyInit(this.dom.buttonDelete);
this.setButtonState(this.dom.buttonDelete, newValue);
}
set editDisabled(newValue) {
this.isEditDisabled = newValue;
if (!this.isEditing) {
this.lazyInit(this.dom.buttonEdit);
this.setButtonState(this.dom.buttonEdit, newValue);
}
}
lazyInit(element: HTMLElement) {
if (!element) {
Utils.getAllElementsById(this.dom, this.shadowRoot);
}
}
setButtonState(button: HTMLButtonElement, isDisabled: boolean) {
// 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.buttonFullscreen.onclick = () => this.toggleFullscreen();
});
};
eventDispatcher(eventName) {
return () => {
this.dispatchEvent(new CustomEvent(eventName, {
composed: true,
}));
};
}
toggleEdit(isCancel) {
this.isEditing = !this.isEditing;
// toggle modal mode;
document.getElementById('modalCrudEdit').classList.toggle(CLASS_BG);
document.body.classList.toggle('modal-open');
document.body.style.overflow = this.isEditing && this.hasAttribute(ATTR_FULLSCREEN) ? 'hidden' : '';
this.dom.divRoot.classList.toggle(this.hasAttribute(ATTR_FULLSCREEN) ? CLASS_FG_FULLSCREEN : CLASS_FG);
// toggle button states;
this.setButtonState(this.dom.buttonEdit, this.isEditing || this.isEditDisabled);
this.setButtonState(this.dom.buttonFullscreen, !this.isEditing);
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,
},
}));
}
toggleFullscreen() {
this.toggleAttribute(ATTR_FULLSCREEN);
this.dom.buttonFullscreen.querySelector('.bi').classList.toggle(ICON_CLASS_FS);
this.dom.buttonFullscreen.querySelector('.bi').classList.toggle(ICON_CLASS_FS_EXIT);
document.body.style.overflow = this.isEditing && this.hasAttribute(ATTR_FULLSCREEN) ? 'hidden' : '';
if (this.dom.divRoot.classList.contains(CLASS_FG)) {
this.dom.divRoot.classList.replace(CLASS_FG, CLASS_FG_FULLSCREEN);
} else {
this.dom.divRoot.classList.replace(CLASS_FG_FULLSCREEN, CLASS_FG);
}
}
}
customElements.define('crud-toolbar', CrudToolbar);