-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy.html.js
96 lines (90 loc) · 3.43 KB
/
my.html.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
var auth = authorize_and_redirect();
var user_id = Cookies.get("user_id");
var max_payload_length = 75;
function create_record_div(rec) {
var div = $(format('<div id="record" class="well well-sm" record-id="{0}"></div>', rec.id));
var header = $(format('<h5 class="record-header"></h5>'));
div.append(header);
header.append($(format("<span>{0}</span>", rec.tm_payload_str)));
header.append(document.createTextNode(" "));
if (rec.state=="new") {
header.append($(format("<span>{0}</span>", "待审核")));
header.append(document.createTextNode(" "));
header.append($(format('<a href="/donate.html?id={0}&r=/my.html">捐赠</a>', rec.id)));
header.append(document.createTextNode(" "));
header.append($(format('<a id="btn-del" href="javascript:void(0);">删除</a>')));
}
else if (rec.state=="confirmed") {
header.append($(format("<span>{0}</span>", "已审核")));
}
else if (rec.state=="rejected") {
header.append($(format("<span>{0}</span>", "已拒绝")));
}
else if (rec.state=="uploaded") {
header.append($(format("<span>{0}</span>", "已发布")));
header.append(document.createTextNode(" "));
header.append($(format('<a href="/post.html?id={0}">证书</a>', rec.id)));
}
div.append($(format('<p class="record-main-text">{0}</p>', rec.payload)));
return div;
}
function load_all_record() {
$.ajax({
beforeSend: function(xhr){xhr.setRequestHeader("X-Auth", auth);},
type: "POST",
url: API_URL+"GetUserRecords",
dataType: "json",
error: api_error,
success: function(d) { if (api_success(d)) {
for (var i=0; i<d.result.length; i++) {
$("#records").append(create_record_div(d.result[i]));
}
}}
});
}
$(function(){
$("#user_id").html(user_id);
load_all_record();
var txt_payload = $("#txt-payload");
txt_payload.keyup(function(e) {
var maxchars = max_payload_length;
var byteslength = get_byte_len($(this).val());
$("#payload-len").text(maxchars-byteslength);
});
$("#btn-send").click(function(event) {
event.preventDefault();
var payload = txt_payload.val();
var byteslength = get_byte_len(payload);
if (payload.length <= 0 || byteslength > max_payload_length) {
show_msg("信息太长无法发布");
return;
}
txt_payload.focus();
$.ajax({
beforeSend: function(xhr){xhr.setRequestHeader("X-Auth", auth);},
type: "POST",
url: API_URL+"CreateRecord",
data: { payload:payload },
dataType: "json",
error: api_error,
success: function(d) { if (api_success(d)) {
$("#records").prepend(create_record_div(d.result));
txt_payload.val("");
}}
});
});
$("#records").on("click", "#btn-del", function(event) {
event.preventDefault();
var record = $(this).parent().parent();
var record_id = record.attr("record-id");
$.ajax({
beforeSend: function(xhr){xhr.setRequestHeader("X-Auth", auth);},
type: "POST",
url: API_URL+"RemoveRecord",
data: { rec_id:record_id },
dataType: "json",
error: api_error,
success: function(d) { if (api_success(d)) record.remove(); }
});
});
});