wisemapping-open-source/web2d/src/main/javascript/peer/svg/ElementPeer.js

227 lines
7.5 KiB
JavaScript
Raw Normal View History

/*
2011-08-21 17:42:00 +02:00
* 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) {
2009-06-07 20:59:43 +02:00
this._native = svgElement;
this._size = {width:1,height:1};
this._changeListeners = {};
// http://support.adobe.com/devsup/devsup.nsf/docs/50493.htm
};
web2d.peer.svg.ElementPeer.prototype.svgNamespace = 'http://www.w3.org/2000/svg';
web2d.peer.svg.ElementPeer.prototype.linkNamespace = 'http://www.w3.org/1999/xlink';
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.setChildren = function(children) {
2009-06-07 20:59:43 +02:00
this._children = children;
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.getChildren = function() {
2009-06-07 20:59:43 +02:00
var result = this._children;
2011-08-21 17:42:00 +02:00
if (!$defined(result)) {
2009-06-07 20:59:43 +02:00
result = [];
this._children = result;
}
return result;
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.getParent = function() {
2009-06-07 20:59:43 +02:00
return this._parent;
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.setParent = function(parent) {
2009-06-07 20:59:43 +02:00
this._parent = parent;
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.appendChild = function(elementPeer) {
2009-06-07 20:59:43 +02:00
// Store parent and child relationship.
elementPeer.setParent(this);
var children = this.getChildren();
children.include(elementPeer);
// Append element as a child.
this._native.appendChild(elementPeer._native);
// Broadcast events ...
web2d.peer.utils.EventUtils.broadcastChangeEvent(this, "strokeStyle");
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.removeChild = function(elementPeer) {
2009-06-07 20:59:43 +02:00
// Store parent and child relationship.
elementPeer.setParent(null);
var children = this.getChildren();
// Remove from children array ...
var length = children.length;
2011-07-26 20:07:53 +02:00
children.erase(elementPeer);
2009-06-07 20:59:43 +02:00
var newLength = children.length;
2011-08-21 17:42:00 +02:00
if (newLength >= length) {
2009-06-07 20:59:43 +02:00
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
2011-08-21 17:42:00 +02:00
* http://developer.mozilla.org/en/docs/addEvent
2009-06-07 20:59:43 +02:00
*/
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.addEvent = function(type, listener) {
this._native.addEventListener(type, listener, false);
2009-06-07 20:59:43 +02:00
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.removeEvent = function(type, listener) {
this._native.removeEventListener(type, listener, false);
2009-06-07 20:59:43 +02:00
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.setSize = function(width, height) {
if ($defined(width) && this._size.width != parseInt(width)) {
2009-06-07 20:59:43 +02:00
this._size.width = parseInt(width);
this._native.setAttribute('width', parseInt(width));
}
2011-08-21 17:42:00 +02:00
if ($defined(height) && this._size.height != parseInt(height)) {
2009-06-07 20:59:43 +02:00
this._size.height = parseInt(height);
this._native.setAttribute('height', parseInt(height));
}
web2d.peer.utils.EventUtils.broadcastChangeEvent(this, "strokeStyle");
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.getSize = function() {
2009-06-07 20:59:43 +02:00
return {width:this._size.width,height:this._size.height};
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.setFill = function(color, opacity) {
if ($defined(color)) {
2009-06-07 20:59:43 +02:00
this._native.setAttribute('fill', color);
}
2011-08-21 17:42:00 +02:00
if ($defined(opacity)) {
2009-06-07 20:59:43 +02:00
this._native.setAttribute('fill-opacity', opacity);
}
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.getFill = function() {
2009-06-07 20:59:43 +02:00
var color = this._native.getAttribute('fill');
var opacity = this._native.getAttribute('fill-opacity');
return {color:color, opacity:Number(opacity)};
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.getStroke = function() {
2009-06-07 20:59:43 +02:00
var vmlStroke = this._native;
var color = vmlStroke.getAttribute('stroke');
var dashstyle = this._stokeStyle;
var opacity = vmlStroke.getAttribute('stroke-opacity');
var width = vmlStroke.getAttribute('stroke-width');
return {color: color, style: dashstyle, opacity: opacity, width: width};
};
web2d.peer.svg.ElementPeer.prototype.__stokeStyleToStrokDasharray = {solid:[],dot:[1,3],dash:[4,3],longdash:[10,2],dashdot:[5,3,1,3]};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.setStroke = function(width, style, color, opacity) {
if ($defined(width)) {
2009-06-07 20:59:43 +02:00
this._native.setAttribute('stroke-width', width + "px");
}
2011-08-21 17:42:00 +02:00
if ($defined(color)) {
2009-06-07 20:59:43 +02:00
this._native.setAttribute('stroke', color);
}
2011-08-21 17:42:00 +02:00
if ($defined(style)) {
2009-06-07 20:59:43 +02:00
// 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;
var strokeWidth = this._native.getAttribute('stroke-width');
strokeWidth = parseFloat(strokeWidth);
var scaledPoints = [];
2011-08-21 17:42:00 +02:00
for (var i = 0; i < dashArrayPoints.length; i++) {
2009-06-07 20:59:43 +02:00
// VML scale the stroke based on the stroke width.
scaledPoints[i] = dashArrayPoints[i] * strokeWidth;
// Scale the points based on the scale.
scaledPoints[i] = (scaledPoints[i] * scale) + "px";
}
// this._native.setAttribute('stroke-dasharray', scaledPoints);
this._stokeStyle = style;
}
2011-08-21 17:42:00 +02:00
if ($defined(opacity)) {
2009-06-07 20:59:43 +02:00
this._native.setAttribute('stroke-opacity', opacity);
}
};
/*
2011-08-21 17:42:00 +02:00
* style='visibility: visible'
*/
web2d.peer.svg.ElementPeer.prototype.setVisibility = function(isVisible) {
2009-06-07 20:59:43 +02:00
this._native.setAttribute('visibility', (isVisible) ? 'visible' : 'hidden');
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.isVisible = function() {
2009-06-07 20:59:43 +02:00
var visibility = this._native.getAttribute('visibility');
return !(visibility == 'hidden');
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.updateStrokeStyle = function() {
2009-06-07 20:59:43 +02:00
var strokeStyle = this._stokeStyle;
2011-08-21 17:42:00 +02:00
if (this.getParent()) {
if (strokeStyle && strokeStyle != 'solid') {
2009-06-07 20:59:43 +02:00
this.setStroke(null, strokeStyle);
}
}
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.attachChangeEventListener = function(type, listener) {
2009-06-07 20:59:43 +02:00
var listeners = this.getChangeEventListeners(type);
2011-08-21 17:42:00 +02:00
if (!$defined(listener)) {
2009-06-07 20:59:43 +02:00
throw "Listener can not be null";
}
listeners.push(listener);
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.getChangeEventListeners = function(type) {
2009-06-07 20:59:43 +02:00
var listeners = this._changeListeners[type];
2011-08-21 17:42:00 +02:00
if (!$defined(listeners)) {
2009-06-07 20:59:43 +02:00
listeners = [];
this._changeListeners[type] = listeners;
}
return listeners;
};
/**
* Move element to the front
*/
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.moveToFront = function() {
2009-06-07 20:59:43 +02:00
this._native.parentNode.appendChild(this._native);
};
/**
* Move element to the back
*/
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.moveToBack = function() {
2009-06-07 20:59:43 +02:00
this._native.parentNode.insertBefore(this._native, this._native.parentNode.firstChild);
};
2011-08-21 17:42:00 +02:00
web2d.peer.svg.ElementPeer.prototype.setCursor = function(type) {
2009-06-07 20:59:43 +02:00
this._native.style.cursor = type;
};