2 * The generic dialogue class for use in Moodle.
4 * @module moodle-core-notification
5 * @submodule moodle-core-notification-dialogue
8 var DIALOGUE_NAME = 'Moodle dialogue',
10 DIALOGUE_FULLSCREEN_CLASS,
11 DIALOGUE_HIDDEN_CLASS,
12 EXISTING_WINDOW_SELECTOR,
15 DIALOGUE_MODAL_CLASS = 'yui3-widget-modal';
16 DIALOGUE_FULLSCREEN_CLASS = DIALOGUE_PREFIX+'-fullscreen';
17 DIALOGUE_HIDDEN_CLASS = DIALOGUE_PREFIX+'-hidden';
18 EXISTING_WINDOW_SELECTOR = '[role=dialog]';
19 NOSCROLLING_CLASS = 'no-scrolling';
22 * A re-usable dialogue box with Moodle classes applied.
24 * @param {Object} config Object literal specifying the dialogue configuration properties.
26 * @class M.core.dialogue
29 DIALOGUE = function(config) {
31 var id = 'moodle-dialogue-'+COUNT;
32 config.notificationBase =
33 Y.Node.create('<div class="'+CSS.BASE+'">')
34 .append(Y.Node.create('<div id="'+id+'" role="dialog" aria-labelledby="'+id+'-header-text" class="'+CSS.WRAP+'"></div>')
35 .append(Y.Node.create('<div id="'+id+'-header-text" class="'+CSS.HEADER+' yui3-widget-hd"></div>'))
36 .append(Y.Node.create('<div class="'+CSS.BODY+' yui3-widget-bd"></div>'))
37 .append(Y.Node.create('<div class="'+CSS.FOOTER+' yui3-widget-ft"></div>')));
38 Y.one(document.body).append(config.notificationBase);
40 if (config.additionalBaseClass) {
41 config.notificationBase.addClass(config.additionalBaseClass);
44 config.srcNode = '#'+id;
45 config.width = config.width || '400px';
46 config.visible = config.visible || false;
47 config.center = config.centered && true;
48 config.centered = false;
51 if (config.width === 'auto') {
55 // lightbox param to keep the stable versions API.
56 if (config.lightbox !== false) {
59 delete config.lightbox;
61 // closeButton param to keep the stable versions API.
62 if (config.closeButton === false) {
63 config.buttons = null;
67 section: Y.WidgetStdMod.HEADER,
68 classNames: 'closebutton',
75 DIALOGUE.superclass.constructor.apply(this, [config]);
77 if (config.closeButton !== false) {
78 // The buttons constructor does not allow custom attributes
79 this.get('buttons').header[0].setAttribute('title', this.get('closeButtonTitle'));
82 Y.extend(DIALOGUE, Y.Panel, {
83 // Window resize event listener.
85 // Orientation change event listener.
86 _orientationevent : null,
89 * Initialise the dialogue.
94 initializer : function(config) {
99 this.after('visibleChange', this.visibilityChanged, this);
101 this.centerDialogue();
103 if (!config.visible) {
106 this.set('COUNT', COUNT);
108 // Workaround upstream YUI bug http://yuilibrary.com/projects/yui3/ticket/2532507
109 // and allow setting of z-index in theme.
110 bb = this.get('boundingBox');
112 if (config.extraClasses) {
113 Y.Array.each(config.extraClasses, bb.addClass, bb);
115 if (config.visible) {
121 * Either set the zindex to the supplied value, or set it to one more than the highest existing
122 * dialog in the page.
124 * @method visibilityChanged
127 applyZIndex : function() {
128 var highestzindex = 0,
132 bb = this.get('boundingBox');
133 if (this.get('zIndex')) {
134 // The zindex was specified so we should use that.
135 bb.setStyle('zIndex', this.get('zIndex'));
137 // Determine the correct zindex by looking at all existing dialogs in the page.
138 // Get the zindex of the parent of each wrapper node.
139 Y.all(EXISTING_WINDOW_SELECTOR).each(function (node) {
140 zindex = node.getStyle('zIndex');
142 // In most cases the zindex is set on the parent of the dialog.
144 zindex = node.get('parentNode').getStyle('zIndex');
148 zindex = parseInt(zindex, 10);
150 if (zindex > highestzindex) {
151 highestzindex = zindex;
155 // Only set the zindex if we found a wrapper.
156 if (highestzindex > 0) {
157 bb.setStyle('zIndex', highestzindex + 1);
163 * Enable or disable document scrolling (see if there are any modal or fullscreen popups).
165 * @method toggleDocumentScrolling
166 * @param Boolean scroll - If true, allow document scrolling.
169 toggleDocumentScrolling : function() {
170 var windowroot = Y.one(Y.config.doc.body),
174 search = '.' + DIALOGUE_FULLSCREEN_CLASS + ', .' + DIALOGUE_MODAL_CLASS;
175 Y.all(search).each(function (node) {
176 if (!node.hasClass(DIALOGUE_HIDDEN_CLASS)) {
182 // Remember the previous value:
183 windowroot = Y.one('html');
186 if (windowroot.hasClass(NOSCROLLING_CLASS)) {
187 windowroot.removeClass(NOSCROLLING_CLASS);
190 windowroot.addClass(NOSCROLLING_CLASS);
195 * Event listener for the visibility changed event.
197 * @method visibilityChanged
200 visibilityChanged : function(e) {
202 if (e.attrName === 'visible') {
203 this.get('maskNode').addClass(CSS.LIGHTBOX);
204 if (e.prevVal && !e.newVal) {
205 if (this._resizeevent) {
206 this._resizeevent.detach();
207 this._resizeevent = null;
209 if (this._orientationevent) {
210 this._orientationevent.detach();
211 this._orientationevent = null;
214 if (!e.prevVal && e.newVal) {
215 // This needs to be done each time the dialog is shown as new dialogs may have been opened.
217 // This needs to be done each time the dialog is shown as the window may have been resized.
218 this.makeResponsive();
219 if (!this.shouldResizeFullscreen()) {
220 if (this.get('draggable')) {
221 titlebar = '#' + this.get('id') + ' .' + CSS.HEADER;
222 this.plug(Y.Plugin.Drag, {handles : [titlebar]});
223 Y.one(titlebar).setStyle('cursor', 'move');
227 if (this.get('center') && !e.prevVal && e.newVal) {
228 this.centerDialogue();
230 this.toggleDocumentScrolling();
234 * If the responsive attribute is set on the dialog, and the window size is
235 * smaller than the responsive width - make the dialog fullscreen.
237 * @method makeResponsive
240 makeResponsive : function() {
241 var bb = this.get('boundingBox'),
244 if (this.shouldResizeFullscreen()) {
245 // Make this dialogue fullscreen on a small screen.
246 // Disable the page scrollbars.
248 // Size and position the fullscreen dialog.
250 bb.addClass(DIALOGUE_PREFIX+'-fullscreen');
251 bb.setStyles({'left' : null, 'top' : null, 'width' : null, 'height' : null});
253 content = Y.one('#' + this.get('id') + ' .' + CSS.BODY);
254 content.setStyle('overflow', 'auto');
256 if (this.get('responsive')) {
257 // We must reset any of the fullscreen changes.
258 bb.removeClass(DIALOGUE_PREFIX+'-fullscreen')
259 .setStyles({'overflow' : 'inherit',
260 'width' : this.get('width'),
261 'height' : this.get('height')});
262 content = Y.one('#' + this.get('id') + ' .' + CSS.BODY);
263 content.setStyle('overflow', 'inherit');
269 * Center the dialog on the screen.
271 * @method centerDialogue
274 centerDialogue : function() {
275 var bb = this.get('boundingBox'),
276 hidden = bb.hasClass(DIALOGUE_HIDDEN_CLASS),
280 // Don't adjust the position if we are in full screen mode.
281 if (this.shouldResizeFullscreen()) {
285 bb.setStyle('top', '-1000px').removeClass(DIALOGUE_HIDDEN_CLASS);
287 x = Math.max(Math.round((bb.get('winWidth') - bb.get('offsetWidth'))/2), 15);
288 y = Math.max(Math.round((bb.get('winHeight') - bb.get('offsetHeight'))/2), 15) + Y.one(window).get('scrollTop');
289 bb.setStyles({ 'left' : x, 'top' : y});
292 bb.addClass(DIALOGUE_HIDDEN_CLASS);
296 * Return if this dialogue should be fullscreen or not.
297 * Responsive attribute must be true and we should not be in an iframe and the screen width should
298 * be less than the responsive width.
300 * @method shouldResizeFullscreen
303 shouldResizeFullscreen : function() {
304 return (window === window.parent) && this.get('responsive') &&
305 Math.floor(Y.one(document.body).get('winWidth')) < this.get('responsiveWidth');
309 * Override the show method to set keyboard focus on the dialogue.
316 header = this.headerNode,
317 content = this.bodyNode;
319 result = DIALOGUE.superclass.show.call(this);
320 if (header && header !== '') {
322 } else if (content && content !== '') {
328 NAME : DIALOGUE_NAME,
329 CSS_PREFIX : DIALOGUE_PREFIX,
336 * Whether to display the dialogue modally and with a
339 * @attribute lightbox
344 validator : Y.Lang.isBoolean,
349 * Whether to display a close button on the dialogue.
351 * Note, we do not recommend hiding the close button as this has
352 * potential accessibility concerns.
354 * @attribute closeButton
359 validator : Y.Lang.isBoolean,
364 * The title for the close button if one is to be shown.
366 * @attribute closeButtonTitle
371 validator : Y.Lang.isString,
376 * Whether to display the dialogue centrally on the screen.
383 validator : Y.Lang.isBoolean,
388 * Whether to make the dialogue movable around the page.
390 * @attribute draggable
395 validator : Y.Lang.isBoolean,
400 * Used to generate a unique id for the dialogue.
411 * Used to disable the fullscreen resizing behaviour if required.
413 * @attribute responsive
418 validator : Y.Lang.isBoolean,
423 * The width that this dialogue should be resized to fullscreen.
425 * @attribute responsiveWidth
435 M.core.dialogue = DIALOGUE;