-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
108 lines (93 loc) · 2.61 KB
/
util.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
function eid(sId) {
return document.getElementById(sId);
}
var agt = navigator.userAgent.toLowerCase();
var is_ie = (agt.indexOf("msie") != -1);
var localAccess = (document.location.href.substring(0,4).toLowerCase() == 'file');
function getOffsetLeft(el) {
var ret = 0;
do {
ret += el.offsetLeft;
} while (el = el.offsetParent);
return ret;
}
function getOffsetTop(el) {
var ret = 0;
do {
ret += el.offsetTop;
} while (el = el.offsetParent);
return ret;
}
function scanLeft(txt, pos, delims, inclusive) {
while (pos > 0) {
var c = txt.charAt(pos);
if (delims.indexOf(c) >= 0)
return (inclusive) ? pos : pos + 1;
pos--;
}
return 0;
}
function scanRight(txt, pos, delims, inclusive) {
var len = txt.length;
while (pos < len) {
var c = txt.charAt(pos);
if (delims.indexOf(c) >= 0)
return (inclusive) ? pos : pos - 1;
pos++;
}
return len - 1;
}
//--- Selection
function Sel() {}
Sel.setRange = function(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
Sel.getRange = function(input) {
if (input.setSelectionRange) {
return { 'start': input.selectionStart, 'end': input.selectionEnd };
}
else
if (document.selection) {
var oRange = document.selection.createRange();
var oTextRange = oElement.createTextRange();
oTextRange.setEndPoint('EndToStart', oRange);
return { 'start': oTextRange.text.length, 'end': (this.start + oRange.text.length) };
}
}
Sel.getText = function(input, sel) {
sel = sel || Sel.getRange(input);
return input.value.slice(sel.start, sel.end);
}
Sel.replace = function(input, replaceString) {
if (input.setSelectionRange) {
var selectionStart = input.selectionStart;
var selectionEnd = input.selectionEnd;
input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
if (selectionStart != selectionEnd) {
Sel.setRange(input, selectionStart, selectionStart + replaceString.length);
} else {
Sel.setRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
}
} else
if (document.selection) {
var range = document.selection.createRange();
if (range.parentElement() == input) {
var isCollapsed = range.text == '';
range.text = replaceString;
if (!isCollapsed) {
range.moveStart('character', -replaceString.length);
range.select();
}
}
}
}