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