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

67 lines
1.4 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
define [
"libs/backbone"
"libs/mustache"
"libs/bootstrap/bootstrap2full"
], () ->
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 08:13:46 -04:00
buttonOptions.callback(button)
if !buttonOptions.close? or buttonOptions.close
self.remove()
2014-02-12 05:23:40 -05: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