mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
179 lines
4.3 KiB
CoffeeScript
179 lines
4.3 KiB
CoffeeScript
define [
|
|
"utils/ContextMenu"
|
|
"libs/backbone"
|
|
"libs/mustache"
|
|
], (ContextMenu) ->
|
|
EntityView = Backbone.View.extend
|
|
entityTemplate: $("#entityTemplate").html()
|
|
|
|
initialize: () ->
|
|
@ide = @options.manager.ide
|
|
@manager = @options.manager
|
|
@manager.registerView(@model.id, @)
|
|
@bindToModel()
|
|
|
|
events: () ->
|
|
events = {}
|
|
events["click ##{@model.id} > .js-clickable"] = "parentOnClick"
|
|
events["click ##{@model.id} > .entity-label"] = "parentOnClick"
|
|
events["click .dropdown-caret"] = "showContextMenuFromCaret"
|
|
events["contextmenu"] = "showContextMenuFromRightClick"
|
|
return events
|
|
|
|
render: () ->
|
|
@$el.append(Mustache.to_html @entityTemplate, @model.attributes)
|
|
@_bindToDomElements()
|
|
@_initializeRenameBox()
|
|
@_initializeDrag()
|
|
return @
|
|
|
|
_bindToDomElements: () ->
|
|
@$nameEl = @$(".name")
|
|
@$inputEl = @$("input.js-rename")
|
|
@$entityListItemEl = @$el.children(".entity-list-item")
|
|
@$labelEl = @$entityListItemEl.children(".entity-label")
|
|
|
|
bindToModel: () ->
|
|
@model.on "change:name", (model) =>
|
|
@$nameEl.text(model.get("name"))
|
|
|
|
hideRenameBox: () ->
|
|
@$nameEl.show()
|
|
@$inputEl.hide()
|
|
|
|
showRenameBox: () ->
|
|
@$nameEl.hide()
|
|
@$inputEl.show()
|
|
|
|
setLabels: (labels) ->
|
|
label = labels[@model.get("id")]
|
|
if label?
|
|
@$entityListItemEl.addClass("show-label")
|
|
@$labelEl.text("±")
|
|
return true
|
|
else
|
|
@$entityListItemEl.removeClass("show-label")
|
|
@$labelEl.text("")
|
|
return false
|
|
|
|
select: () ->
|
|
@selected = true
|
|
@$entityListItemEl.addClass("selected")
|
|
|
|
deselect: () ->
|
|
@selected = false
|
|
@$entityListItemEl.removeClass("selected")
|
|
|
|
isSelected: () ->
|
|
@selected
|
|
|
|
parentOnClick: (e) ->
|
|
doubleClickInterval = 600
|
|
e.preventDefault()
|
|
if @lastClick and new Date() - @lastClick < doubleClickInterval
|
|
@onDoubleClick(e)
|
|
else
|
|
@lastClick = new Date()
|
|
@onClick(e)
|
|
|
|
onDoubleClick: (e) ->
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
if !@readonly
|
|
@startRename()
|
|
|
|
showContextMenuFromCaret: (e) ->
|
|
e.stopPropagation()
|
|
caret = @$(".dropdown-caret")
|
|
offset = caret.offset()
|
|
position =
|
|
top: offset.top + caret.outerHeight()
|
|
right: $(document.body).width() - (offset.left + caret.outerWidth())
|
|
@toggleContextMenu(position)
|
|
|
|
showContextMenuFromRightClick: (e) ->
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
position =
|
|
left: e.pageX
|
|
top: e.pageY
|
|
@showContextMenu(position)
|
|
|
|
toggleContextMenu: (position) ->
|
|
if @contextMenu?
|
|
@contextMenu.destroy()
|
|
else
|
|
@showContextMenu(position)
|
|
|
|
showContextMenu: (position) ->
|
|
entries = @getContextMenuEntries()
|
|
return if !entries?
|
|
|
|
@manager.trigger "contextmenu:beforeshow", @model, entries
|
|
|
|
@contextMenu = new ContextMenu(position, entries)
|
|
@contextMenu.on "destroy", () =>
|
|
delete @contextMenu
|
|
|
|
getContextMenuEntries: () ->
|
|
return null if @readonly
|
|
return [{
|
|
text: "Rename"
|
|
onClick: () =>
|
|
@startRename()
|
|
ga('send', 'event', 'editor-interaction', 'renameEntity', "entityView")
|
|
}, {
|
|
text: "Delete"
|
|
onClick: () =>
|
|
@manager.confirmDelete(@model)
|
|
ga('send', 'event', 'editor-interaction', 'deleteEntity', "entityView")
|
|
}]
|
|
|
|
_initializeDrag: () ->
|
|
@$entityListItemEl.draggable
|
|
delay: 250
|
|
opacity: 0.7
|
|
helper: "clone"
|
|
scroll: true
|
|
|
|
_initializeRenameBox: () ->
|
|
@$inputEl.click (e) -> e.stopPropagation() # Don't stop rename on click in input
|
|
@$inputEl.keydown (event) =>
|
|
code = event.keyCode || event.which
|
|
if code == 13
|
|
@_finishRename()
|
|
@hideRenameBox()
|
|
|
|
startRename: () ->
|
|
if !@renaming
|
|
@renaming = true
|
|
@showRenameBox()
|
|
name = @model.get("name")
|
|
@$inputEl.val(name).focus()
|
|
if @$inputEl[0].setSelectionRange?
|
|
selectionEnd = name.lastIndexOf(".")
|
|
if selectionEnd == -1
|
|
selectionEnd = name.length
|
|
@$inputEl[0].setSelectionRange(0, selectionEnd)
|
|
setTimeout =>
|
|
$(document.body).on "click.entity-rename", () =>
|
|
@_finishRename()
|
|
, 0
|
|
|
|
_finishRename: () ->
|
|
$(document.body).off "click.entity-rename"
|
|
@renaming = false
|
|
name = @$inputEl.val()
|
|
@manager.renameEntity(@model, name)
|
|
@hideRenameBox()
|
|
|
|
makeReadOnly: () ->
|
|
@$entityListItemEl.draggable("disable")
|
|
@readonly = true
|
|
|
|
makeReadWrite: () ->
|
|
@$entityListItemEl.draggable("enable")
|
|
delete @readonly
|
|
|
|
|
|
|