Skip to content

Instantly share code, notes, and snippets.

@zakmac
Last active October 2, 2017 14:56
Show Gist options
  • Save zakmac/3b4032ef70f5a06962f3 to your computer and use it in GitHub Desktop.
Save zakmac/3b4032ef70f5a06962f3 to your computer and use it in GitHub Desktop.
An Ember.js mixin that enables firing an action using multiple trigger events
/**
*
* About: Fire an action in Ember.js using multiple triggers
* Author: Zak MacDonald <http://github.com/zakmac>
* JSBin: http://jsbin.com/tisopu/edit?html,js,output
*
*/
/**
* MultipleActionTriggersMixin
*
* @description a mixin to enable firing a (single) specified action using multiple
* triggers
* @memberof App
* @constructor
* @author Zak MacDonald <http://github.com/zakmac>
* @type external:Ember.Mixin
*/
App.MultipleActionTriggersMixin = Ember.Mixin.create({
// Properties
/**
* actionName
*
* @description a string matching the name of the action to be handled by a trigger
* @memberof App.MultipleActionTriggersMixin
* @instance
* @type {string}
*/
actionName: '',
/**
* actionTriggers
*
* @description space-delimited string identifying which events fire an action
* @memberof App.MultipleActionTriggersMixin
* @instance
* @type {string}
*/
actionTriggers: '',
/**
* actionableEvents
*
* @description a computed property used by ember view actions to determine
* whether to fire an action when that action is called
* @memberof App.MultipleActionTriggersMixin
* @instance
* @type {object}
*/
actionableEvents: Ember.computed('actionTriggers', function() {
var triggers = this.get('actionTriggers').split(' ');
return {
click : triggers.indexOf('click') > -1,
contextMenu: triggers.indexOf('contextMenu') > -1,
doubleClick: triggers.indexOf('doubleClick') > -1,
focusIn : triggers.indexOf('focusIn') > -1,
focusOut : triggers.indexOf('focusOut') > -1,
mouseDown : triggers.indexOf('mouseDown') > -1,
mouseEnter : triggers.indexOf('mouseEnter') > -1,
mouseLeave : triggers.indexOf('mouseLeave') > -1,
mouseMove : triggers.indexOf('mouseMove') > -1,
mouseUp : triggers.indexOf('mouseUp') > -1
};
}),
// Actions
click: function() {
if (this.get('actionableEvents.click')) {
this.send(this.get('actionName'));
}
},
contextMenu: function() {
if (this.get('actionableEvents.contextMenu')) {
this.send(this.get('actionName'));
}
},
doubleClick: function() {
if (this.get('actionableEvents.doubleClick')) {
this.send(this.get('actionName'));
}
},
focusIn: function() {
if (this.get('actionableEvents.focusIn')) {
this.send(this.get('actionName'));
}
},
focusOut: function() {
if (this.get('actionableEvents.focusOut')) {
this.send(this.get('actionName'));
}
},
mouseDown: function() {
if (this.get('actionableEvents.mouseDown')) {
this.send(this.get('actionName'));
}
},
mouseEnter: function() {
if (this.get('actionableEvents.mouseEnter')) {
this.send(this.get('actionName'));
}
},
mouseLeave: function() {
if (this.get('actionableEvents.mouseLeave')) {
this.send(this.get('actionName'));
}
},
mouseMove: function() {
if (this.get('actionableEvents.mouseMove')) {
this.send(this.get('actionName'));
}
},
mouseUp: function() {
if (this.get('actionableEvents.mouseUp')) {
this.send(this.get('actionName'));
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment