source: [view]
var node = args.node = dojo.byId(args.node),
s = node.style,
cs = dojo.getComputedStyle(node),
nodeCoords = dojo.coords(node, true);
args.duration = args.duration || 1000;
args.words = args.words || false;
var originalHTML = (args.text && typeof(args.text) == "string") ? args.text : node.innerHTML,
originalHeight = s.height,
originalWidth = s.width,
animations = [];
dojo.style(node, {
height: cs.height,
width: cs.width
});
// The following regular expression courtesy of Phil Haack
// http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx
var tagReg = /(<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>)/g;
// Translation: /(HTML tag plus spaces)|(word/letter without '<' plus spaces)/g
var reg = (args.words ?
/(<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>)\s*|([^\s<]+\s*)/g :
/(<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>)\s*|([^\s<]\s*)/g
);
// Split the text into pieces
var pieces = (typeof args.text == "string") ? args.text.match(reg) : node.innerHTML.match(reg);
var html = "";
var numPieces = 0;
var number = 0;
for(var i = 0; i < pieces.length; i++){
var piece = pieces[i];
if(!piece.match(tagReg)){
html += "" + piece + "";
numPieces++;
}else{
html += piece;
}
}
node.innerHTML = html;
// Find the newly-created spans and create their animations
function animatePieces(piece){
var next = piece.nextSibling;
if(piece.tagName == "SPAN" && piece.childNodes.length == 1 && piece.firstChild.nodeType == 3){
var pieceCoords = dojo.coords(piece, true);
number++;
dojo.style(piece, {
padding: 0,
margin: 0,
top: (args.crop ? "0px" : pieceCoords.t + "px"),
left: (args.crop ? "0px" : pieceCoords.l + "px"),
display: "inline"
});
var pieceAnimation = args.pieceAnimation(piece, pieceCoords, nodeCoords, number, numPieces);
if(dojo.isArray(pieceAnimation)){
// if pieceAnimation is an array, append its elements
animations = animations.concat(pieceAnimation);
}else{
// otherwise, append it
animations[animations.length] = pieceAnimation;
}
}else if(piece.firstChild){
animatePieces(piece.firstChild);
}
if(next){
animatePieces(next);
}
}
animatePieces(node.firstChild);
var anim = dojo.fx.combine(animations);
dojo.connect(anim, "onEnd", anim, function(){
node.innerHTML = originalHTML;
dojo.style(node, {
height: originalHeight,
width: originalWidth
});
});
if(args.onPlay){
dojo.connect(anim, "onPlay", anim, args.onPlay);
}
if(args.onEnd){
dojo.connect(anim, "onEnd", anim, args.onEnd);
}
return anim; // dojo.Animation