Ext.namespace('Ext', 'Ext.util');
Ext.util.SpriteManager = function(config){
Ext.apply(this,config,{
spriteSource:true,
spriteWidth:1,
spriteHeight:1,
spriteColumns:1,
spriteRows:1
});
};
Ext.util.SpriteManager.prototype = {
/**
* Gets the CSS Style for the appropriate image
* @param {Object...} spriteLocation Expects an object with an X and Y location of the sprite to display
* @return {String} returns the CSS style string enclosed in braces {}
*/
getSpriteCSS : function(spriteLocation){
var locationX = spriteLocation.x-1 || 0;
var locationY = spriteLocation.y-1 || 0;
var spriteCss = ['{'];
var spriteW = this.spriteWidth / this.spriteColumns;
var spriteH = this.spriteHeight / this.spriteRows;
var spriteXPos = locationX * spriteW;
var spriteYPos = locationY * spriteH;
if(!typeof this.spriteSource =="boolean")
{
spriteCss.push('background:url(' + this.spriteSource +');');
}
spriteCss.push('height:' + spriteH + 'px;');
spriteCss.push('width:' + spriteW + 'px;');
spriteCss.push('background-position:-' + spriteXPos + 'px -' + spriteYPos + 'px;');
spriteCss.push('}');
return spriteCss.join("");
},
/**
* Applies the CSS Style for the appropriate sprite image to an element
* @param {element} Ext.Element to apply the sprite style to.
* @param {Object...} spriteLocation Expects an object with an X and Y location of the sprite to display
*/
applySpriteCss : function(el, spriteLocation)
{
var css = this.getSpriteCSS(spriteLocation);
el.applyStyles(css);
}
};
Recent Comments