overleaf/services/web/public/coffee/utils/ContextMenu.coffee

63 lines
1.2 KiB
CoffeeScript
Raw Normal View History

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) ->
e.preventDefault()
2014-02-12 05:23:40 -05:00
if @options.onClick
@options.onClick()
ContextMenu = Backbone.View.extend
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()
for entry in entries
@addEntry(entry)
@show(position)
2014-02-12 05:23:40 -05:00
render: () ->
@setElement($(@templates.menu))
2014-02-12 05:23:40 -05:00
$(document.body).append(@$el)
return @
destroy: () ->
@$el.remove()
@trigger "destroy"
2014-02-12 05:23:40 -05:00
show: (position) ->
2014-02-12 05:23:40 -05:00
page = $(document.body)
page.on "click.hideContextMenu", (e) =>
page.off "click.hideContextMenu"
@destroy()
2014-02-12 05:23:40 -05:00
@$el.css
position: "absolute"
"z-index": 10000
@$el.css position
2014-02-12 05:23:40 -05:00
addEntry: (options) ->
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