1 |
efrain |
1 |
/**
|
|
|
2 |
* The generic dialogue class for use in Moodle.
|
|
|
3 |
*
|
|
|
4 |
* @module moodle-core-notification
|
|
|
5 |
* @submodule moodle-core-notification-dialogue
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
var DIALOGUE_NAME = 'Moodle dialogue',
|
|
|
9 |
DIALOGUE,
|
|
|
10 |
DIALOGUE_FULLSCREEN_CLASS = DIALOGUE_PREFIX + '-fullscreen',
|
|
|
11 |
DIALOGUE_HIDDEN_CLASS = DIALOGUE_PREFIX + '-hidden',
|
|
|
12 |
DIALOGUE_SELECTOR = ' [role=dialog]',
|
|
|
13 |
MENUBAR_SELECTOR = '[role=menubar]',
|
|
|
14 |
DOT = '.',
|
|
|
15 |
HAS_ZINDEX = 'moodle-has-zindex',
|
|
|
16 |
CAN_RECEIVE_FOCUS_SELECTOR = 'input:not([type="hidden"]):not([disabled]):not([tabindex^="-"]),' +
|
|
|
17 |
'a[href]:not([disabled]):not([tabindex^="-"]),' +
|
|
|
18 |
'button:not([disabled]):not([tabindex^="-"]),' +
|
|
|
19 |
'textarea:not([disabled]):not([tabindex^="-"]),' +
|
|
|
20 |
'select:not([disabled]):not([tabindex^="-"]),' +
|
|
|
21 |
'[tabindex]:not([disabled]):not([tabindex^="-"])',
|
|
|
22 |
FORM_SELECTOR = 'form';
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* A re-usable dialogue box with Moodle classes applied.
|
|
|
26 |
*
|
|
|
27 |
* @param {Object} c Object literal specifying the dialogue configuration properties.
|
|
|
28 |
* @constructor
|
|
|
29 |
* @class M.core.dialogue
|
|
|
30 |
* @extends Panel
|
|
|
31 |
*/
|
|
|
32 |
DIALOGUE = function(config) {
|
|
|
33 |
// The code below is a hack to add the custom content node to the DOM, on the fly, per-instantiation and to assign the value
|
|
|
34 |
// of 'srcNode' to this newly created node. Normally (see docs: https://yuilibrary.com/yui/docs/widget/widget-extend.html),
|
|
|
35 |
// this node would be pre-existing in the DOM, and an id string would simply be passed in as a property of the config object
|
|
|
36 |
// during widget instantiation, however, because we're creating it on the fly (and 'config.srcNode' isn't set yet), care must
|
|
|
37 |
// be taken to add it to the DOM and to properly set the value of 'config.srcNode' before calling the parent constructor.
|
|
|
38 |
// Note: additional classes can be added to this content node by setting the 'additionalBaseClass' config property (a string).
|
|
|
39 |
var id = 'moodle-dialogue-' + Y.stamp(this) + '-wrap'; // Can't use this.get('id') as it's not set at this stage.
|
|
|
40 |
config.notificationBase =
|
|
|
41 |
Y.Node.create('<div class="' + CSS_CLASSES.BASE + '">')
|
|
|
42 |
.append(Y.Node.create(
|
|
|
43 |
'<div id="' + id + '" role="dialog" ' +
|
|
|
44 |
'aria-labelledby="' + id + '-header-text" class="' + CSS_CLASSES.WRAP + '" aria-live="polite"></div>'
|
|
|
45 |
)
|
|
|
46 |
.append(Y.Node.create('<div class="' + CSS_CLASSES.HEADER + ' yui3-widget-hd"></div>'))
|
|
|
47 |
.append(Y.Node.create('<div class="' + CSS_CLASSES.BODY + ' yui3-widget-bd"></div>'))
|
|
|
48 |
.append(Y.Node.create('<div class="' + CSS_CLASSES.FOOTER + ' yui3-widget-ft"></div>')));
|
|
|
49 |
config.attachmentPoint = config.attachmentPoint || document.body;
|
|
|
50 |
Y.one(config.attachmentPoint).append(config.notificationBase);
|
|
|
51 |
config.srcNode = '#' + id;
|
|
|
52 |
delete config.buttons; // Don't let anyone pass in buttons as we want to control these during init. addButton can be used later.
|
|
|
53 |
DIALOGUE.superclass.constructor.apply(this, [config]);
|
|
|
54 |
};
|
|
|
55 |
Y.extend(DIALOGUE, Y.Panel, {
|
|
|
56 |
// Window resize event listener.
|
|
|
57 |
_resizeevent: null,
|
|
|
58 |
// Orientation change event listener.
|
|
|
59 |
_orientationevent: null,
|
|
|
60 |
_calculatedzindex: false,
|
|
|
61 |
// Current maskNode id
|
|
|
62 |
_currentMaskNodeId: null,
|
|
|
63 |
/**
|
|
|
64 |
* The original position of the dialogue before it was reposition to
|
|
|
65 |
* avoid browser jumping.
|
|
|
66 |
*
|
|
|
67 |
* @property _originalPosition
|
|
|
68 |
* @protected
|
|
|
69 |
* @type Array
|
|
|
70 |
*/
|
|
|
71 |
_originalPosition: null,
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* The list of elements that have been aria hidden when displaying
|
|
|
75 |
* this dialogue.
|
|
|
76 |
*
|
|
|
77 |
* @property _hiddenSiblings
|
|
|
78 |
* @protected
|
|
|
79 |
* @type Array
|
|
|
80 |
*/
|
|
|
81 |
_hiddenSiblings: null,
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Hide the modal only if it doesn't contain a form.
|
|
|
85 |
*
|
|
|
86 |
* @method hideIfNotForm
|
|
|
87 |
*/
|
|
|
88 |
hideIfNotForm: function() {
|
|
|
89 |
var bb = this.get('boundingBox'),
|
|
|
90 |
formElement = bb.one(FORM_SELECTOR);
|
|
|
91 |
|
|
|
92 |
if (formElement === null) {
|
|
|
93 |
this.hide();
|
|
|
94 |
}
|
|
|
95 |
},
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Initialise the dialogue.
|
|
|
99 |
*
|
|
|
100 |
* @method initializer
|
|
|
101 |
*/
|
|
|
102 |
initializer: function() {
|
|
|
103 |
var bb;
|
|
|
104 |
|
|
|
105 |
if (this.get('closeButton') !== false) {
|
|
|
106 |
var title = this.get('closeButtonTitle');
|
|
|
107 |
// The buttons constructor does not allow custom attributes.
|
|
|
108 |
this.get('buttons').header[0].setAttribute('title', title);
|
|
|
109 |
this.get('buttons').header[0].setAttribute('aria-label', title);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
this.setStdModContent(Y.WidgetStdMod.HEADER,
|
|
|
113 |
'<h5 id="' + this.get('id') + '-wrap-header-text">' + this.get('headerContent') + '</h5>',
|
|
|
114 |
Y.WidgetStdMod.REPLACE);
|
|
|
115 |
|
|
|
116 |
// Initialise the element cache.
|
|
|
117 |
this._hiddenSiblings = [];
|
|
|
118 |
|
|
|
119 |
if (this.get('render')) {
|
|
|
120 |
this.render();
|
|
|
121 |
}
|
|
|
122 |
this.after('visibleChange', this.visibilityChanged, this);
|
|
|
123 |
if (this.get('center')) {
|
|
|
124 |
this.centerDialogue();
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
if (this.get('modal')) {
|
|
|
128 |
// If we're a modal then make sure our container is ARIA
|
|
|
129 |
// hidden by default. ARIA visibility is managed for modal dialogues.
|
|
|
130 |
this.get(BASE).set('aria-hidden', 'true');
|
|
|
131 |
this.plug(Y.M.core.LockScroll);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// Remove the `focusoutside` listener.
|
|
|
135 |
// It conflicts with the ARIA focuslock manager which supports both YUI and non-YUI dialogues.
|
|
|
136 |
this.set('focusOn', Y.Array(this.get('focusOn')).filter(function(node) {
|
|
|
137 |
return node.eventName !== 'focusoutside';
|
|
|
138 |
}));
|
|
|
139 |
|
|
|
140 |
Y.one('document').on('orientationchange', function() {
|
|
|
141 |
// This will detect a change in orientation and re-trigger centering.
|
|
|
142 |
this.centerDialogOnVisible();
|
|
|
143 |
}, this);
|
|
|
144 |
|
|
|
145 |
Y.one('window').on('resize', function() {
|
|
|
146 |
// Detect window resize (most browsers).
|
|
|
147 |
this.centerDialogOnVisible();
|
|
|
148 |
}, this);
|
|
|
149 |
// Observe dialog on size change.
|
|
|
150 |
this.centerDialogOnDialogSizeChange(this);
|
|
|
151 |
|
|
|
152 |
// Workaround upstream YUI bug http://yuilibrary.com/projects/yui3/ticket/2532507
|
|
|
153 |
// and allow setting of z-index in theme.
|
|
|
154 |
bb = this.get('boundingBox');
|
|
|
155 |
bb.addClass(HAS_ZINDEX);
|
|
|
156 |
|
|
|
157 |
// Add any additional classes that were specified.
|
|
|
158 |
Y.Array.each(this.get('extraClasses'), bb.addClass, bb);
|
|
|
159 |
|
|
|
160 |
if (this.get('visible')) {
|
|
|
161 |
this.applyZIndex();
|
|
|
162 |
this.applyAndTrapFocus();
|
|
|
163 |
// Only do accessibility hiding for modals because the ARIA spec
|
|
|
164 |
// says that all ARIA dialogues should be modal.
|
|
|
165 |
if (this.get('modal')) {
|
|
|
166 |
// Make this dialogue visible to screen readers.
|
|
|
167 |
this.setAccessibilityVisible();
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
// Recalculate the zIndex every time the modal is altered.
|
|
|
171 |
this.on('maskShow', this.applyZIndex);
|
|
|
172 |
|
|
|
173 |
this.on('maskShow', function() {
|
|
|
174 |
// When the mask shows, position the boundingBox at the top-left of the window such that when it is
|
|
|
175 |
// focused, the position does not change.
|
|
|
176 |
var w = Y.one(Y.config.win),
|
|
|
177 |
bb = this.get('boundingBox');
|
|
|
178 |
|
|
|
179 |
if (!this.get('center')) {
|
|
|
180 |
this._originalPosition = bb.getXY();
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
// Check if maskNode already init click event.
|
|
|
184 |
var maskNode = this.get('maskNode');
|
|
|
185 |
if (this._currentMaskNodeId !== maskNode.get('_yuid')) {
|
|
|
186 |
this._currentMaskNodeId = maskNode.get('_yuid');
|
|
|
187 |
maskNode.on('click', this.hideIfNotForm, this);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
if (bb.getStyle('position') !== 'fixed') {
|
|
|
191 |
// If the boundingBox has been positioned in a fixed manner, then it will not position correctly to scrollTop.
|
|
|
192 |
bb.setStyles({
|
|
|
193 |
top: w.get('scrollTop'),
|
|
|
194 |
left: w.get('scrollLeft')
|
|
|
195 |
});
|
|
|
196 |
}
|
|
|
197 |
}, this);
|
|
|
198 |
|
|
|
199 |
// Add any additional classes to the content node if required.
|
|
|
200 |
var nBase = this.get('notificationBase');
|
|
|
201 |
var additionalClasses = this.get('additionalBaseClass');
|
|
|
202 |
if (additionalClasses !== '') {
|
|
|
203 |
nBase.addClass(additionalClasses);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
// Remove the dialogue from the DOM when it is destroyed.
|
|
|
207 |
this.after('destroyedChange', function() {
|
|
|
208 |
this.get(BASE).remove(true);
|
|
|
209 |
}, this);
|
|
|
210 |
},
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
* Either set the zindex to the supplied value, or set it to one more than the highest existing
|
|
|
214 |
* dialog in the page.
|
|
|
215 |
*
|
|
|
216 |
* @method applyZIndex
|
|
|
217 |
*/
|
|
|
218 |
applyZIndex: function() {
|
|
|
219 |
var highestzindex = 1040,
|
|
|
220 |
zindexvalue = 1,
|
|
|
221 |
bb = this.get('boundingBox'),
|
|
|
222 |
ol = this.get('maskNode'),
|
|
|
223 |
zindex = this.get('zIndex');
|
|
|
224 |
if (zindex !== 0 && !this._calculatedzindex) {
|
|
|
225 |
// The zindex was specified so we should use that.
|
|
|
226 |
bb.setStyle('zIndex', zindex);
|
|
|
227 |
} else {
|
|
|
228 |
// Determine the correct zindex by looking at all existing dialogs and menubars in the page.
|
|
|
229 |
Y.all(DIALOGUE_SELECTOR + ', ' + MENUBAR_SELECTOR + ', ' + DOT + HAS_ZINDEX).each(function(node) {
|
|
|
230 |
var zindex = this.findZIndex(node);
|
|
|
231 |
if (zindex > highestzindex) {
|
|
|
232 |
highestzindex = zindex;
|
|
|
233 |
}
|
|
|
234 |
}, this);
|
|
|
235 |
// Only set the zindex if we found a wrapper.
|
|
|
236 |
zindexvalue = (highestzindex + 1).toString();
|
|
|
237 |
bb.setStyle('zIndex', zindexvalue);
|
|
|
238 |
this.set('zIndex', zindexvalue);
|
|
|
239 |
if (this.get('modal')) {
|
|
|
240 |
ol.setStyle('zIndex', zindexvalue);
|
|
|
241 |
|
|
|
242 |
// In IE8, the z-indexes do not take effect properly unless you toggle
|
|
|
243 |
// the lightbox from 'fixed' to 'static' and back. This code does so
|
|
|
244 |
// using the minimum setTimeouts that still actually work.
|
|
|
245 |
if (Y.UA.ie && Y.UA.compareVersions(Y.UA.ie, 9) < 0) {
|
|
|
246 |
setTimeout(function() {
|
|
|
247 |
ol.setStyle('position', 'static');
|
|
|
248 |
setTimeout(function() {
|
|
|
249 |
ol.setStyle('position', 'fixed');
|
|
|
250 |
}, 0);
|
|
|
251 |
}, 0);
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
this._calculatedzindex = true;
|
|
|
255 |
}
|
|
|
256 |
},
|
|
|
257 |
|
|
|
258 |
/**
|
|
|
259 |
* Finds the zIndex of the given node or its parent.
|
|
|
260 |
*
|
|
|
261 |
* @method findZIndex
|
|
|
262 |
* @param {Node} node The Node to apply the zIndex to.
|
|
|
263 |
* @return {Number} Either the zIndex, or 0 if one was not found.
|
|
|
264 |
*/
|
|
|
265 |
findZIndex: function(node) {
|
|
|
266 |
// In most cases the zindex is set on the parent of the dialog.
|
|
|
267 |
var zindex = node.getStyle('zIndex') || node.ancestor().getStyle('zIndex');
|
|
|
268 |
if (zindex) {
|
|
|
269 |
return parseInt(zindex, 10);
|
|
|
270 |
}
|
|
|
271 |
return 0;
|
|
|
272 |
},
|
|
|
273 |
|
|
|
274 |
/**
|
|
|
275 |
* Event listener for the visibility changed event.
|
|
|
276 |
*
|
|
|
277 |
* @method visibilityChanged
|
|
|
278 |
* @param {EventFacade} e
|
|
|
279 |
*/
|
|
|
280 |
visibilityChanged: function(e) {
|
|
|
281 |
var titlebar, bb;
|
|
|
282 |
if (e.attrName === 'visible') {
|
|
|
283 |
this.get('maskNode').addClass(CSS_CLASSES.LIGHTBOX);
|
|
|
284 |
// Going from visible to hidden.
|
|
|
285 |
if (e.prevVal && !e.newVal) {
|
|
|
286 |
bb = this.get('boundingBox');
|
|
|
287 |
if (this._resizeevent) {
|
|
|
288 |
this._resizeevent.detach();
|
|
|
289 |
this._resizeevent = null;
|
|
|
290 |
}
|
|
|
291 |
if (this._orientationevent) {
|
|
|
292 |
this._orientationevent.detach();
|
|
|
293 |
this._orientationevent = null;
|
|
|
294 |
}
|
|
|
295 |
require(['core/local/aria/focuslock'], function(FocusLockManager) {
|
|
|
296 |
// Untrap focus when the dialogue is hidden.
|
|
|
297 |
FocusLockManager.untrapFocus();
|
|
|
298 |
});
|
|
|
299 |
|
|
|
300 |
if (this.get('modal')) {
|
|
|
301 |
// Hide this dialogue from screen readers.
|
|
|
302 |
this.setAccessibilityHidden();
|
|
|
303 |
}
|
|
|
304 |
}
|
|
|
305 |
// Going from hidden to visible.
|
|
|
306 |
if (!e.prevVal && e.newVal) {
|
|
|
307 |
// This needs to be done each time the dialog is shown as new dialogs may have been opened.
|
|
|
308 |
this.applyZIndex();
|
|
|
309 |
// This needs to be done each time the dialog is shown as the window may have been resized.
|
|
|
310 |
this.makeResponsive();
|
|
|
311 |
if (!this.shouldResizeFullscreen()) {
|
|
|
312 |
if (this.get('draggable')) {
|
|
|
313 |
titlebar = '#' + this.get('id') + ' .' + CSS_CLASSES.HEADER;
|
|
|
314 |
this.plug(Y.Plugin.Drag, {handles: [titlebar]});
|
|
|
315 |
Y.one(titlebar).setStyle('cursor', 'move');
|
|
|
316 |
}
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
// Only do accessibility hiding for modals because the ARIA spec
|
|
|
320 |
// says that all ARIA dialogues should be modal.
|
|
|
321 |
if (this.get('modal')) {
|
|
|
322 |
// Make this dialogue visible to screen readers.
|
|
|
323 |
this.setAccessibilityVisible();
|
|
|
324 |
}
|
|
|
325 |
}
|
|
|
326 |
if (this.get('center') && !e.prevVal && e.newVal) {
|
|
|
327 |
this.centerDialogue();
|
|
|
328 |
}
|
|
|
329 |
}
|
|
|
330 |
},
|
|
|
331 |
/**
|
|
|
332 |
* If the responsive attribute is set on the dialog, and the window size is
|
|
|
333 |
* smaller than the responsive width - make the dialog fullscreen.
|
|
|
334 |
*
|
|
|
335 |
* @method makeResponsive
|
|
|
336 |
*/
|
|
|
337 |
makeResponsive: function() {
|
|
|
338 |
var bb = this.get('boundingBox');
|
|
|
339 |
|
|
|
340 |
if (this.shouldResizeFullscreen()) {
|
|
|
341 |
// Make this dialogue fullscreen on a small screen.
|
|
|
342 |
// Disable the page scrollbars.
|
|
|
343 |
|
|
|
344 |
// Size and position the fullscreen dialog.
|
|
|
345 |
|
|
|
346 |
bb.addClass(DIALOGUE_FULLSCREEN_CLASS);
|
|
|
347 |
bb.setStyles({'left': null,
|
|
|
348 |
'top': null,
|
|
|
349 |
'width': null,
|
|
|
350 |
'height': null,
|
|
|
351 |
'right': null,
|
|
|
352 |
'bottom': null});
|
|
|
353 |
} else {
|
|
|
354 |
if (this.get('responsive')) {
|
|
|
355 |
// We must reset any of the fullscreen changes.
|
|
|
356 |
bb.removeClass(DIALOGUE_FULLSCREEN_CLASS)
|
|
|
357 |
.setStyles({'width': this.get('width'),
|
|
|
358 |
'height': this.get('height')});
|
|
|
359 |
}
|
|
|
360 |
}
|
|
|
361 |
|
|
|
362 |
// Update Lock scroll if the plugin is present.
|
|
|
363 |
if (this.lockScroll) {
|
|
|
364 |
this.lockScroll.updateScrollLock(this.shouldResizeFullscreen());
|
|
|
365 |
}
|
|
|
366 |
},
|
|
|
367 |
/**
|
|
|
368 |
* Center the dialog on the screen.
|
|
|
369 |
*
|
|
|
370 |
* @method centerDialogue
|
|
|
371 |
*/
|
|
|
372 |
centerDialogue: function() {
|
|
|
373 |
var bb = this.get('boundingBox'),
|
|
|
374 |
hidden = bb.hasClass(DIALOGUE_HIDDEN_CLASS),
|
|
|
375 |
x,
|
|
|
376 |
y;
|
|
|
377 |
|
|
|
378 |
// Don't adjust the position if we are in full screen mode.
|
|
|
379 |
if (this.shouldResizeFullscreen()) {
|
|
|
380 |
return;
|
|
|
381 |
}
|
|
|
382 |
if (hidden) {
|
|
|
383 |
bb.setStyle('top', '-1000px').removeClass(DIALOGUE_HIDDEN_CLASS);
|
|
|
384 |
}
|
|
|
385 |
x = Math.max(Math.round((bb.get('winWidth') - bb.get('offsetWidth')) / 2), 15);
|
|
|
386 |
y = Math.max(Math.round((bb.get('winHeight') - bb.get('offsetHeight')) / 2), 15) + Y.one(window).get('scrollTop');
|
|
|
387 |
bb.setStyles({'left': x, 'top': y});
|
|
|
388 |
|
|
|
389 |
if (hidden) {
|
|
|
390 |
bb.addClass(DIALOGUE_HIDDEN_CLASS);
|
|
|
391 |
}
|
|
|
392 |
this.makeResponsive();
|
|
|
393 |
},
|
|
|
394 |
|
|
|
395 |
/**
|
|
|
396 |
* Automatic re-center dialog when dialog size is changed.
|
|
|
397 |
*
|
|
|
398 |
* @method centerDialogOnDialogSizeChange
|
|
|
399 |
* @param {M.core.dialogue} dialog object to apply centering.
|
|
|
400 |
*/
|
|
|
401 |
centerDialogOnDialogSizeChange: function(dialog) {
|
|
|
402 |
// ResizeObserver doesn't get recognized in JSHint.
|
|
|
403 |
// So we need to suppress the false warning.
|
|
|
404 |
var observer = new ResizeObserver(function() { // jshint ignore:line
|
|
|
405 |
dialog.centerDialogOnVisible();
|
|
|
406 |
});
|
|
|
407 |
var bb = dialog.get('boundingBox');
|
|
|
408 |
observer.observe(bb._node, {attributes: true, attributeFilter: ['class']});
|
|
|
409 |
},
|
|
|
410 |
|
|
|
411 |
/**
|
|
|
412 |
* Centering dialog when dialog is visible.
|
|
|
413 |
*
|
|
|
414 |
* @method centerDialogOnVisible
|
|
|
415 |
*/
|
|
|
416 |
centerDialogOnVisible: function() {
|
|
|
417 |
if (!this.get('visible')) {
|
|
|
418 |
return; // Only centre visible dialogue.
|
|
|
419 |
}
|
|
|
420 |
|
|
|
421 |
if (this.name !== DIALOGUE_NAME) {
|
|
|
422 |
return; // Only centre Moodle dialogues.
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
if (this.shouldResizeFullscreen()) {
|
|
|
426 |
this.makeResponsive();
|
|
|
427 |
}
|
|
|
428 |
this.centerDialogue();
|
|
|
429 |
},
|
|
|
430 |
|
|
|
431 |
/**
|
|
|
432 |
* Return whether this dialogue should be fullscreen or not.
|
|
|
433 |
*
|
|
|
434 |
* Responsive attribute must be true and we should not be in an iframe and the screen width should
|
|
|
435 |
* be less than the responsive width.
|
|
|
436 |
*
|
|
|
437 |
* @method shouldResizeFullscreen
|
|
|
438 |
* @return {Boolean}
|
|
|
439 |
*/
|
|
|
440 |
shouldResizeFullscreen: function() {
|
|
|
441 |
return (window === window.parent) && this.get('responsive') &&
|
|
|
442 |
Math.floor(Y.one(document.body).get('winWidth')) < this.get('responsiveWidth');
|
|
|
443 |
},
|
|
|
444 |
|
|
|
445 |
_focus: function() {
|
|
|
446 |
this.focus();
|
|
|
447 |
},
|
|
|
448 |
|
|
|
449 |
show: function() {
|
|
|
450 |
var result = DIALOGUE.superclass.show.call(this);
|
|
|
451 |
if (!this.get('center') && this._originalPosition) {
|
|
|
452 |
// Restore the dialogue position to it's location before it was moved at show time.
|
|
|
453 |
this.get('boundingBox').setXY(this._originalPosition);
|
|
|
454 |
}
|
|
|
455 |
this.applyAndTrapFocus();
|
|
|
456 |
return result;
|
|
|
457 |
},
|
|
|
458 |
|
|
|
459 |
hide: function(e) {
|
|
|
460 |
if (e) {
|
|
|
461 |
// If the event was closed by an escape key event, then we need to check that this
|
|
|
462 |
// dialogue is currently focused to prevent closing all dialogues in the stack.
|
|
|
463 |
if (e.type === 'key' && e.keyCode === 27 && !this.get('focused')) {
|
|
|
464 |
return;
|
|
|
465 |
}
|
|
|
466 |
}
|
|
|
467 |
|
|
|
468 |
// Unlock scroll if the plugin is present.
|
|
|
469 |
if (this.lockScroll) {
|
|
|
470 |
this.lockScroll.disableScrollLock();
|
|
|
471 |
}
|
|
|
472 |
|
|
|
473 |
return DIALOGUE.superclass.hide.call(this, arguments);
|
|
|
474 |
},
|
|
|
475 |
/**
|
|
|
476 |
* Setup key delegation to keep tabbing within the open dialogue.
|
|
|
477 |
*
|
|
|
478 |
* @method keyDelegation
|
|
|
479 |
*/
|
|
|
480 |
keyDelegation: function() {
|
|
|
481 |
Y.log('The keyDelegation function has been deprecated in favour of the AMD core/local/aria/focuslock module');
|
|
|
482 |
var bb = this.get('boundingBox');
|
|
|
483 |
bb.delegate('key', function(e) {
|
|
|
484 |
var target = e.target;
|
|
|
485 |
var direction = 'forward';
|
|
|
486 |
if (e.shiftKey) {
|
|
|
487 |
direction = 'backward';
|
|
|
488 |
}
|
|
|
489 |
if (this.trapFocus(target, direction)) {
|
|
|
490 |
e.preventDefault();
|
|
|
491 |
}
|
|
|
492 |
}, 'down:9', CAN_RECEIVE_FOCUS_SELECTOR, this);
|
|
|
493 |
},
|
|
|
494 |
|
|
|
495 |
/**
|
|
|
496 |
* Trap the tab focus within the open modal.
|
|
|
497 |
*
|
|
|
498 |
* @method trapFocus
|
|
|
499 |
* @param {string} target the element target
|
|
|
500 |
* @param {string} direction tab key for forward and tab+shift for backward
|
|
|
501 |
* @return {Boolean} The result of the focus action.
|
|
|
502 |
*/
|
|
|
503 |
trapFocus: function(target, direction) {
|
|
|
504 |
var bb = this.get('boundingBox'),
|
|
|
505 |
firstitem = bb.one(CAN_RECEIVE_FOCUS_SELECTOR),
|
|
|
506 |
lastitem = bb.all(CAN_RECEIVE_FOCUS_SELECTOR).pop();
|
|
|
507 |
|
|
|
508 |
if (target === lastitem && direction === 'forward') { // Tab key.
|
|
|
509 |
return firstitem.focus();
|
|
|
510 |
} else if (target === firstitem && direction === 'backward') { // Tab+shift key.
|
|
|
511 |
return lastitem.focus();
|
|
|
512 |
}
|
|
|
513 |
},
|
|
|
514 |
|
|
|
515 |
/**
|
|
|
516 |
* Sets the appropriate aria attributes on this dialogue and the other
|
|
|
517 |
* elements in the DOM to ensure that screen readers are able to navigate
|
|
|
518 |
* the dialogue popup correctly.
|
|
|
519 |
*
|
|
|
520 |
* @method setAccessibilityVisible
|
|
|
521 |
*/
|
|
|
522 |
setAccessibilityVisible: function() {
|
|
|
523 |
// Get the element that contains this dialogue because we need it
|
|
|
524 |
// to filter out from the document.body child elements.
|
|
|
525 |
var container = this.get(BASE);
|
|
|
526 |
|
|
|
527 |
// We need to get a list containing each sibling element and the shallowest
|
|
|
528 |
// non-ancestral nodes in the DOM. We can shortcut this a little by leveraging
|
|
|
529 |
// the fact that this dialogue is always appended to the document body therefore
|
|
|
530 |
// it's siblings are the shallowest non-ancestral nodes. If that changes then
|
|
|
531 |
// this code should also be updated.
|
|
|
532 |
Y.one(document.body).get('children').each(function(node) {
|
|
|
533 |
// Skip the element that contains us.
|
|
|
534 |
if (node !== container) {
|
|
|
535 |
var hidden = node.get('aria-hidden');
|
|
|
536 |
// If they are already hidden we can ignore them.
|
|
|
537 |
if (hidden !== 'true') {
|
|
|
538 |
// Save their current state.
|
|
|
539 |
node.setData('previous-aria-hidden', hidden);
|
|
|
540 |
this._hiddenSiblings.push(node);
|
|
|
541 |
|
|
|
542 |
// Hide this node from screen readers.
|
|
|
543 |
node.set('aria-hidden', 'true');
|
|
|
544 |
}
|
|
|
545 |
}
|
|
|
546 |
}, this);
|
|
|
547 |
|
|
|
548 |
// Make us visible to screen readers.
|
|
|
549 |
container.set('aria-hidden', 'false');
|
|
|
550 |
},
|
|
|
551 |
|
|
|
552 |
/**
|
|
|
553 |
* Restores the aria visibility on the DOM elements changed when displaying
|
|
|
554 |
* the dialogue popup and makes the dialogue aria hidden to allow screen
|
|
|
555 |
* readers to navigate the main page correctly when the dialogue is closed.
|
|
|
556 |
*
|
|
|
557 |
* @method setAccessibilityHidden
|
|
|
558 |
*/
|
|
|
559 |
setAccessibilityHidden: function() {
|
|
|
560 |
var container = this.get(BASE);
|
|
|
561 |
container.set('aria-hidden', 'true');
|
|
|
562 |
|
|
|
563 |
// Restore the sibling nodes back to their original values.
|
|
|
564 |
Y.Array.each(this._hiddenSiblings, function(node) {
|
|
|
565 |
var previousValue = node.getData('previous-aria-hidden');
|
|
|
566 |
// If the element didn't previously have an aria-hidden attribute
|
|
|
567 |
// then we can just remove the one we set.
|
|
|
568 |
if (previousValue === null) {
|
|
|
569 |
node.removeAttribute('aria-hidden');
|
|
|
570 |
} else {
|
|
|
571 |
// Otherwise set it back to the old value (which will be false).
|
|
|
572 |
node.set('aria-hidden', previousValue);
|
|
|
573 |
}
|
|
|
574 |
});
|
|
|
575 |
|
|
|
576 |
// Clear the cache. No longer need to store these.
|
|
|
577 |
this._hiddenSiblings = [];
|
|
|
578 |
},
|
|
|
579 |
|
|
|
580 |
/**
|
|
|
581 |
* Focuses on the node specified by focusOnShowSelector, or the first focusable node if nothing is specified.
|
|
|
582 |
* It also traps the focus to the current bounding box.
|
|
|
583 |
*
|
|
|
584 |
* @method applyAndTrapFocus
|
|
|
585 |
*/
|
|
|
586 |
applyAndTrapFocus: function() {
|
|
|
587 |
var content = this.bodyNode;
|
|
|
588 |
var focusSelector = this.get('focusOnShowSelector');
|
|
|
589 |
var focusNode = null;
|
|
|
590 |
|
|
|
591 |
// Try and find a node to focus on using the focusOnShowSelector attribute.
|
|
|
592 |
if (focusSelector !== null) {
|
|
|
593 |
focusNode = this.get('boundingBox').one(focusSelector);
|
|
|
594 |
}
|
|
|
595 |
if (!focusNode) {
|
|
|
596 |
// Fall back to the first focusable element in the body of the dialogue if no focus node was found yet.
|
|
|
597 |
if (content && content !== '') {
|
|
|
598 |
focusNode = content.one(CAN_RECEIVE_FOCUS_SELECTOR);
|
|
|
599 |
}
|
|
|
600 |
}
|
|
|
601 |
require(['core/local/aria/focuslock'], function(FocusLockManager) {
|
|
|
602 |
// Trap focus to the current bounding box.
|
|
|
603 |
FocusLockManager.trapFocus(this.get('boundingBox').getDOMNode());
|
|
|
604 |
if (focusNode) {
|
|
|
605 |
focusNode.focus();
|
|
|
606 |
}
|
|
|
607 |
}.bind(this));
|
|
|
608 |
},
|
|
|
609 |
}, {
|
|
|
610 |
NAME: DIALOGUE_NAME,
|
|
|
611 |
CSS_PREFIX: DIALOGUE_PREFIX,
|
|
|
612 |
ATTRS: {
|
|
|
613 |
/**
|
|
|
614 |
* Any additional classes to add to the base Node.
|
|
|
615 |
*
|
|
|
616 |
* @attribute additionalBaseClass
|
|
|
617 |
* @type String
|
|
|
618 |
* @default ''
|
|
|
619 |
*/
|
|
|
620 |
additionalBaseClass: {
|
|
|
621 |
value: ''
|
|
|
622 |
},
|
|
|
623 |
|
|
|
624 |
/**
|
|
|
625 |
* The Notification base Node.
|
|
|
626 |
*
|
|
|
627 |
* @attribute notificationBase
|
|
|
628 |
* @type Node
|
|
|
629 |
*/
|
|
|
630 |
notificationBase: {
|
|
|
631 |
|
|
|
632 |
},
|
|
|
633 |
|
|
|
634 |
/**
|
|
|
635 |
* Whether to display the dialogue modally and with a
|
|
|
636 |
* lightbox style.
|
|
|
637 |
*
|
|
|
638 |
* @attribute lightbox
|
|
|
639 |
* @type Boolean
|
|
|
640 |
* @default true
|
|
|
641 |
* @deprecated Since Moodle 2.7. Please use modal instead.
|
|
|
642 |
*/
|
|
|
643 |
lightbox: {
|
|
|
644 |
lazyAdd: false,
|
|
|
645 |
setter: function(value) {
|
|
|
646 |
Y.log("The lightbox attribute of M.core.dialogue has been deprecated since Moodle 2.7, " +
|
|
|
647 |
"please use the modal attribute instead",
|
|
|
648 |
'warn', 'moodle-core-notification-dialogue');
|
|
|
649 |
this.set('modal', value);
|
|
|
650 |
}
|
|
|
651 |
},
|
|
|
652 |
|
|
|
653 |
/**
|
|
|
654 |
* Whether to display a close button on the dialogue.
|
|
|
655 |
*
|
|
|
656 |
* Note, we do not recommend hiding the close button as this has
|
|
|
657 |
* potential accessibility concerns.
|
|
|
658 |
*
|
|
|
659 |
* @attribute closeButton
|
|
|
660 |
* @type Boolean
|
|
|
661 |
* @default true
|
|
|
662 |
*/
|
|
|
663 |
closeButton: {
|
|
|
664 |
validator: Y.Lang.isBoolean,
|
|
|
665 |
value: true
|
|
|
666 |
},
|
|
|
667 |
|
|
|
668 |
/**
|
|
|
669 |
* The title for the close button if one is to be shown.
|
|
|
670 |
*
|
|
|
671 |
* @attribute closeButtonTitle
|
|
|
672 |
* @type String
|
|
|
673 |
* @default 'Close'
|
|
|
674 |
*/
|
|
|
675 |
closeButtonTitle: {
|
|
|
676 |
validator: Y.Lang.isString,
|
|
|
677 |
value: M.util.get_string('closebuttontitle', 'moodle')
|
|
|
678 |
},
|
|
|
679 |
|
|
|
680 |
/**
|
|
|
681 |
* Whether to display the dialogue centrally on the screen.
|
|
|
682 |
*
|
|
|
683 |
* @attribute center
|
|
|
684 |
* @type Boolean
|
|
|
685 |
* @default true
|
|
|
686 |
*/
|
|
|
687 |
center: {
|
|
|
688 |
validator: Y.Lang.isBoolean,
|
|
|
689 |
value: true
|
|
|
690 |
},
|
|
|
691 |
|
|
|
692 |
/**
|
|
|
693 |
* Whether to make the dialogue movable around the page.
|
|
|
694 |
*
|
|
|
695 |
* @attribute draggable
|
|
|
696 |
* @type Boolean
|
|
|
697 |
* @default false
|
|
|
698 |
*/
|
|
|
699 |
draggable: {
|
|
|
700 |
validator: Y.Lang.isBoolean,
|
|
|
701 |
value: false
|
|
|
702 |
},
|
|
|
703 |
|
|
|
704 |
/**
|
|
|
705 |
* Used to generate a unique id for the dialogue.
|
|
|
706 |
*
|
|
|
707 |
* @attribute COUNT
|
|
|
708 |
* @type String
|
|
|
709 |
* @default null
|
|
|
710 |
* @writeonce
|
|
|
711 |
*/
|
|
|
712 |
COUNT: {
|
|
|
713 |
writeOnce: true,
|
|
|
714 |
valueFn: function() {
|
|
|
715 |
return Y.stamp(this);
|
|
|
716 |
}
|
|
|
717 |
},
|
|
|
718 |
|
|
|
719 |
/**
|
|
|
720 |
* Used to disable the fullscreen resizing behaviour if required.
|
|
|
721 |
*
|
|
|
722 |
* @attribute responsive
|
|
|
723 |
* @type Boolean
|
|
|
724 |
* @default true
|
|
|
725 |
*/
|
|
|
726 |
responsive: {
|
|
|
727 |
validator: Y.Lang.isBoolean,
|
|
|
728 |
value: true
|
|
|
729 |
},
|
|
|
730 |
|
|
|
731 |
/**
|
|
|
732 |
* The width that this dialogue should be resized to fullscreen.
|
|
|
733 |
*
|
|
|
734 |
* @attribute responsiveWidth
|
|
|
735 |
* @type Number
|
|
|
736 |
* @default 768
|
|
|
737 |
*/
|
|
|
738 |
responsiveWidth: {
|
|
|
739 |
value: 768
|
|
|
740 |
},
|
|
|
741 |
|
|
|
742 |
/**
|
|
|
743 |
* Selector to a node that should recieve focus when this dialogue is shown.
|
|
|
744 |
*
|
|
|
745 |
* The default behaviour is to focus on the header.
|
|
|
746 |
*
|
|
|
747 |
* @attribute focusOnShowSelector
|
|
|
748 |
* @default null
|
|
|
749 |
* @type String
|
|
|
750 |
*/
|
|
|
751 |
focusOnShowSelector: {
|
|
|
752 |
value: null
|
|
|
753 |
}
|
|
|
754 |
}
|
|
|
755 |
});
|
|
|
756 |
|
|
|
757 |
Y.Base.modifyAttrs(DIALOGUE, {
|
|
|
758 |
/**
|
|
|
759 |
* String with units, or number, representing the width of the Widget.
|
|
|
760 |
* If a number is provided, the default unit, defined by the Widgets
|
|
|
761 |
* DEF_UNIT, property is used.
|
|
|
762 |
*
|
|
|
763 |
* If a value of 'auto' is used, then an empty String is instead
|
|
|
764 |
* returned.
|
|
|
765 |
*
|
|
|
766 |
* @attribute width
|
|
|
767 |
* @default '400px'
|
|
|
768 |
* @type {String|Number}
|
|
|
769 |
*/
|
|
|
770 |
width: {
|
|
|
771 |
value: '400px',
|
|
|
772 |
setter: function(value) {
|
|
|
773 |
if (value === 'auto') {
|
|
|
774 |
return '';
|
|
|
775 |
}
|
|
|
776 |
return value;
|
|
|
777 |
}
|
|
|
778 |
},
|
|
|
779 |
|
|
|
780 |
/**
|
|
|
781 |
* Boolean indicating whether or not the Widget is visible.
|
|
|
782 |
*
|
|
|
783 |
* We override this from the default Widget attribute value.
|
|
|
784 |
*
|
|
|
785 |
* @attribute visible
|
|
|
786 |
* @default false
|
|
|
787 |
* @type Boolean
|
|
|
788 |
*/
|
|
|
789 |
visible: {
|
|
|
790 |
value: false
|
|
|
791 |
},
|
|
|
792 |
|
|
|
793 |
/**
|
|
|
794 |
* A convenience Attribute, which can be used as a shortcut for the
|
|
|
795 |
* `align` Attribute.
|
|
|
796 |
*
|
|
|
797 |
* Note: We override this in Moodle such that it sets a value for the
|
|
|
798 |
* `center` attribute if set. The `centered` will always return false.
|
|
|
799 |
*
|
|
|
800 |
* @attribute centered
|
|
|
801 |
* @type Boolean|Node
|
|
|
802 |
* @default false
|
|
|
803 |
*/
|
|
|
804 |
centered: {
|
|
|
805 |
setter: function(value) {
|
|
|
806 |
if (value) {
|
|
|
807 |
this.set('center', true);
|
|
|
808 |
}
|
|
|
809 |
return false;
|
|
|
810 |
}
|
|
|
811 |
},
|
|
|
812 |
|
|
|
813 |
/**
|
|
|
814 |
* Boolean determining whether to render the widget during initialisation.
|
|
|
815 |
*
|
|
|
816 |
* We override this to change the default from false to true for the dialogue.
|
|
|
817 |
* We then proceed to early render the dialogue during our initialisation rather than waiting
|
|
|
818 |
* for YUI to render it after that.
|
|
|
819 |
*
|
|
|
820 |
* @attribute render
|
|
|
821 |
* @type Boolean
|
|
|
822 |
* @default true
|
|
|
823 |
*/
|
|
|
824 |
render: {
|
|
|
825 |
value: true,
|
|
|
826 |
writeOnce: true
|
|
|
827 |
},
|
|
|
828 |
|
|
|
829 |
/**
|
|
|
830 |
* Any additional classes to add to the boundingBox.
|
|
|
831 |
*
|
|
|
832 |
* @attribute extraClasses
|
|
|
833 |
* @type Array
|
|
|
834 |
* @default []
|
|
|
835 |
*/
|
|
|
836 |
extraClasses: {
|
|
|
837 |
value: []
|
|
|
838 |
},
|
|
|
839 |
|
|
|
840 |
/**
|
|
|
841 |
* Identifier for the widget.
|
|
|
842 |
*
|
|
|
843 |
* @attribute id
|
|
|
844 |
* @type String
|
|
|
845 |
* @default a product of guid().
|
|
|
846 |
* @writeOnce
|
|
|
847 |
*/
|
|
|
848 |
id: {
|
|
|
849 |
writeOnce: true,
|
|
|
850 |
valueFn: function() {
|
|
|
851 |
var id = 'moodle-dialogue-' + Y.stamp(this);
|
|
|
852 |
return id;
|
|
|
853 |
}
|
|
|
854 |
},
|
|
|
855 |
|
|
|
856 |
/**
|
|
|
857 |
* Collection containing the widget's buttons.
|
|
|
858 |
*
|
|
|
859 |
* @attribute buttons
|
|
|
860 |
* @type Object
|
|
|
861 |
* @default {}
|
|
|
862 |
*/
|
|
|
863 |
buttons: {
|
|
|
864 |
getter: Y.WidgetButtons.prototype._getButtons,
|
|
|
865 |
setter: Y.WidgetButtons.prototype._setButtons,
|
|
|
866 |
valueFn: function() {
|
|
|
867 |
if (this.get('closeButton') === false) {
|
|
|
868 |
return null;
|
|
|
869 |
} else {
|
|
|
870 |
return [
|
|
|
871 |
{
|
|
|
872 |
section: Y.WidgetStdMod.HEADER,
|
|
|
873 |
classNames: 'closebutton',
|
|
|
874 |
action: function() {
|
|
|
875 |
this.hide();
|
|
|
876 |
}
|
|
|
877 |
}
|
|
|
878 |
];
|
|
|
879 |
}
|
|
|
880 |
}
|
|
|
881 |
}
|
|
|
882 |
});
|
|
|
883 |
|
|
|
884 |
Y.Base.mix(DIALOGUE, [Y.M.core.WidgetFocusAfterHide]);
|
|
|
885 |
|
|
|
886 |
M.core.dialogue = DIALOGUE;
|