| 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 | var snap = this.options.snap, // we are using this alot, better to cache it |
| 40 | newX = snap ? this.currentPage.pageX : this.x, |
| 41 | newY = snap ? this.currentPage.pageY : this.y, |
| 42 | now = utils.getTime(), |
| 43 | prevTime = this.keyTime || 0, |
| 44 | acceleration = 0.250, |
| 45 | pos; |
| 46 | |
| 47 | if ( this.options.useTransition && this.isInTransition ) { |
| 48 | pos = this.getComputedPosition(); |
| 49 | |
| 50 | this._translate(Math.round(pos.x), Math.round(pos.y)); |
| 51 | this.isInTransition = false; |
| 52 | } |
| 53 | |
| 54 | this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0; |
| 55 | |
| 56 | switch ( e.keyCode ) { |
| 57 | case this.options.keyBindings.pageUp: |
| 58 | if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) { |
| 59 | newX += snap ? 1 : this.wrapperWidth; |
| 60 | } else { |
| 61 | newY += snap ? 1 : this.wrapperHeight; |
| 62 | } |
| 63 | break; |
| 64 | case this.options.keyBindings.pageDown: |
| 65 | if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) { |
| 66 | newX -= snap ? 1 : this.wrapperWidth; |
| 67 | } else { |
| 68 | newY -= snap ? 1 : this.wrapperHeight; |
| 69 | } |
| 70 | break; |
| 71 | case this.options.keyBindings.end: |
| 72 | newX = snap ? this.pages.length-1 : this.maxScrollX; |
| 73 | newY = snap ? this.pages[0].length-1 : this.maxScrollY; |
| 74 | break; |
| 75 | case this.options.keyBindings.home: |
| 76 | newX = 0; |
| 77 | newY = 0; |
| 78 | break; |
| 79 | case this.options.keyBindings.left: |
| 80 | newX += snap ? -1 : 5 + this.keyAcceleration>>0; |
| 81 | break; |
| 82 | case this.options.keyBindings.up: |
| 83 | newY += snap ? 1 : 5 + this.keyAcceleration>>0; |
| 84 | break; |
| 85 | case this.options.keyBindings.right: |
| 86 | newX -= snap ? -1 : 5 + this.keyAcceleration>>0; |
| 87 | break; |
| 88 | case this.options.keyBindings.down: |
| 89 | newY -= snap ? 1 : 5 + this.keyAcceleration>>0; |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | if ( snap ) { |
| 94 | this.goToPage(newX, newY); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if ( newX > 0 ) { |
| 99 | newX = 0; |
| 100 | this.keyAcceleration = 0; |
| 101 | } else if ( newX < this.maxScrollX ) { |
| 102 | newX = this.maxScrollX; |
| 103 | this.keyAcceleration = 0; |
| 104 | } |
| 105 | |
| 106 | if ( newY > 0 ) { |
| 107 | newY = 0; |
| 108 | this.keyAcceleration = 0; |
| 109 | } else if ( newY < this.maxScrollY ) { |
| 110 | newY = this.maxScrollY; |
| 111 | this.keyAcceleration = 0; |
| 112 | } |
| 113 | |
| 114 | this.scrollTo(newX, newY, 0); |
| 115 | |
| 116 | this.keyTime = now; |
| 117 | }, |