Rename Event listener

Remove hacks
This commit is contained in:
Paulo Veiga
2011-08-21 12:42:00 -03:00
parent 0e854c8942
commit ad13c296ad
42 changed files with 276 additions and 2944 deletions

View File

@@ -93,7 +93,7 @@ web2d.Element.prototype._supportedEvents = ["click","dblclick","mousemove","mous
* The following events types are supported:
*
*/
web2d.Element.prototype.addEventListener = function(type, listener)
web2d.Element.prototype.addEvent = function(type, listener)
{
if (!this._supportedEvents.include(type))
{
@@ -106,7 +106,7 @@ web2d.Element.prototype.addEventListener = function(type, listener)
this._dispatcherByEventType[type] = new web2d.EventDispatcher(this);
var eventListener = this._dispatcherByEventType[type].eventListener;
this._peer.addEventListener(type, eventListener);
this._peer.addEvent(type, eventListener);
}
this._dispatcherByEventType[type].addListener(type, listener);
@@ -122,7 +122,7 @@ web2d.Element.prototype.addEventListener = function(type, listener)
* The listener parameter takes an interface implemented by the user which contains the methods to be called when the event occurs.
* This interace will be invoked passing an event as argument and the 'this' referece in the function will be the element.
*/
web2d.Element.prototype.removeEventListener = function(type, listener)
web2d.Element.prototype.removeEvent = function(type, listener)
{
var dispatcher = this._dispatcherByEventType[type];
if (dispatcher == null)
@@ -134,7 +134,7 @@ web2d.Element.prototype.removeEventListener = function(type, listener)
// If there is not listeners, EventDispatcher must be removed.
if (dispatcher.getListenersCount() <= 0)
{
this._peer.removeEventListener(type, dispatcher.eventListener);
this._peer.removeEvent(type, dispatcher.eventListener);
this._dispatcherByEventType[type] = null;
}
};

View File

@@ -1,25 +1,23 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
web2d.peer.svg.ElementPeer = function(svgElement)
{
web2d.peer.svg.ElementPeer = function(svgElement) {
this._native = svgElement;
this._dblClickListeners = new Hash();
this._size = {width:1,height:1};
this._changeListeners = {};
// http://support.adobe.com/devsup/devsup.nsf/docs/50493.htm
@@ -28,34 +26,28 @@ web2d.peer.svg.ElementPeer = function(svgElement)
web2d.peer.svg.ElementPeer.prototype.svgNamespace = 'http://www.w3.org/2000/svg';
web2d.peer.svg.ElementPeer.prototype.linkNamespace = 'http://www.w3.org/1999/xlink';
web2d.peer.svg.ElementPeer.prototype.setChildren = function(children)
{
web2d.peer.svg.ElementPeer.prototype.setChildren = function(children) {
this._children = children;
};
web2d.peer.svg.ElementPeer.prototype.getChildren = function()
{
web2d.peer.svg.ElementPeer.prototype.getChildren = function() {
var result = this._children;
if (!$defined(result))
{
if (!$defined(result)) {
result = [];
this._children = result;
}
return result;
};
web2d.peer.svg.ElementPeer.prototype.getParent = function()
{
web2d.peer.svg.ElementPeer.prototype.getParent = function() {
return this._parent;
};
web2d.peer.svg.ElementPeer.prototype.setParent = function(parent)
{
web2d.peer.svg.ElementPeer.prototype.setParent = function(parent) {
this._parent = parent;
};
web2d.peer.svg.ElementPeer.prototype.appendChild = function(elementPeer)
{
web2d.peer.svg.ElementPeer.prototype.appendChild = function(elementPeer) {
// Store parent and child relationship.
elementPeer.setParent(this);
var children = this.getChildren();
@@ -69,8 +61,7 @@ web2d.peer.svg.ElementPeer.prototype.appendChild = function(elementPeer)
};
web2d.peer.svg.ElementPeer.prototype.removeChild = function(elementPeer)
{
web2d.peer.svg.ElementPeer.prototype.removeChild = function(elementPeer) {
// Store parent and child relationship.
elementPeer.setParent(null);
var children = this.getChildren();
@@ -81,85 +72,34 @@ web2d.peer.svg.ElementPeer.prototype.removeChild = function(elementPeer)
children.erase(elementPeer);
var newLength = children.length;
if (newLength >= length)
{
if (newLength >= length) {
throw "Could not remove the element.";
}
/*var found = false;
children = children.reject(function(iter)
{
var equals = (iter._native == elementPeer._native);
if (equals)
{
found = true;
}
return equals;
});
// Could found the element ?
if (!found)
{
throw "Could not remove the element.";
}*/
// Append element as a child.
this._native.removeChild(elementPeer._native);
};
/**
* http://www.w3.org/TR/DOM-Level-3-Events/events.html
* http://developer.mozilla.org/en/docs/addEventListener
* http://developer.mozilla.org/en/docs/addEvent
*/
web2d.peer.svg.ElementPeer.prototype.addEventListener = function(type, listener)
{
if (type == 'dblclick')
{
// This is workaround to support double click...
var dblListener = function(e)
{
if (e.detail >= 2)
{
listener.call(this, e);
}
};
web2d.peer.svg.ElementPeer.prototype.addEvent = function(type, listener) {
this._native.addEventListener(type, listener, false);
this._dblClickListeners[listener] = dblListener;
this._native.addEventListener(type, dblListener, false);
} else
{
this._native.addEventListener(type, listener, false);
}
};
web2d.peer.svg.ElementPeer.prototype.removeEventListener = function(type, listener)
{
if (type == 'dblclick')
{
// This is workaround to support double click...
var dblClickListener = this._dblClickListeners[listener];
if (dblClickListener == null)
{
throw "Could not find listener to remove";
}
type = 'click';
this._native.removeEventListener(type, dblClickListener, false);
delete this._dblClickListeners[listener];
} else
{
this._native.removeEventListener(type, listener, false);
}
web2d.peer.svg.ElementPeer.prototype.removeEvent = function(type, listener) {
this._native.removeEventListener(type, listener, false);
};
web2d.peer.svg.ElementPeer.prototype.setSize = function(width, height)
{
if ($defined(width) && this._size.width != parseInt(width))
{
web2d.peer.svg.ElementPeer.prototype.setSize = function(width, height) {
if ($defined(width) && this._size.width != parseInt(width)) {
this._size.width = parseInt(width);
this._native.setAttribute('width', parseInt(width));
}
if ($defined(height) && this._size.height != parseInt(height))
{
if ($defined(height) && this._size.height != parseInt(height)) {
this._size.height = parseInt(height);
this._native.setAttribute('height', parseInt(height));
}
@@ -167,32 +107,26 @@ web2d.peer.svg.ElementPeer.prototype.setSize = function(width, height)
web2d.peer.utils.EventUtils.broadcastChangeEvent(this, "strokeStyle");
};
web2d.peer.svg.ElementPeer.prototype.getSize = function()
{
web2d.peer.svg.ElementPeer.prototype.getSize = function() {
return {width:this._size.width,height:this._size.height};
};
web2d.peer.svg.ElementPeer.prototype.setFill = function(color, opacity)
{
if ($defined(color))
{
web2d.peer.svg.ElementPeer.prototype.setFill = function(color, opacity) {
if ($defined(color)) {
this._native.setAttribute('fill', color);
}
if ($defined(opacity))
{
if ($defined(opacity)) {
this._native.setAttribute('fill-opacity', opacity);
}
};
web2d.peer.svg.ElementPeer.prototype.getFill = function()
{
web2d.peer.svg.ElementPeer.prototype.getFill = function() {
var color = this._native.getAttribute('fill');
var opacity = this._native.getAttribute('fill-opacity');
return {color:color, opacity:Number(opacity)};
};
web2d.peer.svg.ElementPeer.prototype.getStroke = function()
{
web2d.peer.svg.ElementPeer.prototype.getStroke = function() {
var vmlStroke = this._native;
var color = vmlStroke.getAttribute('stroke');
var dashstyle = this._stokeStyle;
@@ -202,18 +136,14 @@ web2d.peer.svg.ElementPeer.prototype.getStroke = function()
};
web2d.peer.svg.ElementPeer.prototype.__stokeStyleToStrokDasharray = {solid:[],dot:[1,3],dash:[4,3],longdash:[10,2],dashdot:[5,3,1,3]};
web2d.peer.svg.ElementPeer.prototype.setStroke = function(width, style, color, opacity)
{
if ($defined(width))
{
web2d.peer.svg.ElementPeer.prototype.setStroke = function(width, style, color, opacity) {
if ($defined(width)) {
this._native.setAttribute('stroke-width', width + "px");
}
if ($defined(color))
{
if ($defined(color)) {
this._native.setAttribute('stroke', color);
}
if ($defined(style))
{
if ($defined(style)) {
// Scale the dash array in order to be equal to VML. In VML, stroke style doesn't scale.
var dashArrayPoints = this.__stokeStyleToStrokDasharray[style];
var scale = 1 / web2d.peer.utils.TransformUtil.workoutScale(this).width;
@@ -222,8 +152,7 @@ web2d.peer.svg.ElementPeer.prototype.setStroke = function(width, style, color, o
strokeWidth = parseFloat(strokeWidth);
var scaledPoints = [];
for (var i = 0; i < dashArrayPoints.length; i++)
{
for (var i = 0; i < dashArrayPoints.length; i++) {
// VML scale the stroke based on the stroke width.
scaledPoints[i] = dashArrayPoints[i] * strokeWidth;
@@ -235,53 +164,43 @@ web2d.peer.svg.ElementPeer.prototype.setStroke = function(width, style, color, o
this._stokeStyle = style;
}
if ($defined(opacity))
{
if ($defined(opacity)) {
this._native.setAttribute('stroke-opacity', opacity);
}
};
/*
* style='visibility: visible'
*/
web2d.peer.svg.ElementPeer.prototype.setVisibility = function(isVisible)
{
* style='visibility: visible'
*/
web2d.peer.svg.ElementPeer.prototype.setVisibility = function(isVisible) {
this._native.setAttribute('visibility', (isVisible) ? 'visible' : 'hidden');
};
web2d.peer.svg.ElementPeer.prototype.isVisible = function()
{
web2d.peer.svg.ElementPeer.prototype.isVisible = function() {
var visibility = this._native.getAttribute('visibility');
return !(visibility == 'hidden');
};
web2d.peer.svg.ElementPeer.prototype.updateStrokeStyle = function()
{
web2d.peer.svg.ElementPeer.prototype.updateStrokeStyle = function() {
var strokeStyle = this._stokeStyle;
if (this.getParent())
{
if (strokeStyle && strokeStyle != 'solid')
{
if (this.getParent()) {
if (strokeStyle && strokeStyle != 'solid') {
this.setStroke(null, strokeStyle);
}
}
};
web2d.peer.svg.ElementPeer.prototype.attachChangeEventListener = function(type, listener)
{
web2d.peer.svg.ElementPeer.prototype.attachChangeEventListener = function(type, listener) {
var listeners = this.getChangeEventListeners(type);
if (!$defined(listener))
{
if (!$defined(listener)) {
throw "Listener can not be null";
}
listeners.push(listener);
};
web2d.peer.svg.ElementPeer.prototype.getChangeEventListeners = function(type)
{
web2d.peer.svg.ElementPeer.prototype.getChangeEventListeners = function(type) {
var listeners = this._changeListeners[type];
if (!$defined(listeners))
{
if (!$defined(listeners)) {
listeners = [];
this._changeListeners[type] = listeners;
}
@@ -291,20 +210,17 @@ web2d.peer.svg.ElementPeer.prototype.getChangeEventListeners = function(type)
/**
* Move element to the front
*/
web2d.peer.svg.ElementPeer.prototype.moveToFront = function()
{
web2d.peer.svg.ElementPeer.prototype.moveToFront = function() {
this._native.parentNode.appendChild(this._native);
};
/**
* Move element to the back
*/
web2d.peer.svg.ElementPeer.prototype.moveToBack = function()
{
web2d.peer.svg.ElementPeer.prototype.moveToBack = function() {
this._native.parentNode.insertBefore(this._native, this._native.parentNode.firstChild);
};
web2d.peer.svg.ElementPeer.prototype.setCursor = function(type)
{
web2d.peer.svg.ElementPeer.prototype.setCursor = function(type) {
this._native.style.cursor = type;
};

View File

@@ -239,8 +239,8 @@ function testElementEventHandling()
var listener = function() { /* Dummy event listener */
};
elem.addEventListener(eventType, listener);
elem.removeEventListener(eventType, listener);
elem.addEvent(eventType, listener);
elem.removeEvent(eventType, listener);
};
testEventHandling(workspace, null, 'mouseover');

View File

@@ -59,13 +59,13 @@ function testEventTest()
alert("My Listener 2");
};
workspace.addEventListener("click", firstListener);
workspace.addEvent("click", firstListener);
// Handle gracefully invalid event types...
var catchException = false
try
{
workspace.addEventListener("click2", firstListener);
workspace.addEvent("click2", firstListener);
} catch(e)
{
catchException = true;
@@ -77,7 +77,7 @@ function testEventTest()
catchException = false
try
{
workspace.removeEventListener("click", secondListener);
workspace.removeEvent("click", secondListener);
} catch(e)
{
catchException = true;
@@ -85,13 +85,13 @@ function testEventTest()
assert("Invalid remove operation seems to be broken", catchException);
//// Remove a valid listener.
workspace.removeEventListener("click", firstListener);
workspace.removeEvent("click", firstListener);
//// It has been removed?
catchException = false;
try
{
workspace.removeEventListener("click", firstListener);
workspace.removeEvent("click", firstListener);
} catch(e)
{
catchException = true;
@@ -101,16 +101,16 @@ function testEventTest()
// Check multiple registation of a type ...
//// Add two listeners ...
workspace.addEventListener("dblclick", firstListener);
workspace.addEventListener("dblclick", secondListener);
workspace.addEvent("dblclick", firstListener);
workspace.addEvent("dblclick", secondListener);
//// Remove it ...
workspace.removeEventListener("dblclick", firstListener);
workspace.removeEventListener("dblclick", secondListener);
workspace.removeEvent("dblclick", firstListener);
workspace.removeEvent("dblclick", secondListener);
/// Check multiple registration on different types ...
workspace.addEventListener("click", firstListener);
workspace.addEventListener("mouseover", secondListener);
workspace.addEvent("click", firstListener);
workspace.addEvent("mouseover", secondListener);
}

View File

@@ -73,10 +73,10 @@
this._enable = !this._enable;
if (this._enable)
{
this._element.addEventListener(this._type, this._listener);
this._element.addEvent(this._type, this._listener);
} else
{
this._element.removeEventListener(this._type, this._listener);
this._element.removeEvent(this._type, this._listener);
}
return this._enable;
};
@@ -95,7 +95,7 @@
alert("Listener #:" + count);
};
this._listeners.push(listener);
this._element.addEventListener(this._type, listener);
this._element.addEvent(this._type, listener);
}
@@ -109,7 +109,7 @@
if (this._listeners.length > 0)
{
var listener = this._listeners.pop();
this._element.removeEventListener(this._type, listener);
this._element.removeEvent(this._type, listener);
}
}

View File

@@ -135,9 +135,9 @@
var listener = function(e) {
alert("Click event on:" + this.getType())
};
group.addEventListener("click", listener);
elipseLeft.addEventListener("click", listener);
elipseRight.addEventListener("click", listener);
group.addEvent("click", listener);
elipseLeft.addEvent("click", listener);
elipseRight.addEvent("click", listener);
workspace.addItAsChildTo($("groupEventsContainer"));
}
@@ -301,7 +301,7 @@
group.setSize(100, 100);
group.setPosition(0, 0);
group.setCoordSize(100, 100);
group.addEventListener("mouseover", function() {
group.addEvent("mouseover", function() {
alert("Mouse Over Group");
});
workspace.appendChild(group);
@@ -309,7 +309,7 @@
elipseOuter = new web2d.Elipse();
elipseOuter.setPosition(50, 50);
elipseOuter.setSize(50, 50);
group.addEventListener("mouseover", function() {
group.addEvent("mouseover", function() {
alert("Mouse Over elipseOuter");
});
group.appendChild(elipseOuter);

View File

@@ -112,17 +112,17 @@
workspace.appendChild(nodeGroup);
// Add behaviour ...
inerRect.addEventListener("mouseover", function() {
inerRect.addEvent("mouseover", function() {
outerRect.setVisibility(true);
});
inerRect.addEventListener("mouseout", function() {
inerRect.addEvent("mouseout", function() {
if (!nodeGroup._drag)
{
outerRect.setVisibility(false);
}
});
nodeGroup.addEventListener("mousedown", function(e)
nodeGroup.addEvent("mousedown", function(e)
{
var shadowGroup = new web2d.Group();
shadowGroup.setSize(200, 60);
@@ -165,7 +165,7 @@
shadowGroup.setPosition(posx - 50, posy - 150);
};
}
workspace.addEventListener("mousemove", workspace._moveFunction);
workspace.addEvent("mousemove", workspace._moveFunction);
var mouseUp = function(e)
{
@@ -175,11 +175,11 @@
nodeGroup.setPosition(pos.x, pos.y);
nodeGroup._drag = false;
outerRect.setVisibility(true);
workspace.removeEventListener("mousemove", workspace._moveFunction);
workspace.removeEventListener("mouseup", mouseUp);
workspace.removeEvent("mousemove", workspace._moveFunction);
workspace.removeEvent("mouseup", mouseUp);
};
workspace.addEventListener("mouseup", mouseUp);
workspace.addEvent("mouseup", mouseUp);
});

View File

@@ -246,7 +246,7 @@
var rect = new web2d.Rect(0, rectAttributes);
rect.setPosition(120, 20);
workspace.appendChild(rect);
rect.addEventListener("mouseover", function() {
rect.addEvent("mouseover", function() {
alert("Mouse Over");
});

View File

@@ -84,9 +84,9 @@ web2d.Loader =
web2d.Loader.checkLoaded(filename);
}
if(typeof(js.addEventListener) != 'undefined') {
if(typeof(js.addEvent) != 'undefined') {
/* The FF, Chrome, Safari, Opera way */
js.addEventListener('load',calltheCBcmn,false);
js.addEvent('load',calltheCBcmn,false);
}
else {
/* The MS IE 8+ way (may work with others - I dunno)*/