16095641699cd0782cefea4f5c952e18d6d50b55
[moodle.git] / lib / amd / src / tag.js
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/>.
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  */
25 define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str', 'core/yui'],
26         function($, ajax, templates, notification, str, Y) {
27     return /** @alias module:core/tag */ {
29         /**
30          * Initialises tag index page.
31          *
32          * @method initTagindexPage
33          */
34         initTagindexPage: function() {
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[0].search.replace(/^\?/, ''),
40                     tagarea = target.closest('.tagarea[data-ta]'),
41                     args = query.split('&').reduce(function(s, c) {
42                       var t = c.split('=');
43                       s[t[0]] = decodeURIComponent(t[1]);
44                       return s;
45                     }, {});
47                 var promises = ajax.call([{
48                     methodname: 'core_tag_get_tagindex',
49                     args: {tagindex: args}
50                 }], true);
52                 $.when.apply($, promises)
53                     .done(function(data) {
54                         templates.render('core_tag/index', data).done(function(html) {
55                             tagarea.replaceWith(html);
56                         });
57                     });
58             });
59         },
61         /**
62          * Initialises tag management page.
63          *
64          * @method initManagePage
65          */
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)
71                     .then(function(s) {
72                         return $('label[for="tagselect' + e.ajaxreturn.itemid + '"]').html(s);
73                     })
74                     .fail(notification.exception);
75                 str.get_string('now').done(function(s) {
76                     $(e.target).closest('tr').find('td.col-timemodified').html(s);
77                 });
78                 if (e.ajaxreturn.itemtype === 'tagflag') {
79                     var row = $(e.target).closest('tr');
80                     if (e.ajaxreturn.value === '0') {
81                         row.removeClass('flagged-tag');
82                     } else {
83                         row.addClass('flagged-tag');
84                     }
85                 }
86             });
88             // Confirmation for single tag delete link.
89             $('.tag-management-table').delegate('a.tagdelete', 'click', function(e) {
90                 e.preventDefault();
91                 var href = $(this).attr('href');
92                 str.get_strings([
93                         {key: 'delete'},
94                         {key: 'confirmdeletetag', component: 'tag'},
95                         {key: 'yes'},
96                         {key: 'no'},
97                     ]).done(function(s) {
98                         notification.confirm(s[0], s[1], s[2], s[3], function() {
99                             window.location.href = href;
100                         });
101                     }
102                 );
103             });
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;
109                 if (!cnt) {
110                     return;
111                 }
112                 var tempElement = $("<input type='hidden'/>").attr('name', this.name);
113                 e.preventDefault();
114                 str.get_strings([
115                         {key: 'delete'},
116                         {key: 'confirmdeletetags', component: 'tag'},
117                         {key: 'yes'},
118                         {key: 'no'},
119                     ]).done(function(s) {
120                         notification.confirm(s[0], s[1], s[2], s[3], function() {
121                             tempElement.appendTo(form);
122                             form.submit();
123                         });
124                     }
125                 );
126             });
128             // Confirmation for bulk tag combine button.
129             $("#tag-management-combine").click(function(e) {
130                 e.preventDefault();
131                 var form = $(this).closest('form').get(0),
132                     tags = $(form).find("input[type=checkbox]:checked");
133                 if (tags.length <= 1) {
134                     str.get_strings([
135                         {key: 'combineselected', component: 'tag'},
136                         {key: 'selectmultipletags', component: 'tag'},
137                         {key: 'ok'},
138                     ]).done(function(s) {
139                             notification.alert(s[0], s[1], s[2]);
140                         }
141                     );
142                     return;
143                 }
144                 var tempElement = $("<input type='hidden'/>").attr('name', this.name);
145                 str.get_strings([
146                     {key: 'combineselected', component: 'tag'},
147                     {key: 'selectmaintag', component: 'tag'},
148                     {key: 'continue'},
149                     {key: 'cancel'},
150                 ]).done(function(s) {
151                     var el = $('<div><form id="combinetags_form" class="form-inline">' +
152                         '<p class="description"></p><p class="options"></p>' +
153                         '<p class="mdl-align"><input type="submit" class="btn btn-primary" id="combinetags_submit"/>' +
154                         '<input type="button" class="btn btn-secondary" id="combinetags_cancel"/></p>' +
155                         '</form></div>');
156                     el.find('.description').html(s[1]);
157                     el.find('#combinetags_submit').attr('value', s[2]);
158                     el.find('#combinetags_cancel').attr('value', s[3]);
159                     var fldset = el.find('.options');
160                     tags.each(function() {
161                         var tagid = $(this).val(),
162                             tagname = $('.inplaceeditable[data-itemtype=tagname][data-itemid=' + tagid + ']').attr('data-value');
163                         fldset.append($('<input type="radio" name="maintag" id="combinetags_maintag_' + tagid + '" value="' +
164                             tagid + '"/><label for="combinetags_maintag_' + tagid + '">' + tagname + '</label><br>'));
165                     });
166                     // TODO: MDL-57778 Convert to core/modal.
167                     Y.use('moodle-core-notification-dialogue', function() {
168                         var panel = new M.core.dialogue({
169                             draggable: true,
170                             modal: true,
171                             closeButton: true,
172                             headerContent: s[0],
173                             bodyContent: el.html()
174                         });
175                         panel.show();
176                         $('#combinetags_form input[type=radio]').first().focus().prop('checked', true);
177                         $('#combinetags_form #combinetags_cancel').on('click', function() {
178                             panel.destroy();
179                         });
180                         $('#combinetags_form').on('submit', function() {
181                             tempElement.appendTo(form);
182                             var maintag = $('input[name=maintag]:checked', '#combinetags_form').val();
183                             $("<input type='hidden'/>").attr('name', 'maintag').attr('value', maintag).appendTo(form);
184                             form.submit();
185                             return false;
186                         });
187                     });
188                 });
189             });
191             // When user changes tag name to some name that already exists suggest to combine the tags.
192             $('body').on('updatefailed', '[data-inplaceeditable][data-itemtype=tagname]', function(e) {
193                 var exception = e.exception; // The exception object returned by the callback.
194                 var newvalue = e.newvalue; // The value that user tried to udpated the element to.
195                 var tagid = $(e.target).attr('data-itemid');
196                 if (exception.errorcode === 'namesalreadybeeingused') {
197                     e.preventDefault(); // This will prevent default error dialogue.
198                     str.get_strings([
199                         {key: 'nameuseddocombine', component: 'tag'},
200                         {key: 'yes'},
201                         {key: 'cancel'},
202                     ]).done(function(s) {
203                         notification.confirm(e.message, s[0], s[1], s[2], function() {
204                             window.location.href = window.location.href + "&newname=" + encodeURIComponent(newvalue) +
205                                 "&tagid=" + encodeURIComponent(tagid) +
206                                 '&action=renamecombine&sesskey=' + M.cfg.sesskey;
207                         });
208                     });
209                 }
210             });
212             // Form for adding standard tags.
213             $('body').on('click', 'a[data-action=addstandardtag]', function(e) {
214                 e.preventDefault();
215                 str.get_strings([
216                     {key: 'addotags', component: 'tag'},
217                     {key: 'inputstandardtags', component: 'tag'},
218                     {key: 'continue'},
219                     {key: 'cancel'},
220                 ]).done(function(s) {
221                     var el = $('<div><form id="addtags_form" class="form-inline" method="POST">' +
222                         '<input type="hidden" name="action" value="addstandardtag"/>' +
223                         '<input type="hidden" name="sesskey" value="' + M.cfg.sesskey + '"/>' +
224                         '<p><label for="id_tagslist">' + s[1] + '</label>' +
225                         '<input type="text" id="id_tagslist" name="tagslist"/></p>' +
226                         '<p class="mdl-align"><input type="submit" class="btn btn-primary" id="addtags_submit"/>' +
227                         '<input type="button" class="btn btn-secondary" id="addtags_cancel"/></p>' +
228                         '</form></div>');
229                     el.find('#addtags_form').attr('action', window.location.href);
230                     el.find('#addtags_submit').attr('value', s[2]);
231                     el.find('#addtags_cancel').attr('value', s[3]);
232                     // TODO: MDL-57778 Convert to core/modal.
233                     Y.use('moodle-core-notification-dialogue', function() {
234                         var panel = new M.core.dialogue({
235                             draggable: true,
236                             modal: true,
237                             closeButton: true,
238                             headerContent: s[0],
239                             bodyContent: el.html()
240                         });
241                         panel.show();
242                         $('#addtags_form input[type=text]').focus();
243                         $('#addtags_form #addtags_cancel').on('click', function() {
244                             panel.destroy();
245                         });
246                     });
247                 });
248             });
249         },
251         /**
252          * Initialises tag collection management page.
253          *
254          * @method initManageCollectionsPage
255          */
256         initManageCollectionsPage: function() {
257             $('body').on('updated', '[data-inplaceeditable]', function(e) {
258                 var ajaxreturn = e.ajaxreturn,
259                     areaid, collid, isenabled;
260                 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareaenable') {
261                     areaid = $(this).attr('data-itemid');
262                     $(".tag-collections-table ul[data-collectionid] li[data-areaid=" + areaid + "]").hide();
263                     isenabled = ajaxreturn.value;
264                     if (isenabled === '1') {
265                         $(this).closest('tr').removeClass('dimmed_text');
266                         collid = $(this).closest('tr').find('[data-itemtype="tagareacollection"]').attr("data-value");
267                         $(".tag-collections-table ul[data-collectionid=" + collid + "] li[data-areaid=" + areaid + "]").show();
268                     } else {
269                         $(this).closest('tr').addClass('dimmed_text');
270                     }
271                 }
272                 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareacollection') {
273                     areaid = $(this).attr('data-itemid');
274                     $(".tag-collections-table ul[data-collectionid] li[data-areaid=" + areaid + "]").hide();
275                     collid = $(this).attr('data-value');
276                     isenabled = $(this).closest('tr').find('[data-itemtype="tagareaenable"]').attr("data-value");
277                     if (isenabled === "1") {
278                         $(".tag-collections-table ul[data-collectionid=" + collid + "] li[data-areaid=" + areaid + "]").show();
279                     }
280                 }
281             });
283             $('body').on('click', '.addtagcoll > a', function(e) {
284                 e.preventDefault();
285                 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
286                 str.get_strings([
287                         {key: 'addtagcoll', component: 'tag'},
288                         {key: 'name'},
289                         {key: 'searchable', component: 'tag'},
290                         {key: 'create'},
291                         {key: 'cancel'},
292                     ]).done(function(s) {
293                         var el = $('<div><form id="addtagcoll_form" class="form-inline">' +
294                             '<p><label for="addtagcoll_name"></label>: ' +
295                             '<input id="addtagcoll_name" type="text"/></p>' +
296                             '<p><label for="addtagcoll_searchable"></label>: ' +
297                             '<input id="addtagcoll_searchable" type="checkbox" value="1" checked/></p>' +
298                             '<p class="mdl-align"><input type="submit" class="btn btn-primary" id="addtagcoll_submit"/>' +
299                             '<input type="button" class="btn btn-secondary" id="addtagcoll_cancel"/></p>' +
300                             '</form></div>');
301                         el.find('label[for="addtagcoll_name"]').html(s[1]);
302                         el.find('label[for="addtagcoll_searchable"]').html(s[2]);
303                         el.find('#addtagcoll_submit').attr('value', s[3]);
304                         el.find('#addtagcoll_cancel').attr('value', s[4]);
305                         // TODO: MDL-57778 Convert to core/modal.
306                         Y.use('moodle-core-notification-dialogue', function() {
307                             var panel = new M.core.dialogue({
308                                 draggable: true,
309                                 modal: true,
310                                 closeButton: true,
311                                 headerContent: s[0],
312                                 bodyContent: el.html()
313                             });
314                             panel.show();
315                             $('#addtagcoll_form #addtagcoll_name').focus();
316                             $('#addtagcoll_form #addtagcoll_cancel').on('click', function() {
317                                 panel.destroy();
318                             });
319                             $('#addtagcoll_form').on('submit', function() {
320                                 var name = $('#addtagcoll_form #addtagcoll_name').val();
321                                 var searchable = $('#addtagcoll_form #addtagcoll_searchable').prop('checked') ? 1 : 0;
322                                 if (String(name).length > 0) {
323                                     window.location.href = href + "&name=" + encodeURIComponent(name) + "&searchable=" + searchable;
324                                 }
325                                 return false;
326                             });
327                         });
328                     }
329                 );
331             });
333             $('body').on('click', '.tag-collections-table .action_delete', function(e) {
334                 e.preventDefault();
335                 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
336                 str.get_strings([
337                         {key: 'delete'},
338                         {key: 'suredeletecoll', component: 'tag', param: $(this).attr('data-collname')},
339                         {key: 'yes'},
340                         {key: 'no'},
341                     ]).done(function(s) {
342                         notification.confirm(s[0], s[1], s[2], s[3], function() {
343                             window.location.href = href;
344                         });
345                     }
346                 );
347             });
348         }
349     };
350 });