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/modal_factory', 'core/modal_events'],
26 function($, ajax, templates, notification, str, ModalFactory, ModalEvents) {
27 return /** @alias module:core/tag */ {
30 * Initialises tag index page.
32 * @method initTagindexPage
34 initTagindexPage: function() {
35 // Click handler for changing tag type.
36 $('body').delegate('.tagarea[data-ta] a[data-quickload=1]', 'click', function(e) {
39 query = target[0].search.replace(/^\?/, ''),
40 tagarea = target.closest('.tagarea[data-ta]'),
41 args = query.split('&').reduce(function(s, c) {
43 s[t[0]] = decodeURIComponent(t[1]);
47 var promises = ajax.call([{
48 methodname: 'core_tag_get_tagindex',
49 args: {tagindex: args}
52 $.when.apply($, promises)
53 .done(function(data) {
54 templates.render('core_tag/index', data).done(function(html) {
55 tagarea.replaceWith(html);
62 * Initialises tag management page.
64 * @method initManagePage
66 initManagePage: function() {
68 // Set cell 'time modified' to 'now' when any of the element is updated in this row.
69 $('body').on('updated', '[data-inplaceeditable]', function(e) {
70 str.get_string('selecttag', 'core_tag', e.ajaxreturn.value)
72 return $('label[for="tagselect' + e.ajaxreturn.itemid + '"]').html(s);
74 .fail(notification.exception);
75 str.get_string('now').done(function(s) {
76 $(e.target).closest('tr').find('td.col-timemodified').html(s);
78 if (e.ajaxreturn.itemtype === 'tagflag') {
79 var row = $(e.target).closest('tr');
80 if (e.ajaxreturn.value === '0') {
81 row.removeClass('flagged-tag');
83 row.addClass('flagged-tag');
88 // Confirmation for single tag delete link.
89 $('.tag-management-table').delegate('a.tagdelete', 'click', function(e) {
91 var href = $(this).attr('href');
94 {key: 'confirmdeletetag', component: 'tag'},
98 notification.confirm(s[0], s[1], s[2], s[3], function() {
99 window.location.href = href;
105 // Confirmation for bulk tag delete button.
106 $("#tag-management-delete").click(function(e) {
107 var form = $(this).closest('form').get(0),
108 cnt = $(form).find("input[type=checkbox]:checked").length;
112 var tempElement = $("<input type='hidden'/>").attr('name', this.name);
116 {key: 'confirmdeletetags', component: 'tag'},
119 ]).done(function(s) {
120 notification.confirm(s[0], s[1], s[2], s[3], function() {
121 tempElement.appendTo(form);
128 // Confirmation for bulk tag combine button.
129 $("#tag-management-combine").click(function(e) {
131 var form = $(this).closest('form').get(0),
132 tags = $(form).find("input[type=checkbox]:checked");
133 if (tags.length <= 1) {
135 {key: 'combineselected', component: 'tag'},
136 {key: 'selectmultipletags', component: 'tag'},
138 ]).done(function(s) {
139 notification.alert(s[0], s[1], s[2]);
144 var tempElement = $("<input type='hidden'/>").attr('name', this.name);
145 var saveButtonText = '';
147 tags.each(function() {
148 var tagid = $(this).val(),
149 tagname = $('.inplaceeditable[data-itemtype=tagname][data-itemid=' + tagid + ']').attr('data-value');
157 {key: 'combineselected', component: 'tag'},
159 ]).then(function(langStrings) {
160 var modalTitle = langStrings[0];
161 saveButtonText = langStrings[1];
162 var templateContext = {
165 return ModalFactory.create({
167 body: templates.render('core_tag/combine_tags', templateContext),
168 type: ModalFactory.types.SAVE_CANCEL
170 }).then(function(modal) {
171 modal.setSaveButtonText(saveButtonText);
173 // Handle save event.
174 modal.getRoot().on(ModalEvents.save, function(e) {
177 // Append this temp element in the form in the tags list, not the form in the modal. Confusing, right?!?
178 tempElement.appendTo(form);
179 // Get the selected tag from the modal.
180 var maintag = $('input[name=maintag]:checked', '#combinetags_form').val();
181 // Append this in the tags list form.
182 $("<input type='hidden'/>").attr('name', 'maintag').attr('value', maintag).appendTo(form);
183 // Submit the tags list form.
187 // Handle hidden event.
188 modal.getRoot().on(ModalEvents.hidden, function() {
189 // Destroy when hidden.
194 // Tick the first option.
195 $('#combinetags_form input[type=radio]').first().focus().prop('checked', true);
199 }).catch(notification.exception);
202 // When user changes tag name to some name that already exists suggest to combine the tags.
203 $('body').on('updatefailed', '[data-inplaceeditable][data-itemtype=tagname]', function(e) {
204 var exception = e.exception; // The exception object returned by the callback.
205 var newvalue = e.newvalue; // The value that user tried to udpated the element to.
206 var tagid = $(e.target).attr('data-itemid');
207 if (exception.errorcode === 'namesalreadybeeingused') {
208 e.preventDefault(); // This will prevent default error dialogue.
210 {key: 'nameuseddocombine', component: 'tag'},
213 ]).done(function(s) {
214 notification.confirm(e.message, s[0], s[1], s[2], function() {
215 window.location.href = window.location.href + "&newname=" + encodeURIComponent(newvalue) +
216 "&tagid=" + encodeURIComponent(tagid) +
217 '&action=renamecombine&sesskey=' + M.cfg.sesskey;
223 // Form for adding standard tags.
224 $('body').on('click', 'a[data-action=addstandardtag]', function(e) {
227 var saveButtonText = '';
229 {key: 'addotags', component: 'tag'},
231 ]).then(function(langStrings) {
232 var modalTitle = langStrings[0];
233 saveButtonText = langStrings[1];
234 var templateContext = {
235 actionurl: window.location.href,
236 sesskey: M.cfg.sesskey
238 return ModalFactory.create({
240 body: templates.render('core_tag/add_tags', templateContext),
241 type: ModalFactory.types.SAVE_CANCEL
243 }).then(function(modal) {
244 modal.setSaveButtonText(saveButtonText);
246 // Handle save event.
247 modal.getRoot().on(ModalEvents.save, function(e) {
248 var tagsInput = $(e.currentTarget).find('#id_tagslist');
249 var name = tagsInput.val().trim();
251 // Set the text field's value to the trimmed value.
254 // Add submit event listener to the form.
255 var tagsForm = $('#addtags_form');
256 tagsForm.on('submit', function(e) {
257 // Validate the form.
258 var form = $('#addtags_form');
259 if (form[0].checkValidity() === false) {
263 form.addClass('was-validated');
265 // BS2 compatibility.
266 $('[data-region="tagslistinput"]').addClass('error');
267 var errorMessage = $('#id_tagslist_error_message');
268 errorMessage.removeAttr('hidden');
269 errorMessage.addClass('help-block');
272 // Try to submit the form.
278 // Handle hidden event.
279 modal.getRoot().on(ModalEvents.hidden, function() {
280 // Destroy when hidden.
288 }).catch(notification.exception);
293 * Initialises tag collection management page.
295 * @method initManageCollectionsPage
297 initManageCollectionsPage: function() {
298 $('body').on('updated', '[data-inplaceeditable]', function(e) {
299 var ajaxreturn = e.ajaxreturn,
300 areaid, collid, isenabled;
301 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareaenable') {
302 areaid = $(this).attr('data-itemid');
303 $(".tag-collections-table ul[data-collectionid] li[data-areaid=" + areaid + "]").hide();
304 isenabled = ajaxreturn.value;
305 if (isenabled === '1') {
306 $(this).closest('tr').removeClass('dimmed_text');
307 collid = $(this).closest('tr').find('[data-itemtype="tagareacollection"]').attr("data-value");
308 $(".tag-collections-table ul[data-collectionid=" + collid + "] li[data-areaid=" + areaid + "]").show();
310 $(this).closest('tr').addClass('dimmed_text');
313 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareacollection') {
314 areaid = $(this).attr('data-itemid');
315 $(".tag-collections-table ul[data-collectionid] li[data-areaid=" + areaid + "]").hide();
316 collid = $(this).attr('data-value');
317 isenabled = $(this).closest('tr').find('[data-itemtype="tagareaenable"]').attr("data-value");
318 if (isenabled === "1") {
319 $(".tag-collections-table ul[data-collectionid=" + collid + "] li[data-areaid=" + areaid + "]").show();
324 $('body').on('click', '.addtagcoll > a', function(e) {
336 var href = $(this).attr('data-url');
337 var saveButtonText = '';
338 str.get_strings(keys).then(function(langStrings) {
339 var modalTitle = langStrings[0];
340 saveButtonText = langStrings[1];
341 var templateContext = {
343 sesskey: M.cfg.sesskey
345 return ModalFactory.create({
347 body: templates.render('core_tag/add_tag_collection', templateContext),
348 type: ModalFactory.types.SAVE_CANCEL
350 }).then(function(modal) {
351 modal.setSaveButtonText(saveButtonText);
353 // Handle save event.
354 modal.getRoot().on(ModalEvents.save, function(e) {
355 var collectionInput = $(e.currentTarget).find('#addtagcoll_name');
356 var name = collectionInput.val().trim();
357 // Set the text field's value to the trimmed value.
358 collectionInput.val(name);
360 // Add submit event listener to the form.
361 var form = $('#addtagcoll_form');
362 form.on('submit', function(e) {
363 // Validate the form.
364 if (form[0].checkValidity() === false) {
368 form.addClass('was-validated');
370 // BS2 compatibility.
371 $('[data-region="addtagcoll_nameinput"]').addClass('error');
372 var errorMessage = $('#id_addtagcoll_name_error_message');
373 errorMessage.removeAttr('hidden');
374 errorMessage.addClass('help-block');
377 // Try to submit the form.
383 // Handle hidden event.
384 modal.getRoot().on(ModalEvents.hidden, function() {
385 // Destroy when hidden.
393 }).catch(notification.exception);
396 $('body').on('click', '.tag-collections-table .action_delete', function(e) {
398 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
401 {key: 'suredeletecoll', component: 'tag', param: $(this).attr('data-collname')},
404 ]).done(function(s) {
405 notification.confirm(s[0], s[1], s[2], s[3], function() {
406 window.location.href = href;