150 lines
6.4 KiB
CoffeeScript
150 lines
6.4 KiB
CoffeeScript
import Ember from 'ember'
|
|
export default Ember.Component.extend
|
|
store: Ember.inject.service()
|
|
|
|
alert_message: ""
|
|
modal_options: {}
|
|
layoutName: 'modals/layout'
|
|
willOpenModal: -> # can be implemented, is fired after loading attributes, before rendering
|
|
is_saving: false # temporary state to prevent double triggering
|
|
class: '' # manual set or component name derived
|
|
component_name_based_class: ''
|
|
privateWillOpenModal: ->
|
|
component_name = @toString().replace(/.*(controller|component):/, '').replace(/::ember.*$/, '').replace(/\W+$/, '')
|
|
@set 'component_name', component_name
|
|
@set 'component_name_based_class', '' + @get('class') + ' ' + component_name.replace(/\//g, ' modal-')
|
|
|
|
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 title_params = @get('modal_options.title_params')
|
|
translation_params = title_params
|
|
else if model = @get('model')
|
|
if model.toJSON
|
|
translation_params = model.toJSON()
|
|
else if model.__changeset__ and model._content?.toJSON
|
|
translation_params = model._content.toJSON()
|
|
translation_params.id = model.id if model.id
|
|
|
|
# return translated title_path if directly set by options
|
|
# return translated title_path if directly set by controller
|
|
if title_path = @get('modal_options.title_path') || @get('title_path')
|
|
return tspan(title_path, translation_params).htmlSafe()
|
|
|
|
translation_params.emptyWhenNotFound = true # allow for fallbacks
|
|
|
|
partial_title_path = "#{@get('partialName').replace(/-/g, '_').replace(/\W/g, '.')}.title"
|
|
return partialNameTranslation.htmlSafe() if partialNameTranslation = tspan(partial_title_path, translation_params)
|
|
|
|
# @toString() => <frontend@component:workflow-action-definition/modals/edit-workflow-action-definition::ember873>
|
|
underscored = @get('component_name').underscore().replace(/\//g, '.')
|
|
return convention_translation.htmlSafe() if convention_translation = tspan("#{underscored}.title", translation_params)
|
|
|
|
# Return a kind of humanized version of the title
|
|
underscored.replace(/.*\./,'').capitalize().replace(/_/, ' ')
|
|
).property('model.id', 'modal_options.title_path')
|
|
body: (->
|
|
@get('modal_options.body')
|
|
).property('modal_options.body')
|
|
|
|
save_error: (error)->
|
|
@set 'is_saving', false
|
|
if typeof error is 'string'
|
|
@set 'alert_message', error
|
|
else if error?.status is 403 or error?.status is 401
|
|
@set 'alert_message', 'Unauthorized action'
|
|
else
|
|
@set 'alert_message', 'Something went wrong'
|
|
|
|
save_success: ->
|
|
@set 'is_saving', false
|
|
@set 'alert_message', ''
|
|
pre_hook_modals_count = @get('globals.modals_counter')
|
|
if save_callback = @get('modal_options.save')
|
|
save_callback.apply(@, arguments)
|
|
after_hook_modals_count = @get('globals.modals_counter')
|
|
@send 'closeModal' unless after_hook_modals_count > pre_hook_modals_count # when modals count changed, another modal is opened by the hook. Do not close
|
|
|
|
actions:
|
|
close: ->
|
|
pre_hook_modals_count = @get('globals.modals_counter')
|
|
if close = @get('modal_options.close')
|
|
close.apply(@)
|
|
after_hook_modals_count = @get('globals.modals_counter')
|
|
unless @preventClose or after_hook_modals_count > pre_hook_modals_count # when modals count changed, another modal is opened by the hook. Do not close
|
|
@set 'alert_message', ''
|
|
@send 'closeModal'
|
|
closeOnOverlay: ->
|
|
@send('rollback_and_close') if @get('modal_options.closeOnOverlay')
|
|
false
|
|
modalClick: ->
|
|
@send('close') if @get('modal_options.closeOnModalClick')
|
|
false
|
|
ok: ->
|
|
if ok = @get('modal_options.ok')
|
|
ok.call(@)
|
|
@set 'alert_message', ''
|
|
@send 'closeModal' unless @preventClose
|
|
confirm: -> @send('ok')
|
|
saveRecord: ->
|
|
return if @get('is_saving') # prevent click enthusiasts from creating to many instances or things like that
|
|
@set 'is_saving', true
|
|
model = @get("model")
|
|
if typeof model.validate is 'function'
|
|
model.validate() # validate if method exists
|
|
if changeset = model.get('changeset')
|
|
changeset.validate()
|
|
if changeset.get('isValid')
|
|
changeset.save().then @save_success.bind(@), @save_error.bind(@)
|
|
else
|
|
@set 'is_saving', false
|
|
else
|
|
if model.get('isValid')
|
|
model.save().then @save_success.bind(@), @save_error.bind(@)
|
|
#@send 'closeModal' unless @preventClose
|
|
else
|
|
console.log "[VALIDATION ERRORS]"
|
|
@set 'is_saving', false
|
|
console.log model.get("errors")
|
|
|
|
rollback_and_close: ->
|
|
if model = @get("model")
|
|
if model.__changeset__
|
|
model.rollback()
|
|
if model.get("_content.isNew") # delete new records on modal rollback
|
|
model.get("_content").deleteRecord() #NOTE: maybe this should be unloadRecord --force=true (state/dirty tracking issues)
|
|
else
|
|
model.rollbackAttributes() if typeof model.rollbackAttributes is 'function'
|
|
@send 'close'
|
|
|
|
destroyRecord: ->
|
|
my_scope = @
|
|
destroy_callback = @get('modal_options.destroy_callback') || ->
|
|
# transition to top level route if current is nested inside a model.show route
|
|
if modelName = my_scope.get('model.constructor.modelName')
|
|
flat_model_name = modelName.replace(/^\w+\//, '').replace('/', '.') # panda/projec => project
|
|
if my_scope.get('globals.current_route.path')?.match("^#{flat_model_name}.show")
|
|
my_scope.get('router').transitionTo flat_model_name
|
|
|
|
if @get('model.isNew')
|
|
model = @get('model')
|
|
model = model.get("_content") if model.__changeset__
|
|
model.destroyRecord().then -> destroy_callback.call(my_scope, model)
|
|
@send 'closeModal' unless @preventClose
|
|
else
|
|
@modal 'confirm',
|
|
title_path: @get('modal_options.destroy_text_path') || 'general.destroy.are_you_sure'
|
|
model: @get('model')
|
|
ok: ->
|
|
model = @get("model")
|
|
model = model.get("_content") if model.__changeset__
|
|
model.destroyRecord().then -> destroy_callback.call(my_scope, model)
|
|
@send 'closeModal' unless @preventClose
|
|
|
|
closeModal: ->
|
|
container = Ember.getOwner(@)
|
|
ar = container.lookup('route:application')
|
|
ar.send 'closeModal'
|