MDL-59934 calendar: remove more button from event form modal
[moodle.git] / calendar / amd / src / event_form.js
CommitLineData
aa091225
RW
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.
12//
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/>.
15
16/**
17 * A javascript module to enhance the event form.
18 *
19 * @module core_calendar/event_form
20 * @package core_calendar
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
02e091b1 24define(['jquery'], function($) {
aa091225
RW
25
26 var SELECTORS = {
aa091225
RW
27 EVENT_GROUP_COURSE_ID: '[name="groupcourseid"]',
28 EVENT_GROUP_ID: '[name="groupid"]',
aa091225 29 SELECT_OPTION: 'option',
aa091225
RW
30 };
31
32 /**
33 * Parse the group id select element in the event form and pull out
34 * the course id from the value to allow us to toggle other select
35 * elements based on the course id for the group a user selects.
36 *
37 * This is a little hacky but I couldn't find a better way to pass
38 * the course id for each group id with the limitations of mforms.
39 *
40 * The group id options are rendered with a value like:
41 * "<courseid>-<groupid>"
42 * E.g.
43 * For a group with id 10 in a course with id 3 the value of the
44 * option will be 3-10.
45 *
46 * @method parseGroupSelect
47 * @param {object} formElement The root form element
48 */
49 var parseGroupSelect = function(formElement) {
50 formElement.find(SELECTORS.EVENT_GROUP_ID)
51 .find(SELECTORS.SELECT_OPTION)
52 .each(function(index, element) {
d6942fb5 53 element = $(element);
aa091225
RW
54 var value = element.attr('value');
55 var splits = value.split('-');
56 var courseId = splits[0];
57
58 element.attr('data-course-id', courseId);
59 });
60 };
61
aa091225
RW
62 /**
63 * Listen for when the user changes the group course when configuring
64 * a group event and filter the options in the group select to only
65 * show the groups available within the course the user has selected.
66 *
67 * @method addCourseGroupSelectListeners
68 * @param {object} formElement The root form element
69 */
70 var addCourseGroupSelectListeners = function(formElement) {
71 var courseGroupSelect = formElement.find(SELECTORS.EVENT_GROUP_COURSE_ID);
72 var groupSelect = formElement.find(SELECTORS.EVENT_GROUP_ID);
73 var groupSelectOptions = groupSelect.find(SELECTORS.SELECT_OPTION);
74 var filterGroupSelectOptions = function() {
75 var selectedCourseId = courseGroupSelect.val();
76 var selectedIndex = null;
77
78 groupSelectOptions.each(function(index, element) {
79 element = $(element);
80
81 if (element.attr('data-course-id') == selectedCourseId) {
82 element.removeClass('hidden');
83 element.prop('disabled', false);
84
a49c3d8a 85 if (selectedIndex === null || element.attr('selected')) {
aa091225
RW
86 selectedIndex = index;
87 }
88 } else {
89 element.addClass('hidden');
90 element.prop('disabled', true);
91 }
92 });
93
94 groupSelect.prop('selectedIndex', selectedIndex);
95 };
96
97 courseGroupSelect.on('change', filterGroupSelectOptions);
98 filterGroupSelectOptions();
99 };
100
101 /**
a4af4c96 102 * Initialise all of the form enhancements.
aa091225
RW
103 *
104 * @method init
105 * @param {string} formId The value of the form's id attribute
aa091225 106 */
02e091b1 107 var init = function(formId) {
aa091225
RW
108 var formElement = $('#' + formId);
109
aa091225 110 parseGroupSelect(formElement);
aa091225 111 addCourseGroupSelectListeners(formElement);
aa091225
RW
112 };
113
114 return {
115 init: init,
aa091225
RW
116 };
117});