|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
if (typeof window.CustomEvent !== "function") { |
|
|
var CustomEvent = function (event, params) { |
|
|
params = params || {bubbles: false, cancelable: false, detail: undefined}; |
|
|
var evt = document.createEvent('CustomEvent'); |
|
|
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail); |
|
|
return evt; |
|
|
}; |
|
|
CustomEvent.prototype = window.Event.prototype; |
|
|
window.CustomEvent = CustomEvent; |
|
|
} |
|
|
} catch (e) { |
|
|
|
|
|
} |
|
|
|
|
|
var EditArea = function(textareaid) { |
|
|
if (typeof textareaid === "string") |
|
|
this.textarea = document.getElementById(textareaid); |
|
|
else |
|
|
this.textarea = textareaid; |
|
|
this.initialText = this.textarea.value; |
|
|
this.isDragging = false; |
|
|
} |
|
|
|
|
|
EditArea.prototype.addSelectionListener = function(listener) { |
|
|
this.textarea.onmousemove = function(ev) { |
|
|
if (this.isDragging) |
|
|
listener.fireSelectionChanged(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
EditArea.prototype.addChangeListener = function(listener) { |
|
|
this.changelistener = listener; |
|
|
this.textarea.onkeyup = function() { |
|
|
listener.fireChanged(); |
|
|
}; |
|
|
this.textarea.onmousedown = function() { |
|
|
this.isDragging = true; |
|
|
listener.fireSelectionChanged(); |
|
|
}; |
|
|
this.textarea.onmouseup = function() { |
|
|
this.isDragging = false; |
|
|
listener.fireChanged(); |
|
|
}; |
|
|
this.textarea.onchange = function() { |
|
|
listener.fireChanged(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
EditArea.prototype.getSelection = function() { |
|
|
return {start: this.textarea.selectionStart, end: this.textarea.selectionEnd}; |
|
|
}; |
|
|
|
|
|
EditArea.prototype.setSelection = function(start, end) { |
|
|
if(this.textarea.setSelectionRange) |
|
|
this.textarea.setSelectionRange(start, end); |
|
|
else if(this.textarea.createTextRange) { |
|
|
|
|
|
var e = this.textarea.createTextRange(); |
|
|
e.collapse(true); |
|
|
e.moveEnd('character', end); |
|
|
e.moveStart('character', start); |
|
|
e.select(); |
|
|
} |
|
|
this.textarea.focus(); |
|
|
}; |
|
|
|
|
|
EditArea.prototype.getString = function() { |
|
|
return this.textarea.value; |
|
|
}; |
|
|
|
|
|
EditArea.prototype.setString = function(str) { |
|
|
this.textarea.value = str; |
|
|
this.initialText = this.getString(); |
|
|
if (this.changelistener) { |
|
|
this.changelistener.fireChanged(); |
|
|
} |
|
|
}; |
|
|
|
|
|
EditArea.prototype.getElem = function() { |
|
|
return this.textarea; |
|
|
}; |
|
|
|
|
|
module.exports = EditArea; |
|
|
|