-
Notifications
You must be signed in to change notification settings - Fork 28
/
Dialog.js
66 lines (60 loc) · 1.96 KB
/
Dialog.js
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
/** @module delite/Dialog */
define([
"dcl/dcl",
"./a11y",
"./Widget"
], function (dcl, a11y, Widget) {
/**
* Base class for modal dialogs, where tabbing from the last element loops to the first, and vice-versa.
* @mixin module:delite/Dialog
* @augments module:delite/Widget
*/
return dcl(Widget, /** @lends module:delite/Dialog# */ {
declaredClass: "delite/Dialog",
/**
* Whether or not dialog is modal.
*/
modal: true,
constructor: function () {
this.on("keydown", this._dialogKeyDownHandler.bind(this));
},
focus: function () {
// Focus on first field.
this._getFocusItems();
if (this._firstFocusItem && this._firstFocusItem !== this) {
this._firstFocusItem.focus();
}
},
/**
* Finds focusable items in dialog,
* and sets `this._firstFocusItem` and `this._lastFocusItem`.
*
* @protected
*/
_getFocusItems: function () {
var elems = a11y.getTabNavigable(this);
this._firstFocusItem = elems[0] || this.closeButtonNode || this;
this._lastFocusItem = elems[elems.length - 1] || this._firstFocusItem;
},
_dialogKeyDownHandler: function (/*Event*/ evt) {
if (this.modal && evt.key === "Tab") {
this._getFocusItems(this);
var node = evt.target;
if (this._firstFocusItem === this._lastFocusItem) {
// don't move focus anywhere, but don't allow browser to move focus off of dialog either
evt.preventDefault();
} else if (node === this._firstFocusItem && evt.shiftKey) {
// if we are shift-tabbing from first focusable item in dialog, send focus to last item
this._lastFocusItem.focus();
evt.preventDefault();
} else if (node === this._lastFocusItem && !evt.shiftKey) {
// if we are tabbing from last focusable item in dialog, send focus to first item
this._firstFocusItem.focus();
evt.preventDefault();
}
// Call stopImmediatePropagation() so the popup.js doesn't see the TAB and close the Dialog.
evt.stopImmediatePropagation();
}
}
});
});