Repository navigation
Expand file tree
/
Copy patharticle.js
More file actions
686 lines (620 loc) · 23.3 KB
/
Copy patharticle.js
File metadata and controls
686 lines (620 loc) · 23.3 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* Copyright (c) 2010-2017, b3log.org & hacpai.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview article for admin
*
* @author <a href="http://vanessa.b3log.org">Liyuan Li</a>
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.4.5.7, May 21, 2017
*/
admin.article = {
currentEditorType: '',
// 当发文章,取消发布,更新文章时设置为 false。不需在离开编辑器时进行提示。
isConfirm: true,
status: {
id: undefined,
isArticle: undefined,
articleHadBeenPublished: undefined
},
content: "",
// 自动保存草稿定时器
autoSaveDraftTimer: "",
// 自动保存间隔
AUTOSAVETIME: 1000 * 60,
/**
* 初始化上传组建
*/
initUploadFile: function (id) {
var filename = "";
$('#' + id).fileupload({
multipart: true,
url: "https://up.qbox.me",
add: function (e, data) {
filename = data.files[0].name;
data.submit();
$('#' + id + ' span').text('uploading...');
},
formData: function (form) {
var data = form.serializeArray();
var ext = filename.substring(filename.lastIndexOf(".") + 1);
data.push({name: 'key', value: getUUID() + "." + ext});
data.push({name: 'token', value: qiniu.qiniuUploadToken});
return data;
},
done: function (e, data) {
$('#' + id + ' span').text('');
var qiniuKey = data.result.key;
if (!qiniuKey) {
alert("Upload error, please check Qiniu configurations");
return;
}
$('#' + id).after('<div>![' + data.files[0].name + '](http://'
+ qiniu.qiniuDomain + qiniuKey + ')</div>');
},
fail: function (e, data) {
$('#' + id + ' span').text("Upload error, please check Qiniu configurations [" + data.errorThrown + "]");
}
}).on('fileuploadprocessalways', function (e, data) {
var currentFile = data.files[data.index];
if (data.files.error && currentFile.error) {
alert(currentFile.error);
}
});
},
/**
* @description 获取文章并把值塞入发布文章页面
* @param {String} id 文章 id
* @param {Boolean} isArticle 文章或者草稿
*/
get: function (id, isArticle) {
this.status.id = id;
this.status.isArticle = isArticle;
admin.selectTab("article/article");
},
/**
* @description 获取文章内容
*/
getAndSet: function () {
$("#loadMsg").text(Label.loadingLabel);
$("#tipMsg").text("");
$.ajax({
url: latkeConfig.servePath + "/console/article/" + admin.article.status.id,
type: "GET",
cache: false,
success: function (result, textStatus) {
$("#tipMsg").text(result.msg);
if (!result.sc) {
$("#loadMsg").text("");
return;
}
// set default value for article.
$("#title").val(result.article.articleTitle);
admin.article.status.articleHadBeenPublished = result.article.articleHadBeenPublished;
if (admin.article.currentEditorType !== result.article.articleEditorType) {
admin.editors.articleEditor.remove();
admin.editors.abstractEditor.remove();
admin.article.currentEditorType = result.article.articleEditorType;
admin.editors.articleEditor.init(result.article.articleEditorType);
admin.editors.abstractEditor.init(result.article.articleEditorType);
}
admin.editors.articleEditor.setContent(result.article.articleContent);
admin.editors.abstractEditor.setContent(result.article.articleAbstract);
admin.article.content = admin.editors.articleEditor.getContent();
var tags = result.article.articleTags,
tagsString = '';
for (var i = 0; i < tags.length; i++) {
if (0 === i) {
tagsString = tags[i].tagTitle;
} else {
tagsString += "," + tags[i].tagTitle;
}
}
$("#tag").val(tagsString);
$("#permalink").val(result.article.articlePermalink);
$("#viewPwd").val(result.article.articleViewPwd);
$("#articleCommentable").prop("checked", result.article.articleCommentable);
// signs
var signs = result.article.signs;
$(".signs button").each(function (i) {
if (parseInt(result.article.articleSignId) === parseInt(signs[i].oId)) {
$("#articleSign" + signs[i].oId).addClass("selected");
} else {
$("#articleSign" + signs[i].oId).removeClass("selected");
}
});
admin.article.setStatus();
$("#loadMsg").text("");
}
});
},
/**
* @description 删除文章
* @param {String} id 文章 id
* @param {String} fromId 文章来自草稿夹(draft)/文件夹(article)
* @param {String} title 文章标题
*/
del: function (id, fromId, title) {
var isDelete = confirm(Label.confirmRemoveLabel + Label.articleLabel + '"' + title + '"?');
if (isDelete) {
$("#loadMsg").text(Label.loadingLabel);
$("#tipMsg").text("");
$.ajax({
url: latkeConfig.servePath + "/console/article/" + id,
type: "DELETE",
cache: false,
success: function (result, textStatus) {
$("#tipMsg").text(result.msg);
if (!result.sc) {
$("#loadMsg").text("");
return;
}
admin[fromId + "List"].getList(1);
}
});
}
},
/**
* @@description 添加文章
* @param {Boolean} articleIsPublished 文章是否发布过
* @param {Boolean} isAuto 是否为自动保存
*/
add: function (articleIsPublished, isAuto) {
if (admin.article.validate()) {
var that = this;
that._addDisabled();
$("#loadMsg").text(Label.loadingLabel);
$("#tipMsg").text("");
var signId = "";
$(".signs button").each(function () {
if (this.className === "selected") {
signId = this.id.substr(this.id.length - 1, 1);
}
});
var articleContent = admin.editors.articleEditor.getContent(),
articleAbstract = admin.editors.abstractEditor.getContent();
var requestJSONObject = {
"article": {
"articleTitle": $("#title").val(),
"articleContent": articleContent,
"articleAbstract": articleAbstract,
"articleTags": this.trimUniqueArray($("#tag").val()).toString(),
"articlePermalink": $("#permalink").val(),
"articleIsPublished": articleIsPublished,
"articleSignId": signId,
"postToCommunity": $("#postToCommunity").prop("checked"),
"articleCommentable": $("#articleCommentable").prop("checked"),
"articleViewPwd": $("#viewPwd").val()
}
};
$.ajax({
url: latkeConfig.servePath + "/console/article/",
type: "POST",
cache: false,
data: JSON.stringify(requestJSONObject),
success: function (result, textStatus) {
if (isAuto) {
$("#tipMsg").text(Label.autoSaveLabel);
admin.article.status.id = result.oId;
return;
}
$("#tipMsg").text(result.msg);
if (!result.sc) {
return;
}
if (articleIsPublished) {
admin.article.status.id = undefined;
admin.selectTab("article/article-list");
} else {
admin.selectTab("article/draft-list");
}
admin.article.isConfirm = false;
},
complete: function (jqXHR, textStatus) {
that._removeDisabled();
$("#loadMsg").text("");
if (jqXHR.status === 403) {
$.get("/admin-index.do");
that.add(articleIsPublished);
}
}
});
}
},
/**
* @description 更新文章
* @param {Boolean} articleIsPublished 文章是否发布过
* @param {Boolean} isAuto 是否为自动保存
*/
update: function (articleIsPublished, isAuto) {
if (admin.article.validate()) {
var that = this;
that._addDisabled();
$("#loadMsg").text(Label.loadingLabel);
$("#tipMsg").text("");
var signId = "";
$(".signs button").each(function () {
if (this.className === "selected") {
signId = this.id.substr(this.id.length - 1, 1);
}
});
var articleContent = admin.editors.articleEditor.getContent(),
articleAbstract = admin.editors.abstractEditor.getContent();
var requestJSONObject = {
"article": {
"oId": this.status.id,
"articleTitle": $("#title").val(),
"articleContent": articleContent,
"articleAbstract": articleAbstract,
"articleTags": this.trimUniqueArray($("#tag").val()).toString(),
"articlePermalink": $("#permalink").val(),
"articleIsPublished": articleIsPublished,
"articleSignId": signId,
"articleCommentable": $("#articleCommentable").prop("checked"),
"articleViewPwd": $("#viewPwd").val(),
"postToCommunity": $("#postToCommunity").prop("checked"),
"articleEditorType": admin.article.currentEditorType
}
};
$.ajax({
url: latkeConfig.servePath + "/console/article/",
type: "PUT",
cache: false,
data: JSON.stringify(requestJSONObject),
success: function (result, textStatus) {
if (isAuto) {
$("#tipMsg").text(Label.autoSaveLabel);
return;
}
$("#tipMsg").text(result.msg);
if (!result.sc) {
return;
}
if (articleIsPublished) {
admin.selectTab("article/article-list");
} else {
admin.selectTab("article/draft-list");
}
$("#tipMsg").text(Label.updateSuccLabel);
admin.article.status.id = undefined;
admin.article.isConfirm = false;
},
complete: function (jqXHR, textStatus) {
that._removeDisabled();
$("#loadMsg").text("");
if (jqXHR.status === 403) {
$.get("/admin-index.do");
that.update(articleIsPublished);
}
}
});
}
},
/**
* @description 发布文章页面设置文章按钮、发布到社区等状态的显示
*/
setStatus: function () {
$.ajax({// Gets all tags
url: latkeConfig.servePath + "/console/tags",
type: "GET",
cache: false,
success: function (result, textStatus) {
$("#tipMsg").text(result.msg);
if (!result.sc) {
$("#loadMsg").text("");
return;
}
if (0 >= result.tags.length) {
return;
}
$("#tagCheckboxPanel>span").remove("");
var spans = "";
for (var i = 0; i < result.tags.length; i++) {
spans += "<span>" + result.tags[i].tagTitle + "</span>";
}
$("#tagCheckboxPanel").html(spans + '<div class="clear"></div>');
$("#loadMsg").text("");
}
});
// set button status
if (this.status) {
if (this.status.isArticle) {
$("#unSubmitArticle").show();
$("#saveArticle").hide();
$("#submitArticle").show();
} else {
$("#submitArticle").show();
$("#unSubmitArticle").hide();
$("#saveArticle").show();
}
if (this.status.articleHadBeenPublished) {
$("#postToCommunityPanel").hide();
} else {
// 1.0.0 开始默认会发布到社区
// $("#postToCommunityPanel").show();
}
} else {
$("#submitArticle").show();
$("#unSubmitArticle").hide();
$("#saveArticle").show();
// 1.0.0 开始默认会发布到社区
// $("#postToCommunityPanel").show();
}
$("#postToCommunity").attr("checked", "checked");
},
/**
* @description 清除发布文章页面的输入框的内容
*/
clear: function () {
this.status = {
id: undefined,
isArticle: undefined,
articleHadBeenPublished: undefined
};
this.setStatus();
$("#title").val("");
admin.editors.articleEditor.setContent("");
admin.editors.abstractEditor.setContent("");
// reset tag
$("#tag").val("");
$("#tagCheckboxPanel").hide().find("span").removeClass("selected");
$("#permalink").val("");
$("#articleCammentable").prop("checked", true);
$("#postToCommunity").prop("checked", true);
$(".signs button").each(function (i) {
if (i === 0) {
this.className = "selected";
} else {
this.className = "";
}
});
$(".editor-preview-active").html("").removeClass('editor-preview-active');
$("#uploadContent").remove();
},
/**
* @description 初始化发布文章页面
* @param {Function} fun 切面函数
*/
init: function (fun) {
this.currentEditorType = Label.editorType;
// Inits Signs.
$(".signs button").click(function (i) {
$(".signs button").removeClass('selected');
$(this).addClass('selected');
});
// For tag auto-completion
$.ajax({// Gets all tags
url: latkeConfig.servePath + "/console/tags",
type: "GET",
cache: false,
success: function (result, textStatus) {
$("#tipMsg").text(result.msg);
if (!result.sc) {
$("#loadMsg").text("");
return;
}
if (0 >= result.tags.length) {
return;
}
var tags = [];
for (var i = 0; i < result.tags.length; i++) {
tags.push(result.tags[i].tagTitle);
}
$("#tag").completed({
height: 160,
buttonText: Label.selectLabel,
data: tags
}).width($("#tag").parent().width() - 68);
$("#loadMsg").text("");
}
});
// submit action
$("#submitArticle").click(function () {
if (admin.article.status.id) {
admin.article.update(true);
} else {
admin.article.add(true);
}
}
);
$("#saveArticle").click(function () {
if (admin.article.status.id) {
admin.article.update(admin.article.status.isArticle);
} else {
admin.article.add(false);
}
});
this.initUploadFile('articleUpload');
// editor
admin.editors.articleEditor = new SoloEditor({
id: "articleContent",
kind: "all",
fun: fun,
height: 500
});
admin.editors.abstractEditor = new SoloEditor({
id: "abstract",
kind: "simple",
height: 200
});
admin.article.clearDraftTimer();
admin.article.autoSaveDraftTimer = setInterval(function () {
admin.article._autoSaveToDraft();
}, admin.article.AUTOSAVETIME);
},
/**
* @description 自动保存草稿件
*/
_autoSaveToDraft: function () {
if ($("#title").val().replace(/\s/g, "") === "" ||
admin.editors.articleEditor.getContent().replace(/\s/g, "") === "" ||
$("#tag").val().replace(/\s/g, "") === "") {
return;
}
if (admin.article.status.id) {
if (!admin.article.status.isArticle) {
admin.article.update(false, true);
}
} else {
admin.article.add(false, true);
admin.article.status.isArticle = false;
}
},
/**
* @description 关闭定时器
*/
clearDraftTimer: function () {
if (admin.article.autoSaveDraftTimer !== "") {
window.clearInterval(admin.article.autoSaveDraftTimer);
admin.article.autoSaveDraftTimer = "";
}
},
/**
* @description 验证发布文章字段的合法性
*/
validate: function () {
var articleContent = admin.editors.articleEditor.getContent();
if ($("#title").val().replace(/\s/g, "") === "") {
$("#tipMsg").text(Label.titleEmptyLabel);
$("#title").focus().val("");
} else if (articleContent.replace(/\s/g, "") === "") {
$("#tipMsg").text(Label.contentEmptyLabel);
} else if ($("#tag").val().replace(/\s/g, "") === "") {
$("#tipMsg").text(Label.tagsEmptyLabel);
$("#tag").focus().val("");
} else {
return true;
}
return false;
},
/**
* @description 取消发布
* @param {Boolean} isAuto 是否为自动保存
*/
unPublish: function (isAuto) {
var that = this;
that._addDisabled();
$.ajax({
url: latkeConfig.servePath + "/console/article/unpublish/" + admin.article.status.id,
type: "PUT",
cache: false,
success: function (result, textStatus) {
if (isAuto) {
$("#tipMsg").text(Label.autoSaveLabel);
return;
}
$("#tipMsg").text(result.msg);
if (!result.sc) {
return;
}
admin.selectTab("article/draft-list");
admin.article.status.id = undefined;
admin.article.isConfirm = false;
},
complete: function (jqXHR, textStatus) {
that._removeDisabled();
$("#loadMsg").text("");
if (jqXHR.status === 403) {
$.get("/admin-index.do");
that.unPublish();
}
}
});
},
/**
* @description 数组中无重复
* @param {String} str 被解析的字符串
* @returns {String} 无重复的字符串
*/
trimUniqueArray: function (str) {
str = str.toString();
var arr = str.split(",");
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].replace(/(^\s*)|(\s*$)/g, "");
if (arr[i] === "") {
arr.splice(i, 1);
i--;
}
}
var unique = $.unique(arr);
return unique.toString();
},
/**
* @description 点击发文文章时的处理
*/
prePost: function () {
$("#loadMsg").text(Label.loadingLabel);
admin.article.content = "";
if (!admin.editors.articleEditor.getContent) {
return;
}
var articleContent = admin.editors.articleEditor.getContent();
if (window.location.hash === "#article/article" &&
articleContent.replace(/\s/g, '') !== "") {
if (confirm(Label.editorPostLabel)) {
admin.article.clear();
}
}
$("#tipMsg").text("");
$("#loadMsg").text("");
if (admin.article.currentEditorType !== Label.editorType) {
admin.editors.articleEditor.remove();
admin.editors.abstractEditor.remove();
admin.article.currentEditorType = Label.editorType;
admin.editors.articleEditor.init(Label.editorType);
admin.editors.abstractEditor.init(Label.editorType);
}
},
/**
* @description: 仿重复提交,点击一次后,按钮设置为 disabled
*/
_addDisabled: function () {
$("#unSubmitArticle").attr("disabled", "disabled");
$("#saveArticle").attr("disabled", "disabled");
$("#submitArticle").attr("disabled", "disabled");
},
/**
* @description: 仿重复提交,当后台有数据返回后,按钮移除 disabled 状态
*/
_removeDisabled: function () {
$("#unSubmitArticle").removeAttr("disabled");
$("#saveArticle").removeAttr("disabled");
$("#submitArticle").removeAttr("disabled");
}
};
/**
* @description 注册到 admin 进行管理
*/
admin.register.article = {
"obj": admin.article,
"init": admin.article.init,
"refresh": function () {
admin.editors.abstractEditor.setContent('');
admin.editors.articleEditor.setContent('');
$("#loadMsg").text("");
$("#tipMsg").text("");
}
};
function getUUID() {
var d = new Date().getTime();
var ret = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
ret = ret.replace(new RegExp("-", 'g'), "");
return ret;
}
;