MDL-52707 core_tag: allow to combine tags
[moodle.git] / lib / amd / src / tag.js
CommitLineData
8e355853
MG
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 * AJAX helper for the tag management page.
18 *
19 * @module core/tag
20 * @package core_tag
21 * @copyright 2015 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @since 3.0
24 */
cdc5f978
MG
25define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'],
26 function($, ajax, templates, notification, str) {
8e355853
MG
27 return /** @alias module:core/tag */ {
28
29 /**
c026a28d 30 * Initialises tag index page.
8e355853 31 *
131b78b9 32 * @method initTagindexPage
c026a28d 33 */
131b78b9 34 initTagindexPage: function() {
c026a28d
MG
35 // Click handler for changing tag type.
36 $('body').delegate('.tagarea[data-ta] a[data-quickload=1]', 'click', function(e) {
37 e.preventDefault();
38 var target = $( this ),
39 query = target.context.search.replace(/^\?/, ''),
40 tagarea = target.closest('.tagarea[data-ta]'),
41 args = query.split('&').reduce(function(s,c){var t=c.split('=');s[t[0]]=decodeURIComponent(t[1]);return s;},{});
42
43 var promises = ajax.call([{
44 methodname: 'core_tag_get_tagindex',
45 args: { tagindex: args }
46 }], true);
47
48 $.when.apply($, promises)
49 .done( function(data) {
50 templates.render('core_tag/index', data).done(function(html) {
51 tagarea.replaceWith(html);
52 });
53 });
54 });
55 },
56
57 /**
58 * Initialises tag management page.
59 *
131b78b9 60 * @method initManagePage
8e355853 61 */
131b78b9 62 initManagePage: function() {
8e355853 63
21d9596c
MG
64 // Set cell 'time modified' to 'now' when any of the element is updated in this row.
65 $('body').on('updated', '[data-inplaceeditable]', function(e) {
66 str.get_string('now').done(function(s) {
67 $(e.target).closest('tr').find('td.col-timemodified').html(s);
68 });
69 if (e.ajaxreturn.itemtype === 'tagflag') {
70 var row = $(e.target).closest('tr');
71 if (e.ajaxreturn.value === '0') {
72 row.removeClass('flagged-tag');
73 } else {
74 row.addClass('flagged-tag');
75 }
8e355853 76 }
8e355853
MG
77 });
78
79 // Confirmation for single tag delete link.
80 $('.tag-management-table').delegate('a.tagdelete', 'click', function(e) {
81 e.preventDefault();
82 var href = $(this).attr('href');
83 str.get_strings([
84 {key : 'delete'},
85 {key : 'confirmdeletetag', component : 'tag'},
86 {key : 'yes'},
87 {key : 'no'},
88 ]).done(function(s) {
89 notification.confirm(s[0], s[1], s[2], s[3], function() {
90 window.location.href = href;
91 });
92 }
93 );
94 });
95
96 // Confirmation for bulk tag delete button.
97 $("#tag-management-delete").click(function(e){
98 var form = $(this).closest('form').get(0),
99 cnt = $(form).find("input[type=checkbox]:checked").length;
100 if (!cnt) {
101 return false;
102 }
0d202789 103 var tempElement = $("<input type='hidden'/>").attr('name', this.name);
8e355853
MG
104 e.preventDefault();
105 str.get_strings([
106 {key : 'delete'},
107 {key : 'confirmdeletetags', component : 'tag'},
108 {key : 'yes'},
109 {key : 'no'},
110 ]).done(function(s) {
111 notification.confirm(s[0], s[1], s[2], s[3], function() {
0d202789 112 tempElement.appendTo(form);
8e355853
MG
113 form.submit();
114 });
115 }
116 );
117 });
0d202789
MG
118
119 // Confirmation for bulk tag combine button.
120 $("#tag-management-combine").click(function(e){
121 e.preventDefault();
122 var form = $(this).closest('form').get(0),
123 tags = $(form).find("input[type=checkbox]:checked");
124 if (tags.length <= 1) {
125 str.get_strings([
126 {key : 'combineselected', component : 'tag'},
127 {key : 'selectmultipletags', component : 'tag'},
128 {key : 'ok'},
129 ]).done(function(s) {
130 notification.alert(s[0], s[1], s[2]);
131 }
132 );
133 return false;
134 }
135 var tempElement = $("<input type='hidden'/>").attr('name', this.name);
136 str.get_strings([
137 {key : 'combineselected', component : 'tag'},
138 {key : 'selectmaintag', component : 'tag'},
139 {key : 'continue'},
140 {key : 'cancel'},
141 ]).done(function(s) {
142 var el = $('<div><form id="combinetags_form" class="form-inline">'+
143 '<p class="description"></p><p class="options"></p>' +
144 '<p class="mdl-align"><input type="submit" id="combinetags_submit"/>'+
145 '<input type="button" id="combinetags_cancel"/></p>' +
146 '</form></div>');
147 el.find('.description').html(s[1]);
148 el.find('#combinetags_submit').attr('value', s[2]);
149 el.find('#combinetags_cancel').attr('value', s[3]);
150 var fldset = el.find('.options');
151 tags.each(function() {
152 var tagid = $(this).val(),
153 tagname = $('.inplaceeditable[data-itemtype=tagname][data-itemid='+tagid+']').attr('data-value');
154 fldset.append($('<input type="radio" name="maintag" id="combinetags_maintag_'+tagid+'" value="'+tagid+
155 '"/><label for="combinetags_maintag_'+tagid+'">'+tagname+'</label><br>'));
156 });
157 var panel = new M.core.dialogue ({
158 draggable: true,
159 modal: true,
160 closeButton: true,
161 headerContent: s[0],
162 bodyContent: el.html()
163 });
164 panel.show();
165 $('#combinetags_form input[type=radio]').first().focus().prop('checked', true);
166 $('#combinetags_form #combinetags_cancel').on('click', function() {
167 panel.destroy();
168 });
169 $('#combinetags_form').on('submit', function() {
170 tempElement.appendTo(form);
171 var maintag = $('input[name=maintag]:checked', '#combinetags_form').val();
172 $("<input type='hidden'/>").attr('name', 'maintag').attr('value', maintag).appendTo(form);
173 form.submit();
174 return false;
175 });
176 });
177 });
178
179 // When user changes tag name to some name that already exists suggest to combine the tags.
180 $('body').on('updatefailed', '[data-inplaceeditable][data-itemtype=tagname]', function(e) {
181 var exception = e.exception; // The exception object returned by the callback.
182 var newvalue = e.newvalue; // The value that user tried to udpated the element to.
183 var tagid = $(e.target).attr('data-itemid');
184 if (exception.errorcode === 'namesalreadybeeingused') {
185 e.preventDefault(); // This will prevent default error dialogue.
186 str.get_strings([
187 {key : 'nameuseddocombine', component : 'tag'},
188 {key : 'yes'},
189 {key : 'cancel'},
190 ]).done(function(s) {
191 notification.confirm(e.message, s[0], s[1], s[2], function() {
192 window.location.href = window.location.href + "&newname=" + encodeURIComponent(newvalue) +
193 "&tagid=" + encodeURIComponent(tagid) +
194 '&action=renamecombine&sesskey=' + M.cfg.sesskey;
195 });
196 });
197 }
198 });
131b78b9
MG
199 },
200
201 /**
202 * Initialises tag collection management page.
203 *
204 * @method initManageCollectionsPage
205 */
206 initManageCollectionsPage: function() {
207 $('body').on('updated', '[data-inplaceeditable]', function(e) {
352bfbf5 208 var ajaxreturn = e.ajaxreturn,
352bfbf5 209 areaid, collid, isenabled;
131b78b9 210 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareaenable') {
352bfbf5 211 areaid = $(this).attr('data-itemid');
131b78b9 212 $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
352bfbf5 213 isenabled = ajaxreturn.value;
131b78b9
MG
214 if (isenabled === '1') {
215 $(this).closest('tr').removeClass('dimmed_text');
352bfbf5 216 collid = $(this).closest('tr').find('[data-itemtype="tagareacollection"]').attr("data-value");
21d9596c
MG
217 $(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]")
218 .removeClass('hidden');
131b78b9
MG
219 } else {
220 $(this).closest('tr').addClass('dimmed_text');
221 }
222 }
223 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareacollection') {
352bfbf5 224 areaid = $(this).attr('data-itemid');
131b78b9 225 $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
352bfbf5
MG
226 collid = $(this).attr('data-value');
227 isenabled = $(this).closest('tr').find('[data-itemtype="tagareaenable"]').attr("data-value");
131b78b9 228 if (isenabled === "1") {
21d9596c
MG
229 $(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]")
230 .removeClass('hidden');
131b78b9
MG
231 }
232 }
352bfbf5
MG
233 });
234
235 $('body').on('click', '.addtagcoll > a', function(e) {
236 e.preventDefault();
237 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
238 str.get_strings([
239 {key : 'addtagcoll', component : 'tag'},
240 {key : 'name'},
241 {key : 'searchable', component : 'tag'},
242 {key : 'create'},
243 {key : 'cancel'},
244 ]).done(function(s) {
245 var el = $('<div><form id="addtagcoll_form" class="form-inline">'+
246 '<p><label for="addtagcoll_name"></label>: ' +
cbecc3c3
MG
247 '<input id="addtagcoll_name" type="text"/></p>' +
248 '<p><label for="addtagcoll_searchable"></label>: ' +
352bfbf5
MG
249 '<input id="addtagcoll_searchable" type="checkbox" value="1" checked/></p>' +
250 '<p class="mdl-align"><input type="submit" id="addtagcoll_submit"/>'+
251 '<input type="button" id="addtagcoll_cancel"/></p>' +
252 '</form></div>');
253 el.find('label[for="addtagcoll_name"]').html(s[1]);
254 el.find('label[for="addtagcoll_searchable"]').html(s[2]);
255 el.find('#addtagcoll_submit').attr('value', s[3]);
256 el.find('#addtagcoll_cancel').attr('value', s[4]);
257 var panel = new M.core.dialogue ({
258 draggable: true,
259 modal: true,
260 closeButton: true,
261 headerContent: s[0],
262 bodyContent: el.html()
263 });
264 panel.show();
265 $('#addtagcoll_form #addtagcoll_name').focus();
266 $('#addtagcoll_form #addtagcoll_cancel').on('click', function() {
267 panel.destroy();
268 });
269 $('#addtagcoll_form').on('submit', function() {
270 var name = $('#addtagcoll_form #addtagcoll_name').val();
271 var searchable = $('#addtagcoll_form #addtagcoll_searchable').prop('checked') ? 1 : 0;
272 if (String(name).length > 0) {
273 window.location.href = href + "&name=" + encodeURIComponent(name) + "&searchable=" + searchable;
274 }
275 return false;
276 });
277 }
278 );
279
280 });
281
282 $('body').on('click', '.tag-collections-table .action_delete', function(e) {
283 e.preventDefault();
284 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
285 str.get_strings([
286 {key : 'delete'},
287 {key : 'suredeletecoll', component : 'tag', param : $(this).attr('data-collname') },
288 {key : 'yes'},
289 {key : 'no'},
290 ]).done(function(s) {
291 notification.confirm(s[0], s[1], s[2], s[3], function() {
292 window.location.href = href;
293 });
294 }
295 );
296 });
8e355853
MG
297 }
298 };
299});