namespace methods from lib

This commit is contained in:
Fabian Jakobs 2010-04-08 16:06:45 +02:00
commit 971b96c132
4 changed files with 67 additions and 61 deletions

View file

@ -13,7 +13,7 @@ var keys = {
function KeyBinding(element, host) function KeyBinding(element, host)
{ {
addListener(element, "keydown", function(e) lib.addListener(element, "keydown", function(e)
{ {
var key = e.keyCode; var key = e.keyCode;
@ -23,7 +23,7 @@ function KeyBinding(element, host)
if (e.metaKey) if (e.metaKey)
{ {
host.selectAll(); host.selectAll();
return stopEvent(e); return lib.stopEvent(e);
} }
break; break;
@ -33,7 +33,7 @@ function KeyBinding(element, host)
} else { } else {
host.navigateUp(); host.navigateUp();
} }
return stopEvent(e); return lib.stopEvent(e);
case keys.DOWN: case keys.DOWN:
if (e.shiftKey) { if (e.shiftKey) {
@ -41,7 +41,7 @@ function KeyBinding(element, host)
} else { } else {
host.navigateDown(); host.navigateDown();
} }
return stopEvent(e); return lib.stopEvent(e);
case keys.LEFT: case keys.LEFT:
if (e.metaKey && e.shiftKey) { if (e.metaKey && e.shiftKey) {
@ -53,7 +53,7 @@ function KeyBinding(element, host)
} else { } else {
host.navigateLeft(); host.navigateLeft();
} }
return stopEvent(e); return lib.stopEvent(e);
case keys.RIGHT: case keys.RIGHT:
if (e.metaKey && e.shiftKey) { if (e.metaKey && e.shiftKey) {
@ -65,7 +65,7 @@ function KeyBinding(element, host)
} else { } else {
host.navigateRight(); host.navigateRight();
} }
return stopEvent(e); return lib.stopEvent(e);
case keys.POS1: case keys.POS1:
if (e.shiftKey) { if (e.shiftKey) {
@ -73,7 +73,7 @@ function KeyBinding(element, host)
} else { } else {
host.navigateLineStart(); host.navigateLineStart();
} }
return stopEvent(e); return lib.stopEvent(e);
case keys.END: case keys.END:
if (e.shiftKey) { if (e.shiftKey) {
@ -81,19 +81,19 @@ function KeyBinding(element, host)
} else { } else {
host.navigateLineEnd(); host.navigateLineEnd();
} }
return stopEvent(e); return lib.stopEvent(e);
case keys.DELETE: case keys.DELETE:
host.removeRight(); host.removeRight();
return stopEvent(e); return lib.stopEvent(e);
case keys.BACKSPACE: case keys.BACKSPACE:
host.removeLeft(); host.removeLeft();
return stopEvent(e); return lib.stopEvent(e);
case keys.TAB: case keys.TAB:
host.onTextInput(" "); host.onTextInput(" ");
return stopEvent(e); return lib.stopEvent(e);
} }
}); });
}; };
@ -106,13 +106,13 @@ function Editor(doc, renderer)
this.textInput = new TextInput(container, this); this.textInput = new TextInput(container, this);
new KeyBinding(container, this); new KeyBinding(container, this);
addListener(container, "mousedown", bind(this.onMouseDown, this)); lib.addListener(container, "mousedown", lib.bind(this.onMouseDown, this));
addListener(container, "dblclick", bind(this.onMouseDoubleClick, this)); lib.addListener(container, "dblclick", lib.bind(this.onMouseDoubleClick, this));
addMouseWheelListener(container, bind(this.onMouseWheel, this)); lib.addMouseWheelListener(container, lib.bind(this.onMouseWheel, this));
addTripleClickListener(container, bind(this.selectCurrentLine, this)); lib.addTripleClickListener(container, lib.bind(this.selectCurrentLine, this));
this.doc = doc; this.doc = doc;
doc.addChangeListener(bind(this.onDocumentChange, this)); doc.addChangeListener(lib.bind(this.onDocumentChange, this));
renderer.setDocument(doc); renderer.setDocument(doc);
this.cursor = { this.cursor = {
@ -184,10 +184,10 @@ function Editor(doc, renderer)
_self.renderer.scrollCursorIntoView(); _self.renderer.scrollCursorIntoView();
}; };
capture(this.container, onMouseSelection, onMouseSelectionEnd); lib.capture(this.container, onMouseSelection, onMouseSelectionEnd);
var timerId = setInterval(onSelectionInterval, 20 ); var timerId = setInterval(onSelectionInterval, 20 );
return preventDefault(e); return lib.preventDefault(e);
}, },
onMouseDoubleClick : function(e) onMouseDoubleClick : function(e)
@ -233,7 +233,7 @@ function Editor(doc, renderer)
{ {
var delta = e.wheel; var delta = e.wheel;
this.renderer.scrollToY(this.renderer.getScrollTop() - (delta * 15)); this.renderer.scrollToY(this.renderer.getScrollTop() - (delta * 15));
return preventDefault(e); return lib.preventDefault(e);
}, },
getCopyText : function() getCopyText : function()

View file

@ -51,23 +51,23 @@ function TextInput(parentNode, host) {
text.select(); text.select();
} }
addListener(text, "keypress", onTextInput, false); lib.addListener(text, "keypress", onTextInput, false);
addListener(text, "textInput", onTextInput, false); lib.addListener(text, "textInput", onTextInput, false);
addListener(text, "paste", onTextInput, false); lib.addListener(text, "paste", onTextInput, false);
addListener(text, "propertychange", onTextInput, false); lib.addListener(text, "propertychange", onTextInput, false);
addListener(text, "copy", onCopy, false); lib.addListener(text, "copy", onCopy, false);
addListener(text, "cut", onCut, false); lib.addListener(text, "cut", onCut, false);
addListener(text, "compositionstart", onCompositionStart, false); lib.addListener(text, "compositionstart", onCompositionStart, false);
addListener(text, "compositionupdate", onCompositionUpdate, false); lib.addListener(text, "compositionupdate", onCompositionUpdate, false);
addListener(text, "compositionend", onCompositionEnd, false); lib.addListener(text, "compositionend", onCompositionEnd, false);
addListener(text, "blur", function() { lib.addListener(text, "blur", function() {
host.onBlur(); host.onBlur();
}, false); }, false);
addListener(text, "focus", function() { lib.addListener(text, "focus", function() {
host.onFocus(); host.onFocus();
}, false); }, false);

View file

@ -18,7 +18,7 @@
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
var el = document.getElementById("juhu"); var el = document.getElementById("juhu");
addTripleClickListener(el, function() { lib.addTripleClickListener(el, function() {
console.log("triple"); console.log("triple");
}) })

66
lib.js
View file

@ -1,4 +1,8 @@
function addListener(elem, type, callback) { (function() {
window.lib = {};
lib.addListener = function(elem, type, callback) {
if (elem.addEventListener) { if (elem.addEventListener) {
return elem.addEventListener(type, callback, false); 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) { if (elem.removeEventListener) {
return elem.removeEventListener(type, callback, false); 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) { if (elem.innerText !== undefined) {
elem.innerText = text; elem.innerText = text;
} }
@ -29,20 +33,20 @@ function setText(elem, text) {
} }
} }
function stopEvent(e) { lib.stopEvent = function(e) {
stopPropagation(e); lib.stopPropagation(e);
preventDefault(e); lib.preventDefault(e);
return false; return false;
} }
function stopPropagation(e) { lib.stopPropagation = function(e) {
if (e.stopPropagation) if (e.stopPropagation)
e.stopPropagation(); e.stopPropagation();
else else
e.cancelBubble = true; e.cancelBubble = true;
} }
function preventDefault (e) lib.preventDefault = function(e)
{ {
if (e.preventDefault) if (e.preventDefault)
e.preventDefault(); e.preventDefault();
@ -50,7 +54,7 @@ function preventDefault (e)
e.returnValue = false; e.returnValue = false;
} }
inherits = function (ctor, superCtor) { lib.inherits = function(ctor, superCtor) {
var tempCtor = function(){}; var tempCtor = function(){};
tempCtor.prototype = superCtor.prototype; tempCtor.prototype = superCtor.prototype;
ctor.super_ = superCtor.prototype; ctor.super_ = superCtor.prototype;
@ -58,25 +62,25 @@ inherits = function (ctor, superCtor) {
ctor.prototype.constructor = ctor; ctor.prototype.constructor = ctor;
}; };
getInnerWidth = function(element) lib.getInnerWidth = function(element)
{ {
return ( return (
parseInt(computedStyle(element, "paddingLeft")) + parseInt(lib.computedStyle(element, "paddingLeft")) +
parseInt(computedStyle(element, "paddingRight")) + parseInt(lib.computedStyle(element, "paddingRight")) +
element.clientWidth element.clientWidth
); );
}; };
getInnerHeight = function(element) lib.getInnerHeight = function(element)
{ {
return ( return (
parseInt(computedStyle(element, "paddingTop")) + parseInt(lib.computedStyle(element, "paddingTop")) +
parseInt(computedStyle(element, "paddingBottom")) + parseInt(lib.computedStyle(element, "paddingBottom")) +
element.clientHeight element.clientHeight
); );
}; };
computedStyle = function(element, style) lib.computedStyle = function(element, style)
{ {
if (window.getComputedStyle) { if (window.getComputedStyle) {
return (window.getComputedStyle(element, null))[style]; 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 el = document.createElement("div");
var style = el.style; var style = el.style;
@ -101,13 +105,13 @@ scrollbarHeight = function() {
return height; return height;
} }
bind = function(fcn, context) { lib.bind = function(fcn, context) {
return function() { return function() {
return fcn.apply(context, arguments); return fcn.apply(context, arguments);
} }
} }
capture = function(el, eventHandler, releaseCaptureHandler) lib.capture = function(el, eventHandler, releaseCaptureHandler)
{ {
function onMouseMove(e) function onMouseMove(e)
{ {
@ -130,17 +134,17 @@ capture = function(el, eventHandler, releaseCaptureHandler)
document.addEventListener("mouseup", onMouseUp, true); document.addEventListener("mouseup", onMouseUp, true);
} }
function addMouseWheelListener(el, callback) lib.addMouseWheelListener = function(el, callback)
{ {
var listener = function(e) { var listener = function(e) {
e.wheel = (e.wheelDelta) ? e.wheelDelta / 120 : -(e.detail || 0) / 3; e.wheel = (e.wheelDelta) ? e.wheelDelta / 120 : -(e.detail || 0) / 3;
callback(e); callback(e);
} }
addListener(el, "DOMMouseScroll", listener); lib.addListener(el, "DOMMouseScroll", listener);
addListener(el, "mousewheel", listener); lib.addListener(el, "mousewheel", listener);
}; };
function autoRemoveListener(el, type, callback, timeout) lib.autoremoveListener = function(el, type, callback, timeout)
{ {
var listener = function(e) var listener = function(e)
{ {
@ -150,18 +154,20 @@ function autoRemoveListener(el, type, callback, timeout)
} }
var remove = function() { 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); var timeoutId = setTimeout(remove, timeout);
} }
function addTripleClickListener(el, callback) lib.addTripleClickListener = function(el, callback)
{ {
addListener(el, "mousedown", function() { lib.addListener(el, "mousedown", function() {
autoRemoveListener(el, "mousedown", function() { lib.autoremoveListener(el, "mousedown", function() {
autoRemoveListener(el, "mousedown", callback, 300); lib.autoremoveListener(el, "mousedown", callback, 300);
}, 300); }, 300);
}); });
} }
})();