UNPKG

2.9 kBJavaScriptView Raw
1/*
2Copyright 2013-2015 ASIAL CORPORATION
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16*/
17import util from '../../ons/util.js';
18import animit from '../../ons/animit.js';
19import BaseAnimator from '../../ons/base-animator.js';
20
21export class PopoverAnimator extends BaseAnimator {
22
23 /**
24 * @param {Object} options
25 * @param {String} options.timing
26 * @param {Number} options.duration
27 * @param {Number} options.delay
28 */
29 constructor({timing = 'cubic-bezier(.1, .7, .4, 1)', delay = 0, duration = 0.2} = {}) {
30 super({ timing, delay, duration });
31 }
32
33 show(popover, callback) {
34 callback();
35 }
36
37 hide(popover, callback) {
38 callback();
39 }
40
41 _animate(element, {from, to, options, callback, restore = false, animation}) {
42 options = util.extend({}, this.options, options);
43
44 if (animation) {
45 from = animation.from;
46 to = animation.to;
47 }
48
49 animation = animit(element);
50 if (restore) {
51 animation = animation.saveStyle();
52 }
53 animation = animation.queue(from).wait(this.delay).queue({
54 css: to,
55 duration: this.duration,
56 timing: this.timing
57 });
58 if (restore) {
59 animation = animation.restoreStyle();
60 }
61 if (callback) {
62 animation = animation.queue((done) => {
63 callback();
64 done();
65 });
66 }
67 return animation;
68 }
69
70 _animateAll(element, animations) {
71 Object.keys(animations).forEach(key => this._animate(element[key], animations[key]).play());
72 }
73
74}
75
76const fade = {
77 out: {
78 from: {opacity: 1.0},
79 to: {opacity: 0}
80 },
81 in: {
82 from: {opacity: 0},
83 to: {opacity: 1.0}
84 }
85};
86
87export class MDFadePopoverAnimator extends PopoverAnimator {
88 show(popover, callback) {
89 this._animateAll(popover, {
90 _mask: fade.in,
91 _popover: {animation: fade.in, restore: true, callback}
92 });
93 }
94
95 hide(popover, callback) {
96 this._animateAll(popover, {
97 _mask: fade.out,
98 _popover: {animation: fade.out, restore: true, callback}
99 });
100 }
101}
102
103export class IOSFadePopoverAnimator extends MDFadePopoverAnimator {
104 show(popover, callback) {
105 this._animateAll(popover, {
106 _mask: fade.in,
107 _popover: {
108 from: {
109 transform: 'scale3d(1.3, 1.3, 1.0)',
110 opacity: 0
111 },
112 to: {
113 transform: 'scale3d(1.0, 1.0, 1.0)',
114 opacity: 1.0
115 },
116 restore: true,
117 callback
118 }
119 });
120 }
121}