forked from wuhaoworld/github-issues-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
126 lines (115 loc) · 3.72 KB
/
app.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
Ractive.DEBUG = false;
function index(page){
var page = parseInt(page) || 1;
window._G = window._G || {post: {}, postList: {}};
$('title').html(_config['blog_name']);
if(_G.postList[page] != undefined){
$('#container').html(_G.postList[page]);
return;
}
$.ajax({
url:"https://api.github.com/repos/"+_config['owner']+"/"+_config['repo']+"/issues",
data:{
filter : 'created',
page : page,
// access_token : _config['access_token'],
per_page : _config['per_page']
},
beforeSend:function(){
$('#container').html('<center><img src="loading.gif" class="loading"></center>');
},
success:function(data, textStatus, jqXHR){
var link = jqXHR.getResponseHeader("Link") || "";
var next = false;
var prev = false;
if(link.indexOf('rel="next"') > 0){
next = true;
}
if(link.indexOf('rel="prev"') > 0){
prev = true;
}
var ractive = new Ractive({
template : '#listTpl',
data : {
posts : data,
next : next,
prev : prev,
page : page
}
});
window._G.postList[page] = ractive.toHTML();
$('#container').html(window._G.postList[page]);
//将文章列表的信息存到全局变量中,避免重复请求
for(i in data){
var ractive = new Ractive({
template : '#detailTpl',
data : {post: data[i]}
});
window._G.post[data[i].number] = {};
window._G.post[data[i].number].body = ractive.toHTML();
var title = data[i].title + " | " + _config['blog_name'];
window._G.post[data[i].number].title = title;
}
}
});
}
function highlight(){
$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
}
// 动态加载多说评论框的函数
function toggleDuoshuoComments(container, id){
var el = document.createElement('div');
var url = window.location.href;
el.setAttribute('data-thread-key', id);
el.setAttribute('data-url', url);
DUOSHUO.EmbedThread(el);
jQuery(container).append(el);
}
function detail(id){
if(!window._G){
window._G = {post: {}, postList: {}};
window._G.post[id] = {};
}
if(_G.post[id].body != undefined){
$('#container').html(_G.post[id].body);
$('title').html(_G.post[id].title);
toggleDuoshuoComments('#container', id);
highlight();
return;
}
$.ajax({
url:"https://api.github.com/repos/"+_config['owner']+"/"+_config['repo']+"/issues/" + id,
data:{
// access_token:_config['access_token']
},
beforeSend:function(){
$('#container').html('<center><img src="loading.gif" alt="loading" class="loading"></center>');
},
success:function(data){
var ractive = new Ractive({
el: "#container",
template: '#detailTpl',
data: {post: data}
});
$('title').html(data.title + " | " + _config['blog_name']);
toggleDuoshuoComments('#container', id);
highlight();
}
});
}
var helpers = Ractive.defaults.data;
helpers.markdown2HTML = function(content){
return marked(content);
}
helpers.formatTime = function(time){
return time.substr(0,10);
}
var routes = {
'/': index,
'p:page': index,
'post/:postId': detail
};
var router = Router(routes);
router.init('/');