Created
February 10, 2012 01:04
-
-
Save jeffmicklos/1784946 to your computer and use it in GitHub Desktop.
menu model and controller
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
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