Skip to content

Commit

Permalink
Merge pull request #18 from XvisionAS/master
Browse files Browse the repository at this point in the history
many different fixes
  • Loading branch information
olivierchatry authored Oct 19, 2017
2 parents 3a18a1f + 4732d71 commit 13517a5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 58 deletions.
47 changes: 29 additions & 18 deletions addon/create-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,80 @@ import Ember from 'ember';
export default function(bindEvent, unbindEvent) {

return Ember.Mixin.create({

mousetraps:[],
bindShortcuts: Ember.on(bindEvent, function() {
var self = this;
var shortcuts = this.get('keyboardShortcuts');

const shortcuts = this.get('keyboardShortcuts');
const self = this;
if (Ember.typeOf(shortcuts) !== 'object') { return; }

this.mousetraps = [];

Object.keys(shortcuts).forEach(function(shortcut) {
var actionObject = shortcuts[shortcut];
var mousetrap = new Mousetrap(document.body);
var preventDefault = true;
const actionObject = shortcuts[shortcut];
let mousetrap;
let preventDefault = true;

function invokeAction(action, eventType) {
var type = Ember.typeOf(action);

var callback;
if (type === 'string') {
mousetrap.bind(shortcut, function(){
callback = function(){
self.send(action);
return preventDefault !== true;
}, eventType);
}
}
else if (type === 'function') {
mousetrap.bind(shortcut, action.bind(self), eventType);
callback = action.bind(self)
}
else {
throw new Error('Invalid value for keyboard shortcut: ' + action);
}
mousetrap.bind(shortcut, callback, eventType);
}

if (Ember.typeOf(actionObject) === 'object') {
if (actionObject.global === false) {
mousetrap = new Mousetrap(document);
mousetrap = new Mousetrap(document.body);
} else if (actionObject.scoped) {
if (Ember.typeOf(actionObject.scoped) === 'boolean') {
mousetrap = new Mousetrap(self.get('element'));
mousetrap = new Mousetrap(this.get('element'));
} else if (Ember.typeOf(actionObject.scoped) === 'string') {
mousetrap = new Mousetrap(document.querySelector(actionObject.scoped));
}
} else if (actionObject.targetElement) {
mousetrap = new Mousetrap(actionObject.targetElement);
}

if (actionObject.preventDefault === false) {
preventDefault = false;
}

invokeAction(actionObject.action, actionObject.eventType);
} else {
mousetrap = new Mousetrap(document.body);
invokeAction(actionObject);
}

self.mousetraps.push(mousetrap);

});

}),

unbindShortcuts: Ember.on(unbindEvent, function() {
const _removeEvent = (object, type, callback) => {
if (object.removeEventListener) {
object.removeEventListener(type, callback, false);
return;
}
object.detachEvent('on' + type, callback);
}
this.mousetraps.forEach(
(mousetrap) => mousetrap.reset()
(mousetrap) => {
// manually unbind JS event
_removeEvent(mousetrap.target, 'keypress', mousetrap._handleKeyEvent);
_removeEvent(mousetrap.target, 'keydown', mousetrap._handleKeyEvent);
_removeEvent(mousetrap.target, 'keyup', mousetrap._handleKeyEvent);
mousetrap.reset()
}
);
this.mousetraps = []
})

});
Expand Down
19 changes: 0 additions & 19 deletions bower.json

This file was deleted.

31 changes: 26 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
/* jshint node: true */
'use strict';
var path = require('path');
var resolve = require('resolve');
var Funnel = require('broccoli-funnel');
var mergeTrees = require('broccoli-merge-trees');
var fastbootTransform = require('fastboot-transform');

module.exports = {
name: 'ember-keyboard-shortcuts',

included: function(app, parentAddon) {
var target = (parentAddon || app);
target.import(app.bowerDirectory + '/mousetrap/mousetrap.js');
}
};
included: function(app) {
this._super.included.apply(this, arguments);
app.import('vendor/mousetrap/mousetrap.js');
},

treeForVendor: function(tree) {
var trees = [];
var mousetrap = fastbootTransform(new Funnel(this.pathBase('mousetrap'), {
destDir: 'mousetrap'
}));
trees = trees.concat([mousetrap]);
if (tree) {
trees.push(tree);
}
return mergeTrees(trees);
},

pathBase: function(packageName) {
return path.dirname(resolve.sync(packageName + '/package.json', { basedir: __dirname }));
},
};
34 changes: 18 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,33 @@
"author": "Trym Skaar <[email protected]>",
"license": "MIT",
"devDependencies": {
"broccoli-asset-rev": "^2.0.2",
"ember-cli": "0.2.3",
"ember-cli-app-version": "0.3.3",
"ember-cli-content-security-policy": "0.4.0",
"ember-cli-dependency-checker": "0.0.8",
"ember-cli-htmlbars": "0.7.4",
"ember-cli-ic-ajax": "0.1.1",
"ember-cli-inject-live-reload": "^1.3.0",
"ember-cli-qunit": "0.3.10",
"ember-cli-uglify": "1.0.1",
"ember-data": "1.0.0-beta.16.1",
"ember-export-application-global": "^1.0.2",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-try": "0.0.4"
"broccoli-asset-rev": "~2.6.0",
"ember-cli": "~2.16.2",
"ember-source": "~2.16.0",
"ember-cli-app-version": "~3.1.0",
"ember-cli-content-security-policy": "~1.0.0",
"ember-cli-dependency-checker": "~2.0.1",
"ember-cli-htmlbars": "~2.0.3",
"ember-cli-ic-ajax": "~1.0.0",
"ember-cli-inject-live-reload": "~1.7.0",
"ember-cli-qunit": "~4.0.1",
"ember-cli-uglify": "~2.0.0",
"ember-export-application-global": "~2.0.0",
"ember-disable-prototype-extensions": "~1.1.3",
"ember-try": "~0.2.17"
},
"description": "ember-cli addon that uses mousetrap to facilitate keyboard shortcuts",
"keywords": [
"ember-addon",
"mousetrap"
],
"dependencies": {
"ember-cli-babel": "^5.0.0"
"ember-cli-babel": "~6.8.2",
"mousetrap": "~1.6.1",
"fastboot-transform": "~0.1.2"
},
"ember-addon": {
"configPath": "tests/dummy/config",
"defaultBlueprint": "ember-keyboard-shortcuts"
}
}
}

0 comments on commit 13517a5

Please sign in to comment.