http://www.jackslocum.com/yui/2006/08/19/a-splitbar-component-for-yahoo-ui/YAHOO.ext.Animator = function(){
this.actors = [];
this.playlist = new YAHOO.ext.Animator.AnimSequence();
this.captureDelegate = this.capture.createDelegate(this);
this.playDelegate = this.play.createDelegate(this);
this.syncing = false;
this.stopping = false;
this.playing = false;
for(var i = 0; i < arguments.length; i++){
this.addActor(arguments[i]);
}
};
YAHOO.ext.Animator.prototype = {
capture : function(actor, action){
if(this.syncing){
if(!this.syncMap[actor.id]){
this.syncMap[actor.id] = new YAHOO.ext.Animator.AnimSequence();
}
this.syncMap[actor.id].add(action);
}else{
this.playlist.add(action);
}
},
addActor : function(actor){
actor.onCapture.subscribe(this.captureDelegate);
this.actors.push(actor);
},
startCapture : function(clearPlaylist){
for(var i = 0; i < this.actors.length; i++){
var a = this.actors[i];
if(!this.isCapturing(a)){
a.onCapture.subscribe(this.captureDelegate);
}
a.capturing = true;
}
if(clearPlaylist){
this.playlist = new YAHOO.ext.Animator.AnimSequence();
}
},
isCapturing : function(actor){
var subscribers = actor.onCapture.subscribers;
if(subscribers){
for(var i = 0; i < subscribers.length; i++){
if(subscribers[i] && subscribers[i].contains(this.captureDelegate)){
return true;
}
}
}
return false;
},
stopCapture : function(){
for(var i = 0; i < this.actors.length; i++){
var a = this.actors[i];
a.onCapture.unsubscribe(this.captureDelegate);
a.capturing = false;
}
},
beginSync : function(){
this.syncing = true;
this.syncMap = {};
},
endSync : function(){
this.syncing = false;
var composite = new YAHOO.ext.Animator.CompositeSequence();
for(key in this.syncMap){
if(typeof this.syncMap[key] != 'function'){
composite.add(this.syncMap[key]);
}
}
this.playlist.add(composite);
this.syncMap = null;
},
play : function(oncomplete){
if(this.playing) return; this.stopCapture();
this.playlist.play(oncomplete);
},
stop : function(){
this.playlist.stop();
},
isPlaying : function(){
return this.playlist.isPlaying();
},
clear : function(){
this.playlist = new YAHOO.ext.Animator.AnimSequence();
},
addCall : function(fcn, args, scope){
this.playlist.add(new YAHOO.ext.Actor.Action(scope, fcn, args || []));
},
addAsyncCall : function(fcn, callbackIndex, args, scope){
this.playlist.add(new YAHOO.ext.Actor.AsyncAction(scope, fcn, args || [], callbackIndex));
},
pause : function(seconds){
this.playlist.add(new YAHOO.ext.Actor.PauseAction(seconds));
}
};
YAHOO.ext.Animator.select = function(selector){
var els;
if(typeof selector == 'string'){
els = YAHOO.ext.Element.selectorFunction(selector);
}else if(selector instanceof Array){
els = selector;
}else{
throw 'Invalid selector';
}
return new YAHOO.ext.AnimatorComposite(els);
};
var getActors = YAHOO.ext.Animator.select;
YAHOO.ext.AnimatorComposite = function(els){
this.animator = new YAHOO.ext.Animator();
this.addElements(els);
this.syncAnims = true;
};
YAHOO.ext.AnimatorComposite.prototype = {
isComposite: true,
addElements : function(els){
if(!els) return this;
var anim = this.animator;
for(var i = 0, len = els.length; i < len; i++) {
anim.addActor(new YAHOO.ext.Actor(els[i]));
}
anim.startCapture();
return this;
},
sequence : function(){
this.syncAnims = false;
return this;
},
sync : function(){
this.syncAnims = true;
return this;
},
invoke : function(fn, args){
var els = this.animator.actors;
if(this.syncAnims) this.animator.beginSync();
for(var i = 0, len = els.length; i < len; i++) {
YAHOO.ext.Actor.prototype[fn].apply(els[i], args);
}
if(this.syncAnims) this.animator.endSync();
return this;
},
play : function(callback){
this.animator.play(callback);
return this;
},
reset : function(callback){
this.animator.startCapture(true);
return this;
},
pause : function(seconds){
this.animator.pause(seconds);
return this;
},
getAnimator : function(){
return this.animator;
},
each : function(fn, scope){
var els = this.animator.actors;
if(this.syncAnims) this.animator.beginSync();
for(var i = 0, len = els.length; i < len; i++){
fn.call(scope || els[i], els[i], this, i);
}
if(this.syncAnims) this.animator.endSync();
return this;
},
addCall : function(fcn, args, scope){
this.animator.addCall(fcn, args, scope);
return this;
},
addAsyncCall : function(fcn, callbackIndex, args, scope){
this.animator.addAsyncCall(fcn, callbackIndex, args, scope);
return this;
}
};
for(var fnName in YAHOO.ext.Actor.prototype){
if(typeof YAHOO.ext.Actor.prototype[fnName] == 'function'){
YAHOO.ext.CompositeElement.createCall(YAHOO.ext.AnimatorComposite.prototype, fnName);
}
}
YAHOO.ext.Animator.AnimSequence = function(){
this.actions = [];
this.nextDelegate = this.next.createDelegate(this);
this.playDelegate = this.play.createDelegate(this);
this.oncomplete = null;
this.playing = false;
this.stopping = false;
this.actionIndex = -1;
};
YAHOO.ext.Animator.AnimSequence.prototype = {
add : function(action){
this.actions.push(action);
},
next : function(){
if(this.stopping){
this.playing = false;
if(this.oncomplete){
this.oncomplete(this, false);
}
return;
}
var nextAction = this.actions[++this.actionIndex];
if(nextAction){
nextAction.play(this.nextDelegate);
}else{
this.playing = false;
if(this.oncomplete){
this.oncomplete(this, true);
}
}
},
play : function(oncomplete){
if(this.playing) return; this.oncomplete = oncomplete;
this.stopping = false;
this.playing = true;
this.actionIndex = -1;
this.next();
},
stop : function(){
this.stopping = true;
},
isPlaying : function(){
return this.playing;
},
clear : function(){
this.actions = [];
},
addCall : function(fcn, args, scope){
this.actions.push(new YAHOO.ext.Actor.Action(scope, fcn, args || []));
},
addAsyncCall : function(fcn, callbackIndex, args, scope){
this.actions.push(new YAHOO.ext.Actor.AsyncAction(scope, fcn, args || [], callbackIndex));
},
pause : function(seconds){
this.actions.push(new YAHOO.ext.Actor.PauseAction(seconds));
}
};
YAHOO.ext.Animator.CompositeSequence = function(){
this.sequences = [];
this.completed = 0;
this.trackDelegate = this.trackCompletion.createDelegate(this);
}
YAHOO.ext.Animator.CompositeSequence.prototype = {
add : function(sequence){
this.sequences.push(sequence);
},
play : function(onComplete){
this.completed = 0;
if(this.sequences.length < 1){
if(onComplete)onComplete();
return;
}
this.onComplete = onComplete;
for(var i = 0; i < this.sequences.length; i++){
this.sequences[i].play(this.trackDelegate);
}
},
trackCompletion : function(){
++this.completed;
if(this.completed >= this.sequences.length && this.onComplete){
this.onComplete();
}
},
stop : function(){
for(var i = 0; i < this.sequences.length; i++){
this.sequences[i].stop();
}
},
isPlaying : function(){
for(var i = 0; i < this.sequences.length; i++){
if(this.sequences[i].isPlaying()){
return true;
}
}
return false;
}
};