initial import
[roojs1] / Roo / lib / Motion.js
1 /*
2  * Portions of this file are based on pieces of Yahoo User Interface Library
3  * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
4  * YUI licensed under the BSD License:
5  * http://developer.yahoo.net/yui/license.txt
6  * <script type="text/javascript">
7  *
8  */
9     (function() {
10         Roo.lib.Motion = function(el, attributes, duration, method) {
11             if (el) {
12                 Roo.lib.Motion.superclass.constructor.call(this, el, attributes, duration, method);
13             }
14         };
15
16         Roo.extend(Roo.lib.Motion, Roo.lib.ColorAnim);
17
18
19         var Y = Roo.lib;
20         var superclass = Y.Motion.superclass;
21         var proto = Y.Motion.prototype;
22
23         proto.toString = function() {
24             var el = this.getEl();
25             var id = el.id || el.tagName;
26             return ("Motion " + id);
27         };
28
29         proto.patterns.points = /^points$/i;
30
31         proto.setAttribute = function(attr, val, unit) {
32             if (this.patterns.points.test(attr)) {
33                 unit = unit || 'px';
34                 superclass.setAttribute.call(this, 'left', val[0], unit);
35                 superclass.setAttribute.call(this, 'top', val[1], unit);
36             } else {
37                 superclass.setAttribute.call(this, attr, val, unit);
38             }
39         };
40
41         proto.getAttribute = function(attr) {
42             if (this.patterns.points.test(attr)) {
43                 var val = [
44                         superclass.getAttribute.call(this, 'left'),
45                         superclass.getAttribute.call(this, 'top')
46                         ];
47             } else {
48                 val = superclass.getAttribute.call(this, attr);
49             }
50
51             return val;
52         };
53
54         proto.doMethod = function(attr, start, end) {
55             var val = null;
56
57             if (this.patterns.points.test(attr)) {
58                 var t = this.method(this.currentFrame, 0, 100, this.totalFrames) / 100;
59                 val = Y.Bezier.getPosition(this.runtimeAttributes[attr], t);
60             } else {
61                 val = superclass.doMethod.call(this, attr, start, end);
62             }
63             return val;
64         };
65
66         proto.setRuntimeAttribute = function(attr) {
67             if (this.patterns.points.test(attr)) {
68                 var el = this.getEl();
69                 var attributes = this.attributes;
70                 var start;
71                 var control = attributes['points']['control'] || [];
72                 var end;
73                 var i, len;
74
75                 if (control.length > 0 && !(control[0] instanceof Array)) {
76                     control = [control];
77                 } else {
78                     var tmp = [];
79                     for (i = 0,len = control.length; i < len; ++i) {
80                         tmp[i] = control[i];
81                     }
82                     control = tmp;
83                 }
84
85                 Roo.fly(el).position();
86
87                 if (isset(attributes['points']['from'])) {
88                     Roo.lib.Dom.setXY(el, attributes['points']['from']);
89                 }
90                 else {
91                     Roo.lib.Dom.setXY(el, Roo.lib.Dom.getXY(el));
92                 }
93
94                 start = this.getAttribute('points');
95
96
97                 if (isset(attributes['points']['to'])) {
98                     end = translateValues.call(this, attributes['points']['to'], start);
99
100                     var pageXY = Roo.lib.Dom.getXY(this.getEl());
101                     for (i = 0,len = control.length; i < len; ++i) {
102                         control[i] = translateValues.call(this, control[i], start);
103                     }
104
105
106                 } else if (isset(attributes['points']['by'])) {
107                     end = [ start[0] + attributes['points']['by'][0], start[1] + attributes['points']['by'][1] ];
108
109                     for (i = 0,len = control.length; i < len; ++i) {
110                         control[i] = [ start[0] + control[i][0], start[1] + control[i][1] ];
111                     }
112                 }
113
114                 this.runtimeAttributes[attr] = [start];
115
116                 if (control.length > 0) {
117                     this.runtimeAttributes[attr] = this.runtimeAttributes[attr].concat(control);
118                 }
119
120                 this.runtimeAttributes[attr][this.runtimeAttributes[attr].length] = end;
121             }
122             else {
123                 superclass.setRuntimeAttribute.call(this, attr);
124             }
125         };
126
127         var translateValues = function(val, start) {
128             var pageXY = Roo.lib.Dom.getXY(this.getEl());
129             val = [ val[0] - pageXY[0] + start[0], val[1] - pageXY[1] + start[1] ];
130
131             return val;
132         };
133
134         var isset = function(prop) {
135             return (typeof prop !== 'undefined');
136         };
137     })();