namespace methods from lib
This commit is contained in:
parent
77f941515e
commit
971b96c132
4 changed files with 67 additions and 61 deletions
38
Editor.js
38
Editor.js
|
|
@ -13,7 +13,7 @@ var keys = {
|
|||
|
||||
function KeyBinding(element, host)
|
||||
{
|
||||
addListener(element, "keydown", function(e)
|
||||
lib.addListener(element, "keydown", function(e)
|
||||
{
|
||||
var key = e.keyCode;
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ function KeyBinding(element, host)
|
|||
if (e.metaKey)
|
||||
{
|
||||
host.selectAll();
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ function KeyBinding(element, host)
|
|||
} else {
|
||||
host.navigateUp();
|
||||
}
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
|
||||
case keys.DOWN:
|
||||
if (e.shiftKey) {
|
||||
|
|
@ -41,7 +41,7 @@ function KeyBinding(element, host)
|
|||
} else {
|
||||
host.navigateDown();
|
||||
}
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
|
||||
case keys.LEFT:
|
||||
if (e.metaKey && e.shiftKey) {
|
||||
|
|
@ -53,7 +53,7 @@ function KeyBinding(element, host)
|
|||
} else {
|
||||
host.navigateLeft();
|
||||
}
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
|
||||
case keys.RIGHT:
|
||||
if (e.metaKey && e.shiftKey) {
|
||||
|
|
@ -65,7 +65,7 @@ function KeyBinding(element, host)
|
|||
} else {
|
||||
host.navigateRight();
|
||||
}
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
|
||||
case keys.POS1:
|
||||
if (e.shiftKey) {
|
||||
|
|
@ -73,7 +73,7 @@ function KeyBinding(element, host)
|
|||
} else {
|
||||
host.navigateLineStart();
|
||||
}
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
|
||||
case keys.END:
|
||||
if (e.shiftKey) {
|
||||
|
|
@ -81,19 +81,19 @@ function KeyBinding(element, host)
|
|||
} else {
|
||||
host.navigateLineEnd();
|
||||
}
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
|
||||
case keys.DELETE:
|
||||
host.removeRight();
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
|
||||
case keys.BACKSPACE:
|
||||
host.removeLeft();
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
|
||||
case keys.TAB:
|
||||
host.onTextInput(" ");
|
||||
return stopEvent(e);
|
||||
return lib.stopEvent(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
@ -106,13 +106,13 @@ function Editor(doc, renderer)
|
|||
this.textInput = new TextInput(container, this);
|
||||
new KeyBinding(container, this);
|
||||
|
||||
addListener(container, "mousedown", bind(this.onMouseDown, this));
|
||||
addListener(container, "dblclick", bind(this.onMouseDoubleClick, this));
|
||||
addMouseWheelListener(container, bind(this.onMouseWheel, this));
|
||||
addTripleClickListener(container, bind(this.selectCurrentLine, this));
|
||||
lib.addListener(container, "mousedown", lib.bind(this.onMouseDown, this));
|
||||
lib.addListener(container, "dblclick", lib.bind(this.onMouseDoubleClick, this));
|
||||
lib.addMouseWheelListener(container, lib.bind(this.onMouseWheel, this));
|
||||
lib.addTripleClickListener(container, lib.bind(this.selectCurrentLine, this));
|
||||
|
||||
this.doc = doc;
|
||||
doc.addChangeListener(bind(this.onDocumentChange, this));
|
||||
doc.addChangeListener(lib.bind(this.onDocumentChange, this));
|
||||
renderer.setDocument(doc);
|
||||
|
||||
this.cursor = {
|
||||
|
|
@ -184,10 +184,10 @@ function Editor(doc, renderer)
|
|||
_self.renderer.scrollCursorIntoView();
|
||||
};
|
||||
|
||||
capture(this.container, onMouseSelection, onMouseSelectionEnd);
|
||||
lib.capture(this.container, onMouseSelection, onMouseSelectionEnd);
|
||||
var timerId = setInterval(onSelectionInterval, 20 );
|
||||
|
||||
return preventDefault(e);
|
||||
return lib.preventDefault(e);
|
||||
},
|
||||
|
||||
onMouseDoubleClick : function(e)
|
||||
|
|
@ -233,7 +233,7 @@ function Editor(doc, renderer)
|
|||
{
|
||||
var delta = e.wheel;
|
||||
this.renderer.scrollToY(this.renderer.getScrollTop() - (delta * 15));
|
||||
return preventDefault(e);
|
||||
return lib.preventDefault(e);
|
||||
},
|
||||
|
||||
getCopyText : function()
|
||||
|
|
|
|||
22
TextInput.js
22
TextInput.js
|
|
@ -51,23 +51,23 @@ function TextInput(parentNode, host) {
|
|||
text.select();
|
||||
}
|
||||
|
||||
addListener(text, "keypress", onTextInput, false);
|
||||
addListener(text, "textInput", onTextInput, false);
|
||||
addListener(text, "paste", onTextInput, false);
|
||||
addListener(text, "propertychange", onTextInput, false);
|
||||
lib.addListener(text, "keypress", onTextInput, false);
|
||||
lib.addListener(text, "textInput", onTextInput, false);
|
||||
lib.addListener(text, "paste", onTextInput, false);
|
||||
lib.addListener(text, "propertychange", onTextInput, false);
|
||||
|
||||
addListener(text, "copy", onCopy, false);
|
||||
addListener(text, "cut", onCut, false);
|
||||
lib.addListener(text, "copy", onCopy, false);
|
||||
lib.addListener(text, "cut", onCut, false);
|
||||
|
||||
addListener(text, "compositionstart", onCompositionStart, false);
|
||||
addListener(text, "compositionupdate", onCompositionUpdate, false);
|
||||
addListener(text, "compositionend", onCompositionEnd, false);
|
||||
lib.addListener(text, "compositionstart", onCompositionStart, false);
|
||||
lib.addListener(text, "compositionupdate", onCompositionUpdate, false);
|
||||
lib.addListener(text, "compositionend", onCompositionEnd, false);
|
||||
|
||||
addListener(text, "blur", function() {
|
||||
lib.addListener(text, "blur", function() {
|
||||
host.onBlur();
|
||||
}, false);
|
||||
|
||||
addListener(text, "focus", function() {
|
||||
lib.addListener(text, "focus", function() {
|
||||
host.onFocus();
|
||||
}, false);
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@
|
|||
<script type="text/javascript" charset="utf-8">
|
||||
|
||||
var el = document.getElementById("juhu");
|
||||
addTripleClickListener(el, function() {
|
||||
lib.addTripleClickListener(el, function() {
|
||||
console.log("triple");
|
||||
})
|
||||
|
||||
|
|
|
|||
66
lib.js
66
lib.js
|
|
@ -1,4 +1,8 @@
|
|||
function addListener(elem, type, callback) {
|
||||
(function() {
|
||||
|
||||
window.lib = {};
|
||||
|
||||
lib.addListener = function(elem, type, callback) {
|
||||
if (elem.addEventListener) {
|
||||
return elem.addEventListener(type, callback, false);
|
||||
}
|
||||
|
|
@ -11,7 +15,7 @@ function addListener(elem, type, callback) {
|
|||
}
|
||||
}
|
||||
|
||||
function removeListener(elem, type, callback) {
|
||||
lib.removeListener = function(elem, type, callback) {
|
||||
if (elem.removeEventListener) {
|
||||
return elem.removeEventListener(type, callback, false);
|
||||
}
|
||||
|
|
@ -20,7 +24,7 @@ function removeListener(elem, type, callback) {
|
|||
}
|
||||
}
|
||||
|
||||
function setText(elem, text) {
|
||||
lib.setText = function(elem, text) {
|
||||
if (elem.innerText !== undefined) {
|
||||
elem.innerText = text;
|
||||
}
|
||||
|
|
@ -29,20 +33,20 @@ function setText(elem, text) {
|
|||
}
|
||||
}
|
||||
|
||||
function stopEvent(e) {
|
||||
stopPropagation(e);
|
||||
preventDefault(e);
|
||||
lib.stopEvent = function(e) {
|
||||
lib.stopPropagation(e);
|
||||
lib.preventDefault(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
function stopPropagation(e) {
|
||||
lib.stopPropagation = function(e) {
|
||||
if (e.stopPropagation)
|
||||
e.stopPropagation();
|
||||
else
|
||||
e.cancelBubble = true;
|
||||
}
|
||||
|
||||
function preventDefault (e)
|
||||
lib.preventDefault = function(e)
|
||||
{
|
||||
if (e.preventDefault)
|
||||
e.preventDefault();
|
||||
|
|
@ -50,7 +54,7 @@ function preventDefault (e)
|
|||
e.returnValue = false;
|
||||
}
|
||||
|
||||
inherits = function (ctor, superCtor) {
|
||||
lib.inherits = function(ctor, superCtor) {
|
||||
var tempCtor = function(){};
|
||||
tempCtor.prototype = superCtor.prototype;
|
||||
ctor.super_ = superCtor.prototype;
|
||||
|
|
@ -58,25 +62,25 @@ inherits = function (ctor, superCtor) {
|
|||
ctor.prototype.constructor = ctor;
|
||||
};
|
||||
|
||||
getInnerWidth = function(element)
|
||||
lib.getInnerWidth = function(element)
|
||||
{
|
||||
return (
|
||||
parseInt(computedStyle(element, "paddingLeft")) +
|
||||
parseInt(computedStyle(element, "paddingRight")) +
|
||||
parseInt(lib.computedStyle(element, "paddingLeft")) +
|
||||
parseInt(lib.computedStyle(element, "paddingRight")) +
|
||||
element.clientWidth
|
||||
);
|
||||
};
|
||||
|
||||
getInnerHeight = function(element)
|
||||
lib.getInnerHeight = function(element)
|
||||
{
|
||||
return (
|
||||
parseInt(computedStyle(element, "paddingTop")) +
|
||||
parseInt(computedStyle(element, "paddingBottom")) +
|
||||
parseInt(lib.computedStyle(element, "paddingTop")) +
|
||||
parseInt(lib.computedStyle(element, "paddingBottom")) +
|
||||
element.clientHeight
|
||||
);
|
||||
};
|
||||
|
||||
computedStyle = function(element, style)
|
||||
lib.computedStyle = function(element, style)
|
||||
{
|
||||
if (window.getComputedStyle) {
|
||||
return (window.getComputedStyle(element, null))[style];
|
||||
|
|
@ -85,7 +89,7 @@ computedStyle = function(element, style)
|
|||
}
|
||||
}
|
||||
|
||||
scrollbarHeight = function() {
|
||||
lib.scrollbarHeight = function() {
|
||||
var el = document.createElement("div");
|
||||
var style = el.style;
|
||||
|
||||
|
|
@ -101,13 +105,13 @@ scrollbarHeight = function() {
|
|||
return height;
|
||||
}
|
||||
|
||||
bind = function(fcn, context) {
|
||||
lib.bind = function(fcn, context) {
|
||||
return function() {
|
||||
return fcn.apply(context, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
capture = function(el, eventHandler, releaseCaptureHandler)
|
||||
lib.capture = function(el, eventHandler, releaseCaptureHandler)
|
||||
{
|
||||
function onMouseMove(e)
|
||||
{
|
||||
|
|
@ -130,17 +134,17 @@ capture = function(el, eventHandler, releaseCaptureHandler)
|
|||
document.addEventListener("mouseup", onMouseUp, true);
|
||||
}
|
||||
|
||||
function addMouseWheelListener(el, callback)
|
||||
lib.addMouseWheelListener = function(el, callback)
|
||||
{
|
||||
var listener = function(e) {
|
||||
e.wheel = (e.wheelDelta) ? e.wheelDelta / 120 : -(e.detail || 0) / 3;
|
||||
callback(e);
|
||||
}
|
||||
addListener(el, "DOMMouseScroll", listener);
|
||||
addListener(el, "mousewheel", listener);
|
||||
lib.addListener(el, "DOMMouseScroll", listener);
|
||||
lib.addListener(el, "mousewheel", listener);
|
||||
};
|
||||
|
||||
function autoRemoveListener(el, type, callback, timeout)
|
||||
lib.autoremoveListener = function(el, type, callback, timeout)
|
||||
{
|
||||
var listener = function(e)
|
||||
{
|
||||
|
|
@ -150,18 +154,20 @@ function autoRemoveListener(el, type, callback, timeout)
|
|||
}
|
||||
|
||||
var remove = function() {
|
||||
removeListener(el, type, listener);
|
||||
lib.removeListener(el, type, listener);
|
||||
};
|
||||
|
||||
addListener(el, type, listener);
|
||||
lib.addListener(el, type, listener);
|
||||
var timeoutId = setTimeout(remove, timeout);
|
||||
}
|
||||
|
||||
function addTripleClickListener(el, callback)
|
||||
lib.addTripleClickListener = function(el, callback)
|
||||
{
|
||||
addListener(el, "mousedown", function() {
|
||||
autoRemoveListener(el, "mousedown", function() {
|
||||
autoRemoveListener(el, "mousedown", callback, 300);
|
||||
lib.addListener(el, "mousedown", function() {
|
||||
lib.autoremoveListener(el, "mousedown", function() {
|
||||
lib.autoremoveListener(el, "mousedown", callback, 300);
|
||||
}, 300);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue