MDL-51579 course: Bump version to update mobile service
[moodle.git] / lib / amd / src / tag.js
blob0edf6ef2aa4680a6f7d94f4f9ff55ce6c2f7d01a
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.
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/config'],
26         function($, ajax, templates, notification, str, cfg) {
27     return /** @alias module:core/tag */ {
29         /**
30          * Initialises handlers for AJAX methods.
31          *
32          * @method init
33          */
34         init_manage_page: function() {
36             var update_modified = function(el) {
37                 var row = el.closest('tr').get(0);
38                 if (row) {
39                     var td = $(row).find('td.col-timemodified').get(0);
40                     str.get_string('now').done(function(s) {
41                         $(td).html(s);
42                     });
43                 }
44             };
46             // Click handler for changing tag type.
47             $('.tag-management-table').delegate('.tagtype', 'click', function(e) {
48                 e.preventDefault();
49                 var target = $( this ),
50                     tagid = target.attr('data-id'),
51                     currentvalue = target.attr('data-value'),
52                     official = (currentvalue === "1") ? 0 : 1;
54                 var promises = ajax.call([{
55                     methodname: 'core_tag_update_tags',
56                     args: { tags : [ { id : tagid , official : official } ] }
57                 }, {
58                     methodname: 'core_tag_get_tags',
59                     args: { tags : [ { id : tagid } ] }
60                 }], true);
62                 $.when.apply($, promises)
63                     .done( function(updateresult, data) {
64                         if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
65                             templates.render('core_tag/tagtype', data.tags[0]).done(function(html) {
66                                 update_modified(target);
67                                 var parent = target.parent();
68                                 target.replaceWith(html);
69                                 parent.find('.tagtype').get(0).focus();
70                             });
71                         }
72                     });
73             });
75             // Click handler for flagging/resetting tag flag.
76             $('.tag-management-table').delegate('.tagflag', 'click', function(e) {
77                 e.preventDefault();
78                 var target = $( this ),
79                     tagid = target.attr('data-id'),
80                     currentvalue = target.attr('data-value'),
81                     flag = (currentvalue === "0") ? 1 : 0;
83                 var promises = ajax.call([{
84                     methodname: 'core_tag_update_tags',
85                     args: { tags : [ { id : tagid , flag : flag } ] }
86                 }, {
87                     methodname: 'core_tag_get_tags',
88                     args: { tags : [ { id : tagid } ] }
89                 }], true);
91                 $.when.apply($, promises)
92                     .done( function(updateresult, data) {
93                         if (updateresult.warnings[0] === undefined && data.tags[0] !== undefined) {
94                             var row = target.closest('tr').get(0);
95                             if (row) {
96                                 if (data.tags[0].flag) {
97                                     $(row).addClass('flagged-tag');
98                                 } else {
99                                     $(row).removeClass('flagged-tag');
100                                 }
101                             }
102                             templates.render('core_tag/tagflag', data.tags[0]).done(function(html) {
103                                 update_modified(target);
104                                 var parent = target.parent();
105                                 target.replaceWith(html);
106                                 parent.find('.tagflag').get(0).focus();
107                             });
108                         }
109                     });
110             });
112             // Confirmation for single tag delete link.
113             $('.tag-management-table').delegate('a.tagdelete', 'click', function(e) {
114                 e.preventDefault();
115                 var href = $(this).attr('href');
116                 str.get_strings([
117                         {key : 'delete'},
118                         {key : 'confirmdeletetag', component : 'tag'},
119                         {key : 'yes'},
120                         {key : 'no'},
121                     ]).done(function(s) {
122                         notification.confirm(s[0], s[1], s[2], s[3], function() {
123                             window.location.href = href;
124                         });
125                     }
126                 );
127             });
129             // Confirmation for bulk tag delete button.
130             $("#tag-management-delete").click(function(e){
131                 var form = $(this).closest('form').get(0),
132                     cnt = $(form).find("input[type=checkbox]:checked").length;
133                 if (!cnt) {
134                     return false;
135                 }
136                 e.preventDefault();
137                 str.get_strings([
138                         {key : 'delete'},
139                         {key : 'confirmdeletetags', component : 'tag'},
140                         {key : 'yes'},
141                         {key : 'no'},
142                     ]).done(function(s) {
143                         notification.confirm(s[0], s[1], s[2], s[3], function() {
144                             form.submit();
145                         });
146                     }
147                 );
148             });
150             // Edit tag name.
151             $('.tag-management-table').delegate('.tagnameedit', 'click keypress', function(e) {
152                 if (e.type === 'keypress' && e.keyCode !== 13) {
153                     return;
154                 }
155                 e.stopImmediatePropagation();
156                 e.preventDefault();
157                 var target = $(this),
158                     tdelement = $( target.closest('td').get(0) ),
159                     inputelement = $( tdelement.find('input').get(0) ),
160                     tagid = target.attr('data-id');
162                 var change_name = function(tagid, newname) {
163                     var promises = ajax.call([{
164                         methodname: 'core_tag_update_tags',
165                         args: { tags : [ { id : tagid , rawname : newname } ] }
166                     }, {
167                         methodname: 'core_tag_get_tags',
168                         args: { tags : [ { id : tagid } ] }
169                     }], true);
171                     $.when.apply($, promises)
172                         .done( function(updateresult, data) {
173                             if (updateresult.warnings[0] !== undefined) {
174                                 str.get_string('error').done(function(s) {
175                                     notification.alert(s, updateresult.warnings[0].message);
176                                 });
177                             } else if (data.tags[0] !== undefined) {
178                                 templates.render('core_tag/tagname', data.tags[0]).done(function(html) {
179                                     update_modified(tdelement);
180                                     tdelement.html(html);
181                                     $(tdelement.find('.tagnameedit').get(0)).focus();
182                                 });
183                             }
184                         });
185                 };
187                 var turn_editing_off = function() {
188                     $('.tag-management-table td.tageditingon').each(function() {
189                         var td = $( this ),
190                             input = $( td.find('input').get(0) );
191                         input.off();
192                         td.removeClass('tageditingon');
193                         // Reset input value to the one that was there before editing.
194                         input.val(td.attr('data-value'));
195                     });
196                 };
198                 // Turn editing on for the current element and register handler for Enter/Esc keys.
199                 turn_editing_off();
200                 tdelement.addClass('tageditingon');
201                 tdelement.attr('data-value', inputelement.val());
202                 inputelement.select();
203                 inputelement.on('keypress focusout', function(e) {
204                     if (cfg.behatsiterunning && e.type === 'focusout') {
205                         // Behat triggers focusout too often.
206                         return;
207                     }
208                     if (e.type === 'keypress' && e.keyCode === 13) {
209                         change_name(tagid, inputelement.val());
210                         turn_editing_off();
211                     }
212                     if ((e.type === 'keypress' && e.keyCode === 27) || e.type === 'focusout') {
213                         turn_editing_off();
214                     }
215                 });
216             });
217         }
218     };