weekly release 4.0dev
[moodle.git] / mod / forum / amd / src / pin_toggle.js
CommitLineData
34e2f9c0 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 * This module is the highest level module for the calendar. It is
18 * responsible for initialising all of the components required for
19 * the calendar to run. It also coordinates the interaction between
20 * components by listening for and responding to different events
21 * triggered within the calendar UI.
22 *
23 * @module mod_forum/pin_toggle
34e2f9c0 24 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27define([
28 'jquery',
29 'core/ajax',
30 'core/str',
31 'core/templates',
32 'core/notification',
33 'mod_forum/repository',
34 'mod_forum/selectors',
ac915316 35 'core/str',
34e2f9c0 36], function(
37 $,
38 Ajax,
39 Str,
40 Templates,
41 Notification,
42 Repository,
ac915316 43 Selectors,
49f5b16c 44 String
34e2f9c0 45) {
46
47 /**
48 * Registery event listeners for the pin toggle.
49 *
50 * @param {object} root The calendar root element
49f5b16c
RW
51 * @param {boolean} preventDefault Should the default action of the event be prevented
52 * @param {function} callback Success callback
34e2f9c0 53 */
49f5b16c 54 var registerEventListeners = function(root, preventDefault, callback) {
34e2f9c0 55 root.on('click', Selectors.pin.toggle, function(e) {
56 var toggleElement = $(this);
57 var forumid = toggleElement.data('forumid');
58 var discussionid = toggleElement.data('discussionid');
59 var pinstate = toggleElement.data('targetstate');
8885cd57 60 Repository.setPinDiscussionState(forumid, discussionid, pinstate)
34e2f9c0 61 .then(function(context) {
49f5b16c 62 return callback(toggleElement, context);
34e2f9c0 63 })
ac915316
P
64 .then(function() {
65 return String.get_string("pinupdated", "forum")
66 .done(function(s) {
67 return Notification.addNotification({
68 message: s,
69 type: "info"
70 });
71 });
72 })
34e2f9c0 73 .fail(Notification.exception);
74
49f5b16c
RW
75 if (preventDefault) {
76 e.preventDefault();
77 }
959b996c 78 });
34e2f9c0 79 };
80
81 return {
49f5b16c 82 init: registerEventListeners
34e2f9c0 83 };
84});