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
|
/**
* GLSAMaker 2
* Draft editing JS
*/
if (typeof GLSAMaker == "undefined" || !GLSAMaker) {
var GLSAMaker = {};
}
if (typeof GLSAMaker.misc == "undefined" || !GLSAMaker.misc) {
GLSAMaker.misc = function() {
return {
/**
* Find out the selection position in a text field
* via: http://stackoverflow.com/questions/4928586/get-caret-position-in-html-input
*
* @param el
*/
getInputSelection : function(el) {
var start = 0, end = 0, normalizedValue, range,
textInputRange, len, endRange;
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else {
range = document.selection.createRange();
if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");
// Create a working TextRange that lives only in the input
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
}
return {
start: start,
end: end
};
},
/**
* Sets the caret position of a control
* @param ctrl
* @param pos
*/
setInputSelection: function (ctrl, data) {
if (ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange(data.start, data.end);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', data.end);
range.moveStart('character', data.start);
range.select();
}
},
escapeRegExp : function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
};
}();
}
GLSAMaker.misc.ui = function() {
return {
/**
* Docks an element to the right
*/
dock : function(elem) {
if (!elem) {
return;
}
elem.toggleClassName('docked-right');
}
};
}();
|