summaryrefslogtreecommitdiff
path: root/lib/scripts/hotkeys.js
blob: 76a277aea2ad6b09e7d2850def2b16678b58d90d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
 * Some of these scripts were taken from TinyMCE (http://tinymce.moxiecode.com/) and were modified for DokuWiki
 *
 * Class handles accesskeys using javascript and also provides ability
 * to register and use other hotkeys as well.
 *
 * @author Marek Sacha <sachamar@fel.cvut.cz>
 */
function Hotkeys() {

    this.shortcuts = new Array();

    /**
     * Set modifier keys, for instance:
     *  this.modifier = 'ctrl';
     *  this.modifier = 'ctrl+shift';
     *  this.modifier = 'ctrl+alt+shift';
     *  this.modifier = 'alt';
     *  this.modifier = 'alt+shift';
     *
     *  overwritten in intitialize (see below)
     */
    this.modifier = 'ctrl+alt';

    /**
     * Initialization
     *
     * This function looks up all the accesskeys used in the current page
     * (at anchor elements and button elements [type="submit"]) and registers
     * appropriate shortcuts.
     *
     * Secondly, initialization registers listeners on document to catch all
     * keyboard events.
     *
     * @author Marek Sacha <sachamar@fel.cvut.cz>
     */
    this.initialize = function() {
        var t = this;

        //switch modifier key based on OS FS#1958
        if(is_macos){
            t.modifier = 'ctrl+alt';
        }else{
            t.modifier = 'alt';
        }

        /**
         * Lookup all anchors with accesskey and register event - go to anchor
         * target.
         */
        var anchors = document.getElementsByTagName("a");
        t.each(anchors, function(a) {
            if (a.accessKey != "") {
                t.addShortcut(t.modifier + '+' + a.accessKey, function() {
                    location.href = a.href;
                });
                a.accessKey = '';
            }
        });

        /**
         * Lookup all button [type="submit"] with accesskey and register event -
         * perform "click" on a button.
         */
        var inputs = document.getElementsByTagName("button");
        t.each(inputs, function(i) {
            if (i.type == "submit" && i.accessKey != "") {
                t.addShortcut(t.modifier + '+' + i.accessKey, function() {
                    i.click();
                });
                i.accessKey = '';
            }
        });

        /**
         * Lookup all buttons with accesskey and register event -
         * perform "click" on a button.
         */
        var buttons = document.getElementsByTagName("button");
        t.each(buttons, function(b) {
            if (b.accessKey != "") {
                t.addShortcut(t.modifier + '+' + b.accessKey, function() {
                    b.click();
                });
                b.accessKey = '';
            }
        });

        /**
         * Register listeners on document to catch keyboard events.
         */

        addEvent(document,'keyup',function (e) {
            return t.onkeyup.call(t,e);
        });

        addEvent(document,'keypress',function (e) {
            return t.onkeypress.call(t,e);
        });

        addEvent(document,'keydown',function (e) {
            return t.onkeydown.call(t,e);
        });
    };

    /**
     * Keyup processing function
     * Function returns true if keyboard event has registered handler, and
     * executes the handler function.
     *
     * @param e KeyboardEvent
     * @author Marek Sacha <sachamar@fel.cvut.cz>
     * @return b boolean
     */
    this.onkeyup = function(e) {
        var t = this;
        var v = t.findShortcut(e);
        if (v != null && v != false) {
            v.func.call(t);
            return false;
        }
        return true;
    };

    /**
     * Keydown processing function
     * Function returns true if keyboard event has registered handler
     *
     * @param e KeyboardEvent
     * @author Marek Sacha <sachamar@fel.cvut.cz>
     * @return b boolean
     */
    this.onkeydown = function(e) {
        var t = this;
        var v = t.findShortcut(e);
        if (v != null && v != false) {
            return false;
        }
        return true;
    };

    /**
     * Keypress processing function
     * Function returns true if keyboard event has registered handler
     *
     * @param e KeyboardEvent
     * @author Marek Sacha <sachamar@fel.cvut.cz>
     * @return b
     */
    this.onkeypress = function(e) {
        var t = this;
        var v = t.findShortcut(e);
        if (v != null && v != false) {
            return false;
        }
        return true;
    };

    /**
     * Register new shortcut
     *
     * This function registers new shortcuts, each shortcut is defined by its
     * modifier keys and a key (with + as delimiter). If shortcut is pressed
     * cmd_function is performed.
     *
     * For example:
     *  pa = "ctrl+alt+p";
     *  pa = "shift+alt+s";
     *
     * Full example of method usage:
     *  hotkeys.addShortcut('ctrl+s',function() {
     *      document.getElementByID('form_1').submit();
     *  });
     *
     * @param pa String description of the shortcut (ctrl+a, ctrl+shift+p, .. )
     * @param cmd_func Function to be called if shortcut is pressed
     * @author Marek Sacha <sachamar@fel.cvut.cz>
     */
    this.addShortcut = function(pa, cmd_func) {
        var t = this;

        var o = {
            func : cmd_func,
            alt : false,
            ctrl : false,
            shift : false
        };

        t.each(t.explode(pa, '+'), function(v) {
            switch (v) {
                case 'alt':
                case 'ctrl':
                case 'shift':
                    o[v] = true;
                    break;

                default:
                    o.charCode = v.charCodeAt(0);
                    o.keyCode = v.toUpperCase().charCodeAt(0);
            }
        });

        t.shortcuts.push((o.ctrl ? 'ctrl' : '') + ',' + (o.alt ? 'alt' : '') + ',' + (o.shift ? 'shift' : '') + ',' + o.keyCode,  o);

        return true;
    };

    /**
     * @property isMac
     */
    this.isMac = is_macos;

    /**
     * Apply function cb on each element of o in the namespace of s
     * @param o Array of objects
     * @param cb Function to be called on each object
     * @param s Namespace to be used during call of cb (default namespace is o)
     * @author Marek Sacha <sachamar@fel.cvut.cz>
     */
    this.each = function(o, cb, s) {
        var n, l;

        if (!o)
            return 0;

        s = s || o;

        if (o.length !== undefined) {
            // Indexed arrays, needed for Safari
            for (n=0, l = o.length; n < l; n++) {
                if (cb.call(s, o[n], n, o) === false)
                    return 0;
            }
        } else {
            // Hashtables
            for (n in o) {
                if (o.hasOwnProperty(n)) {
                    if (cb.call(s, o[n], n, o) === false)
                        return 0;
                }
            }
        }

        return 1;
    };

    /**
     * Explode string according to delimiter
     * @param s String
     * @param d Delimiter (default ',')
     * @author Marek Sacha <sachamar@fel.cvut.cz>
     * @return a Array of tokens
     */
    this.explode = function(s, d) {
        return  s.split(d || ',');
    };

    /**
     * Find if the shortcut was registered
     *
     * @param e KeyboardEvent
     * @author Marek Sacha <sachamar@fel.cvut.cz>
     * @return v Shortcut structure or null if not found
     */
    this.findShortcut = function (e) {
        var t = this;
        var v = null;

        /* No modifier key used - shortcut does not exist */
        if (!e.altKey && !e.ctrlKey && !e.metaKey) {
            return v;
        }

        t.each(t.shortcuts, function(o) {
            if (o.ctrl != e.ctrlKey)
                return;

            if (o.alt != e.altKey)
                return;

            if (o.shift != e.shiftKey)
                return;

            if (e.keyCode == o.keyCode || (e.charCode && e.charCode == o.charCode)) {
                v = o;
                return;
            }
        });
        return v;
    };
}

/**
 * Init function for hotkeys. Called from js.php, to ensure hotkyes are initialized after toolbar.
 * Call of addInitEvent(initializeHotkeys) is unnecessary now.
 *
 * @author Marek Sacha <sachamar@fel.cvut.cz>
 */
function initializeHotkeys() {
    var hotkeys = new Hotkeys();
    hotkeys.initialize();
}