import Ember from 'ember' import config from 'ember-get-config' import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin' export default Ember.Mixin.create ApplicationRouteMixin, current_user_loaded: (user) -> @get('initial_resources_resolve')(user) router: Ember.inject.service() beforeModel: (transition) -> @set 'globals.initial_resources_fulfilled', false time_now = new Date() console.log "Application beforeModel" initial_resources_loaded = new Ember.RSVP.Promise((resolve, reject) => @set 'initial_resources_resolve', resolve @set 'initial_resources_reject', reject ).then((resources) => console.log "Initial resources loaded (#{Math.round((new Date() - time_now) / 100) / 10}s)" # rounded devide by 1000 @set 'globals.initial_resources_fulfilled', true ).catch((reason) => console.log "No initial resource loading (#{Math.round((new Date() - time_now) / 100) / 10}s)" # rounded devide by 1000 @set('globals.flash_message', reason) if reason ).finally -> Ember.$('#initial-loader').remove() @set 'initial_resources_loaded_promise', initial_resources_loaded @_loadCurrentUser arguments... initial_resources_loaded sessionAuthenticated: -> @_super arguments... @_loadCurrentUser() _loadCurrentUser: (transition) -> #if userId = @get('session.data.authenticated.user_id') if transition?.targetName is 'login' # don't load current user on login page @get('initial_resources_resolve')() # do not halt propagation return @get('store').queryRecord('user', me: true).then (user) => @set 'globals.current_user', user @current_user_loaded user # overloadable , => @set 'globals.current_user', null @get('initial_resources_resolve')() # no initial resources without user, do not halt application propagation (login page) @transitionTo('login') actions: error: (error, maybeTransition) -> return unless @connections?.length # cannot use outlet at this moment console.log error container = Ember.getOwner(@) if status = error.status || error?.errors?[0].status switch parseInt(status) when 401, 403 @set 'globals.current_user', null @transitionTo 'login' return #when 404 # # nothing for now apparently # #maybeTransition?.abort?() # #@transitionTo 'four-oh-four' # #return controller = container.lookup "component:error-handling" return unless controller?.set controller.set 'error', error @render 'global/error-messages', into: 'application' outlet: 'error-handling' controller: controller #@set "globals.flash_message", "Oops! Something went wrong!
#{error.message}".htmlSafe() clear_error: -> #@disconnectOutlet # https://github.com/emberjs/ember-inspector/issues/625 # outlet: 'error-handling' # parentView: 'application' @render 'modals/nothing', outlet: 'error-handling' into: 'application' controller: 'application' @set 'globals.modal_active', false clear_error_and_go_home: -> @send 'clear_error' @transitionTo 'index' didTransition: -> @_super arguments... Ember.run.next => return unless path = @get('router.currentRouteName') @set 'globals.current_route', Ember.Object.create path: path # a.b.c node: path.replace(/.*\./, '') # c primary: '.' in path openModal: (modalName, options={}) -> if typeof options is 'function' console.warn "Modal #{modalName} call has function as argument in stead of options!" return else if options.promise? and options.content options = {model: options.content} else if typeof options isnt 'object' options = {model: options} # opening a model on an unresolved promise is a bad thing, so we can assume (model) promise content for non paginated arrays as model options.model = options.model.content if options.model?.promise? and options.model.content and not options.model.dunlop_remote_array container = Ember.getOwner(@) console.log "MODAL: #{modalName}" modalNameParts = modalName.split('/') partialName = modalNameParts.pop() controller = container.lookup "component:modals/#{modalName}" if not controller and modalNameParts.length controller = container.lookup "component:#{modalNameParts.join('/')}/modals/#{partialName}" controller ||= container.lookup "component:#{modalName}" controller ||= container.lookup('component:modals/base') #try # controller = @controllerFor("modals/#{modalName}") #catch error # controller = @controllerFor("modals/base") #controller ||= @controllerFor("modals/base") controller.set 'model', options.model defaultModalOptions = closeOnOverlay: true closeOnModalClick: false modal_options = Ember.Object.create($.extend(defaultModalOptions, options)) controller.set 'modal_options', modal_options # use root level partials for wizards and elementary use case # Try to resolve the modalName traversing up the hierarchy so a modalName # of wizard/create-distribution-point/copper/edit-distribution-point will look for partials: # # wizard/create-distribution-point/copper/-edit-distribution-point # wizard/create-distribution-point/copper/modals/-edit-distribution-point # modals/wizard/create-distribution-point/copper/-edit-distribution-point # # wizard/create-distribution-point/-edit-distribution-point # wizard/create-distribution-point/modals/-edit-distribution-point # modals/wizard/create-distribution-point/-edit-distribution-point # # wizard/-edit-distribution-point # wizard/modals/-edit-distribution-point # modals/wizard/-edit-distribution-point # # -edit-distribution-point # modals/-edit-distribution-point # unless controller.get('partialName') partialPath = null while not partialPath and modalNameParts.length if container.lookup "template:#{modalNameParts.join('/')}/-#{partialName}" partialPath = "#{modalNameParts.join('/')}/#{partialName}" # without the dash else if container.lookup "template:#{modalNameParts.join('/')}/modals/-#{partialName}" partialPath = "#{modalNameParts.join('/')}/modals/#{partialName}" # without the dash else if container.lookup "template:modals/#{modalNameParts.join('/')}/modals/-#{partialName}" partialPath = "modals/#{modalNameParts.join('/')}/modals/#{partialName}" # without the dash else if container.lookup "template:components/#{modalNameParts.join('/')}/-#{partialName}" partialPath = "components/#{modalNameParts.join('/')}/#{partialName}" # without the dash modalNameParts.pop() partialPath ||= partialName if container.lookup("template:-#{partialName}") # can be root template partialPath ||= "modals/#{partialName}" if container.lookup("template:modals/-#{partialName}") # can be root template unless partialPath message = "Cannot find template for modal '#{modalName}'" console.log message @set "globals.flash_message", message return controller.set 'partialName', partialPath @incrementProperty 'globals.modals_counter' controller.privateWillOpenModal() # setup expected attributes to be used in willOpenModal controller.willOpenModal() # can be implemented to setup stuff before rendering and hooks @render 'modals/layout', into: 'application' outlet: 'modal' controller: controller @set 'globals.modal_active', true false # return false since this result will in most cases the the last statement of an action. So prevent bubbling closeModal: -> #@disconnectOutlet # https://github.com/emberjs/ember-inspector/issues/625 # outlet: 'modal' # parentView: 'application' @render 'modals/nothing', outlet: 'modal' into: 'application' controller: 'application' @set 'globals.modal_active', false signOut: -> @set 'globals.current_user', null @get('session').invalidate() if @get('session.isAuthenticated') @transitionTo 'login' try $.ajax url: "#{config.apiHost}/users/sign_out" type: 'DELETE' dataType: 'json' destroyRecord: (model, options = {}) -> return unless model model = model.get("_content") if model.__changeset__ @send 'openModal', 'confirm', title_path: options.destroy_text_path || 'general.destroy.are_you_sure' model: model body: options.body ok: -> @get('model').destroyRecord() @send 'closeModal' unless @preventClose false reload: (target) -> target?.reload?() false