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