1 |
efrain |
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 |
* Module for viewing a discussion in nested v2 view.
|
|
|
18 |
*
|
|
|
19 |
* @module mod_forum/discussion_nested_v2
|
|
|
20 |
* @copyright 2019 Ryan Wyllie <ryan@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
import $ from 'jquery';
|
|
|
24 |
import AutoRows from 'core/auto_rows';
|
|
|
25 |
import CustomEvents from 'core/custom_interaction_events';
|
|
|
26 |
import * as FormChangeChecker from 'core_form/changechecker';
|
|
|
27 |
import Notification from 'core/notification';
|
|
|
28 |
import Templates from 'core/templates';
|
|
|
29 |
import Discussion from 'mod_forum/discussion';
|
|
|
30 |
import InPageReply from 'mod_forum/inpage_reply';
|
|
|
31 |
import LockToggle from 'mod_forum/lock_toggle';
|
|
|
32 |
import FavouriteToggle from 'mod_forum/favourite_toggle';
|
|
|
33 |
import Pin from 'mod_forum/pin_toggle';
|
|
|
34 |
import Selectors from 'mod_forum/selectors';
|
|
|
35 |
import Subscribe from 'mod_forum/subscription_toggle';
|
|
|
36 |
|
|
|
37 |
const ANIMATION_DURATION = 150;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Get the closest post container element from the given element.
|
|
|
41 |
*
|
|
|
42 |
* @param {Object} element jQuery element to search from
|
|
|
43 |
* @return {Object} jQuery element
|
|
|
44 |
*/
|
|
|
45 |
const getPostContainer = (element) => {
|
|
|
46 |
return element.closest(Selectors.post.post);
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Get the closest post container element from the given element.
|
|
|
51 |
*
|
|
|
52 |
* @param {Object} element jQuery element to search from
|
|
|
53 |
* @param {Number} id Id of the post to find.
|
|
|
54 |
* @return {Object} jQuery element
|
|
|
55 |
*/
|
|
|
56 |
const getPostContainerById = (element, id) => {
|
|
|
57 |
return element.find(`${Selectors.post.post}[data-post-id=${id}]`);
|
|
|
58 |
};
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Get the parent post container elements from the given element.
|
|
|
62 |
*
|
|
|
63 |
* @param {Object} element jQuery element to search from
|
|
|
64 |
* @return {Object} jQuery element
|
|
|
65 |
*/
|
|
|
66 |
const getParentPostContainers = (element) => {
|
|
|
67 |
return element.parents(Selectors.post.post);
|
|
|
68 |
};
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Get the post content container element from the post container element.
|
|
|
72 |
*
|
|
|
73 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
74 |
* @return {Object} jQuery element
|
|
|
75 |
*/
|
|
|
76 |
const getPostContentContainer = (postContainer) => {
|
|
|
77 |
return postContainer.children().not(Selectors.post.repliesContainer).find(Selectors.post.forumCoreContent);
|
|
|
78 |
};
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Get the in page reply container element from the post container element.
|
|
|
82 |
*
|
|
|
83 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
84 |
* @return {Object} jQuery element
|
|
|
85 |
*/
|
|
|
86 |
const getInPageReplyContainer = (postContainer) => {
|
|
|
87 |
return postContainer.children().filter(Selectors.post.inpageReplyContainer);
|
|
|
88 |
};
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Get the in page reply form element from the post container element.
|
|
|
92 |
*
|
|
|
93 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
94 |
* @return {Object} jQuery element
|
|
|
95 |
*/
|
|
|
96 |
const getInPageReplyForm = (postContainer) => {
|
|
|
97 |
return getInPageReplyContainer(postContainer).find(Selectors.post.inpageReplyContent);
|
|
|
98 |
};
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Get the in page reply create (reply) button element from the post container element.
|
|
|
102 |
*
|
|
|
103 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
104 |
* @return {Object} jQuery element
|
|
|
105 |
*/
|
|
|
106 |
const getInPageReplyCreateButton = (postContainer) => {
|
|
|
107 |
return getPostContentContainer(postContainer).find(Selectors.post.inpageReplyCreateButton);
|
|
|
108 |
};
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Get the replies visibility toggle container (show/hide replies button container) element
|
|
|
112 |
* from the post container element.
|
|
|
113 |
*
|
|
|
114 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
115 |
* @return {Object} jQuery element
|
|
|
116 |
*/
|
|
|
117 |
const getRepliesVisibilityToggleContainer = (postContainer) => {
|
|
|
118 |
return postContainer.children(Selectors.post.repliesVisibilityToggleContainer);
|
|
|
119 |
};
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Get the replies container element from the post container element.
|
|
|
123 |
*
|
|
|
124 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
125 |
* @return {Object} jQuery element
|
|
|
126 |
*/
|
|
|
127 |
const getRepliesContainer = (postContainer) => {
|
|
|
128 |
return postContainer.children(Selectors.post.repliesContainer);
|
|
|
129 |
};
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Check if the post has any replies.
|
|
|
133 |
*
|
|
|
134 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
135 |
* @return {Bool}
|
|
|
136 |
*/
|
|
|
137 |
const hasReplies = (postContainer) => {
|
|
|
138 |
return getRepliesContainer(postContainer).children().length > 0;
|
|
|
139 |
};
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Get the show replies button element from the replies visibility toggle container element.
|
|
|
143 |
*
|
|
|
144 |
* @param {Object} replyVisibilityToggleContainer jQuery element for the toggle container
|
|
|
145 |
* @return {Object} jQuery element
|
|
|
146 |
*/
|
|
|
147 |
const getShowRepliesButton = (replyVisibilityToggleContainer) => {
|
|
|
148 |
return replyVisibilityToggleContainer.find(Selectors.post.showReplies);
|
|
|
149 |
};
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Get the hide replies button element from the replies visibility toggle container element.
|
|
|
153 |
*
|
|
|
154 |
* @param {Object} replyVisibilityToggleContainer jQuery element for the toggle container
|
|
|
155 |
* @return {Object} jQuery element
|
|
|
156 |
*/
|
|
|
157 |
const getHideRepliesButton = (replyVisibilityToggleContainer) => {
|
|
|
158 |
return replyVisibilityToggleContainer.find(Selectors.post.hideReplies);
|
|
|
159 |
};
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Check if the replies are visible.
|
|
|
163 |
*
|
|
|
164 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
165 |
* @return {Bool}
|
|
|
166 |
*/
|
|
|
167 |
const repliesVisible = (postContainer) => {
|
|
|
168 |
const repliesContainer = getRepliesContainer(postContainer);
|
|
|
169 |
return repliesContainer.is(':visible');
|
|
|
170 |
};
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Show the post replies.
|
|
|
174 |
*
|
|
|
175 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
176 |
* @param {Number|null} postIdToSee Id of the post to scroll into view (if any)
|
|
|
177 |
*/
|
|
|
178 |
const showReplies = (postContainer, postIdToSee = null) => {
|
|
|
179 |
const repliesContainer = getRepliesContainer(postContainer);
|
|
|
180 |
const replyVisibilityToggleContainer = getRepliesVisibilityToggleContainer(postContainer);
|
|
|
181 |
const showButton = getShowRepliesButton(replyVisibilityToggleContainer);
|
|
|
182 |
const hideButton = getHideRepliesButton(replyVisibilityToggleContainer);
|
|
|
183 |
|
|
|
184 |
showButton.addClass('hidden');
|
|
|
185 |
hideButton.removeClass('hidden');
|
|
|
186 |
|
|
|
187 |
repliesContainer.slideDown({
|
|
|
188 |
duration: ANIMATION_DURATION,
|
|
|
189 |
queue: false,
|
|
|
190 |
complete: () => {
|
|
|
191 |
if (postIdToSee) {
|
|
|
192 |
const postContainerToSee = getPostContainerById(repliesContainer, postIdToSee);
|
|
|
193 |
if (postContainerToSee.length) {
|
|
|
194 |
postContainerToSee[0].scrollIntoView();
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
}).css('display', 'none').fadeIn(ANIMATION_DURATION);
|
|
|
199 |
};
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Hide the post replies.
|
|
|
203 |
*
|
|
|
204 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
205 |
*/
|
|
|
206 |
const hideReplies = (postContainer) => {
|
|
|
207 |
const repliesContainer = getRepliesContainer(postContainer);
|
|
|
208 |
const replyVisibilityToggleContainer = getRepliesVisibilityToggleContainer(postContainer);
|
|
|
209 |
const showButton = getShowRepliesButton(replyVisibilityToggleContainer);
|
|
|
210 |
const hideButton = getHideRepliesButton(replyVisibilityToggleContainer);
|
|
|
211 |
|
|
|
212 |
showButton.removeClass('hidden');
|
|
|
213 |
hideButton.addClass('hidden');
|
|
|
214 |
|
|
|
215 |
repliesContainer.slideUp({
|
|
|
216 |
duration: ANIMATION_DURATION,
|
|
|
217 |
queue: false
|
|
|
218 |
}).fadeOut(ANIMATION_DURATION);
|
|
|
219 |
};
|
|
|
220 |
|
|
|
221 |
/** Variable to hold the showInPageReplyForm function after it's built. */
|
|
|
222 |
let showInPageReplyForm = null;
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Build the showInPageReplyForm function with the given additional template context.
|
|
|
226 |
*
|
|
|
227 |
* @param {Object} additionalTemplateContext Additional render context for the in page reply template.
|
|
|
228 |
* @return {Function}
|
|
|
229 |
*/
|
|
|
230 |
const buildShowInPageReplyFormFunction = (additionalTemplateContext) => {
|
|
|
231 |
/**
|
|
|
232 |
* Show the in page reply form in the given in page reply container. The form
|
|
|
233 |
* display will be animated.
|
|
|
234 |
*
|
|
|
235 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
236 |
*/
|
|
|
237 |
return async(postContainer) => {
|
|
|
238 |
|
|
|
239 |
const inPageReplyContainer = getInPageReplyContainer(postContainer);
|
|
|
240 |
const repliesVisibilityToggleContainer = getRepliesVisibilityToggleContainer(postContainer);
|
|
|
241 |
const inPageReplyCreateButton = getInPageReplyCreateButton(postContainer);
|
|
|
242 |
|
|
|
243 |
if (!hasInPageReplyForm(inPageReplyContainer)) {
|
|
|
244 |
try {
|
|
|
245 |
const html = await renderInPageReplyTemplate(additionalTemplateContext, inPageReplyCreateButton, postContainer);
|
|
|
246 |
Templates.appendNodeContents(inPageReplyContainer, html, '');
|
|
|
247 |
} catch (e) {
|
|
|
248 |
Notification.exception(e);
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
FormChangeChecker.watchForm(postContainer[0].querySelector('form'));
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
inPageReplyCreateButton.fadeOut(ANIMATION_DURATION, () => {
|
|
|
255 |
const inPageReplyForm = getInPageReplyForm(postContainer);
|
|
|
256 |
inPageReplyForm.slideDown({
|
|
|
257 |
duration: ANIMATION_DURATION,
|
|
|
258 |
queue: false,
|
|
|
259 |
complete: () => {
|
|
|
260 |
inPageReplyForm.find('textarea').focus();
|
|
|
261 |
}
|
|
|
262 |
}).css('display', 'none').fadeIn(ANIMATION_DURATION);
|
|
|
263 |
|
|
|
264 |
if (repliesVisibilityToggleContainer.length && hasReplies(postContainer)) {
|
|
|
265 |
repliesVisibilityToggleContainer.fadeIn(ANIMATION_DURATION);
|
|
|
266 |
hideReplies(postContainer);
|
|
|
267 |
}
|
|
|
268 |
});
|
|
|
269 |
};
|
|
|
270 |
};
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* Hide the in page reply form in the given in page reply container. The form
|
|
|
274 |
* display will be animated.
|
|
|
275 |
*
|
|
|
276 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
277 |
* @param {Number|null} postIdToSee Id of the post to scroll into view (if any)
|
|
|
278 |
*/
|
|
|
279 |
const hideInPageReplyForm = (postContainer, postIdToSee = null) => {
|
|
|
280 |
const inPageReplyForm = getInPageReplyForm(postContainer);
|
|
|
281 |
const inPageReplyCreateButton = getInPageReplyCreateButton(postContainer);
|
|
|
282 |
const repliesVisibilityToggleContainer = getRepliesVisibilityToggleContainer(postContainer);
|
|
|
283 |
|
|
|
284 |
if (repliesVisibilityToggleContainer.length && hasReplies(postContainer)) {
|
|
|
285 |
repliesVisibilityToggleContainer.fadeOut(ANIMATION_DURATION);
|
|
|
286 |
if (!repliesVisible(postContainer)) {
|
|
|
287 |
showReplies(postContainer, postIdToSee);
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
inPageReplyForm.slideUp({
|
|
|
292 |
duration: ANIMATION_DURATION,
|
|
|
293 |
queue: false,
|
|
|
294 |
complete: () => {
|
|
|
295 |
inPageReplyCreateButton.fadeIn(ANIMATION_DURATION);
|
|
|
296 |
}
|
|
|
297 |
}).fadeOut(200);
|
|
|
298 |
};
|
|
|
299 |
|
|
|
300 |
/**
|
|
|
301 |
* Check if the in page reply container contains the in page reply form.
|
|
|
302 |
*
|
|
|
303 |
* @param {Object} inPageReplyContainer jQuery element for the in page reply container
|
|
|
304 |
* @return {Bool}
|
|
|
305 |
*/
|
|
|
306 |
const hasInPageReplyForm = (inPageReplyContainer) => {
|
|
|
307 |
return inPageReplyContainer.find(Selectors.post.inpageReplyContent).length > 0;
|
|
|
308 |
};
|
|
|
309 |
|
|
|
310 |
/**
|
|
|
311 |
* Render the template to generate the in page reply form HTML.
|
|
|
312 |
*
|
|
|
313 |
* @param {Object} additionalTemplateContext Additional render context for the in page reply template
|
|
|
314 |
* @param {Object} button jQuery element for the reply button that was clicked
|
|
|
315 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
316 |
* @return {Object} jQuery promise
|
|
|
317 |
*/
|
|
|
318 |
const renderInPageReplyTemplate = (additionalTemplateContext, button, postContainer) => {
|
|
|
319 |
const postContentContainer = getPostContentContainer(postContainer);
|
|
|
320 |
const currentSubject = postContentContainer.find(Selectors.post.forumSubject).text();
|
|
|
321 |
const currentAuthorName = postContentContainer.find(Selectors.post.authorName).text();
|
|
|
322 |
const context = {
|
|
|
323 |
postid: postContainer.data('post-id'),
|
|
|
324 |
"reply_url": button.attr('data-href'),
|
|
|
325 |
sesskey: M.cfg.sesskey,
|
|
|
326 |
parentsubject: currentSubject,
|
|
|
327 |
parentauthorname: currentAuthorName,
|
|
|
328 |
canreplyprivately: button.data('can-reply-privately'),
|
|
|
329 |
postformat: InPageReply.CONTENT_FORMATS.MOODLE,
|
|
|
330 |
...additionalTemplateContext
|
|
|
331 |
};
|
|
|
332 |
|
|
|
333 |
return Templates.render('mod_forum/inpage_reply_v2', context);
|
|
|
334 |
};
|
|
|
335 |
|
|
|
336 |
/**
|
|
|
337 |
* Increment the total reply count in the show/hide replies buttons for the post.
|
|
|
338 |
*
|
|
|
339 |
* @param {Object} postContainer jQuery element for the post container
|
|
|
340 |
*/
|
|
|
341 |
const incrementTotalReplyCount = (postContainer) => {
|
|
|
342 |
getRepliesVisibilityToggleContainer(postContainer).find(Selectors.post.replyCount).each((index, element) => {
|
|
|
343 |
const currentCount = parseInt(element.innerText, 10);
|
|
|
344 |
element.innerText = currentCount + 1;
|
|
|
345 |
});
|
|
|
346 |
};
|
|
|
347 |
|
|
|
348 |
/**
|
|
|
349 |
* Create all of the event listeners for the discussion.
|
|
|
350 |
*
|
|
|
351 |
* @param {Object} root jQuery element for the discussion container
|
|
|
352 |
*/
|
|
|
353 |
const registerEventListeners = (root) => {
|
|
|
354 |
CustomEvents.define(root, [CustomEvents.events.activate]);
|
|
|
355 |
// Auto expanding text area for in page reply.
|
|
|
356 |
AutoRows.init(root);
|
|
|
357 |
|
|
|
358 |
// Reply button is clicked.
|
|
|
359 |
root.on(CustomEvents.events.activate, Selectors.post.inpageReplyCreateButton, (e, data) => {
|
|
|
360 |
data.originalEvent.preventDefault();
|
|
|
361 |
const postContainer = getPostContainer($(e.currentTarget));
|
|
|
362 |
showInPageReplyForm(postContainer);
|
|
|
363 |
});
|
|
|
364 |
|
|
|
365 |
// Cancel in page reply button.
|
|
|
366 |
root.on(CustomEvents.events.activate, Selectors.post.inpageReplyCancelButton, (e, data) => {
|
|
|
367 |
data.originalEvent.preventDefault();
|
|
|
368 |
const postContainer = getPostContainer($(e.currentTarget));
|
|
|
369 |
hideInPageReplyForm(postContainer);
|
|
|
370 |
});
|
|
|
371 |
|
|
|
372 |
// Show replies button clicked.
|
|
|
373 |
root.on(CustomEvents.events.activate, Selectors.post.showReplies, (e, data) => {
|
|
|
374 |
data.originalEvent.preventDefault();
|
|
|
375 |
const postContainer = getPostContainer($(e.target));
|
|
|
376 |
showReplies(postContainer);
|
|
|
377 |
});
|
|
|
378 |
|
|
|
379 |
// Hide replies button clicked.
|
|
|
380 |
root.on(CustomEvents.events.activate, Selectors.post.hideReplies, (e, data) => {
|
|
|
381 |
data.originalEvent.preventDefault();
|
|
|
382 |
const postContainer = getPostContainer($(e.target));
|
|
|
383 |
hideReplies(postContainer);
|
|
|
384 |
});
|
|
|
385 |
|
|
|
386 |
// Post created with in page reply.
|
|
|
387 |
root.on(InPageReply.EVENTS.POST_CREATED, Selectors.post.inpageSubmitBtn, (e, newPostId) => {
|
|
|
388 |
const currentTarget = $(e.currentTarget);
|
|
|
389 |
const postContainer = getPostContainer(currentTarget);
|
|
|
390 |
const postContainers = getParentPostContainers(currentTarget);
|
|
|
391 |
hideInPageReplyForm(postContainer, newPostId);
|
|
|
392 |
|
|
|
393 |
postContainers.each((index, container) => {
|
|
|
394 |
incrementTotalReplyCount($(container));
|
|
|
395 |
});
|
|
|
396 |
});
|
|
|
397 |
};
|
|
|
398 |
|
|
|
399 |
/**
|
|
|
400 |
* Initialise the javascript for the discussion in nested v2 display mode.
|
|
|
401 |
*
|
|
|
402 |
* @param {Object} root jQuery element for the discussion container
|
|
|
403 |
* @param {Object} context Additional render context for the in page reply template
|
|
|
404 |
*/
|
|
|
405 |
export const init = (root, context) => {
|
|
|
406 |
// Build the showInPageReplyForm function with the additional render context.
|
|
|
407 |
showInPageReplyForm = buildShowInPageReplyFormFunction(context);
|
|
|
408 |
// Add discussion event listeners.
|
|
|
409 |
registerEventListeners(root);
|
|
|
410 |
// Initialise default discussion javascript (keyboard nav etc).
|
|
|
411 |
Discussion.init(root);
|
|
|
412 |
// Add in page reply javascript.
|
|
|
413 |
InPageReply.init(root);
|
|
|
414 |
|
|
|
415 |
// Initialise the settings menu javascript.
|
|
|
416 |
const discussionToolsContainer = root.find(Selectors.discussion.tools);
|
|
|
417 |
LockToggle.init(discussionToolsContainer, false);
|
|
|
418 |
FavouriteToggle.init(discussionToolsContainer, false, (toggleElement, response) => {
|
|
|
419 |
const newTargetState = response.userstate.favourited ? 0 : 1;
|
|
|
420 |
return toggleElement.data('targetstate', newTargetState);
|
|
|
421 |
});
|
|
|
422 |
Pin.init(discussionToolsContainer, false, (toggleElement, response) => {
|
|
|
423 |
const newTargetState = response.pinned ? 0 : 1;
|
|
|
424 |
return toggleElement.data('targetstate', newTargetState);
|
|
|
425 |
});
|
|
|
426 |
Subscribe.init(discussionToolsContainer, false, (toggleElement, response) => {
|
|
|
427 |
const newTargetState = response.userstate.subscribed ? 0 : 1;
|
|
|
428 |
toggleElement.data('targetstate', newTargetState);
|
|
|
429 |
});
|
|
|
430 |
};
|