-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.foo.js
More file actions
410 lines (364 loc) · 12.9 KB
/
lib.foo.js
File metadata and controls
410 lines (364 loc) · 12.9 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* 随机数
* @param {[type]} under 下限 必填
* @param {[type]} over 上限
* @return {[type]} 返回结果
* ①.如果over为空,返回(0-under)区间随机数
* ②.如果over && under均不为空,返回(over-under)区间随机数
*/
function randomNum(under, over) {
switch(arguments.length){
case 1: return parseInt(Math.random() * under + 1);
case 2: return parseInt(Math.random() * (over - under + 1) + under);
default: return 0;
}
}
/**
* 动态修改微信页面title
* @param {[type]} argument title
* @return {[type]} [description]
*/
function changeDocumentTitle (argument) {
var $body = $('body');
document.title = argument;
// hack在微信等webview中无法修改document.title的情况
var $iframe = $('<iframe style="display:none;" src="/favicon.ico"></iframe>').on('load', function() {
setTimeout(function() {
$iframe.off('load').remove()
}, 0)
}).appendTo($body);
}
// 将当前时间换成时间格式字符串
var timestamp3 = 1403058804;
var newDate = new Date();
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
// 获取referrer
function getReferrer() {
var referrer = '';
try {
referrer = window.top.document.referrer;
} catch(e) {
if(window.parent) {
try {
referrer = window.parent.document.referrer;
} catch(e2) {
referrer = '';
}
}
}
if(referrer === '') {
referrer = document.referrer;
}
return referrer;
};
/* =========== 其他 =========== */
// Popup
function popup(event) {
// Prevent the link opening
if(event.target.nodeName.toLowerCase() == "a") {
if(event.preventDefault) {
event.preventDefault();
}
else {
event.returnValue = false;
}
}
var link = event.target,
url = link.href,
width = link.getAttribute("data-window-width") || 600,
height = link.getAttribute("data-window-height") || 600,
name = link.getAttribute("data-window-name") || "popup";
// If window exists, just focus it
if(window["window-"+name] && !window["window-"+name].closed) {
window["window-"+name].focus();
}
else {
// Get position
var left = window.screenLeft !== undefined ? window.screenLeft : screen.left;
var top = window.screenTop !== undefined ? window.screenTop : screen.top;
// Open in the centre of the screen
var x = (screen.width / 2) - (width / 2) + left,
y = (screen.height / 2) - (height / 2) + top;
// Open that window
window["window-"+name] = window.open(url, name, "top=" + y +",left="+ x +",width=" + width + ",height=" + height);
// Focus new window
window["window-"+name].focus();
}
}
// Trigger popups
document.querySelector(".js-popup").addEventListener("click", popup);
// Get JSONP
function getJSONP(url, callback) {
var name = "jsonp_callback_" + Math.round(100000 * Math.random());
// Cleanup to prevent memory leaks and hit original callback
window[name] = function(data) {
delete window[name];
document.body.removeChild(script);
callback(data);
};
// Create a faux script
var script = document.createElement("script");
script.setAttribute("src", url + (url.indexOf("?") >= 0 ? "&" : "?") + "callback=" + name);
// Inject to the body
document.body.appendChild(script);
}
// Get star count
var storageSupported = ("sessionStorage" in window),
selectors = {
github: ".js-stargazers-count",
twitter: ".js-tweet-count"
};
// Display the count next to the button
function displayCount(selector, count) {
document.querySelector(selector).innerHTML = count;
}
// Add star
function formatGitHubCount(count) {
return "★ " + count;
}
// Check if it's in session storage first
if (storageSupported && "github_stargazers" in window.sessionStorage) {
displayCount(selectors.github, formatGitHubCount(window.sessionStorage.github_stargazers));
} else {
getJSONP("https://api.github.com/repos/selz/plyr?access_token=a46ac653210ba6a6be44260c29c333470c3fbbf5", function(json) {
if (json && typeof json.data.stargazers_count !== "undefined") {
// Update UI
displayCount(selectors.github, formatGitHubCount(json.data.stargazers_count));
// Store in session storage
window.sessionStorage.github_stargazers = json.data.stargazers_count;
}
});
}
// Get tweet count
if (storageSupported && "tweets" in window.sessionStorage) {
displayCount(selectors.twitter, window.sessionStorage.tweets);
} else {
getJSONP("https://cdn.api.twitter.com/1/urls/count.json?url=plyr.io", function(json) {
if (json && typeof json.count !== "undefined") {
// Update UI
displayCount(selectors.twitter, json.count);
// Store in session storage
window.sessionStorage.tweets = json.count;
}
});
}
// js从数组中删除指定值
Array.prototype.indexOf = function(val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
Array.prototype.remove = function(val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
var _arr = ['a', 'b', 'c'];
_arr.remove('b'); // ['a', 'c']
/*
* 删除数组中的某一个对象
* _arr:数组
* _obj:需删除的对象
*/
function removeAaary(_arr, _obj) {
var length = _arr.length;
for (var i = 0; i < length; i++) {
if (_arr[i] == _obj) {
if (i == 0) {
_arr.shift(); //删除并返回数组的第一个元素
return _arr;
}
else if (i == length - 1) {
_arr.pop(); //删除并返回数组的最后一个元素
return _arr;
}
else {
_arr.splice(i, 1); //删除下标为i的元素
return _arr;
}
}
}
}
//获取浏览器参数 函数
//作用:抓取浏览器URL传递的参数 name为参数名称
//调用实例:http://www.uis.cc/index.html?page=1&active=158 抓取 page = getQueryString('page');
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r !== null) {
return unescape(r[2]);
}
return null;
}
/**
* 组合字符串 zu-he-zi-fu-chuan
* @param {[type]} string zu-he-zi-fu-chuan
* @return {[type]} zuHeZiFuChuan
*/
function getCombo (string) {
var arr = string.split("-");
var len = arr.length;
for (var i = 1; i < len; i++) {
arr[i] = arr[i].charAt(0).toUpperCase()+arr[i].substr(1, arr[i].length-1)
}
string = arr.join("");
return string;
}
getCombo("zu-he-zi-fu-chuan");
/**
* 动态修改微信页面title
*/
function changeDocumentTitle (argument) {
var $body = $('body');
document.title = argument;
// hack在微信等webview中无法修改document.title的情况
var $iframe = $('<iframe style="display:none;" src="/favicon.ico"></iframe>').on('load', function() {
setTimeout(function() {
$iframe.off('load').remove()
}, 0)
}).appendTo($body);
}
changeDocumentTitle("webclown.net");
// 计算两个日期相差有多少天
Math.abs(new Date('2017-02-01').getTime() - new Date('2017-07-31').getTime())/(1000 * 60 * 60 * 24);
// 返回指定日期是当年中的第几周
var getYearWeek = function(data) {
/*
date1是当前日期
date2是当年第一天
d是当前日期是今年第多少天
用d + 当前年的第一天的周差距的和在除以7就是本年第几周
*/
var _data = new Date(data);
var date1 = new Date(_data.getFullYear(), parseInt((_data.getMonth()+1) - 1), _data.getDate()),
date2 = new Date(_data.getFullYear(), 0, 1),
d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
return Math.ceil(
(d + ((date2.getDay() + 1) - 1)) / 7
);
};
// ===========================
// js如何判断一组数字是否连续
// https://segmentfault.com/q/1010000002943744
function arrange(source) {
var t;
var ta;
var r = [];
source.forEach(function(v) {
console.log(t, v); // 跟踪调试用
if (t === v) {
ta.push(t);
t++;
return;
}
ta = [v];
t = v + 1;
r.push(ta);
});
return r;
}
var arr = [3, 4, 13, 14, 15, 17, 20, 22];
console.log(arrange(arr));
// ===========================
/**
* 合并相同单元格
* @Author http://zhsq-java.iteye.com/blog/2021833
* @param {[object]} tbl [table对应的dom元素]
* @param {[number]} beginRow [从第几行开始合并(从0开始)]
* @param {[number]} endRow [合并到哪一行,负数表示从底下数几行不合并]
* @param {[array]} colIdxes [合并的列下标的数组,如[0,1]表示合并前两列,[0]表示只合并第一列]
* @return {[type]} [description]
*/
function mergeSameCell(tbl,beginRow,endRow,colIdxes){
var colIdx = colIdxes[0];
var newColIdxes = colIdxes.concat();
newColIdxes.splice(0,1)
var delRows = new Array();
var rs = tbl.rows;
//endRow为0的时候合并到最后一行,小于0时表示最后有-endRow行不合并
if(endRow === 0){
endRow = rs.length - 1;
}else if(endRow < 0){
endRow = rs.length - 1 + endRow;
}
var rowSpan = 1; //要设置的rowSpan的值
var rowIdx = beginRow; //要设置rowSpan的cell行下标
var cellValue; //存储单元格里面的内容
for(var i=beginRow; i<= endRow + 1; i++){
if(i === endRow + 1){//过了最后一行的时候合并前面的单元格
if(newColIdxes.length > 0){
mergeSameCell(tbl,rowIdx,endRow,newColIdxes);
}
rs[rowIdx].cells[colIdx].rowSpan = rowSpan;
rs[rowIdx].cells[colIdx].style.height = rowSpan * 130 + 'px';
}else{
var cell = rs[i].cells[colIdx];
if(i === beginRow){//第一行的时候初始化各个参数
cellValue = cell.innerHTML;
rowSpan = 1;
rowIdx = i;
}else if(cellValue != cell.innerHTML){//数据改变合并前面的单元格
cellValue = cell.innerHTML;
if(newColIdxes.length > 0){
mergeSameCell(tbl,rowIdx,i - 1,newColIdxes);
}
rs[rowIdx].cells[colIdx].rowSpan = rowSpan;
rs[rowIdx].cells[colIdx].style.height = rowSpan * 130 + 'px';
rowSpan = 1;
rowIdx = i;
}else if(cellValue === cell.innerHTML){//数据和前面的数据重复的时候删除单元格
rowSpan++;
delRows.push(i);
}
}
}
for(var j=0;j<delRows.length; j++){
rs[delRows[j]].deleteCell(colIdx);
}
}
mergeSameCell(document.querySelector('table'), 1, 3, 1);
// ========================
/**
* 字符串code换取值
* @param {String} strCode 字符串code: '0'、'1, 2, 5'
* @param {Object} listMap {0: '张三', 1: '李四'}
* @extends i18n 翻译
* @returns '张三, 李四'
* @example convertCode('1, 3', { 0: '张三', 1: '李四', 2: '王五', 3: '赵六' })
* => '李四, 赵六'
*/
export function convertCode(strCode, listMap) {
var res = ''
if (strCode && listMap) {
var strArr = strCode.split(',').map(Number)
var arr = []
if (!isNaN(strArr[0])) {
strArr.forEach(item => {
arr.push(listMap[item])
})
res = arr.join(i18n.t('symbol.comma'))
}
}
return res
}