weekly release 3.2dev
[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 });
1e34257e
MG
199
200 // Form for adding standard tags.
201 $('body').on('click', 'a[data-action=addstandardtag]', function(e) {
202 e.preventDefault();
203 str.get_strings([
204 {key : 'addotags', component : 'tag'},
205 {key : 'inputstandardtags', component : 'tag'},
206 {key : 'continue'},
207 {key : 'cancel'},
208 ]).done(function(s) {
209 var el = $('<div><form id="addtags_form" class="form-inline" method="POST">' +
210 '<input type="hidden" name="action" value="addstandardtag"/>' +
211 '<input type="hidden" name="sesskey" value="' + M.cfg.sesskey + '"/>' +
212 '<p><label for="id_tagslist">' + s[1] + '</label>' +
213 '<input type="text" id="id_tagslist" name="tagslist"/></p>' +
214 '<p class="mdl-align"><input type="submit" id="addtags_submit"/>' +
215 '<input type="button" id="addtags_cancel"/></p>' +
216 '</form></div>');
217 el.find('#addtags_form').attr('action', window.location.href);
218 el.find('#addtags_submit').attr('value', s[2]);
219 el.find('#addtags_cancel').attr('value', s[3]);
220 var panel = new M.core.dialogue ({
221 draggable: true,
222 modal: true,
223 closeButton: true,
224 headerContent: s[0],
225 bodyContent: el.html()
226 });
227 panel.show();
228 $('#addtags_form input[type=text]').focus();
229 $('#addtags_form #addtags_cancel').on('click', function() {
230 panel.destroy();
231 });
232 });
233 });
131b78b9
MG
234 },
235
236 /**
237 * Initialises tag collection management page.
238 *
239 * @method initManageCollectionsPage
240 */
241 initManageCollectionsPage: function() {
242 $('body').on('updated', '[data-inplaceeditable]', function(e) {
352bfbf5 243 var ajaxreturn = e.ajaxreturn,
352bfbf5 244 areaid, collid, isenabled;
131b78b9 245 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareaenable') {
352bfbf5 246 areaid = $(this).attr('data-itemid');
131b78b9 247 $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
352bfbf5 248 isenabled = ajaxreturn.value;
131b78b9
MG
249 if (isenabled === '1') {
250 $(this).closest('tr').removeClass('dimmed_text');
352bfbf5 251 collid = $(this).closest('tr').find('[data-itemtype="tagareacollection"]').attr("data-value");
21d9596c
MG
252 $(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]")
253 .removeClass('hidden');
131b78b9
MG
254 } else {
255 $(this).closest('tr').addClass('dimmed_text');
256 }
257 }
258 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareacollection') {
352bfbf5 259 areaid = $(this).attr('data-itemid');
131b78b9 260 $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
352bfbf5
MG
261 collid = $(this).attr('data-value');
262 isenabled = $(this).closest('tr').find('[data-itemtype="tagareaenable"]').attr("data-value");
131b78b9 263 if (isenabled === "1") {
21d9596c
MG
264 $(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]")
265 .removeClass('hidden');
131b78b9
MG
266 }
267 }
352bfbf5
MG
268 });
269
270 $('body').on('click', '.addtagcoll > a', function(e) {
271 e.preventDefault();
272 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
273 str.get_strings([
274 {key : 'addtagcoll', component : 'tag'},
275 {key : 'name'},
276 {key : 'searchable', component : 'tag'},
277 {key : 'create'},
278 {key : 'cancel'},
279 ]).done(function(s) {
280 var el = $('<div><form id="addtagcoll_form" class="form-inline">'+
281 '<p><label for="addtagcoll_name"></label>: ' +
cbecc3c3
MG
282 '<input id="addtagcoll_name" type="text"/></p>' +
283 '<p><label for="addtagcoll_searchable"></label>: ' +
352bfbf5
MG
284 '<input id="addtagcoll_searchable" type="checkbox" value="1" checked/></p>' +
285 '<p class="mdl-align"><input type="submit" id="addtagcoll_submit"/>'+
286 '<input type="button" id="addtagcoll_cancel"/></p>' +
287 '</form></div>');
288 el.find('label[for="addtagcoll_name"]').html(s[1]);
289 el.find('label[for="addtagcoll_searchable"]').html(s[2]);
290 el.find('#addtagcoll_submit').attr('value', s[3]);
291 el.find('#addtagcoll_cancel').attr('value', s[4]);
292 var panel = new M.core.dialogue ({
293 draggable: true,
294 modal: true,
295 closeButton: true,
296 headerContent: s[0],
297 bodyContent: el.html()
298 });
299 panel.show();
300 $('#addtagcoll_form #addtagcoll_name').focus();
301 $('#addtagcoll_form #addtagcoll_cancel').on('click', function() {
302 panel.destroy();
303 });
304 $('#addtagcoll_form').on('submit', function() {
305 var name = $('#addtagcoll_form #addtagcoll_name').val();
306 var searchable = $('#addtagcoll_form #addtagcoll_searchable').prop('checked') ? 1 : 0;
307 if (String(name).length > 0) {
308 window.location.href = href + "&name=" + encodeURIComponent(name) + "&searchable=" + searchable;
309 }
310 return false;
311 });
312 }
313 );
314
315 });
316
317 $('body').on('click', '.tag-collections-table .action_delete', function(e) {
318 e.preventDefault();
319 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
320 str.get_strings([
321 {key : 'delete'},
322 {key : 'suredeletecoll', component : 'tag', param : $(this).attr('data-collname') },
323 {key : 'yes'},
324 {key : 'no'},
325 ]).done(function(s) {
326 notification.confirm(s[0], s[1], s[2], s[3], function() {
327 window.location.href = href;
328 });
329 }
330 );
331 });
8e355853
MG
332 }
333 };
334});