diff --git a/addon/create-mixin.js b/addon/create-mixin.js index 23dc2c9..24cbfc0 100644 --- a/addon/create-mixin.js +++ b/addon/create-mixin.js @@ -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 = [] }) }); diff --git a/bower.json b/bower.json deleted file mode 100644 index b4daddd..0000000 --- a/bower.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "ember-keyboard-shortcuts", - "dependencies": { - "ember": "1.11.3", - "ember-cli-shims": "ember-cli/ember-cli-shims#0.0.3", - "ember-cli-test-loader": "ember-cli-test-loader#0.1.3", - "ember-data": "1.0.0-beta.16.1", - "ember-load-initializers": "ember-cli/ember-load-initializers#0.1.4", - "ember-qunit": "0.3.1", - "ember-qunit-notifications": "0.0.7", - "ember-resolver": "~0.1.15", - "jquery": "^1.11.1", - "loader.js": "ember-cli/loader.js#3.2.0", - "qunit": "~1.17.1" - }, - "devDependencies": { - "mousetrap": "~1.4.6" - } -} diff --git a/index.js b/index.js index 203a6d2..63403b7 100644 --- a/index.js +++ b/index.js @@ -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 })); + }, +}; \ No newline at end of file diff --git a/package.json b/package.json index f0ed109..cca4135 100644 --- a/package.json +++ b/package.json @@ -30,20 +30,20 @@ "author": "Trym Skaar ", "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": [ @@ -51,10 +51,12 @@ "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" } -} +} \ No newline at end of file