API Docs for: 3.8.0
Show:

File: transition/js/transition-timer.js

  1. /**
  2. * Provides the base Transition class, for animating numeric properties.
  3. *
  4. * @module transition
  5. * @submodule transition-timer
  6. */


  7. var Transition = Y.Transition;

  8. Y.mix(Transition.prototype, {
  9.     _start: function() {
  10.         if (Transition.useNative) {
  11.             this._runNative();
  12.         } else {
  13.             this._runTimer();
  14.         }
  15.     },

  16.     _runTimer: function() {
  17.         var anim = this;
  18.         anim._initAttrs();

  19.         Transition._running[Y.stamp(anim)] = anim;
  20.         anim._startTime = new Date();
  21.         Transition._startTimer();
  22.     },

  23.     _endTimer: function() {
  24.         var anim = this;
  25.         delete Transition._running[Y.stamp(anim)];
  26.         anim._startTime = null;
  27.     },

  28.     _runFrame: function() {
  29.         var t = new Date() - this._startTime;
  30.         this._runAttrs(t);
  31.     },

  32.     _runAttrs: function(time) {
  33.         var anim = this,
  34.             node = anim._node,
  35.             config = anim._config,
  36.             uid = Y.stamp(node),
  37.             attrs = Transition._nodeAttrs[uid],
  38.             customAttr = Transition.behaviors,
  39.             done = false,
  40.             allDone = false,
  41.             data,
  42.             name,
  43.             attribute,
  44.             setter,
  45.             elapsed,
  46.             delay,
  47.             d,
  48.             t,
  49.             i;

  50.         for (name in attrs) {
  51.             if ((attribute = attrs[name]) && attribute.transition === anim) {
  52.                 d = attribute.duration;
  53.                 delay = attribute.delay;
  54.                 elapsed = (time - delay) / 1000;
  55.                 t = time;
  56.                 data = {
  57.                     type: 'propertyEnd',
  58.                     propertyName: name,
  59.                     config: config,
  60.                     elapsedTime: elapsed
  61.                 };

  62.                 setter = (i in customAttr && 'set' in customAttr[i]) ?
  63.                         customAttr[i].set : Transition.DEFAULT_SETTER;

  64.                 done = (t >= d);

  65.                 if (t > d) {
  66.                     t = d;
  67.                 }

  68.                 if (!delay || time >= delay) {
  69.                     setter(anim, name, attribute.from, attribute.to, t - delay, d - delay,
  70.                         attribute.easing, attribute.unit);

  71.                     if (done) {
  72.                         delete attrs[name];
  73.                         anim._count--;

  74.                         if (config[name] && config[name].on && config[name].on.end) {
  75.                             config[name].on.end.call(Y.one(node), data);
  76.                         }

  77.                         //node.fire('transition:propertyEnd', data);

  78.                         if (!allDone && anim._count <= 0) {
  79.                             allDone = true;
  80.                             anim._end(elapsed);
  81.                             anim._endTimer();
  82.                         }
  83.                     }
  84.                 }

  85.             }
  86.         }
  87.     },

  88.     _initAttrs: function() {
  89.         var anim = this,
  90.             customAttr = Transition.behaviors,
  91.             uid = Y.stamp(anim._node),
  92.             attrs = Transition._nodeAttrs[uid],
  93.             attribute,
  94.             duration,
  95.             delay,
  96.             easing,
  97.             val,
  98.             name,
  99.             mTo,
  100.             mFrom,
  101.             unit, begin, end;

  102.         for (name in attrs) {
  103.             if ((attribute = attrs[name]) && attribute.transition === anim) {
  104.                 duration = attribute.duration * 1000;
  105.                 delay = attribute.delay * 1000;
  106.                 easing = attribute.easing;
  107.                 val = attribute.value;

  108.                 // only allow supported properties
  109.                 if (name in anim._node.style || name in Y.DOM.CUSTOM_STYLES) {
  110.                     begin = (name in customAttr && 'get' in customAttr[name])  ?
  111.                             customAttr[name].get(anim, name) : Transition.DEFAULT_GETTER(anim, name);

  112.                     mFrom = Transition.RE_UNITS.exec(begin);
  113.                     mTo = Transition.RE_UNITS.exec(val);

  114.                     begin = mFrom ? mFrom[1] : begin;
  115.                     end = mTo ? mTo[1] : val;
  116.                     unit = mTo ? mTo[2] : mFrom ?  mFrom[2] : ''; // one might be zero TODO: mixed units

  117.                     if (!unit && Transition.RE_DEFAULT_UNIT.test(name)) {
  118.                         unit = Transition.DEFAULT_UNIT;
  119.                     }

  120.                     if (typeof easing === 'string') {
  121.                         if (easing.indexOf('cubic-bezier') > -1) {
  122.                             easing = easing.substring(13, easing.length - 1).split(',');
  123.                         } else if (Transition.easings[easing]) {
  124.                             easing = Transition.easings[easing];
  125.                         }
  126.                     }

  127.                     attribute.from = Number(begin);
  128.                     attribute.to = Number(end);
  129.                     attribute.unit = unit;
  130.                     attribute.easing = easing;
  131.                     attribute.duration = duration + delay;
  132.                     attribute.delay = delay;
  133.                 } else {
  134.                     delete attrs[name];
  135.                     anim._count--;
  136.                 }
  137.             }
  138.         }
  139.     },

  140.     destroy: function() {
  141.         this.detachAll();
  142.         this._node = null;
  143.     }
  144. }, true);

  145. Y.mix(Y.Transition, {
  146.     _runtimeAttrs: {},
  147.     /*
  148.      * Regex of properties that should use the default unit.
  149.      *
  150.      * @property RE_DEFAULT_UNIT
  151.      * @static
  152.      */
  153.     RE_DEFAULT_UNIT: /^width|height|top|right|bottom|left|margin.*|padding.*|border.*$/i,

  154.     /*
  155.      * The default unit to use with properties that pass the RE_DEFAULT_UNIT test.
  156.      *
  157.      * @property DEFAULT_UNIT
  158.      * @static
  159.      */
  160.     DEFAULT_UNIT: 'px',

  161.     /*
  162.      * Time in milliseconds passed to setInterval for frame processing
  163.      *
  164.      * @property intervalTime
  165.      * @default 20
  166.      * @static
  167.      */
  168.     intervalTime: 20,

  169.     /*
  170.      * Bucket for custom getters and setters
  171.      *
  172.      * @property behaviors
  173.      * @static
  174.      */
  175.     behaviors: {
  176.         left: {
  177.             get: function(anim, attr) {
  178.                 return Y.DOM._getAttrOffset(anim._node, attr);
  179.             }
  180.         }
  181.     },

  182.     /*
  183.      * The default setter to use when setting object properties.
  184.      *
  185.      * @property DEFAULT_SETTER
  186.      * @static
  187.      */
  188.     DEFAULT_SETTER: function(anim, att, from, to, elapsed, duration, fn, unit) {
  189.         from = Number(from);
  190.         to = Number(to);

  191.         var node = anim._node,
  192.             val = Transition.cubicBezier(fn, elapsed / duration);

  193.         val = from + val[0] * (to - from);

  194.         if (node) {
  195.             if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
  196.                 unit = unit || '';
  197.                 Y.DOM.setStyle(node, att, val + unit);
  198.             }
  199.         } else {
  200.             anim._end();
  201.         }
  202.     },

  203.     /*
  204.      * The default getter to use when getting object properties.
  205.      *
  206.      * @property DEFAULT_GETTER
  207.      * @static
  208.      */
  209.     DEFAULT_GETTER: function(anim, att) {
  210.         var node = anim._node,
  211.             val = '';

  212.         if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
  213.             val = Y.DOM.getComputedStyle(node, att);
  214.         }

  215.         return val;
  216.     },

  217.     _startTimer: function() {
  218.         if (!Transition._timer) {
  219.             Transition._timer = setInterval(Transition._runFrame, Transition.intervalTime);
  220.         }
  221.     },

  222.     _stopTimer: function() {
  223.         clearInterval(Transition._timer);
  224.         Transition._timer = null;
  225.     },

  226.     /*
  227.      * Called per Interval to handle each animation frame.
  228.      * @method _runFrame
  229.      * @private
  230.      * @static
  231.      */    
  232.     _runFrame: function() {
  233.         var done = true,
  234.             anim;
  235.         for (anim in Transition._running) {
  236.             if (Transition._running[anim]._runFrame) {
  237.                 done = false;
  238.                 Transition._running[anim]._runFrame();
  239.             }
  240.         }

  241.         if (done) {
  242.             Transition._stopTimer();
  243.         }
  244.     },

  245.     cubicBezier: function(p, t) {
  246.         var x0 = 0,
  247.             y0 = 0,
  248.             x1 = p[0],
  249.             y1 = p[1],
  250.             x2 = p[2],
  251.             y2 = p[3],
  252.             x3 = 1,
  253.             y3 = 0,

  254.             A = x3 - 3 * x2 + 3 * x1 - x0,
  255.             B = 3 * x2 - 6 * x1 + 3 * x0,
  256.             C = 3 * x1 - 3 * x0,
  257.             D = x0,
  258.             E = y3 - 3 * y2 + 3 * y1 - y0,
  259.             F = 3 * y2 - 6 * y1 + 3 * y0,
  260.             G = 3 * y1 - 3 * y0,
  261.             H = y0,

  262.             x = (((A*t) + B)*t + C)*t + D,
  263.             y = (((E*t) + F)*t + G)*t + H;

  264.         return [x, y];
  265.     },

  266.     easings: {
  267.         ease: [0.25, 0, 1, 0.25],
  268.         linear: [0, 0, 1, 1],
  269.         'ease-in': [0.42, 0, 1, 1],
  270.         'ease-out': [0, 0, 0.58, 1],
  271.         'ease-in-out': [0.42, 0, 0.58, 1]
  272.     },

  273.     _running: {},
  274.     _timer: null,

  275.     RE_UNITS: /^(-?\d*\.?\d*){1}(em|ex|px|in|cm|mm|pt|pc|%)*$/
  276. }, true);

  277. Transition.behaviors.top = Transition.behaviors.bottom = Transition.behaviors.right = Transition.behaviors.left;

  278. Y.Transition = Transition;

  279.