/
ons-speed-dial.js
executable file
·551 lines (500 loc) · 14.9 KB
/
ons-speed-dial.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import _setImmediate from 'babel-runtime/core-js/set-immediate';
import _Promise from 'babel-runtime/core-js/promise';
import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
/*
Copyright 2013-2015 ASIAL CORPORATION
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import onsElements from '../ons/elements';
import util from '../ons/util';
import autoStyle from '../ons/autostyle';
import ModifierUtil from '../ons/internal/modifier-util';
import BaseElement from './base/base-element';
import contentReady from '../ons/content-ready';
import styler from '../ons/styler';
var defaultClassName = 'speed-dial';
var scheme = {
'': 'speed-dial--*'
};
/**
* @element ons-speed-dial
* @category control
* @description
* [en]
* Element that displays a Material Design Speed Dialog component. It is useful when there are more than one primary action that can be performed in a page.
*
* The Speed dial looks like a `<ons-fab>` element but will expand a menu when tapped.
* [/en]
* [ja][/ja]
* @codepen dYQYLg
* @tutorial vanilla/Reference/speed-dial
* @seealso ons-speed-dial-item
* [en]The `<ons-speed-dial-item>` represents a menu item.[/en]
* [ja]ons-speed-dial-itemコンポーネント[/ja]
* @seealso ons-fab
* [en]ons-fab component[/en]
* [ja]ons-fabコンポーネント[/ja]
* @example
* <ons-speed-dial position="left bottom">
* <ons-fab>
* <ons-icon icon="fa-twitter"></ons-icon>
* </ons-fab>
* <ons-speed-dial-item>A</ons-speed-dial-item>
* <ons-speed-dial-item>B</ons-speed-dial-item>
* <ons-speed-dial-item>C</ons-speed-dial-item>
* </ons-speed-dial>
*/
var SpeedDialElement = function (_BaseElement) {
_inherits(SpeedDialElement, _BaseElement);
/**
* @event open
* @description
* [en]Fired when the menu items are shown.[/en]
* [ja][/ja]
*/
/**
* @event close
* @description
* [en]Fired when the menu items are hidden.[/en]
* [ja][/ja]
*/
/**
* @attribute modifier
* @type {String}
* @description
* [en]The appearance of the component.[/en]
* [ja]このコンポーネントの表現を指定します。[/ja]
*/
/**
* @attribute ripple
* @description
* [en]If this attribute is defined, the button will have a ripple effect when tapped.[/en]
* [ja][/ja]
*/
/**
* @attribute position
* @type {String}
* @description
* [en]
* Specify the vertical and horizontal position of the component.
* I.e. to display it in the top right corner specify "right top".
* Choose from "right", "left", "top" and "bottom".
* [/en]
* [ja]
* この要素を表示する左右と上下の位置を指定します。
* 例えば、右上に表示する場合には"right top"を指定します。
* 左右と上下の位置の指定には、rightとleft、topとbottomがそれぞれ指定できます。
* [/ja]
*/
/**
* @attribute direction
* @type {String}
* @description
* [en]Specify the direction the items are displayed. Possible values are "up", "down", "left" and "right".[/en]
* [ja]
* 要素が表示する方向を指定します。up, down, left, rightが指定できます。
* [/ja]
*/
/**
* @attribute disabled
* @description
* [en]Specify if button should be disabled.[/en]
* [ja]無効化する場合に指定します。[/ja]
*/
function SpeedDialElement() {
_classCallCheck(this, SpeedDialElement);
var _this = _possibleConstructorReturn(this, (SpeedDialElement.__proto__ || _Object$getPrototypeOf(SpeedDialElement)).call(this));
contentReady(_this, function () {
_this._compile();
});
_this._itemShown = false;
_this._boundOnClick = _this._onClick.bind(_this);
return _this;
}
_createClass(SpeedDialElement, [{
key: '_compile',
value: function _compile() {
this.classList.add(defaultClassName);
autoStyle.prepare(this);
this._updateRipple();
ModifierUtil.initModifier(this, scheme);
if (this.hasAttribute('direction')) {
this._updateDirection(this.getAttribute('direction'));
} else {
this._updateDirection('up');
}
this._updatePosition();
}
}, {
key: 'attributeChangedCallback',
value: function attributeChangedCallback(name, last, current) {
var _this2 = this;
switch (name) {
case 'class':
util.restoreClass(this, defaultClassName, scheme);
break;
case 'modifier':
ModifierUtil.onModifierChanged(last, current, this, scheme);
break;
case 'ripple':
contentReady(this, function () {
return _this2._updateRipple();
});
break;
case 'direction':
contentReady(this, function () {
return _this2._updateDirection(current);
});
break;
case 'position':
contentReady(this, function () {
return _this2._updatePosition();
});
break;
}
}
}, {
key: 'connectedCallback',
value: function connectedCallback() {
this.addEventListener('click', this._boundOnClick, false);
}
}, {
key: 'disconnectedCallback',
value: function disconnectedCallback() {
this.removeEventListener('click', this._boundOnClick, false);
}
}, {
key: '_onClick',
value: function _onClick(e) {
if (this.onClick) {
this.onClick.apply(this);
return _Promise.resolve();
} else if (!this.disabled && this.visible) {
return this.toggleItems();
}
}
}, {
key: '_show',
value: function _show() {
if (!this.inline) {
return this.show();
}
return _Promise.resolve();
}
}, {
key: '_hide',
value: function _hide() {
var _this3 = this;
return new _Promise(function (resolve) {
if (!_this3.inline) {
_setImmediate(function () {
return _this3.hide().then(resolve);
});
} else {
resolve();
}
});
}
}, {
key: '_updateRipple',
value: function _updateRipple() {
if (this._fab) {
this.hasAttribute('ripple') ? this._fab.setAttribute('ripple', '') : this._fab.removeAttribute('ripple');
}
}
}, {
key: '_updateDirection',
value: function _updateDirection(direction) {
var children = this.items;
for (var i = 0; i < children.length; i++) {
styler(children[i], {
transitionDelay: 25 * i + 'ms',
bottom: 'auto',
right: 'auto',
top: 'auto',
left: 'auto'
});
}
switch (direction) {
case 'up':
for (var _i = 0; _i < children.length; _i++) {
children[_i].style.bottom = 72 + 56 * _i + 'px';
children[_i].style.right = '8px';
}
break;
case 'down':
for (var _i2 = 0; _i2 < children.length; _i2++) {
children[_i2].style.top = 72 + 56 * _i2 + 'px';
children[_i2].style.left = '8px';
}
break;
case 'left':
for (var _i3 = 0; _i3 < children.length; _i3++) {
children[_i3].style.top = '8px';
children[_i3].style.right = 72 + 56 * _i3 + 'px';
}
break;
case 'right':
for (var _i4 = 0; _i4 < children.length; _i4++) {
children[_i4].style.top = '8px';
children[_i4].style.left = 72 + 56 * _i4 + 'px';
}
break;
default:
util.throw('Argument must be one of up, down, left or right.');
}
}
}, {
key: '_updatePosition',
value: function _updatePosition() {
var position = this.getAttribute('position');
this.classList.remove('fab--top__left', 'fab--bottom__right', 'fab--bottom__left', 'fab--top__right', 'fab--top__center', 'fab--bottom__center');
switch (position) {
case 'top right':
case 'right top':
this.classList.add('fab--top__right');
break;
case 'top left':
case 'left top':
this.classList.add('fab--top__left');
break;
case 'bottom right':
case 'right bottom':
this.classList.add('fab--bottom__right');
break;
case 'bottom left':
case 'left bottom':
this.classList.add('fab--bottom__left');
break;
case 'center top':
case 'top center':
this.classList.add('fab--top__center');
break;
case 'center bottom':
case 'bottom center':
this.classList.add('fab--bottom__center');
break;
default:
break;
}
}
}, {
key: '_getTranslate',
value: function _getTranslate() {
var isBottom = (this.getAttribute('position') || '').indexOf('bottom') >= 0;
var translate = isBottom ? 'translate3d(0px, -' + (util.globals.fabOffset || 0) + 'px, 0px) ' : '';
return translate;
}
/**
* @method show
* @signature show()
* @description
* [en]Show the speed dial.[/en]
* [ja]Speed dialを表示します。[/ja]
*/
}, {
key: 'show',
value: function show() {
this._fab.show();
styler(this, { transform: this._getTranslate });
return _Promise.resolve();
}
/**
* @method hide
* @signature hide()
* @description
* [en]Hide the speed dial.[/en]
* [ja]Speed dialを非表示にします。[/ja]
*/
}, {
key: 'hide',
value: function hide() {
var _this4 = this;
return this.hideItems().then(function () {
return _this4._fab.hide();
});
}
/**
* @method showItems
* @signature showItems()
* @description
* [en]Show the speed dial items.[/en]
* [ja]Speed dialの子要素を表示します。[/ja]
*/
}, {
key: 'showItems',
value: function showItems() {
if (this.hasAttribute('direction')) {
this._updateDirection(this.getAttribute('direction'));
} else {
this._updateDirection('up');
}
var totalDelay = 0;
if (!this._itemShown) {
var children = this.items;
for (var i = 0; i < children.length; i++) {
var delay = 25 * i;
totalDelay += delay;
styler(children[i], {
transform: 'scale(1)',
transitionDelay: delay + 'ms'
});
}
totalDelay += 50;
this._itemShown = true;
util.triggerElementEvent(this, 'open');
}
var deferred = util.defer();
setTimeout(deferred.resolve, totalDelay);
return deferred.promise;
}
/**
* @method hideItems
* @signature hideItems()
* @description
* [en]Hide the speed dial items.[/en]
* [ja]Speed dialの子要素を非表示にします。[/ja]
*/
}, {
key: 'hideItems',
value: function hideItems() {
var totalDelay = 0;
if (this._itemShown) {
var children = this.items;
for (var i = 0; i < children.length; i++) {
var delay = 25 * (children.length - i);
totalDelay += delay;
styler(children[i], {
transform: 'scale(0)',
transitionDelay: delay + 'ms'
});
}
totalDelay += 50;
this._itemShown = false;
util.triggerElementEvent(this, 'close');
}
var deferred = util.defer();
setTimeout(deferred.resolve, totalDelay);
return deferred.promise;
}
/**
* @property disabled
* @type {Boolean}
* @description
* [en]Whether the element is disabled or not.[/en]
* [ja]無効化されている場合に`true`。[/ja]
*/
}, {
key: 'isOpen',
/**
* @method isOpen
* @signature isOpen()
* @description
* [en]Returns whether the menu is open or not.[/en]
* [ja][/ja]
*/
value: function isOpen() {
return this._itemShown;
}
/**
* @method toggle
* @signature toggle()
* @description
* [en]Toggle visibility.[/en]
* [ja]Speed dialの表示非表示を切り替えます。[/ja]
*/
}, {
key: 'toggle',
value: function toggle() {
return this.visible ? this.hide() : this.show();
}
/**
* @method toggleItems
* @signature toggleItems()
* @description
* [en]Toggle item visibility.[/en]
* [ja]Speed dialの子要素の表示非表示を切り替えます。[/ja]
*/
}, {
key: 'toggleItems',
value: function toggleItems() {
return this.isOpen() ? this.hideItems() : this.showItems();
}
}, {
key: 'items',
get: function get() {
return util.arrayFrom(this.querySelectorAll('ons-speed-dial-item'));
}
}, {
key: '_fab',
get: function get() {
return util.findChild(this, 'ons-fab');
}
}, {
key: 'disabled',
set: function set(value) {
if (value) {
this.hideItems();
}
util.arrayFrom(this.children).forEach(function (e) {
util.match(e, '.fab') && util.toggleAttribute(e, 'disabled', value);
});
return util.toggleAttribute(this, 'disabled', value);
},
get: function get() {
return this.hasAttribute('disabled');
}
/**
* @property inline
* @readonly
* @type {Boolean}
* @description
* [en]Whether the element is inline or not.[/en]
* [ja]インライン要素の場合に`true`。[/ja]
*/
}, {
key: 'inline',
get: function get() {
return this.hasAttribute('inline');
}
/**
* @property visible
* @readonly
* @type {Boolean}
* @description
* [en]Whether the element is visible or not.[/en]
* [ja]要素が見える場合に`true`。[/ja]
*/
}, {
key: 'visible',
get: function get() {
return this._fab.visible && this.style.display !== 'none';
}
}], [{
key: 'observedAttributes',
get: function get() {
return ['class', 'modifier', 'ripple', 'direction', 'position'];
}
}, {
key: 'events',
get: function get() {
return ['open', 'close'];
}
}]);
return SpeedDialElement;
}(BaseElement);
export default SpeedDialElement;
onsElements.SpeedDial = SpeedDialElement;
customElements.define('ons-speed-dial', SpeedDialElement);