blob: d113ee9d2312cdef9124c14e1f05213f3a9b081d [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: pages-discussions.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: pages-discussions.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @file DTPS pages &amp; discussion functions
* @author jottocraft
*
* @copyright Copyright (c) 2018-2020 jottocraft. All rights reserved.
* @license GPL-2.0-only
*/
/**
* Renders the discussions list for a class
*
* @param {string} courseID The course ID to render discussion threads for
* @param {string} [defaultThread] The thread to load by default
*/
dtps.loadThreadsList = function (courseID, defaultThread) {
//Get class index and set as selected class
var classNum = dtps.classes.map(course => course.id).indexOf(courseID);
dtps.selectedClass = classNum;
//Set discussions as the selected content
dtps.selectedContent = "discuss";
$("#dtpsTabBar .btn").removeClass("active");
$("#dtpsTabBar .btn.discuss").addClass("active");
if (classNum == -1) {
//Class does not exist
dtps.error("The selected class doesn't exist", "classNum check failed @ dtps.loadThreadsList");
}
//Load class color and things
dtps.presentClass(classNum);
jQuery("body").removeClass("collapsedSidebar");
if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "discuss")) {
//Render sidebar
jQuery(".sidebar").html(/*html*/`
&lt;div class="bigLogo" style="text-align: center; margin: 10px 0 20px; height: 38px;">
&lt;i style="font-size: 28px; margin-right: 7px; vertical-align: middle;" class="material-icons">forum&lt;/i>
&lt;h4 style="color: var(--text); display: inline-block; font-size: 28px; vertical-align: middle; margin: 0px;">Discussions&lt;/h4>
&lt;/div>
&lt;div class="items">
&lt;div class="spinner">&lt;/div>
&lt;/div>
`);
//Delete existing class content HTML
jQuery(".classContent").html("");
}
//Fetch discussion thread data
dtpsLMS.fetchDiscussionThreads(dtps.classes[classNum].id).then(function (threadData) {
//Store fetched data to the selected class
dtps.classes[classNum].discussions = threadData;
if (dtps.classes[classNum].discussions.length == 0) {
//No discussion topics in this class
jQuery(".sidebar .items").html(/*html*/`
&lt;div onclick="fluid.screen('stream', '${dtps.classes[classNum].id}');" class="class item main back">
&lt;span class="label">Classes&lt;/span>
&lt;div class="icon">
&lt;i class="material-icons">keyboard_arrow_left&lt;/i>
&lt;/div>
&lt;/div>
&lt;p style="text-align: center; font-weight: bold; margin-top: 60px;">No discussions found&lt;/p>
&lt;p style="text-align: center; font-size: 14px;">This class doesn't have any discussions&lt;/p>
`);
} else {
//Loop over discusson threads array to create discussion thread HTML for the sidebar
var discussionHTML = dtps.classes[classNum].discussions.map(discussionThread => {
return /*html*/`
&lt;div data-threadID="${discussionThread.id}" class="item">
&lt;span class="label">${discussionThread.title}&lt;/span>
&lt;div class="icon">
&lt;i style="font-family: 'Material Icons Extended';" class="material-icons">${discussionThread.locked ? "lock_outline" : "chat_bubble_outline"}&lt;/i>
&lt;/div>
&lt;/div>
`;
}).join("");
//Render discussion threads in the sidebar
if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "discuss")) {
jQuery(".sidebar .items").html(/*html*/`
&lt;div onclick="fluid.screen('stream', '${dtps.classes[classNum].id}');" class="class item main back">
&lt;span class="label">Classes&lt;/span>
&lt;div class="icon">
&lt;i class="material-icons">keyboard_arrow_left&lt;/i>
&lt;/div>
&lt;/div>
${
dtps.classes[classNum].newDiscussionThreadURL ? (/*html*/`
&lt;div onclick="window.open('${dtps.classes[classNum].newDiscussionThreadURL}')" class="class item back">
&lt;span class="label">New discussion&lt;/span>
&lt;div class="icon">
&lt;i class="material-icons">add&lt;/i>
&lt;/div>
&lt;/div>
`) : ""
}
&lt;div class="divider">&lt;/div>
${discussionHTML}
`);
}
//Load default thread if provided
if (defaultThread) {
dtps.loadThreadPosts(classNum, defaultThread);
}
//Add click event listeners for discussion threads
$(".sidebar .item:not(.back)").click(function (event) {
//Show the selected thread as active
$(this).siblings().removeClass("active");
$(this).addClass("active");
//Get thread ID from HTML attribute
var postID = $(this).attr("data-threadID");
//Load the thread
dtps.loadThreadPosts(classNum, postID);
});
}
}).catch(function (err) {
dtps.error("Couldn't fetch discussion threads", "Caught promise rejection @ dtps.loadThreadsList", err);
});
}
/**
* Fetches and displays posts in a discussion
*
* @param {number} classNum The class number to render
* @param {string} threadID The discussion thread to render
*/
dtps.loadThreadPosts = function (classNum, threadID) {
//Show loading indicator
if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "discuss")) {
jQuery(".classContent").html(`&lt;div class="spinner">&lt;/div>`);
}
//Get discussion data
var thread = null;
dtps.classes[classNum].discussions.forEach(discussionThread => {
if (discussionThread.id == threadID) {
thread = discussionThread;
}
});
if (!thread) {
dtps.error("Thread does not exist", "thread is null @ dtps.loadThreadPosts");
throw null;
}
//Fetch discussion posts from the LMS
dtpsLMS.fetchDiscussionPosts(dtps.classes[classNum].id, threadID).then(function (postData) {
//Store posts in the discussion thread
thread.posts = postData;
//Post HTML array
var postHTML = [];
thread.posts.forEach((post, index) => {
//Get background and text color for iFrame content
var computedBackgroundColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--cards");
var computedTextColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--text");
//Create a new blob data URL with the post's content
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"/>
&lt;style>body {background-color: ${computedBackgroundColor}; color: ${computedTextColor};&lt;/style>
${post.body}
`], { type: 'text/html' });
var discussionPostContentURL = window.URL.createObjectURL(blob);
//Create HTML for replies
var replyHTML = [];
if (post.replies) {
//Add space above replies
replyHTML.push(`&lt;br />`);
//Get HTML for each reply and add to array
post.replies.forEach(reply => {
replyHTML.push(/*html*/`
&lt;div style="margin-left: ${(reply.depth || 0) * 50}px;" class="discussionReply">
&lt;div class="discussionHeader">
${post.author ? /*html*/`
&lt;img src="${reply.author.photoURL}" />
&lt;h5>${reply.author.name}&lt;/h5>
` : ``}
&lt;span style="cursor: pointer;" onclick="window.open('${reply.replyURL}')">
&lt;i class="material-icons">reply&lt;/i>
&lt;span>Reply&lt;/span>
&lt;/span>
&lt;/div>
${reply.body}
&lt;/div>
`)
});
//Add space below replies
replyHTML.push(`&lt;br />`);
}
//Create HTML for this post
//Note that index == 0 is the initial post
postHTML.push(/*html*/`
&lt;div class="card" style="margin-top: 20px;${index == 0 ? "margin-bottom: 75px;" : "padding: 20px 30px;"}">
&lt;!-- Thread title (Initial post) -->
${index == 0 ? /*html*/`
&lt;h4 style="font-weight: bold">${thread.title}&lt;/h4>
` : ''}
&lt;!-- Author header -->
&lt;div ${index == 0 ? `style="margin-bottom: 32px;"` : ""} class="discussionHeader">
${post.author ? /*html*/`
&lt;img src="${post.author.photoURL}" />
&lt;h5>${post.author.name}&lt;/h5>
` : ``}
&lt;i class="material-icons">calendar_today&lt;/i>
&lt;span>${dtps.formatDate(post.postedAt)}&lt;/span>
&lt;!-- Thread info (initial post) -->
${index == 0 ? /*html*/`
${thread.locked ? `&lt;i class="material-icons">lock&lt;/i>` : ""}
${thread.requireInitialPost ? /*html*/`
&lt;i class="material-icons">visibility&lt;/i>
&lt;span>You must post before you can see other replies&lt;/span>
` : ""}
` : ''}
&lt;/div>
${post.body}
${replyHTML.join("")}
&lt;!-- Reply / Add post footer -->
${post.replyURL ? /*html*/`
&lt;div ${index == 0 ? `style="margin-top: 32px;"` : ""} class="discussionFooter">
${index == 0 ? /*html*/`
&lt;button onclick="window.open('${post.replyURL}')" class="btn small">&lt;i class="material-icons">post_add&lt;/i> Add Post&lt;/button>
` : /*html*/`
&lt;button onclick="window.open('${post.replyURL}')" class="btn small">&lt;i class="material-icons">reply&lt;/i> Reply&lt;/button>
`}
&lt;/div>
` : ""}
&lt;/div>
`);
});
//Render HTML
jQuery(".classContent").html(postHTML.join(""));
}).catch(function (err) {
dtps.error("Could not fetch discussion posts", "Caught promise rejection @ dtps.loadThreadPosts", err);
});
}
/**
* Renders the pages list for a class
*
* @param {string} courseID The course ID to render pages for
* @param {string} [defaultPage] If provided, load the pageID by default
*/
dtps.loadPagesList = function (courseID, defaultPage) {
//Get class index and set as selected class
var classNum = dtps.classes.map(course => course.id).indexOf(courseID);
dtps.selectedClass = classNum;
//Set pages as the selected content
dtps.selectedContent = "pages";
$("#dtpsTabBar .btn").removeClass("active");
$("#dtpsTabBar .btn.pages").addClass("active");
if (classNum == -1) {
//Class does not exist
dtps.error("The selected class doesn't exist", "classNum check failed @ dtps.loadPagesList");
}
//Load class color and things
dtps.presentClass(classNum);
jQuery("body").removeClass("collapsedSidebar");
if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "pages")) {
//Render sidebar
jQuery(".sidebar").html(/*html*/`
&lt;div class="bigLogo" style="text-align: center; margin: 10px 0 20px; height: 38px;">
&lt;i style="font-size: 28px; margin-right: 7px; vertical-align: middle;" class="material-icons">insert_drive_file&lt;/i>
&lt;h4 style="color: var(--text); display: inline-block; font-size: 28px; vertical-align: middle; margin: 0px;">Pages&lt;/h4>
&lt;/div>
&lt;div class="items">
&lt;div class="spinner">&lt;/div>
&lt;/div>
`);
//Delete existing class content HTML
jQuery(".classContent").html("");
}
dtpsLMS.fetchPages(dtps.classes[classNum].id).then(function (pagesData) {
//Store fetched pages data to the class
dtps.classes[classNum].pages = pagesData;
if (pagesData.length == 0) {
//No pages in this class
jQuery(".sidebar .items").html(/*html*/`
&lt;div onclick="fluid.screen('stream', '${dtps.classes[classNum].id}');" class="class item main back">
&lt;span class="label">Classes&lt;/span>
&lt;div class="icon">
&lt;i class="material-icons">keyboard_arrow_left&lt;/i>
&lt;/div>
&lt;/div>
&lt;p style="text-align: center; font-weight: bold; margin-top: 60px;">No pages found&lt;/p>
&lt;p style="text-align: center; font-size: 14px;">This class doesn't have any pages&lt;/p>
`);
} else {
//Loop over discusson threads array to create discussion thread HTML for the sidebar
var pageHTML = dtps.classes[classNum].pages.map(page => {
return /*html*/`
&lt;div data-pageID="${page.id}" class="item ${(defaultPage &amp;&amp; (page.id == defaultPage)) ? "active" : ""}">
&lt;span class="label">${page.title}&lt;/span>
&lt;div class="icon">
&lt;i class="material-icons">insert_drive_file&lt;/i>
&lt;/div>
&lt;/div>
`;
}).join("");
//Render discussion threads in the sidebar
if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "pages")) {
jQuery(".sidebar .items").html(/*html*/`
&lt;div onclick="fluid.screen('stream', '${dtps.classes[classNum].id}');" class="class item main back">
&lt;span class="label">Classes&lt;/span>
&lt;div class="icon">
&lt;i class="material-icons">keyboard_arrow_left&lt;/i>
&lt;/div>
&lt;/div>
&lt;div class="divider">&lt;/div>
${pageHTML}
`);
}
//Load default page if provided
if (defaultPage) {
dtps.loadPage(classNum, defaultPage);
}
//Add click event listeners for pages
$(".sidebar .item:not(.back)").click(function (event) {
//Show the selected thread as active
$(this).siblings().removeClass("active");
$(this).addClass("active");
//Get page ID from HTML attribute
var pageID = $(this).attr("data-pageID");
//Load the page content
dtps.loadPage(classNum, pageID);
});
}
}).catch((err) => {
dtps.error("Couldn't fetch pages", "Caught promise rejection @ dtps.loadPagesList", err);
});
}
/**
* Fetches and renders a page and its contents
*
* @param {string} classNum The class number of the page to render
* @param {string} pageID The page ID to render
*/
dtps.loadPage = function (classNum, pageID) {
//Show loading indicator
if ((dtps.selectedClass == classNum) &amp;&amp; (dtps.selectedContent == "pages")) {
jQuery(".classContent").html(`&lt;div class="spinner">&lt;/div>`);
}
//Get page data
var page = null;
dtps.classes[classNum].pages.forEach(pageCheck => {
if (pageCheck.id == pageID) {
page = pageCheck;
}
});
if (!page) {
dtps.error("Page does not exist", "page is null @ dtps.loadPage");
throw null;
}
//Fetch page content
dtpsLMS.fetchPageContent(dtps.classes[classNum].id, page.id).then(function (pageContentData) {
//Store page content in the page object
page.content = pageContentData;
if ((dtps.classes[dtps.selectedClass].id == dtps.classes[classNum].id) &amp;&amp; (dtps.selectedContent == "pages")) {
//Get computed background and text color to style the iFrame with
var computedBackgroundColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--cards");
var computedTextColor = getComputedStyle($(".card.details")[0]).getPropertyValue("--text");
//Generate a blob with the page content and get its data URL
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"/>
&lt;style>body {background-color: ${computedBackgroundColor}; color: ${computedTextColor};&lt;/style>
${page.content}
`], { type: 'text/html' });
//Get URL from blob
var pageContentURL = window.URL.createObjectURL(blob);
//Render page card in the class content
jQuery(".classContent").html(/*html*/`
&lt;div class="card">
&lt;!-- Page title -->
&lt;h4 style="font-weight: bold;">${page.title}&lt;/h4>
&lt;!-- Page header -->
&lt;div style="margin-bottom: 32px;" class="discussionHeader">
${page.author ? /*html*/`
&lt;img src="${page.author.photoURL}" />
&lt;h5>${page.author.name}&lt;/h5>
` : ``}
&lt;i class="material-icons">calendar_today&lt;/i>
&lt;span>${"Last Updated " + dtps.formatDate(page.updatedAt)}&lt;/span>
&lt;/div>
&lt;!-- Page content -->
&lt;br />
&lt;iframe id="classPageIframe" onload="dtps.iframeLoad('classPageIframe')" style="margin: 10px 0px; width: 100%; border: none; outline: none;" src="${pageContentURL}" />
&lt;/div>
`);
}
}).catch((err) => {
dtps.error("Couldn't load page", "Caught promise rejection @ dtps.loadPage", err);
});
}
//Fluid UI screen definitions
fluid.externalScreens.discussions = (param) => {
//Split parameter string into variables
var courseID = param.split("|")[0];
var threadID = param.split("|")[1];
dtps.loadThreadsList(courseID, threadID);
}
fluid.externalScreens.pages = (param) => {
//Split parameter string into variables
var courseID = param.split("|")[0];
var pageID = param.split("|")[1];
dtps.loadPagesList(courseID, pageID);
}
//Type definitions
/**
* @typedef {Object} DiscussionThread
* @description Defines discussion thread objects in DTPS
* @property {string} title Title of the discussion thread
* @property {string} id Discussion thread ID
* @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.
* @property {boolean} [locked] True if posting to the discussion thread is locked
* @property {boolean} [requireInitialPost] True if the user must post before viewing others' posts
*/
/**
* @typedef {Object} DiscussionPost
* @description Defines discussion post objects in DTPS
* @property {string} id Discussion post ID
* @property {string} body Discussion post body HTML
* @property {Date} postedAt Date for when the post was posted
* @property {number} [depth] The depth level this post is
* @property {User} [author] Discussion post author
* @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.
* @property {string} [replyURL] A URL that the user can visit to reply to this post
*/
/**
* @typedef {Object} Page
* @description Defines Page objects in DTPS
* @property {string} title Page title
* @property {string} id Page ID
* @property {string} content Page content. Assume the page hasn't been selected/loaded if this is undefined.
* @property {Date} [updatedAt] When the page was last updated
* @property {User} [author] Page author
*/</code></pre>
</article>
</section>
</div>
<nav>
<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>
</nav>
<br class="clear">
<footer>
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)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>