08ff1a405f7cfbad53b3a39cb04ac45bac2f366e
[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'],
26         function($, ajax, templates, notification, str) {
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.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;},{});
43                 var promises = ajax.call([{
44                     methodname: 'core_tag_get_tagindex',
45                     args: { tagindex: args }
46                 }], true);
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         },
57         /**
58          * Initialises tag management page.
59          *
60          * @method initManagePage
61          */
62         initManagePage: function() {
64             var update_modified = function(el) {
65                 var row = el.closest('tr').get(0);
66                 if (row) {
67                     var td = $(row).find('td.col-timemodified').get(0);
68                     str.get_string('now').done(function(s) {
69                         $(td).html(s);
70                     });
71                 }
72             };
74             // Click handler for changing tag type.
75             $('.tag-management-table').delegate('.tagisstandard', 'click', function(e) {
76                 e.preventDefault();
77                 var target = $( this ),
78                     tagid = target.attr('data-id'),
79                     currentvalue = target.attr('data-value'),
80                     isstandard = (currentvalue === "1") ? 0 : 1;
82                 var promises = ajax.call([{
83                     methodname: 'core_tag_update_tags',
84                     args: { tags : [ { id : tagid , isstandard : isstandard } ] }
85                 }, {
86                     methodname: 'core_tag_get_tags',
87                     args: { tags : [ { id : tagid } ] }
88                 }], true);
90                 $.when.apply($, promises)
91                     .done( function(updateresult, data) {
92                         if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
93                             templates.render('core_tag/tagisstandard', data.tags[0]).done(function(html) {
94                                 update_modified(target);
95                                 var parent = target.parent();
96                                 target.replaceWith(html);
97                                 parent.find('.tagisstandard').get(0).focus();
98                             });
99                         }
100                     });
101             });
103             // Click handler for flagging/resetting tag flag.
104             $('.tag-management-table').delegate('.tagflag', 'click', function(e) {
105                 e.preventDefault();
106                 var target = $( this ),
107                     tagid = target.attr('data-id'),
108                     currentvalue = target.attr('data-value'),
109                     flag = (currentvalue === "0") ? 1 : 0;
111                 var promises = ajax.call([{
112                     methodname: 'core_tag_update_tags',
113                     args: { tags : [ { id : tagid , flag : flag } ] }
114                 }, {
115                     methodname: 'core_tag_get_tags',
116                     args: { tags : [ { id : tagid } ] }
117                 }], true);
119                 $.when.apply($, promises)
120                     .done( function(updateresult, data) {
121                         if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
122                             var row = target.closest('tr').get(0);
123                             if (row) {
124                                 if (data.tags[0].flag) {
125                                     $(row).addClass('flagged-tag');
126                                 } else {
127                                     $(row).removeClass('flagged-tag');
128                                 }
129                             }
130                             templates.render('core_tag/tagflag', data.tags[0]).done(function(html) {
131                                 update_modified(target);
132                                 var parent = target.parent();
133                                 target.replaceWith(html);
134                                 parent.find('.tagflag').get(0).focus();
135                             });
136                         }
137                     });
138             });
140             // Confirmation for single tag delete link.
141             $('.tag-management-table').delegate('a.tagdelete', 'click', function(e) {
142                 e.preventDefault();
143                 var href = $(this).attr('href');
144                 str.get_strings([
145                         {key : 'delete'},
146                         {key : 'confirmdeletetag', component : 'tag'},
147                         {key : 'yes'},
148                         {key : 'no'},
149                     ]).done(function(s) {
150                         notification.confirm(s[0], s[1], s[2], s[3], function() {
151                             window.location.href = href;
152                         });
153                     }
154                 );
155             });
157             // Confirmation for bulk tag delete button.
158             $("#tag-management-delete").click(function(e){
159                 var form = $(this).closest('form').get(0),
160                     cnt = $(form).find("input[type=checkbox]:checked").length;
161                 if (!cnt) {
162                     return false;
163                 }
164                 e.preventDefault();
165                 str.get_strings([
166                         {key : 'delete'},
167                         {key : 'confirmdeletetags', component : 'tag'},
168                         {key : 'yes'},
169                         {key : 'no'},
170                     ]).done(function(s) {
171                         notification.confirm(s[0], s[1], s[2], s[3], function() {
172                             form.submit();
173                         });
174                     }
175                 );
176             });
177         },
179         /**
180          * Initialises tag collection management page.
181          *
182          * @method initManageCollectionsPage
183          */
184         initManageCollectionsPage: function() {
185             $('body').on('updated', '[data-inplaceeditable]', function(e) {
186                 var ajaxreturn = e.ajaxreturn,
187                     oldvalue = e.oldvalue,
188                     areaid, collid, isenabled;
189                 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareaenable') {
190                     areaid = $(this).attr('data-itemid');
191                     $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
192                     isenabled = ajaxreturn.value;
193                     if (isenabled === '1') {
194                         $(this).closest('tr').removeClass('dimmed_text');
195                         collid = $(this).closest('tr').find('[data-itemtype="tagareacollection"]').attr("data-value");
196                         $(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]").removeClass('hidden');
197                     } else {
198                         $(this).closest('tr').addClass('dimmed_text');
199                     }
200                 }
201                 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareacollection') {
202                     areaid = $(this).attr('data-itemid');
203                     $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
204                     collid = $(this).attr('data-value');
205                     isenabled = $(this).closest('tr').find('[data-itemtype="tagareaenable"]').attr("data-value");
206                     if (isenabled === "1") {
207                         $(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]").removeClass('hidden');
208                     }
209                 }
210             });
212             $('body').on('click', '.addtagcoll > a', function(e) {
213                 e.preventDefault();
214                 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
215                 str.get_strings([
216                         {key : 'addtagcoll', component : 'tag'},
217                         {key : 'name'},
218                         {key : 'searchable', component : 'tag'},
219                         {key : 'create'},
220                         {key : 'cancel'},
221                     ]).done(function(s) {
222                         var el = $('<div><form id="addtagcoll_form" class="form-inline">'+
223                             '<p><label for="addtagcoll_name"></label>: ' +
224                             '<input id="addtagcoll_name" type="text"/><br/>' +
225                             '<label for="addtagcoll_searchable"></label>: ' +
226                             '<input id="addtagcoll_searchable" type="checkbox" value="1" checked/></p>' +
227                             '<p class="mdl-align"><input type="submit" id="addtagcoll_submit"/>'+
228                             '<input type="button" id="addtagcoll_cancel"/></p>' +
229                             '</form></div>');
230                         el.find('label[for="addtagcoll_name"]').html(s[1]);
231                         el.find('label[for="addtagcoll_searchable"]').html(s[2]);
232                         el.find('#addtagcoll_submit').attr('value', s[3]);
233                         el.find('#addtagcoll_cancel').attr('value', s[4]);
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                         $('#addtagcoll_form #addtagcoll_name').focus();
243                         $('#addtagcoll_form #addtagcoll_cancel').on('click', function() {
244                             panel.destroy();
245                         });
246                         $('#addtagcoll_form').on('submit', function() {
247                             var name = $('#addtagcoll_form #addtagcoll_name').val();
248                             var searchable = $('#addtagcoll_form #addtagcoll_searchable').prop('checked') ? 1 : 0;
249                             if (String(name).length > 0) {
250                                 window.location.href = href + "&name=" + encodeURIComponent(name) + "&searchable=" + searchable;
251                             }
252                             return false;
253                         });
254                     }
255                 );
257             });
259             $('body').on('click', '.tag-collections-table .action_delete', function(e) {
260                 e.preventDefault();
261                 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
262                 str.get_strings([
263                         {key : 'delete'},
264                         {key : 'suredeletecoll', component : 'tag', param : $(this).attr('data-collname') },
265                         {key : 'yes'},
266                         {key : 'no'},
267                     ]).done(function(s) {
268                         notification.confirm(s[0], s[1], s[2], s[3], function() {
269                             window.location.href = href;
270                         });
271                     }
272                 );
273             });
274         }
275     };
276 });