Skip to content

Instantly share code, notes, and snippets.

@jiggliemon
Created November 15, 2011 18:02
Show Gist options
  • Save jiggliemon/1367807 to your computer and use it in GitHub Desktop.
Save jiggliemon/1367807 to your computer and use it in GitHub Desktop.
var Feature = new Class({
initialize: function (el) {
var el = this.el = document.id(el);
var options = this.getObjectFromAttributes(el);
var feature = options.feature = this.getType(el);
if (el.get('text')) {
options.template = el.get('text');
}
return new Feature[feature.capitalize()]( options );
}
,getType: function (el) {
type = el.get('type')? el.get('type') : el.get('tag');
type = type.replace(/:|\//,'::');
if (type.charAt('::') !== -1) {
type = type.split('::').pop();
} else {
throw new Error('You suck.');
}
return type;
}
,getObjectFromAttributes: function (el) {
var attrs = el.attributes, i = 0, options = {}, attr;
for (;i < attrs.length; i++) {
attr = attrs[i];
options[attr.nodeName.replace('data-','')] = attr.nodeValue;
}
return options;
}
,handleArguments: function (args) {
switch (args.length) {
case 0:
throw new Error('`initialize` requires at least one argument');
break;
case 1:
if(typeOf(args[0]) === 'element') {
this.el = document.id(args[0]);
this.setOptions(this.getObjectFromAttributes(this.el));
if (this.el.get('id')) {
this.getContainer().set('id',this.el.get('id'));
}
if (this.el.get('class')) {
this.getContainer().set('class',this.el.get('class'));
}
} else {
this.setOptions(args[0]);
}
break;
case 2:
this.el = document.id(args[0]);
this.setOptions(args[1]);
break;
}
}
,getContainer: function () {
return (this.container)? this.container : this.container = new Element('div.sml-module-wrapper');
}
,toElement: function () {
return this.getContainer();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment