forked from isnowfy/simple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
361 lines (361 loc) · 14.7 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
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
var global = {};
var gconfig = null;
var repo = null;
var editor = null;
var contentpattern = /<!-- content -->\n([\s\S]*)\n<!-- content end -->\n/m;
var pathpattern = /\/\/path\nvar path=\"(.*)\";\n\/\/path end\n/m;
var mdpattern = /<!-- markdown -->\n([\s\S]*)\n<!-- markdown end -->\n/m;
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth()+1).toString();
var dd = this.getDate().toString();
return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]);
};
function reescape(data) {
return data.replace(/>/g, ">").replace(/</g, "<");
}
function mdupdate() {
var converter = new Showdown.converter();
var tmp = $("#editmd").val();
tmp = tmp.replace(/~~~~\{(.*)\}\n([\s\S]*?)~~~~\n/mg, function(a1, a2, a3) {return "<pre><code class=\"language-"+a2+"\">"+reescape(a3)+"</code></pre>";});
tmp = tmp.replace(/~~~~\n([\s\S]*?)~~~~\n/mg, function(a1, a2) {return "<pre><code>"+reescape(a2)+"</code></pre>"});
tmp = converter.makeHtml(tmp);
$("#edithtml").html(tmp);
Prism.highlightAll();
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "edithtml"]);
}
function curry(fn) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
var innerArgs = Array.prototype.slice.call(arguments);
var finalArgs = args.concat(innerArgs);
return fn.apply(null, finalArgs);
};
}
function asyncFinish(total, success) {
var cnt = 0;
$("#loading").show();
return function() {
cnt++;
if (cnt == total) {
$("#loading").hide();
if (typeof success == "function")
success();
}
}
}
function asyncSeq(success) {
var args = Array.prototype.slice.call(arguments, 1);
var finish = asyncFinish(args.length, success);
for (var i = 0; i < args.length; ++i) {
args[i](finish);
}
}
function syncSeq(success) {
var args = Array.prototype.slice.call(arguments, 1);
var finish = asyncFinish(1, success);
var l = args.length;
var tmp = curry(args[l-1], finish);
for (var i = l-2; i >= 0; --i)
tmp = curry(args[i], tmp);
tmp();
}
function errShow(item, err) {
if (typeof err != "undefined" && err != null) {
console.log(err);
$("#loading").hide();
item.show();
return true;
}
return false;
}
function asyncWrite(data, target, err, finish) {
if (!repo)
Spine.Route.navigate("");
repo.write("master", target, data, "simple",
function(e) {
var ret = err(e);
if (ret == false)
finish();
});
}
function asyncWriteFile(source, target, err, finish) {
if (!repo)
Spine.Route.navigate("");
$.ajax({
url: source,
type: "GET",
success: function(data) {asyncWrite(data, target, err, finish)},
error: function(e) {err(e);}
});
}
function checkpass(user, pass, cbsuccess, cberror) {
var github = new Github({
username: user,
password: pass,
auth: "basic"
});
var u = github.getUser();
u.show(user, function(err, ret){
$("#loading").hide()
if (!cberror(err)) {
global.github = github;
global.user = user;
repo = github.getRepo(user, user+".github.io");
cbsuccess();
}
});
}
$(document).ready(function() {
var Logins = Spine.Controller.sub({
el: $("#login"),
elements: {
"form": "form",
"#loginuser": "user",
"#loginpass": "pass",
"#loginerror": "err",
},
events: {
"submit form": "check",
},
check: function(e) {
$("#loading").show();
e.preventDefault();
checkpass(this.user.val(), this.pass.val(),
function(){Spine.Route.navigate("/main");},
curry(errShow, this.err));
},
init: function() {
this.user.val("");
this.pass.val("");
$("#loading").hide();
this.err.hide();
}
});
var Mains = Spine.Controller.sub({
el: $("#main"),
elements: {
"#initerror": "err",
"#initok": "ok",
},
events: {
"click #init": "initRepo",
"click #go": "go",
},
initRepo: function(e) {
e.preventDefault();
var error = curry(errShow, this.err);
var a1 = curry(asyncWriteFile, "template/index.html", "index.html", error);
var a2 = curry(asyncWriteFile, "template/main.css", "main.css", error);
var a3 = curry(asyncWriteFile, "template/main.js", "main.js", error);
var config = {"name": global.user, "number_of_posts_per_page": 7, "disqus_shortname": "", "posts": [], "pages": []};
var a4 = curry(asyncWrite, JSON.stringify(config), "main.json", error);
var a5 = curry(asyncWrite, "", "CNAME", error);
syncSeq(function() {$("#initok").show()}, a1, a2, a3, a4, a5);
},
go: function(e) {
this.navigate("/posts");
},
init: function() {
$("#loading").hide();
this.err.hide();
}
});
var Posts = Spine.Controller.sub({
el: $("#posts"),
init: function(param) {
$("#loading").hide();
if (editor != null)
editor.destroy();
var type = null;
var num = null;
var now = null;
if (typeof param != "undefined" && param.hasOwnProperty("type"))
type = param.type;
if (typeof param != "undefined" && param.hasOwnProperty("num"))
num = param.num;
if (repo != null) {
$("#loading").show();
repo.read("master", "main.json", function(err, data) {
$("#loading").hide();
$("#posttitle").val("");
$("#postpath").val("");
$("#postdate").val("");
$("#posttags").val("");
$("#editmd").val("");
$("#edithtml").html("");
var config = JSON.parse(data);
config.posts.sort(function(a, b){
if (a.date > b.date)
return -1;
if (a.date < b.date)
return 1;
return 0;
});
gconfig = config;
var posts = config.posts;
var pages = config.pages;
for (var i = 0; i < posts.length; ++i) {
posts[i].num = i;
posts[i].type = "post";
posts[i].active = false;
if (type == "post" && num != "null" && Math.floor(num) == i) {
posts[i].active = true;
now = posts[i];
$("#postSave").attr("href", "#/posts/savepost");
$("#postDelete").attr("href", "#/posts/deletepost/"+i);
}
}
for (var i = 0; i < pages.length; ++i) {
pages[i].num = i;
pages[i].type = "page";
pages[i].active = false;
if (type == "page" && num != "null" && Math.floor(num) == i) {
pages[i].active = true;
now = pages[i];
$("#postSave").attr("href", "#/posts/savepage");
$("#postDelete").attr("href", "#/posts/deletepage/"+i);
}
}
var itemTemplate = Hogan.compile($("#postsItem").html());
var postsItemHtml = itemTemplate.render({items: posts});
var pagesItemHtml = itemTemplate.render({items: pages});
$("#postItems").html(postsItemHtml);
$("#pageItems").html(pagesItemHtml);
if (type != null && type.slice(0, 3) == "new") {
if (type.slice(3) == "post") {
$("#postSave").attr("href", "#/posts/savepost");
$("#postDelete").attr("href", "#/posts");
}
if (type.slice(3) == "page") {
$("#postSave").attr("href", "#/posts/savepage");
$("#postDelete").attr("href", "#/posts");
}
$("#postdate").val((new Date()).yyyymmdd());
}
if (now != null) {
$("#posttitle").val(now.title);
$("#postpath").val(now.path);
$("#postdate").val(now.date);
$("#posttags").val(now.tags);
$("#loading").show();
repo.read("master", now.path, function(err, data) {
$("#loading").hide();
var content = data.match(contentpattern)[1];
var md = data.match(mdpattern)[1];
$("#editmd").val(md);
$("#edithtml").html(content);
});
}
});
}
}
});
var SimpleApp = Spine.Controller.sub({
el: $("body"),
init: function() {
this.logins = new Logins();
this.mains = new Mains();
this.posts = new Posts();
$("#postDelete").click(function(){return confirm("Are you sure you want to delete?");});
this.routes({
"": function() {this.logins.init();this.logins.active();},
"/main": function() {this.mains.init();this.mains.active();},
"/posts/:type/:num": function(param) {
var type = param.type;
var num = Math.floor(param.num);
var temp = this;
if (type.slice(0, 6) == "delete") {
$("#loading").show();
var posts = [];
if (type == "deletepost") {
var now = gconfig.posts[num];
for (var i = 0; i < gconfig.posts.length; ++i) {
if (i != num)
posts.push(gconfig.posts[i]);
}
gconfig.posts = posts;
}
if (type == "deletepage") {
var now = gconfig.pages[num];
for (var i = 0; i < gconfig.pages.length; ++i) {
if (i != num)
posts.push(gconfig.pages[i]);
}
gconfig.pages = posts;
}
repo.write("master", "main.json", JSON.stringify(gconfig), "remove", function(err) {
repo.delete("master", now.path, function(err) {
temp.posts.init(param);
temp.posts.active();
});
});
}
else {
temp.posts.init(param);
temp.posts.active();
}
},
"/posts/:type": function(param) {
var type = param.type;
var temp = this;
if (type.slice(0, 4) == "save") {
$("#loading").show();
if (type == "savepost") {
var template = "template/post.html";
var posts = gconfig.posts;
}
if (type == "savepage") {
var template = "template/page.html";
var posts = gconfig.pages;
}
var now = {"title": $("#posttitle").val(),
"date": $("#postdate").val(),
"tags": $("#posttags").val(),
"path": $("#postpath").val()};
var mark = null;
for (var i = 0; i < posts.length; ++i)
if (posts[i].path == now.path)
mark = i;
if (mark != null)
posts[mark] = now;
else
posts.unshift(now);
var content = $("#edithtml").html().replace(/\$/mg, "$$$$");
var md = $("#editmd").val().replace(/\$/mg, "$$$$");
$.ajax({
url: template,
type: "GET",
success: function(data) {
$("#saveerror").hide();
data = data.replace(contentpattern, "<!-- content -->\n"+content+"\n<!-- content end -->\n");
data = data.replace("//path//", now.path);
data = data.replace(mdpattern, "<!-- markdown -->\n"+md+"\n<!-- markdown end -->\n");
repo.write("master", now.path, data, "save", function(err) {
repo.write("master", "main.json", JSON.stringify(gconfig), "save", function(err) {
if (!errShow($("saveerror", err))) {
temp.posts.init(param);
temp.posts.active();
}
});
});
},
error: function(e) {err(e);}
});
}
else {
temp.posts.init(param);
temp.posts.active();
}
},
"/posts": function() {this.posts.init();this.posts.active();}
});
this.manager = new Spine.Manager(this.logins, this.mains, this.posts);
Spine.Route.setup();
}
});
new SimpleApp();
$("#editmd").on("keyup", function() {
mdupdate();
});
});