easy-component-ui 0.0.1 → 1.0.3
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.
- package/components/Base.js +90 -0
- package/components/ea-alert/index.js +298 -0
- package/components/ea-avatar/index.js +277 -0
- package/components/ea-backtop/index.js +232 -0
- package/components/ea-badge/index.js +160 -0
- package/components/ea-button/index.js +584 -0
- package/components/ea-button-group/index.js +459 -0
- package/components/ea-calendar/index.js +409 -0
- package/components/ea-card/index.js +77 -0
- package/components/ea-carousel/index.js +434 -0
- package/components/ea-checkbox/index.js +314 -0
- package/components/ea-checkbox-group/index.js +107 -0
- package/components/ea-collapse/index.js +293 -0
- package/components/ea-descriptions/index.js +240 -0
- package/components/ea-descriptions-item/index.js +110 -0
- package/components/ea-empty/index.js +141 -0
- package/{icon → components/ea-icon}/config.json +1017 -1017
- package/{icon → components/ea-icon}/css/animation.css +85 -85
- package/{icon → components/ea-icon}/css/fontello.css +224 -224
- package/components/ea-icon/font/fontello.svg +683 -0
- package/components/ea-icon/index.css +2 -0
- package/components/ea-icon/index.js +47 -0
- package/components/ea-image/index.js +412 -0
- package/components/ea-infinite-scroll/index.js +170 -0
- package/components/ea-input/index.js +765 -0
- package/components/ea-input-number/index.js +458 -0
- package/components/ea-link/index.js +200 -0
- package/components/ea-loading/index.js +218 -0
- package/components/ea-message/MessageClass.js +71 -0
- package/components/ea-message/index.js +233 -0
- package/components/ea-message-box/EaMessageBoxClass.js +48 -0
- package/components/ea-message-box/index.js +202 -0
- package/components/ea-pagination/index.js +444 -0
- package/components/ea-progress/index.js +333 -0
- package/components/ea-radio/index.js +287 -0
- package/components/ea-radio-group/index.js +59 -0
- package/components/ea-rate/index.js +326 -0
- package/components/ea-result/index.js +167 -0
- package/components/ea-select/index.js +34 -0
- package/components/ea-skeleton/index.js +341 -0
- package/components/ea-switch/index.js +301 -0
- package/components/ea-tag/index.js +212 -0
- package/components/ea-textarea/index.js +333 -0
- package/components/ea-timeline/index.js +334 -0
- package/components/ea-ui-base-style.css +0 -0
- package/package.json +1 -1
- package/utils/createElement.js +30 -0
- package/utils/handleTemplate.js +19 -0
- package/utils/setStyle.js +8 -0
- package/icon/font/fontello.svg +0 -346
- package/icon/index.css +0 -2
- package/index.js +0 -1693
- /package/{icon → components/ea-icon}/css/fontello-codes.css +0 -0
- /package/{icon → components/ea-icon}/css/fontello-embedded.css +0 -0
- /package/{icon → components/ea-icon}/css/fontello-ie7-codes.css +0 -0
- /package/{icon → components/ea-icon}/css/fontello-ie7.css +0 -0
- /package/{icon → components/ea-icon}/font/fontello.eot +0 -0
- /package/{icon → components/ea-icon}/font/fontello.ttf +0 -0
- /package/{icon → components/ea-icon}/font/fontello.woff +0 -0
- /package/{icon → components/ea-icon}/font/fontello.woff2 +0 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import Base from '../Base.js';
|
|
3
|
+
|
|
4
|
+
const imageElement = `
|
|
5
|
+
<svg class="skeleton-image" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
|
|
6
|
+
<path d="M15 20h70v60H15z" stroke="#c0c4cc" stroke-width="5px" fill="none" />
|
|
7
|
+
<circle r="8" cx="32" cy="35" fill="#c0c4cc" />
|
|
8
|
+
<path d="M60 42.5L39 75h42z" fill="#c0c4cc" />
|
|
9
|
+
<path d="M35 52.5L20 75h-4 32z" fill="#c0c4cc" />
|
|
10
|
+
</svg>
|
|
11
|
+
`;
|
|
12
|
+
|
|
13
|
+
const stylesheet = `
|
|
14
|
+
@keyframes skeleton-loading {
|
|
15
|
+
0% {
|
|
16
|
+
background-position: 100% 50%;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
.ea-skeleton_item,
|
|
20
|
+
.ea-skeleton-item_wrap {
|
|
21
|
+
position: relative;
|
|
22
|
+
display: inline-block;
|
|
23
|
+
background-color: #f2f2f2;
|
|
24
|
+
height: 16px;
|
|
25
|
+
border-radius: 4px;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.ea-skeleton-item_wrap.animated {
|
|
29
|
+
background-image: linear-gradient(90deg, #f6f6f6 25%, #e8e8e8 37%, #f6f6f6 63%);
|
|
30
|
+
background-size: 400% 100%;
|
|
31
|
+
animation: skeleton-loading 1.4s ease infinite;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.ea-skeleton_wrap.animated .ea-skeleton_item {
|
|
35
|
+
background-image: linear-gradient(90deg, #f6f6f6 25%, #e8e8e8 37%, #f6f6f6 63%);
|
|
36
|
+
background-size: 400% 100%;
|
|
37
|
+
animation: skeleton-loading 1.4s ease infinite;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.ea-skeleton_wrap {
|
|
41
|
+
width: 100%;
|
|
42
|
+
}
|
|
43
|
+
.ea-skeleton_wrap .ea-skeleton_item.el-skeleton_p {
|
|
44
|
+
width: 100%;
|
|
45
|
+
}
|
|
46
|
+
.ea-skeleton_wrap .ea-skeleton_item.el-skeleton_p.el-skeleton_paragraph {
|
|
47
|
+
width: 100%;
|
|
48
|
+
margin-top: 16px;
|
|
49
|
+
}
|
|
50
|
+
.ea-skeleton_wrap .ea-skeleton_item.el-skeleton_p.is-first {
|
|
51
|
+
width: 33%;
|
|
52
|
+
}
|
|
53
|
+
.ea-skeleton_wrap .ea-skeleton_item.el-skeleton_p.is-last {
|
|
54
|
+
width: 61%;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.ea-skeleton-item_wrap .skeleton-image {
|
|
58
|
+
width: 30%;
|
|
59
|
+
height: 30%;
|
|
60
|
+
}
|
|
61
|
+
.ea-skeleton-item_wrap.ea-skeleton_p {
|
|
62
|
+
width: 100%;
|
|
63
|
+
}
|
|
64
|
+
.ea-skeleton-item_wrap.ea-skeleton_image {
|
|
65
|
+
width: unset;
|
|
66
|
+
display: flex;
|
|
67
|
+
align-items: center;
|
|
68
|
+
justify-content: center;
|
|
69
|
+
border-radius: 0;
|
|
70
|
+
}
|
|
71
|
+
.ea-skeleton-item_wrap.ea-skeleton_text {
|
|
72
|
+
width: 100%;
|
|
73
|
+
height: 13px;
|
|
74
|
+
}
|
|
75
|
+
`;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 创建一个具有骨架屏样式的元素。
|
|
79
|
+
*
|
|
80
|
+
* @param {string} item - 包含要添加到新元素的类名。
|
|
81
|
+
* @param {boolean} isFirst - 指示生成的元素是否应该是列表中的第一个元素。
|
|
82
|
+
* @param {boolean} isLast - 指示生成的元素是否应该是列表中的最后一个元素。
|
|
83
|
+
* @returns {HTMLElement} - 返回一个新创建的div元素。
|
|
84
|
+
*/
|
|
85
|
+
const getSkeletonElement = (className) => {
|
|
86
|
+
const el = document.createElement('div');
|
|
87
|
+
el.className = `ea-skeleton_item el-skeleton_p ${className ? className : ''}`;
|
|
88
|
+
|
|
89
|
+
return el;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export class EaSkeleton extends Base {
|
|
93
|
+
#mounted = false;
|
|
94
|
+
|
|
95
|
+
#wrap;
|
|
96
|
+
|
|
97
|
+
#customizationSlot;
|
|
98
|
+
#defaultSlot;
|
|
99
|
+
|
|
100
|
+
constructor() {
|
|
101
|
+
super();
|
|
102
|
+
|
|
103
|
+
const shadowRoot = this.attachShadow({ mode: 'open' });
|
|
104
|
+
const wrap = document.createElement('div');
|
|
105
|
+
wrap.className = 'ea-skeleton_wrap';
|
|
106
|
+
|
|
107
|
+
const defaultSlot = document.createElement('slot');
|
|
108
|
+
defaultSlot.style.display = 'none';
|
|
109
|
+
|
|
110
|
+
const customizationSlot = document.createElement('slot');
|
|
111
|
+
customizationSlot.name = 'template';
|
|
112
|
+
|
|
113
|
+
this.#wrap = wrap;
|
|
114
|
+
this.#defaultSlot = defaultSlot;
|
|
115
|
+
this.#customizationSlot = customizationSlot;
|
|
116
|
+
|
|
117
|
+
this.build(shadowRoot, stylesheet);
|
|
118
|
+
this.shadowRoot.appendChild(wrap);
|
|
119
|
+
this.shadowRoot.appendChild(customizationSlot);
|
|
120
|
+
this.shadowRoot.appendChild(defaultSlot);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ------- row 骨架屏的渲染行数 -------
|
|
124
|
+
// #region
|
|
125
|
+
get row() {
|
|
126
|
+
return this.getAttrNumber('row') || 4;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
set row(value) {
|
|
130
|
+
this.setAttribute('row', value);
|
|
131
|
+
}
|
|
132
|
+
// #endregion
|
|
133
|
+
// ------- end -------
|
|
134
|
+
|
|
135
|
+
// ------- animated 骨架屏的动画效果 -------
|
|
136
|
+
// #region
|
|
137
|
+
get animated() {
|
|
138
|
+
return this.getAttrBoolean('animated');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
set animated(value) {
|
|
142
|
+
this.setAttribute('animated', value);
|
|
143
|
+
|
|
144
|
+
this.#wrap.classList.toggle('animated', value);
|
|
145
|
+
|
|
146
|
+
}
|
|
147
|
+
// #endregion
|
|
148
|
+
// ------- end -------
|
|
149
|
+
|
|
150
|
+
// ------- count 元素重复数 -------
|
|
151
|
+
// #region
|
|
152
|
+
get count() {
|
|
153
|
+
return this.getAttrNumber('count') || 1;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
set count(value) {
|
|
157
|
+
this.setAttribute('count', value);
|
|
158
|
+
|
|
159
|
+
this.#wrap.innerHTML = '';
|
|
160
|
+
}
|
|
161
|
+
// #endregion
|
|
162
|
+
// ------- end -------
|
|
163
|
+
|
|
164
|
+
// ------- loading 是否显示骨架屏 -------
|
|
165
|
+
// #region
|
|
166
|
+
get loading() {
|
|
167
|
+
return this.getAttrBoolean('loading') || true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
set loading(value) {
|
|
171
|
+
this.setAttribute('loading', value);
|
|
172
|
+
|
|
173
|
+
if (value) {
|
|
174
|
+
this.#customizationSlot.style.display = 'block';
|
|
175
|
+
this.#defaultSlot.style.display = 'none';
|
|
176
|
+
} else {
|
|
177
|
+
this.#customizationSlot.style.display = 'none';
|
|
178
|
+
this.#defaultSlot.style.display = 'block';
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// #endregion
|
|
182
|
+
// ------- end -------
|
|
183
|
+
|
|
184
|
+
// 默认骨架屏的初始化
|
|
185
|
+
defaultSkeletonInit(row) {
|
|
186
|
+
row = Number(row) || 4;
|
|
187
|
+
|
|
188
|
+
const firstSkeleton = getSkeletonElement('is-first');
|
|
189
|
+
this.#wrap.appendChild(firstSkeleton);
|
|
190
|
+
|
|
191
|
+
for (let i = 0; i < row - 2; i++) {
|
|
192
|
+
const paragraphSkeleton = getSkeletonElement('el-skeleton_paragraph');
|
|
193
|
+
this.#wrap.appendChild(paragraphSkeleton);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const lastSkeleton = getSkeletonElement('el-skeleton_paragraph is-last');
|
|
197
|
+
this.#wrap.appendChild(lastSkeleton);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 渲染自定义骨架屏
|
|
201
|
+
customizationSkeletonInit() {
|
|
202
|
+
const item = this.querySelectorAll('ea-skeleton-item');
|
|
203
|
+
|
|
204
|
+
if (item.length > 0) this.#wrap.innerHTML = "";
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 渲染带动画的骨架屏
|
|
208
|
+
handleSkeletonItemAniamted(isAnimated) {
|
|
209
|
+
if (!isAnimated) return;
|
|
210
|
+
|
|
211
|
+
const items = this.querySelectorAll('ea-skeleton-item');
|
|
212
|
+
items.forEach(item => {
|
|
213
|
+
item.setAttribute("animated", true);
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 渲染骨架屏的渲染个数
|
|
218
|
+
handleSkeletonItemCount(count) {
|
|
219
|
+
const item = this.querySelector('[slot="template"]');
|
|
220
|
+
let template = ``;
|
|
221
|
+
|
|
222
|
+
for (let i = 0; i < count; i++) {
|
|
223
|
+
template += item.innerHTML;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
item.innerHTML = template;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
init() {
|
|
230
|
+
const that = this;
|
|
231
|
+
|
|
232
|
+
this.animated = this.animated;
|
|
233
|
+
|
|
234
|
+
this.count = this.count;
|
|
235
|
+
|
|
236
|
+
this.loading = this.loading;
|
|
237
|
+
|
|
238
|
+
const items = this.querySelectorAll('ea-skeleton-item');
|
|
239
|
+
if (items.length > 0) {
|
|
240
|
+
this.#wrap.style.display = "none";
|
|
241
|
+
|
|
242
|
+
this.handleSkeletonItemCount(this.count);
|
|
243
|
+
|
|
244
|
+
this.handleSkeletonItemAniamted(this.animated);
|
|
245
|
+
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
this.row = this.row;
|
|
252
|
+
this.defaultSkeletonInit(this.row);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
connectedCallback() {
|
|
256
|
+
this.init();
|
|
257
|
+
|
|
258
|
+
this.#mounted = true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export class EaSkeletonItem extends Base {
|
|
263
|
+
#wrap;
|
|
264
|
+
|
|
265
|
+
static get observedAttributes() {
|
|
266
|
+
return ['animated'];
|
|
267
|
+
}
|
|
268
|
+
get variantOptions() {
|
|
269
|
+
return ["text", "image", "p"];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
constructor() {
|
|
273
|
+
super();
|
|
274
|
+
|
|
275
|
+
const shadowRoot = this.attachShadow({ mode: 'open' });
|
|
276
|
+
const wrap = document.createElement('div');
|
|
277
|
+
wrap.className = 'ea-skeleton-item_wrap';
|
|
278
|
+
|
|
279
|
+
this.#wrap = wrap;
|
|
280
|
+
|
|
281
|
+
this.build(shadowRoot, stylesheet);
|
|
282
|
+
this.shadowRoot.appendChild(wrap);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// ------- style html标签上的样式 -------
|
|
286
|
+
// #region
|
|
287
|
+
get elementStyle() {
|
|
288
|
+
return this.getAttribute('style');
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
set elementStyle(value) {
|
|
292
|
+
this.setAttribute('style', value);
|
|
293
|
+
this.#wrap.setAttribute('style', value);
|
|
294
|
+
}
|
|
295
|
+
// #endregion
|
|
296
|
+
// ------- end -------
|
|
297
|
+
|
|
298
|
+
// ------- variant html标签标识 -------
|
|
299
|
+
// #region
|
|
300
|
+
get variant() {
|
|
301
|
+
return this.getAttribute('variant');
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
set variant(value) {
|
|
305
|
+
this.variantOptions.includes(value) ? this.setAttribute('variant', value) : this.setAttribute('variant', 'text');
|
|
306
|
+
|
|
307
|
+
if (value === "image") this.#wrap.innerHTML = imageElement;
|
|
308
|
+
|
|
309
|
+
this.#wrap.classList.add("ea-skeleton_" + this.variant);
|
|
310
|
+
}
|
|
311
|
+
// #endregion
|
|
312
|
+
// ------- end -------
|
|
313
|
+
|
|
314
|
+
init() {
|
|
315
|
+
const that = this;
|
|
316
|
+
|
|
317
|
+
this.variant = this.variant;
|
|
318
|
+
|
|
319
|
+
this.elementStyle = this.elementStyle;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
connectedCallback() {
|
|
323
|
+
this.init();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
attributeChangedCallback(name, oldVal, newVal) {
|
|
327
|
+
switch (name) {
|
|
328
|
+
case "animated":
|
|
329
|
+
this.#wrap.classList.toggle("animated", this.getAttrBoolean(name));
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (!customElements.get('ea-skeleton')) {
|
|
336
|
+
customElements.define('ea-skeleton', EaSkeleton);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (!customElements.get('ea-skeleton-item')) {
|
|
340
|
+
customElements.define('ea-skeleton-item', EaSkeletonItem);
|
|
341
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import Base from '../Base.js';
|
|
3
|
+
|
|
4
|
+
const stylesheet = `
|
|
5
|
+
.ea-switch_wrap {
|
|
6
|
+
display: flex;
|
|
7
|
+
align-items: center;
|
|
8
|
+
}
|
|
9
|
+
.ea-switch_wrap input {
|
|
10
|
+
display: none;
|
|
11
|
+
}
|
|
12
|
+
.ea-switch_wrap span {
|
|
13
|
+
display: block;
|
|
14
|
+
cursor: default;
|
|
15
|
+
}
|
|
16
|
+
.ea-switch_wrap .ea-switch_label {
|
|
17
|
+
color: #606266;
|
|
18
|
+
font-size: 0.875rem;
|
|
19
|
+
cursor: pointer;
|
|
20
|
+
transition: color 0.2s;
|
|
21
|
+
}
|
|
22
|
+
.ea-switch_wrap .ea-switch_label.ea-switch_label--active {
|
|
23
|
+
color: #409eff;
|
|
24
|
+
}
|
|
25
|
+
.ea-switch_wrap .ea-switch_core {
|
|
26
|
+
position: relative;
|
|
27
|
+
cursor: pointer;
|
|
28
|
+
margin: 0 0.75rem;
|
|
29
|
+
width: 2.5rem;
|
|
30
|
+
height: 1.25rem;
|
|
31
|
+
line-height: 1.25rem;
|
|
32
|
+
background-color: #dcdfe6;
|
|
33
|
+
border-radius: 999px;
|
|
34
|
+
transition: background-color 0.2s;
|
|
35
|
+
}
|
|
36
|
+
.ea-switch_wrap .ea-switch_core.ea-switch_core--checked {
|
|
37
|
+
background-color: #409eff;
|
|
38
|
+
}
|
|
39
|
+
.ea-switch_wrap .ea-switch_core::after {
|
|
40
|
+
content: "";
|
|
41
|
+
display: block;
|
|
42
|
+
position: absolute;
|
|
43
|
+
left: 2px;
|
|
44
|
+
top: 50%;
|
|
45
|
+
transform: translate(0, -50%);
|
|
46
|
+
width: 1rem;
|
|
47
|
+
height: 1rem;
|
|
48
|
+
border-radius: 999px;
|
|
49
|
+
background-color: #fff;
|
|
50
|
+
transition: left 0.2s, transform 0.2s;
|
|
51
|
+
}
|
|
52
|
+
.ea-switch_wrap .ea-switch_core.ea-switch_core--checked::after {
|
|
53
|
+
left: calc(100% - 1rem - 2px);
|
|
54
|
+
}
|
|
55
|
+
.ea-switch_wrap .ea-switch_core.ea-switch_core--disabled {
|
|
56
|
+
background-color: #c0c4cc;
|
|
57
|
+
}
|
|
58
|
+
.ea-switch_wrap.ea-switch_wrap--disabled {
|
|
59
|
+
cursor: not-allowed;
|
|
60
|
+
}
|
|
61
|
+
.ea-switch_wrap.ea-switch_wrap--disabled .ea-switch_label,
|
|
62
|
+
.ea-switch_wrap.ea-switch_wrap--disabled .ea-switch_core {
|
|
63
|
+
pointer-events: none;
|
|
64
|
+
}
|
|
65
|
+
.ea-switch_wrap.ea-switch_wrap--disabled .ea-switch_core {
|
|
66
|
+
background-color: #eff1f5;
|
|
67
|
+
}
|
|
68
|
+
.ea-switch_wrap.ea-switch_wrap--disabled .ea-switch_label {
|
|
69
|
+
color: #c0c4cc;
|
|
70
|
+
}
|
|
71
|
+
.ea-switch_wrap.ea-switch_wrap--disabled .ea-switch_label.ea-switch_label--active {
|
|
72
|
+
color: #7ebfff;
|
|
73
|
+
}
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
const inputDom = () => {
|
|
77
|
+
const input = document.createElement('input');
|
|
78
|
+
input.type = 'checkbox';
|
|
79
|
+
input.className = 'ea-switch_input';
|
|
80
|
+
|
|
81
|
+
return input;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const inputCoreDom = () => {
|
|
85
|
+
const div = document.createElement('span');
|
|
86
|
+
div.className = 'ea-switch_core';
|
|
87
|
+
|
|
88
|
+
return div;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const labelDom = (posi) => {
|
|
92
|
+
const label = document.createElement('span');
|
|
93
|
+
label.className = `ea-switch_label ea-switch_label--${posi}`;
|
|
94
|
+
|
|
95
|
+
return label;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export class EaSwitch extends Base {
|
|
99
|
+
#mounted = false;
|
|
100
|
+
|
|
101
|
+
#wrap;
|
|
102
|
+
#input;
|
|
103
|
+
|
|
104
|
+
#labelLeft;
|
|
105
|
+
#inputCore;
|
|
106
|
+
#labelRight;
|
|
107
|
+
|
|
108
|
+
static get observedAttributes() {
|
|
109
|
+
return ['checked'];
|
|
110
|
+
}
|
|
111
|
+
constructor() {
|
|
112
|
+
super();
|
|
113
|
+
|
|
114
|
+
const shadowRoot = this.attachShadow({ mode: 'open' });
|
|
115
|
+
const wrap = document.createElement('div');
|
|
116
|
+
wrap.className = 'ea-switch_wrap';
|
|
117
|
+
|
|
118
|
+
const input = inputDom();
|
|
119
|
+
const leftLabel = labelDom('left');
|
|
120
|
+
const inputCore = inputCoreDom();
|
|
121
|
+
const rightLabel = labelDom('right');
|
|
122
|
+
|
|
123
|
+
wrap.appendChild(input);
|
|
124
|
+
wrap.appendChild(leftLabel);
|
|
125
|
+
wrap.appendChild(inputCore);
|
|
126
|
+
wrap.appendChild(rightLabel);
|
|
127
|
+
|
|
128
|
+
this.#wrap = wrap;
|
|
129
|
+
this.#input = input;
|
|
130
|
+
this.#labelLeft = leftLabel;
|
|
131
|
+
this.#inputCore = inputCore;
|
|
132
|
+
this.#labelRight = rightLabel;
|
|
133
|
+
|
|
134
|
+
this.build(shadowRoot, stylesheet);
|
|
135
|
+
this.shadowRoot.appendChild(wrap);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ------- inactive-text 关闭时选项 -------
|
|
139
|
+
// #region
|
|
140
|
+
get inactiveText() {
|
|
141
|
+
return this.getAttribute('inactive-text');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
set inactiveText(value) {
|
|
145
|
+
this.setAttribute('inactive-text', value);
|
|
146
|
+
this.#labelLeft.innerText = value;
|
|
147
|
+
}
|
|
148
|
+
// #endregion
|
|
149
|
+
// ------- end -------
|
|
150
|
+
|
|
151
|
+
// ------- active-text 开启时选项 -------
|
|
152
|
+
// #region
|
|
153
|
+
get activeText() {
|
|
154
|
+
return this.getAttribute('active-text');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
set activeText(value) {
|
|
158
|
+
this.setAttribute('active-text', value);
|
|
159
|
+
this.#labelRight.innerText = value;
|
|
160
|
+
}
|
|
161
|
+
// #endregion
|
|
162
|
+
// ------- end -------
|
|
163
|
+
|
|
164
|
+
// ------- checked 选中 -------
|
|
165
|
+
// #region
|
|
166
|
+
get checked() {
|
|
167
|
+
return this.getAttrBoolean('checked');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
set checked(value) {
|
|
171
|
+
this.setAttribute('checked', value);
|
|
172
|
+
this.#input.checked = value;
|
|
173
|
+
this.#input.setAttribute('checked', value);
|
|
174
|
+
|
|
175
|
+
if (value) {
|
|
176
|
+
this.#inputCore.classList.add('ea-switch_core--checked');
|
|
177
|
+
this.#labelRight.classList.add('ea-switch_label--active');
|
|
178
|
+
|
|
179
|
+
this.#labelLeft.classList.remove('ea-switch_label--active');
|
|
180
|
+
} else {
|
|
181
|
+
this.#inputCore.classList.remove('ea-switch_core--checked');
|
|
182
|
+
this.#labelLeft.classList.add('ea-switch_label--active');
|
|
183
|
+
|
|
184
|
+
this.#labelRight.classList.remove('ea-switch_label--active');
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// #endregion
|
|
188
|
+
// ------- end -------
|
|
189
|
+
|
|
190
|
+
// ------- disabled 禁用 -------
|
|
191
|
+
// #region
|
|
192
|
+
get disabled() {
|
|
193
|
+
return this.getAttrBoolean('disabled');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
set disabled(value) {
|
|
197
|
+
this.setAttribute('disabled', value);
|
|
198
|
+
this.#input.disabled = value;
|
|
199
|
+
this.#wrap.classList.toggle('ea-switch_wrap--disabled', value);
|
|
200
|
+
}
|
|
201
|
+
// #endregion
|
|
202
|
+
// ------- end -------
|
|
203
|
+
|
|
204
|
+
// ------- inactive-color 关闭时颜色 -------
|
|
205
|
+
// #region
|
|
206
|
+
get inactiveColor() {
|
|
207
|
+
return this.getAttribute('inactive-color');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
set inactiveColor(value) {
|
|
211
|
+
if (!value) return;
|
|
212
|
+
|
|
213
|
+
this.setAttribute('inactive-color', value);
|
|
214
|
+
if (value) this.#inputCore.style.backgroundColor = value;
|
|
215
|
+
}
|
|
216
|
+
// #endregion
|
|
217
|
+
// ------- end -------
|
|
218
|
+
|
|
219
|
+
// ------- active-color 开启时颜色 -------
|
|
220
|
+
// #region
|
|
221
|
+
get activeColor() {
|
|
222
|
+
return this.getAttribute('active-color');
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
set activeColor(value) {
|
|
226
|
+
if (!value) return;
|
|
227
|
+
|
|
228
|
+
this.setAttribute('active-color', value);
|
|
229
|
+
this.#inputCore.style.backgroundColor = value;
|
|
230
|
+
}
|
|
231
|
+
// #endregion
|
|
232
|
+
// ------- end -------
|
|
233
|
+
|
|
234
|
+
handleInputChecked(e) {
|
|
235
|
+
const currentChecked = this.#input.checked;
|
|
236
|
+
|
|
237
|
+
if (this.inactiveColor && this.activeColor) {
|
|
238
|
+
this.#inputCore.style.backgroundColor = currentChecked ? this.inactiveColor : this.activeColor;
|
|
239
|
+
} else {
|
|
240
|
+
this.#inputCore.classList.toggle('ea-switch_core--checked', currentChecked);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
init() {
|
|
245
|
+
const that = this;
|
|
246
|
+
|
|
247
|
+
// 设置勾选值
|
|
248
|
+
this.checked = this.checked;
|
|
249
|
+
|
|
250
|
+
// 设置开启/关闭值
|
|
251
|
+
this.inactiveText = this.inactiveText;
|
|
252
|
+
this.activeText = this.activeText;
|
|
253
|
+
|
|
254
|
+
// 设置禁用
|
|
255
|
+
this.disabled = this.disabled;
|
|
256
|
+
|
|
257
|
+
// 设置颜色
|
|
258
|
+
if (this.activeColor && this.inactiveColor) {
|
|
259
|
+
if (this.checked) this.activeColor = this.activeColor;
|
|
260
|
+
else this.inactiveColor = this.inactiveColor;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
this.#labelLeft.addEventListener('click', function (e) {
|
|
265
|
+
that.checked = !that.#input.checked;
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
this.#labelRight.addEventListener('click', function (e) {
|
|
269
|
+
that.checked = !that.#input.checked;
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
this.#inputCore.addEventListener('click', function (e) {
|
|
273
|
+
that.checked = !that.#input.checked;
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
connectedCallback() {
|
|
278
|
+
this.init();
|
|
279
|
+
this.#mounted = true;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
attributeChangedCallback(name, oldVal, newVal) {
|
|
283
|
+
if (!this.#mounted) return;
|
|
284
|
+
|
|
285
|
+
if (name === 'checked') {
|
|
286
|
+
const value = this.checked ? this.activeText : this.inactiveText;
|
|
287
|
+
this.handleInputChecked();
|
|
288
|
+
|
|
289
|
+
this.dispatchEvent(new CustomEvent("change", {
|
|
290
|
+
detail: {
|
|
291
|
+
checked: this.checked,
|
|
292
|
+
value,
|
|
293
|
+
},
|
|
294
|
+
}))
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (!customElements.get('ea-switch')) {
|
|
300
|
+
customElements.define('ea-switch', EaSwitch);
|
|
301
|
+
}
|