blob: d113ee9d2312cdef9124c14e1f05213f3a9b081d [file] [log] [blame]
j1c53f822020-06-07 15:32:06 -07001<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <title>JSDoc: Source: pages-discussions.js</title>
6
7 <script src="scripts/prettify/prettify.js"> </script>
8 <script src="scripts/prettify/lang-css.js"> </script>
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13 <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14</head>
15
16<body>
17
18<div id="main">
19
20 <h1 class="page-title">Source: pages-discussions.js</h1>
21
22
23
24
25
26
27 <section>
28 <article>
29 <pre class="prettyprint source linenums"><code>/**
30 * @file DTPS pages &amp; discussion functions
31 * @author jottocraft
32 *
33 * @copyright Copyright (c) 2018-2020 jottocraft. All rights reserved.
34 * @license GPL-2.0-only
35 */
36
37
38/**
39 * Renders the discussions list for a class
40 *
41 * @param {string} courseID The course ID to render discussion threads for
j35a697e2020-07-24 20:28:00 -070042 * @param {string} [defaultThread] The thread to load by default
j1c53f822020-06-07 15:32:06 -070043 */
j35a697e2020-07-24 20:28:00 -070044dtps.loadThreadsList = function (courseID, defaultThread) {
j1c53f822020-06-07 15:32:06 -070045 //Get class index and set as selected class
46 var classNum = dtps.classes.map(course => course.id).indexOf(courseID);
47 dtps.selectedClass = classNum;
48
49 //Set discussions as the selected content
50 dtps.selectedContent = "discuss";
51 $("#dtpsTabBar .btn").removeClass("active");
52 $("#dtpsTabBar .btn.discuss").addClass("active");
53
54 if (classNum == -1) {
55 //Class does not exist
56 dtps.error("The selected class doesn't exist", "classNum check failed @ dtps.loadThreadsList");
57 }
58
59 //Load class color and things
60 dtps.presentClass(classNum);
61 jQuery("body").removeClass("collapsedSidebar");
62
63 if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "discuss")) {
64 //Render sidebar
65 jQuery(".sidebar").html(/*html*/`
66 &lt;div class="bigLogo" style="text-align: center; margin: 10px 0 20px; height: 38px;">
67 &lt;i style="font-size: 28px; margin-right: 7px; vertical-align: middle;" class="material-icons">forum&lt;/i>
68 &lt;h4 style="color: var(--text); display: inline-block; font-size: 28px; vertical-align: middle; margin: 0px;">Discussions&lt;/h4>
69 &lt;/div>
70
71 &lt;div class="items">
72 &lt;div class="spinner">&lt;/div>
73 &lt;/div>
74 `);
75
76 //Delete existing class content HTML
77 jQuery(".classContent").html("");
78 }
79
80 //Fetch discussion thread data
81 dtpsLMS.fetchDiscussionThreads(dtps.classes[classNum].id).then(function (threadData) {
82 //Store fetched data to the selected class
83 dtps.classes[classNum].discussions = threadData;
84
85 if (dtps.classes[classNum].discussions.length == 0) {
86 //No discussion topics in this class
87 jQuery(".sidebar .items").html(/*html*/`
88 &lt;div onclick="fluid.screen('stream', '${dtps.classes[classNum].id}');" class="class item main back">
89 &lt;span class="label">Classes&lt;/span>
90 &lt;div class="icon">
91 &lt;i class="material-icons">keyboard_arrow_left&lt;/i>
92 &lt;/div>
93 &lt;/div>
94
95 &lt;p style="text-align: center; font-weight: bold; margin-top: 60px;">No discussions found&lt;/p>
96 &lt;p style="text-align: center; font-size: 14px;">This class doesn't have any discussions&lt;/p>
97 `);
98 } else {
99 //Loop over discusson threads array to create discussion thread HTML for the sidebar
100 var discussionHTML = dtps.classes[classNum].discussions.map(discussionThread => {
101 return /*html*/`
102 &lt;div data-threadID="${discussionThread.id}" class="item">
103 &lt;span class="label">${discussionThread.title}&lt;/span>
104 &lt;div class="icon">
105 &lt;i style="font-family: 'Material Icons Extended';" class="material-icons">${discussionThread.locked ? "lock_outline" : "chat_bubble_outline"}&lt;/i>
106 &lt;/div>
107 &lt;/div>
108 `;
109 }).join("");
110
111 //Render discussion threads in the sidebar
112 if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "discuss")) {
113 jQuery(".sidebar .items").html(/*html*/`
114 &lt;div onclick="fluid.screen('stream', '${dtps.classes[classNum].id}');" class="class item main back">
115 &lt;span class="label">Classes&lt;/span>
116 &lt;div class="icon">
117 &lt;i class="material-icons">keyboard_arrow_left&lt;/i>
118 &lt;/div>
119 &lt;/div>
120
121 ${
122 dtps.classes[classNum].newDiscussionThreadURL ? (/*html*/`
123 &lt;div onclick="window.open('${dtps.classes[classNum].newDiscussionThreadURL}')" class="class item back">
124 &lt;span class="label">New discussion&lt;/span>
125 &lt;div class="icon">
126 &lt;i class="material-icons">add&lt;/i>
127 &lt;/div>
128 &lt;/div>
129 `) : ""
130 }
131
132 &lt;div class="divider">&lt;/div>
133
134 ${discussionHTML}
135 `);
136 }
137
j35a697e2020-07-24 20:28:00 -0700138 //Load default thread if provided
139 if (defaultThread) {
140 dtps.loadThreadPosts(classNum, defaultThread);
141 }
142
j1c53f822020-06-07 15:32:06 -0700143 //Add click event listeners for discussion threads
144 $(".sidebar .item:not(.back)").click(function (event) {
145 //Show the selected thread as active
146 $(this).siblings().removeClass("active");
147 $(this).addClass("active");
148
149 //Get thread ID from HTML attribute
150 var postID = $(this).attr("data-threadID");
151
152 //Load the thread
153 dtps.loadThreadPosts(classNum, postID);
154 });
155 }
156
157 }).catch(function (err) {
158 dtps.error("Couldn't fetch discussion threads", "Caught promise rejection @ dtps.loadThreadsList", err);
159 });
160}
161
162/**
163 * Fetches and displays posts in a discussion
164 *
165 * @param {number} classNum The class number to render
166 * @param {string} threadID The discussion thread to render
167 */
168dtps.loadThreadPosts = function (classNum, threadID) {
169 //Show loading indicator
170 if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "discuss")) {
171 jQuery(".classContent").html(`&lt;div class="spinner">&lt;/div>`);
172 }
173
174 //Get discussion data
j35a697e2020-07-24 20:28:00 -0700175 var thread = null;
j1c53f822020-06-07 15:32:06 -0700176 dtps.classes[classNum].discussions.forEach(discussionThread => {
177 if (discussionThread.id == threadID) {
178 thread = discussionThread;
179 }
180 });
181
j35a697e2020-07-24 20:28:00 -0700182 if (!thread) {
183 dtps.error("Thread does not exist", "thread is null @ dtps.loadThreadPosts");
184 throw null;
185 }
186
j1c53f822020-06-07 15:32:06 -0700187 //Fetch discussion posts from the LMS
188 dtpsLMS.fetchDiscussionPosts(dtps.classes[classNum].id, threadID).then(function (postData) {
189 //Store posts in the discussion thread
190 thread.posts = postData;
191
192 //Post HTML array
193 var postHTML = [];
194
195 thread.posts.forEach((post, index) => {
196 //Get background and text color for iFrame content
197 var computedBackgroundColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--cards");
198 var computedTextColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--text");
199
200 //Create a new blob data URL with the post's content
201 var blob = new Blob([`
202 &lt;base target="_blank" />
203 &lt;link type="text/css" rel="stylesheet" href="https://cdn.jottocraft.com/CanvasCSS.css" media="screen,projection"/>
204 &lt;style>body {background-color: ${computedBackgroundColor}; color: ${computedTextColor};&lt;/style>
205 ${post.body}
206 `], { type: 'text/html' });
207 var discussionPostContentURL = window.URL.createObjectURL(blob);
208
209 //Create HTML for replies
210 var replyHTML = [];
211 if (post.replies) {
212 //Add space above replies
213 replyHTML.push(`&lt;br />`);
214
215 //Get HTML for each reply and add to array
216 post.replies.forEach(reply => {
217 replyHTML.push(/*html*/`
218 &lt;div style="margin-left: ${(reply.depth || 0) * 50}px;" class="discussionReply">
219 &lt;div class="discussionHeader">
220 ${post.author ? /*html*/`
221 &lt;img src="${reply.author.photoURL}" />
222 &lt;h5>${reply.author.name}&lt;/h5>
223 ` : ``}
224
225 &lt;span style="cursor: pointer;" onclick="window.open('${reply.replyURL}')">
226 &lt;i class="material-icons">reply&lt;/i>
227 &lt;span>Reply&lt;/span>
228 &lt;/span>
229 &lt;/div>
230
231 ${reply.body}
232 &lt;/div>
233 `)
234 });
235
236 //Add space below replies
237 replyHTML.push(`&lt;br />`);
238 }
239
240 //Create HTML for this post
241 //Note that index == 0 is the initial post
242 postHTML.push(/*html*/`
243 &lt;div class="card" style="margin-top: 20px;${index == 0 ? "margin-bottom: 75px;" : "padding: 20px 30px;"}">
244 &lt;!-- Thread title (Initial post) -->
245 ${index == 0 ? /*html*/`
246 &lt;h4 style="font-weight: bold">${thread.title}&lt;/h4>
247 ` : ''}
248
249 &lt;!-- Author header -->
250 &lt;div ${index == 0 ? `style="margin-bottom: 32px;"` : ""} class="discussionHeader">
251 ${post.author ? /*html*/`
252 &lt;img src="${post.author.photoURL}" />
253 &lt;h5>${post.author.name}&lt;/h5>
254 ` : ``}
255
256 &lt;i class="material-icons">calendar_today&lt;/i>
257 &lt;span>${dtps.formatDate(post.postedAt)}&lt;/span>
258
259 &lt;!-- Thread info (initial post) -->
260 ${index == 0 ? /*html*/`
261 ${thread.locked ? `&lt;i class="material-icons">lock&lt;/i>` : ""}
262 ${thread.requireInitialPost ? /*html*/`
263 &lt;i class="material-icons">visibility&lt;/i>
264 &lt;span>You must post before you can see other replies&lt;/span>
265 ` : ""}
266 ` : ''}
267 &lt;/div>
268
269 ${post.body}
270
271 ${replyHTML.join("")}
272
273 &lt;!-- Reply / Add post footer -->
274 ${post.replyURL ? /*html*/`
275 &lt;div ${index == 0 ? `style="margin-top: 32px;"` : ""} class="discussionFooter">
276 ${index == 0 ? /*html*/`
277 &lt;button onclick="window.open('${post.replyURL}')" class="btn small">&lt;i class="material-icons">post_add&lt;/i> Add Post&lt;/button>
278 ` : /*html*/`
279 &lt;button onclick="window.open('${post.replyURL}')" class="btn small">&lt;i class="material-icons">reply&lt;/i> Reply&lt;/button>
280 `}
281 &lt;/div>
282 ` : ""}
283 &lt;/div>
284 `);
285 });
286
287 //Render HTML
288 jQuery(".classContent").html(postHTML.join(""));
289 }).catch(function (err) {
290 dtps.error("Could not fetch discussion posts", "Caught promise rejection @ dtps.loadThreadPosts", err);
291 });
292}
293
294/**
295 * Renders the pages list for a class
296 *
297 * @param {string} courseID The course ID to render pages for
j35a697e2020-07-24 20:28:00 -0700298 * @param {string} [defaultPage] If provided, load the pageID by default
j1c53f822020-06-07 15:32:06 -0700299 */
j35a697e2020-07-24 20:28:00 -0700300dtps.loadPagesList = function (courseID, defaultPage) {
j1c53f822020-06-07 15:32:06 -0700301 //Get class index and set as selected class
302 var classNum = dtps.classes.map(course => course.id).indexOf(courseID);
303 dtps.selectedClass = classNum;
304
305 //Set pages as the selected content
306 dtps.selectedContent = "pages";
307 $("#dtpsTabBar .btn").removeClass("active");
308 $("#dtpsTabBar .btn.pages").addClass("active");
309
310 if (classNum == -1) {
311 //Class does not exist
312 dtps.error("The selected class doesn't exist", "classNum check failed @ dtps.loadPagesList");
313 }
314
315 //Load class color and things
316 dtps.presentClass(classNum);
317 jQuery("body").removeClass("collapsedSidebar");
318
319 if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "pages")) {
320 //Render sidebar
321 jQuery(".sidebar").html(/*html*/`
322 &lt;div class="bigLogo" style="text-align: center; margin: 10px 0 20px; height: 38px;">
323 &lt;i style="font-size: 28px; margin-right: 7px; vertical-align: middle;" class="material-icons">insert_drive_file&lt;/i>
324 &lt;h4 style="color: var(--text); display: inline-block; font-size: 28px; vertical-align: middle; margin: 0px;">Pages&lt;/h4>
325 &lt;/div>
326
327 &lt;div class="items">
328 &lt;div class="spinner">&lt;/div>
329 &lt;/div>
330 `);
331
332 //Delete existing class content HTML
333 jQuery(".classContent").html("");
334 }
335
336 dtpsLMS.fetchPages(dtps.classes[classNum].id).then(function (pagesData) {
337 //Store fetched pages data to the class
338 dtps.classes[classNum].pages = pagesData;
339
340 if (pagesData.length == 0) {
341 //No pages in this class
342 jQuery(".sidebar .items").html(/*html*/`
343 &lt;div onclick="fluid.screen('stream', '${dtps.classes[classNum].id}');" class="class item main back">
344 &lt;span class="label">Classes&lt;/span>
345 &lt;div class="icon">
346 &lt;i class="material-icons">keyboard_arrow_left&lt;/i>
347 &lt;/div>
348 &lt;/div>
349
350 &lt;p style="text-align: center; font-weight: bold; margin-top: 60px;">No pages found&lt;/p>
351 &lt;p style="text-align: center; font-size: 14px;">This class doesn't have any pages&lt;/p>
352 `);
353 } else {
354 //Loop over discusson threads array to create discussion thread HTML for the sidebar
355 var pageHTML = dtps.classes[classNum].pages.map(page => {
356 return /*html*/`
j35a697e2020-07-24 20:28:00 -0700357 &lt;div data-pageID="${page.id}" class="item ${(defaultPage &amp;&amp; (page.id == defaultPage)) ? "active" : ""}">
j1c53f822020-06-07 15:32:06 -0700358 &lt;span class="label">${page.title}&lt;/span>
359 &lt;div class="icon">
360 &lt;i class="material-icons">insert_drive_file&lt;/i>
361 &lt;/div>
362 &lt;/div>
363 `;
364 }).join("");
365
366 //Render discussion threads in the sidebar
367 if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "pages")) {
368 jQuery(".sidebar .items").html(/*html*/`
369 &lt;div onclick="fluid.screen('stream', '${dtps.classes[classNum].id}');" class="class item main back">
370 &lt;span class="label">Classes&lt;/span>
371 &lt;div class="icon">
372 &lt;i class="material-icons">keyboard_arrow_left&lt;/i>
373 &lt;/div>
374 &lt;/div>
375
376 &lt;div class="divider">&lt;/div>
377
378 ${pageHTML}
379 `);
380 }
381
j35a697e2020-07-24 20:28:00 -0700382 //Load default page if provided
383 if (defaultPage) {
384 dtps.loadPage(classNum, defaultPage);
385 }
386
j1c53f822020-06-07 15:32:06 -0700387 //Add click event listeners for pages
388 $(".sidebar .item:not(.back)").click(function (event) {
389 //Show the selected thread as active
390 $(this).siblings().removeClass("active");
391 $(this).addClass("active");
392
393 //Get page ID from HTML attribute
394 var pageID = $(this).attr("data-pageID");
395
396 //Load the page content
397 dtps.loadPage(classNum, pageID);
398 });
399 }
400 }).catch((err) => {
401 dtps.error("Couldn't fetch pages", "Caught promise rejection @ dtps.loadPagesList", err);
402 });
403}
404
405/**
406 * Fetches and renders a page and its contents
407 *
408 * @param {string} classNum The class number of the page to render
409 * @param {string} pageID The page ID to render
410 */
411dtps.loadPage = function (classNum, pageID) {
412 //Show loading indicator
413 if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "pages")) {
414 jQuery(".classContent").html(`&lt;div class="spinner">&lt;/div>`);
415 }
416
417 //Get page data
j35a697e2020-07-24 20:28:00 -0700418 var page = null;
j1c53f822020-06-07 15:32:06 -0700419 dtps.classes[classNum].pages.forEach(pageCheck => {
420 if (pageCheck.id == pageID) {
421 page = pageCheck;
422 }
423 });
424
j35a697e2020-07-24 20:28:00 -0700425 if (!page) {
426 dtps.error("Page does not exist", "page is null @ dtps.loadPage");
427 throw null;
428 }
429
j1c53f822020-06-07 15:32:06 -0700430 //Fetch page content
431 dtpsLMS.fetchPageContent(dtps.classes[classNum].id, page.id).then(function (pageContentData) {
432 //Store page content in the page object
433 page.content = pageContentData;
434
435 if ((dtps.classes[dtps.selectedClass].id == dtps.classes[classNum].id) &amp;&amp; (dtps.selectedContent == "pages")) {
436 //Get computed background and text color to style the iFrame with
437 var computedBackgroundColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--cards");
438 var computedTextColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--text");
439
440 //Generate a blob with the page content and get its data URL
441 var blob = new Blob([`
442 &lt;base target="_blank" />
443 &lt;link type="text/css" rel="stylesheet" href="https://cdn.jottocraft.com/CanvasCSS.css" media="screen,projection"/>
444 &lt;style>body {background-color: ${computedBackgroundColor}; color: ${computedTextColor};&lt;/style>
445 ${page.content}
446 `], { type: 'text/html' });
447
448 //Get URL from blob
449 var pageContentURL = window.URL.createObjectURL(blob);
450
451 //Render page card in the class content
452 jQuery(".classContent").html(/*html*/`
453 &lt;div class="card">
454 &lt;!-- Page title -->
455 &lt;h4 style="font-weight: bold;">${page.title}&lt;/h4>
456
457 &lt;!-- Page header -->
458 &lt;div style="margin-bottom: 32px;" class="discussionHeader">
459 ${page.author ? /*html*/`
460 &lt;img src="${page.author.photoURL}" />
461 &lt;h5>${page.author.name}&lt;/h5>
462 ` : ``}
463
464 &lt;i class="material-icons">calendar_today&lt;/i>
465 &lt;span>${"Last Updated " + dtps.formatDate(page.updatedAt)}&lt;/span>
466 &lt;/div>
467
468 &lt;!-- Page content -->
469 &lt;br />
470 &lt;iframe id="classPageIframe" onload="dtps.iframeLoad('classPageIframe')" style="margin: 10px 0px; width: 100%; border: none; outline: none;" src="${pageContentURL}" />
471 &lt;/div>
472 `);
473 }
474 }).catch((err) => {
475 dtps.error("Couldn't load page", "Caught promise rejection @ dtps.loadPage", err);
476 });
477}
478
479//Fluid UI screen definitions
j35a697e2020-07-24 20:28:00 -0700480fluid.externalScreens.discussions = (param) => {
481 //Split parameter string into variables
482 var courseID = param.split("|")[0];
483 var threadID = param.split("|")[1];
484
485 dtps.loadThreadsList(courseID, threadID);
j1c53f822020-06-07 15:32:06 -0700486}
487
j35a697e2020-07-24 20:28:00 -0700488fluid.externalScreens.pages = (param) => {
489 //Split parameter string into variables
490 var courseID = param.split("|")[0];
491 var pageID = param.split("|")[1];
492
493 dtps.loadPagesList(courseID, pageID);
j1c53f822020-06-07 15:32:06 -0700494}
495
496//Type definitions
497
498/**
499* @typedef {Object} DiscussionThread
500* @description Defines discussion thread objects in DTPS
501* @property {string} title Title of the discussion thread
502* @property {string} id Discussion thread ID
503* @property {DiscussionPost[]} posts Posts in this thread, with the initial one first. Assume that the topic has not been selected and loaded if this is undefined, and that there are no posts if this is an empty array.
504* @property {boolean} [locked] True if posting to the discussion thread is locked
505* @property {boolean} [requireInitialPost] True if the user must post before viewing others' posts
506*/
507
508/**
509* @typedef {Object} DiscussionPost
510* @description Defines discussion post objects in DTPS
511* @property {string} id Discussion post ID
512* @property {string} body Discussion post body HTML
513* @property {Date} postedAt Date for when the post was posted
514* @property {number} [depth] The depth level this post is
515* @property {User} [author] Discussion post author
516* @property {DiscussionPost[]} [replies] Replies to this post. Nested replies (replies to replies) should be after this post in the array with a depth of 1, not in the replies key.
517* @property {string} [replyURL] A URL that the user can visit to reply to this post
518*/
519
520/**
521* @typedef {Object} Page
522* @description Defines Page objects in DTPS
523* @property {string} title Page title
524* @property {string} id Page ID
525* @property {string} content Page content. Assume the page hasn't been selected/loaded if this is undefined.
526* @property {Date} [updatedAt] When the page was last updated
527* @property {User} [author] Page author
528*/</code></pre>
529 </article>
530 </section>
531
532
533
534
535</div>
536
537<nav>
538 <h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="dtps.html">dtps</a></li><li><a href="dtpsLMS.html">dtpsLMS</a></li></ul><h3><a href="global.html">Global</a></h3>
539</nav>
540
541<br class="clear">
542
543<footer>
j35a697e2020-07-24 20:28:00 -0700544 Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Fri Jul 24 2020 19:25:37 GMT-0700 (Pacific Daylight Time)
j1c53f822020-06-07 15:32:06 -0700545</footer>
546
547<script> prettyPrint(); </script>
548<script src="scripts/linenumber.js"> </script>
549</body>
550</html>