MDL-53254 tags: tag collections inplace editable
[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
MG
63
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 };
73
74 // Click handler for changing tag type.
e11d7380 75 $('.tag-management-table').delegate('.tagisstandard', 'click', function(e) {
8e355853
MG
76 e.preventDefault();
77 var target = $( this ),
78 tagid = target.attr('data-id'),
79 currentvalue = target.attr('data-value'),
e11d7380 80 isstandard = (currentvalue === "1") ? 0 : 1;
8e355853 81
ef4c23cc 82 var promises = ajax.call([{
8e355853 83 methodname: 'core_tag_update_tags',
e11d7380 84 args: { tags : [ { id : tagid , isstandard : isstandard } ] }
ef4c23cc
MG
85 }, {
86 methodname: 'core_tag_get_tags',
87 args: { tags : [ { id : tagid } ] }
88 }], true);
89
90 $.when.apply($, promises)
91 .done( function(updateresult, data) {
92 if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
e11d7380 93 templates.render('core_tag/tagisstandard', data.tags[0]).done(function(html) {
8e355853
MG
94 update_modified(target);
95 var parent = target.parent();
96 target.replaceWith(html);
e11d7380 97 parent.find('.tagisstandard').get(0).focus();
8e355853
MG
98 });
99 }
ef4c23cc 100 });
8e355853
MG
101 });
102
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;
110
ef4c23cc 111 var promises = ajax.call([{
8e355853 112 methodname: 'core_tag_update_tags',
ef4c23cc
MG
113 args: { tags : [ { id : tagid , flag : flag } ] }
114 }, {
115 methodname: 'core_tag_get_tags',
116 args: { tags : [ { id : tagid } ] }
117 }], true);
118
119 $.when.apply($, promises)
120 .done( function(updateresult, data) {
121 if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
8e355853
MG
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 }
ef4c23cc 137 });
8e355853
MG
138 });
139
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 });
156
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 });
131b78b9
MG
177 },
178
179 /**
180 * Initialises tag collection management page.
181 *
182 * @method initManageCollectionsPage
183 */
184 initManageCollectionsPage: function() {
185 $('body').on('updated', '[data-inplaceeditable]', function(e) {
352bfbf5
MG
186 var ajaxreturn = e.ajaxreturn,
187 oldvalue = e.oldvalue,
188 areaid, collid, isenabled;
131b78b9 189 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareaenable') {
352bfbf5 190 areaid = $(this).attr('data-itemid');
131b78b9 191 $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
352bfbf5 192 isenabled = ajaxreturn.value;
131b78b9
MG
193 if (isenabled === '1') {
194 $(this).closest('tr').removeClass('dimmed_text');
352bfbf5 195 collid = $(this).closest('tr').find('[data-itemtype="tagareacollection"]').attr("data-value");
131b78b9
MG
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') {
352bfbf5 202 areaid = $(this).attr('data-itemid');
131b78b9 203 $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
352bfbf5
MG
204 collid = $(this).attr('data-value');
205 isenabled = $(this).closest('tr').find('[data-itemtype="tagareaenable"]').attr("data-value");
131b78b9
MG
206 if (isenabled === "1") {
207 $(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]").removeClass('hidden');
208 }
209 }
352bfbf5
MG
210 });
211
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 );
256
257 });
258
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 });
8e355853
MG
274 }
275 };
276});