MDL-61135 mod_quiz: add question bank AMD modal
[moodle.git] / mod / quiz / amd / src / quizquestionbank.js
blob700142194063b4cc4cd65b6ab97bfbe118804f5a
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 /**
17  * Initialise the question bank modal on the quiz page.
18  *
19  * @module    mod_quiz/quizquestionbank
20  * @package   mod_quiz
21  * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 define(
25     [
26         'jquery',
27         'core/notification',
28         'core/custom_interaction_events',
29         'core/modal_factory',
30         'mod_quiz/modal_quiz_question_bank'
31     ],
32     function(
33         $,
34         Notification,
35         CustomEvents,
36         ModalFactory,
37         ModalQuizQuestionBank
38     ) {
40     var SELECTORS = {
41         ADD_QUESTION_LINKS:   '.menu [data-action="questionbank"]',
42     };
44     return {
45         init: function(contextId) {
46             var body = $('body');
48             // Create a question bank modal using the factory.
49             // The same modal will be used by all of the add question
50             // links on the page. The content of the modal will be
51             // changed depending on which link is clicked.
52             ModalFactory.create(
53                 {
54                     type: ModalQuizQuestionBank.TYPE,
55                     large: true
56                 },
57                 // Created a deligated listener rather than a single
58                 // trigger element.
59                 [body, SELECTORS.ADD_QUESTION_LINKS]
60             ).then(function(modal) {
61                 // Save the Moodle context id that the modal is being rendered in.
62                 modal.setContextId(contextId);
64                 body.on(CustomEvents.events.activate, SELECTORS.ADD_QUESTION_LINKS, function(e) {
65                     // We need to listen for activations on the trigger elements because there are
66                     // several on the page and we need to know which one was activated in order to
67                     // set some relevant data on the modal.
68                     var triggerElement = $(e.target).closest(SELECTORS.ADD_QUESTION_LINKS);
69                     modal.setAddOnPageId(triggerElement.attr('data-addonpage'));
70                     modal.setTitle(triggerElement.attr('data-header'));
71                 });
73                 return modal;
74             }).fail(Notification.exception);
75         }
76     };
77 });