weekly release 4.0dev
[moodle.git] / mod / forum / amd / src / discussion_list.js
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/>.
16 /**
17  * Module for the list of discussions on when viewing a forum.
18  *
19  * @module     mod_forum/discussion_list
20  * @package    mod_forum
21  * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 define([
25     'jquery',
26     'core/templates',
27     'core/str',
28     'core/notification',
29     'mod_forum/subscription_toggle',
30     'mod_forum/selectors',
31     'mod_forum/repository',
32     'core/pubsub',
33     'mod_forum/forum_events',
34 ], function(
35     $,
36     Templates,
37     Str,
38     Notification,
39     SubscriptionToggle,
40     Selectors,
41     Repository,
42     PubSub,
43     ForumEvents
44 ) {
45     var registerEventListeners = function(root) {
46         PubSub.subscribe(ForumEvents.SUBSCRIPTION_TOGGLED, function(data) {
47             var discussionId = data.discussionId;
48             var subscribed = data.subscriptionState;
49             var discussionListItem = root.find(Selectors.discussion.item + '[data-discussionid= ' + discussionId + ']');
50             var subscribedLabel = discussionListItem.find(Selectors.discussion.subscribedLabel);
51             if (subscribed) {
52                 discussionListItem.addClass('subscribed');
53                 subscribedLabel.removeAttr('hidden');
54             } else {
55                 discussionListItem.removeClass('subscribed');
56                 subscribedLabel.attr('hidden', true);
57             }
58         });
60         root.on('click', Selectors.favourite.toggle, function() {
61             var toggleElement = $(this);
62             var forumId = toggleElement.data('forumid');
63             var discussionId = toggleElement.data('discussionid');
64             var subscriptionState = toggleElement.data('targetstate');
65             Repository.setFavouriteDiscussionState(forumId, discussionId, subscriptionState)
66                 .then(function() {
67                     return location.reload();
68                 })
69                 .catch(Notification.exception);
70         });
72         root.on('click', Selectors.pin.toggle, function(e) {
73             e.preventDefault();
74             var toggleElement = $(this);
75             var forumId = toggleElement.data('forumid');
76             var discussionId = toggleElement.data('discussionid');
77             var state = toggleElement.data('targetstate');
78             Repository.setPinDiscussionState(forumId, discussionId, state)
79                 .then(function() {
80                     return location.reload();
81                 })
82                 .catch(Notification.exception);
83         });
85         root.on('click', Selectors.lock.toggle, function(e) {
86             var toggleElement = $(this);
87             var forumId = toggleElement.data('forumid');
88             var discussionId = toggleElement.data('discussionid');
89             var state = toggleElement.data('state');
91             Repository.setDiscussionLockState(forumId, discussionId, state)
92                 .then(function(context) {
93                     var icon = toggleElement.parents(Selectors.summary.actions).find(Selectors.lock.icon);
94                     var lockedLabel = toggleElement.parents(Selectors.discussion.item).find(Selectors.discussion.lockedLabel);
95                     if (context.locked) {
96                         icon.removeClass('hidden');
97                         lockedLabel.removeAttr('hidden');
98                     } else {
99                         icon.addClass('hidden');
100                         lockedLabel.attr('hidden', true);
101                     }
102                     return context;
103                 })
104                 .then(function(context) {
105                     context.forumid = forumId;
106                     return Templates.render('mod_forum/discussion_lock_toggle', context);
107                 })
108                 .then(function(html, js) {
109                     return Templates.replaceNode(toggleElement, html, js);
110                 })
111                 .then(function() {
112                     return Str.get_string('lockupdated', 'forum')
113                         .done(function(s) {
114                             return Notification.addNotification({
115                                 message: s,
116                                 type: "info"
117                             });
118                         });
119                 })
120                 .catch(Notification.exception);
122             e.preventDefault();
123         });
124     };
126     return {
127         init: function(root) {
128             SubscriptionToggle.init(root, false, function(toggleElement, context) {
129                 var toggleId = toggleElement.attr('id');
130                 var newTargetState = context.userstate.subscribed ? 0 : 1;
131                 toggleElement.data('targetstate', newTargetState);
133                 var stringKey = context.userstate.subscribed ? 'unsubscribediscussion' : 'subscribediscussion';
134                 return Str.get_string(stringKey, 'mod_forum')
135                     .then(function(string) {
136                         toggleElement.closest('td').find('label[for="' + toggleId + '"]').find('span').text(string);
137                         return string;
138                     });
139             });
140             registerEventListeners(root);
141         }
142     };
143 });