2014-02-12 05:23:40 -05:00
|
|
|
define [
|
|
|
|
"libs/backbone"
|
|
|
|
"libs/mustache"
|
|
|
|
], () ->
|
|
|
|
ContextMenuEntry = Backbone.View.extend
|
|
|
|
template: $("#contextMenuEntryTemplate").html()
|
|
|
|
|
|
|
|
events:
|
|
|
|
"click a" : "onClick"
|
|
|
|
|
|
|
|
render: () ->
|
|
|
|
@setElement($(Mustache.to_html(@template, @options)))
|
|
|
|
return @
|
|
|
|
|
|
|
|
onClick: (e) ->
|
2014-03-13 09:00:54 -04:00
|
|
|
e.preventDefault()
|
2014-02-12 05:23:40 -05:00
|
|
|
if @options.onClick
|
|
|
|
@options.onClick()
|
|
|
|
|
|
|
|
ContextMenu = Backbone.View.extend
|
2014-03-13 09:00:54 -04:00
|
|
|
templates:
|
|
|
|
menu: $("#contextMenuTemplate").html()
|
|
|
|
divider: $("#contextMenuDividerTemplate").html()
|
|
|
|
|
|
|
|
initialize: (position, entries) ->
|
|
|
|
if ContextMenu.currentMenu?
|
|
|
|
ContextMenu.currentMenu.destroy()
|
|
|
|
ContextMenu.currentMenu = @
|
2014-02-12 05:23:40 -05:00
|
|
|
@render()
|
2014-03-13 09:00:54 -04:00
|
|
|
for entry in entries
|
|
|
|
@addEntry(entry)
|
|
|
|
@show(position)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
render: () ->
|
2014-03-13 09:00:54 -04:00
|
|
|
@setElement($(@templates.menu))
|
2014-02-12 05:23:40 -05:00
|
|
|
$(document.body).append(@$el)
|
|
|
|
return @
|
|
|
|
|
2014-03-13 09:00:54 -04:00
|
|
|
destroy: () ->
|
|
|
|
@$el.remove()
|
|
|
|
@trigger "destroy"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-03-13 09:00:54 -04:00
|
|
|
show: (position) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
page = $(document.body)
|
|
|
|
page.on "click.hideContextMenu", (e) =>
|
|
|
|
page.off "click.hideContextMenu"
|
2014-03-13 09:00:54 -04:00
|
|
|
@destroy()
|
2014-02-12 05:23:40 -05:00
|
|
|
@$el.css
|
|
|
|
position: "absolute"
|
|
|
|
"z-index": 10000
|
2014-03-13 09:00:54 -04:00
|
|
|
@$el.css position
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
addEntry: (options) ->
|
2014-03-13 09:00:54 -04:00
|
|
|
if options.divider
|
|
|
|
@$el.append $(@templates.divider)
|
|
|
|
else
|
|
|
|
entry = new ContextMenuEntry(options)
|
|
|
|
@$el.append entry.render().el
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|