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