| 1 | |
| 2 | _initKeys: function (e) { |
| 3 | // default key bindings |
| 4 | var keys = { |
| 5 | pageUp: 33, |
| 6 | pageDown: 34, |
| 7 | end: 35, |
| 8 | home: 36, |
| 9 | left: 37, |
| 10 | up: 38, |
| 11 | right: 39, |
| 12 | down: 40 |
| 13 | }; |
| 14 | var i; |
| 15 | |
| 16 | // if you give me characters I give you keycode |
| 17 | if ( typeof this.options.keyBindings == 'object' ) { |
| 18 | for ( i in this.options.keyBindings ) { |
| 19 | if ( typeof this.options.keyBindings[i] == 'string' ) { |
| 20 | this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0); |
| 21 | } |
| 22 | } |
| 23 | } else { |
| 24 | this.options.keyBindings = {}; |
| 25 | } |
| 26 | |
| 27 | for ( i in keys ) { |
| 28 | this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i]; |
| 29 | } |
| 30 | |
| 31 | utils.addEvent(window, 'keydown', this); |
| 32 | |
| 33 | this.on('destroy', function () { |
| 34 | utils.removeEvent(window, 'keydown', this); |
| 35 | }); |
| 36 | }, |
| 37 | |
| 38 | _key: function (e) { |
| 39 | if ( !this.enabled ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | var snap = this.options.snap, // we are using this alot, better to cache it |
| 44 | newX = snap ? this.currentPage.pageX : this.x, |
| 45 | newY = snap ? this.currentPage.pageY : this.y, |
| 46 | now = utils.getTime(), |
| 47 | prevTime = this.keyTime || 0, |
| 48 | acceleration = 0.250, |
| 49 | pos; |
| 50 | |
| 51 | if ( this.options.useTransition && this.isInTransition ) { |
| 52 | pos = this.getComputedPosition(); |
| 53 | |
| 54 | this._translate(Math.round(pos.x), Math.round(pos.y)); |
| 55 | this.isInTransition = false; |
| 56 | } |
| 57 | |
| 58 | this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0; |
| 59 | |
| 60 | switch ( e.keyCode ) { |
| 61 | case this.options.keyBindings.pageUp: |
| 62 | if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) { |
| 63 | newX += snap ? 1 : this.wrapperWidth; |
| 64 | } else { |
| 65 | newY += snap ? 1 : this.wrapperHeight; |
| 66 | } |
| 67 | break; |
| 68 | case this.options.keyBindings.pageDown: |
| 69 | if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) { |
| 70 | newX -= snap ? 1 : this.wrapperWidth; |
| 71 | } else { |
| 72 | newY -= snap ? 1 : this.wrapperHeight; |
| 73 | } |
| 74 | break; |
| 75 | case this.options.keyBindings.end: |
| 76 | newX = snap ? this.pages.length-1 : this.maxScrollX; |
| 77 | newY = snap ? this.pages[0].length-1 : this.maxScrollY; |
| 78 | break; |
| 79 | case this.options.keyBindings.home: |
| 80 | newX = 0; |
| 81 | newY = 0; |
| 82 | break; |
| 83 | case this.options.keyBindings.left: |
| 84 | newX += snap ? -1 : 5 + this.keyAcceleration>>0; |
| 85 | break; |
| 86 | case this.options.keyBindings.up: |
| 87 | newY += snap ? 1 : 5 + this.keyAcceleration>>0; |
| 88 | break; |
| 89 | case this.options.keyBindings.right: |
| 90 | newX -= snap ? -1 : 5 + this.keyAcceleration>>0; |
| 91 | break; |
| 92 | case this.options.keyBindings.down: |
| 93 | newY -= snap ? 1 : 5 + this.keyAcceleration>>0; |
| 94 | break; |
| 95 | default: |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if ( snap ) { |
| 100 | this.goToPage(newX, newY); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if ( newX > 0 ) { |
| 105 | newX = 0; |
| 106 | this.keyAcceleration = 0; |
| 107 | } else if ( newX < this.maxScrollX ) { |
| 108 | newX = this.maxScrollX; |
| 109 | this.keyAcceleration = 0; |
| 110 | } |
| 111 | |
| 112 | if ( newY > 0 ) { |
| 113 | newY = 0; |
| 114 | this.keyAcceleration = 0; |
| 115 | } else if ( newY < this.maxScrollY ) { |
| 116 | newY = this.maxScrollY; |
| 117 | this.keyAcceleration = 0; |
| 118 | } |
| 119 | |
| 120 | this.scrollTo(newX, newY, 0); |
| 121 | |
| 122 | this.keyTime = now; |
| 123 | }, |