Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('moodle-core-chooserdialogue', function (Y, NAME) {
2
 
3
/**
4
 * A type of dialogue used as for choosing options.
5
 *
6
 * @module moodle-core-chooserdialogue
7
 */
8
 
9
/**
10
 * A type of dialogue used as for choosing options.
11
 *
12
 * @constructor
13
 * @class M.core.chooserdialogue
14
 */
15
var CHOOSERDIALOGUE = function() {
16
    CHOOSERDIALOGUE.superclass.constructor.apply(this, arguments);
17
};
18
 
19
Y.extend(CHOOSERDIALOGUE, Y.Base, {
20
    // The panel widget
21
    panel: null,
22
 
23
    // The submit button - we disable this until an element is set
24
    submitbutton: null,
25
 
26
    // The chooserdialogue container
27
    container: null,
28
 
29
    // Any event listeners we may need to cancel later
30
    listenevents: [],
31
 
32
    bodycontent: null,
33
    headercontent: null,
34
    instanceconfig: null,
35
 
36
    // The hidden field storing the disabled element values for submission.
37
    hiddenRadioValue: null,
38
 
39
    setup_chooser_dialogue: function(bodycontent, headercontent, config) {
40
        this.bodycontent = bodycontent;
41
        this.headercontent = headercontent;
42
        this.instanceconfig = config;
43
    },
44
 
45
    prepare_chooser: function() {
46
        if (this.panel) {
47
            return;
48
        }
49
 
50
        // Ensure that we're showing the JS version of the chooser.
51
        Y.one(Y.config.doc.body).addClass('jschooser');
52
 
53
        // Set Default options
54
        var paramkey,
55
            params = {
56
                bodyContent: this.bodycontent.get('innerHTML'),
57
                headerContent: this.headercontent.get('innerHTML'),
58
                width: '540px',
59
                draggable: true,
60
                visible: false, // Hide by default
61
                zindex: 100, // Display in front of other items
62
                modal: true, // This dialogue should be modal.
63
                shim: true,
64
                closeButtonTitle: this.get('closeButtonTitle'),
65
                focusOnPreviousTargetAfterHide: true,
66
                render: false,
67
                extraClasses: this._getClassNames()
68
            };
69
 
70
        // Override with additional options
71
        for (paramkey in this.instanceconfig) {
72
          params[paramkey] = this.instanceconfig[paramkey];
73
        }
74
 
75
        // Create the panel
76
        this.panel = new M.core.dialogue(params);
77
 
78
        // Remove the template for the chooser
79
        this.bodycontent.remove();
80
        this.headercontent.remove();
81
 
82
        // Hide and then render the panel
83
        this.panel.hide();
84
        this.panel.render();
85
 
86
        // Set useful links.
87
        this.container = this.panel.get('boundingBox').one('.choosercontainer');
88
        this.options = this.container.all('.option input[type=radio]');
89
 
90
        // The hidden form element we use when submitting.
91
        this.hiddenRadioValue = Y.Node.create('<input type="hidden" value="" />');
92
        this.container.one('form').appendChild(this.hiddenRadioValue);
93
 
94
 
95
        // Add the chooserdialogue class to the container for styling
96
        this.panel.get('boundingBox').addClass('chooserdialogue');
97
    },
98
 
99
    /**
100
      * Display the module chooser
101
      *
102
      * @method display_chooser
103
      * @param {EventFacade} e Triggering Event
104
      */
105
    display_chooser: function(e) {
106
        var bb, dialogue, thisevent;
107
        this.prepare_chooser();
108
 
109
        // Stop the default event actions before we proceed
110
        e.preventDefault();
111
 
112
        bb = this.panel.get('boundingBox');
113
        dialogue = this.container.one('.alloptions');
114
 
115
        // This will detect a change in orientation and retrigger centering
116
        thisevent = Y.one('document').on('orientationchange', function() {
117
            this.center_dialogue(dialogue);
118
        }, this);
119
        this.listenevents.push(thisevent);
120
 
121
        // Detect window resizes (most browsers)
122
        thisevent = Y.one('window').on('resize', function() {
123
            this.center_dialogue(dialogue);
124
        }, this);
125
        this.listenevents.push(thisevent);
126
 
127
        // These will trigger a check_options call to display the correct help
128
        thisevent = this.container.on('click', this.check_options, this);
129
        this.listenevents.push(thisevent);
130
        thisevent = this.container.on('key_up', this.check_options, this);
131
        this.listenevents.push(thisevent);
132
        thisevent = this.container.on('dblclick', function(e) {
133
            if (e.target.ancestor('div.option')) {
134
                this.check_options();
135
 
136
                // Prevent duplicate submissions
137
                this.submitbutton.setAttribute('disabled', 'disabled');
138
                this.options.setAttribute('disabled', 'disabled');
139
                this.cancel_listenevents();
140
 
141
                this.container.one('form').submit();
142
            }
143
        }, this);
144
        this.listenevents.push(thisevent);
145
 
146
        this.container.one('form').on('submit', function() {
147
            // Prevent duplicate submissions on submit
148
            this.submitbutton.setAttribute('disabled', 'disabled');
149
            this.options.setAttribute('disabled', 'disabled');
150
            this.cancel_listenevents();
151
        }, this);
152
 
153
        // Hook onto the cancel button to hide the form
154
        thisevent = this.container.one('.addcancel').on('click', this.cancel_popup, this);
155
        this.listenevents.push(thisevent);
156
 
157
        // Hide will be managed by cancel_popup after restoring the body overflow
158
        thisevent = bb.one('button.closebutton').on('click', this.cancel_popup, this);
159
        this.listenevents.push(thisevent);
160
 
161
        // Grab global keyup events and handle them
162
        thisevent = Y.one('document').on('keydown', this.handle_key_press, this);
163
        this.listenevents.push(thisevent);
164
 
165
        // Add references to various elements we adjust
166
        this.submitbutton = this.container.one('.submitbutton');
167
 
168
        // Disable the submit element until the user makes a selection
169
        this.submitbutton.set('disabled', 'true');
170
 
171
        // Ensure that the options are shown
172
        this.options.removeAttribute('disabled');
173
 
174
        // Display the panel
175
        this.panel.show(e);
176
 
177
        // Re-centre the dialogue after we've shown it.
178
        this.center_dialogue(dialogue);
179
 
180
        // Finally, focus the first radio element - this enables form selection via the keyboard
181
        this.container.one('.option input[type=radio]').focus();
182
 
183
        // Trigger check_options to set the initial jumpurl
184
        this.check_options();
185
    },
186
 
187
    /**
188
     * Cancel any listen events in the listenevents queue
189
     *
190
     * Several locations add event handlers which should only be called before the form is submitted. This provides
191
     * a way of cancelling those events.
192
     *
193
     * @method cancel_listenevents
194
     */
195
    cancel_listenevents: function() {
196
        // Detach all listen events to prevent duplicate triggers
197
        var thisevent;
198
        while (this.listenevents.length) {
199
            thisevent = this.listenevents.shift();
200
            thisevent.detach();
201
        }
202
    },
203
 
204
    /**
205
      * Calculate the optimum height of the chooser dialogue
206
      *
207
      * This tries to set a sensible maximum and minimum to ensure that some options are always shown, and preferably
208
      * all, whilst fitting the box within the current viewport.
209
      *
210
      * @method center_dialogue
211
      * @param Node {dialogue} Y.Node The dialogue
212
      */
213
    center_dialogue: function(dialogue) {
214
        var bb = this.panel.get('boundingBox'),
215
            winheight = bb.get('winHeight'),
216
            newheight, totalheight;
217
 
218
        if (this.panel.shouldResizeFullscreen()) {
219
            dialogue.setStyle('maxHeight', '100%');
220
            dialogue.setStyle('height', 'auto');
221
            this.panel.makeResponsive();
222
            return;
223
        }
224
 
225
        // Try and set a sensible max-height -- this must be done before setting the top
226
        // Set a default height of 640px
227
        newheight = this.get('maxheight');
228
        if (winheight <= newheight) {
229
            // Deal with smaller window sizes
230
            if (winheight <= this.get('minheight')) {
231
                newheight = this.get('minheight');
232
            } else {
233
                newheight = winheight;
234
            }
235
        }
236
 
237
        // If the dialogue is larger than a reasonable minimum height, we
238
        // disable the page scrollbars.
239
        if (newheight > this.get('minheight')) {
240
            // Disable the page scrollbars.
241
            if (this.panel.lockScroll && !this.panel.lockScroll.isActive()) {
242
                this.panel.lockScroll.enableScrollLock(true);
243
            }
244
        } else {
245
            // Re-enable the page scrollbars.
246
            if (this.panel.lockScroll && this.panel.lockScroll.isActive()) {
247
                this.panel.lockScroll.disableScrollLock();
248
            }
249
        }
250
 
251
        // Take off 15px top and bottom for borders, plus 69px for the title and 57px for the
252
        // button area before setting the new max-height.
253
        totalheight = newheight;
254
        newheight = newheight - (69 + 57 + 15 + 15);
255
        dialogue.setStyle('maxHeight', newheight + 'px');
256
 
257
        var dialogueheight = bb.getStyle('height');
258
        if (dialogueheight.match(/.*px$/)) {
259
            dialogueheight = dialogueheight.replace(/px$/, '');
260
        } else {
261
            dialogueheight = totalheight;
262
        }
263
 
264
        if (dialogueheight < this.get('baseheight')) {
265
            dialogueheight = this.get('baseheight');
266
            dialogue.setStyle('height', dialogueheight + 'px');
267
        } else {
268
            dialogue.setStyle('height', 'auto');
269
        }
270
 
271
        this.panel.centerDialogue();
272
    },
273
 
274
    handle_key_press: function(e) {
275
        if (e.keyCode === 27) {
276
            this.cancel_popup(e);
277
        }
278
    },
279
 
280
    cancel_popup: function(e) {
281
        // Prevent normal form submission before hiding
282
        e.preventDefault();
283
        this.hide();
284
    },
285
 
286
    hide: function() {
287
        // Cancel all listen events
288
        this.cancel_listenevents();
289
 
290
        this.container.detachAll();
291
        this.panel.hide();
292
    },
293
 
294
    check_options: function() {
295
        // Check which options are set, and change the parent class
296
        // to show/hide help as required
297
        this.options.each(function(thisoption) {
298
            var optiondiv = thisoption.get('parentNode').get('parentNode');
299
            if (thisoption.get('checked')) {
300
                optiondiv.addClass('selected');
301
 
302
                // Trigger any events for this option
303
                this.option_selected(thisoption);
304
 
305
                // Ensure that the form may be submitted
306
                this.submitbutton.removeAttribute('disabled');
307
 
308
                // Ensure that the radio remains focus so that keyboard navigation is still possible
309
                thisoption.focus();
310
            } else {
311
                optiondiv.removeClass('selected');
312
            }
313
        }, this);
314
    },
315
 
316
    option_selected: function(e) {
317
        // Set a hidden input field with the value and name of the radio button.  When we submit the form, we
318
        // disable the radios to prevent duplicate submission. This has the result however that the value is never
319
        // submitted so we set this value to a hidden field instead
320
        this.hiddenRadioValue.setAttrs({
321
            value: e.get('value'),
322
            name: e.get('name')
323
        });
324
    },
325
 
326
    /**
327
     * Return an array of class names prefixed with 'chooserdialogue-' and
328
     * the name of the type of dialogue.
329
     *
330
     * Note: Class name are converted to lower-case.
331
     *
332
     * If an array of arguments is supplied, each of these is prefixed and
333
     * lower-cased also.
334
     *
335
     * If no arguments are supplied, then the prefix is returned on it's
336
     * own.
337
     *
338
     * @method _getClassNames
339
     * @param {Array} [args] Any additional names to prefix and lower-case.
340
     * @return {Array}
341
     * @private
342
     */
343
    _getClassNames: function(args) {
344
        var prefix = 'chooserdialogue-' + this.name,
345
            results = [];
346
 
347
        results.push(prefix.toLowerCase());
348
        if (args) {
349
            var arg;
350
            for (arg in args) {
351
                results.push((prefix + '-' + arg).toLowerCase());
352
            }
353
        }
354
 
355
        return results;
356
    }
357
},
358
{
359
    NAME: 'moodle-core-chooserdialogue',
360
    ATTRS: {
361
        /**
362
         * The minimum height (in pixels) before resizing is prevented and scroll
363
         * locking disabled.
364
         *
365
         * @attribute minheight
366
         * @type Number
367
         * @default 300
368
         */
369
        minheight: {
370
            value: 300
371
        },
372
 
373
        /**
374
         * The base height??
375
         *
376
         * @attribute baseheight
377
         * @type Number
378
         * @default 400
379
         */
380
        baseheight: {
381
            value: 400
382
        },
383
 
384
        /**
385
         * The maximum height (in pixels) at which we stop resizing.
386
         *
387
         * @attribute maxheight
388
         * @type Number
389
         * @default 300
390
         */
391
        maxheight: {
392
            value: 660
393
        },
394
 
395
        /**
396
         * The title of the close button.
397
         *
398
         * @attribute closeButtonTitle
399
         * @type String
400
         * @default 'Close'
401
         */
402
        closeButtonTitle: {
403
            validator: Y.Lang.isString,
404
            value: 'Close'
405
        }
406
    }
407
});
408
M.core = M.core || {};
409
M.core.chooserdialogue = CHOOSERDIALOGUE;
410
 
411
 
412
}, '@VERSION@', {"requires": ["base", "panel", "moodle-core-notification"]});