|
1 | 1 | # Aurelia Keyboard Plugin |
2 | | -This is a plugin to easily manage keyboard binding within an aurelia application. |
| 2 | +This is an easy to use keyboard navigation plugin for aurelia. It is based on the popular mousetrap javascript library. |
| 3 | + |
| 4 | +## Installing the plugin |
| 5 | +1. Ensure that [NodeJS](http://nodejs.org/) and jspm is installed. This provides the platform on which the build tooling runs. |
| 6 | +2. Install jspm. |
| 7 | +3. Install aurelia |
| 8 | +4. Install aurelia-keyboard-navigation |
| 9 | + |
| 10 | +```shell |
| 11 | + jspm install github:novatrox/aurelia-keyboard-plugin |
| 12 | +``` |
| 13 | + |
| 14 | +5. Configure aurelia-keyboard-navigation |
| 15 | + |
| 16 | +```shell |
| 17 | + import { AKPConfiguration } from 'aurelia-keyboard-plugin'; |
| 18 | + |
| 19 | + aurelia.use.plugin('novatrox/aurelia-keyboard-plugin', (config: AKPConfiguration) => { |
| 20 | + config.useDefaults(); |
| 21 | + }); |
| 22 | +``` |
| 23 | +Possible configuration options are: value(default setting) |
| 24 | +* preventBubbleClass('akp-nobubble') : Class name to give inputs that should bypass keys bound by plugin |
| 25 | +* defaultPreventInputBubble(false): Should inputs not respond to binding by default |
| 26 | +* defaultPrevent(false): Should callbacks fire e.preventDefault(); by default |
| 27 | +* defaultGlobal(true): Should keybinds be global by default |
| 28 | +
|
| 29 | +6. You are done. Read the tutorial to learn how to use the plugin |
| 30 | +
|
| 31 | +
|
| 32 | +## Using the plugin |
| 33 | +To understand the trigger combinations. Read more about Mousetrap on [https://craig.is/killing/mice](https://craig.is/killing/mice) |
| 34 | +
|
| 35 | +The plugin exposes a global custom attribute called keybind="", it has several properties to bind to. |
| 36 | +* trigger: Which keys should trigger |
| 37 | +* delegate: Callback, when no delegate is defined element.click() is default. |
| 38 | +* prevent: Should call e.preventDefault(); (default false) |
| 39 | +* global: Is global, otherwise will only fire when scope is in focus (inputs) (default true) |
| 40 | +
|
| 41 | +
|
| 42 | +### Tutorial |
| 43 | +
|
| 44 | +*Simple keybind to make enter click your button, because no delegate is given the plugin will fire element.click()* |
| 45 | +```shell |
| 46 | + <form> |
| 47 | + <input type="text" value="Something to post"/> |
| 48 | + <button type="submit" keybind="trigger: enter;">Submit form</submit> |
| 49 | + </form> |
| 50 | +``` |
| 51 | +
|
| 52 | +
|
| 53 | +*Simple keybind to perform search when in search input scope, because global is set to false it will only trigger when focus is inside the input* |
| 54 | +
|
| 55 | +```shell |
| 56 | +<input type="text" placeholder="Search for something" keybind="trigger: enter; delegate.call: doSearch(); global: false;" /> |
| 57 | +``` |
| 58 | +
|
| 59 | +*Simple keybind to navigate, when args is defined the plugin passes a KeyboardEvent parameter to the delegate, the parameter must be called args.* |
3 | 60 |
|
4 | 61 | ```shell |
5 | | - <form keybind="trigger: enter, delegate.call: submitHandler();"> |
6 | | - <input type="text" placeholder="Something to post" /> |
7 | | - <button type="submit">Submit</button> |
8 | | - <button keybind="trigger: escape, delegate.call: cancel();">Cancel</button> |
9 | | - </form> |
| 62 | +**view.html** |
| 63 | + |
| 64 | +<table keybind="trigger: up, down; delegate.call: doNavigation(args);> |
| 65 | +<tbody repeat.for="item of items"> |
| 66 | +<tr> |
| 67 | +<td>${item.id}</td> |
| 68 | +<td>Selected: ${item.selected}</td> |
| 69 | +</tr> |
| 70 | +</table> |
| 71 | +
|
| 72 | +**view.ts* |
| 73 | +doNavigation(args) { |
| 74 | + let currentIndex = this.items.map(function (i) { |
| 75 | + return i.selected; |
| 76 | + }).indexOf(true); |
| 77 | + |
| 78 | + let isDown = args.keyCode === 40; |
| 79 | + items[currentIndex].selected = false; |
| 80 | + if (isDown) { |
| 81 | + items[++currentIndex].selected = true; |
| 82 | + } else { |
| 83 | + items[--currentIndex].selected = true; |
| 84 | + } |
| 85 | +} |
| 86 | +
|
| 87 | +``` |
| 88 | +
|
| 89 | +*Simple keybind to perform save. Prevent is set to true to prevent webbrowser from opening default save dialog.* |
| 90 | +```shell |
| 91 | + <textarea keybind="trigger: ctrl+s; delegate.call: silentSave(); prevent: true; global:false;"></textarea> |
10 | 92 | ``` |
11 | 93 |
|
12 | 94 |
|
| 95 | +
|
| 96 | +
|
| 97 | +
|
| 98 | +
|
13 | 99 | # Based on aurelia-skeleton-plugin |
14 | 100 |
|
15 | 101 | [](https://zenhub.io) |
|
0 commit comments