92 lines
3.1 KiB
JavaScript
92 lines
3.1 KiB
JavaScript
/**
|
||
* @author Ryan Johnson <ryan@livepipe.net>
|
||
* @copyright 2007 LivePipe LLC
|
||
* @package Control.TextArea
|
||
* @license MIT
|
||
* @url http://livepipe.net/projects/control_textarea/
|
||
* @version 2.0.0
|
||
*/
|
||
|
||
if(typeof(Control) == 'undefined')
|
||
Control = {};
|
||
Control.TextArea = Class.create();
|
||
Object.extend(Control.TextArea.prototype,{
|
||
onChangeTimeoutLength: 500,
|
||
element: false,
|
||
onChangeTimeout: false,
|
||
initialize: function(textarea){
|
||
this.element = $(textarea);
|
||
$(this.element).observe('keyup',this.doOnChange.bindAsEventListener(this));
|
||
$(this.element).observe('paste',this.doOnChange.bindAsEventListener(this));
|
||
$(this.element).observe('input',this.doOnChange.bindAsEventListener(this));
|
||
},
|
||
doOnChange: function(event){
|
||
if(this.onChangeTimeout)
|
||
window.clearTimeout(this.onChangeTimeout);
|
||
this.onChangeTimeout = window.setTimeout(function(){
|
||
if(this.notify)
|
||
this.notify('change',this.getValue());
|
||
}.bind(this),this.onChangeTimeoutLength);
|
||
},
|
||
getValue: function(){
|
||
return this.element.value;
|
||
},
|
||
getSelection: function(){
|
||
/*
|
||
http://jira.csdn.net/browse/JAVAEYE-484
|
||
ITeye bbcode 编辑器 加样式标签会覆盖文字
|
||
原来的getSelection()方式在Firefox的textarea中不生效,修改为优先使用selectionStart selectionEnd
|
||
*/
|
||
if(document.selection) {
|
||
return document.selection.createRange().text;
|
||
} else if(this.element.setSelectionRange) {
|
||
return this.element.value.substring(this.element.selectionStart, this.element.selectionEnd);
|
||
} else if(window.getSelection){
|
||
this.element.focus();
|
||
return window.getSelection().toString();
|
||
} else {
|
||
return false;
|
||
}
|
||
},
|
||
replaceSelection: function(text){
|
||
var scrollTop=this.element.scrollTop,
|
||
l = text.length;
|
||
if(!!document.selection){
|
||
this.element.focus();
|
||
var old=document.selection.createRange().text;
|
||
var range=document.selection.createRange();
|
||
range.text=text;
|
||
range-=old.length-text.length;
|
||
}else if(!!this.element.setSelectionRange){
|
||
var selection_start=this.element.selectionStart,selection_end = this.element.selectionEnd;
|
||
this.element.value=this.element.value.slice(0,selection_start)+text+this.element.value.slice(selection_end);
|
||
this.element.setSelectionRange(selection_start,selection_start+text.length);
|
||
}
|
||
this.doOnChange();
|
||
this.element.focus();
|
||
//this.element.scrollTop=scrollTop;
|
||
},
|
||
wrapSelection: function(before,after){
|
||
|
||
this.replaceSelection(before + this.getSelection() + after);
|
||
|
||
|
||
},
|
||
insertBeforeSelection: function(text){
|
||
this.replaceSelection(text + this.getSelection());
|
||
},
|
||
insertAfterSelection: function(text){
|
||
this.replaceSelection(this.getSelection() + text);
|
||
},
|
||
injectEachSelectedLine: function(callback,before,after){
|
||
this.replaceSelection((before || '') + $A(this.getSelection().split("\n")).inject([],callback).join("\n") + (after || ''));
|
||
},
|
||
insertBeforeEachSelectedLine: function(text,before,after){
|
||
this.injectEachSelectedLine(function(lines,line){
|
||
lines.push(text + line);
|
||
return lines;
|
||
},before,after);
|
||
}
|
||
});
|
||
if(typeof(Object.Event) != 'undefined')
|
||
Object.Event.extend(Control.TextArea); |