62 lines
2.3 KiB
CoffeeScript
62 lines
2.3 KiB
CoffeeScript
@App.modals.BaseController = Ember.ObjectController.extend
|
|
needs: ['application']
|
|
alert_message: ""
|
|
title: (->
|
|
# return title if directly set by options
|
|
return @get('modal_options.title') if @get('modal_options.title')
|
|
# return translated title_path if directly set by controller
|
|
translation_params = {}
|
|
if model = @get('model')
|
|
translation_params = model.serialize() if model.serialize
|
|
|
|
return tspan(@title_path, translation_params).htmlSafe() if @title_path
|
|
# return translated title_path if directly set by options
|
|
return tspan(@get('modal_options.title_path'), translation_params).htmlSafe() if @get('modal_options.title_path')
|
|
# infer title path based on controller name App.modals.AddSectionController => add_section
|
|
underscored = `this.constructor.toString().substr(11).replace(/Controller$/, '').underscore()`
|
|
# find translated title or humanize the controller name
|
|
if convention_translation = ttry("modal.#{underscored}.title", translation_params)
|
|
tspan(@get("modal.#{underscored}.title"), translation_params).htmlSafe()
|
|
else
|
|
underscored.capitalize().replace(/_/, ' ')
|
|
).property('model.id')
|
|
save_error: (error)->
|
|
switch error.status
|
|
when 403
|
|
@set 'alert_message', 'Unauthorized action'
|
|
else
|
|
@set 'alert_message', 'Something went wrong'
|
|
save_success: ->
|
|
@set 'alert_message', ''
|
|
@send 'closeModal'
|
|
|
|
actions:
|
|
close: ->
|
|
if close = @get('modal_options.close')
|
|
close.apply(@)
|
|
@set 'alert_message', ''
|
|
@send 'closeModal' unless @preventClose
|
|
closeOnOverlay: ->
|
|
@send('close') if @get('modal_options.closeOnOverlay')
|
|
false
|
|
modalClick: ->
|
|
@send('close') if @get('modal_options.closeOnModalClick')
|
|
false
|
|
ok: ->
|
|
if ok = @get('modal_options.ok')
|
|
ok.apply(@)
|
|
@set 'alert_message', ''
|
|
@send 'closeModal' unless @preventClose
|
|
confirm: -> @send('ok')
|
|
save: ->
|
|
if @get('model.isValid')
|
|
@get('model').save().then @save_success.bind(@), @save_error.bind(@)
|
|
#@send 'closeModal' unless @preventClose
|
|
destroy: ->
|
|
@modal 'confirm',
|
|
title_path: @get('modal_options.destroy_text_path') || 'general.destroy.text'
|
|
model: @get('model')
|
|
ok: ->
|
|
@get('model').destroyRecord()
|
|
@send 'closeModal' unless @preventClose
|