-
Notifications
You must be signed in to change notification settings - Fork 3
/
tangy-list.js
126 lines (114 loc) · 3.39 KB
/
tangy-list.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
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
import 'dr-niels-sortable-list/sortable-list.js'
import './util/html-element-props.js'
import '@polymer/paper-checkbox/paper-checkbox.js'
import '@polymer/paper-button/paper-button.js'
import '@polymer/iron-icon/iron-icon.js'
import '@polymer/iron-icons/iron-icons.js'
import './style/tangy-common-styles.js'
import './style/tangy-element-styles.js'
import './tangy-list-item.js'
// https://stackoverflow.com/a/2117523/10139471
function uuid() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
)
}
export class TangyList extends PolymerElement {
static get is () {
return 'tangy-list'
}
static get _props() {
return ['name','value','label','disabled','invalid','incomplete','hidden']
}
static get properties () {
return {
name: {
type: String,
value: '',
reflectToAttribute: true
},
maxCount: {
type: Number,
value: 999,
reflectToAttribute: true
},
initialCount: {
type: Number,
value: 1,
reflectToAttribute: true
},
label: {
type: String,
value: '',
reflectToAttribute: true
},
disabled: {
type: Boolean,
value: false,
reflectToAttribute: true
},
invalid: {
type: Boolean,
value: false,
reflectToAttribute: true
},
incomplete: {
type: Boolean,
value: true,
reflectToAttribute: true
},
hidden: {
type: Boolean,
value: false,
reflectToAttribute: true
}
}
}
static get template () {
return html`
<style include="tangy-common-styles"></style>
<style include="tangy-element-styles"></style>
<style>
tangy-list-item {
width: 100%
}
</style>
<sortable-list id="items" style="width:100%">
</sortable-list>
<paper-button on-click="onClickNewItem" style="margin-left: 15px; background: var(--accent-color); color: var(--accent-text-color);" raised class="add-another"><iron-icon icon="add-circle"></iron-icon>ADD ANOTHER</paper-button>
`
}
set value(value) {
this.$.items.innerHTML = ``
value.forEach(itemValue => this.addItem(itemValue))
}
get value() {
return [...this.shadowRoot.querySelectorAll('tangy-list-item')].map(itemEl => itemEl.value)
}
connectedCallback () {
super.connectedCallback()
if (this.querySelector(`template[type="tangy-list/initial-items"]`)) {
this.$.items.innerHTML = this.querySelector(`template[type="tangy-list/initial-items"]`).innerHTML
} else {
for (let i = 0; i < this.initialCount; i++ ) {
this.addItem()
}
}
}
onClickNewItem(event) {
this.addItem()
}
addItem(itemValue) {
const itemEl = document.createElement('tangy-list-item')
itemEl.innerHTML = this.querySelector(`template[type="tangy-list/new-item"]`).innerHTML
if (itemValue) itemEl.value = itemValue
this.$.items.appendChild(itemEl)
}
validate() {
// If one itemEl.validate() returns false, return false.
return [...this.$.items.querySelectorAll('tangy-list-item')]
.reduce((isValid, itemEl) => itemEl.validate() ? isValid : false, true)
}
}
window.customElements.define(TangyList.is, TangyList)