From cfee3e374bcb68b8db80e576382ff306e64b8d35 Mon Sep 17 00:00:00 2001 From: Arnaud SJongers Date: Wed, 13 Jul 2016 15:03:55 +0200 Subject: [PATCH 01/11] fixed index.js failing if used in an addon --- index.js | 9 ++++++--- package.json | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 203a6d2..ef5f8ed 100644 --- a/index.js +++ b/index.js @@ -4,8 +4,11 @@ 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); + if (typeof app.import !== 'function' && app.app) { + app = app.app; + } + app.import(app.bowerDirectory + '/mousetrap/mousetrap.js'); } }; diff --git a/package.json b/package.json index f0ed109..be18568 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-keyboard-shortcuts", - "version": "0.3.0", + "version": "0.3.1", "directories": { "doc": "doc", "test": "tests" From a42463ad9d14161e7ebed1e5173d70742bbd54ff Mon Sep 17 00:00:00 2001 From: Arnaud SJongers Date: Mon, 12 Sep 2016 13:45:18 +0200 Subject: [PATCH 02/11] fix import --- bower.json | 4 +--- package.json | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/bower.json b/bower.json index b4daddd..7c4df2f 100644 --- a/bower.json +++ b/bower.json @@ -11,9 +11,7 @@ "ember-resolver": "~0.1.15", "jquery": "^1.11.1", "loader.js": "ember-cli/loader.js#3.2.0", - "qunit": "~1.17.1" - }, - "devDependencies": { + "qunit": "~1.17.1", "mousetrap": "~1.4.6" } } diff --git a/package.json b/package.json index be18568..e0f0873 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-keyboard-shortcuts", - "version": "0.3.1", + "version": "0.3.2", "directories": { "doc": "doc", "test": "tests" From 62943dcbd8ed2d251db22498c9433bfc66d7b59c Mon Sep 17 00:00:00 2001 From: Arnaud SJongers Date: Mon, 12 Sep 2016 14:16:46 +0200 Subject: [PATCH 03/11] try to fix import --- index.js | 10 +--------- package.json | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index ef5f8ed..3366cb7 100644 --- a/index.js +++ b/index.js @@ -2,13 +2,5 @@ 'use strict'; module.exports = { - name: 'ember-keyboard-shortcuts', - - included: function(app) { - this._super.included.apply(this, arguments); - if (typeof app.import !== 'function' && app.app) { - app = app.app; - } - app.import(app.bowerDirectory + '/mousetrap/mousetrap.js'); - } + name: 'ember-keyboard-shortcuts' }; diff --git a/package.json b/package.json index e0f0873..c6a5e28 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-keyboard-shortcuts", - "version": "0.3.2", + "version": "0.3.3", "directories": { "doc": "doc", "test": "tests" From 87407d50318bf3372dd0c3a62b087e62bf7f8006 Mon Sep 17 00:00:00 2001 From: Arnaud SJongers Date: Tue, 13 Sep 2016 09:57:27 +0200 Subject: [PATCH 04/11] should work now --- bower.json | 5 ++++- index.js | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/bower.json b/bower.json index 7c4df2f..5985596 100644 --- a/bower.json +++ b/bower.json @@ -12,6 +12,9 @@ "jquery": "^1.11.1", "loader.js": "ember-cli/loader.js#3.2.0", "qunit": "~1.17.1", - "mousetrap": "~1.4.6" + "mousetrap": "1.5.3" + }, + "devDependencies": { + "mousetrap": "1.5.3" } } diff --git a/index.js b/index.js index 3366cb7..0393549 100644 --- a/index.js +++ b/index.js @@ -2,5 +2,15 @@ 'use strict'; module.exports = { - name: 'ember-keyboard-shortcuts' + name: 'ember-keyboard-shortcuts', + included: function(app) { + this._super.included.apply(this, arguments); + if (typeof app.import !== 'function' && app.app) { + app = app.app; + } + if (typeof app.import !== 'function' && app.app) { + app = app.app; + } + app.import(app.bowerDirectory + '/mousetrap/mousetrap.js'); + } }; From 4d8d7958b30c760b8754625a4856fccc699043ba Mon Sep 17 00:00:00 2001 From: Arnaud SJongers Date: Tue, 13 Sep 2016 09:59:04 +0200 Subject: [PATCH 05/11] =?UTF-8?q?should=20work=20now=C2=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c6a5e28..00263f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-keyboard-shortcuts", - "version": "0.3.3", + "version": "0.3.4", "directories": { "doc": "doc", "test": "tests" From f9d6e574af4175a25c2d92b407d0ffe85276526b Mon Sep 17 00:00:00 2001 From: Olivier Chatry Date: Wed, 30 Nov 2016 20:45:00 +0100 Subject: [PATCH 06/11] fixes crash if unbind is called before bind --- addon/create-mixin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addon/create-mixin.js b/addon/create-mixin.js index 23dc2c9..bd9f949 100644 --- a/addon/create-mixin.js +++ b/addon/create-mixin.js @@ -5,7 +5,7 @@ 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'); From 30ae696b720c8bf18046dc8daf9f938001099dbd Mon Sep 17 00:00:00 2001 From: Olivier Chatry Date: Thu, 19 Oct 2017 12:28:45 +0200 Subject: [PATCH 07/11] updated to remove bower dependencies --- bower.json | 20 -------------------- index.js | 32 ++++++++++++++++++++++++-------- package.json | 36 +++++++++++++++++++----------------- 3 files changed, 43 insertions(+), 45 deletions(-) delete mode 100644 bower.json diff --git a/bower.json b/bower.json deleted file mode 100644 index 5985596..0000000 --- a/bower.json +++ /dev/null @@ -1,20 +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", - "mousetrap": "1.5.3" - }, - "devDependencies": { - "mousetrap": "1.5.3" - } -} diff --git a/index.js b/index.js index 0393549..63403b7 100644 --- a/index.js +++ b/index.js @@ -1,16 +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) { this._super.included.apply(this, arguments); - if (typeof app.import !== 'function' && app.app) { - app = app.app; - } - if (typeof app.import !== 'function' && app.app) { - app = app.app; + 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); } - app.import(app.bowerDirectory + '/mousetrap/mousetrap.js'); - } -}; + 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 00263f5..cca4135 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-keyboard-shortcuts", - "version": "0.3.4", + "version": "0.3.0", "directories": { "doc": "doc", "test": "tests" @@ -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 From ad0b25bdf102c7c0bf784dd2e2c327d1349ea54e Mon Sep 17 00:00:00 2001 From: Olivier Chatry Date: Thu, 19 Oct 2017 13:33:04 +0200 Subject: [PATCH 08/11] fix hording of event --- addon/create-mixin.js | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/addon/create-mixin.js b/addon/create-mixin.js index bd9f949..be9fc43 100644 --- a/addon/create-mixin.js +++ b/addon/create-mixin.js @@ -7,67 +7,78 @@ export default function(bindEvent, unbindEvent) { return Ember.Mixin.create({ mousetraps:[], bindShortcuts: Ember.on(bindEvent, function() { - var self = this; var shortcuts = this.get('keyboardShortcuts'); if (Ember.typeOf(shortcuts) !== 'object') { return; } this.mousetraps = []; - Object.keys(shortcuts).forEach(function(shortcut) { + Object.keys(shortcuts).forEach((shortcut) => { var actionObject = shortcuts[shortcut]; - var mousetrap = new Mousetrap(document.body); + var mousetrap; var preventDefault = true; function invokeAction(action, eventType) { var type = Ember.typeOf(action); - + var callback; if (type === 'string') { - mousetrap.bind(shortcut, function(){ - self.send(action); + callback = function(){ + this.send(action); return preventDefault !== true; - }, eventType); + } } else if (type === 'function') { - mousetrap.bind(shortcut, action.bind(self), eventType); + callback = action.bind(this) } 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); - + this.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 = [] }) }); From 4f4df339e0237b6a643ecc7d3601316c5091757e Mon Sep 17 00:00:00 2001 From: Olivier Chatry Date: Thu, 19 Oct 2017 13:48:10 +0200 Subject: [PATCH 09/11] changed back this to self --- addon/create-mixin.js | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/addon/create-mixin.js b/addon/create-mixin.js index bd9f949..a0836cb 100644 --- a/addon/create-mixin.js +++ b/addon/create-mixin.js @@ -7,38 +7,38 @@ 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')); @@ -48,26 +48,37 @@ export default function(bindEvent, unbindEvent) { } 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 = [] }) }); From e9da87a9387a0e7bbb6300a70f6158c3eb0eecf1 Mon Sep 17 00:00:00 2001 From: Olivier Chatry Date: Thu, 19 Oct 2017 13:54:36 +0200 Subject: [PATCH 10/11] fix bug where this was the wrong this --- addon/create-mixin.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/addon/create-mixin.js b/addon/create-mixin.js index be9fc43..f174470 100644 --- a/addon/create-mixin.js +++ b/addon/create-mixin.js @@ -7,20 +7,22 @@ export default function(bindEvent, unbindEvent) { return Ember.Mixin.create({ mousetraps:[], bindShortcuts: Ember.on(bindEvent, function() { - var shortcuts = this.get('keyboardShortcuts'); + const shortcuts = this.get('keyboardShortcuts'); - if (Ember.typeOf(shortcuts) !== 'object') { return; } + if (Ember.typeOf(shortcuts) !== 'object') { + return; + } this.mousetraps = []; Object.keys(shortcuts).forEach((shortcut) => { - var actionObject = shortcuts[shortcut]; - var mousetrap; - var preventDefault = true; + const actionObject = shortcuts[shortcut]; + let mousetrap; + let preventDefault = true; - function invokeAction(action, eventType) { - var type = Ember.typeOf(action); - var callback; + const invokeAction = (action, eventType) => { + const type = Ember.typeOf(action); + let callback; if (type === 'string') { callback = function(){ this.send(action); From 7a0cb6f85b0e138c9d18a70b2ecbb25eb669677c Mon Sep 17 00:00:00 2001 From: Olivier Chatry Date: Thu, 19 Oct 2017 13:58:16 +0200 Subject: [PATCH 11/11] fix another this issue --- addon/create-mixin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addon/create-mixin.js b/addon/create-mixin.js index f174470..387a19c 100644 --- a/addon/create-mixin.js +++ b/addon/create-mixin.js @@ -24,7 +24,7 @@ export default function(bindEvent, unbindEvent) { const type = Ember.typeOf(action); let callback; if (type === 'string') { - callback = function(){ + callback = () => { this.send(action); return preventDefault !== true; }