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