weekly release 4.0dev
[moodle.git] / mod / forum / amd / src / posts_list.js
CommitLineData
9b4f09ba
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 * 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/posts_list
24 * @package mod_forum
25 * @copyright 2019 Peter Dias
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28define([
29 'jquery',
30 'core/templates',
31 'core/notification',
454d76f6 32 'core/pending',
9b4f09ba
P
33 'mod_forum/selectors',
34 'mod_forum/inpage_reply',
35 ], function(
36 $,
37 Templates,
38 Notification,
454d76f6 39 Pending,
9b4f09ba
P
40 Selectors,
41 InPageReply
42 ) {
43
44 var registerEventListeners = function(root) {
45 root.on('click', Selectors.post.inpageReplyLink, function(e) {
46 e.preventDefault();
e6104044
MG
47 // After adding a reply a url hash is being generated that scrolls (points) to the newly added reply.
48 // The hash being present causes this scrolling behavior to the particular reply to persists even when
49 // another, non-related in-page replay link is being clicked which ultimately causes a bad user experience.
50 // A particular solution for this problem would be changing the browser's history state when a url hash is
51 // present.
52 if (window.location.hash) {
53 // Remove the fragment identifier from the url.
54 var url = window.location.href.split('#')[0];
55 history.pushState({}, document.title, url);
56 }
454d76f6 57 var pending = new Pending('inpage-reply');
9b4f09ba 58 var currentTarget = $(e.currentTarget).parents(Selectors.post.forumCoreContent);
a0c9b6af 59 var currentSubject = currentTarget.find(Selectors.post.forumSubject);
9b4f09ba
P
60 var currentRoot = $(e.currentTarget).parents(Selectors.post.forumContent);
61 var context = {
62 postid: $(currentRoot).data('post-id'),
a0c9b6af
P
63 "reply_url": $(e.currentTarget).attr('href'),
64 sesskey: M.cfg.sesskey,
9ba09a12 65 parentsubject: currentSubject.data('replySubject'),
f6548dfb
SA
66 canreplyprivately: $(e.currentTarget).data('can-reply-privately'),
67 postformat: InPageReply.CONTENT_FORMATS.MOODLE
9b4f09ba
P
68 };
69
70 if (!currentRoot.find(Selectors.post.inpageReplyContent).length) {
71 Templates.render('mod_forum/inpage_reply', context)
72 .then(function(html, js) {
73 return Templates.appendNodeContents(currentTarget, html, js);
74 })
75 .then(function() {
454d76f6
RW
76 return currentRoot.find(Selectors.post.inpageReplyContent)
77 .slideToggle(300, pending.resolve).find('textarea').focus();
9b4f09ba
P
78 })
79 .fail(Notification.exception);
80 } else {
81 var form = currentRoot.find(Selectors.post.inpageReplyContent);
454d76f6 82 form.slideToggle(300, pending.resolve);
9b4f09ba
P
83 if (form.is(':visible')) {
84 form.find('textarea').focus();
85 }
86 }
87 });
88 };
89
90 return {
91 init: function(root) {
92 registerEventListeners(root);
93 InPageReply.init(root);
94 }
95 };
96});