1 |
efrain |
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 |
* Functionality for the form element defaultcustom
|
|
|
18 |
*
|
|
|
19 |
* @module core_form/defaultcustom
|
|
|
20 |
* @copyright 2017 Marina Glancy
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @since 3.3
|
|
|
23 |
*/
|
|
|
24 |
define(['jquery'], function($) {
|
|
|
25 |
var onChangeSelect = function(event) {
|
|
|
26 |
var element = $(event.target),
|
|
|
27 |
defaultvalue = JSON.parse(element.attr('data-defaultvalue')),
|
|
|
28 |
customvalue = JSON.parse(element.attr('data-customvalue')),
|
|
|
29 |
type = element.attr('data-type'),
|
|
|
30 |
form = element.closest('form'),
|
|
|
31 |
elementName = element.attr('name').replace(/\[customize\]$/, '[value]'),
|
|
|
32 |
newvalue = element.prop('checked') ? customvalue : defaultvalue;
|
|
|
33 |
|
|
|
34 |
if (type === 'text') {
|
|
|
35 |
form.find('[name="' + elementName + '"]').val(newvalue);
|
|
|
36 |
} else if (type === 'date_selector') {
|
|
|
37 |
form.find('[name="' + elementName + '[day]"]').val(newvalue.day);
|
|
|
38 |
form.find('[name="' + elementName + '[month]"]').val(newvalue.month);
|
|
|
39 |
form.find('[name="' + elementName + '[year]"]').val(newvalue.year);
|
|
|
40 |
} else if (type === 'date_time_selector') {
|
|
|
41 |
form.find('[name="' + elementName + '[day]"]').val(newvalue.day);
|
|
|
42 |
form.find('[name="' + elementName + '[month]"]').val(newvalue.month);
|
|
|
43 |
form.find('[name="' + elementName + '[year]"]').val(newvalue.year);
|
|
|
44 |
form.find('[name="' + elementName + '[hour]"]').val(newvalue.hour);
|
|
|
45 |
form.find('[name="' + elementName + '[minute]"]').val(newvalue.minute);
|
|
|
46 |
}
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
var selector = 'input[data-defaultcustom=true]';
|
|
|
50 |
$('body').on('change', selector, onChangeSelect);
|
|
|
51 |
});
|