summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/app/animation.js
blob: f1d90b0944972b71d2db884e4ac6633e9b0ef224 (plain)
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
//>>built
define("dojox/app/animation", ["dojo/_base/kernel", 
        "dojo/_base/lang",
        "dojo/_base/declare",
        "dojo/_base/array",
        "dojo/_base/Deferred",
        "dojo/DeferredList",
        "dojo/on",
        "dojo/_base/sniff"], 
        function(dojo, lang, declare, array, deferred, deferredList, on, has){
    //TODO create cross platform animation/transition effects
    var transitionEndEventName = "transitionend";
    var transitionPrefix = "t"; //by default use "t" prefix and "ransition" to make word "transition"
    var translateMethodStart = "translate3d(";//Android 2.x does not support translateX in CSS Transition, we need to use translate3d in webkit browsers
    var translateMethodEnd = ",0,0)";
    if(has("webkit")){
        transitionPrefix = "WebkitT";
        transitionEndEventName = "webkitTransitionEnd";
    }else if(has("mozilla")){
        transitionPrefix = "MozT";
        translateMethodStart = "translateX(";
        translateMethodEnd = ")";
    }
    
    

    //TODO find a way to lock the animation and prevent animation conflict
    declare("dojox.app.animation", null, {
        

        constructor: function(args){
            //default config should be in animation object itself instead of its prototype
            //otherwise, it might be easy for making mistake of modifying prototype
            var defaultConfig = {
                startState: {},
                endState: {},
                node: null,
                duration: 250,
                "in": true,
                direction: 1,
                autoClear: true
            };
            
            lang.mixin(this, defaultConfig);
            lang.mixin(this, args);
            
            //create the deferred object which will resolve after the animation is finished.
            //We can rely on "onAfterEnd" function to notify the end of a single animation,
            //but using a deferred object is easier to wait for multiple animations end.
            if(!this.deferred){
                this.deferred = new deferred();
            }
        },
        
        play: function(){
            //play the animation using CSS3 Transition
            dojox.app.animation.groupedPlay([this]);
        },
        
        //method to apply the state of the transition
        _applyState: function(state){
            var style = this.node.style;
            for(var property in state){
                if(state.hasOwnProperty(property)){
                    style[property] = state[property];
                }
            }
        },
        
        //method to initialize state for transition
        initState: function(){
            
            //apply the immediate style change for initial state.
            this.node.style[transitionPrefix + "ransitionProperty"] = "none";
            this.node.style[transitionPrefix + "ransitionDuration"] = "0ms";
            this._applyState(this.startState);
            
        },
        
        _beforeStart: function(){
            if (this.node.style.display === "none"){
                this.node.style.display = "";
            }
            this.beforeStart();
        },
        
        _beforeClear: function(){
            this.node.style[transitionPrefix + "ransitionProperty"] = null;
            this.node.style[transitionPrefix + "ransitionDuration"] = null;
            if(this["in"] !== true){
                this.node.style.display = "none";
            }            
            this.beforeClear();
        },
        
        _onAfterEnd: function(){
            this.deferred.resolve(this.node);
            if(this.node.id && dojox.app.animation.playing[this.node.id]===this.deferred){
                delete dojox.app.animation.playing[this.node.id];
            }
            this.onAfterEnd();
        },
        
        beforeStart: function(){
            
        },
        
        beforeClear: function(){
            
        },
        
        onAfterEnd: function(){
            
        },
        
        //method to start the transition
        start: function(){
            this._beforeStart();
            
            var self = this;
            //change the transition duration
            self.node.style[transitionPrefix + "ransitionProperty"] = "all";
            self.node.style[transitionPrefix + "ransitionDuration"] = self.duration + "ms";
            
            //connect to clear the transition state after the transition end.
            //Since the transition is conducted asynchronously, we need to 
            //connect to transition end event to clear the state
            on.once(self.node, transitionEndEventName, function(){
                self.clear();
            });
            
            this._applyState(this.endState);
        },
        
        //method to clear state after transition
        clear: function(){
            this._beforeClear();
            this._removeState(this.endState);
            console.log(this.node.id + " clear.");
            this._onAfterEnd();
        },
        
        //create removeState method
        _removeState: function(state){
            var style = this.node.style;
            for(var property in state){
                if(state.hasOwnProperty(property)){
                    style[property] = null;
                }
            }
        }
        
    });
    
    //TODO add the lock mechanism for all of the transition effects
    //     consider using only one object for one type of transition.
    //TODO create the first animation, slide.
    dojox.app.animation.slide = function(node, config){

        //TODO create the return and set the startState, endState of the return
        var ret = new dojox.app.animation(config);
        ret.node = node;
        
        var startX = "0";
        var endX = "0";
        
        if(ret["in"]){
            if(ret.direction === 1){
                startX = "100%";
            }else{
                startX = "-100%";
            }
        }else{
            if(ret.direction === 1){
                endX = "-100%";
            }else{
                endX = "100%";
            }
        }
        
        
        ret.startState[transitionPrefix + "ransform"]=translateMethodStart+startX+translateMethodEnd;
        
        ret.endState[transitionPrefix + "ransform"]=translateMethodStart+endX+translateMethodEnd;
        
        return ret;
    };
        
    
    //fade in/out animation effects
    dojox.app.animation.fade = function(node, config){
        
        var ret = new dojox.app.animation(config);
        ret.node = node;
        
        var startOpacity = "0";
        var endOpacity = "0";
        
        if(ret["in"]){
            endOpacity = "1";
        }else{
            startOpacity = "1";
        }
        
        lang.mixin(ret, {
            startState:{
                "opacity": startOpacity
            },
            endState:{
                "opacity": endOpacity
            }
        });
        
        return ret;
    };
    
  //fade in/out animation effects
    dojox.app.animation.flip = function(node, config){
        
        var ret = new dojox.app.animation(config);
        ret.node = node;
       
        if(ret["in"]){
            //Need to set opacity here because Android 2.2 has bug that
            //scale(...) in transform does not persist status
            lang.mixin(ret,{
                startState:{
                    "opacity": "0"
                },
                endState:{
                    "opacity": "1"
                }
            });
            ret.startState[transitionPrefix + "ransform"]="scale(0,0.8) skew(0,-30deg)";
            ret.endState[transitionPrefix + "ransform"]="scale(1,1) skew(0,0)";
        }else{
            lang.mixin(ret,{
                startState:{
                    "opacity": "1"
                },
                endState:{
                    "opacity": "0"
                }
            });         
            ret.startState[transitionPrefix + "ransform"]="scale(1,1) skew(0,0)";
            ret.endState[transitionPrefix + "ransform"]="scale(0,0.8) skew(0,30deg)";
        }
        
        return ret;
    };
    
    var getWaitingList = function(/*Array*/ nodes){
        var defs = [];
        array.forEach(nodes, function(node){
            //check whether the node is under other animation
            if(node.id && dojox.app.animation.playing[node.id]){
                //TODO hook on deferred object in dojox.app.animation.playing
                defs.push(dojox.app.animation.playing[node.id]);
            }
            
        });
        return new deferredList(defs);
    };
    
    dojox.app.animation.getWaitingList = getWaitingList;
    
    //TODO groupedPlay should ensure the UI update happens when
    //all animations end.
    //the group player to start multiple animations together
    dojox.app.animation.groupedPlay = function(/*Array*/args){
        //args should be array of dojox.app.animation
        
        var animNodes = array.filter(args, function(item){
            return item.node;
        });
        
        var waitingList = getWaitingList(animNodes);

        //update registry with deferred objects in animations of args.
        array.forEach(args, function(item){
            if(item.node.id){
                dojox.app.animation.playing[item.node.id] = item.deferred;
            }
        });
        
        //TODO wait for all deferred object in deferred list to resolve
        dojo.when(waitingList, function(){
            array.forEach(args, function(item){
                //set the start state
                item.initState();
            });
            
            //Assume the fps of the animation should be higher than 30 fps and
            //allow the browser to use one frame's time to redraw so that
            //the transition can be started
            setTimeout(function(){
                array.forEach(args, function(item){
                    item.start();
                });            
            }, 33);
        });        
    };
    
    //the chain player to start multiple animations one by one
    dojox.app.animation.chainedPlay = function(/*Array*/args){
        //args should be array of dojox.app.animation
        
        var animNodes = array.filter(args, function(item){
            return item.node;
        });
        
        var waitingList = getWaitingList(animNodes);

        //update registry with deferred objects in animations of args.
        array.forEach(args, function(item){
            if(item.node.id){
                dojox.app.animation.playing[item.node.id] = item.deferred;
            }
        });
        
        dojo.when(waitingList, function(){
            array.forEach(args, function(item){
                //set the start state
                item.initState();
            });
            
            //chain animations together
            for (var i=1, len=args.length; i < len; i++){
                args[i-1].deferred.then(lang.hitch(args[i], function(){
                    this.start();
                }));
            }
            
            //Assume the fps of the animation should be higher than 30 fps and
            //allow the browser to use one frame's time to redraw so that
            //the transition can be started
            setTimeout(function(){
                args[0].start();
            }, 33);
        });        
    };
    
    //TODO complete the registry mechanism for animation handling and prevent animation conflicts
    dojox.app.animation.playing = {};
    
    return dojox.app.animation;
});