/* jshint esnext: true */ import Ember from 'ember'; // When mixed into an Ember.Component class, actions can be explicitly or implicitly delegated to the parent. // In general this makes the most sense for an app-level component, where actions are not mututated as they bubble // up to higher levels. export default Ember.Mixin.create({ // When true, all actions will be implicitly passed on to the parent delegateAllActions: false, // Explicitly delegated actions // example: delegatedActions: ['select', 'show', 'edit', 'destroy'] // May also be declared a as a comma-separated attribute in your template // Example: {{my-component delegatedActions="select,show,edit"}} delegatedActions: [], _delegatedActions: Ember.computed('delegatedActions', function() { 'use strict'; var delegatedActions = this.get('delegatedActions'); if (typeof(delegatedActions) === 'string') { return delegatedActions.split(',').map(s => s.trim()); } else { return delegatedActions; } }), // Override send to call delegatedActions as appropriate // If an action handler is defined, event will not be delegated send(actionName) { 'use strict'; if (!this._actions || !this._actions[actionName]) { if (this.get('delegateAllActions')) { // Delegate all actions Ember.Logger.debug(`Action delegated implicitly by ${this.toString()}:`, actionName); return this.delegateAction.apply(this, arguments); } else { Ember.Logger.warn(`Actions not delegated by ${this.toString()}:`, actionName); } var delegatedActions = this.get('_delegatedActions'); if (delegatedActions.contains(actionName) && this.sendAction) { Ember.Logger.info(`Action delegated by ${this.toString()}:`, actionName); return this.delegateAction.apply(this, arguments); } else { Ember.Logger.warn(`Action not delegated by ${this.toString()}:`, actionName); } } return this._super.apply(this, arguments); }, // Trigger action explicitly delegateAction(actionName, ...contexts) { 'use strict'; Ember.Logger.debug("delegateAction", actionName, contexts); this.triggerAction({ action: actionName, actionContext: contexts }); return; } });