Skip to content

Instantly share code, notes, and snippets.

@jeffmicklos
Created February 10, 2012 01:04
Show Gist options
  • Save jeffmicklos/1784946 to your computer and use it in GitHub Desktop.
Save jeffmicklos/1784946 to your computer and use it in GitHub Desktop.
menu model and controller
Menu = {
active: false,
activeID: null,
togglePairs: {
'menu': {
'trigger': $('#menu'),
'expandable': $('#menu-expandable')
},
'search': {
'trigger': $('#search'),
'expandable': $('#search-expandable')
}
},
setActiveID: function(triggerID, MenuController) {
if(triggerID == this.activeID) {
this.active = false;
this.activeID = null;
} else {
this.active = true;
this.activeID = triggerID;
}
$(MenuController).trigger('update');
}
};
//////////////////////////////////////////////////////////////////
MenuController = function(container, options) {
$(this).bind('update', $.proxy(this.onUpdate, this));
container.delegate('li', 'click', $.proxy(this.onClick, this));
if(options.defaultID) {
Menu.setActiveID(options.defaultID, this);
}
};
MenuController.prototype.onClick = function(event) {
var triggerID = event.currentTarget.id;
Menu.setActiveID(triggerID, this);
};
MenuController.prototype.onUpdate = function(event) {
$.each(Menu.togglePairs, function(key, togglePairs) {
if(Menu.active && key == Menu.activeID) {
togglePairs.trigger.addClass('active');
togglePairs.expandable.slideDown('fast');
} else {
togglePairs.trigger.removeClass('active');
togglePairs.expandable.slideUp('fast');
}
});
};
//////////////////////////////////////////////////////////////////
new MenuController($('#chrome'), {
'defaultID': 'menu'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment