Merge branch 'MOODLE_310_MDL-70117' of https://github.com/golenkovm/moodle into MOODL...
[moodle.git] / question / yui / src / chooser / js / chooser.js
1 var SELECTORS = {
2     CREATENEWQUESTION: 'div.createnewquestion',
3     CREATENEWQUESTIONFORM: 'div.createnewquestion form',
4     CHOOSERDIALOGUE: 'div.chooserdialoguebody',
5     CHOOSERHEADER: 'div.choosertitle'
6 };
8 function Chooser() {
9     Chooser.superclass.constructor.apply(this, arguments);
10 }
12 Y.extend(Chooser, M.core.chooserdialogue, {
13     initializer: function() {
14         Y.all('form').each(function(node) {
15             if (/question\/addquestion\.php/.test(node.getAttribute('action'))) {
16                 node.on('submit', this.displayQuestionChooser, this);
17             }
18         }, this);
19     },
20     displayQuestionChooser: function(e) {
21         var dialogue = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERDIALOGUE),
22             header = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERHEADER);
24         if (this.container === null) {
25             // Setup the dialogue, and then prepare the chooser if it's not already been set up.
26             this.setup_chooser_dialogue(dialogue, header, {});
27             this.prepare_chooser();
28         }
30         // Update all of the hidden fields within the questionbank form.
31         var originForm = e.target.ancestor('form', true),
32             targetForm = this.container.one('form'),
33             hiddenElements = originForm.all('input[type="hidden"]');
35         targetForm.all('input.customfield').remove();
36         hiddenElements.each(function(field) {
37             targetForm.appendChild(field.cloneNode())
38                 .removeAttribute('id')
39                 .addClass('customfield');
40         });
42         // Display the chooser dialogue.
43         this.display_chooser(e);
44     }
45 }, {
46     NAME: 'questionChooser'
47 });
49 M.question = M.question || {};
50 M.question.init_chooser = function(config) {
51     return new Chooser(config);
52 };