utils.js
13.2 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
;var RCS = {
templateCache:{}
};
(function(RCS){
/*
var tpl = '<a>{{this.key1}} <p>-</p> {{this.value1.c}}</a>';
var data = {"key1":1,"value1":{"c":"cccccc"}};
var html = render(tpl,data);
*/
function render(template, data) {
template = template || "";
data = data || [""];
var re = /{%((?:(?!%}).)+)%}/g, reExp = /(^( )?(var|if|for|else|switch|case|default|break|{|}))(.*)?/g, code = 'var r=[];\n', cursor = 0;
var add = function(line, js) {
js? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
(code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
return add;
}
var match;
while(match = re.exec(template)) {
add(template.slice(cursor, match.index))(match[1], true);
cursor = match.index + match[0].length;
}
add(template.substr(cursor, template.length - cursor));
code += 'return r.join("");';
data = isNaN(data.length) ? [data] : data;
var html = "";
for(var i = 0, length = data.length; i < length; i++) {
html += new Function(code.replace(/[\r\t\n]/g, '')).apply(data[i]);
}
return html;
}
var utils = {
$ : function(selector){
return document.querySelectorAll(selector);
},
show : function(node){
node.style.display = "block";
},
hide : function(node){
node.style.display = "none";
},
removeNode : function(selector){
var thisNode = utils.$(selector)[0];
if (thisNode) {
thisNode.parentNode.removeChild(thisNode);
}
},
getStyle : function (node, prop) {
if(node.currentStyle) {
return node.currentStyle[prop] || '';
}
else if(window.getComputedStyle) {
return window.getComputedStyle(node , null)[prop];
}
},
indexOf : function (array, item) {
if (array.indexOf){
return array.indexOf(item);
}
for (var i = 0, len = array.length; i < len; i++){
if (array[i] === item){
return i;
}
}
return -1;
},
copy : function(json1, json2, flag, fn){
fn = fn || function(e){return e;}
for (var key in json2){
if (flag || typeof json1[key] === 'undefined' || json1[key] === null){
json1[key] = fn(json2[key]);
}
}
return json1;
},
isChild : function(node,nodeParent){
while (node && node.tagName && node.tagName.toLowerCase() != "body"){
if (node == nodeParent){
return true;
}
node = node.parentNode;
}
return false;
},
getTime : function(time){
var nowDate = new Date();
var sendDate = new Date(time);
var dateStr = '';
if (nowDate.getFullYear() == sendDate.getFullYear() && nowDate.getMonth() == sendDate.getMonth() && nowDate.getDate() == sendDate.getDate()) {
dateStr = (sendDate.getHours()>9 ? sendDate.getHours():'0'+sendDate.getHours())+':'+(sendDate.getMinutes()>9 ? sendDate.getMinutes():'0'+sendDate.getMinutes());
} else {
dateStr = sendDate.getFullYear()+'-'+(sendDate.getMonth()>8 ? (sendDate.getMonth()+1):'0'+(sendDate.getMonth()+1))+'-'+(sendDate.getDate()>9 ? sendDate.getDate():'0'+sendDate.getDate())+' '+(sendDate.getHours()>9 ? sendDate.getHours():'0'+sendDate.getHours())+':'+(sendDate.getMinutes()>9 ? sendDate.getMinutes():'0'+sendDate.getMinutes());
}
return dateStr;
},
getFileSize : function(size){
var g = Math.pow(1024, 3);
var m = Math.pow(1024, 2);
var k = Math.pow(1024, 1);
if (size > g) {
size = (size / g).toFixed(2) + 'G';
} else if (size > m) {
size = (size / m).toFixed(2) + 'M';
} else if (size > k) {
size = (size / k).toFixed(2) + 'K';
} else {
size = size + 'B';
}
return size;
},
getFormValue : function(formArray){
var formData = {};
for (var i = 0; i < formArray.length; i++) {
formData[formArray[i]] = document.getElementsByName(formArray[i])[0].value;
}
return formData;
},
fadein : function(ele) {
ele.style.opacity = 0;
ele.style.display = "block";
if (ele) {
var v = 0;
var timer = null;
timer = setInterval(function() {
v += 1;
setOpacity(ele, v);
if (v == 100) {
clearInterval(timer);
}
}, 1);
}
},
fadeout: function(ele) {
if (ele) {
var v = 100;
var timer = null;
timer = setInterval(function() {
v -= 1;
setOpacity(ele, v);
if (v == 0) {
ele.style.display = "none";
clearInterval(timer);
}
}, 1);
}
},
downloadHistoryMsgFile: function(url,name){
var a = document.createElement('a');
a.href = url;
a.setAttribute('download', name || '');
a.click();
},
encodeHtmlStr: function(str) {
var replaceRule = [
{
symbol: '&',
html: '&'
},
//下述方法有问题,字符串中如有空格,会多加空格
//white-space: pre-wrap; 能实现同样效果,并支持ie9, 故注释掉
// {
// symbol: '[\\u0020]',
// html: ' \u0020'
// },
{
symbol: '[\\u0009]',
html: ' \u0020'
},
{
symbol: '<',
html: '<'
},
{
symbol: '>',
html: '>'
},
{
symbol: '"',
html: '"'
},
{
symbol: '\'',
html: '''
},
{
symbol: '\\n\\r',
html: '<br/>'
},
{
symbol: '\\r\\n',
html: '<br/>'
},
{
symbol: '\\n',
html: '<br/>'
}
];
for (var i = 0, len = replaceRule.length; i < len; i++) {
var rule = replaceRule[i];
var regExp = new RegExp(rule.symbol, 'g');
str = str.replace(regExp, rule.html);
}
return str;
},
replaceUri: function(str, callback) {
var result = '';
var protocol = '((?:http|https|ftp)\\:\\/\\/)?';
var ip = '(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])';
var host = '(?!@)(?:[a-z0-9-]{1,36}\\.)+[a-z]{2,6}';
var port = '(?:\\:[0-9]{1,5})?';
var path = '(?:[a-zA-Z0-9.,;?\\\'+&%$#=~_\\-!()*\\/]*)';
var uriReg = new RegExp(protocol + '(?:(?:' + ip + ')|(?:' + host +'))' + port + path, 'ig');
result = str.replace(uriReg, function(uriStr, prot) {
var lastIndex = arguments[arguments.length - 2];
var prevChar = str.substr(lastIndex - 1, 1);
var isEmail = prevChar === '@';
var notDomain = !chkDomain(uriStr, prot);
if (isEmail || notDomain) {
return uriStr;
}
return callback.apply(null, arguments);
});
return result;
},
replaceEmail: function(str, callback) {
var result = '';
var emailReg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/gi;
result = str.replace(emailReg, callback);
return result;
},
browserRedirect: function(callback) {
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
callback("phone");
} else {
callback("pc");
}
},
cloneObj: function(obj){
var str, newobj = obj.constructor === Array ? [] : {};
if(typeof obj !== 'object'){
return;
} else if(window.JSON){
str = JSON.stringify(obj), //系列化对象
newobj = JSON.parse(str); //还原
} else {
for(var i in obj){
newobj[i] = typeof obj[i] === 'object' ?
cloneObj(obj[i]) : obj[i];
}
}
return newobj;
}
};
var setOpacity = function(ele, opacity) {
if (ele.style.opacity != undefined) {
///兼容FF和GG和新版本IE
ele.style.opacity = opacity / 100;
} else {
///兼容老版本ie
ele.style.filter = "alpha(opacity=" + opacity + ")";
}
}
var domainArray = [
'.com', '.net', '.org', '.biz', '.coop', '.info', '.museum', '.name',
'.pro', '.edu', '.gov', '.int', '.mil', '.ac', '.ad', '.ae', '.af', '.ag',
'.ai', '.al', '.am', '.an', '.ao', '.aq', '.ar', '.as', '.at', '.au', '.aw',
'.az', '.ba', '.bb', '.bd', '.be', '.bf', '.bg', '.bh', '.bi', '.bj', '.bm',
'.bn', '.bo', '.br', '.bs', '.bt', '.bv', '.bw', '.by', '.bz', '.ca', '.cc',
'.cd', '.cf', '.cg', '.ch', '.ci', '.ck', '.cl', '.cm', '.cn', '.co', '.cr',
'.cu', '.cv', '.cx', '.cy', '.cz', '.de', '.dj', '.dk', '.dm', '.do', '.dz',
'.ec', '.ee', '.eg', '.eh', '.er', '.es', '.et', '.fi', '.fj', '.fk', '.fm',
'.fo', '.fr', '.ga', '.gd', '.ge', '.gf', '.gg', '.gh', '.gi', '.gl', '.gm',
'.gn', '.gp', '.gq', '.gr', '.gs', '.gt', '.gu', '.gv', '.gy', '.hk', '.hm',
'.hn', '.hr', '.ht', '.hu', '.id', '.ie', '.il', '.im', '.in', '.io', '.iq',
'.ir', '.is', '.it', '.je', '.jm', '.jo', '.jp', '.ke', '.kg', '.kh', '.ki',
'.km', '.kn', '.kp', '.kr', '.kw', '.ky', '.kz', '.la', '.lb', '.lc', '.li',
'.lk', '.lr', '.ls', '.lt', '.lu', '.lv', '.ly', '.ma', '.mc', '.md', '.me',
'.mh', '.mk', '.ml', '.mm', '.mn', '.mo', '.mp', '.mq', '.mr', '.ms', '.mt',
'.mu', '.mv', '.mw', '.mx', '.my', '.mz', '.na', '.nc', '.ne', '.nf', '.ng',
'.ni', '.nl', '.no', '.np', '.nr', '.nu', '.nz', '.om', '.pa', '.pe', '.pf',
'.pg', '.ph', '.pk', '.pl', '.pm', '.pn', '.pr', '.ps', '.pt', '.pw', '.py',
'.qa', '.re', '.ro', '.rw', '.ru', '.sa', '.sb', '.sc', '.sd', '.se', '.sg',
'.sh', '.si', '.sj', '.sk', '.sl', '.sm', '.sn', '.so', '.sr', '.st', '.sv',
'.sy', '.sz', '.tc', '.td', '.tf', '.tg', '.th', '.tj', '.tk', '.tm', '.tn',
'.to', '.tp', '.tr', '.tt', '.tv', '.tw', '.tz', '.ua', '.ug', '.uk', '.um',
'.us', '.uy', '.uz', '.va', '.vc', '.ve', '.vg', '.vi', '.vn', '.vu', '.ws',
'.wf', '.ye', '.yt', '.yu', '.za', '.zm', '.zw', '.mg'];
var getLocation = function(href) {
var location = document.createElement('a');
location.href = href;
return location;
};
function ValidateIPaddress(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
return true;
}
return false;
}
var chkDomain = function(str, protocol) {
var link = str;
if(!protocol){
link = 'http://' + link;
}
var location = getLocation(link);
if(ValidateIPaddress(location.hostname)){
return true;
}
var domain = location.hostname.replace(/^.+\./, '');
if(domainArray.indexOf('.' + domain) < 0){
return false;
}
return true;
};
utils.render = render;
RCS.utils = utils;
})(RCS);