MDL-55000 grade: Convert remaining YUI2 -> AMD
[moodle.git] / grade / amd / src / edittree_index.js
CommitLineData
b5233f9b
AN
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 * Handle add/remove competency links.
18 *
19 * @module grade
20 * @package edittree_index
21 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24define([
25 'jquery',
26], function($) {
27 /**
28 * Enhance the edittree functionality.
29 *
30 * @method edittree
31 */
32 var edittree = function() {
33 // Watch items and toggle the move menu accordingly.
34 $('body').on('change', '.itemselect.ignoredirty', edittree.checkMoveMenuState);
35
36 // Watch for the 'All' and 'None' links.
37 $('body').on('click', '[data-action="grade_edittree-index-bulkselect"]', edittree.toggleAllSelectItems);
38
39 // Watch for the weight override checkboxes.
40 $('body').on('change', '.weightoverride', edittree.toggleWeightInput);
41
42 // Watch changes to the bulk move menu and submit.
43 $('#menumoveafter').on('change', function() {
44 var form = $(this).closest('form'),
45 bulkmove = form.find('#bulkmoveinput');
46
47 bulkmove.val(1);
48 form.submit();
49 });
50
51 // CHeck the initial state of the move menu.
52 edittree.checkMoveMenuState();
53 };
54
55 /**
56 * Toggle the weight input field based on its checkbox.
57 *
58 * @method toggleWeightInput
59 * @param {EventFacade} e
60 * @private
61 */
62 edittree.toggleWeightInput = function(e) {
63 e.preventDefault();
64 var node = $(this),
65 row = node.closest('tr');
66
67 $('input[name="weight_' + row.data('itemid') + '"]').prop('disabled', !node.prop('checked'));
68 };
69
70 /**
71 * Toggle all select boxes on or off.
72 *
73 * @method toggleAllSelectItems
74 * @param {EventFacade} e
75 * @private
76 */
77 edittree.toggleAllSelectItems = function(e) {
78 e.preventDefault();
79
80 var node = $(this),
81 row = node.closest('tr');
82 $('.' + row.data('category') + ' .itemselect').prop('checked', node.data('checked'));
83
84 edittree.checkMoveMenuState();
85 };
86
87 /**
88 * Get the move menu.
89 *
90 * @method getMoveMenu
91 * @private
92 * @return {jQuery}
93 */
94 edittree.getMoveMenu = function() {
95 return $('#menumoveafter');
96 };
97
98 /**
99 * Check whether any checkboxes are ticked.
100 *
101 * @method checkMoveMenuState
102 * @private
103 * @return {Boolean}
104 */
105 edittree.checkMoveMenuState = function() {
106 var menu = edittree.getMoveMenu();
107 if (!menu.length) {
108 return false;
109 }
110
111 var selected;
112 $('.itemselect').each(function() {
113 selected = $(this).prop('checked');
114
115 // Return early if any are checked.
116 return !selected;
117 });
118
119 menu.prop('disabled', !selected);
120
121 return selected;
122 };
123
124 return /** @alias module:grade/edittree_index */ {
125 enhance: edittree
126 };
127});