complex.js 5.3 KB

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