mirrored from git://git.moodle.org/moodle.git
/
moodle-core-notification-dialogue.js
448 lines (408 loc) · 13.3 KB
/
moodle-core-notification-dialogue.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
YUI.add('moodle-core-notification-dialogue', function (Y, NAME) {
var DIALOGUE_PREFIX,
BASE,
COUNT,
CONFIRMYES,
CONFIRMNO,
TITLE,
QUESTION,
CSS;
DIALOGUE_PREFIX = 'moodle-dialogue',
BASE = 'notificationBase',
COUNT = 0,
CONFIRMYES = 'yesLabel',
CONFIRMNO = 'noLabel',
TITLE = 'title',
QUESTION = 'question',
CSS = {
BASE : 'moodle-dialogue-base',
WRAP : 'moodle-dialogue-wrap',
HEADER : 'moodle-dialogue-hd',
BODY : 'moodle-dialogue-bd',
CONTENT : 'moodle-dialogue-content',
FOOTER : 'moodle-dialogue-ft',
HIDDEN : 'hidden',
LIGHTBOX : 'moodle-dialogue-lightbox'
};
// Set up the namespace once.
M.core = M.core || {};
/**
* The generic dialogue class for use in Moodle.
*
* @module moodle-core-notification
* @submodule moodle-core-notification-dialogue
*/
var DIALOGUE_NAME = 'Moodle dialogue',
DIALOGUE,
DIALOGUE_FULLSCREEN_CLASS = DIALOGUE_PREFIX + '-fullscreen',
DIALOGUE_HIDDEN_CLASS = DIALOGUE_PREFIX + '-hidden',
DIALOGUE_MODAL_CLASS = 'yui3-widget-modal',
DIALOGUE_SELECTOR =' [role=dialog]',
MENUBAR_SELECTOR = '[role=menubar]',
NOSCROLLING_CLASS = 'no-scrolling';
/**
* A re-usable dialogue box with Moodle classes applied.
*
* @param {Object} config Object literal specifying the dialogue configuration properties.
* @constructor
* @class M.core.dialogue
* @extends Y.Panel
*/
DIALOGUE = function(config) {
COUNT++;
var id = 'moodle-dialogue-'+COUNT;
config.notificationBase =
Y.Node.create('<div class="'+CSS.BASE+'">')
.append(Y.Node.create('<div id="'+id+'" role="dialog" aria-labelledby="'+id+'-header-text" class="'+CSS.WRAP+'"></div>')
.append(Y.Node.create('<div id="'+id+'-header-text" class="'+CSS.HEADER+' yui3-widget-hd"></div>'))
.append(Y.Node.create('<div class="'+CSS.BODY+' yui3-widget-bd"></div>'))
.append(Y.Node.create('<div class="'+CSS.FOOTER+' yui3-widget-ft"></div>')));
Y.one(document.body).append(config.notificationBase);
if (config.additionalBaseClass) {
config.notificationBase.addClass(config.additionalBaseClass);
}
config.srcNode = '#'+id;
config.width = config.width || '400px';
config.visible = config.visible || false;
config.center = config.centered && true;
config.centered = false;
config.COUNT = COUNT;
if (config.width === 'auto') {
delete config.width;
}
// lightbox param to keep the stable versions API.
if (config.lightbox !== false) {
config.modal = true;
}
delete config.lightbox;
// closeButton param to keep the stable versions API.
if (config.closeButton === false) {
config.buttons = null;
} else {
config.buttons = [
{
section: Y.WidgetStdMod.HEADER,
classNames: 'closebutton',
action: function () {
this.hide();
}
}
];
}
DIALOGUE.superclass.constructor.apply(this, [config]);
if (config.closeButton !== false) {
// The buttons constructor does not allow custom attributes
this.get('buttons').header[0].setAttribute('title', this.get('closeButtonTitle'));
}
};
Y.extend(DIALOGUE, Y.Panel, {
// Window resize event listener.
_resizeevent : null,
// Orientation change event listener.
_orientationevent : null,
/**
* Initialise the dialogue.
*
* @method initializer
* @return void
*/
initializer : function(config) {
var bb;
this.render();
this.show();
this.after('visibleChange', this.visibilityChanged, this);
if (config.center) {
this.centerDialogue();
}
if (!config.visible) {
this.hide();
}
this.set('COUNT', COUNT);
// Workaround upstream YUI bug http://yuilibrary.com/projects/yui3/ticket/2532507
// and allow setting of z-index in theme.
bb = this.get('boundingBox');
if (config.extraClasses) {
Y.Array.each(config.extraClasses, bb.addClass, bb);
}
if (config.visible) {
this.applyZIndex();
}
},
/**
* Either set the zindex to the supplied value, or set it to one more than the highest existing
* dialog in the page.
*
* @method visibilityChanged
* @return void
*/
applyZIndex : function() {
var highestzindex = 0,
bb = this.get('boundingBox'),
zindex = this.get('zIndex');
if (zindex) {
// The zindex was specified so we should use that.
bb.setStyle('zIndex', zindex);
} else {
// Determine the correct zindex by looking at all existing dialogs and menubars in the page.
Y.all(DIALOGUE_SELECTOR+', '+MENUBAR_SELECTOR).each(function (node) {
var zindex = this.findZIndex(node);
if (zindex > highestzindex) {
highestzindex = zindex;
}
}, this);
// Only set the zindex if we found a wrapper.
if (highestzindex > 0) {
bb.setStyle('zIndex', (highestzindex + 1).toString());
}
}
},
/**
* Finds the zIndex of the given node or its parent.
*
* @method findZIndex
* @param Node node
* @returns int Return either the zIndex of 0 if one was not found.
*/
findZIndex : function(node) {
// In most cases the zindex is set on the parent of the dialog.
var zindex = node.getStyle('zIndex') || node.ancestor().getStyle('zIndex');
if (zindex) {
return parseInt(zindex, 10);
}
return 0;
},
/**
* Enable or disable document scrolling (see if there are any modal or fullscreen popups).
*
* @method toggleDocumentScrolling
* @param Boolean scroll - If true, allow document scrolling.
* @return void
*/
toggleDocumentScrolling : function() {
var windowroot = Y.one(Y.config.doc.body),
scroll = true,
search;
search = '.' + DIALOGUE_FULLSCREEN_CLASS + ', .' + DIALOGUE_MODAL_CLASS;
Y.all(search).each(function (node) {
if (!node.hasClass(DIALOGUE_HIDDEN_CLASS)) {
scroll = false;
}
});
if (Y.UA.ie > 0) {
// Remember the previous value:
windowroot = Y.one('html');
}
if (scroll) {
if (windowroot.hasClass(NOSCROLLING_CLASS)) {
windowroot.removeClass(NOSCROLLING_CLASS);
}
} else {
windowroot.addClass(NOSCROLLING_CLASS);
}
},
/**
* Event listener for the visibility changed event.
*
* @method visibilityChanged
* @return void
*/
visibilityChanged : function(e) {
var titlebar;
if (e.attrName === 'visible') {
this.get('maskNode').addClass(CSS.LIGHTBOX);
if (e.prevVal && !e.newVal) {
if (this._resizeevent) {
this._resizeevent.detach();
this._resizeevent = null;
}
if (this._orientationevent) {
this._orientationevent.detach();
this._orientationevent = null;
}
}
if (!e.prevVal && e.newVal) {
// This needs to be done each time the dialog is shown as new dialogs may have been opened.
this.applyZIndex();
// This needs to be done each time the dialog is shown as the window may have been resized.
this.makeResponsive();
if (!this.shouldResizeFullscreen()) {
if (this.get('draggable')) {
titlebar = '#' + this.get('id') + ' .' + CSS.HEADER;
this.plug(Y.Plugin.Drag, {handles : [titlebar]});
Y.one(titlebar).setStyle('cursor', 'move');
}
}
}
if (this.get('center') && !e.prevVal && e.newVal) {
this.centerDialogue();
}
this.toggleDocumentScrolling();
}
},
/**
* If the responsive attribute is set on the dialog, and the window size is
* smaller than the responsive width - make the dialog fullscreen.
*
* @method makeResponsive
* @return void
*/
makeResponsive : function() {
var bb = this.get('boundingBox'),
content;
if (this.shouldResizeFullscreen()) {
// Make this dialogue fullscreen on a small screen.
// Disable the page scrollbars.
// Size and position the fullscreen dialog.
bb.addClass(DIALOGUE_PREFIX+'-fullscreen');
bb.setStyles({'left' : null, 'top' : null, 'width' : null, 'height' : null});
content = Y.one('#' + this.get('id') + ' .' + CSS.BODY);
content.setStyle('overflow', 'auto');
} else {
if (this.get('responsive')) {
// We must reset any of the fullscreen changes.
bb.removeClass(DIALOGUE_PREFIX+'-fullscreen')
.setStyles({'overflow' : 'inherit',
'width' : this.get('width'),
'height' : this.get('height')});
content = Y.one('#' + this.get('id') + ' .' + CSS.BODY);
content.setStyle('overflow', 'inherit');
}
}
},
/**
* Center the dialog on the screen.
*
* @method centerDialogue
* @return void
*/
centerDialogue : function() {
var bb = this.get('boundingBox'),
hidden = bb.hasClass(DIALOGUE_HIDDEN_CLASS),
x,
y;
// Don't adjust the position if we are in full screen mode.
if (this.shouldResizeFullscreen()) {
return;
}
if (hidden) {
bb.setStyle('top', '-1000px').removeClass(DIALOGUE_HIDDEN_CLASS);
}
x = Math.max(Math.round((bb.get('winWidth') - bb.get('offsetWidth'))/2), 15);
y = Math.max(Math.round((bb.get('winHeight') - bb.get('offsetHeight'))/2), 15) + Y.one(window).get('scrollTop');
bb.setStyles({ 'left' : x, 'top' : y});
if (hidden) {
bb.addClass(DIALOGUE_HIDDEN_CLASS);
}
},
/**
* Return if this dialogue should be fullscreen or not.
* Responsive attribute must be true and we should not be in an iframe and the screen width should
* be less than the responsive width.
*
* @method shouldResizeFullscreen
* @return Boolean
*/
shouldResizeFullscreen : function() {
return (window === window.parent) && this.get('responsive') &&
Math.floor(Y.one(document.body).get('winWidth')) < this.get('responsiveWidth');
}
}, {
NAME : DIALOGUE_NAME,
CSS_PREFIX : DIALOGUE_PREFIX,
ATTRS : {
notificationBase : {
},
/**
* Whether to display the dialogue modally and with a
* lightbox style.
*
* @attribute lightbox
* @type Boolean
* @default true
*/
lightbox : {
validator : Y.Lang.isBoolean,
value : true
},
/**
* Whether to display a close button on the dialogue.
*
* Note, we do not recommend hiding the close button as this has
* potential accessibility concerns.
*
* @attribute closeButton
* @type Boolean
* @default true
*/
closeButton : {
validator : Y.Lang.isBoolean,
value : true
},
/**
* The title for the close button if one is to be shown.
*
* @attribute closeButtonTitle
* @type String
* @default 'Close'
*/
closeButtonTitle : {
validator : Y.Lang.isString,
value : 'Close'
},
/**
* Whether to display the dialogue centrally on the screen.
*
* @attribute center
* @type Boolean
* @default true
*/
center : {
validator : Y.Lang.isBoolean,
value : true
},
/**
* Whether to make the dialogue movable around the page.
*
* @attribute draggable
* @type Boolean
* @default false
*/
draggable : {
validator : Y.Lang.isBoolean,
value : false
},
/**
* Used to generate a unique id for the dialogue.
*
* @attribute COUNT
* @type Integer
* @default 0
*/
COUNT: {
value: 0
},
/**
* Used to disable the fullscreen resizing behaviour if required.
*
* @attribute responsive
* @type Boolean
* @default true
*/
responsive : {
validator : Y.Lang.isBoolean,
value : true
},
/**
* The width that this dialogue should be resized to fullscreen.
*
* @attribute responsiveWidth
* @type Integer
* @default 768
*/
responsiveWidth : {
value : 768
}
}
});
M.core.dialogue = DIALOGUE;
}, '@VERSION@', {"requires": ["base", "node", "panel", "event-key", "dd-plugin"]});