on-demand release 3.7dev+
[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();
45 var currentTarget = $(e.currentTarget).parents(Selectors.post.forumCoreContent);
a0c9b6af 46 var currentSubject = currentTarget.find(Selectors.post.forumSubject);
9b4f09ba
P
47 var currentRoot = $(e.currentTarget).parents(Selectors.post.forumContent);
48 var context = {
49 postid: $(currentRoot).data('post-id'),
a0c9b6af
P
50 "reply_url": $(e.currentTarget).attr('href'),
51 sesskey: M.cfg.sesskey,
52 parentsubject: currentSubject.html()
9b4f09ba
P
53 };
54
55 if (!currentRoot.find(Selectors.post.inpageReplyContent).length) {
56 Templates.render('mod_forum/inpage_reply', context)
57 .then(function(html, js) {
58 return Templates.appendNodeContents(currentTarget, html, js);
59 })
60 .then(function() {
a0c9b6af 61 return currentRoot.find(Selectors.post.inpageReplyContent).toggle().find('textarea').focus();
9b4f09ba
P
62 })
63 .fail(Notification.exception);
64 } else {
65 var form = currentRoot.find(Selectors.post.inpageReplyContent);
66 form.toggle();
67 if (form.is(':visible')) {
68 form.find('textarea').focus();
69 }
70 }
71 });
72 };
73
74 return {
75 init: function(root) {
76 registerEventListeners(root);
77 InPageReply.init(root);
78 }
79 };
80});