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

66 lines
1.4 KiB
CoffeeScript
Raw Normal View History

2014-02-12 10:23:40 +00:00
define [
"libs/backbone"
"libs/mustache"
], () ->
Modal = Backbone.View.extend {
templates:
modal: $("#genericModalTemplate").html()
button: $("#genericModalButtonTemplate").html()
initialize: () ->
@render()
@options.buttons ||= []
self = @
for buttonOptions in @options.buttons
do (buttonOptions) ->
button = $(Mustache.to_html self.templates.button, buttonOptions)
self.$(".modal-footer").prepend button
button.on "click", (e) ->
e.preventDefault()
if buttonOptions.callback?
2014-03-11 12:13:46 +00:00
buttonOptions.callback(button)
if !buttonOptions.close? or buttonOptions.close
self.remove()
2014-02-12 10:23:40 +00:00
@$el.modal
# make sure we control when the modal is hidden
keyboard: false
backdrop: "static"
@$el.on "hidden", () => @remove()
if @options.clearBackdrop
$(".modal-backdrop").addClass("clear-modal-backdrop")
@$el.find('input').on "keydown", (event)->
code = event.keyCode || event.which
if code == 13
self.$el.find('.btn-primary').click()
@$el.find('input').focus()
remove: () ->
@$el.modal("hide")
Backbone.View.prototype.remove.call(this)
render: () ->
@setElement $(
Mustache.to_html @templates.modal, @options
)
if @options.el?
@$(".message").append @options.el
$(document.body).append(@$el)
}, {
createModal: (options) ->
new Modal(options)
}
return Modal