diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 1722ab68c..7399a9eb5 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -26,3 +26,4 @@ window.createKeyEvent = (pattern) -> shiftKey: 'shift' in keys metaKey: 'meta' in keys which: key + diff --git a/spec/stdlib/template-spec.coffee b/spec/stdlib/template-spec.coffee index 35649b5bb..458d9bb4c 100644 --- a/spec/stdlib/template-spec.coffee +++ b/spec/stdlib/template-spec.coffee @@ -89,3 +89,26 @@ describe "Template", -> expect(view.subview.view()).toBe view.subview expect(view.subview.header.view()).toBe view.subview + describe "$.fn.bindKey", -> + describe "when passed a key pattern and a method name", -> + it "calls the named method on the parent view when a keydown event matches the pattern", -> + view.someMethod = jasmine.createSpy('someMethod') + view.li1.bindKey 'ctrl+m', 'someMethod' + + view.li1.trigger window.createKeyEvent('meta+z') + expect(view.someMethod).not.toHaveBeenCalled() + + view.li1.trigger window.createKeyEvent('ctrl+m') + expect(view.someMethod).toHaveBeenCalled() + + describe "when passed a key pattern and a function", -> + it "calls the given function when a keydown event matches the pattern", -> + action = jasmine.createSpy('someMethod') + view.li1.bindKey 'ctrl+m', action + + view.li1.trigger window.createKeyEvent('meta+z') + expect(action).not.toHaveBeenCalled() + + view.li1.trigger window.createKeyEvent('ctrl+m') + expect(action).toHaveBeenCalled() + diff --git a/src/atom/file-finder.coffee b/src/atom/file-finder.coffee index c5ef93f67..75b2689fa 100644 --- a/src/atom/file-finder.coffee +++ b/src/atom/file-finder.coffee @@ -18,11 +18,6 @@ class FileFinder extends Template @bindKey 'up', 'moveUp' @bindKey 'down', 'moveDown' - bindKey: (pattern, methodName) -> - @on 'keydown', (event) => - if window.keyEventMatchesPattern(event, pattern) - this[methodName]() - populateUrlList: -> @urlList.empty() for url in @findMatches(@input.val()) diff --git a/src/stdlib/template.coffee b/src/stdlib/template.coffee index cac08434f..c0984fb07 100644 --- a/src/stdlib/template.coffee +++ b/src/stdlib/template.coffee @@ -51,3 +51,11 @@ class Template $.fn.view = -> this.data('view') +$.fn.bindKey = (pattern, action) -> + @on 'keydown', (event) => + if window.keyEventMatchesPattern(event, pattern) + if _.isString(action) + this.view()[action]() + else + action() +