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