Skip to content

Instantly share code, notes, and snippets.

@imathis
Created July 30, 2010 23:14
Show Gist options
  • Save imathis/501480 to your computer and use it in GitHub Desktop.
Save imathis/501480 to your computer and use it in GitHub Desktop.
var FS = FS || {};
FS.Toggler = Class.create({
options: {
trigger: 'a[toggles]'
},
initialize: function(options){
this.options = $H(this.options).merge($H(options));
var elements = $$(this.options.get('trigger'));
elements.each(function(el){
el.observe('click', function(event){event.stop(); this.click(el);}.bind(this));
el.store('toggle-elements', this.getElementsFromString(el.readAttribute('toggles')));
}.bind(this))
},
click: function(el){
el.retrieve('toggle-elements').invoke('toggle');
},
getElementsFromString: function(elementString){
return elementString.split(',').map(function(str){
str = str.strip();
return (str.include(".") || str.include(" ") ? $$(str) : $(str.replace('#', '')))
}).flatten();
}
});
document.observe("dom:loaded", function() {
new FS.Toggler();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment