-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
126 lines (110 loc) · 3.6 KB
/
utils.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
window.utils = window.utils || {};
function byId(i){
return document.getElementById(i);
}
(function () {
var query, params = {};
if (query = location.href.replace(/#.+?/, '').split('?')[1]) {
query = query.split('&');
for (var k = 0; k < query.length; k++) {
var v = query[k], param = v.split('=');
if (param.length == 2)
params[param[0]] = param[1];
}
}
utils.request = params;
})();
Object.prototype.forEach = function (callback, thisObj) {
if (callback && typeof this.length != 'undefined') {
for (var i = 0; i < this.length; i++) {
if (callback.call(thisObj || this, this[i], this) === false)
break;
}
}
}
String.prototype.format = function (obj) {
var str = this;
for (var k in obj)
str = str.replace(new RegExp('\\{' + k + '\\}'), obj[k]);
return str;
}
String.prototype.formats = function (obj) {
var str = '';
for (var i=0;i < obj.length; i++)
str += this.format(obj[i]);
return str;
}
utils.httpGet = function(url, callback, error) {
var xmlRequest = new XMLHttpRequest();
xmlRequest.onreadystatechange = function (ev) {
if (xmlRequest.readyState == 4) {
if(xmlRequest.status == 303 || xmlRequest.status == 200){
var content = xmlRequest.responseText;
callback && callback(content);
}else{
error && error(xmlRequest.responseText);
}
}
};
xmlRequest.open('get', url, true);
xmlRequest.send();
}
utils.JsonToUrl = function(obj){
var str = '';
for(var k in obj){
if(typeof obj[k] != 'Object' && typeof obj[k] != 'function')
str += k + '=' + obj[k] + '&';
}
return str.replace(/&$/,'');
}
function scroll(setting){
setting = setting || {};
this.setting = setting;
var _this = this;
var defaultSetting = {
page : 1,
pageSize : 30,
templateId :'template',
containerId : 'container',
url : '',
onloadDataStart : null,
onloadDataEnd : null,
onsuccess : null
};
for(var k in defaultSetting){
this.setting[k] = setting[k] || defaultSetting[k];
}
_this.lock = false,_this.isLastPage = false,_this.page = 1,_this.pageSize = 30;
window.onscroll = function(event){
if(document.body.scrollHeight == window.screen.height + window.scrollY)
_this.onScrollToBottom();
}
}
scroll.prototype.onScrollToBottom = function(){
if(this.lock || this.isLastPage)
return;
this.lock = true;
this.loadData();
}
scroll.prototype.loadData = function(){
this.setting.onloadDataStart && this.setting.onloadDataStart(this.setting.page, this.setting.pageSize);
if(this.isLastPage)
return;
_this = this;
httpGet(this.setting.url, function(data){
data = JSON.parse(data);
data = (_this.setting.onloadDataEnd && _this.setting.onloadDataEnd(data)) || data;
if(!data || !data.length){
_this.isLastPage = true;
return;
}
var html = '',template = byId(_this.setting.templateId).innerHTML;
html = template.formats(data);
byId(_this.setting.containerId).appendHTML(html);
_this.setting.onsuccess && _this.setting.onsuccess(data);
_this.setting.page++;
_this.lock = false;
},function(errData){
_this.lock = false;
});
}