-
Notifications
You must be signed in to change notification settings - Fork 0
/
juicy-select.html
119 lines (110 loc) · 4.19 KB
/
juicy-select.html
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
<!--
`juicy-select element` - Polymer select element with multiple option support.
@element juicy-select
version: 1.1.3
-->
<link rel="import" href="../polymer/polymer.html">
<dom-module id="juicy-select">
<template>
<style>
:host {
display: inline-flex;
}
:host > ::slotted(select) {
width: 100%;
}
</style>
<slot></slot>
</template>
<script>
customElements.define('juicy-select', class extends Polymer.Element {
static get is() { return 'juicy-select' }
static get properties() {
return {
options: { type: Array, value: [], notify: true },
multiple: { type: Boolean, value: false },
value: { type: String, notify: true, observer: "valueChanged" },
selectedProperty: { type: String, observer: "optionsChanged" },
textProperty: { type: String, observer: "optionsChanged" },
valueProperty: { type: String, observer: "optionsChanged" },
captionText: { type: String, observer: "optionsChanged" }
};
}
static get observers() {
return ['optionsChanged(options.*)'];
}
constructor() {
super();
this.selectChange = this.selectChange.bind(this);
}
connectedCallback() {
super.connectedCallback();
this.$select = this.querySelector("select");
if (!this.$select) {
this.$select = document.createElement("select");
this.appendChild(this.$select);
if (this.multiple) {
this.$select.setAttribute("multiple", "multiple");
}
}
else {
this.set("multiple", this.$select.hasAttribute("multiple"));
}
if (this.$select) {
this.generateOptions();
}
this.addEventListener("change", this.selectChange);
}
disconnectedCallback() {
super.disconnectedCallback();
this.$select.removeEventListener('change', this.selectChange);
}
optionsChanged() {
if (!this.$select) {
return;
}
this.generateOptions();
}
generateOptions() {
if (!this.multiple && this.captionText) {
this.$select.innerHTML = "<option value=''>" + this.captionText + "</option>";
} else {
this.$select.innerHTML = "";
}
if (!this.options || !this.options.length) {
return;
}
for (var i = 0; i < this.options.length; i++) {
var row = this.options[i];
var opt = document.createElement("option");
opt.innerText = row[this.textProperty];
opt.value = row[this.valueProperty];
if (this.multiple) {
opt.selected = row[this.selectedProperty];
} else {
opt.selected = this.value == opt.value;
}
this.$select.appendChild(opt);
}
}
selectChange() {
if (!this.$select) {
return
}
if (this.multiple) {
var options = this.$select.querySelectorAll("option");
for (var i = 0; i < options.length; i++) {
this.set("options." + i + "." + this.selectedProperty, options[i].selected);
}
} else {
this.set("value", this.$select.value);
}
}
valueChanged(newVal, oldVal) {
if (this.$select) {
this.$select.value = newVal;
}
}
});
</script>
</dom-module>