2009-06-11 19:43:45 +02:00
|
|
|
/*
|
2011-07-27 18:44:09 +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.
|
2009-07-09 22:06:08 +02:00
|
|
|
*/
|
2009-06-11 19:43:45 +02:00
|
|
|
|
2009-06-07 20:59:43 +02:00
|
|
|
core.Utils =
|
|
|
|
{
|
2011-07-27 18:44:09 +02:00
|
|
|
escapeInvalidTags: function (text) {
|
2009-06-07 20:59:43 +02:00
|
|
|
//todo:Pablo. scape invalid tags in a text
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
/*
|
|
|
|
Function: $defined
|
|
|
|
Returns true if the passed in value/object is defined, that means is not null or undefined.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
obj - object to inspect
|
|
|
|
*/
|
|
|
|
function $defined(obj) {
|
|
|
|
return (obj != undefined);
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2009-06-07 20:59:43 +02:00
|
|
|
/**
|
|
|
|
* http://kevlindev.com/tutorials/javascript/inheritance/index.htm
|
|
|
|
* A function used to extend one class with another
|
|
|
|
*
|
|
|
|
* @param {Object} subClass
|
2009-07-09 22:06:08 +02:00
|
|
|
* The inheriting class, or subclass
|
2009-06-07 20:59:43 +02:00
|
|
|
* @param {Object} baseClass
|
2009-07-09 22:06:08 +02:00
|
|
|
* The class from which to inherit
|
2009-06-07 20:59:43 +02:00
|
|
|
*/
|
|
|
|
objects = {};
|
|
|
|
objects.extend = function(subClass, baseClass) {
|
|
|
|
function inheritance() {
|
|
|
|
}
|
2009-07-09 22:06:08 +02:00
|
|
|
|
2009-06-07 20:59:43 +02:00
|
|
|
inheritance.prototype = baseClass.prototype;
|
|
|
|
|
|
|
|
subClass.prototype = new inheritance();
|
|
|
|
subClass.prototype.constructor = subClass;
|
|
|
|
subClass.baseConstructor = baseClass;
|
|
|
|
subClass.superClass = baseClass.prototype;
|
|
|
|
};
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
core.assert = function(assert, message) {
|
|
|
|
if (!assert) {
|
2009-06-07 20:59:43 +02:00
|
|
|
var stack;
|
2011-07-27 18:44:09 +02:00
|
|
|
try {
|
2009-06-07 20:59:43 +02:00
|
|
|
null.eval();
|
2011-07-27 18:44:09 +02:00
|
|
|
} catch(e) {
|
2009-06-07 20:59:43 +02:00
|
|
|
stack = e;
|
|
|
|
}
|
2010-12-13 15:07:20 +01:00
|
|
|
wLogger.error(message + "," + stack);
|
|
|
|
// core.Logger.logError(message + "," + stack);
|
2009-06-07 20:59:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
Math.sign = function(value) {
|
2009-06-07 20:59:43 +02:00
|
|
|
return (value >= 0) ? 1 : -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Extensions ....
|
|
|
|
function $import(src) {
|
|
|
|
var scriptElem = document.createElement('script');
|
|
|
|
scriptElem.setAttribute('src', src);
|
|
|
|
scriptElem.setAttribute('type', 'text/javascript');
|
|
|
|
document.getElementsByTagName('head')[0].appendChild(scriptElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the mouse position.
|
|
|
|
*/
|
2011-07-27 18:44:09 +02:00
|
|
|
core.Utils.getMousePosition = function(event) {
|
2009-06-07 20:59:43 +02:00
|
|
|
var xcoord = -1;
|
|
|
|
var ycoord = -1;
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
if (!$defined(event)) {
|
|
|
|
if ($defined(window.event)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
//Internet Explorer
|
|
|
|
event = window.event;
|
|
|
|
} else {
|
|
|
|
//total failure, we have no way of referencing the event
|
|
|
|
throw "Could not obtain mouse position";
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(event.$extended)) {
|
2010-11-20 23:43:54 +01:00
|
|
|
event = event.event;
|
|
|
|
}
|
2009-06-07 20:59:43 +02:00
|
|
|
if (typeof( event.pageX ) == 'number') {
|
|
|
|
//most browsers
|
|
|
|
xcoord = event.pageX;
|
|
|
|
ycoord = event.pageY;
|
|
|
|
} else if (typeof( event.clientX ) == 'number') {
|
|
|
|
//Internet Explorer and older browsers
|
|
|
|
//other browsers provide this, but follow the pageX/Y branch
|
|
|
|
xcoord = event.clientX;
|
|
|
|
ycoord = event.clientY;
|
|
|
|
var badOldBrowser = ( window.navigator.userAgent.indexOf('Opera') + 1 ) ||
|
2011-07-27 18:44:09 +02:00
|
|
|
( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 ) ||
|
|
|
|
( navigator.vendor == 'KDE' );
|
2009-06-07 20:59:43 +02:00
|
|
|
if (!badOldBrowser) {
|
|
|
|
if (document.body && ( document.body.scrollLeft || document.body.scrollTop )) {
|
|
|
|
//IE 4, 5 & 6 (in non-standards compliant mode)
|
|
|
|
xcoord += document.body.scrollLeft;
|
|
|
|
ycoord += document.body.scrollTop;
|
|
|
|
} else if (document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop )) {
|
|
|
|
//IE 6 (in standards compliant mode)
|
|
|
|
xcoord += document.documentElement.scrollLeft;
|
|
|
|
ycoord += document.documentElement.scrollTop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw "Could not obtain mouse position";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {x:xcoord,y:ycoord};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the position of the passed element.
|
|
|
|
*/
|
2011-07-27 18:44:09 +02:00
|
|
|
core.Utils.workOutDivElementPosition = function(divElement) {
|
2009-06-07 20:59:43 +02:00
|
|
|
var curleft = 0;
|
|
|
|
var curtop = 0;
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(divElement.offsetParent)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
curleft = divElement.offsetLeft;
|
|
|
|
curtop = divElement.offsetTop;
|
|
|
|
while (divElement = divElement.offsetParent) {
|
|
|
|
curleft += divElement.offsetLeft;
|
|
|
|
curtop += divElement.offsetTop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {x:curleft,y:curtop};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
core.Utils.innerXML = function(/*Node*/node) {
|
|
|
|
// summary:
|
|
|
|
// Implementation of MS's innerXML function.
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(node.innerXML)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
return node.innerXML;
|
|
|
|
// string
|
2011-07-27 18:44:09 +02:00
|
|
|
} else if ($defined(node.xml)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
return node.xml;
|
|
|
|
// string
|
2011-07-27 18:44:09 +02:00
|
|
|
} else if ($defined(XMLSerializer)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
return (new XMLSerializer()).serializeToString(node);
|
|
|
|
// string
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
core.Utils.createDocument = function() {
|
|
|
|
// summary:
|
|
|
|
// cross-browser implementation of creating an XML document object.
|
|
|
|
var doc = null;
|
|
|
|
var _document = window.document;
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(window.ActiveXObject)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
var prefixes = [ "MSXML2", "Microsoft", "MSXML", "MSXML3" ];
|
|
|
|
for (var i = 0; i < prefixes.length; i++) {
|
|
|
|
try {
|
|
|
|
doc = new ActiveXObject(prefixes[i] + ".XMLDOM");
|
|
|
|
} catch(e) { /* squelch */
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(doc)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ((_document.implementation) &&
|
2011-07-27 18:44:09 +02:00
|
|
|
(_document.implementation.createDocument)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
doc = _document.implementation.createDocument("", "", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
// DOMDocument
|
|
|
|
};
|
|
|
|
|
|
|
|
core.Utils.createDocumentFromText = function(/*string*/str, /*string?*/mimetype) {
|
|
|
|
// summary:
|
|
|
|
// attempts to create a Document object based on optional mime-type,
|
|
|
|
// using str as the contents of the document
|
2011-07-27 18:44:09 +02:00
|
|
|
if (!$defined(mimetype)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
mimetype = "text/xml";
|
|
|
|
}
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(window.DOMParser)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
var parser = new DOMParser();
|
|
|
|
return parser.parseFromString(str, mimetype);
|
|
|
|
// DOMDocument
|
2011-07-27 18:44:09 +02:00
|
|
|
} else if ($defined(window.ActiveXObject)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
var domDoc = core.Utils.createDocument();
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(domDoc)) {
|
2009-06-07 20:59:43 +02:00
|
|
|
domDoc.async = false;
|
|
|
|
domDoc.loadXML(str);
|
|
|
|
return domDoc;
|
|
|
|
// DOMDocument
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2010-12-13 15:07:20 +01:00
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
core.Utils.calculateRelationShipPointCoordinates = function(topic, controlPoint) {
|
2010-12-13 15:07:20 +01:00
|
|
|
var size = topic.getSize();
|
|
|
|
var position = topic.getPosition();
|
2011-07-27 18:44:09 +02:00
|
|
|
var m = (position.y - controlPoint.y) / (position.x - controlPoint.x);
|
2010-12-13 15:07:20 +01:00
|
|
|
var y,x;
|
|
|
|
var gap = 5;
|
2011-07-27 18:44:09 +02:00
|
|
|
if (controlPoint.y > position.y + (size.height / 2)) {
|
|
|
|
y = position.y + (size.height / 2) + gap;
|
|
|
|
x = position.x - ((position.y - y) / m);
|
|
|
|
if (x > position.x + (size.width / 2)) {
|
|
|
|
x = position.x + (size.width / 2);
|
|
|
|
} else if (x < position.x - (size.width / 2)) {
|
|
|
|
x = position.x - (size.width / 2);
|
2010-12-13 15:07:20 +01:00
|
|
|
}
|
2011-07-27 18:44:09 +02:00
|
|
|
} else if (controlPoint.y < position.y - (size.height / 2)) {
|
|
|
|
y = position.y - (size.height / 2) - gap;
|
|
|
|
x = position.x - ((position.y - y) / m);
|
|
|
|
if (x > position.x + (size.width / 2)) {
|
|
|
|
x = position.x + (size.width / 2);
|
|
|
|
} else if (x < position.x - (size.width / 2)) {
|
|
|
|
x = position.x - (size.width / 2);
|
2010-12-13 15:07:20 +01:00
|
|
|
}
|
2011-07-27 18:44:09 +02:00
|
|
|
} else if (controlPoint.x < (position.x - size.width / 2)) {
|
|
|
|
x = position.x - (size.width / 2) - gap;
|
|
|
|
y = position.y - (m * (position.x - x));
|
|
|
|
} else {
|
|
|
|
x = position.x + (size.width / 2) + gap;
|
|
|
|
y = position.y - (m * (position.x - x));
|
2010-12-13 15:07:20 +01:00
|
|
|
}
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
return new core.Point(x, y);
|
2010-12-13 15:07:20 +01:00
|
|
|
};
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
core.Utils.calculateDefaultControlPoints = function(srcPos, tarPos) {
|
|
|
|
var y = srcPos.y - tarPos.y;
|
|
|
|
var x = srcPos.x - tarPos.x;
|
|
|
|
var m = y / x;
|
|
|
|
var l = Math.sqrt(y * y + x * x) / 3;
|
|
|
|
var fix = 1;
|
|
|
|
if (srcPos.x > tarPos.x) {
|
|
|
|
fix = -1;
|
2010-12-13 15:07:20 +01:00
|
|
|
}
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
var x1 = srcPos.x + Math.sqrt(l * l / (1 + (m * m))) * fix;
|
|
|
|
var y1 = m * (x1 - srcPos.x) + srcPos.y;
|
|
|
|
var x2 = tarPos.x + Math.sqrt(l * l / (1 + (m * m))) * fix * -1;
|
|
|
|
var y2 = m * (x2 - tarPos.x) + tarPos.y;
|
|
|
|
|
|
|
|
return [new core.Point(-srcPos.x + x1, -srcPos.y + y1),new core.Point(-tarPos.x + x2, -tarPos.y + y2)];
|
2011-02-09 15:29:09 +01:00
|
|
|
};
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
core.Utils.setVisibilityAnimated = function(elems, isVisible, doneFn) {
|
2011-02-09 15:29:09 +01:00
|
|
|
core.Utils.animateVisibility(elems, isVisible, doneFn);
|
|
|
|
};
|
2011-07-27 18:44:09 +02:00
|
|
|
core.Utils.setChildrenVisibilityAnimated = function(rootElem, isVisible) {
|
2011-02-09 15:29:09 +01:00
|
|
|
var children = core.Utils._addInnerChildrens(rootElem);
|
|
|
|
core.Utils.animateVisibility(children, isVisible);
|
|
|
|
};
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
core.Utils.animateVisibility = function (elems, isVisible, doneFn) {
|
|
|
|
var _fadeEffect = null;
|
|
|
|
var _opacity = (isVisible ? 0 : 1);
|
|
|
|
if (isVisible) {
|
|
|
|
elems.forEach(function(child, index) {
|
|
|
|
if ($defined(child)) {
|
|
|
|
child.setOpacity(_opacity);
|
|
|
|
child.setVisibility(isVisible);
|
|
|
|
}
|
|
|
|
});
|
2011-02-09 15:29:09 +01:00
|
|
|
}
|
2011-07-27 18:44:09 +02:00
|
|
|
var fadeEffect = function(index) {
|
2011-02-09 15:29:09 +01:00
|
|
|
var step = 10;
|
2011-07-27 18:44:09 +02:00
|
|
|
if ((_opacity <= 0 && !isVisible) || (_opacity >= 1 && isVisible)) {
|
2011-02-09 15:29:09 +01:00
|
|
|
$clear(_fadeEffect);
|
|
|
|
_fadeEffect = null;
|
2011-07-27 18:44:09 +02:00
|
|
|
elems.forEach(function(child, index) {
|
|
|
|
if ($defined(child)) {
|
2011-04-16 22:41:06 +02:00
|
|
|
child.setVisibility(isVisible);
|
|
|
|
}
|
2011-02-09 15:29:09 +01:00
|
|
|
|
|
|
|
});
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(doneFn))
|
2011-02-09 15:29:09 +01:00
|
|
|
doneFn.attempt();
|
|
|
|
}
|
2011-07-27 18:44:09 +02:00
|
|
|
else {
|
2011-02-09 15:29:09 +01:00
|
|
|
var fix = 1;
|
2011-07-27 18:44:09 +02:00
|
|
|
if (isVisible) {
|
2011-02-09 15:29:09 +01:00
|
|
|
fix = -1;
|
|
|
|
}
|
2011-07-27 18:44:09 +02:00
|
|
|
_opacity -= (1 / step) * fix;
|
|
|
|
elems.forEach(function(child, index) {
|
|
|
|
if ($defined(child)) {
|
2011-04-16 22:41:06 +02:00
|
|
|
child.setOpacity(_opacity);
|
|
|
|
}
|
2011-02-09 15:29:09 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2011-07-27 18:44:09 +02:00
|
|
|
_fadeEffect = fadeEffect.periodical(10);
|
2011-02-09 15:29:09 +01:00
|
|
|
};
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
core.Utils.animatePosition = function (elems, doneFn, designer) {
|
2011-04-12 14:59:03 +02:00
|
|
|
var _moveEffect = null;
|
|
|
|
var i = 10;
|
|
|
|
var step = 10;
|
2011-07-27 18:44:09 +02:00
|
|
|
var moveEffect = function () {
|
|
|
|
if (i > 0) {
|
2011-04-12 14:59:03 +02:00
|
|
|
var keys = elems.keys();
|
2011-07-27 18:44:09 +02:00
|
|
|
for (var j = 0; j < keys.length; j++) {
|
2011-04-12 14:59:03 +02:00
|
|
|
var id = keys[j];
|
|
|
|
var mod = elems.get(id);
|
|
|
|
var allTopics = designer._getTopics();
|
2011-07-27 18:44:09 +02:00
|
|
|
var currentTopic = allTopics.filter(function(node) {
|
|
|
|
return node.getId() == id;
|
2011-04-12 14:59:03 +02:00
|
|
|
})[0];
|
2011-07-27 18:44:09 +02:00
|
|
|
var xStep = (mod.originalPos.x - mod.newPos.x) / step;
|
|
|
|
var yStep = (mod.originalPos.y - mod.newPos.y) / step;
|
2011-04-12 14:59:03 +02:00
|
|
|
var newPos = currentTopic.getPosition().clone();
|
2011-07-27 18:44:09 +02:00
|
|
|
newPos.x += xStep;
|
|
|
|
newPos.y += yStep;
|
2011-04-12 14:59:03 +02:00
|
|
|
currentTopic.setPosition(newPos, false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$clear(_moveEffect);
|
|
|
|
var keys = elems.keys();
|
2011-07-27 18:44:09 +02:00
|
|
|
for (var j = 0; j < keys.length; j++) {
|
2011-04-12 14:59:03 +02:00
|
|
|
var id = keys[j];
|
|
|
|
var mod = elems.get(id);
|
|
|
|
var allTopics = designer._getTopics();
|
2011-07-27 18:44:09 +02:00
|
|
|
var currentTopic = allTopics.filter(function(node) {
|
|
|
|
return node.getId() == id;
|
2011-04-12 14:59:03 +02:00
|
|
|
})[0];
|
|
|
|
currentTopic.setPosition(mod.originalPos, false);
|
|
|
|
}
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(doneFn))
|
2011-04-12 14:59:03 +02:00
|
|
|
doneFn.attempt();
|
|
|
|
}
|
|
|
|
i--;
|
|
|
|
};
|
2011-07-27 18:44:09 +02:00
|
|
|
_moveEffect = moveEffect.periodical(10);
|
2011-04-12 14:59:03 +02:00
|
|
|
};
|
|
|
|
|
2011-07-27 18:44:09 +02:00
|
|
|
core.Utils._addInnerChildrens = function(elem) {
|
2011-02-09 15:29:09 +01:00
|
|
|
var children = [];
|
|
|
|
var childs = elem._getChildren();
|
2011-07-27 18:44:09 +02:00
|
|
|
for (var i = 0; i < childs.length; i++) {
|
2011-02-09 15:29:09 +01:00
|
|
|
var child = childs[i];
|
|
|
|
children.push(child);
|
|
|
|
children.push(child.getOutgoingLine());
|
|
|
|
var relationships = child.getRelationships();
|
|
|
|
children = children.concat(relationships);
|
|
|
|
var innerChilds = core.Utils._addInnerChildrens(child);
|
|
|
|
children = children.concat(innerChilds);
|
|
|
|
}
|
|
|
|
return children;
|
2010-12-13 15:07:20 +01:00
|
|
|
};
|