-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpurejsblog.1.0.1.js
410 lines (391 loc) · 10.9 KB
/
purejsblog.1.0.1.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
var postsEditable = false;
var args = {};
$(document).ready(function(){
init_();
if (formKey == '' || ssKey == ''){
showMsg('notconfig');
}
var url = 'http://spreadsheets.google.com/feeds/list/' + ssKey + '/od6/public/values?alt=json-in-script';
if ($.isEmptyObject(args)){
url += '&max-results=' + maxPostsNum;
url += '&reverse=true';
$.getJSON(url + '&callback=?', showHomePage);
} else if (args.action && args.action == 'new'){
newPost();
return ;
} else if (args.p){
if (!/^(\d+)$/.test(args.p)){
location.href = location.pathname;
return ;
}
url += '&max-results=1&sq=postid%3D' + args.p;
$.getJSON(url + '&callback=?', showSinglePage);
} else if (args.page){
if (!/^(\d+)$/.test(args.page)){
location.href = location.pathname;
return ;
}
var index = (args.page - 1) * maxPostsNum + 1;
url += '&start-index=' + index + '&max-results=' + maxPostsNum;
url += '&reverse=true';
$.getJSON(url + '&callback=?', showHomePage);
} else{
showMsg('notfound');
}
});
function init_(){
args = getArgs();
if (args.editable){
postsEditable = true;
delete args.editable;
}
document.title = blogTitle;
$('#header h1:first a').text(blogTitle).attr('href', makeUrl('home'));
$('#homelink').attr('href', makeUrl('home'));
$('#aboutlink').attr('href', makeUrl('post', aboutPageId));
loadingAnimate(true);
}
function showHomePage(data){
loadingAnimate(false);
var feed = data.feed;
var totalResults = parseInt(feed.openSearch$totalResults.$t);
var entry = feed.entry;
if (!entry){
showMsg('nopost');
initEditableButton();
return ;
}
for (var i = 0; i < entry.length; i++){
var article = entry[i];
$article = makeArticleDom(article);
$article.appendTo($('#articles'));
}
pageLinks(totalResults);
initEditableButton();
}
function showSinglePage(data){
loadingAnimate(false);
var feed = data.feed;
var totalResults = parseInt(feed.openSearch$totalResults.$t);
if (!feed.entry){
showMsg('notfound');
return ;
}
var entry = feed.entry[0];
$article = makeArticleDom(entry);
$article.appendTo($('#articles'));
document.title = $article.find('h2.entry-title a').text();
initEditableButton();
if (args.action && args.action == 'edit'){
editPost($article);
}
}
function newPost(){
loadingAnimate(false);
document.title = 'Add New Post';
initActionForm_('new');
$("#ss-form").submit(function(){
var pwd = $('#pwdinput').val();
if (pwd == ''){
alert('ERROR: The password field is empty.');
return false;
}
});
$('#hiddenframe').load(function(){
var pwd = $('#pwdinput').val();
if (pwd == '')
return ;
$('#formbox p.msg a').attr('href', makeUrl('home'));
$('#formbox p.msg').hide().fadeIn();
});
}
function editPost($article){
initActionForm_('edit');
var id = $article.data('id');
$('#postIdInput').val(id);
$('#formbox a.cancel')
.attr('href', makeUrl('post', id));
$('#titleinput').val($article.data('title'))
$('#contenttextarea').val($article.data('content'));
$('#titleinput, #contenttextarea').keyup(function(){
var title = $('#titleinput').val();
var content = $('#contenttextarea').val();
updatePost($article, title, content);
});
$("#ss-form").submit(function(){
var pwd = $('#pwdinput').val();
if (pwd == ''){
alert('ERROR: The password field is empty.');
return false;
}
});
$('#hiddenframe').load(function(){
var pwd = $('#pwdinput').val();
if (pwd == '')
return ;
setTimeout(function(){
location.href = makeUrl('post', id);
}, 1000);
});
}
function initActionForm_(type){
var $form = $('#ss-form');
switch(type){
case 'new':
$('#formbox').show();
$('#formbox a.cancel, #formbox p.msg a')
.attr('href', location.pathname);
break;
case 'edit':
$form.find('h2').text('Edit Post');
$('#sbtinput').val('Save');
$('#formbox').show();
break;
}
$('#titleinput').attr('name', titleFieldName);
$('#contenttextarea').attr('name', contentFieldName);
$('#pwdinput').attr('name', passwordFieldName);
$('#postIdInput').attr('name', postidFieldName);
var actionUrl = "https://spreadsheets.google.com/formResponse?formkey=" +
formKey;
$form.attr("action", actionUrl);
}
function getArgs(){
var args = new Object();
var query = location.search.substring(1);
var pairs = query.split("&");
for(var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf('=');
if (pos == -1) continue;
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
value = decodeURIComponent(value);
args[argname] = value;
}
return args;
}
loadingAnimate.timeId = false;
function loadingAnimate(isRun){
var $load = $('#articleloading');
var $loadDots = $('#loadingdots');
if (isRun){
$('#loadingmsg').text('Loading');
$load.show();
$loadDots.show();
loadingAnimate.timeId = setInterval(function(){
var dotsNum = $loadDots.text().length;
if (dotsNum < 6){
$loadDots.text($loadDots.text() + '.');
} else{
$loadDots.text('.');
}
}, 500);
} else{
if (loadingAnimate.timeId)
clearInterval(loadingAnimate.timeId);
$load.hide();
}
}
function showMsg(type){
loadingAnimate(false);
$('#articleloading').show();
$('#loadingdots').hide();
switch(type){
case 'nopost':
$('#loadingmsg').text('No posts.');
break;
case 'notfound':
$('#loadingmsg').text('Page not found');
break;
case 'notconfig':
$('#loadingmsg').html('This blog is not yet setup. <a href="http://purejsblog.cuoluo.net/index.html?p=20">Learn more...</a>');
break;
}
}
function makeArticleDom(entry){
var articleHtml = '<div class="article">' +
'<div class="entry-date"><abbr class="published"></abbr></div>' +
'<h2 class="entry-title"><a href=""></a></h2>' +
'<div class="entry-content"></div></div>';
var $article = $(articleHtml);
var id = entry.gsx$postid.$t;
var published = entry.gsx$timestamp.$t.split(' ')[0];
var title = entry.gsx$title.$t;
var content = entry.gsx$content.$t;
$article.data('id', id);
$article.data('title', title);
$article.data('content', content);
$article.attr('id', 'post_' + id);
$article.find('abbr.published').text(published);
var postUrl = makeUrl('post', id);
$article.find('h2.entry-title a').attr('href', postUrl);
updatePost($article, title, content);
var $editBox = makeEditBox(entry);
$article.find('h2.entry-title').after($editBox);
$article.hover(
function () {
if (postsEditable)
$(this).find('div.editbox').addClass("visible");
},
function () {
if (postsEditable)
$(this).find('div.editbox').removeClass("visible");
}
);
return $article;
}
function updatePost($article, title, content){
$article.find('h2.entry-title a').text(title);
if (contentToHtml){
$article.find('div.entry-content').html('<pre>'+content+'</pre>');
} else{
var html = $('<div/>').text(content).html();
$article.find('div.entry-content').html('<pre>'+html+'</pre>');
}
}
function makeEditBox(entry){
var id = entry.gsx$postid.$t;
var editBoxHtml = '<div class="editbox">' +
'<ul class="mini_commands">' +
'<li><a class="editlink" href="/">Edit</a></li>' +
'<li><a class="deletelink" onclick="return false;" href="/">Delete</a></li>' +
'</ul></div>';
var $editBox = $(editBoxHtml);
$editBox.find('a.editlink').attr('href', '?action=edit&p=' + id);
$editBox.find('a.deletelink').click(deletePost)
.attr('href', '?action=delete&p=' + id);
return $editBox;
}
function deletePost(){
var pwd = window.prompt("Input admin password to confirm delete.","");
if (!pwd)
return ;
var $article = $(this).parents("div.article");
var id = $article.data('id');
$('#titleinput').val('');
$('#contenttextarea').val('');
$('#pwdinput').val(pwd);
$('#postIdInput').val(id);
initActionForm_('delete');
$('#sbtinput').click();
$('#hiddenframe').load(function(){
var pwd = $('#pwdinput').val();
if (pwd == '')
return ;
$article.fadeOut(1600, function(){
location.href = location.pathname;
});
});
}
function makeUrl(type, val){
var url = location.pathname + '?';
if (postsEditable)
url += 'editable=true&';
switch(type){
case 'post':
url += 'p=' + val;
break;
case 'page':
url += 'page=' + val;
break;
case 'home':
url = location.pathname;
break;
}
if (location.protocol == 'file:' && $.browser.msie)
url = 'file://' + url;
return url;
}
function pageLinks(total){
var lastPage = Math.ceil(total/maxPostsNum);
if (lastPage == 1)
return ;
var $pagination = $('#pagination');
$pagination.show();
if (args.page)
var curPage = parseInt(args.page);
else
var curPage = 1;
if (curPage == 1){
var prevLinkHtml = '<span class="disabled prev_page">« Previous</span>';
var nextLinkHtml = '<a rel="next" class="next_page" href="' +
makeUrl('page', 2) + '">Next »</a>';
} else if (curPage == lastPage){
var prePage = lastPage - 1;
var prevLinkHtml = '<a rel="prev start" class="prev_page" href="' +
makeUrl('page', prePage) + '">« Previous</a>';
var nextLinkHtml = '<span class="disabled next_page">Next »</span>';
} else{
var prevLinkHtml = '<a rel="prev start" class="prev_page" href="' +
makeUrl('page', (curPage - 1)) + '">« Previous</a>';
var nextLinkHtml = '<a rel="next" class="next_page" href="' +
makeUrl('page', (curPage + 1)) + '">Next »</a>';
}
var pageLinksHtml = '';
for (var i = 1; i <= lastPage; i++){
if (i == curPage)
pageLinksHtml += '<span class="current">' + i + '</span>';
else
pageLinksHtml += '<a rel="next" href="'+makeUrl('page', i)+'">'+i+'</a>';
}
pageLinksHtml = prevLinkHtml + pageLinksHtml + nextLinkHtml;
$(pageLinksHtml).appendTo($pagination);
}
function initEditableButton(){
if ($('div.article').length > 1)
var t = 'make posts ';
else
var t = 'make post ';
if (args.action == 'edit')
return ;
var $editonoff = $('#editonoff');
$editonoff.show();
if (postsEditable){
$editonoff.removeClass('off').addClass('on');
$editonoff.text(t + 'uneditable');
makeEditable(true);
} else{
$editonoff.removeClass('on').addClass('off');
$editonoff.text(t + 'editable');
makeEditable(false);
}
$editonoff.click(function(){
if ($(this).hasClass('on')){
// turn off editable
$(this).text(t + 'editable');
$(this).removeClass('on').addClass('off');
postsEditable = false;
makeEditable(false);
} else{
// turn on editable
$(this).text(t + 'uneditable');
$(this).removeClass('off').addClass('on');
postsEditable = true;
makeEditable(true);
}
});
$('#helplink').addClass('leftline');
}
function makeEditable(b){
$('#aboutlink').attr('href', makeUrl('post', aboutPageId));
var curPage = parseInt($('#pagination span.current').text());
var $addnewpost = $('#addnewpost');
if (b)
$addnewpost.show();
else
$addnewpost.hide();
$('#pagination a').each(function(i){
if ($(this).hasClass('prev_page')){
this.href = makeUrl('page', curPage - 1);
} else if ($(this).hasClass('next_page')){
this.href = makeUrl('page', curPage + 1);
} else{
var page = $(this).text();
this.href = makeUrl('page', page);
}
});
$('div.article').each(function(i){
var id = $(this).data('id');
$(this).find('h2.entry-title a').attr('href', makeUrl('post', id));
});
}