blob: 801008ce3ab3851d8b691a88042fab7602f80fef [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: scripts/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: scripts/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;">There aren't any discussion topics&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="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 $(".class: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("Could not 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.loadPages = 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.loadPages");
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 if (data.error) {
326 jQuery(".sidebar").html(`&lt;div onclick="dtps.selectedContent = 'stream'; dtps.chroma(); dtps.classStream(dtps.selectedClass);" class="class item main back">
327 &lt;span class="label">Classes&lt;/span>
328 &lt;div class="icon">&lt;i class="material-icons">keyboard_arrow_left&lt;/i>&lt;/div>
329 &lt;/div>
330 &lt;div class="divider">&lt;/div>
331 &lt;h5 style="text-align: center; font-weight: bold;">No pages found&lt;/h5>&lt;p style="text-align: center;">This class doesn't have any pages&lt;/p>`)
332 } else {
333 dtps.classes[num].pages = [];
334 dtps.classes[num].pagelist = [];
335 for (var i = 0; i &lt; data.length; i++) {
336 dtps.classes[num].pages.push({
337 id: data[i].page_id,
338 title: data[i].title,
339 content: "",
340 num: i
341 });
342 dtps.classes[num].pagelist.push(`
343 &lt;div onclick="dtps.selectedPage = ` + data[i].page_id + `" class="class item ` + data[i].page_id + `">
344 &lt;span class="label">` + data[i].title + `&lt;/span>
345 &lt;div class="icon">&lt;i class="material-icons">notes&lt;/i>&lt;/div>
346 &lt;/div>
347 `);
348 }
349 if ((dtps.selectedClass == num) &amp;&amp; (dtps.selectedContent == "pages")) {
350 jQuery(".sidebar").html(`&lt;div onclick="dtps.selectedContent = 'stream'; dtps.chroma(); dtps.classStream(dtps.selectedClass);" class="class item main back">
351 &lt;span class="label">Classes&lt;/span>
352 &lt;div class="icon">&lt;i class="material-icons">keyboard_arrow_left&lt;/i>&lt;/div>
353 &lt;/div>
354 &lt;div class="divider">&lt;/div>
355 ` + dtps.classes[num].pagelist.join(""))
356 }
357
358 $(".class:not(.back)").click(function (event) {
359 $(this).siblings().removeClass("active")
360 $(this).addClass("active")
361 dtps.getPage(dtps.classes[dtps.selectedClass].id, dtps.selectedPage);
362 });
363 }
364 });
365}
366
367//Loads a page
368dtps.getPage = function (classID, id, fromModuleStream) {
369 if (id == undefined) var id = dtps.selectedPage;
370 if ((dtps.classes[dtps.selectedClass].id == classID) &amp;&amp; ((dtps.selectedContent == "pages") || fromModuleStream)) {
371 jQuery(".classContent").html(`&lt;div class="spinner">&lt;/div>`);
372 }
373 var spinnerTmp = true;
374 dtps.webReq("canvas", "/api/v1/courses/" + classID + "/pages/" + id, function (resp) {
375 var data = JSON.parse(resp);
376 if ((dtps.classes[dtps.selectedClass].id == classID) &amp;&amp; ((dtps.selectedContent == "pages") || fromModuleStream)) {
377 var blob = new Blob([`&lt;base target="_blank" /> &lt;link type="text/css" rel="stylesheet" href="https://cdn.jottocraft.com/CanvasCSS.css" media="screen,projection"/>
378 &lt;style>body {background-color: ` + getComputedStyle($(".card.details")[0]).getPropertyValue("--cards") + `; color: ` + getComputedStyle($(".card.details")[0]).getPropertyValue("--text") + `}&lt;/style>` + data.body], { type: 'text/html' });
379 var newurl = window.URL.createObjectURL(blob);
380 jQuery(".classContent").html((fromModuleStream ? `&lt;div class="acrylicMaterial" onclick="dtps.moduleStream(dtps.selectedClass)" style="line-height: 40px;display: inline-block;border-radius: 20px;margin: 82px 0px 0px 82px; cursor: pointer;">
381 &lt;div style="font-size: 16px;display: inline-block;vertical-align: middle;margin: 0px 20px;">&lt;i style="vertical-align: middle;" class="material-icons">keyboard_arrow_left&lt;/i> Back&lt;/div>&lt;/div>` : "") + `
382 &lt;div class="card">
383 &lt;h4 style="font-weight: bold;">` + data.title + `&lt;/h4>
384 &lt;br />
385 &lt;iframe id="classPageIframe" onload="dtps.iframeLoad('classPageIframe')" style="margin: 10px 0px; width: 100%; border: none; outline: none;" src="` + newurl + `" />
386 &lt;/div>
387 `);
388 }
389 });
390}
391
392//Fluid UI screen definitions
393fluid.externalScreens.discussions = (courseID) => {
394 dtps.loadThreadsList(courseID);
395}
396
397fluid.externalScreens.pages = (courseID) => {
398 dtps.loadPages(courseID);
399}
400
401//Type definitions
402
403/**
404* @typedef {Object} DiscussionThread
405* @description Defines discussion thread objects in DTPS
406* @property {string} title Title of the discussion thread
407* @property {string} id Discussion thread ID
408* @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.
409* @property {boolean} [locked] True if posting to the discussion thread is locked
410* @property {boolean} [requireInitialPost] True if the user must post before viewing others' posts
411*/
412
413/**
414* @typedef {Object} DiscussionPost
415* @description Defines discussion post objects in DTPS
416* @property {string} id Discussion post ID
417* @property {string} body Discussion post body HTML
418* @property {Date} postedAt Date for when the post was posted
419* @property {number} [depth] The depth level this post is
420* @property {User} [author] Discussion post author
421* @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.
422* @property {string} [replyURL] A URL that the user can visit to reply to this post
423*/
424
425/**
426* @typedef {Object} Page
427* @description Defines Page objects in DTPS
428* @property {string} title Page title
429* @property {string} id Page ID
430* @property {string} content Page content. Assume the page hasn't been selected/loaded if this is undefined.
431* @property {Date} [updatedAt] When the page was last updated
432* @property {User} [author] Page author
433*/</code></pre>
434 </article>
435 </section>
436
437
438
439
440</div>
441
442<nav>
443 <h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="dtps.html">dtps</a></li><li><a href="dtps.gradeCalc.html">gradeCalc</a></li><li><a href="dtpsLMS.html">dtpsLMS</a></li></ul><h3><a href="global.html">Global</a></h3>
444</nav>
445
446<br class="clear">
447
448<footer>
449 Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Fri May 29 2020 18:46:27 GMT-0700 (Pacific Daylight Time)
450</footer>
451
452<script> prettyPrint(); </script>
453<script src="scripts/linenumber.js"> </script>
454</body>
455</html>