MDL-64506 templates: Move BS2 labels to BS4 badges
[moodle.git] / lib / yui / src / dock / js / actionkey.js
CommitLineData
84192d78
SH
1/**
2 * Dock JS.
3 *
4 * This file contains the action key event definition that is used for accessibility handling within the Dock.
5 *
6 * @module moodle-core-dock
7 */
8
9/**
10 * A 'dock:actionkey' Event.
11 * The event consists of the left arrow, right arrow, enter and space keys.
12 * More keys can be mapped to action meanings.
13 * actions: collapse , expand, toggle, enter.
14 *
15 * This event is subscribed to by dockitems.
16 * The on() method to subscribe allows specifying the desired trigger actions as JSON.
17 *
18 * This event can also be delegated if needed.
19 *
20 * @namespace M.core.dock
21 * @class ActionKey
22 */
23Y.Event.define("dock:actionkey", {
a69a7e89
SH
24 // Webkit and IE repeat keydown when you hold down arrow keys.
25 // Opera links keypress to page scroll; others keydown.
26 // Firefox prevents page scroll via preventDefault() on either
27 // keydown or keypress.
28 _event: (Y.UA.webkit || Y.UA.ie) ? 'keydown' : 'keypress',
84192d78
SH
29
30 /**
31 * The keys to trigger on.
a69a7e89 32 * @property _keys
84192d78 33 */
a69a7e89 34 _keys: {
5bb4f444 35 // arrows
a69a7e89
SH
36 '37': 'collapse',
37 '39': 'expand',
5bb4f444 38 // (@todo: lrt/rtl/M.core_dock.cfg.orientation decision to assign arrow to meanings)
a69a7e89
SH
39 '32': 'toggle',
40 '13': 'enter'
41 },
84192d78
SH
42
43 /**
44 * Handles key events
45 * @method _keyHandler
46 * @param {EventFacade} e
47 * @param {SyntheticEvent.Notifier} notifier The notifier used to trigger the execution of subscribers
48 * @param {Object} args
49 */
5bb4f444 50 _keyHandler: function(e, notifier, args) {
a69a7e89
SH
51 var actObj;
52 if (!args.actions) {
53 actObj = {collapse: true, expand: true, toggle: true, enter: true};
54 } else {
55 actObj = args.actions;
56 }
57 if (this._keys[e.keyCode] && actObj[this._keys[e.keyCode]]) {
58 e.action = this._keys[e.keyCode];
59 notifier.fire(e);
60 }
61 },
84192d78
SH
62
63 /**
64 * Subscribes to events.
65 * @method on
66 * @param {Node} node The node this subscription was applied to.
67 * @param {Subscription} sub The object tracking this subscription.
68 * @param {SyntheticEvent.Notifier} notifier The notifier used to trigger the execution of subscribers
69 */
5bb4f444 70 on: function(node, sub, notifier) {
a69a7e89
SH
71 // subscribe to _event and ask keyHandler to handle with given args[0] (the desired actions).
72 if (sub.args === null) {
5bb4f444 73 // no actions given
a69a7e89
SH
74 sub._detacher = node.on(this._event, this._keyHandler, this, notifier, {actions: false});
75 } else {
76 sub._detacher = node.on(this._event, this._keyHandler, this, notifier, sub.args[0]);
77 }
78 },
84192d78
SH
79
80 /**
81 * Detaches an event listener
82 * @method detach
a69a7e89
SH
83 * @param {Node} node The node this subscription was applied to.
84 * @param {Subscription} sub The object tracking this subscription.
85 * @param {SyntheticEvent.Notifier} notifier The notifier used to trigger the execution of subscribers
84192d78 86 */
5bb4f444
DP
87 detach: function(node, sub) {
88 // detach our _detacher handle of the subscription made in on()
a69a7e89
SH
89 sub._detacher.detach();
90 },
84192d78
SH
91
92 /**
93 * Creates a delegated event listener.
94 * @method delegate
95 * @param {Node} node The node this subscription was applied to.
96 * @param {Subscription} sub The object tracking this subscription.
97 * @param {SyntheticEvent.Notifier} notifier The notifier used to trigger the execution of subscribers
98 * @param {String|function} filter Selector string or function that accpets an event object and returns null.
99 */
5bb4f444 100 delegate: function(node, sub, notifier, filter) {
a69a7e89
SH
101 // subscribe to _event and ask keyHandler to handle with given args[0] (the desired actions).
102 if (sub.args === null) {
5bb4f444
DP
103 // no actions given
104 sub._delegateDetacher = node.delegate(this._event, this._keyHandler, filter, this, notifier, {actions: false});
a69a7e89
SH
105 } else {
106 sub._delegateDetacher = node.delegate(this._event, this._keyHandler, filter, this, notifier, sub.args[0]);
107 }
108 },
84192d78
SH
109
110 /**
111 * Detaches a delegated event listener.
112 * @method detachDelegate
113 * @param {Node} node The node this subscription was applied to.
114 * @param {Subscription} sub The object tracking this subscription.
115 * @param {SyntheticEvent.Notifier} notifier The notifier used to trigger the execution of subscribers
116 * @param {String|function} filter Selector string or function that accpets an event object and returns null.
117 */
5bb4f444 118 detachDelegate: function(node, sub) {
a69a7e89
SH
119 sub._delegateDetacher.detach();
120 }
84192d78 121});