1 | /*
|
2 | Copyright 2013-2015 ASIAL CORPORATION
|
3 |
|
4 | Licensed under the Apache License, Version 2.0 (the "License");
|
5 | you may not use this file except in compliance with the License.
|
6 | You may obtain a copy of the License at
|
7 |
|
8 | http://www.apache.org/licenses/LICENSE-2.0
|
9 |
|
10 | Unless required by applicable law or agreed to in writing, software
|
11 | distributed under the License is distributed on an "AS IS" BASIS,
|
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | See the License for the specific language governing permissions and
|
14 | limitations under the License.
|
15 |
|
16 | */
|
17 |
|
18 | import onsElements from '../ons/elements.js';
|
19 | import util from '../ons/util.js';
|
20 | import autoStyle from '../ons/autostyle.js';
|
21 | import ModifierUtil from '../ons/internal/modifier-util.js';
|
22 | import BaseElement from './base/base-element.js';
|
23 | import contentReady from '../ons/content-ready.js';
|
24 |
|
25 | const scheme = {
|
26 | '': 'select-* select--*',
|
27 | '.select-input': 'select-input--*'
|
28 | };
|
29 |
|
30 | const defaultClassName = 'select';
|
31 |
|
32 | const INPUT_ATTRIBUTES = [
|
33 | 'autofocus',
|
34 | 'disabled',
|
35 | 'form',
|
36 | 'multiple',
|
37 | 'name',
|
38 | 'required',
|
39 | 'size'
|
40 | ];
|
41 |
|
42 | /**
|
43 | * @element ons-select
|
44 | * @category form
|
45 | * @modifier material
|
46 | * [en]Displays a Material Design select input.[/en]
|
47 | * [ja][/ja]
|
48 | * @modifier underbar
|
49 | * [en]Displays a horizontal line underneath a select input.[/en]
|
50 | * [ja][/ja]
|
51 | * @description
|
52 | * [en]
|
53 | * Select component. If you want to place a select with an ID of `my-id` on a page, use `<ons-select select-id="my-id">`.
|
54 | *
|
55 | * The component will automatically display as a Material Design select on Android.
|
56 | *
|
57 | * Most attributes that can be used for a normal `<select>` element can also be used on the `<ons-select>` element.
|
58 | * [/en]
|
59 | * [ja]セレクトボックスを表示するコンポーネントです。`select` 要素に使用できる属性の多くが `ons-select` 要素でも利用できます。[/ja]
|
60 | * @codepen hLayx
|
61 | * @tutorial vanilla/Reference/select
|
62 | * @guide theming.html#modifiers [en]More details about the `modifier` attribute[/en][ja]modifier属性の使い方[/ja]
|
63 | * @guide theming.html#cross-platform-styling-autostyling [en]Information about cross platform styling[/en][ja]Information about cross platform styling[/ja]
|
64 | * @example
|
65 | * <ons-select>
|
66 | * <option value="1">1</option>
|
67 | * <option value="2">2nd</option>
|
68 | * <option value="3">3rd option</option>
|
69 | * </ons-select>
|
70 | */
|
71 |
|
72 | export default class SelectElement extends BaseElement {
|
73 |
|
74 | /**
|
75 | * @attribute autofocus
|
76 | * @type {Boolean}
|
77 | * @default false
|
78 | * @description
|
79 | * [en]Element automatically gains focus on page load.[/en]
|
80 | * [ja]ページロード時にこのセレクトボックスにフォーカスが移るようにします。[/ja]
|
81 | */
|
82 |
|
83 | /**
|
84 | * @attribute disabled
|
85 | * @type {Boolean}
|
86 | * @default false
|
87 | * @description
|
88 | * [en]Specify if select input should be disabled.[/en]
|
89 | * [ja]このセレクトボックスを無効化する場合に指定します。[/ja]
|
90 | */
|
91 |
|
92 | /**
|
93 | * @attribute form
|
94 | * @type {String}
|
95 | * @description
|
96 | * [en]Associate a select element to an existing form on the page, even if not nested.[/en]
|
97 | * [ja]このセレクトボックスを、指定した `form` 要素に紐付けます。セレクトボックスを `form` 要素の外側に配置する際に使用します。[/ja]
|
98 | */
|
99 |
|
100 | /**
|
101 | * @attribute multiple
|
102 | * @type {Boolean}
|
103 | * @default false
|
104 | * @description
|
105 | * [en]If this attribute is defined, multiple options can be selected at once.[/en]
|
106 | * [ja]選択肢の複数選択を有効にします。[/ja]
|
107 | */
|
108 |
|
109 | /**
|
110 | * @attribute name
|
111 | * @type {String}
|
112 | * @description
|
113 | * [en]Name the select element, useful for instance if it is part of a form.[/en]
|
114 | * [ja]このセレクトボックスの名前を指定します。通常 `form` 要素と共に使用します。[/ja]
|
115 | */
|
116 |
|
117 | /**
|
118 | * @attribute required
|
119 | * @type {Boolean}
|
120 | * @description
|
121 | * [en]Make the select input required for submitting the form it is part of.[/en]
|
122 | * [ja]このセレクトボックスを入力必須にする場合に指定します。通常 `form` 要素と共に使用します。[/ja]
|
123 | */
|
124 |
|
125 | /**
|
126 | * @attribute select-id
|
127 | * @type {String}
|
128 | * @description
|
129 | * [en]ID given to the inner select, useful for dynamic manipulation.[/en]
|
130 | * [ja]このセレクトボックスが内部に持つ select 要素に与える ID を指定します。セレクトボックスの内容を動的に変更する必要がある場合に使用します。[/ja]
|
131 | */
|
132 |
|
133 | /**
|
134 | * @attribute size
|
135 | * @type {Number}
|
136 | * @default 1
|
137 | * @description
|
138 | * [en]How many options are displayed; if there are more than the size then a scroll appears to navigate them.[/en]
|
139 | * [ja]一度に表示する選択肢の個数を指定します。選択肢がこの属性で指定した個数よりも多い場合、スクロールが有効になります。[/ja]
|
140 | */
|
141 |
|
142 | constructor() {
|
143 | super();
|
144 |
|
145 | contentReady(this, () => this._compile());
|
146 |
|
147 | this._deriveGetters();
|
148 | }
|
149 |
|
150 | static get observedAttributes() {
|
151 | return ['modifier', 'class', ...INPUT_ATTRIBUTES];
|
152 | }
|
153 |
|
154 | attributeChangedCallback(name, last, current) {
|
155 | switch (name) {
|
156 | case 'class':
|
157 | util.restoreClass(this, defaultClassName, scheme);
|
158 | break;
|
159 | case 'modifier':
|
160 | ModifierUtil.onModifierChanged(last, current, this, scheme);
|
161 | break;
|
162 | }
|
163 |
|
164 | if (INPUT_ATTRIBUTES.indexOf(name) >= 0) {
|
165 | contentReady(this, () => this._updateBoundAttributes());
|
166 | }
|
167 | }
|
168 |
|
169 | get _select() {
|
170 | return this.querySelector('select');
|
171 | }
|
172 |
|
173 | _updateBoundAttributes() {
|
174 | INPUT_ATTRIBUTES.forEach((attr) => {
|
175 | if (this.hasAttribute(attr)) {
|
176 | this._select.setAttribute(attr, this.getAttribute(attr));
|
177 | }
|
178 | else {
|
179 | this._select.removeAttribute(attr);
|
180 | }
|
181 | });
|
182 | }
|
183 |
|
184 | /**
|
185 | * @property length
|
186 | * @description
|
187 | * [en]Number of options in the select box.[/en]
|
188 | * [ja]このセレクトボックスに含まれる選択肢の個数を返します。 `select` 要素[/ja]
|
189 | */
|
190 |
|
191 | /**
|
192 | * @property options
|
193 | * @description
|
194 | * [en]Several options for handling the select DOM object.[/en]
|
195 | * [ja]このセレクトボックスに含まれる `option` 要素の配列を返します。[/ja]
|
196 | */
|
197 |
|
198 | /**
|
199 | * @property selectedIndex
|
200 | * @description
|
201 | * [en]Index of the currently selected option.[/en]
|
202 | * [ja]現在選択されている選択肢のインデックスを返します。[/ja]
|
203 | */
|
204 |
|
205 | /**
|
206 | * @property value
|
207 | * @description
|
208 | * [en]Value of the currently selected option.[/en]
|
209 | * [ja]現在選択されている選択肢の値を返します。[/ja]
|
210 | */
|
211 | _compile() {
|
212 | autoStyle.prepare(this);
|
213 |
|
214 | this.classList.add(defaultClassName);
|
215 | const sel = this._select || document.createElement('select');
|
216 | if (!sel.id && this.hasAttribute('select-id')) {
|
217 | sel.id = this.getAttribute('select-id');
|
218 | }
|
219 | sel.classList.add('select-input');
|
220 | if (!this._select) {
|
221 | util.arrayFrom(this.childNodes).forEach(element => sel.appendChild(element));
|
222 | this.appendChild(sel);
|
223 | }
|
224 |
|
225 | ModifierUtil.initModifier(this, scheme);
|
226 | }
|
227 |
|
228 | _deriveGetters() {
|
229 | ['disabled', 'length', 'multiple', 'name', 'options', 'selectedIndex', 'size', 'value', 'form', 'type']
|
230 | .forEach(key => {
|
231 | Object.defineProperty(this, key, {
|
232 | configurable: true,
|
233 | enumerable: true,
|
234 | get: () => this._select[key],
|
235 | set: ['form', 'type'].indexOf(key) === -1
|
236 | ? value => contentReady(this, () => this._select[key] = value)
|
237 | : undefined
|
238 | });
|
239 | });
|
240 | }
|
241 |
|
242 | add(option, index = null) {
|
243 | this._select.add(option, index);
|
244 | }
|
245 |
|
246 | // If called with an index argument, removes the option element with the given index.
|
247 | // If called with no arguments, removes this.
|
248 | // This behaviour might sound crazy but it is the same as <select>'s `remove` method.
|
249 | // https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/remove
|
250 | remove(index) {
|
251 | if (index === undefined) {
|
252 | Element.prototype.remove.call(this);
|
253 | } else {
|
254 | this._select.remove(index);
|
255 | }
|
256 | }
|
257 | }
|
258 |
|
259 | onsElements.Select = SelectElement;
|
260 | customElements.define('ons-select', SelectElement);
|