-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathh2-mask.js
398 lines (334 loc) · 8.68 KB
/
h2-mask.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
`h2-mask`
Example:
```html
<h2-mask label="输入框" class="mask">
<h2-input placeholder="测试输入" value="Test"></h2-input>
</h2-mask>
```
## Styling
The following custom properties and mixins are available for styling:
|Custom property | Description | Default|
|----------------|-------------|----------|
|`--h2-mask-label` | Mixin applied to the label of mask | {}
*/
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
import './behaviors/base-behavior.js';
import './h2-input.js';
/**
* @customElement
* @polymer
* @demo demo/h2-mask/index.html
*/
class H2Mask extends Polymer.mixinBehaviors([BaseBehavior], Polymer.Element) {
static get template() {
return Polymer.html`
<style>
:host {
display: flex;
width: 300px;
height: 34px;
line-height: 34px;
}
h2-label {
@apply --h2-mask-label;
}
:host .mask__container {
position: relative;
flex: 1;
line-height: inherit;
height: inherit;
}
:host #mask__viewer:hover {
border-color: #cccccc;
}
:host #mask__viewer:hover .mask__viewer__editor {
display: block;
}
:host #mask__viewer {
position: absolute;
top: -1px;
right: 1px;
bottom: -1px;
left: -1px;
border: 1px solid transparent;
border-radius: 4px;
display: flex;
flex-flow: row nowrap;
align-items: center;
}
:host .mask__viewer__editor {
display: none;
background-color: #eeeeee;
justify-self: flex-end;
padding: 0 4px;
height: inherit;
border-left: 1px solid #ccc;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
cursor: pointer;
}
:host .mask__viewer__container {
flex: 1;
text-align: left;
font-size: inherit;
padding: 4px 8px;
line-height: inherit;
white-space: nowrap;
overflow-x: hidden;
text-overflow: ellipsis;
}
:host #mask__editor:focus {
display: block;
}
:host #mask__editor {
display: none;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: inherit;
line-height: inherit;
}
:host .button-container {
position: absolute;
right: 5px;
padding: 4px;
border: 1px solid #ccc;
border-top: none;
border-radius: 0 0 4px 4px;
background-color: #f0f0f0;
box-shadow: 0 3px 6px rgba(111, 111, 111, 0.2);
z-index: 5;
display: flex;
flex-flow: row nowrap;
}
:host .button-container > button {
background-color: #f0f0f0;
border: 1px solid #cccccc;
cursor: pointer;
border-radius: 4px;
padding: 0;
width: 22px;
height: 22px;
}
:host .button-container > button:hover {
background-color: #ffffff;
border-color: #66afe9;
}
:host iron-icon {
width: 20px;
height: 20px;
}
:host .btn--cancel {
margin-left: 3px;
}
::slotted(*) {
width: inherit;
height: inherit;
line-height: inherit;
font-size: inherit;
}
:host([data-edit-mode]) #mask__viewer {
display: none !important;
}
:host([data-edit-mode]) #mask__editor {
display: block !important;
}
</style>
<h2-label value="[[label]]"></h2-label>
<div class="mask__container">
<div id="mask__editor">
<slot>
<div id="defaultSlot"></div>
</slot>
<div class="button-container">
<button title="提交" on-click="_submit">
<iron-icon icon="icons:check"></iron-icon>
</button>
<button title="取消" class="btn--cancel" on-click="_cancel">
<iron-icon icon="icons:clear"></iron-icon>
</button>
</div>
</div>
<div id="mask__viewer">
<div class="mask__viewer__container">[[_viewValue]]</div>
<div id="editBtn" class="mask__viewer__editor" title="点击开始编辑">
<iron-icon icon="editor:mode-edit"></iron-icon>
</div>
</div>
</div>
`;
}
static get properties() {
return {
/**
* The value of the slotted node.
*/
value: {
type: String,
notify: true
},
/**
* 当前的数据
*/
_valueObj: {
type: Object
},
/**
* 最近一次保存的数据
*/
_lastValueObj: {
type: Object
},
_viewValue: {
type: String
},
/**
* The label of the mask.
*/
label: {
type: String
},
/**
* The placeholder of the mask.
*/
placeholder: {
type: String
},
/**
* Set to true, if the selection is required.
* @type {boolean}
* @default false
*/
required: {
type: Boolean,
value: false
},
/**
* The attribute name of the item to display on mask.
*/
attrForDisplay: {
type: String,
value: 'label'
},
/**
* slot插槽中的组件
*/
_slotNode: {
type: Object
}
};
}
static get is() {
return "h2-mask";
}
static get observers() {
return [];
}
ready() {
super.ready();
const viewField = this.$["mask__viewer"];
const slotNodes = this.root.querySelector("slot")
.assignedNodes()
.filter(n => n.nodeType === Node.ELEMENT_NODE);
this._slotNode = slotNodes[0];
// a hack to get the init value
const valueChangedHandler = (e) => {
this.__initValueOfSlottedElement();
this.__updateViewer();
if (e.type === 'value-changed') {
this._slotNode.removeEventListener('value-changed', valueChangedHandler);
} else if (e.type === 'selected-values-changed') {
this._slotNode.removeEventListener('selected-values-changed', valueChangedHandler);
}
};
this._slotNode.addEventListener('value-changed', valueChangedHandler);
this._slotNode.addEventListener('selected-values-changed', valueChangedHandler);
viewField.addEventListener("click", e => {
e.stopPropagation();
this.__initValueOfSlottedElement();
this._displayEditField(true);
this.isFunction(this._slotNode.doFocus) && this._slotNode.doFocus();
});
/**
* 组件失去焦点的时候关闭组件
*/
this.addEventListener("focusout", e => {
e.stopPropagation();
setTimeout(() => {
// double check the focus is out
if (!this._slotNode.shadowRoot.activeElement) {
this.__getValueOfSlottedElement();
this.__updateViewer();
this._displayEditField(false);
}
}, 100);
});
setTimeout(() => {
this.__initValueOfSlottedElement();
this.__updateViewer();
}, 0);
}
/**
* 显示编辑区域
* @private
*/
_displayEditField(display) {
if (display) {
this.setAttribute('data-edit-mode', '');
} else {
this.removeAttribute('data-edit-mode');
}
}
__initValueOfSlottedElement() {
const {value, selectedValues} = this._slotNode;
this._lastValueObj = {value, selectedValues};
this._valueObj = {value, selectedValues};
this.value = this._slotNode.value;
}
__getValueOfSlottedElement() {
this._lastValueObj = this.deepClone(this._valueObj);
const {value, selectedValues} = this._slotNode;
this._valueObj = {value, selectedValues};
this.value = this._slotNode.value;
}
__resetValueOfSlottedElement() {
this._valueObj = this._lastValueObj;
this._slotNode.set('value', this._valueObj.value);
}
__updateViewer() {
const selectedValues = this._valueObj.selectedValues;
let _viewValue;
if (Array.isArray(selectedValues)) {
_viewValue = selectedValues.map(selected => selected[this.attrForDisplay]).join(', ')
}
this._viewValue = _viewValue || this._valueObj.value || '无';
}
/**
* 提交操作
* @private
*/
_submit(e) {
e.stopPropagation();
this.__getValueOfSlottedElement();
this.__updateViewer();
this._displayEditField(false);
}
/**
* 取消操作
* @private
*/
_cancel(e) {
e.stopPropagation();
this.__resetValueOfSlottedElement();
this.__updateViewer();
this._displayEditField(false);
}
}
window.customElements.define(H2Mask.is, H2Mask);