~keith-hughitt/helioviewer.org/2.0

3 by Keith Hughitt
Helioviewer.org 2.0.2
1
/**
2
 * @fileOverview
3
 * @author <a href="mailto:keith.hughitt@nasa.gov">Keith Hughitt</a>
4
 */
5
/*jslint browser: true, white: true, onevar: true, undef: true, nomen: false, eqeqeq: true, plusplus: true, 
6
bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxlen: 120, sub: true */
7
/*globals document, $, Class */
8
"use strict";
9
var KeyboardManager = Class.extend(
10
/** @lends KeyboardManager.prototype */
11
{
12
    /**
13
     * @constructs
14
     */
15
    init: function (controller) {
16
        this.controller = controller;
17
        this._initEventHandlers();
18
    },
19
    
20
21
    /**
22
     * @description Initialize keyboard-related event handlers.
23
     * 
24
     * TODO: use events or public method instead of zoomControl's (private) method.
25
     * 
26
     * TODO (2009/07/29): Webkit doesn't support keypress events for non alphanumeric
27
     * keys (http://ejohn.org/blog/keypress-in-safari-31/).
28
     * 
29
     * Instead of using keypress, it may be better to use keydown and a boolean to decide 
30
     * when vp is moving and when it should be stationary.
31
     * 
32
     * Simple implementation:
33
     *     vp.movingUp (Boolean), vp.movingDown (Boolean), vp.movingLeft (Boolean), vp.movingRight (Boolean)
34
     *     
35
     * From there it is also simple to add support for diagonal movement, etc.
36
     */
37
    _initEventHandlers: function () {
38
        var onKeyPress, self = this;
39
40
        /**
41
         * @description Sets up keyboard shortcuts
42
         * @TODO 01/04/2010: Use something like js-hotkeys (http://code.google.com/p/js-hotkeys/)
43
         *                   to allow for more advanced keyboard navigation such as "cntl + z" to undo, etc
44
         * TODO 01/16/2009: Create buttons for mouse-coord and detail toggles
45
         */
46
        onKeyPress = function (e) {
47
            var key, character, vp = self.controller.viewport;
48
            
49
            // Letters use Event.which, while arrows, etc. use Event.keyCode
50
            if (e.keyCode) {
51
                key = e.keyCode;
52
            }
53
            else if (e.which) {
54
                key = e.which;
55
            }
56
            
57
            // Get character pressed (letters, etc)
58
            character = String.fromCharCode(key);
59
60
            // Arrow keys
61
            if (key === 37 || key === 38 || key === 39 || key === 40) {
62
                vp.startMoving();
63
                vp.moveCounter += 1; // Threshold
64
                //if ((vp.moveCounter % vp.imageUpdateThrottle) !== 0)
65
                //    return;
66
                vp.moveCounter = vp.moveCounter % vp.tileUpdateThrottle;
67
                
68
                //Right-arrow
69
                if (key === 37) {
70
                    vp.moveBy(8, 0);
71
                }
72
                    
73
                //Up-arrow
74
                else if (key === 38) {
75
                    vp.moveBy(0, 8);
76
                }
77
                    
78
                //Left-arrow
79
                else if (key === 39) {
80
                    vp.moveBy(-8, 0);
81
                }
82
                    
83
                //Down-arrow
84
                else if (key === 40) {
85
                    vp.moveBy(0, -8);
86
                }
87
                
88
                vp.endMoving();
89
                return false;
90
            }        
91
92
            else if (character === "c") {
93
                $("#center-button").click();
94
            }
95
            else if (character === "m") {
96
                vp.toggleMouseCoords();
97
            }
98
            else if (character === "-" || character === "_") {
99
                $("#zoomControlZoomOut").click();
100
            }
101
            else if (character === "=" || character === "+") {
102
                $("#zoomControlZoomIn").click();
103
            }
104
            else if (character === "d") {
105
                self.controller.eventLayers.toggleLabels();
106
            }
107
            else if (character === "f") {
108
                $("#fullscreen-btn").click();
109
            }
110
            else if (character === ",") {
111
                $("#timeBackBtn").click();
112
            }
113
            else if (character === ".") {
114
                $("#timeForwardBtn").click();
115
            }
116
        };
117
        
118
        // Event-handlers
119
        $(document).keypress(function (e) {
120
            if (e.target.tagName !== "INPUT") {
121
                onKeyPress(e);
122
            }
123
        });     
124
    }
125
});