source: Dev/branches/rest-dojo-ui/client/dojox/fx/style.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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