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