-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
57 lines (53 loc) · 1.65 KB
/
main.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
$(document).ready(function(){
redraw();
$("ul").on('click','li', function(){
var id = $(this).attr('id');
$.get("http://mi.ecjtu.net/"+id+"/vote", function(result){
console.log(result,result['id'],result['vote'])
changeColor(result['id'], calcColor(result['vote']));
},'json');
});
$(".new button").click( function(){
var author = $("#college option:selected").text() + $("#grade option:selected").text();
var content = $("#content").val();
$.post("http://mi.ecjtu.net/new", {'author':author, 'content':content}, function(result){
redraw();
});
});
});
function changeColor(id, color){
console.log(id,color)
$('ul li[id='+id+']').css('background-color', color);
}
function redraw(){
$('ul').html('');
$.get("http://mi.ecjtu.net/list?count=30&start_from=0", function(result){
for(var i=0;i<result.length;i++){
var tmpl = [
' <li class="main" id="'+result[i]['id']+'"><a href="javascript:void(0)">',
' <div class="author">',
result[i]['author'],
' </div>',
' <div class="content">',
result[i]['content'],
' </div>',
' </a></li>'].join('\n');
$('ul').append(tmpl);
$('ul li:last').css('background-color', calcColor(result[i]['vote']));
console.log(calcColor(result[i]['vote']));
}
},'json');
}
function calcColor(vote){
var str = '#';
var r = parseInt(vote * (243-22)/20 + 22);
var g = parseInt(160);
var b = parseInt(133 - vote * (133-18)/20);
r = r > 254 ? 254 : r;
b = b < 16 ? 16 : b;
//10 to 16
str += r < 10 ? "0" + r.toString(16) : r.toString(16);
str += g < 10 ? "0" + g.toString(16) : g.toString(16);
str += b < 10 ? "0" + b.toString(16) : b.toString(16);
return str;
}