-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientLogic.js.html
169 lines (137 loc) · 4.19 KB
/
ClientLogic.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<script>
$(function() {
google.script.run.withSuccessHandler(renderTagList)
.getTagsSortedByPopularity();
findEntriesByTag('All', 0, 10);
});
String.prototype.format = function (){
var args = arguments;
return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (curlyBrack, index) {
return ((curlyBrack == "{{") ? "{" : ((curlyBrack == "}}") ? "}" : args[index]));
});
};
/**
* Displays each tag as an html link. Upon click, request the entries matching
* the tags and display it in the entry area.
*/
function renderTagList(tags) {
tags.forEach(function(tag) {
var tagDisplayObj = getHtmlForASingleTag(tag.name, tag.count);
$("#tags-list").append(tagDisplayObj.html);
addTagClickEvent(tagDisplayObj.id, tag.name);
});
}
// The code in this function runs when the page is loaded.
function onAddEntry() {
var tagsArray = $("#input-tags").val().split(' ');
google.script.run.withSuccessHandler(refreshTopTen)
.addEntry($("#input-content").val(), tagsArray);
return false;
};
function refreshTopTen(things) {
$("#input-tags").val('');
$("#input-content").val('');
findEntriesByTag('All', 0, 10);
}
/**
* Clears the entry div container and renders the provided entries.
* @param entries
*/
function renderEntries(result) {
var entries = result.entries;
// clear all the entry tag events first
var entryTagIdPrefix = 'entry-tag-';
//$("[id^=" + entryTagIdPrefix + "]").off('click');
var entryListNode = $("#entry-list");
// Prev and Next links
entryListNode.append('<div>');
if ( result.offset > 0 ) {
entryListNode.append("[<a href='' id='prev'>Prev</a>] ");
}
else {
entryListNode.append("[Prev] ");
}
entryListNode.append("displaying entries {0} to {1} ".format(
result.offset + 1, result.offset + result.count));
if ( result.hasMore ) {
entryListNode.append("[<a href='' id='next'>Next</a>] ");
}
else {
entryListNode.append("[Next] ");
}
entryListNode.append('</div>');
$("#prev").click(function() {
findEntriesByTag(result.searchValue,
result.offset - result.count,
result.count);
return false;
});
$("#next").click(function() {
findEntriesByTag(result.searchValue,
result.offset + result.count,
result.count);
return false;
});
var markDownReader = new commonmark.Parser();
var markDownWriter = new commonmark.HtmlRenderer();
entries.forEach(function(entry) {
var contentHtml = markDownWriter.render(
markDownReader.parse(entry.content));
var html = '<div id="entry">';
html += "<p id='entry-content'>{0}</p>".format(contentHtml);
html += 'Tags: ';
// accumulate the id and the tag name to register the event, after the
// tag link has been added to the DOM tree
var tagIds = [];
entry.tags.forEach(function(name) {
var tagDisplayObj = getHtmlForASingleTag(name, null, entryTagIdPrefix);
html += tagDisplayObj.html;
tagDisplayObj.name = name;
tagIds.push(tagDisplayObj);
});
html += "<br />{0}".format(
new Date(entry.creationTime).toLocaleString());
html += '</div>';
entryListNode.append(html);
tagIds.forEach(function(obj) {
addTagClickEvent(obj.id, obj.name);
});
});
}
/**
* Returns an object containing the HTML string for the tag and the element's
* id.
* @return {html: string, id: string}
*/
function getHtmlForASingleTag(tagName, tagCount, idPrefix) {
if ( 'undefined' == typeof idPrefix ) {
idPrefix = 'tag-';
}
var id = idPrefix + tagName;
var html;
if (null != tagCount) {
html = "<a href='' id='{0}'>{1} ({2})</a> ".format(id, tagName, tagCount);
}
else {
html = "<a href='' id='{0}'>{1}</a> ".format(id, tagName);
}
return {html: html, id: id};
}
/**
* Handles clicking on a tag -> find entries matching the provided tag.
*/
function addTagClickEvent(id, tagName) {
$("#" + id).click(function() {
findEntriesByTag(tagName, 0, 10);
return false;
});
}
function findEntriesByTag(tagName, offset, count) {
$("#entry-list").html('');
$("#prev").off('click');
$("#next").off('click');
google.script.run
.withSuccessHandler(renderEntries)
.findEntriesByTag(tagName, offset, count);
}
</script>