Skip to content

Instantly share code, notes, and snippets.

@prb112
Created October 25, 2013 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prb112/7160173 to your computer and use it in GitHub Desktop.
Save prb112/7160173 to your computer and use it in GitHub Desktop.
lays out forum entries given the given forum entry
<!DOCTYPE html>
<html lang="en">
<head>
<title>Social Business Playground</title>
<script type="text/javascript">
dojoConfig = {
parseOnLoad: true
};
</script>
<script type="text/javascript" src="http://localhost:8080/sbt.dojo180/dojo/dojo.js.uncompressed.js"></script>
<script type="text/javascript" src="http://localhost:8080/sbt.sample.web/library?lib=dojo&ver=1.8.0&env=connectionsEnvironment"></script>
<script>
require(['dojo/parser']);
</script>
</head>
<body>
<div id="4f61977c-7075-44e7-a85a-26170bb89bf9"></div>
<pre>
<div id="json" class="alert"></div>
</pre>
<script>
try {
require(["sbt/connections/ForumService", "sbt/dom", "sbt/json", "dojo/dom-construct"],
function(ForumService, dom, json, domConstruct) {
var forumService = new ForumService();
var rootTopicUuid = "4f61977c-7075-44e7-a85a-26170bb89bf9";
var promise = forumService.getForumTopicReplies(rootTopicUuid);
promise.then(
function(forumReplies) {
// Sets up the inital set of keys
var keys = [];
keys.push(rootTopicUuid);
// Entries which are going to be processed
var entries = [];
// Processes the Forum Replies
// Orders the Entries into a nice lookup array
for(var idx in forumReplies) {
//Get the Forum Reply
var forumReply = forumReplies[idx];
var topicId = forumReply.getReplyToPostUuid();
var entry = entries[topicId];
/**
* Checks to see if the entry exists and makes a
* a real array and adds the current forumReply
* to the right array
*/
if(typeof entry === 'undefined'){
entry = [];
}
entry.push(forumReply);
entries[topicId] = entry;
//-> Increments the Arrays for the topics
//-> Ensures that the Keys are recorded in an array
//-> we can process
var replyId = forumReply.getReplyUuid();
keys.push(replyId);
// For Debug:
// Topic/Forum -> Output to Console
console.log(idx + " " + replyId + " " + topicId);
}
// For Debug:
console.log(entries);
console.log(keys);
//Debug Only -
//dom.setText("json", json.jsonBeanStringify(forumReplies));
//Now that you have the array, you can do an easy layout
//Start at the Root Topic Uuid
// Refers to the item with a given attribute id = rootUuid
// ks - keys
// ens - entries
// root - rootTopicUuid
// indent - number of levels
var process = function(ks, ens, root, indent){
var forumReplies = ens[root];
for(var idx in forumReplies){
var reply = forumReplies[idx];
var node = dom.byId(root);
var forumReply = domConstruct.create("div");
forumReply.id = reply.getReplyUuid();
forumReply.innerHTML = reply.getContent();
domConstruct.place(forumReply, node, "last");
process(ks, ens, reply.getReplyUuid(), ++indent);
}
};
process(keys, entries, rootTopicUuid, 0);
},
function(error) {
dom.setText("json", json.jsonBeanStringify(error));
}
);
}
);} catch(e) {;
document.getElementById('content').innerHTML = 'Exception:'+e.toString();}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment