2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.fx.style"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.fx.style"] = true;
|
|
|
3 |
dojo.provide("dojox.fx.style");
|
|
|
4 |
dojo.experimental("dojox.fx.style");
|
|
|
5 |
//
|
|
|
6 |
// summary: dojox.fx CSS Class _Animations:
|
|
|
7 |
//
|
|
|
8 |
// description: a set of functions to animate properties based on
|
|
|
9 |
// normalized CSS class definitions.
|
|
|
10 |
//
|
|
|
11 |
// provides: addClass, removeClass, and toggleClass
|
|
|
12 |
//
|
|
|
13 |
dojo.require("dojox.fx._base");
|
|
|
14 |
|
|
|
15 |
// FIXME: should the call signatures match dojo.addClass/removeClass/toggleClass and extend
|
|
|
16 |
// by having a third (or fourth) param to mix in additional _Animation args for advanced
|
|
|
17 |
// usage (delay: curve: repeat: easing: etc ... )
|
|
|
18 |
|
|
|
19 |
dojox.fx.addClass = function(/*dojox.fx._arg.StyleArgs*/ args){
|
|
|
20 |
// summary: Animate the effects of adding a class to a node
|
|
|
21 |
// description:
|
|
|
22 |
// Creates an animation that will animate
|
|
|
23 |
// the properties of a node to the properties
|
|
|
24 |
// defined in a standard CSS .class definition.
|
|
|
25 |
// (calculating the differences itself)
|
|
|
26 |
//
|
|
|
27 |
// example:
|
|
|
28 |
//
|
|
|
29 |
// .bar { line-height: 12px; }
|
|
|
30 |
// .foo { line-height: 40px; }
|
|
|
31 |
// <div class="bar" id="test">
|
|
|
32 |
// Multi<br>line<br>text
|
|
|
33 |
// </div>
|
|
|
34 |
//
|
|
|
35 |
// // animate to line-height:40px
|
|
|
36 |
// dojo.fx.addClass({ node:"test", cssClass:"foo" }).play();
|
|
|
37 |
//
|
|
|
38 |
var node = (args.node = dojo.byId(args.node));
|
|
|
39 |
|
|
|
40 |
var pushClass = (function(){
|
|
|
41 |
// summary: onEnd we want to add the class to the node
|
|
|
42 |
// (as dojo.addClass naturally would) in case our
|
|
|
43 |
// class parsing misses anything the browser would
|
|
|
44 |
// otherwise interpret. this may cause some flicker,
|
|
|
45 |
// and will only apply the class so children can inherit
|
|
|
46 |
// after the animation is done (potentially more flicker)
|
|
|
47 |
var innerNode = node; // FIXME: why do we do this like this?
|
|
|
48 |
return function(){
|
|
|
49 |
dojo.addClass(innerNode, args.cssClass);
|
|
|
50 |
innerNode.style.cssText = _beforeStyle;
|
|
|
51 |
}
|
|
|
52 |
})();
|
|
|
53 |
|
|
|
54 |
// _getCalculatedStleChanges is the core of our style/class animations
|
|
|
55 |
var mixedProperties = dojox.fx._getCalculatedStyleChanges(args,true);
|
|
|
56 |
var _beforeStyle = node.style.cssText;
|
|
|
57 |
var _anim = dojo.animateProperty(dojo.mixin({
|
|
|
58 |
properties: mixedProperties
|
|
|
59 |
},args));
|
|
|
60 |
dojo.connect(_anim,"onEnd",_anim,pushClass);
|
|
|
61 |
return _anim; // dojo._Animation
|
|
|
62 |
};
|
|
|
63 |
|
|
|
64 |
dojox.fx.removeClass = function(/*dojox.fx._arg.StyleArgs*/ args){
|
|
|
65 |
// summary: Animate the effects of removing a class from a node
|
|
|
66 |
// description:
|
|
|
67 |
// Creates an animation that will animate the properties of a
|
|
|
68 |
// node (args.node) to the properties calculated after removing
|
|
|
69 |
// a standard CSS className from a that node.
|
|
|
70 |
//
|
|
|
71 |
// calls dojo.removeClass(args.cssClass) onEnd of animation
|
|
|
72 |
//
|
|
|
73 |
// standard dojo._Animation object rules apply.
|
|
|
74 |
//
|
|
|
75 |
// example:
|
|
|
76 |
// | // animate the removal of "foo" from a node with id="bar"
|
|
|
77 |
// | dojox.fx.removeClass({
|
|
|
78 |
// | node: "bar",
|
|
|
79 |
// | cssClass: "foo"
|
|
|
80 |
// | }).play();
|
|
|
81 |
|
|
|
82 |
var node = (args.node = dojo.byId(args.node));
|
|
|
83 |
|
|
|
84 |
var pullClass = (function(){
|
|
|
85 |
// summary: onEnd we want to remove the class from the node
|
|
|
86 |
// (as dojo.removeClass naturally would) in case our class
|
|
|
87 |
// parsing misses anything the browser would otherwise
|
|
|
88 |
// interpret. this may cause some flicker, and will only
|
|
|
89 |
// apply the class so children can inherit after the
|
|
|
90 |
// animation is done (potentially more flicker)
|
|
|
91 |
//
|
|
|
92 |
var innerNode = node;
|
|
|
93 |
return function(){
|
|
|
94 |
dojo.removeClass(innerNode, args.cssClass);
|
|
|
95 |
innerNode.style.cssText = _beforeStyle;
|
|
|
96 |
}
|
|
|
97 |
})();
|
|
|
98 |
|
|
|
99 |
var mixedProperties = dojox.fx._getCalculatedStyleChanges(args,false);
|
|
|
100 |
var _beforeStyle = node.style.cssText;
|
|
|
101 |
var _anim = dojo.animateProperty(dojo.mixin({
|
|
|
102 |
properties: mixedProperties
|
|
|
103 |
},args));
|
|
|
104 |
dojo.connect(_anim,"onEnd",_anim,pullClass);
|
|
|
105 |
return _anim; // dojo._Animation
|
|
|
106 |
};
|
|
|
107 |
|
|
|
108 |
dojox.fx.toggleClass = function(/*DomNode|String*/node, /*String*/cssClass, /*Boolean?*/condition){
|
|
|
109 |
// summary:
|
|
|
110 |
// Animate the effects of Toggling a class on a Node
|
|
|
111 |
//
|
|
|
112 |
// description:
|
|
|
113 |
// creates an animation that will animate the effect of
|
|
|
114 |
// toggling a class on or off of a node.
|
|
|
115 |
// Adds a class to node if not present, or removes if present.
|
|
|
116 |
// Pass a boolean condition if you want to explicitly add or remove.
|
|
|
117 |
// node:
|
|
|
118 |
// The domNode (or string of the id) to toggle
|
|
|
119 |
// cssClass:
|
|
|
120 |
// String of the classname to add to the node
|
|
|
121 |
// condition:
|
|
|
122 |
// If passed, true means to add the class, false means to remove.
|
|
|
123 |
//
|
|
|
124 |
// example:
|
|
|
125 |
// | // add the class "sampleClass" to a node id="theNode"
|
|
|
126 |
// | dojox.fx.toggleClass("theNode","sampleClass",true).play();
|
|
|
127 |
// example:
|
|
|
128 |
// | // toggle the class "sampleClass" on the node id="theNode"
|
|
|
129 |
// | dojox.fx.toggleClass("theNode","sampleClass").play();
|
|
|
130 |
|
|
|
131 |
if(typeof condition == "undefined"){
|
|
|
132 |
condition = !dojo.hasClass(node, cssClass);
|
|
|
133 |
}
|
|
|
134 |
return dojox.fx[(condition ? "addClass" : "removeClass")]({ node: node, cssClass:cssClass }); // dojo._Animation
|
|
|
135 |
// TODO: support 4th param animMixin to allow passing of easing and duration and other _Animtion options
|
|
|
136 |
};
|
|
|
137 |
|
|
|
138 |
dojox.fx._allowedProperties = [
|
|
|
139 |
// summary:
|
|
|
140 |
// this is our pseudo map of properties we will check for.
|
|
|
141 |
// it should be much more intuitive. a way to normalize and
|
|
|
142 |
// "predict" intent, or even something more clever ...
|
|
|
143 |
// open to suggestions.
|
|
|
144 |
|
|
|
145 |
// no-brainers:
|
|
|
146 |
"width",
|
|
|
147 |
"height",
|
|
|
148 |
// only if position = absolute || relative?
|
|
|
149 |
"left", "top", "right", "bottom",
|
|
|
150 |
// these need to be filtered through dojo.colors?
|
|
|
151 |
// "background", // normalize to:
|
|
|
152 |
/* "backgroundImage", */
|
|
|
153 |
// "backgroundPosition", // FIXME: to be effective, this needs "#px #px"?
|
|
|
154 |
"backgroundColor",
|
|
|
155 |
|
|
|
156 |
"color",
|
|
|
157 |
|
|
|
158 |
// "border",
|
|
|
159 |
"borderBottomColor", "borderBottomWidth",
|
|
|
160 |
"borderTopColor","borderTopWidth",
|
|
|
161 |
"borderLeftColor","borderLeftWidth",
|
|
|
162 |
"borderRightColor","borderRightWidth",
|
|
|
163 |
|
|
|
164 |
// "padding", // normalize to:
|
|
|
165 |
"paddingLeft", "paddingRight", "paddingTop", "paddingBottom",
|
|
|
166 |
// "margin", // normalize to:
|
|
|
167 |
"marginLeft", "marginTop", "marginRight", "marginBottom",
|
|
|
168 |
|
|
|
169 |
// unit import/delicate?:
|
|
|
170 |
"lineHeight",
|
|
|
171 |
"letterSpacing",
|
|
|
172 |
"fontSize"
|
|
|
173 |
];
|
|
|
174 |
|
|
|
175 |
dojox.fx._getStyleSnapshot = function(/* Object */cache){
|
|
|
176 |
// summary:
|
|
|
177 |
// uses a dojo.getComputedStyle(node) cache reference and
|
|
|
178 |
// iterates through the 'documented/supported animate-able'
|
|
|
179 |
// properties.
|
|
|
180 |
//
|
|
|
181 |
// returns: Array
|
|
|
182 |
// an array of raw, calculcated values (no keys), to be normalized/compared
|
|
|
183 |
// elsewhere
|
|
|
184 |
return dojo.map(dojox.fx._allowedProperties,function(style){
|
|
|
185 |
return cache[style]; // String
|
|
|
186 |
}); // Array
|
|
|
187 |
};
|
|
|
188 |
|
|
|
189 |
dojox.fx._getCalculatedStyleChanges = function(/*dojox.fx._arg.StyleArgs*/ args, /*Boolean*/addClass){
|
|
|
190 |
// summary: calclate the difference in style properties between two states
|
|
|
191 |
// description:
|
|
|
192 |
// calculate and normalize(?) the differences between two states
|
|
|
193 |
// of a node (args.node) by quickly adding or removing a class, and
|
|
|
194 |
// iterateing over the results of dojox.fx._getStyleSnapshot()
|
|
|
195 |
//
|
|
|
196 |
// addClass:
|
|
|
197 |
// true to calculate what adding a class would do,
|
|
|
198 |
// false to calculate what removing the class would do
|
|
|
199 |
|
|
|
200 |
var node = (args.node = dojo.byId(args.node));
|
|
|
201 |
var compute = dojo.getComputedStyle(node);
|
|
|
202 |
|
|
|
203 |
// take our snapShots
|
|
|
204 |
var _before = dojox.fx._getStyleSnapshot(compute);
|
|
|
205 |
dojo[(addClass ? "addClass" : "removeClass")](node,args.cssClass);
|
|
|
206 |
var _after = dojox.fx._getStyleSnapshot(compute);
|
|
|
207 |
dojo[(addClass ? "removeClass" : "addClass")](node,args.cssClass);
|
|
|
208 |
|
|
|
209 |
var calculated = {};
|
|
|
210 |
var i = 0;
|
|
|
211 |
dojo.forEach(dojox.fx._allowedProperties,function(prop){
|
|
|
212 |
if(_before[i] != _after[i]){
|
|
|
213 |
// FIXME: the static unit: px is not good, either. need to parse unit from computed style?
|
|
|
214 |
calculated[prop] = { end: parseInt(_after[i]) /* start: parseInt(_before[i]), unit: 'px' */ };
|
|
|
215 |
}
|
|
|
216 |
i++;
|
|
|
217 |
});
|
|
|
218 |
return calculated;
|
|
|
219 |
};
|
|
|
220 |
|
|
|
221 |
}
|