Skip to content

Instantly share code, notes, and snippets.

@MeoMix
Created October 27, 2012 19:27
Show Gist options
  • Save MeoMix/3965764 to your computer and use it in GitHub Desktop.
Save MeoMix/3965764 to your computer and use it in GitHub Desktop.
//Responsible for showing options when interacting with a song list or play list
define(function(){
'use strict';
var selector = $('#ContextMenu');
//Hide the context menu whenever any click occurs not just when selecting an item.
$(document).click(function(){
selector.offset({top:0, left:0}).hide();
});
return {
empty: function(){
selector.empty();
},
addContextMenuItem: function(text, onClick){
$('<a/>', {
href: '#',
text: text,
click: onClick
}).appendTo(selector);
},
show: function(top, left){
//Don't allow the context menu to display off the document viewport.
var needsVerticalFlip = top + selector.height() > $(document).height();
if(needsVerticalFlip){
top = top - selector.height();
}
var needsHorizontalFlip = left + selector.width() > $(document).width();
if(needsHorizontalFlip){
left = left - selector.width();
}
selector.offset({
top: top,
left: left
}).show();
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment