-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaper.js
128 lines (120 loc) · 5.24 KB
/
paper.js
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
// from November, 2016
String.prototype.capitalizeFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
function filter(typ, target) {
var secs = document.getElementsByTagName("section");
for (i = 0; i < secs.length; i++) {
if (target == 'all') {
secs[i].setAttribute("style", "");
continue;
}
if (secs[i].getAttribute(typ).split(", ").indexOf(target) < 0) {
secs[i].setAttribute("style", "display: none;");
} else {
secs[i].setAttribute("style", "");
}
}
}
function sortItem(typ) {
var secs = [].slice.call(document.getElementsByTagName("section"));
secs.sort(function(a, b) {
if (typ == "original") {
return b.getAttribute("num") - a.getAttribute("num")
}
if (typ == "year") {
var n = b.getElementsByClassName("year")[0].innerHTML - a.getElementsByClassName("year")[0].innerHTML;
if (n !== 0) return n;
}
var at = a.getElementsByClassName("title")[0].innerHTML;
var bt = b.getElementsByClassName("title")[0].innerHTML;
if (at > bt) return 1;
if (bt > at) return -1;
return 0;
});
document.getElementById("items").innerHTML = secs.map(x=>x.outerHTML).join("");
}
function parseItem(t, u, m, tag, year, num) {
var msg = "";
msg += '<section tags="' + tag + '" year="' + year + '" num="' + num + '">';
msg += '<div class="title">' + t + '</div>';
msg += u !="-" ? '<div class="url"><a target="_blank" href="' + u + '">' + u + '</a></div>': "-";
msg += '<div class="tag">' + tag + '</div>';
msg += '<div class="year">' + year + '</div>';
msg += '<div class="memo">' + (m != "-" ? '<pre>' + m + '</pre>' : '<span class="unread"></span>') + '</div>';
msg += "</section>";
return msg;
}
function sigmoid(t) {
return 1/(1+Math.pow(Math.E, -t));
}
function genLinks(typ, li) {
var count = {};
li.forEach(function(i) { count[i] = (count[i]||0)+1; });
li = li.filter(function (x, i, self) {
return self.indexOf(x) === i;
});
var reslinks = [];
reslinks.push('<a href="javascript:void(0)" onclick="filter(\'' + typ + '\',\'all\'); return false;">all</a>');
for (var i = 0; i < li.length; i++) {
var t = li[i];
var s = '<a href="javascript:void(0)" onclick="filter(\'' + typ + '\',\'' + t + '\'); return false;">' + t.capitalizeFirstLetter() + "(" + count[t].toString() + ")</a>";
var fsize = 1 + (sigmoid((count[t]-1)*0.5) - 0.5) * 0.7;
s = '<a href="javascript:void(0)" onclick="filter(\'' + typ + '\',\'' + t + '\'); return false;" style="font-size:' + fsize + 'em">' + t.capitalizeFirstLetter() + "(" + count[t].toString() + ")</a>";
reslinks.push(s);
}
return reslinks;
}
function parseJSON(x) {
var colnum = 5;
var xparse = JSON.parse(x);
var idlist = xparse.feed.entry;
var dict = {};
for (i=1;i<=colnum;i++) dict[i] = [];
var counter = 0;
var unread = 0;
for (var i=0; i < idlist.length; i++) {
var tmpitem = idlist[i].gs$cell;
while (tmpitem.col != counter % colnum + 1) {
dict[counter % colnum + 1].push("-");
if (counter % colnum + 1 == 3) {
unread++;
}
counter++;
}
dict[tmpitem.col].push(tmpitem.$t);//.replace(/(http:\/\/[^\s]+)/gi, '<a href="$1">$1</a>'));
counter++;
};
var ret = "";
var tags = [], years = [];
for (var i = 0; i < dict[1].length; i++) {
ret = parseItem(dict[1][i], dict[2][i], dict[3][i], dict[4][i], dict[5][i], i+1) + ret;
tags = tags.concat(dict[4][i] ? dict[4][i].trim().split(","): "");
years = years.concat(dict[5][i] ? dict[5][i].trim() : "");
}
var taglinks = genLinks('tags', tags.map(s => s.trim()).sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
}));
var yearlinks = genLinks('year', years.map(s => s.trim()).sort().reverse());
if (ret) {
document.getElementById("update").innerHTML = "Updated: " + xparse.feed.updated.$t;
document.getElementById("counter").innerHTML = '<span class="counter">Total/Unread: ' + (counter / colnum) + ' / ' + unread + "</span>";
document.getElementById("tags").innerHTML = "Tag: <span class='tag'>" + taglinks.join(", ") + "</span>";
document.getElementById("years").innerHTML = "Year: <span class='year'>" + yearlinks.join(", ") + "</span>";
document.getElementById("sorting").innerHTML = "Sort: <span class='sorting'><a href='javascript:void(0)' onclick='sortItem(\"year\"); return false;'>year</a> / <a href='javascript:void(0)' onclick='sortItem(\"title\"); return false;'>title</a> / <a href='javascript:void(0)' onclick='sortItem(\"original\"); return false;'>original</a></span>";
document.getElementById("items").innerHTML = ret;
} else {
document.getElementById("debug").innerHTML = "Load error";
}
}
function loadData() {
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status==200){
parseJSON(xmlhttp.responseText);
}
};
xmlhttp.open("GET",targetpath,true);
xmlhttp.send(null);
}
loadData();