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 *
c026a28d
MG
32 * @method init_tagindex_page
33 */
34 init_tagindex_page: 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;},{});
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 *
60 * @method init_manage_page
8e355853
MG
61 */
62 init_manage_page: function() {
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 });
8e355853
MG
177 }
178 };
179});