1 YUI.add('moodle-core-notification-dialogue', function (Y, NAME) {
12 DIALOGUE_PREFIX = 'moodle-dialogue',
13 BASE = 'notificationBase',
15 CONFIRMYES = 'yesLabel',
16 CONFIRMNO = 'noLabel',
18 QUESTION = 'question',
20 BASE : 'moodle-dialogue-base',
21 WRAP : 'moodle-dialogue-wrap',
22 HEADER : 'moodle-dialogue-hd',
23 BODY : 'moodle-dialogue-bd',
24 CONTENT : 'moodle-dialogue-content',
25 FOOTER : 'moodle-dialogue-ft',
27 LIGHTBOX : 'moodle-dialogue-lightbox'
30 // Set up the namespace once.
31 M.core = M.core || {};
33 * The generic dialogue class for use in Moodle.
35 * @module moodle-core-notification
36 * @submodule moodle-core-notification-dialogue
39 var DIALOGUE_NAME = 'Moodle dialogue',
41 DIALOGUE_FULLSCREEN_CLASS,
42 DIALOGUE_HIDDEN_CLASS,
43 EXISTING_WINDOW_SELECTOR,
46 DIALOGUE_MODAL_CLASS = 'yui3-widget-modal';
47 DIALOGUE_FULLSCREEN_CLASS = DIALOGUE_PREFIX+'-fullscreen';
48 DIALOGUE_HIDDEN_CLASS = DIALOGUE_PREFIX+'-hidden';
49 EXISTING_WINDOW_SELECTOR = '[role=dialog]';
50 NOSCROLLING_CLASS = 'no-scrolling';
53 * A re-usable dialogue box with Moodle classes applied.
55 * @param {Object} config Object literal specifying the dialogue configuration properties.
57 * @class M.core.dialogue
60 DIALOGUE = function(config) {
62 var id = 'moodle-dialogue-'+COUNT;
63 config.notificationBase =
64 Y.Node.create('<div class="'+CSS.BASE+'">')
65 .append(Y.Node.create('<div id="'+id+'" role="dialog" aria-labelledby="'+id+'-header-text" class="'+CSS.WRAP+'"></div>')
66 .append(Y.Node.create('<div id="'+id+'-header-text" class="'+CSS.HEADER+' yui3-widget-hd"></div>'))
67 .append(Y.Node.create('<div class="'+CSS.BODY+' yui3-widget-bd"></div>'))
68 .append(Y.Node.create('<div class="'+CSS.FOOTER+' yui3-widget-ft"></div>')));
69 Y.one(document.body).append(config.notificationBase);
71 if (config.additionalBaseClass) {
72 config.notificationBase.addClass(config.additionalBaseClass);
75 config.srcNode = '#'+id;
76 config.width = config.width || '400px';
77 config.visible = config.visible || false;
78 config.center = config.centered && true;
79 config.centered = false;
82 if (config.width === 'auto') {
86 // lightbox param to keep the stable versions API.
87 if (config.lightbox !== false) {
90 delete config.lightbox;
92 // closeButton param to keep the stable versions API.
93 if (config.closeButton === false) {
94 config.buttons = null;
98 section: Y.WidgetStdMod.HEADER,
99 classNames: 'closebutton',
100 action: function () {
106 DIALOGUE.superclass.constructor.apply(this, [config]);
108 if (config.closeButton !== false) {
109 // The buttons constructor does not allow custom attributes
110 this.get('buttons').header[0].setAttribute('title', this.get('closeButtonTitle'));
113 Y.extend(DIALOGUE, Y.Panel, {
114 // Window resize event listener.
116 // Orientation change event listener.
117 _orientationevent : null,
120 * Initialise the dialogue.
122 * @method initializer
125 initializer : function(config) {
130 this.after('visibleChange', this.visibilityChanged, this);
132 this.centerDialogue();
134 if (!config.visible) {
137 this.set('COUNT', COUNT);
139 // Workaround upstream YUI bug http://yuilibrary.com/projects/yui3/ticket/2532507
140 // and allow setting of z-index in theme.
141 bb = this.get('boundingBox');
143 if (config.extraClasses) {
144 Y.Array.each(config.extraClasses, bb.addClass, bb);
146 if (config.visible) {
152 * Either set the zindex to the supplied value, or set it to one more than the highest existing
153 * dialog in the page.
155 * @method visibilityChanged
158 applyZIndex : function() {
159 var highestzindex = 0,
163 bb = this.get('boundingBox');
164 if (this.get('zIndex')) {
165 // The zindex was specified so we should use that.
166 bb.setStyle('zIndex', this.get('zIndex'));
168 // Determine the correct zindex by looking at all existing dialogs in the page.
169 // Get the zindex of the parent of each wrapper node.
170 Y.all(EXISTING_WINDOW_SELECTOR).each(function (node) {
171 zindex = node.getStyle('zIndex');
173 // In most cases the zindex is set on the parent of the dialog.
175 zindex = node.get('parentNode').getStyle('zIndex');
179 zindex = parseInt(zindex, 10);
181 if (zindex > highestzindex) {
182 highestzindex = zindex;
186 // Only set the zindex if we found a wrapper.
187 if (highestzindex > 0) {
188 bb.setStyle('zIndex', highestzindex + 1);
194 * Enable or disable document scrolling (see if there are any modal or fullscreen popups).
196 * @method toggleDocumentScrolling
197 * @param Boolean scroll - If true, allow document scrolling.
200 toggleDocumentScrolling : function() {
201 var windowroot = Y.one(Y.config.doc.body),
205 search = '.' + DIALOGUE_FULLSCREEN_CLASS + ', .' + DIALOGUE_MODAL_CLASS;
206 Y.all(search).each(function (node) {
207 if (!node.hasClass(DIALOGUE_HIDDEN_CLASS)) {
213 // Remember the previous value:
214 windowroot = Y.one('html');
217 if (windowroot.hasClass(NOSCROLLING_CLASS)) {
218 windowroot.removeClass(NOSCROLLING_CLASS);
221 windowroot.addClass(NOSCROLLING_CLASS);
226 * Event listener for the visibility changed event.
228 * @method visibilityChanged
231 visibilityChanged : function(e) {
233 if (e.attrName === 'visible') {
234 this.get('maskNode').addClass(CSS.LIGHTBOX);
235 if (e.prevVal && !e.newVal) {
236 if (this._resizeevent) {
237 this._resizeevent.detach();
238 this._resizeevent = null;
240 if (this._orientationevent) {
241 this._orientationevent.detach();
242 this._orientationevent = null;
245 if (!e.prevVal && e.newVal) {
246 // This needs to be done each time the dialog is shown as new dialogs may have been opened.
248 // This needs to be done each time the dialog is shown as the window may have been resized.
249 this.makeResponsive();
250 if (!this.shouldResizeFullscreen()) {
251 if (this.get('draggable')) {
252 titlebar = '#' + this.get('id') + ' .' + CSS.HEADER;
253 this.plug(Y.Plugin.Drag, {handles : [titlebar]});
254 Y.one(titlebar).setStyle('cursor', 'move');
258 if (this.get('center') && !e.prevVal && e.newVal) {
259 this.centerDialogue();
261 this.toggleDocumentScrolling();
265 * If the responsive attribute is set on the dialog, and the window size is
266 * smaller than the responsive width - make the dialog fullscreen.
268 * @method makeResponsive
271 makeResponsive : function() {
272 var bb = this.get('boundingBox'),
275 if (this.shouldResizeFullscreen()) {
276 // Make this dialogue fullscreen on a small screen.
277 // Disable the page scrollbars.
279 // Size and position the fullscreen dialog.
281 bb.addClass(DIALOGUE_PREFIX+'-fullscreen');
282 bb.setStyles({'left' : null, 'top' : null, 'width' : null, 'height' : null});
284 content = Y.one('#' + this.get('id') + ' .' + CSS.BODY);
285 content.setStyle('overflow', 'auto');
287 if (this.get('responsive')) {
288 // We must reset any of the fullscreen changes.
289 bb.removeClass(DIALOGUE_PREFIX+'-fullscreen')
290 .setStyles({'overflow' : 'inherit',
291 'width' : this.get('width'),
292 'height' : this.get('height')});
293 content = Y.one('#' + this.get('id') + ' .' + CSS.BODY);
294 content.setStyle('overflow', 'inherit');
300 * Center the dialog on the screen.
302 * @method centerDialogue
305 centerDialogue : function() {
306 var bb = this.get('boundingBox'),
307 hidden = bb.hasClass(DIALOGUE_HIDDEN_CLASS),
311 // Don't adjust the position if we are in full screen mode.
312 if (this.shouldResizeFullscreen()) {
316 bb.setStyle('top', '-1000px').removeClass(DIALOGUE_HIDDEN_CLASS);
318 x = Math.max(Math.round((bb.get('winWidth') - bb.get('offsetWidth'))/2), 15);
319 y = Math.max(Math.round((bb.get('winHeight') - bb.get('offsetHeight'))/2), 15) + Y.one(window).get('scrollTop');
320 bb.setStyles({ 'left' : x, 'top' : y});
323 bb.addClass(DIALOGUE_HIDDEN_CLASS);
327 * Return if this dialogue should be fullscreen or not.
328 * Responsive attribute must be true and we should not be in an iframe and the screen width should
329 * be less than the responsive width.
331 * @method shouldResizeFullscreen
334 shouldResizeFullscreen : function() {
335 return (window === window.parent) && this.get('responsive') &&
336 Math.floor(Y.one(document.body).get('winWidth')) < this.get('responsiveWidth');
339 NAME : DIALOGUE_NAME,
340 CSS_PREFIX : DIALOGUE_PREFIX,
347 * Whether to display the dialogue modally and with a
350 * @attribute lightbox
355 validator : Y.Lang.isBoolean,
360 * Whether to display a close button on the dialogue.
362 * Note, we do not recommend hiding the close button as this has
363 * potential accessibility concerns.
365 * @attribute closeButton
370 validator : Y.Lang.isBoolean,
375 * The title for the close button if one is to be shown.
377 * @attribute closeButtonTitle
382 validator : Y.Lang.isString,
387 * Whether to display the dialogue centrally on the screen.
394 validator : Y.Lang.isBoolean,
399 * Whether to make the dialogue movable around the page.
401 * @attribute draggable
406 validator : Y.Lang.isBoolean,
411 * Used to generate a unique id for the dialogue.
422 * Used to disable the fullscreen resizing behaviour if required.
424 * @attribute responsive
429 validator : Y.Lang.isBoolean,
434 * The width that this dialogue should be resized to fullscreen.
436 * @attribute responsiveWidth
446 M.core.dialogue = DIALOGUE;
449 }, '@VERSION@', {"requires": ["base", "node", "panel", "event-key", "dd-plugin"]});