-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathutil.js
More file actions
151 lines (138 loc) · 3.37 KB
/
util.js
File metadata and controls
151 lines (138 loc) · 3.37 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
// Utilities
/* Ajax Operation */
/**
* Load a Javascript. This is an alternative of jQuery.getScript in native JavaScript
* @param url
* @param callback
*/
function getScript(url, callback)
{
var script = document.createElement('script');
var prior = document.getElementsByTagName('script')[0];
script.async = 1;
prior.parentNode.insertBefore(script, prior);
script.onload = script.onreadystatechange = function (_, isAbort)
{
if (isAbort || !script.readyState || /loaded|complete/.test(script.readyState))
{
script.onload = script.onreadystatechange = null;
script = undefined;
if (!isAbort)
{
if (callback) callback();
}
}
};
script.src = url;
}
/**
* Load a named JSONP feed.
* @param url
* @param funcName The function name hardcoded in jsonp feed
* @param callback
*/
function getJSONPFeed(url, funcName, callback)
{
if (window.__jsonFeedCache === undefined)
{
window.__jsonFeedCache = {};
}
if (window[funcName] === undefined)
{
window[funcName] = function (data)
{
__jsonFeedCache[funcName] = data;
};
}
getScript(url, function ()
{
callback(__jsonFeedCache[funcName]);
});
}
/**
* Load a javascript feed and the data is defined in the feed as a global object.
* @param url
* @param objName The objName hardcoded in JS feed
* @param callback
*/
function getJSFeed(url, objName, callback)
{
getScript(url, function ()
{
callback(window[objName]);
});
}
/**
* It can be used to get json API/Service in same domain, or different domain with CORS support.
* This is an alternative to jQuery.getJSON()
* @param url
* @param callback
* @param errCallback
*/
function getJSON(url, callback, errCallback)
{
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function ()
{
if (request.status >= 200 && request.status < 400)
{
// Success!
var data = JSON.parse(request.responseText);
if (callback) callback(data);
}
else
{
// We reached our target server, but it returned an error
}
};
request.onerror = function ()
{
// There was a connection error of some sort
if (errCallback) errCallback();
};
request.send();
}
/**
* Add timestamp to url to avoid being cached by browser.
* Note: param 'round' is not required to NeuLion CDN anymore because our CDN will ignore param.t
* Before we use 'round' to let the request URL to be the same one during a short time window,
* to avoid CDN requesting original server too frequently.
* @param url
* @param round
* @returns {string}
*/
function addTimestamp(url, round)
{
return url + (url.indexOf('?') > -1 ? '&' : '?') + 't=' + parseInt(new Date().getTime() / (round ? round * 1000 : 1));
}
/* Dom Operation */
function addClass(el, className)
{
if (el.classList)
el.classList.add(className);
else
el.className += ' ' + className;
}
function removeClass(el, className)
{
if (el.classList)
el.classList.remove(className);
else
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
/* Date functions */
/**
* Convert date object to date string in format: yyyy/MM/dd
* @param date
* @returns {string}
*/
function formatDate(date)
{
var year = date.getFullYear();
var day = date.getDate();
var month = date.getMonth() + 1;
day = day < 10 ? "0" + day : day;
month = month < 10 ? "0" + month : month;
return year + "/" + month + "/" + day;
}