1 // This file is part of Moodle - http://moodle.org/
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.
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/>.
17 * AJAX helper for the tag management page.
21 * @copyright 2015 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str', 'core/config'],
26 function($, ajax, templates, notification, str, cfg) {
27 return /** @alias module:core/tag */ {
30 * Initialises handlers for AJAX methods.
34 init_manage_page: function() {
36 var update_modified = function(el) {
37 var row = el.closest('tr').get(0);
39 var td = $(row).find('td.col-timemodified').get(0);
40 str.get_string('now').done(function(s) {
46 // Click handler for changing tag type.
47 $('.tag-management-table').delegate('.tagtype', 'click', function(e) {
49 var target = $( this ),
50 tagid = target.attr('data-id'),
51 currentvalue = target.attr('data-value'),
52 official = (currentvalue === "1") ? 0 : 1;
54 var promises = ajax.call([{
55 methodname: 'core_tag_update_tags',
56 args: { tags : [ { id : tagid , official : official } ] }
58 methodname: 'core_tag_get_tags',
59 args: { tags : [ { id : tagid } ] }
62 $.when.apply($, promises)
63 .done( function(updateresult, data) {
64 if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
65 templates.render('core_tag/tagtype', data.tags[0]).done(function(html) {
66 update_modified(target);
67 var parent = target.parent();
68 target.replaceWith(html);
69 parent.find('.tagtype').get(0).focus();
75 // Click handler for flagging/resetting tag flag.
76 $('.tag-management-table').delegate('.tagflag', 'click', function(e) {
78 var target = $( this ),
79 tagid = target.attr('data-id'),
80 currentvalue = target.attr('data-value'),
81 flag = (currentvalue === "0") ? 1 : 0;
83 var promises = ajax.call([{
84 methodname: 'core_tag_update_tags',
85 args: { tags : [ { id : tagid , flag : flag } ] }
87 methodname: 'core_tag_get_tags',
88 args: { tags : [ { id : tagid } ] }
91 $.when.apply($, promises)
92 .done( function(updateresult, data) {
93 if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
94 var row = target.closest('tr').get(0);
96 if (data.tags[0].flag) {
97 $(row).addClass('flagged-tag');
99 $(row).removeClass('flagged-tag');
102 templates.render('core_tag/tagflag', data.tags[0]).done(function(html) {
103 update_modified(target);
104 var parent = target.parent();
105 target.replaceWith(html);
106 parent.find('.tagflag').get(0).focus();
112 // Confirmation for single tag delete link.
113 $('.tag-management-table').delegate('a.tagdelete', 'click', function(e) {
115 var href = $(this).attr('href');
118 {key : 'confirmdeletetag', component : 'tag'},
121 ]).done(function(s) {
122 notification.confirm(s[0], s[1], s[2], s[3], function() {
123 window.location.href = href;
129 // Confirmation for bulk tag delete button.
130 $("#tag-management-delete").click(function(e){
131 var form = $(this).closest('form').get(0),
132 cnt = $(form).find("input[type=checkbox]:checked").length;
139 {key : 'confirmdeletetags', component : 'tag'},
142 ]).done(function(s) {
143 notification.confirm(s[0], s[1], s[2], s[3], function() {
151 $('.tag-management-table').delegate('.tagnameedit', 'click keypress', function(e) {
152 if (e.type === 'keypress' && e.keyCode !== 13) {
155 e.stopImmediatePropagation();
157 var target = $(this),
158 tdelement = $( target.closest('td').get(0) ),
159 inputelement = $( tdelement.find('input').get(0) ),
160 tagid = target.attr('data-id');
162 var change_name = function(tagid, newname) {
163 var promises = ajax.call([{
164 methodname: 'core_tag_update_tags',
165 args: { tags : [ { id : tagid , rawname : newname } ] }
167 methodname: 'core_tag_get_tags',
168 args: { tags : [ { id : tagid } ] }
171 $.when.apply($, promises)
172 .done( function(updateresult, data) {
173 if (updateresult.warnings[0] !== undefined) {
174 str.get_string('error').done(function(s) {
175 notification.alert(s, updateresult.warnings[0].message);
177 } else if (data.tags[0] !== undefined) {
178 templates.render('core_tag/tagname', data.tags[0]).done(function(html) {
179 update_modified(tdelement);
180 tdelement.html(html);
181 $(tdelement.find('.tagnameedit').get(0)).focus();
187 var turn_editing_off = function() {
188 $('.tag-management-table td.tageditingon').each(function() {
190 input = $( td.find('input').get(0) );
192 td.removeClass('tageditingon');
193 // Reset input value to the one that was there before editing.
194 input.val(td.attr('data-value'));
198 // Turn editing on for the current element and register handler for Enter/Esc keys.
200 tdelement.addClass('tageditingon');
201 tdelement.attr('data-value', inputelement.val());
202 inputelement.select();
203 inputelement.on('keypress focusout', function(e) {
204 if (cfg.behatsiterunning && e.type === 'focusout') {
205 // Behat triggers focusout too often.
208 if (e.type === 'keypress' && e.keyCode === 13) {
209 change_name(tagid, inputelement.val());
212 if ((e.type === 'keypress' && e.keyCode === 27) || e.type === 'focusout') {