1 YUI.add('moodle-core-chooserdialogue', function(Y) {
3 var CHOOSERDIALOGUE = function() {
4 CHOOSERDIALOGUE.superclass.constructor.apply(this, arguments);
7 Y.extend(CHOOSERDIALOGUE, Y.Base, {
11 // The submit button - we disable this until an element is set
14 // The chooserdialogue container
17 // Any event listeners we may need to cancel later
20 setup_chooser_dialogue : function(bodycontent, headercontent, config) {
21 // Set Default options
23 bodyContent : bodycontent.get('innerHTML'),
24 headerContent : headercontent.get('innerHTML'),
26 visible : false, // Hide by default
27 zindex : 100, // Display in front of other items
28 lightbox : true, // This dialogue should be modal
32 // Override with additional options
33 for (paramkey in config) {
34 params[paramkey] = config[paramkey];
38 this.overlay = new M.core.dialogue(params);
40 // Remove the template for the chooser
42 headercontent.remove();
44 // Hide and then render the overlay
46 this.overlay.render();
49 this.container = this.overlay.get('boundingBox').one('.choosercontainer');
50 this.options = this.container.all('.option input[type=radio]');
52 // Add the chooserdialogue class to the container for styling
53 this.overlay.get('boundingBox').addClass('chooserdialogue');
57 * Display the module chooser
59 * @param e Event Triggering Event
62 display_chooser : function (e) {
63 // Stop the default event actions before we proceed
66 var bb = this.overlay.get('boundingBox');
67 var dialogue = this.container.one('.alloptions');
71 // These will trigger a check_options call to display the correct help
72 thisevent = this.container.on('click', this.check_options, this);
73 this.listenevents.push(thisevent);
74 thisevent = this.container.on('key_up', this.check_options, this);
75 this.listenevents.push(thisevent);
76 thisevent = this.container.on('dblclick', function(e) {
77 if (e.target.ancestor('div.option')) {
80 // Prevent duplicate submissions
81 this.submitbutton.setAttribute('disabled', 'disabled');
82 this.options.setAttribute('disabled', 'disabled');
83 this.cancel_listenevents();
85 this.container.one('form').submit();
88 this.listenevents.push(thisevent);
90 this.container.one('form').on('submit', function(e) {
91 // Prevent duplicate submissions on submit
92 this.submitbutton.setAttribute('disabled', 'disabled');
93 this.options.setAttribute('disabled', 'disabled');
94 this.cancel_listenevents();
97 // Hook onto the cancel button to hide the form
98 this.container.one('#addcancel').on('click', this.cancel_popup, this);
100 // Grab global keyup events and handle them
101 Y.one('document').on('keyup', this.handle_key_press, this);
103 // Add references to various elements we adjust
104 this.jumplink = this.container.one('#jump');
105 this.submitbutton = this.container.one('#submitbutton');
107 // Disable the submit element until the user makes a selection
108 this.submitbutton.set('disabled', 'true');
110 // Ensure that the options are shown
111 this.options.removeAttribute('disabled');
113 // Display the overlay
116 // Re-centre the dialogue after we've shown it.
117 this.center_dialogue(dialogue);
119 // Finally, focus the first radio element - this enables form selection via the keyboard
120 this.container.one('.option input[type=radio]').focus();
122 // Trigger check_options to set the initial jumpurl
123 this.check_options();
127 * Cancel any listen events in the listenevents queue
129 * Several locations add event handlers which should only be called before the form is submitted. This provides
130 * a way of cancelling those events.
134 cancel_listenevents : function () {
135 // Detach all listen events to prevent duplicate triggers
137 while (thisevent = this.listenevents.shift()) {
143 * Calculate the optimum height of the chooser dialogue
145 * This tries to set a sensible maximum and minimum to ensure that some options are always shown, and preferably
146 * all, whilst fitting the box within the current viewport.
148 * @param dialogue Y.Node The dialogue
151 center_dialogue : function(dialogue) {
152 var bb = this.overlay.get('boundingBox');
154 var winheight = bb.get('winHeight');
157 // Try and set a sensible max-height -- this must be done before setting the top
158 // Set a default height of 640px
159 var newheight = this.get('maxheight');
160 if (winheight <= newheight) {
161 // Deal with smaller window sizes
162 if (winheight <= this.get('minheight')) {
163 newheight = this.get('minheight');
165 newheight = winheight;
169 // Set a fixed position if the window is large enough
170 if (newheight > this.get('minheight')) {
171 bb.setStyle('position', 'fixed');
173 bb.setStyle('position', 'absolute');
174 offsettop = Y.one('window').get('scrollTop');
177 // Take off 15px top and bottom for borders, plus 40px each for the title and button area before setting the
179 var totalheight = newheight;
180 newheight = newheight - (15 + 15 + 40 + 40);
181 dialogue.setStyle('max-height', newheight + 'px');
182 dialogue.setStyle('height', newheight + 'px');
184 // Re-calculate the location now that we've changed the size
185 var dialoguetop = Math.max(12, ((winheight - totalheight) / 2)) + offsettop;
187 // We need to set the height for the yui3-widget - can't work
188 // out what we're setting at present -- shoud be the boudingBox
189 bb.setStyle('top', dialoguetop + 'px');
192 handle_key_press : function(e) {
193 if (e.keyCode == 27) {
194 this.cancel_popup(e);
198 cancel_popup : function (e) {
199 // Prevent normal form submission before hiding
205 // Detach the global keypress handler before hiding
206 Y.one('document').detach('keyup', this.handle_key_press, this);
207 this.container.detachAll();
211 check_options : function(e) {
212 // Check which options are set, and change the parent class
213 // to show/hide help as required
214 this.options.each(function(thisoption) {
215 var optiondiv = thisoption.get('parentNode').get('parentNode');
216 if (thisoption.get('checked')) {
217 optiondiv.addClass('selected');
219 // Trigger any events for this option
220 this.option_selected(thisoption);
222 // Ensure that the form may be submitted
223 this.submitbutton.removeAttribute('disabled');
225 // Ensure that the radio remains focus so that keyboard navigation is still possible
228 optiondiv.removeClass('selected');
233 option_selected : function(e) {
237 NAME : 'moodle-core-chooserdialogue',
247 M.core = M.core || {};
248 M.core.chooserdialogue = CHOOSERDIALOGUE;
251 requires:['base', 'overlay', 'moodle-enrol-notification']