ShareBox.TextEdit.prototype = {
_constructor: function(field) {
this.field = $(field).get(0);
return this;
},
getLenInCh: function() {
var str = this.field.value;
var ch = str.match(/[\u4E00-\uFA29]/ig);
var en = str.match(/[^\u4E00-\uFA29]/ig);
var cl = ch ? ch.length * 2 : 0;
var el = en ? en.length : 0;
return Math.ceil((cl + el) / 2);
},
insert: function(value, type) {
var field = this.getField();
value = value.toString();
if (document.selection) { //IE
field.focus();
var seltext = this.getSelectText(field),
startPos = 'string' === typeof seltext ? field.value.length - seltext.length : field.value.length,
sel = document.selection.createRange();
sel.text = value;
sel.select();
if (type) {
var rng = field.createTextRange();
if (type == 'select') {
rng.moveStart('character', startPos);
} else if (type == 'start') {
rng.moveEnd('character', - value.length);
rng.moveStart('character', startPos);
}
rng.select();
}
} else if (field.selectionStart || field.selectionStart == '0') { //^IE
var startPos = field.selectionStart, endPos = field.selectionEnd,
restoreTop = field.scrollTop;
field.value = field.value.substring(0, startPos) + value + field.value.substring(endPos, field.value.length);
if (restoreTop > 0) field.scrollTop = restoreTop;
field.focus();
if (type == 'select') {
field.selectionStart = startPos;
field.selectionEnd = startPos + value.length;
} else if (type == 'start') {
field.selectionStart = field.selectionEnd = startPos;
} else {
field.selectionStart = field.selectionEnd = startPos + value.length;
}
} else{ //others
field.value += value;
field.focus();
}
return this;
},
insertAfterStart: function(value) {
this.insert(value, 'start');
return this;
},
insertAfterSelect: function(value) {
this.insert(value, 'select');
return this;
},
del: function(del_num) {
var field = this.getField();
var pos = this.getPos(field);
if (pos.startPos == 0) { //如果位置为0, 则会 自动在加上匹配内容;
return false;
}
var ft = field.scrollTop;
var val = field.value;
field.value = del_num > 0 ? val.slice(0, pos - del_num) + val.slice(pos): val.slice(0, pos) + val.slice(pos - num);
setPos(field, pos - (del_num < 0 ? 0 : del_num));
setTimeout(function() {
if (field.scrollTop != ft) field.scrollTop = ft;
}, 10);
return this;
},
getSelectText: function() {
var field = this.getField();
field.focus();
if (typeof document.selection != 'undefined') {
return document.selection.createRange().text;
}
if (field.selectionStart || field.selectionStart == '0') {
return field.value.substr(field.selectionStart, field.selectionEnd - field.selectionStart);
}
},
getPos: function() {
var field = this.getField();
if (document.selection) {
field.focus();
var rng = document.selection.createRange();
var tx_rng = document.body.createTextRange();
tx_rng.moveToElementText(field);
for (var startPos = 0; tx_rng.compareEndPoints('StartToStart' , rng) < 0; startPos ++) {
tx_rng.moveStart('character', 1);
}
for (var endPos = 0; tx_rng.compareEndPoints('StartToEnd' , rng) < 0; endPos ++) {
tx_rng.moveStart('character', 1);
}
return {
startPos: startPos,
endPos: endPos
};
} else if (field.selectionStart || field.selectionStart == '0') {
return {
startPos: field.selectionStart,
endPos: field.selectionEnd
};
}
},
select: function(start_pos, end_pos) {
var field = this.getField();
if (start_pos == undefined || end_pos == undefined || start_pos < 0 || end_pos > field.value.length) {
return false;
}
if (document.selection) { //IE
var rng = field.createTextRange();
rng.moveEnd('character', - field.value.length);
rng.moveEnd('character', end_pos);
rng.moveStart('character', start_pos);
rng.select();
} else { //^IE;
field.setSelectionRange(start_pos, end_pos);
field.focus();
}
return this;
},
setPos: function(pos) {
this.select(pos , pos);
return this;
},
selectAll:function() {
var field = this.getField();
this.select(0, field.value.length);
return this;
},
selectString: function(str) {
var field = this.getField();
var index = field.value.indexOf(str);
return index != -1
? this.select(field, index, index + str.length)
: false;
},
getCursorOffset: function(cursor_pos) {
var field = this.getField();
if (document.selection) {
var range = document.selection.createRange();
var $win = $(window);
return {
left: range.boundingLeft + $win.scrollLeft(),
top: range.boundingTop + $win.scrollTop() + range.boundingHeight
};
}
var $field = $(field),
$editor = $field.shareBox(),
w = $field.width(),
h = $field.height(),
pos = $field.offset(),
x = pos.left,
y =
pos.top,
end_pos = $editor.getPos().endPos,
str = $field.val(),
start_str = str.substr(0, end_pos).replace(/[(^*\n*)|(^*\r*)]/g, '<br />'),
end_str = str.substr(end_pos, str.length).replace(/[(^*\n*)|(^*\r*)]/g, '<br />'),
fontSize = $field.css('fontSize'),
padding = $field.css('padding'),
lineHeight = $field.css('lineHeight'),
overflow = $field.css('overflow'),
scrollTop = $field.scrollTop();
if (! this.fake) this.fake = $('<div>').appendTo('body');
this.fake.html(start_str + '<span>x</span>' + end_str)
.css({
padding: padding,
width: w, height: h,
opacity: 0,
overflow: overflow,
position: 'absolute',
left: x, top: y, zIndex: -9999,
lineHeight: lineHeight,
wordWrap: 'break-word',
fontSize: fontSize
})
.scrollTop(scrollTop);
var marker = this.fake.children('span'),
height = marker.outerHeight(),
ofs = marker.offset(),
left = ofs.left,
top =
ofs.top,
st = marker.scrollTop();
return {
left: left,
top: top + height - st
};
},
getField: function() {
return this.field;
}
};