mirrored from git://git.moodle.org/moodle.git
/
moodle-core-chooserdialogue.js
313 lines (257 loc) · 10.3 KB
/
moodle-core-chooserdialogue.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
YUI.add('moodle-core-chooserdialogue', function (Y, NAME) {
var CHOOSERDIALOGUE = function() {
CHOOSERDIALOGUE.superclass.constructor.apply(this, arguments);
};
Y.extend(CHOOSERDIALOGUE, Y.Base, {
// The panel widget
panel: null,
// The submit button - we disable this until an element is set
submitbutton : null,
// The chooserdialogue container
container : null,
// Any event listeners we may need to cancel later
listenevents : [],
bodycontent : null,
headercontent : null,
instanceconfig : null,
// The hidden field storing the disabled element values for submission.
hiddenRadioValue: null,
setup_chooser_dialogue : function(bodycontent, headercontent, config) {
this.bodycontent = bodycontent;
this.headercontent = headercontent;
this.instanceconfig = config;
},
prepare_chooser : function () {
if (this.panel) {
return;
}
// Set Default options
var paramkey,
params = {
bodyContent : this.bodycontent.get('innerHTML'),
headerContent : this.headercontent.get('innerHTML'),
width : '540px',
draggable : true,
visible : false, // Hide by default
zindex : 100, // Display in front of other items
modal: true, // This dialogue should be modal.
shim : true,
closeButtonTitle : this.get('closeButtonTitle'),
render : false
};
// Override with additional options
for (paramkey in this.instanceconfig) {
params[paramkey] = this.instanceconfig[paramkey];
}
// Create the panel
this.panel = new M.core.dialogue(params);
// Remove the template for the chooser
this.bodycontent.remove();
this.headercontent.remove();
// Hide and then render the panel
this.panel.hide();
this.panel.render();
// Set useful links.
this.container = this.panel.get('boundingBox').one('.choosercontainer');
this.options = this.container.all('.option input[type=radio]');
// The hidden form element we use when submitting.
this.hiddenRadioValue = Y.Node.create('<input type="hidden" value="" />');
this.container.one('form').appendChild(this.hiddenRadioValue);
// Add the chooserdialogue class to the container for styling
this.panel.get('boundingBox').addClass('chooserdialogue');
},
/**
* Display the module chooser
*
* @param e Event Triggering Event
* @return void
*/
display_chooser : function (e) {
var bb, dialogue, thisevent;
this.prepare_chooser();
// Stop the default event actions before we proceed
e.preventDefault();
bb = this.panel.get('boundingBox');
dialogue = this.container.one('.alloptions');
// This will detect a change in orientation and retrigger centering
thisevent = Y.one('document').on('orientationchange', function() {
this.center_dialogue(dialogue);
}, this);
this.listenevents.push(thisevent);
// Detect window resizes (most browsers)
thisevent = Y.one('window').on('resize', function() {
this.center_dialogue(dialogue);
}, this);
this.listenevents.push(thisevent);
// These will trigger a check_options call to display the correct help
thisevent = this.container.on('click', this.check_options, this);
this.listenevents.push(thisevent);
thisevent = this.container.on('key_up', this.check_options, this);
this.listenevents.push(thisevent);
thisevent = this.container.on('dblclick', function(e) {
if (e.target.ancestor('div.option')) {
this.check_options();
// Prevent duplicate submissions
this.submitbutton.setAttribute('disabled', 'disabled');
this.options.setAttribute('disabled', 'disabled');
this.cancel_listenevents();
this.container.one('form').submit();
}
}, this);
this.listenevents.push(thisevent);
this.container.one('form').on('submit', function() {
// Prevent duplicate submissions on submit
this.submitbutton.setAttribute('disabled', 'disabled');
this.options.setAttribute('disabled', 'disabled');
this.cancel_listenevents();
}, this);
// Hook onto the cancel button to hide the form
thisevent = this.container.one('.addcancel').on('click', this.cancel_popup, this);
this.listenevents.push(thisevent);
// Hide will be managed by cancel_popup after restoring the body overflow
thisevent = bb.one('button.closebutton').on('click', this.cancel_popup, this);
this.listenevents.push(thisevent);
// Grab global keyup events and handle them
thisevent = Y.one('document').on('keydown', this.handle_key_press, this);
this.listenevents.push(thisevent);
// Add references to various elements we adjust
this.submitbutton = this.container.one('.submitbutton');
// Disable the submit element until the user makes a selection
this.submitbutton.set('disabled', 'true');
// Ensure that the options are shown
this.options.removeAttribute('disabled');
// Display the panel
this.panel.show();
// Re-centre the dialogue after we've shown it.
this.center_dialogue(dialogue);
// Finally, focus the first radio element - this enables form selection via the keyboard
this.container.one('.option input[type=radio]').focus();
// Trigger check_options to set the initial jumpurl
this.check_options();
},
/**
* Cancel any listen events in the listenevents queue
*
* Several locations add event handlers which should only be called before the form is submitted. This provides
* a way of cancelling those events.
*
* @return void
*/
cancel_listenevents : function () {
// Detach all listen events to prevent duplicate triggers
var thisevent;
while (this.listenevents.length) {
thisevent = this.listenevents.shift();
thisevent.detach();
}
},
/**
* Calculate the optimum height of the chooser dialogue
*
* This tries to set a sensible maximum and minimum to ensure that some options are always shown, and preferably
* all, whilst fitting the box within the current viewport.
*
* @param dialogue Y.Node The dialogue
* @return void
*/
center_dialogue : function(dialogue) {
var bb = this.panel.get('boundingBox'),
winheight = bb.get('winHeight'),
newheight, totalheight;
if (this.panel.shouldResizeFullscreen()) {
// No custom sizing required for a fullscreen dialog.
return;
}
// Try and set a sensible max-height -- this must be done before setting the top
// Set a default height of 640px
newheight = this.get('maxheight');
if (winheight <= newheight) {
// Deal with smaller window sizes
if (winheight <= this.get('minheight')) {
newheight = this.get('minheight');
} else {
newheight = winheight;
}
}
// Take off 15px top and bottom for borders, plus 40px each for the title and button area before setting the
// new max-height
totalheight = newheight;
newheight = newheight - (15 + 15 + 40 + 40);
dialogue.setStyle('maxHeight', newheight + 'px');
dialogueheight = bb.getStyle('height');
if (dialogueheight.match(/.*px$/)) {
dialogueheight = dialogueheight.replace(/px$/, '');
} else {
dialogueheight = totalheight;
}
if (dialogueheight < this.get('baseheight')) {
dialogueheight = this.get('baseheight');
dialogue.setStyle('height', dialogueheight + 'px');
}
this.panel.centerDialogue();
},
handle_key_press : function(e) {
if (e.keyCode === 27) {
this.cancel_popup(e);
}
},
cancel_popup : function (e) {
// Prevent normal form submission before hiding
e.preventDefault();
this.hide();
},
hide : function() {
// Cancel all listen events
this.cancel_listenevents();
this.container.detachAll();
this.panel.hide();
},
check_options : function() {
// Check which options are set, and change the parent class
// to show/hide help as required
this.options.each(function(thisoption) {
var optiondiv = thisoption.get('parentNode').get('parentNode');
if (thisoption.get('checked')) {
optiondiv.addClass('selected');
// Trigger any events for this option
this.option_selected(thisoption);
// Ensure that the form may be submitted
this.submitbutton.removeAttribute('disabled');
// Ensure that the radio remains focus so that keyboard navigation is still possible
thisoption.focus();
} else {
optiondiv.removeClass('selected');
}
}, this);
},
option_selected : function(e) {
// Set a hidden input field with the value and name of the radio button. When we submit the form, we
// disable the radios to prevent duplicate submission. This has the result however that the value is never
// submitted so we set this value to a hidden field instead
this.hiddenRadioValue.setAttrs({
value: e.get('value'),
name: e.get('name')
});
}
},
{
NAME : 'moodle-core-chooserdialogue',
ATTRS : {
minheight : {
value : 300
},
baseheight: {
value : 400
},
maxheight : {
value : 660
},
closeButtonTitle : {
validator : Y.Lang.isString,
value : 'Close'
}
}
});
M.core = M.core || {};
M.core.chooserdialogue = CHOOSERDIALOGUE;
}, '@VERSION@', {"requires": ["base", "panel", "moodle-core-notification"]});