weekly release 4.0dev
[moodle.git] / mod / forum / amd / src / favourite_toggle.js
CommitLineData
99bda8a7
P
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 * Handle discussion subscription toggling on a discussion list in
18 * the forum view.
19 *
20 * @module mod_forum/favourite_toggle
99bda8a7
P
21 * @copyright 2019 Peter Dias <peter@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24define([
25 'jquery',
26 'core/templates',
27 'core/notification',
28 'mod_forum/repository',
29 'mod_forum/selectors',
ac915316 30 'core/str',
99bda8a7
P
31 ], function(
32 $,
33 Templates,
34 Notification,
35 Repository,
ac915316 36 Selectors,
49f5b16c 37 String
99bda8a7
P
38 ) {
39
40 /**
41 * Register event listeners for the subscription toggle.
42 *
43 * @param {object} root The discussion list root element
49f5b16c
RW
44 * @param {boolean} preventDefault Should the default action of the event be prevented
45 * @param {function} callback Success callback
99bda8a7 46 */
49f5b16c 47 var registerEventListeners = function(root, preventDefault, callback) {
99bda8a7
P
48 root.on('click', Selectors.favourite.toggle, function(e) {
49 var toggleElement = $(this);
50 var forumId = toggleElement.data('forumid');
51 var discussionId = toggleElement.data('discussionid');
52 var subscriptionState = toggleElement.data('targetstate');
53
8885cd57 54 Repository.setFavouriteDiscussionState(forumId, discussionId, subscriptionState)
99bda8a7 55 .then(function(context) {
49f5b16c 56 return callback(toggleElement, context);
99bda8a7 57 })
ac915316
P
58 .then(function() {
59 return String.get_string("favouriteupdated", "forum")
60 .done(function(s) {
61 return Notification.addNotification({
62 message: s,
63 type: "info"
64 });
65 });
66 })
99bda8a7
P
67 .catch(Notification.exception);
68
49f5b16c
RW
69 if (preventDefault) {
70 e.preventDefault();
71 }
959b996c 72 });
99bda8a7
P
73 };
74
75 return {
49f5b16c 76 init: registerEventListeners
99bda8a7
P
77 };
78});