weekly release 3.1dev
[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 }
103 e.preventDefault();
104 str.get_strings([
105 {key : 'delete'},
106 {key : 'confirmdeletetags', component : 'tag'},
107 {key : 'yes'},
108 {key : 'no'},
109 ]).done(function(s) {
110 notification.confirm(s[0], s[1], s[2], s[3], function() {
111 form.submit();
112 });
113 }
114 );
115 });
131b78b9
MG
116 },
117
118 /**
119 * Initialises tag collection management page.
120 *
121 * @method initManageCollectionsPage
122 */
123 initManageCollectionsPage: function() {
124 $('body').on('updated', '[data-inplaceeditable]', function(e) {
352bfbf5 125 var ajaxreturn = e.ajaxreturn,
352bfbf5 126 areaid, collid, isenabled;
131b78b9 127 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareaenable') {
352bfbf5 128 areaid = $(this).attr('data-itemid');
131b78b9 129 $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
352bfbf5 130 isenabled = ajaxreturn.value;
131b78b9
MG
131 if (isenabled === '1') {
132 $(this).closest('tr').removeClass('dimmed_text');
352bfbf5 133 collid = $(this).closest('tr').find('[data-itemtype="tagareacollection"]').attr("data-value");
21d9596c
MG
134 $(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]")
135 .removeClass('hidden');
131b78b9
MG
136 } else {
137 $(this).closest('tr').addClass('dimmed_text');
138 }
139 }
140 if (ajaxreturn.component === 'core_tag' && ajaxreturn.itemtype === 'tagareacollection') {
352bfbf5 141 areaid = $(this).attr('data-itemid');
131b78b9 142 $(".tag-collections-table ul[data-collectionid] li[data-areaid="+areaid+"]").addClass('hidden');
352bfbf5
MG
143 collid = $(this).attr('data-value');
144 isenabled = $(this).closest('tr').find('[data-itemtype="tagareaenable"]').attr("data-value");
131b78b9 145 if (isenabled === "1") {
21d9596c
MG
146 $(".tag-collections-table ul[data-collectionid="+collid+"] li[data-areaid="+areaid+"]")
147 .removeClass('hidden');
131b78b9
MG
148 }
149 }
352bfbf5
MG
150 });
151
152 $('body').on('click', '.addtagcoll > a', function(e) {
153 e.preventDefault();
154 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
155 str.get_strings([
156 {key : 'addtagcoll', component : 'tag'},
157 {key : 'name'},
158 {key : 'searchable', component : 'tag'},
159 {key : 'create'},
160 {key : 'cancel'},
161 ]).done(function(s) {
162 var el = $('<div><form id="addtagcoll_form" class="form-inline">'+
163 '<p><label for="addtagcoll_name"></label>: ' +
cbecc3c3
MG
164 '<input id="addtagcoll_name" type="text"/></p>' +
165 '<p><label for="addtagcoll_searchable"></label>: ' +
352bfbf5
MG
166 '<input id="addtagcoll_searchable" type="checkbox" value="1" checked/></p>' +
167 '<p class="mdl-align"><input type="submit" id="addtagcoll_submit"/>'+
168 '<input type="button" id="addtagcoll_cancel"/></p>' +
169 '</form></div>');
170 el.find('label[for="addtagcoll_name"]').html(s[1]);
171 el.find('label[for="addtagcoll_searchable"]').html(s[2]);
172 el.find('#addtagcoll_submit').attr('value', s[3]);
173 el.find('#addtagcoll_cancel').attr('value', s[4]);
174 var panel = new M.core.dialogue ({
175 draggable: true,
176 modal: true,
177 closeButton: true,
178 headerContent: s[0],
179 bodyContent: el.html()
180 });
181 panel.show();
182 $('#addtagcoll_form #addtagcoll_name').focus();
183 $('#addtagcoll_form #addtagcoll_cancel').on('click', function() {
184 panel.destroy();
185 });
186 $('#addtagcoll_form').on('submit', function() {
187 var name = $('#addtagcoll_form #addtagcoll_name').val();
188 var searchable = $('#addtagcoll_form #addtagcoll_searchable').prop('checked') ? 1 : 0;
189 if (String(name).length > 0) {
190 window.location.href = href + "&name=" + encodeURIComponent(name) + "&searchable=" + searchable;
191 }
192 return false;
193 });
194 }
195 );
196
197 });
198
199 $('body').on('click', '.tag-collections-table .action_delete', function(e) {
200 e.preventDefault();
201 var href = $(this).attr('data-url') + '&sesskey=' + M.cfg.sesskey;
202 str.get_strings([
203 {key : 'delete'},
204 {key : 'suredeletecoll', component : 'tag', param : $(this).attr('data-collname') },
205 {key : 'yes'},
206 {key : 'no'},
207 ]).done(function(s) {
208 notification.confirm(s[0], s[1], s[2], s[3], function() {
209 window.location.href = href;
210 });
211 }
212 );
213 });
8e355853
MG
214 }
215 };
216});