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 ModifierUtil from '../ons/internal/modifier-util.js';
|
21 | import AnimatorFactory from '../ons/internal/animator-factory.js';
|
22 | import ModalAnimator from './ons-modal/animator.js';
|
23 | import FadeModalAnimator from './ons-modal/fade-animator.js';
|
24 | import LiftModalAnimator from './ons-modal/lift-animator.js';
|
25 | import platform from '../ons/platform.js';
|
26 | import BaseDialogElement from './base/base-dialog.js';
|
27 | import contentReady from '../ons/content-ready.js';
|
28 |
|
29 | const scheme = {
|
30 | '': 'modal--*',
|
31 | 'modal__content': 'modal--*__content'
|
32 | };
|
33 |
|
34 | const defaultClassName = 'modal';
|
35 |
|
36 | const _animatorDict = {
|
37 | 'default': ModalAnimator,
|
38 | 'fade': FadeModalAnimator,
|
39 | 'lift': LiftModalAnimator,
|
40 | 'none': ModalAnimator
|
41 | };
|
42 |
|
43 | /**
|
44 | * @element ons-modal
|
45 | * @category dialog
|
46 | * @description
|
47 | * [en]
|
48 | * Modal component that masks current screen. Underlying components are not subject to any events while the modal component is shown.
|
49 | *
|
50 | * This component can be used to block user input while some operation is running or to show some information to the user.
|
51 | * [/en]
|
52 | * [ja]
|
53 | * 画面全体をマスクするモーダル用コンポーネントです。下側にあるコンポーネントは、
|
54 | * モーダルが表示されている間はイベント通知が行われません。
|
55 | * [/ja]
|
56 | * @seealso ons-dialog
|
57 | * [en]The `<ons-dialog>` component can be used to create a modal dialog.[/en]
|
58 | * [ja][/ja]
|
59 | * @codepen devIg
|
60 | * @tutorial vanilla/reference/modal
|
61 | * @example
|
62 | * <ons-modal id="modal">
|
63 | * Modal content
|
64 | * </ons-modal>
|
65 | * <script>
|
66 | * var modal = document.getElementById('modal');
|
67 | * modal.show();
|
68 | * </script>
|
69 | */
|
70 | export default class ModalElement extends BaseDialogElement {
|
71 |
|
72 | /**
|
73 | * @event preshow
|
74 | * @description
|
75 | * [en]Fired just before the modal is displayed.[/en]
|
76 | * [ja]モーダルが表示される直前に発火します。[/ja]
|
77 | * @param {Object} event [en]Event object.[/en]
|
78 | * @param {Object} event.modal
|
79 | * [en]Component object.[/en]
|
80 | * [ja]コンポーネントのオブジェクト。[/ja]
|
81 | * @param {Function} event.cancel
|
82 | * [en]Execute this function to stop the modal from being shown.[/en]
|
83 | * [ja]この関数を実行すると、ダイアログの表示がキャンセルされます。[/ja]
|
84 | */
|
85 |
|
86 | /**
|
87 | * @event postshow
|
88 | * @description
|
89 | * [en]Fired just after the modal is displayed.[/en]
|
90 | * [ja]モーダルが表示された直後に発火します。[/ja]
|
91 | * @param {Object} event [en]Event object.[/en]
|
92 | * @param {Object} event.modal
|
93 | * [en]Component object.[/en]
|
94 | * [ja]コンポーネントのオブジェクト。[/ja]
|
95 | */
|
96 |
|
97 | /**
|
98 | * @event prehide
|
99 | * @description
|
100 | * [en]Fired just before the modal is hidden.[/en]
|
101 | * [ja]モーダルが隠れる直前に発火します。[/ja]
|
102 | * @param {Object} event [en]Event object.[/en]
|
103 | * @param {Object} event.modal
|
104 | * [en]Component object.[/en]
|
105 | * [ja]コンポーネントのオブジェクト。[/ja]
|
106 | * @param {Function} event.cancel
|
107 | * [en]Execute this function to stop the modal from being hidden.[/en]
|
108 | * [ja]この関数を実行すると、ダイアログの非表示がキャンセルされます。[/ja]
|
109 | */
|
110 |
|
111 | /**
|
112 | * @event posthide
|
113 | * @description
|
114 | * [en]Fired just after the modal is hidden.[/en]
|
115 | * [ja]モーダルが隠れた後に発火します。[/ja]
|
116 | * @param {Object} event [en]Event object.[/en]
|
117 | * @param {Object} event.modal
|
118 | * [en]Component object.[/en]
|
119 | * [ja]コンポーネントのオブジェクト。[/ja]
|
120 | */
|
121 |
|
122 | /**
|
123 | * @attribute animation
|
124 | * @type {String}
|
125 | * @default default
|
126 | * @description
|
127 | * [en]The animation used when showing and hiding the modal. Can be either `"none"`, `"fade"` or `"lift"`.[/en]
|
128 | * [ja]モーダルを表示する際のアニメーション名を指定します。"none"もしくは"fade","lift"を指定できます。[/ja]
|
129 | */
|
130 |
|
131 | /**
|
132 | * @attribute animation-options
|
133 | * @type {Expression}
|
134 | * @description
|
135 | * [en]Specify the animation's duration, timing and delay with an object literal. E.g. `{duration: 0.2, delay: 1, timing: 'ease-in'}`.[/en]
|
136 | * [ja]アニメーション時のduration, timing, delayをオブジェクトリテラルで指定します。e.g. <code>{duration: 0.2, delay: 1, timing: 'ease-in'}</code>[/ja]
|
137 | */
|
138 |
|
139 | /**
|
140 | * @property animationOptions
|
141 | * @type {Object}
|
142 | * @description
|
143 | * [en]Specify the animation's duration, timing and delay with an object literal. E.g. `{duration: 0.2, delay: 1, timing: 'ease-in'}`.[/en]
|
144 | * [ja]アニメーション時のduration, timing, delayをオブジェクトリテラルで指定します。例:{duration: 0.2, delay: 1, timing: 'ease-in'}[/ja]
|
145 | */
|
146 |
|
147 | /**
|
148 | * @attribute visible
|
149 | * @type {Boolean}
|
150 | * @description
|
151 | * [en]Whether the modal is visible or not.[/en]
|
152 | * [ja]要素が見える場合に`true`。[/ja]
|
153 | */
|
154 |
|
155 | constructor() {
|
156 | super();
|
157 |
|
158 | this._defaultDBB = () => undefined;
|
159 | contentReady(this, () => this._compile());
|
160 | }
|
161 |
|
162 | get _scheme() {
|
163 | return scheme;
|
164 | }
|
165 |
|
166 | _updateAnimatorFactory() {
|
167 | return new AnimatorFactory({
|
168 | animators: _animatorDict,
|
169 | baseClass: ModalAnimator,
|
170 | baseClassName: 'ModalAnimator',
|
171 | defaultAnimation: this.getAttribute('animation')
|
172 | });
|
173 | }
|
174 |
|
175 | /**
|
176 | * @property onDeviceBackButton
|
177 | * @type {Object}
|
178 | * @description
|
179 | * [en]Back-button handler.[/en]
|
180 | * [ja]バックボタンハンドラ。[/ja]
|
181 | */
|
182 |
|
183 | _compile() {
|
184 | this.style.display = 'none';
|
185 | this.style.zIndex = 10001;
|
186 | this.classList.add(defaultClassName);
|
187 |
|
188 | if (!util.findChild(this, '.modal__content')) {
|
189 | const content = document.createElement('div');
|
190 | content.classList.add('modal__content');
|
191 |
|
192 | while (this.childNodes[0]) {
|
193 | const node = this.childNodes[0];
|
194 | this.removeChild(node);
|
195 | content.insertBefore(node, null);
|
196 | }
|
197 |
|
198 | this.appendChild(content);
|
199 | }
|
200 |
|
201 | ModifierUtil.initModifier(this, this._scheme);
|
202 | }
|
203 |
|
204 | _toggleStyle(shouldShow) {
|
205 | this.style.display = shouldShow ? 'table' : 'none';
|
206 | }
|
207 |
|
208 | connectedCallback() {
|
209 | super.connectedCallback();
|
210 | }
|
211 |
|
212 | disconnectedCallback() {
|
213 | super.disconnectedCallback();
|
214 | }
|
215 |
|
216 | /**
|
217 | * @property visible
|
218 | * @type {Boolean}
|
219 | * @description
|
220 | * [en]Whether the element is visible or not.[/en]
|
221 | * [ja]要素が見える場合に`true`。[/ja]
|
222 | */
|
223 |
|
224 | /**
|
225 | * @method show
|
226 | * @signature show([options])
|
227 | * @param {Object} [options]
|
228 | * [en]Parameter object.[/en]
|
229 | * [ja]オプションを指定するオブジェクト。[/ja]
|
230 | * @param {String} [options.animation]
|
231 | * [en]Animation name. Available animations are `"none"` and `"fade"`.[/en]
|
232 | * [ja]アニメーション名を指定します。"none", "fade"のいずれかを指定します。[/ja]
|
233 | * @param {String} [options.animationOptions]
|
234 | * [en]Specify the animation's duration, delay and timing. E.g. `{duration: 0.2, delay: 0.4, timing: 'ease-in'}`.[/en]
|
235 | * [ja]アニメーション時のduration, delay, timingを指定します。e.g. {duration: 0.2, delay: 0.4, timing: 'ease-in'}[/ja]
|
236 | * @param {Function} [options.callback]
|
237 | * [en]This function is called after the modal has been revealed.[/en]
|
238 | * [ja]モーダルが表示され終わった後に呼び出される関数オブジェクトを指定します。[/ja]
|
239 | * @description
|
240 | * [en]Show modal.[/en]
|
241 | * [ja]モーダルを表示します。[/ja]
|
242 | * @return {Promise}
|
243 | * [en]Resolves to the displayed element[/en]
|
244 | * [ja][/ja]
|
245 | */
|
246 |
|
247 | /**
|
248 | * @method toggle
|
249 | * @signature toggle([options])
|
250 | * @param {Object} [options]
|
251 | * [en]Parameter object.[/en]
|
252 | * [ja]オプションを指定するオブジェクト。[/ja]
|
253 | * @param {String} [options.animation]
|
254 | * [en]Animation name. Available animations are `"none"` and `"fade"`.[/en]
|
255 | * [ja]アニメーション名を指定します。"none", "fade"のいずれかを指定します。[/ja]
|
256 | * @param {String} [options.animationOptions]
|
257 | * [en]Specify the animation's duration, delay and timing. E.g. `{duration: 0.2, delay: 0.4, timing: 'ease-in'}`.[/en]
|
258 | * [ja]アニメーション時のduration, delay, timingを指定します。e.g. {duration: 0.2, delay: 0.4, timing: 'ease-in'}[/ja]
|
259 | * @param {Function} [options.callback]
|
260 | * [en]This function is called after the modal has been revealed.[/en]
|
261 | * [ja]モーダルが表示され終わった後に呼び出される関数オブジェクトを指定します。[/ja]
|
262 | * @description
|
263 | * [en]Toggle modal visibility.[/en]
|
264 | * [ja]モーダルの表示を切り替えます。[/ja]
|
265 | */
|
266 |
|
267 | /**
|
268 | * @method hide
|
269 | * @signature hide([options])
|
270 | * @param {Object} [options]
|
271 | * [en]Parameter object.[/en]
|
272 | * [ja]オプションを指定するオブジェクト。[/ja]
|
273 | * @param {String} [options.animation]
|
274 | * [en]Animation name. Available animations are `"none"` and `"fade"`.[/en]
|
275 | * [ja]アニメーション名を指定します。"none", "fade"のいずれかを指定します。[/ja]
|
276 | * @param {String} [options.animationOptions]
|
277 | * [en]Specify the animation's duration, delay and timing. E.g. `{duration: 0.2, delay: 0.4, timing: 'ease-in'}`.[/en]
|
278 | * [ja]アニメーション時のduration, delay, timingを指定します。e.g. {duration: 0.2, delay: 0.4, timing: 'ease-in'}[/ja]
|
279 | * @param {Function} [options.callback]
|
280 | * [en]This function is called after the modal has been revealed.[/en]
|
281 | * [ja]モーダルが表示され終わった後に呼び出される関数オブジェクトを指定します。[/ja]
|
282 | * @description
|
283 | * [en]Hide modal.[/en]
|
284 | * [ja]モーダルを非表示にします。[/ja]
|
285 | * @return {Promise}
|
286 | * [en]Resolves to the hidden element[/en]
|
287 | * [ja][/ja]
|
288 | */
|
289 |
|
290 | static get observedAttributes() {
|
291 | return [...super.observedAttributes, 'class'];
|
292 | }
|
293 |
|
294 | attributeChangedCallback(name, last, current) {
|
295 | if (name === 'class') {
|
296 | util.restoreClass(this, defaultClassName, scheme);
|
297 | } else {
|
298 | super.attributeChangedCallback(name, last, current);
|
299 | }
|
300 | }
|
301 |
|
302 | /**
|
303 | * @param {String} name
|
304 | * @param {Function} Animator
|
305 | */
|
306 | static registerAnimator(name, Animator) {
|
307 | if (!(Animator.prototype instanceof ModalAnimator)) {
|
308 | util.throwAnimator('Modal');
|
309 | }
|
310 | _animatorDict[name] = Animator;
|
311 | }
|
312 |
|
313 | static get animators() {
|
314 | return _animatorDict;
|
315 | }
|
316 |
|
317 | static get ModalAnimator() {
|
318 | return ModalAnimator;
|
319 | }
|
320 | }
|
321 |
|
322 | onsElements.Modal = ModalElement;
|
323 | customElements.define('ons-modal', ModalElement);
|