-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
360 lines (339 loc) · 10.4 KB
/
utils.js
File metadata and controls
360 lines (339 loc) · 10.4 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
(function (window, undefined) {
function sUtil() {}
sUtil.fn = sUtil.prototype = {
version: 1.0,
constructor: sUtil,
namespace: function () {
var a = arguments,
o = null,
i,
j,
d;
for (i = 0; i < a.length; i = i + 1) {
d = a[i].split(".");
o = window;
for (j = d[0] == "window" ? 1 : 0; j < d.length; j = j + 1) {
o[d[j]] = o[d[j]] || {};
o = o[d[j]];
}
}
return o;
},
register: function (name, fn) {
var o = window,
j,
d = name.split(".");
for (j = d[0] == "window" ? 1 : 0; j < d.length; j = j + 1) {
if (j < d.length - 1) {
o[d[j]] = o[d[j]] || {};
} else {
if (o[d[j]] === undefined) {
o[d[j]] = function () {
typeof fn === "function" ? fn.apply(this, arguments) : "";
};
} else {
// o[d[j]] has be register;
}
}
o = o[d[j]];
}
return o;
},
parseProps: function (props) {
// json字符串转换成json对象
var mapValue = {
true: true,
false: false,
null: null,
undefined: undefined,
};
if (props) {
// 正则匹配成 key, :, 左侧引号, value, 右侧引号, ",", 6部分,对特殊值做map处理,
// 返回合法json字符串,调用$.parseJSON即可, 举个例子:
// width:'500px',title:'Technology Introduction', collapsible: true
// 然后再正则和map处理=> "width":"500px","title":"Technology Introduction", "collapsible": true
props = props.replace(
/(\w+)(:[\s\uFEFF\xA0]*)(['"])?([^\\"',]+)(['"])?(,?)/g,
function (all, key, colon, lQuotes, value, rQuotes, comma) {
return (
'"' +
key +
'"' +
colon +
(value in mapValue ? mapValue[value] : '"' + value + '"') +
comma
);
}
);
}
try {
return JSON.parse("{" + props + "}");
} catch (e) {
console.log("parseProps转换mu-attr属性成json串出错了");
return {};
}
},
};
window.sUtil = new sUtil();
})(window);
// 防止单击劫持,即通过iframe嵌套页面然后透明,诱导用户单击操作
function banNested() {
//完全不能嵌套
/*if(top != window){
top.location.href = window.location.href;
}*/
//同域可嵌套
try {
top.location.hostname;
if (top.location.hostname != window.location.hostname) {
top.location.href = window.location.href;
}
} catch (e) {
top.location.href = window.location.href;
}
}
var quickSort = function (arr) {
if (arr.length <= 1) {
return arr;
}
var pivotIndex = Math.floor(arr.length / 2);
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot], quickSort(right));
};
/**
* 依赖jQuery的工具
* @type {Object}
*/
var jqueryTool = {
// DOM Opration, 有动画效果的滚动到页面顶部
go2TopWithAnimate: function () {
var rootDom =
document.documentElement.scrollTop === 0
? document.body
: document.documentElement;
try {
$(rootDom).animate(
{
scrollTop: 0,
},
1000
);
} catch (e) {
$(window).scrollTop(0);
}
},
};
// 判断是否加载到页面底部
// $list.scrol1Top + document.body.clientHeight >= $list.scrollHeight;
// 无限分页加载,判断页面是否滚动到底部
function scroll2bottom() {
var pageHeight = Math.max(
document.body.scrollHeight,
document.body.offsetHeight
);
var viewportHeight =
window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight ||
0;
var scrollHeight =
window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop ||
0;
// console.log(pageHeight);
// console.log(viewportHeight);
// console.log(scrollHeight);
return pageHeight - viewportHeight - scrollHeight < 20;
}
// 获取元素离页面顶部的距离
function getPosition(el) {
var t = el.offsetTop;
var elHeight = el.offsetHeight;
for (t; (el = el.offsetParent); ) {
t += el.offsetTop;
}
return {
y: (t + elHeight) * window.devicePixelRatio,
};
}
// 通过fixed和absolute模拟sticky的效果
$.fn.sticky = function (option) {
var def = { top: 0 };
$.extend(def, option);
$.each($(this), function (i, item) {
var curElement = $(item);
var timeId = null;
var offsetTop = curElement.offset().top;
$(window).scroll(function () {
var scrollTop = $(this).scrollTop();
if (scrollTop >= offsetTop - def.top) {
var match = /(msie) ([\w.]+)/.exec(navigator.userAgent.toLowerCase());
if (!!match && match[1].msie && match[2] === "6.0") {
// don't support position fixed in ie6
clearTimeout(timeId);
timeId = setTimeout(function () {
curElement.css({ top: scrollTop + def.top + "px" });
}, 50);
} else {
curElement.css({ position: "fixed", top: def.top + "px" });
}
} else {
curElement.css({ position: "absolute", top: offsetTop + "px" });
}
});
});
$(function () {
$(window).triggerHandler("scroll");
});
};
// 解决ie6背景图片不缓存的问题
try {
document.execCommand("BackgroundImageCache", false, true);
} catch (e) {}
// 判断元素是否在视窗范围内
// listener:{parentEl:"",y:元素离顶点的距离, callback:callback}
function checkCanShow(listener) {
var winH = undefined;
var top = undefined;
if (listener.parentEl) {
winH = listener.parentEl.offsetHeight;
top = listener.parentEl.scrollTop;
} else {
winH = window.screen.availHeight;
top = document.documentElement.scrollTop || document.body.scrollTop;
}
var height = (top + winH) * window.devicePixelRatio * 1.3;
if (listener.y < height) {
typeof listener.callback === "function" ? listener.callback() : "";
}
}
// 获取contenteditable元素中光标的位置
//http://stackoverflow.com/questions/4811822/get-a-ranges-start-and-end-offsets-relative-to-its-parent-container/4812022#4812022
//http://stackoverflow.com/questions/4767848/get-caret-cursor-position-in-contenteditable-area-containing-html-content
//http://jsfiddle.net/TjXEG/900/
function getCaretCharacterOffsetWithin(element) {
var caretOffset = 0;
var doc = element.ownerDocument || element.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(element);
preCaretRange.setEnd(range.endContainer, range.endOffset);
caretOffset = preCaretRange.toString().length;
}
} else if ((sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(element);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
caretOffset = preCaretTextRange.text.length;
}
return caretOffset;
}
// 设置contenteditable元素中光标的位置
//http://stackoverflow.com/questions/6249095/how-to-set-caretcursor-position-in-contenteditable-element-div
//http://jsfiddle.net/timdown/vXnCM/
function setCaret(el, pos) {
if (el.childNodes.length > 0) {
var range = document.createRange();
var sel = window.getSelection();
range.setStart(el.childNodes[0], pos);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
el.focus();
}
/**
* 获取对象属性,通过路径
*/
function getObjValByPath(obj, path) {
let paths = path.split(".");
let res = obj;
let prop;
while ((prop = paths.shift())) {
res = res[prop];
}
return res;
}
// 自关闭标签转换成标准的html关闭标签
function convert(html) {
var tags = /^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i;
return html.replace(/(<(\w+)[^>]*?)\/>/g, function (all, front, tag) {
return tags.test(tag) ? all : front + "></" + tag + ">";
});
}
var HtmlUtil = {
/*1.用正则表达式实现html转码*/
htmlEncodeByRegExp: function (str) {
var s = "";
if (str.length == 0) return "";
s = str.replace(/&/g, "&");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/\'/g, "'");
s = s.replace(/\"/g, """);
return s;
},
/*2.用正则表达式实现html解码*/
htmlDecodeByRegExp: function (str) {
var s = "";
if (str.length == 0) return "";
s = str.replace(/&/g, "&");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/'/g, "'");
s = s.replace(/"/g, '"');
return s;
},
};
var HtmlUtil = {
/*1.用浏览器内部转换器实现html转码*/
htmlEncode: function (html) {
//1.首先动态创建一个容器标签元素,如DIV
var temp = document.createElement("div");
//2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐,google支持)
temp.textContent != undefined
? (temp.textContent = html)
: (temp.innerText = html);
//3.最后返回这个元素的innerHTML,即得到经过HTML编码转换的字符串了
var output = temp.innerHTML;
temp = null;
return output;
},
/*2.用浏览器内部转换器实现html解码*/
htmlDecode: function (text) {
//1.首先动态创建一个容器标签元素,如DIV
var temp = document.createElement("div");
//2.然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持)
temp.innerHTML = text;
//3.最后返回这个元素的innerText(ie支持)或者textContent(火狐,google支持),即得到经过HTML解码的字符串了。
var output = temp.innerText || temp.textContent;
temp = null;
return output;
},
};
const cacheFn = (fn) => {
const cache = Object.create(null);
return (...args) => {
const id = args.join("_");
const hit = cache[id];
return hit || (cache[id] = fn.apply(null, args));
};
};