Created
January 13, 2011 08:49
-
-
Save jjt/777597 to your computer and use it in GitHub Desktop.
Various extensions to js/Mootools that I find useful here and there
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.trim = function() { | |
return this.replace(/^\s+|\s+$/g,""); | |
} | |
Element.implement({ | |
hide:function() { | |
this.set_disp_val(); | |
this.setStyle('display','none'); | |
return this; | |
}, | |
show:function() { | |
this.set_disp_val(); | |
this.setStyle('display',this.retrieve('disp_val')); | |
return this; | |
}, | |
set_disp_val:function() { | |
if(this.retrieve('disp_val')) | |
return; | |
//we don't want to set a hidden element to display | |
//so assume block if nothing else | |
if(this.getStyle('display') == 'none') | |
this.store('disp_val','block'); | |
else | |
this.store('disp_val',this.getStyle('display')); | |
return this; | |
}, | |
toggle:function() { | |
this.set_disp_val(); | |
if(this.getStyle('display') == 'none') | |
this.show(); | |
else | |
this.hide(); | |
return this; | |
}, | |
vis:function() { | |
this.setStyle('visibility','visible'); | |
return this; | |
}, | |
invis:function() { | |
this.setStyle('visibility','hidden'); | |
return this; | |
}, | |
copyStyles:function(source,styles) { | |
if(typeof(styles) == 'string') | |
styles = [styles]; | |
styles.each(function(style){ | |
this.setStyle(style,source.getStyle(style)); | |
},this); | |
return this; | |
}, | |
pulse: function(start,end,duration,prop){ | |
var tween_opts = { | |
'transition':'expo:in', | |
'duration': duration || 500}, | |
tween = this.retrieve('pulse:tween', new Fx.Tween(this, tween_opts)); | |
tween.cancel(); | |
if(start == 'cancel_tweens') | |
return this; | |
start = start || '#ffac00'; | |
prop = prop || 'color'; | |
end = end || this.getStyle(prop); | |
//console.log("EL: " + this.id + "; pulse-start = " + start + "; pulse-end = " + end); | |
//tween.cancel(); | |
tween.set(prop,start); | |
(function(){tween.start(prop,start,end)}).delay(800); | |
return this; | |
}, | |
//gets the first matching ancestor up the tree, or returns false | |
//cond can be 'tag', '.classname', '#id-name', $(Mootools Element) | |
grabParent: function(cond,include_self) { | |
var parents = this.getParents(), | |
true_cond = '', | |
ret = false, | |
el = '', | |
include_self = include_self || false; | |
true_cond = (typeof(cond) == 'string') ? cond.substring(1) : cond; | |
if(include_self) | |
parents.unshift(this); | |
parents = parents.reverse(); | |
for(var i = parents.length - 1; i > -1; i--) { | |
el = parents[i]; | |
if(typeof(cond) == 'object' && cond.$family.name == 'element') { | |
if(el === true_cond) | |
return el; | |
continue; | |
} | |
if((cond.charAt(0) == '#' && $(el).get('id') == true_cond) || (cond.charAt(0) == '.' && $(el).hasClass(true_cond)) ||($(el).get('tag') == cond.toLowerCase())) | |
return el; | |
} | |
return false; | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment