Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | Sample view to illustrate key bindings. |
| 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function (onos) { |
| 24 | 'use strict'; |
| 25 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 26 | var keyDispatch = { |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 27 | Z: keyUndo, |
| 28 | X: keyCut, |
| 29 | C: keyCopy, |
| 30 | V: keyPaste, |
| 31 | space: keySpace |
| 32 | }; |
| 33 | |
| 34 | function keyUndo(view) { |
| 35 | note(view, 'Z = UNDO'); |
| 36 | } |
| 37 | |
| 38 | function keyCut(view) { |
| 39 | note(view, 'X = CUT'); |
| 40 | } |
| 41 | |
| 42 | function keyCopy(view) { |
| 43 | note(view, 'C = COPY'); |
| 44 | } |
| 45 | |
| 46 | function keyPaste(view) { |
| 47 | note(view, 'V = PASTE'); |
| 48 | } |
| 49 | |
| 50 | function keySpace(view) { |
| 51 | note(view, 'The SpaceBar'); |
| 52 | } |
| 53 | |
| 54 | function note(view, msg) { |
| 55 | view.$div.append('p') |
| 56 | .text(msg) |
| 57 | .style({ |
| 58 | 'font-size': '10pt', |
| 59 | color: 'darkorange', |
| 60 | padding: '0 20px', |
| 61 | margin: 0 |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | function keyCallback(view, key, keyCode, event) { |
| 66 | note(view, 'Key = ' + key + ' KeyCode = ' + keyCode); |
| 67 | } |
| 68 | |
Simon Hunt | 0df1b1d | 2014-11-04 22:58:29 -0800 | [diff] [blame] | 69 | function load(view, ctx) { |
| 70 | // this maps specific keys to specific functions (1) |
| 71 | view.setKeys(keyDispatch); |
| 72 | // whereas, this installs a general key handler function (2) |
| 73 | view.setKeys(keyCallback); |
| 74 | |
| 75 | // Note that (1) takes precedence over (2) |
| 76 | |
| 77 | view.$div.append('p') |
| 78 | .text('Press a key or two (try Z,X,C,V and others) ...') |
| 79 | .style('padding', '2px 8px'); |
| 80 | } |
| 81 | |
| 82 | // == register the view here, with links to lifecycle callbacks |
| 83 | |
| 84 | onos.ui.addView('sampleKeys', { |
| 85 | reset: true, // empty the div on reset |
| 86 | load: load |
| 87 | }); |
| 88 | |
| 89 | }(ONOS)); |