2 * Some of these scripts were taken from TinyMCE (http://tinymce.moxiecode.com/) and were modified for DokuWiki
4 * Class handles accesskeys using javascript and also provides ability
5 * to register and use other hotkeys as well.
7 * @author Marek Sacha <sachamar@fel.cvut.cz>
11 this.shortcuts = new Array();
14 * Set modifier keys, for instance:
15 * this.modifier = 'ctrl';
16 * this.modifier = 'ctrl+shift';
17 * this.modifier = 'ctrl+alt+shift';
18 * this.modifier = 'alt';
19 * this.modifier = 'alt+shift';
21 * overwritten in intitialize (see below)
23 this.modifier = 'ctrl+alt';
28 * This function looks up all the accesskeys used in the current page
29 * (at anchor elements and button elements [type="submit"]) and registers
30 * appropriate shortcuts.
32 * Secondly, initialization registers listeners on document to catch all
35 * @author Marek Sacha <sachamar@fel.cvut.cz>
37 this.initialize = function() {
40 //switch modifier key based on OS FS#1958
42 t.modifier = 'ctrl+alt';
48 * Lookup all anchors with accesskey and register event - go to anchor
51 var anchors = document.getElementsByTagName("a");
52 t.each(anchors, function(a) {
53 if (a.accessKey != "") {
54 t.addShortcut(t.modifier + '+' + a.accessKey, function() {
55 location.href = a.href;
62 * Lookup all button [type="submit"] with accesskey and register event -
63 * perform "click" on a button.
65 var inputs = document.getElementsByTagName("button");
66 t.each(inputs, function(i) {
67 if (i.type == "submit" && i.accessKey != "") {
68 t.addShortcut(t.modifier + '+' + i.accessKey, function() {
76 * Lookup all buttons with accesskey and register event -
77 * perform "click" on a button.
79 var buttons = document.getElementsByTagName("button");
80 t.each(buttons, function(b) {
81 if (b.accessKey != "") {
82 t.addShortcut(t.modifier + '+' + b.accessKey, function() {
90 * Register listeners on document to catch keyboard events.
93 addEvent(document,'keyup',function (e) {
94 return t.onkeyup.call(t,e);
97 addEvent(document,'keypress',function (e) {
98 return t.onkeypress.call(t,e);
101 addEvent(document,'keydown',function (e) {
102 return t.onkeydown.call(t,e);
107 * Keyup processing function
108 * Function returns true if keyboard event has registered handler, and
109 * executes the handler function.
111 * @param e KeyboardEvent
112 * @author Marek Sacha <sachamar@fel.cvut.cz>
115 this.onkeyup = function(e) {
117 var v = t.findShortcut(e);
118 if (v != null && v != false) {
126 * Keydown processing function
127 * Function returns true if keyboard event has registered handler
129 * @param e KeyboardEvent
130 * @author Marek Sacha <sachamar@fel.cvut.cz>
133 this.onkeydown = function(e) {
135 var v = t.findShortcut(e);
136 if (v != null && v != false) {
143 * Keypress processing function
144 * Function returns true if keyboard event has registered handler
146 * @param e KeyboardEvent
147 * @author Marek Sacha <sachamar@fel.cvut.cz>
150 this.onkeypress = function(e) {
152 var v = t.findShortcut(e);
153 if (v != null && v != false) {
160 * Register new shortcut
162 * This function registers new shortcuts, each shortcut is defined by its
163 * modifier keys and a key (with + as delimiter). If shortcut is pressed
164 * cmd_function is performed.
168 * pa = "shift+alt+s";
170 * Full example of method usage:
171 * hotkeys.addShortcut('ctrl+s',function() {
172 * document.getElementByID('form_1').submit();
175 * @param pa String description of the shortcut (ctrl+a, ctrl+shift+p, .. )
176 * @param cmd_func Function to be called if shortcut is pressed
177 * @author Marek Sacha <sachamar@fel.cvut.cz>
179 this.addShortcut = function(pa, cmd_func) {
189 t.each(t.explode(pa, '+'), function(v) {
198 o.charCode = v.charCodeAt(0);
199 o.keyCode = v.toUpperCase().charCodeAt(0);
203 t.shortcuts.push((o.ctrl ? 'ctrl' : '') + ',' + (o.alt ? 'alt' : '') + ',' + (o.shift ? 'shift' : '') + ',' + o.keyCode, o);
211 this.isMac = is_macos;
214 * Apply function cb on each element of o in the namespace of s
215 * @param o Array of objects
216 * @param cb Function to be called on each object
217 * @param s Namespace to be used during call of cb (default namespace is o)
218 * @author Marek Sacha <sachamar@fel.cvut.cz>
220 this.each = function(o, cb, s) {
228 if (o.length !== undefined) {
229 // Indexed arrays, needed for Safari
230 for (n=0, l = o.length; n < l; n++) {
231 if (cb.call(s, o[n], n, o) === false)
237 if (o.hasOwnProperty(n)) {
238 if (cb.call(s, o[n], n, o) === false)
248 * Explode string according to delimiter
250 * @param d Delimiter (default ',')
251 * @author Marek Sacha <sachamar@fel.cvut.cz>
252 * @return a Array of tokens
254 this.explode = function(s, d) {
255 return s.split(d || ',');
259 * Find if the shortcut was registered
261 * @param e KeyboardEvent
262 * @author Marek Sacha <sachamar@fel.cvut.cz>
263 * @return v Shortcut structure or null if not found
265 this.findShortcut = function (e) {
269 /* No modifier key used - shortcut does not exist */
270 if (!e.altKey && !e.ctrlKey && !e.metaKey) {
274 t.each(t.shortcuts, function(o) {
275 if (o.ctrl != e.ctrlKey)
278 if (o.alt != e.altKey)
281 if (o.shift != e.shiftKey)
284 if (e.keyCode == o.keyCode || (e.charCode && e.charCode == o.charCode)) {
294 * Init function for hotkeys. Called from js.php, to ensure hotkyes are initialized after toolbar.
295 * Call of addInitEvent(initializeHotkeys) is unnecessary now.
297 * @author Marek Sacha <sachamar@fel.cvut.cz>
299 function initializeHotkeys() {
300 var hotkeys = new Hotkeys();
301 hotkeys.initialize();