$(function() {
var calculator = {
// key styles
primaryStyles: ['fontFamily', 'fontSize', 'fontWeight', 'fontVariant', 'fontStyle',
'paddingLeft', 'paddingTop', 'paddingBottom', 'paddingRight',
'marginLeft', 'marginTop', 'marginBottom', 'marginRight',
'borderLeftColor', 'borderTopColor', 'borderBottomColor', 'borderRightColor',
'borderLeftStyle', 'borderTopStyle', 'borderBottomStyle', 'borderRightStyle',
'borderLeftWidth', 'borderTopWidth', 'borderBottomWidth', 'borderRightWidth',
'line-height', 'outline'],
specificStyle: {
'word-wrap': 'break-word',
'overflow-x': 'hidden',
'overflow-y': 'auto'
},
simulator: $('
').css({
position: 'absolute',
top: 0,
left: 0,
visibility: 'hidden'
}).appendTo(document.body),
toHtml: function(text) {
return text.replace(/\n/g, '
')
.split(' ').join(' ');
},
// calculate position
getCaretPosition: function(cursorPosition) {
var cal = calculator, self = this, element = self[0], elementOffset = self.offset();
// IE has easy way to get caret offset position
if ($.browser.msie) {
// must get focus first
element.focus();
var range = document.selection.createRange();
$('#hskeywords').val(element.scrollTop);
return {
left: range.boundingLeft - elementOffset.left,
top: parseInt(range.boundingTop) - elementOffset.top + element.scrollTop
+ document.documentElement.scrollTop + parseInt(self.getComputedStyle("fontSize"))
};
}
cal.simulator.empty();
// clone primary styles to imitate textarea
$.each(cal.primaryStyles, function(index, styleName) {
self.cloneStyle(cal.simulator, styleName);
});
// caculate width and height
cal.simulator.css($.extend({
'width': self.width(),
'height': self.height()
}, cal.specificStyle));
var value = self.val();
var beforeText = value.substring(0, cursorPosition),
afterText = value.substring(cursorPosition);
var before = $('').html(cal.toHtml(beforeText)),
focus = $(''),
after = $('').html(cal.toHtml(afterText));
cal.simulator.append(before).append(focus).append(after);
var focusOffset = focus.offset(), simulatorOffset = cal.simulator.offset();
// alert(focusOffset.left + ',' + simulatorOffset.left + ',' + element.scrollLeft);
return {
top: focusOffset.top - simulatorOffset.top - element.scrollTop
// calculate and add the font height except Firefox
+ ($.browser.mozilla ? 0 : parseInt(self.getComputedStyle("fontSize"))),
left: focus[0].offsetLeft - cal.simulator[0].offsetLeft - element.scrollLeft
};
}
};
$.fn.extend({
getComputedStyle: function(styleName) {
if (this.length == 0) return;
var thiz = this[0];
var result = this.css(styleName);
result = result || ($.browser.msie ?
thiz.currentStyle[styleName] :
document.defaultView.getComputedStyle(thiz, null)[styleName]);
return result;
},
// easy clone method
cloneStyle: function(target, styleName) {
var styleVal = this.getComputedStyle(styleName);
if (!!styleVal) {
$(target).css(styleName, styleVal);
}
},
cloneAllStyle: function(target, style) {
var thiz = this[0];
for (var styleName in thiz.style) {
var val = thiz.style[styleName];
typeof val == 'string' || typeof val == 'number'
? this.cloneStyle(target, styleName)
: NaN;
}
},
getCursorPosition: function() {
var thiz = this[0], result = 0;
if ('selectionStart' in thiz) {
result = thiz.selectionStart;
} else if ('selection' in document) {
var range = document.selection.createRange();
if (parseInt($.browser.version) > 6) {
thiz.focus();
var length = document.selection.createRange().text.length;
range.moveStart('character', -thiz.value.length);
result = range.text.length - length;
} else {
var bodyRange = document.body.createTextRange();
bodyRange.moveToElementText(thiz);
for (; bodyRange.compareEndPoints("StartToStart", range) < 0; result++)
bodyRange.moveStart('character', 1);
for (var i = 0; i <= result; i++) {
if (thiz.value.charAt(i) == '\n')
result++;
}
var enterCount = thiz.value.split('\n').length - 1;
result -= enterCount;
return result;
}
}
return result;
},
getCaretPosition: calculator.getCaretPosition
});
});
TextareaUtils = (function() {
var it = {};
var ds = document.selection;
var atTime = null;
var reg = new RegExp("@[0-9(请输入网校号)]{0,12}$");
it.init = function(obj, numbObj) {
it.check(obj, numbObj);
$(obj).bind('click keyup input propertychange', function(event) {
$('#atTip').remove();
//$('').appendTo($(obj).parent()).hide();
it.check(obj, numbObj);
window.clearTimeout(atTime);
atTime = window.setTimeout(function() {
var pos = it.getTextPos(obj).start;
var pretext = obj.value.substr(0, pos);
if (reg.test(pretext)) {
var m = pretext.match(reg).toString();
var reg2 = new RegExp("^[0-9]+[ ]{0,}");
var m2 = (obj.value.substr(pos).match(reg2) || "").toString();
var position = $(obj).getCaretPosition(pos - m.length + 2);
showAtContent(obj, obj);
$('#showAtContent').css({ left: ($(obj).offset().left + position.left), top: ($(obj).offset().top + position.top + 10) });
$('#showAtContent input').val(m.substring(1, m.length) + m2.substring(0, m2.length - 1)).change();
$('#showAtContent li').unbind('click').click(function() {
var usernumb = $(this).attr('usernumb');
it.selectText(obj, pos - m.length + 1, pos + m2.length + 1);
it.rangeText(obj, usernumb + " ");
it.selectText(obj, pos - m.length + usernumb.length + 1, pos - m.length + usernumb.length + 1);
$('#showAtContent').hide();
it.selectText(obj, pos - m.length, pos - m.length + usernumb.length + 1);
});
} else {
$('#showAtContent').hide();
}
}, 100);
});
};
it.check = function(obj, numbObj) {
var text = $(obj).val();
text = text.replace(/@请输入网校号/g, "");
text = text.replace(/#请输入话题#/g, "");
text = text.replace(/^[ \r\n]+$/g, "");
text = text.replace(/[ \r\n]+/g, " ");
var n = 140 - text.length;
if (n == 140) { $(numbObj).html("请输入内容"); return false; }
else if (n >= 0) { $(numbObj).html("还可输入" + n + "字"); return true; }
else {
$(numbObj).html("已经超出" + (-n) + "字");
return false;
}
}
//@人
it.inAt = function(obj, str) {
var str = str || "@请输入网校号";
var reg = new RegExp(str),
oValue = obj.value,
val_index = 0,
val_match,
val_pos;
if (reg.test(oValue)) {
val_match = oValue.match(reg);
val_index = val_match.index;
}
else {
val_pos = it.getTextPos(obj);
val_index = val_pos.start;
it.rangeText(obj, str);
}
it.selectText(obj, val_index + str.length, val_index + str.length);
};
//插入话题
it.inTopic = function(obj, str) {
var str = str || "请输入话题";
str = "#" + str + "#";
var reg = new RegExp(str),
oValue = obj.value,
val_index = 0,
val_match,
val_pos;
if (reg.test(oValue)) {
val_match = oValue.match(reg);
val_index = val_match.index;
}
else {
val_pos = it.getTextPos(obj);
val_index = val_pos.start;
it.rangeText(obj, str);
}
it.selectText(obj, val_index + 1, val_index + str.length - 1);
};
//插入文本
it.inText = function(obj, str) {
val_pos = it.getTextPos(obj);
val_index = val_pos.start;
it.rangeText(obj, str);
it.selectText(obj, val_index, val_index + str.length);
};
//插入文本 不选
it.inText1 = function (obj, str) {
val_pos = it.getTextPos(obj);
val_index = val_pos.start;
it.rangeText(obj, str);
it.selectText(obj, val_index + str.length, val_index + str.length);
};
/*获取光标位置*/
it.getTextPos = function(obj) {
if (!obj || obj.offsetWidth == 0) { return false; }
var t = obj, start = 0, end = 0, value = '', range, range_all;
value = t.value;
if (typeof (t.selectionStart) == "number") {
start = t.selectionStart;
end = t.selectionEnd;
}
else if (document.selection && t.offsetWidth > 0) {
t.focus();
range = document.selection.createRange();
if (range.parentElement() == t) {
range_all = document.body.createTextRange();
range_all.moveToElementText(t);
for (start = 0; range_all.compareEndPoints("StartToStart", range) < 0; start++) {
range_all.moveStart('character', 1);
}
for (var i = 0; i <= start; i++) {
if (t.value.charAt(i) == '\n') {
start++;
}
}
range_all.moveToElementText(t);
for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end++) {
range_all.moveStart('character', 1);
}
for (var i = 0; i <= end; i++) {
if (t.value.charAt(i) == '\n') {
end++;
}
}
}
}
else {
start = value.length;
end = value.length;
}
return {
start: start,
end: end
}
};
//光标处替换的文本
it.rangeText = function(obj, val, num) {
if (!obj || obj.offsetWidth == 0) { return false; }
var t = obj, start = 0, end = 0, value = '', val_start = '', val_end = '', pos;
var userAgent = navigator.userAgent.toLowerCase(), ie = /msie/.test(userAgent) && !/opera/.test(userAgent);
if (ie && t.nodeName.toLocaleLowerCase() != 'textarea') {
t.value = val;
t.focus;
return;
}
value = t.value;
pos = it.getTextPos(t);
start = pos.start;
end = pos.end;
if (typeof num == 'number') {
start = start - num;
}
val_start = value.substring(0, start);
val_end = value.substring(end);
t.value = val_start + val + val_end;
if (typeof num == 'number') {
end = end - num;
}
it.selectText(t, end + val.length, end + val.length);
};
//光标定位到指定文本
it.selectText = function(obj, start, stop) {
if (!obj || obj.offsetWidth == 0) { return false; }
var range;
if (obj.setSelectionRange) {
obj.setSelectionRange(start, stop);
}
else {
range = obj.createTextRange();
range.collapse(true);
range.moveStart("character", start);
range.moveEnd("character", stop - start);
range.select();
}
obj.focus();
};
it.shownormal = function(obj, times) {
$(obj).css("background-color", "#FFF");
if (times < 0) return;
times = times - 1;
setTimeout(function() { TextareaUtils.showerror(obj, times) }, 150);
}
it.showerror = function(obj, times) {
$(obj).css("background-color", "#F6CECE");
times = times - 1;
setTimeout(function() { TextareaUtils.shownormal(obj, times) }, 150);
}
return it;
})();
if ($('#weibo_textarea').length > 0)
TextareaUtils.init($('#weibo_textarea')[0], $('#weibo_num')[0]);