source: Dev/branches/rest-dojo-ui/client/dojox/fx/ext-dojo/complex.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: 5.3 KB
Line 
1define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "dojo/_base/connect",
2        "dojo/_base/Color", "dojo/_base/fx", "dojo/fx"],
3        function(dojo, lang, arrayUtil, declare, connectUtil, Color, baseFx, coreFx){
4        lang.getObject("dojox.fx.ext-dojo.complex", true);
5       
6        var da = baseFx.animateProperty;
7        dojo.animateProperty = baseFx.animateProperty = function(options){
8                // summary:
9                //              An extension of dojo.animateProperty which adds functionality
10                //              that animates a "complex property". The primary example is the
11                //              clip style: rect(10px 30px 10px 50px).
12                //              Note this can also be used with (and is actually intended for)
13                //              CSS3 properties, such as transform:
14                //              transform: rotate(10deg) translateX(0px)
15                //
16                //      description:
17                //              The standard animation doesn't know what to do with something like
18                //              rect(...). This class identifies complex properties by they being a
19                //              string and having parenthesis. If so, that property is made into a
20                //              dojox.fx._Complex object and the getValue() is obtained from
21                //              there.
22                //
23                //      example:
24                //              |       var ani = dojo.animateProperty({
25                //              |               node:dojo.byId("myDiv"),
26                //              |               duration:600,
27                //              |               properties:{
28                //              |                       clip:{start:'rect(0px 50px 50px 0px)', end:'rect(10px 30px 30px 10px)'}
29                //              |               }
30                //              |       }).play();
31                //
32                var d = dojo;
33                var ani = da(options);
34
35                connectUtil.connect(ani, "beforeBegin", function(){
36                        // dojo.Animate original still invokes and still
37                        // works. We're appending this functionality to
38                        // modify targeted properties.
39                        ani.curve.getValue = function(r){
40                                // Overwriting dojo.Animate's curve.getValue
41                                // This is mostly duplicate code, except it looks
42                                // for an instance of dojox.fx._Complex.
43                                var ret = {};
44                                for(var p in this._properties){
45                                        var prop = this._properties[p],
46                                                start = prop.start;
47                                        if(start instanceof d.Color){
48                                                ret[p] = d.blendColors(start, prop.end, r, prop.tempColor).toCss();
49                                        }else if(start instanceof dojox.fx._Complex){
50                                                ret[p] = start.getValue(r);
51                                        }else if(!d.isArray(start)){
52                                                ret[p] = ((prop.end - start) * r) + start + (p != "opacity" ? prop.units || "px" : 0);
53                                        }
54                                }
55                                return ret;
56                        };
57
58                        // this.properties has already been set, as has this.curve._properties.
59                        // We're fixing the props in curve which will have NaN attributes from
60                        // our string property.
61                        var pm = {};
62                        for(var p in this.properties){
63                                var o = this.properties[p];
64                                if(typeof(o.start) == "string" && /\(/.test(o.start)){
65                                        this.curve._properties[p].start = new dojox.fx._Complex(o);
66                                }
67                        }
68
69                });
70                return ani; // dojo.Animation
71        }
72
73        return declare("dojox.fx._Complex", null, {
74                // summary:
75                //              A class that takes a complex property such as
76                //              clip style: rect(10px 30px 10px 50px), and breaks it
77                //              into seperate animatable units. The object has a getValue()
78                //              that will return a string with the modified units.
79                //
80                PROP: /\([\w|,|+|\-|#|\.|\s]*\)/g,
81                constructor: function(options){
82                        var beg = options.start.match(this.PROP);
83                        var end = options.end.match(this.PROP);
84
85                        var begProps = arrayUtil.map(beg, this.getProps, this);
86                        var endProps = arrayUtil.map(end, this.getProps, this);
87
88                        this._properties = {};
89                        this.strProp = options.start;
90                        arrayUtil.forEach(begProps, function(prop, i){
91                                arrayUtil.forEach(prop, function(p, j){
92                                        this.strProp = this.strProp.replace(p, "PROP_"+i+""+j);
93                                        this._properties["PROP_"+i+""+j] = this.makePropObject(p, endProps[i][j])
94                                },this);
95                        },this);
96                },
97
98                getValue: function(/*Float*/r){
99                        // summary:
100                        //              Returns a string with teh same integrity as the
101                        //              original star and end, but with the modified units.
102                        var str = this.strProp, u;
103                        for(var nm in this._properties){
104                                var v, o = this._properties[nm];
105                                if(o.units == "isColor"){
106                                        v = Color.blendColors(o.beg, o.end, r).toCss(false);
107                                        u = "";
108                                }else{
109                                        v = ((o.end - o.beg) * r) + o.beg;
110                                        u = o.units;
111                                }
112                                str = str.replace(nm, v + u);
113                        }
114
115                        return str; // String
116                },
117
118                makePropObject: function(/* String */beg, /* String */end){
119                        // summary:
120                        //              Returns an object that stores the numeric value and
121                        //              units of the beggining and ending properties.
122                        //
123                        var b = this.getNumAndUnits(beg);
124                        var e = this.getNumAndUnits(end);
125                        return {
126                                beg:b.num,
127                                end:e.num,
128                                units:b.units
129                        }; // Object
130                },
131
132                getProps: function(/* String */str){
133                        // summary:
134                        //              Helper function that splits a stringified set of properties
135                        //              into individual units.
136                        //
137                        str = str.substring(1, str.length-1);
138                        var s;
139                        if(/,/.test(str)){
140                                str = str.replace(/\s/g, "");
141                                s = str.split(",");
142                        }else{
143                                str = str.replace(/\s{2,}/g, " ");
144                                s = str.split(" ");
145                        }
146                        return s; // String
147                },
148                getNumAndUnits: function(prop){
149                        // summary:
150                        //              Helper function that returns the numeric verion of the string
151                        //              property (or dojo.Color object) and the unit in which it was
152                        //              defined.
153                        //
154                        if(!prop){ return {}; }
155                        if(/#/.test(prop)){
156                                return {
157                                        num: new Color(prop),
158                                        units:"isColor"
159                                }; // Object
160                        }
161                        var o = {
162                                num:parseFloat(/-*[\d\.\d|\d]{1,}/.exec(prop).join(""))
163                        };
164                        o.units = /[a-z]{1,}/.exec(prop);//.join("");
165                        o.units = o.units && o.units.length ? o.units.join("") : "";
166                        return o; // Object
167                }
168        });
169});
Note: See TracBrowser for help on using the repository browser.