(function() {
var libFlyweight;
function fly(el) {
if (!libFlyweight) {
libFlyweight = new Roo.Element.Flyweight();
}
libFlyweight.dom = el;
return libFlyweight;
}
Roo.lib.AnimBase = function(el, attributes, duration, method) {
if (el) {
this.init(el, attributes, duration, method);
}
};
Roo.lib.AnimBase.fly = fly;
Roo.lib.AnimBase.prototype = {
toString: function() {
var el = this.getEl();
var id = el.id || el.tagName;
return ("Anim " + id);
},
patterns: {
noNegatives: /width|height|opacity|padding/i,
offsetAttribute: /^((width|height)|(top|left))$/,
defaultUnit: /width|height|top$|bottom$|left$|right$/i,
offsetUnit: /\d+(em|%|en|ex|pt|in|cm|mm|pc)$/i
},
doMethod: function(attr, start, end) {
return this.method(this.currentFrame, start, end - start, this.totalFrames);
},
setAttribute: function(attr, val, unit) {
if (this.patterns.noNegatives.test(attr)) {
val = (val > 0) ? val : 0;
}
Roo.fly(this.getEl(), '_anim').setStyle(attr, val + unit);
},
getAttribute: function(attr) {
var el = this.getEl();
var val = fly(el).getStyle(attr);
if (val !== 'auto' && !this.patterns.offsetUnit.test(val)) {
return parseFloat(val);
}
var a = this.patterns.offsetAttribute.exec(attr) || [];
var pos = !!( a[3] );
var box = !!( a[2] );
if (box || (fly(el).getStyle('position') == 'absolute' && pos)) {
val = el['offset' + a[0].charAt(0).toUpperCase() + a[0].substr(1)];
} else {
val = 0;
}
return val;
},
getDefaultUnit: function(attr) {
if (this.patterns.defaultUnit.test(attr)) {
return 'px';
}
return '';
},
animateX : function(callback, scope) {
var f = function() {
this.onComplete.removeListener(f);
if (typeof callback == "function") {
callback.call(scope || this, this);
}
};
this.onComplete.addListener(f, this);
this.animate();
},
setRuntimeAttribute: function(attr) {
var start;
var end;
var attributes = this.attributes;
this.runtimeAttributes[attr] = {};
var isset = function(prop) {
return (typeof prop !== 'undefined');
};
if (!isset(attributes[attr]['to']) && !isset(attributes[attr]['by'])) {
return false;
}
start = ( isset(attributes[attr]['from']) ) ? attributes[attr]['from'] : this.getAttribute(attr);
if (isset(attributes[attr]['to'])) {
end = attributes[attr]['to'];
} else if (isset(attributes[attr]['by'])) {
if (start.constructor == Array) {
end = [];
for (var i = 0, len = start.length; i < len; ++i) {
end[i] = start[i] + attributes[attr]['by'][i];
}
} else {
end = start + attributes[attr]['by'];
}
}
this.runtimeAttributes[attr].start = start;
this.runtimeAttributes[attr].end = end;
this.runtimeAttributes[attr].unit = ( isset(attributes[attr].unit) ) ? attributes[attr]['unit'] : this.getDefaultUnit(attr);
},
init: function(el, attributes, duration, method) {
var isAnimated = false;
var startTime = null;
var actualFrames = 0;
el = Roo.getDom(el);
this.attributes = attributes || {};
this.duration = duration || 1;
this.method = method || Roo.lib.Easing.easeNone;
this.useSeconds = true;
this.currentFrame = 0;
this.totalFrames = Roo.lib.AnimMgr.fps;
this.getEl = function() {
return el;
};
this.isAnimated = function() {
return isAnimated;
};
this.getStartTime = function() {
return startTime;
};
this.runtimeAttributes = {};
this.animate = function() {
if (this.isAnimated()) {
return false;
}
this.currentFrame = 0;
this.totalFrames = ( this.useSeconds ) ? Math.ceil(Roo.lib.AnimMgr.fps * this.duration) : this.duration;
Roo.lib.AnimMgr.registerElement(this);
};
this.stop = function(finish) {
if (finish) {
this.currentFrame = this.totalFrames;
this._onTween.fire();
}
Roo.lib.AnimMgr.stop(this);
};
var onStart = function() {
this.onStart.fire();
this.runtimeAttributes = {};
for (var attr in this.attributes) {
this.setRuntimeAttribute(attr);
}
isAnimated = true;
actualFrames = 0;
startTime = new Date();
};
var onTween = function() {
var data = {
duration: new Date() - this.getStartTime(),
currentFrame: this.currentFrame
};
data.toString = function() {
return (
'duration: ' + data.duration +
', currentFrame: ' + data.currentFrame
);
};
this.onTween.fire(data);
var runtimeAttributes = this.runtimeAttributes;
for (var attr in runtimeAttributes) {
this.setAttribute(attr, this.doMethod(attr, runtimeAttributes[attr].start, runtimeAttributes[attr].end), runtimeAttributes[attr].unit);
}
actualFrames += 1;
};
var onComplete = function() {
var actual_duration = (new Date() - startTime) / 1000 ;
var data = {
duration: actual_duration,
frames: actualFrames,
fps: actualFrames / actual_duration
};
data.toString = function() {
return (
'duration: ' + data.duration +
', frames: ' + data.frames +
', fps: ' + data.fps
);
};
isAnimated = false;
actualFrames = 0;
this.onComplete.fire(data);
};
this._onStart = new Roo.util.Event(this);
this.onStart = new Roo.util.Event(this);
this.onTween = new Roo.util.Event(this);
this._onTween = new Roo.util.Event(this);
this.onComplete = new Roo.util.Event(this);
this._onComplete = new Roo.util.Event(this);
this._onStart.addListener(onStart);
this._onTween.addListener(onTween);
this._onComplete.addListener(onComplete);
}
};
})();