Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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