1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
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 defaultClassName = 'segment';
|
26 | const scheme = {
|
27 | '': 'segment--*',
|
28 | '.segment__item': 'segment--*__item',
|
29 | '.segment__input': 'segment--*__input',
|
30 | '.segment__button': 'segment--*__button'
|
31 | };
|
32 |
|
33 | const generateId = (() => {
|
34 | let i = 0;
|
35 | return () => 'ons-segment-gen-' + (i++);
|
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 | export default class SegmentElement extends BaseElement {
|
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 | constructor() {
|
125 | super();
|
126 |
|
127 | this._segmentId = generateId();
|
128 | this._tabbar = null;
|
129 | this._onChange = this._onChange.bind(this);
|
130 | this._onTabbarPreChange = this._onTabbarPreChange.bind(this);
|
131 |
|
132 | contentReady(this, () => {
|
133 | this._compile();
|
134 | setImmediate(() => this._lastActiveIndex = this._tabbar ? this._tabbar.getActiveTabIndex() : this.getActiveButtonIndex());
|
135 | });
|
136 | }
|
137 |
|
138 | _compile() {
|
139 | autoStyle.prepare(this);
|
140 | this.classList.add(defaultClassName);
|
141 |
|
142 | for (let index = this.children.length - 1; index >= 0; index--) {
|
143 | const item = this.children[index];
|
144 | item.classList.add('segment__item');
|
145 |
|
146 | const input = util.findChild(item, '.segment__input') || util.create('input.segment__input');
|
147 | input.type = 'radio';
|
148 | input.value = index;
|
149 | input.name = input.name || this._segmentId;
|
150 | input.checked = !this.hasAttribute('tabbar-id') && index === (this.activeIndex || 0);
|
151 |
|
152 | const button = util.findChild(item, '.segment__button') || util.create('.segment__button');
|
153 | if (button.parentElement !== item) {
|
154 | while (item.firstChild) {
|
155 | button.appendChild(item.firstChild);
|
156 | }
|
157 | }
|
158 |
|
159 | item.appendChild(input);
|
160 | item.appendChild(button);
|
161 | }
|
162 |
|
163 | ModifierUtil.initModifier(this, scheme);
|
164 | }
|
165 |
|
166 | connectedCallback() {
|
167 | contentReady(this, () => {
|
168 | if (this.hasAttribute('tabbar-id')) {
|
169 | const page = util.findParent(this, 'ons-page');
|
170 | this._tabbar = page && page.querySelector('#' + this.getAttribute('tabbar-id'));
|
171 | if (!this._tabbar || this._tabbar.tagName !== 'ONS-TABBAR') {
|
172 | util.throw(`No tabbar with id ${this.getAttribute('tabbar-id')} was found.`);
|
173 | }
|
174 |
|
175 | this._tabbar.setAttribute('hide-tabs', '');
|
176 | setImmediate(() => {
|
177 | const index = this._tabbar.getActiveTabIndex();
|
178 | this._setChecked(index);
|
179 | this.activeIndex = index;
|
180 | });
|
181 |
|
182 | this._tabbar.addEventListener('prechange', this._onTabbarPreChange);
|
183 | }
|
184 | });
|
185 |
|
186 | this.addEventListener('change', this._onChange);
|
187 | }
|
188 |
|
189 | disconnectedCallback() {
|
190 | contentReady(this, () => {
|
191 | if (this._tabbar) {
|
192 | this._tabbar.removeEventListener('prechange', this._onTabbarPreChange);
|
193 | this._tabbar = null;
|
194 | }
|
195 | });
|
196 | this.removeEventListener('change', this._onChange);
|
197 | }
|
198 |
|
199 | _setChecked(index) {
|
200 | this.children[index].firstElementChild.checked = true;
|
201 | }
|
202 |
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | setActiveButton(index, options) {
|
220 | if (this._tabbar) {
|
221 | return this._tabbar.setActiveTab(index, options);
|
222 | }
|
223 |
|
224 | this._setChecked(index);
|
225 | this._postChange(index);
|
226 | return Promise.resolve(index);
|
227 | }
|
228 |
|
229 | |
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | getActiveButtonIndex() {
|
240 | for (let i = this.children.length - 1; i >= 0; i--) {
|
241 | if (this.children[i].firstElementChild.checked) {
|
242 | return i;
|
243 | }
|
244 | }
|
245 | return -1;
|
246 | }
|
247 |
|
248 | _onChange(event) {
|
249 | event.stopPropagation();
|
250 | this._tabbar
|
251 | ? this._tabbar.setActiveTab(this.getActiveButtonIndex(), { reject: false })
|
252 | : this._postChange(this.getActiveButtonIndex());
|
253 | }
|
254 |
|
255 | _onTabbarPreChange(event) {
|
256 | setImmediate(() => {
|
257 | if (!event.detail.canceled) {
|
258 | this._setChecked(event.index);
|
259 | this._postChange(event.index);
|
260 | }
|
261 | });
|
262 | }
|
263 |
|
264 | _postChange(index) {
|
265 | util.triggerElementEvent(this, 'postchange', {
|
266 | index,
|
267 | activeIndex: index,
|
268 | lastActiveIndex: this._lastActiveIndex,
|
269 | segmentItem: this.children[index]
|
270 | });
|
271 | this._lastActiveIndex = index;
|
272 | this.activeIndex = index;
|
273 | }
|
274 |
|
275 | |
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | get activeIndex() {
|
284 | return parseInt(this.getAttribute('active-index'));
|
285 | }
|
286 |
|
287 | set activeIndex(value) {
|
288 | if (value !== null && value !== undefined) {
|
289 | this.setAttribute('active-index', value);
|
290 | }
|
291 | }
|
292 |
|
293 | static get observedAttributes() {
|
294 | return ['class', 'modifier', 'active-index'];
|
295 | }
|
296 |
|
297 | attributeChangedCallback(name, last, current) {
|
298 | switch (name) {
|
299 | case 'class':
|
300 | util.restoreClass(this, defaultClassName, scheme);
|
301 | break;
|
302 | case 'modifier':
|
303 | ModifierUtil.onModifierChanged(last, current, this, scheme);
|
304 | break;
|
305 | case 'active-index':
|
306 | contentReady(this, () => {
|
307 | if (this.getActiveButtonIndex() !== this.activeIndex) {
|
308 | this.setActiveButton(this.activeIndex);
|
309 | }
|
310 | });
|
311 | break;
|
312 | }
|
313 | }
|
314 |
|
315 | static get events() {
|
316 | return ['postchange'];
|
317 | }
|
318 | }
|
319 |
|
320 | util.defineBooleanProperties(SegmentElement, ['disabled']);
|
321 |
|
322 | onsElements.Segment = SegmentElement;
|
323 | customElements.define('ons-segment', SegmentElement);
|