|
1 | | -/*! Select.js v0.1.0 | (c) Bunoon | MIT License */ |
| 1 | +/*! Select.js v0.2.0 | (c) Bunoon | MIT License */ |
2 | 2 | (function() { |
3 | 3 | function render() { |
4 | 4 | var tagTypes = _configuration.domElementTypes; |
|
28 | 28 | element.removeAttribute(_attribute_Name_Options); |
29 | 29 | var container = renderContainer(element); |
30 | 30 | var controlElements = renderControl(container, element, bindingOptions); |
31 | | - renderDropDownItems(controlElements.control, controlElements.dropDown, element); |
32 | | - renderSelectedItems(controlElements.control, controlElements.dropDown, element, bindingOptions, false); |
33 | | - buildDocumentEvents(controlElements.dropDown); |
| 31 | + renderDropDownItems(controlElements); |
| 32 | + renderSelectedItems(controlElements, false); |
| 33 | + buildDocumentEvents(controlElements); |
34 | 34 | fireCustomTrigger(bindingOptions.onRenderComplete, element); |
35 | 35 | } |
36 | 36 | } else { |
|
82 | 82 | var dropDown = createElement("div", "drop-down"); |
83 | 83 | dropDown.style.display = "none"; |
84 | 84 | container.appendChild(dropDown); |
85 | | - control.onclick = function(e) { |
86 | | - showDropDownMenu(e, control, dropDown, element, bindingOptions); |
87 | | - }; |
88 | | - return {control:control, dropDown:dropDown}; |
| 85 | + if (!bindingOptions.showDropDownButton) { |
| 86 | + control.onclick = function() { |
| 87 | + showDropDownMenu(control, dropDown, element, bindingOptions); |
| 88 | + }; |
| 89 | + } |
| 90 | + return {control:control, dropDown:dropDown, select:element, bindingOptions:bindingOptions, multiSelectEnabled:element.hasAttribute("multiple")}; |
89 | 91 | } |
90 | | - function renderDropDownItems(control, dropDown, element, bindingOptions) { |
91 | | - var options = element.options; |
| 92 | + function renderControlButton(controlElements) { |
| 93 | + if (controlElements.bindingOptions.showDropDownButton) { |
| 94 | + var dropDownButton = createElement("div", "button"); |
| 95 | + controlElements.control.appendChild(dropDownButton); |
| 96 | + if (isDropDownMenuVisible(controlElements)) { |
| 97 | + dropDownButton.className += _string.space + "button-open"; |
| 98 | + } |
| 99 | + dropDownButton.onclick = function() { |
| 100 | + showDropDownMenu(controlElements); |
| 101 | + }; |
| 102 | + } |
| 103 | + } |
| 104 | + function renderDropDownItems(controlElements) { |
| 105 | + var options = controlElements.select.options; |
92 | 106 | var optionsLength = options.length; |
93 | | - var multiSelectEnabled = element.hasAttribute("multiple"); |
94 | | - dropDown.innerHTML = _string.empty; |
| 107 | + controlElements.dropDown.innerHTML = _string.empty; |
95 | 108 | var optionIndex = 0; |
96 | 109 | for (; optionIndex < optionsLength; optionIndex++) { |
97 | | - renderDropDownItem(control, dropDown, element, optionIndex, multiSelectEnabled, bindingOptions); |
| 110 | + renderDropDownItem(controlElements, optionIndex); |
98 | 111 | } |
99 | 112 | } |
100 | | - function renderDropDownItem(control, dropDown, element, optionIndex, multiSelectEnabled, bindingOptions) { |
| 113 | + function renderDropDownItem(controlElements, optionIndex) { |
101 | 114 | var item = createElement("div", "item"); |
102 | | - var option = element.options[optionIndex]; |
| 115 | + var option = controlElements.select.options[optionIndex]; |
103 | 116 | item.innerHTML = option.text; |
104 | | - dropDown.appendChild(item); |
| 117 | + controlElements.dropDown.appendChild(item); |
105 | 118 | if (option.selected) { |
106 | 119 | item.className += " selected"; |
107 | 120 | } |
108 | 121 | item.onclick = function(e) { |
109 | 122 | cancelBubble(e); |
110 | | - if (!multiSelectEnabled) { |
111 | | - var optionsLength = element.options.length; |
| 123 | + if (!controlElements.multiSelectEnabled) { |
| 124 | + var optionsLength = controlElements.select.options.length; |
112 | 125 | var optionResetIndex = 0; |
113 | 126 | for (; optionResetIndex < optionsLength; optionResetIndex++) { |
114 | | - element.options[optionResetIndex].selected = false; |
| 127 | + controlElements.select.options[optionResetIndex].selected = false; |
115 | 128 | } |
116 | 129 | } |
117 | | - element.options[optionIndex].selected = !element.options[optionIndex].selected; |
118 | | - if (element.options[optionIndex].selected) { |
| 130 | + controlElements.select.options[optionIndex].selected = !controlElements.select.options[optionIndex].selected; |
| 131 | + if (controlElements.select.options[optionIndex].selected) { |
119 | 132 | item.className = "item selected"; |
120 | 133 | } else { |
121 | 134 | item.className = "item"; |
122 | 135 | } |
123 | | - renderSelectedItems(control, dropDown, element, bindingOptions); |
124 | | - if (!multiSelectEnabled) { |
125 | | - hideDropDownMenu(dropDown); |
| 136 | + renderSelectedItems(controlElements); |
| 137 | + if (!controlElements.multiSelectEnabled) { |
| 138 | + hideDropDownMenu(controlElements); |
126 | 139 | } |
127 | 140 | }; |
128 | 141 | } |
129 | | - function renderSelectedItems(control, dropDown, element, bindingOptions, callCustomTrigger) { |
| 142 | + function renderSelectedItems(controlElements, callCustomTrigger) { |
130 | 143 | callCustomTrigger = isDefinedBoolean(callCustomTrigger) ? callCustomTrigger : true; |
131 | | - var options = element.options; |
| 144 | + var options = controlElements.select.options; |
132 | 145 | var optionsLength = options.length; |
133 | 146 | var optionsSelected = false; |
134 | | - var multiSelectEnabled = element.hasAttribute("multiple"); |
135 | | - control.innerHTML = _string.empty; |
| 147 | + controlElements.control.innerHTML = _string.empty; |
| 148 | + renderControlButton(controlElements); |
136 | 149 | var optionIndex = 0; |
137 | 150 | for (; optionIndex < optionsLength; optionIndex++) { |
138 | 151 | var option = options[optionIndex]; |
139 | 152 | if (option.selected) { |
140 | 153 | optionsSelected = true; |
141 | | - renderSelectedItem(control, dropDown, element, optionIndex, multiSelectEnabled, bindingOptions); |
| 154 | + renderSelectedItem(controlElements, optionIndex); |
142 | 155 | } |
143 | 156 | } |
144 | 157 | if (!optionsSelected) { |
145 | 158 | var noItemsSelected = createElement("div", "no-items-selected"); |
146 | | - noItemsSelected.innerHTML = "There are no items selected"; |
147 | | - control.appendChild(noItemsSelected); |
| 159 | + noItemsSelected.innerHTML = controlElements.bindingOptions.noItemsSelectedText; |
| 160 | + controlElements.control.appendChild(noItemsSelected); |
148 | 161 | } |
149 | 162 | if (callCustomTrigger) { |
150 | | - fireCustomTrigger(bindingOptions.onSelectedItemsChanged); |
| 163 | + fireCustomTrigger(controlElements.bindingOptions.onSelectedItemsChanged, getValuesSelected(controlElements)); |
151 | 164 | } |
152 | 165 | } |
153 | | - function renderSelectedItem(control, dropDown, element, optionIndex, multiSelectEnabled, bindingOptions) { |
| 166 | + function renderSelectedItem(controlElements, optionIndex) { |
154 | 167 | var selectedItem = createElement("div", "selected-item"); |
155 | | - control.appendChild(selectedItem); |
| 168 | + controlElements.control.appendChild(selectedItem); |
156 | 169 | var selectedItemText = createElement("span", "text"); |
157 | | - selectedItemText.innerHTML = element.options[optionIndex].text; |
| 170 | + selectedItemText.innerHTML = controlElements.select.options[optionIndex].text; |
158 | 171 | selectedItem.appendChild(selectedItemText); |
159 | | - if (multiSelectEnabled) { |
| 172 | + if (controlElements.multiSelectEnabled) { |
160 | 173 | var removeButton = createElement("div", "remove"); |
161 | | - removeButton.innerHTML = bindingOptions.removeText; |
| 174 | + removeButton.innerHTML = controlElements.bindingOptions.removeText; |
162 | 175 | selectedItem.appendChild(removeButton); |
163 | 176 | removeButton.onclick = function(e) { |
164 | 177 | cancelBubble(e); |
165 | | - element.options[optionIndex].selected = false; |
166 | | - hideDropDownMenu(dropDown); |
167 | | - renderSelectedItems(control, dropDown, element, bindingOptions); |
| 178 | + controlElements.select.options[optionIndex].selected = false; |
| 179 | + hideDropDownMenu(controlElements); |
| 180 | + renderSelectedItems(controlElements); |
168 | 181 | }; |
169 | 182 | } |
170 | 183 | } |
171 | | - function buildDocumentEvents(dropDown) { |
| 184 | + function buildDocumentEvents(controlElements) { |
172 | 185 | var hideMenu = function() { |
173 | | - hideDropDownMenu(dropDown); |
| 186 | + hideDropDownMenu(controlElements); |
174 | 187 | }; |
175 | 188 | _parameter_Document.body.addEventListener("click", hideMenu); |
176 | 189 | _parameter_Window.addEventListener("resize", hideMenu); |
177 | 190 | _parameter_Window.addEventListener("click", hideMenu); |
178 | 191 | } |
179 | | - function showDropDownMenu(e, control, dropDown, element, bindingOptions) { |
180 | | - setTimeout(function() { |
181 | | - if (dropDown !== null && dropDown.style.display !== "block") { |
182 | | - dropDown.style.display = "block"; |
183 | | - renderDropDownItems(control, dropDown, element, bindingOptions); |
184 | | - } else { |
185 | | - hideDropDownMenu(dropDown); |
186 | | - } |
187 | | - }, 50); |
| 192 | + function showDropDownMenu(controlElements) { |
| 193 | + if (!isDropDownMenuVisible(controlElements)) { |
| 194 | + setTimeout(function() { |
| 195 | + controlElements.dropDown.style.display = "block"; |
| 196 | + renderDropDownItems(controlElements); |
| 197 | + renderSelectedItems(controlElements, false); |
| 198 | + }, controlElements.bindingOptions.dropDownShowDelay); |
| 199 | + } else { |
| 200 | + hideDropDownMenu(controlElements); |
| 201 | + } |
188 | 202 | } |
189 | | - function hideDropDownMenu(dropDown) { |
190 | | - if (dropDown !== null && dropDown.style.display !== "none") { |
191 | | - dropDown.style.display = "none"; |
| 203 | + function hideDropDownMenu(controlElements) { |
| 204 | + if (controlElements.dropDown !== null && controlElements.dropDown.style.display !== "none") { |
| 205 | + controlElements.dropDown.style.display = "none"; |
| 206 | + renderSelectedItems(controlElements, false); |
192 | 207 | } |
193 | 208 | } |
| 209 | + function isDropDownMenuVisible(controlElements) { |
| 210 | + return controlElements.dropDown !== null && controlElements.dropDown.style.display === "block"; |
| 211 | + } |
| 212 | + function getValuesSelected(controlElements) { |
| 213 | + var options = controlElements.select.options; |
| 214 | + var optionsLength = options.length; |
| 215 | + var optionValuesSelected = []; |
| 216 | + var optionIndex = 0; |
| 217 | + for (; optionIndex < optionsLength; optionIndex++) { |
| 218 | + var option = options[optionIndex]; |
| 219 | + if (option.selected) { |
| 220 | + optionValuesSelected.push(option.value); |
| 221 | + } |
| 222 | + } |
| 223 | + return optionValuesSelected; |
| 224 | + } |
194 | 225 | function buildAttributeOptions(newOptions) { |
195 | 226 | var options = !isDefinedObject(newOptions) ? {} : newOptions; |
196 | 227 | options.render = getDefaultBoolean(options.render, true); |
| 228 | + options.dropDownShowDelay = getDefaultNumber(options.dropDownShowDelay, 50); |
| 229 | + options.showDropDownButton = getDefaultBoolean(options.showDropDownButton, true); |
197 | 230 | options = buildAttributeOptionStrings(options); |
198 | 231 | return buildAttributeOptionCustomTriggers(options); |
199 | 232 | } |
200 | 233 | function buildAttributeOptionStrings(options) { |
201 | 234 | options.removeText = getDefaultString(options.removeText, "X"); |
| 235 | + options.noItemsSelectedText = getDefaultString(options.noItemsSelectedText, "There are no items selected"); |
202 | 236 | return options; |
203 | 237 | } |
204 | 238 | function buildAttributeOptionCustomTriggers(options) { |
|
221 | 255 | function isDefinedFunction(object) { |
222 | 256 | return isDefined(object) && typeof object === "function"; |
223 | 257 | } |
| 258 | + function isDefinedNumber(object) { |
| 259 | + return isDefined(object) && typeof object === "number"; |
| 260 | + } |
224 | 261 | function isDefinedArray(object) { |
225 | 262 | return isDefinedObject(object) && object instanceof Array; |
226 | 263 | } |
|
260 | 297 | function getDefaultArray(value, defaultValue) { |
261 | 298 | return isDefinedArray(value) ? value : defaultValue; |
262 | 299 | } |
| 300 | + function getDefaultNumber(value, defaultValue) { |
| 301 | + return isDefinedNumber(value) ? value : defaultValue; |
| 302 | + } |
263 | 303 | function getDefaultStringOrArray(value, defaultValue) { |
264 | 304 | if (isDefinedString(value)) { |
265 | 305 | value = value.split(_string.space); |
|
310 | 350 | return this; |
311 | 351 | }; |
312 | 352 | this.getVersion = function() { |
313 | | - return "0.1.0"; |
| 353 | + return "0.2.0"; |
314 | 354 | }; |
315 | 355 | (function(documentObject, windowObject) { |
316 | 356 | _parameter_Document = documentObject; |
|
0 commit comments