-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
167 lines (164 loc) · 5.41 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
/**
* Created by rongshenghu on 15-8-5.
*/
var storage = {
location: window.localStorage,
set: function (key, value) {
if (typeof(value) == 'object') {
value = JSON.stringify(value);
}
storage.location.setItem(key, value);
},
get_code: function (code) {
var codes = JSON.parse(storage.location.getItem('code'));
if (code) {
storage.set('max_order', 0);
return codes[code];
} else {
return codes ? codes : {};
}
},
get_order: function (order) {
var orders = JSON.parse(storage.location.getItem('order'));
if (order) {
return orders[order];
} else {
return orders ? orders : {};
}
},
get_max_order: function () {
var max = storage.location.getItem('max_order');
return max ? parseInt(max) : 0;
},
set_max_order: function (max_order) {
return storage.location.setItem('max_order', max_order);
},
set_code: function (code) {
var codes = storage.get_code();
var max_order = storage.get_max_order() + 1;
if (!codes[code]) {
codes[code] = max_order;
storage.set_max_order(max_order);
var orders = storage.get_order();
orders[max_order] = code;
storage.set('code', codes);
storage.set('order', orders);
}
console.log(storage.location);
},
remove_code: function (code) {
if (code) {
var codes = storage.get_code();
if (codes[code]) {
var orders = storage.get_order();
delete orders[codes[code]];
delete codes[code];
storage.set('code', codes);
storage.set('order', orders);
}
} else {
storage.location.clear();
}
console.log(storage.location);
}
};
var listen = {
init: function () {
$('#add_item').on('click', function () {
var value = $('#item').val().trim();
$('#item').val('');
panel.add(value);
});
$('#clear').on('click', function () {
panel.clear();
});
$("#item_list ul").on('click', '.top', function () {
var code = $(this).attr('data');
panel.del(code);
panel.add(code.substr(2, 6));
}).on('click', '.del', function () {
var value = $(this).attr('data');
panel.del(value);
});
$("#tip").on('click', function () {
$('div.tip').toggle();
});
$('#item').on('keyup', function (event) {
if (event.keyCode == "13") {
var value = $(this).val().trim();
$(this).val('');
panel.add(value);
}
});
}
};
var panel = {
init: function () {
var orders = storage.get_order();
if (orders) {
$.each(orders, function (key, value) {
panel.add_item(value);
});
}
},
add: function (value) {
finance.handle_code(value, function (code) {
storage.set_code(code);
panel.add_item(code);
});
},
del: function (value) {
storage.remove_code(value);
$("#item_" + value).remove();
},
clear: function () {
storage.remove_code();
$("li.item").remove();
},
add_item: function (value) {
// Place a placeholder
var li = "<li class='item' id='item_" + value + "'></li>";
var exist = $("#item_" + value);
if (exist.attr('id')) {
} else {
$("#last_item").after(li);
}
finance.handle_data(value, function (data) {
var percent = data[5];
var class_coloer = '';
if (percent > 0) {
class_coloer = 'red';
} else if (percent == 0) {
class_coloer = 'gray';
} else {
class_coloer = 'green';
}
var top = "<a href='#' class='top red' data='" + value + "'>Top</a> ";
var link = "<a href='" + finance.get_href_link(value) + "' target='_blank' class='data'>"
+ data[1].substr(0, 1)
+ value.substr(2, 6)
+ "</a>";
var span_value = "<span class='value' title='" + data[1] + "'>" + link + "</span> ";
var span_start = "<span class='start black'>" + parseFloat(data[3] - data[4]).toFixed(3) + "</span> ";
var span_now = "<span class='now " + class_coloer + "'>" + parseFloat(data[3]).toFixed(3) + "</span> ";
var span_percent = "<span class='percent " + class_coloer + "'>" + percent + "%</span> ";
var del = " <a class='del' data='" + value + "' href='#'>X</a>";
var li = "<li class='item' id='item_" + value + "'>" + top + span_value + span_start + span_now + span_percent + del + "</li>";
var exist = $("#item_" + value);
if (exist.attr('id')) {
exist.html(top + span_value + span_start + span_now + span_percent + del);
} else {
$("#last_item").after(li);
}
});
}
};
$(function () {
listen.init();
setTimeout(function () {
panel.init();
}, 50);
setInterval(function () {
panel.init();
}, finance.wait);
});