var _Range = {
getRange: function(){
var sel ;
if (!window.getSelection) {
return null ;
}
sel = window.getSelection();
if (sel.rangeCount > 0) {
return sel.getRangeAt(0);
} else {
return null ;
}
} ,
getCursortPosition: function (ctrl) {
var CaretPos = 0;
if (document.selection) {
ctrl.focus ();
var Sel = document.selection.createRange ();
Sel.moveStart ('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}else if (ctrl.selectionStart || ctrl.selectionStart == '0')
CaretPos = ctrl.selectionStart ;
return (CaretPos);
} ,
setInputCaretPosition(ctrl, pos){
debugger;
if(ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange( pos , pos ) ;
} else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
},
getCaretCharacterOffsetWithin: function( element , current_range ){
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 = current_range ? current_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;
} ,
setCaretPosition: function ( node , textNode , caret ){
node.focus();
var range = document.createRange();
range.setStart(textNode, caret);
range.setEnd(textNode, caret);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} ,
getCursorOffset: function(){
var clonedRange, offset, range, rect, shadowCaret;
if (window.getSelection && (range = this.getRange())) {
if (range.endOffset - 1 > 0 && range.endContainer !== this.domInputor) {
clonedRange = range.cloneRange();
clonedRange.setStart(range.endContainer, range.endOffset - 1);
clonedRange.setEnd(range.endContainer, range.endOffset);
rect = clonedRange.getBoundingClientRect();
offset = {
height: rect.height,
left: rect.left + rect.width,
top: rect.top
};
clonedRange.detach();
}
if (!offset || (offset != null ? offset.height : void 0) === 0) {
clonedRange = range.cloneRange();
shadowCaret = document.createTextNode("|");
clonedRange.insertNode(shadowCaret);
clonedRange.selectNode(shadowCaret);
rect = clonedRange.getBoundingClientRect();
offset = {
height: rect.height,
left: rect.left,
top: rect.top
};
shadowCaret.parentNode.removeChild(shadowCaret);
clonedRange.detach();
}
}
return offset;
} ,
getSelectionStart: function() {
return document.getSelection().anchorNode ;
} ,
moveToEnd : function( obj ){
if (window.getSelection) {
obj.focus();
var range = window.getSelection();
range.selectAllChildren(obj);
range.collapseToEnd();
}
else if (document.selection) {
var range = document.selection.createRange();
range.moveToElementText(obj);
range.collapse(false);
range.select();
}
} ,
insertHtmlAtCurrent: function( html , current_range ){
var sel, range;
if (window.getSelection) {
sel = current_range ? current_range : window.getSelection() ;
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
var el = document.createElement("div");
el.innerHTML = html;
var frag = document.createDocumentFragment(),
node, lastNode;
while ((node = el.firstChild)) {
lastNode = frag.appendChild(node);
}
range.insertNode(frag);
if (lastNode) {
range = range.cloneRange();
range.setStartAfter(lastNode);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
}
} else if (document.selection && document.selection.type != "Control") {
document.selection.createRange().pasteHTML(html);
}
}
}