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