472 lines
16 KiB
JavaScript
Raw Normal View History

2009-06-07 18:59:43 +00:00
/*
2012-10-04 20:48:01 -03:00
* Copyright [2012] [wisemapping]
2011-07-27 14:53:32 -03:00
*
* 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.
*/
mindplot.persistence.XMLSerializer_Pela = new Class({
2011-07-27 14:53:32 -03:00
toXML: function (mindmap) {
2011-07-27 14:53:32 -03:00
$assert(mindmap, "Can not save a null mindmap");
var document = core.Utils.createDocument();
// Store map attributes ...
var mapElem = document.createElement("map");
var name = mindmap.getId();
if ($defined(name)) {
mapElem.setAttribute('name', this.rmXmlInv(name));
2011-07-27 14:53:32 -03:00
}
var version = mindmap.getVersion();
if ($defined(version)) {
mapElem.setAttribute('version', version);
}
2009-06-07 18:59:43 +00:00
document.appendChild(mapElem);
2011-07-27 14:53:32 -03:00
// Create branches ...
var topics = mindmap.getBranches();
for (var i = 0; i < topics.length; i++) {
var topic = topics[i];
var topicDom = this._topicToXML(document, topic);
mapElem.appendChild(topicDom);
2011-07-27 14:53:32 -03:00
}
// Create Relationships
var relationships = mindmap.getRelationships();
if (relationships.length > 0) {
2011-07-27 14:53:32 -03:00
for (var j = 0; j < relationships.length; j++) {
var relationship = relationships[j];
if (mindmap.findNodeById(relationship.getFromNode()) !== null && mindmap.findNodeById(relationship.getToNode()) !== null) {
// Isolated relationships are not persisted ....
var relationDom = this._relationshipToXML(document, relationship);
mapElem.appendChild(relationDom);
}
2011-07-27 14:53:32 -03:00
}
}
2011-07-27 14:53:32 -03:00
return document;
},
2009-06-07 18:59:43 +00:00
_topicToXML: function (document, topic) {
2011-07-27 14:53:32 -03:00
var parentTopic = document.createElement("topic");
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
// Set topic attributes...
2011-09-08 10:03:42 -03:00
if (topic.getType() == mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE) {
parentTopic.setAttribute('central', 'true');
2011-07-27 14:53:32 -03:00
} else {
2011-09-02 02:31:03 -03:00
2009-06-07 18:59:43 +00:00
var pos = topic.getPosition();
parentTopic.setAttribute("position", pos.x + ',' + pos.y);
2011-09-02 02:31:03 -03:00
2009-06-07 18:59:43 +00:00
var order = topic.getOrder();
if (typeof order === 'number' && isFinite(order))
2011-11-13 21:18:34 -03:00
parentTopic.setAttribute("order", order);
2011-07-27 14:53:32 -03:00
}
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var text = topic.getText();
if ($defined(text)) {
2012-01-21 16:37:13 -03:00
this._noteTextToXML(document, parentTopic, text);
2011-07-27 14:53:32 -03:00
}
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var shape = topic.getShapeType();
if ($defined(shape)) {
parentTopic.setAttribute('shape', shape);
2012-03-07 01:13:50 -03:00
if (shape == mindplot.model.TopicShape.IMAGE) {
parentTopic.setAttribute('image', topic.getImageSize().width + "," + topic.getImageSize().height + ":" + topic.getImageUrl());
}
2011-07-27 14:53:32 -03:00
}
2009-06-07 18:59:43 +00:00
2012-11-28 22:09:29 -03:00
if (topic.areChildrenShrunken() && topic.getType() != mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE) {
parentTopic.setAttribute('shrink', 'true');
2011-07-27 14:53:32 -03:00
}
2011-07-27 14:53:32 -03:00
// Font properties ...
var id = topic.getId();
parentTopic.setAttribute('id', id);
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var font = "";
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var fontFamily = topic.getFontFamily();
font += (fontFamily ? fontFamily : '') + ';';
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var fontSize = topic.getFontSize();
font += (fontSize ? fontSize : '') + ';';
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var fontColor = topic.getFontColor();
font += (fontColor ? fontColor : '') + ';';
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var fontWeight = topic.getFontWeight();
font += (fontWeight ? fontWeight : '') + ';';
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var fontStyle = topic.getFontStyle();
font += (fontStyle ? fontStyle : '') + ';';
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
if ($defined(fontFamily) || $defined(fontSize) || $defined(fontColor)
|| $defined(fontWeight) || $defined(fontStyle)) {
parentTopic.setAttribute('fontStyle', font);
}
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var bgColor = topic.getBackgroundColor();
if ($defined(bgColor)) {
parentTopic.setAttribute('bgColor', bgColor);
}
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var brColor = topic.getBorderColor();
if ($defined(brColor)) {
parentTopic.setAttribute('brColor', brColor);
}
2009-06-07 18:59:43 +00:00
2012-03-07 17:40:10 -03:00
var metadata = topic.getMetadata();
if ($defined(metadata)) {
parentTopic.setAttribute('metadata', metadata);
}
// Serialize features ...
var features = topic.getFeatures();
for (var i = 0; i < features.length; i++) {
var feature = features[i];
var featureType = feature.getType();
var featureDom = document.createElement(featureType);
var attributes = feature.getAttributes();
for (var key in attributes) {
var value = attributes[key];
if (key == 'text') {
2013-03-17 13:32:41 -03:00
var cdata = document.createCDATASection(this.rmXmlInv(value));
featureDom.appendChild(cdata);
} else {
featureDom.setAttribute(key, this.rmXmlInv(value));
}
}
parentTopic.appendChild(featureDom);
2011-07-27 14:53:32 -03:00
}
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
//CHILDREN TOPICS
var childTopics = topic.getChildren();
for (var j = 0; j < childTopics.length; j++) {
var childTopic = childTopics[j];
2011-07-27 14:53:32 -03:00
var childDom = this._topicToXML(document, childTopic);
parentTopic.appendChild(childDom);
2009-06-07 18:59:43 +00:00
}
2011-07-27 14:53:32 -03:00
return parentTopic;
},
_noteTextToXML: function (document, elem, text) {
if (text.indexOf('\n') == -1) {
elem.setAttribute('text', this.rmXmlInv(text));
2012-01-21 16:37:13 -03:00
} else {
var textDom = document.createElement("text");
var cdata = document.createCDATASection(this.rmXmlInv(text));
textDom.appendChild(cdata);
elem.appendChild(textDom);
2012-01-21 16:37:13 -03:00
}
},
_relationshipToXML: function (document, relationship) {
var result = document.createElement("relationship");
result.setAttribute("srcTopicId", relationship.getFromNode());
result.setAttribute("destTopicId", relationship.getToNode());
2011-07-27 14:53:32 -03:00
var lineType = relationship.getLineType();
result.setAttribute("lineType", lineType);
2011-07-27 14:53:32 -03:00
if (lineType == mindplot.ConnectionLine.CURVED || lineType == mindplot.ConnectionLine.SIMPLE_CURVED) {
if ($defined(relationship.getSrcCtrlPoint())) {
var srcPoint = relationship.getSrcCtrlPoint();
result.setAttribute("srcCtrlPoint", Math.round(srcPoint.x) + "," + Math.round(srcPoint.y));
2011-07-27 14:53:32 -03:00
}
if ($defined(relationship.getDestCtrlPoint())) {
var destPoint = relationship.getDestCtrlPoint();
result.setAttribute("destCtrlPoint", Math.round(destPoint.x) + "," + Math.round(destPoint.y));
2011-07-27 14:53:32 -03:00
}
}
result.setAttribute("endArrow", relationship.getEndArrow());
result.setAttribute("startArrow", relationship.getStartArrow());
return result;
2011-07-27 14:53:32 -03:00
},
loadFromDom: function (dom, mapId) {
2011-10-02 14:47:54 -03:00
$assert(dom, "dom can not be null");
$assert(mapId, "mapId can not be null");
2011-07-27 14:53:32 -03:00
var rootElem = dom.documentElement;
// Is a wisemap?.
$assert(rootElem.tagName == mindplot.persistence.XMLSerializer_Pela.MAP_ROOT_NODE, "This seem not to be a map document.");
2011-07-27 14:53:32 -03:00
2014-02-19 03:09:24 -03:00
this._idsMap = {};
2011-07-27 14:53:32 -03:00
// Start the loading process ...
var version = rootElem.getAttribute("version");
var mindmap = new mindplot.model.Mindmap(mapId, version);
2011-07-27 14:53:32 -03:00
var children = rootElem.childNodes;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.nodeType == 1) {
switch (child.tagName) {
case "topic":
var topic = this._deserializeNode(child, mindmap);
mindmap.addBranch(topic);
break;
case "relationship":
var relationship = this._deserializeRelationship(child, mindmap);
if (relationship != null)
mindmap.addRelationship(relationship);
break;
}
}
2009-06-07 18:59:43 +00:00
}
2011-07-27 14:53:32 -03:00
this._idsMap = null;
2011-10-02 14:47:54 -03:00
mindmap.setId(mapId);
2011-07-27 14:53:32 -03:00
return mindmap;
},
_deserializeNode: function (domElem, mindmap) {
2011-09-08 10:03:42 -03:00
var type = (domElem.getAttribute('central') != null) ? mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE : mindplot.model.INodeModel.MAIN_TOPIC_TYPE;
2011-11-13 21:18:34 -03:00
2011-07-27 14:53:32 -03:00
// Load attributes...
var id = domElem.getAttribute('id');
if ($defined(id)) {
id = parseInt(id);
}
2011-04-08 15:31:40 +01:00
2014-02-19 03:09:24 -03:00
if (this._idsMap[id]) {
2011-07-27 14:53:32 -03:00
id = null;
} else {
2014-02-19 03:11:13 -03:00
this._idsMap[id] = domElem;
2011-07-27 14:53:32 -03:00
}
2011-04-15 12:59:21 +01:00
2011-07-27 14:53:32 -03:00
var topic = mindmap.createNode(type, id);
// Set text property is it;s defined...
2011-07-27 14:53:32 -03:00
var text = domElem.getAttribute('text');
if ($defined(text) && text) {
2011-07-27 14:53:32 -03:00
topic.setText(text);
}
var fontStyle = domElem.getAttribute('fontStyle');
if ($defined(fontStyle) && fontStyle) {
var font = fontStyle.split(';');
if (font[0]) {
topic.setFontFamily(font[0]);
}
if (font[1]) {
topic.setFontSize(font[1]);
}
if (font[2]) {
topic.setFontColor(font[2]);
}
if (font[3]) {
topic.setFontWeight(font[3]);
}
if (font[4]) {
topic.setFontStyle(font[4]);
}
2011-07-27 14:53:32 -03:00
}
2012-03-07 01:13:50 -03:00
2011-07-27 14:53:32 -03:00
var shape = domElem.getAttribute('shape');
if ($defined(shape)) {
topic.setShapeType(shape);
2012-03-07 01:13:50 -03:00
if (shape == mindplot.model.TopicShape.IMAGE) {
var image = domElem.getAttribute('image');
var size = image.substring(0, image.indexOf(':'));
var url = image.substring(image.indexOf(':') + 1, image.length);
topic.setImageUrl(url);
var split = size.split(',');
topic.setImageSize(split[0], split[1]);
}
2011-07-27 14:53:32 -03:00
}
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var bgColor = domElem.getAttribute('bgColor');
if ($defined(bgColor)) {
topic.setBackgroundColor(bgColor);
}
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var borderColor = domElem.getAttribute('brColor');
if ($defined(borderColor)) {
topic.setBorderColor(borderColor);
}
var order = domElem.getAttribute('order');
if ($defined(order) && order != "NaN") { // Hack for broken maps ...
topic.setOrder(parseInt(order));
}
var isShrink = domElem.getAttribute('shrink');
2012-11-28 22:14:52 -03:00
// Hack: Some production maps has been stored with the central topic collapsed. This is a bug.
if ($defined(isShrink) && type != mindplot.model.INodeModel.CENTRAL_TOPIC_TYPE) {
topic.setChildrenShrunken(isShrink);
}
2009-06-07 18:59:43 +00:00
2011-07-27 14:53:32 -03:00
var position = domElem.getAttribute('position');
if ($defined(position)) {
var pos = position.split(',');
topic.setPosition(pos[0], pos[1]);
}
2009-06-07 18:59:43 +00:00
2012-03-07 17:40:10 -03:00
var metadata = domElem.getAttribute('metadata');
if ($defined(metadata)) {
topic.setMetadata(metadata);
}
2011-07-27 14:53:32 -03:00
//Creating icons and children nodes
var children = domElem.childNodes;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.nodeType == Node.ELEMENT_NODE) {
2011-07-27 14:53:32 -03:00
if (child.tagName == "topic") {
var childTopic = this._deserializeNode(child, mindmap);
childTopic.connectTo(topic);
} else if (mindplot.TopicFeature.isSupported(child.tagName)) {
// Load attributes ...
var namedNodeMap = child.attributes;
var attributes = {};
for (var j = 0; j < namedNodeMap.length; j++) {
var attribute = namedNodeMap.item(j);
attributes[attribute.name] = attribute.value;
}
// Has text node ?.
var textAttr = this._deserializeTextAttr(child);
if (textAttr) {
attributes['text'] = textAttr;
}
// Create a new element ....
var featureType = child.tagName;
2012-07-08 18:41:35 -03:00
var feature = mindplot.TopicFeature.createModel(featureType, attributes);
topic.addFeature(feature);
2012-01-21 16:37:13 -03:00
} else if (child.tagName == "text") {
var nodeText = this._deserializeNodeText(child);
topic.setText(nodeText);
2011-07-27 14:53:32 -03:00
}
2009-06-07 18:59:43 +00:00
}
}
2011-07-27 14:53:32 -03:00
return topic;
},
_deserializeTextAttr: function (domElem) {
var value = domElem.getAttribute("text");
if (!$defined(value)) {
var children = domElem.childNodes;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.nodeType == Node.CDATA_SECTION_NODE) {
value = child.nodeValue;
}
}
2012-06-25 23:13:08 -03:00
} else {
// Notes must be decoded ...
value = unescape(value);
// Hack for empty nodes ...
if (value == "") {
value = " ";
}
}
2012-06-25 23:13:08 -03:00
return value;
2011-07-27 14:53:32 -03:00
},
_deserializeNodeText: function (domElem) {
2012-01-21 16:37:13 -03:00
var children = domElem.childNodes;
var value = null;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.nodeType == Node.CDATA_SECTION_NODE) {
value = child.nodeValue;
}
}
return value;
},
_deserializeRelationship: function (domElement, mindmap) {
2011-07-27 14:53:32 -03:00
var srcId = domElement.getAttribute("srcTopicId");
var destId = domElement.getAttribute("destTopicId");
var lineType = domElement.getAttribute("lineType");
var srcCtrlPoint = domElement.getAttribute("srcCtrlPoint");
var destCtrlPoint = domElement.getAttribute("destCtrlPoint");
var endArrow = domElement.getAttribute("endArrow");
var startArrow = domElement.getAttribute("startArrow");
//If for some reason a relationship lines has source and dest nodes the same, don't import it.
if (srcId == destId) {
return null;
}
2012-07-07 11:52:23 -03:00
// Is the connections points valid ?. If it's not, do not load the relationship ...
2012-07-08 18:41:35 -03:00
if (mindmap.findNodeById(srcId) == null || mindmap.findNodeById(destId) == null) {
2012-07-07 11:52:23 -03:00
return null;
}
2011-07-27 14:53:32 -03:00
var model = mindmap.createRelationship(srcId, destId);
model.setLineType(lineType);
if ($defined(srcCtrlPoint) && srcCtrlPoint != "") {
model.setSrcCtrlPoint(core.Point.fromString(srcCtrlPoint));
}
if ($defined(destCtrlPoint) && destCtrlPoint != "") {
model.setDestCtrlPoint(core.Point.fromString(destCtrlPoint));
}
model.setEndArrow('false');
model.setStartArrow('true');
2011-07-27 14:53:32 -03:00
return model;
2009-06-07 18:59:43 +00:00
}
/*
* This method ensures that the output String has only
* valid XML unicode characters as specified by the
* XML 1.0 standard. For reference, please see
* <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">the
* standard</a>. This method will return an empty
* String if the input is null or empty.
*
* @param in The String whose non-valid characters we want to remove.
* @return The in String, stripped of non-valid characters.
*/,
rmXmlInv: function (str) {
if (str == null || str == undefined)
return null;
var result = "";
for (var i=0;i<str.length;i++){
var c = str.charCodeAt(i);
if ((c == 0x9) || (c == 0xA) || (c == 0xD)
|| ((c >= 0x20) && (c <= 0xD7FF))
|| ((c >= 0xE000) && (c <= 0xFFFD))
|| ((c >= 0x10000) && (c <= 0x10FFFF))) {
result = result + str.charAt(i);
}
}
return result;
}
2011-07-27 14:53:32 -03:00
});
mindplot.persistence.XMLSerializer_Pela.MAP_ROOT_NODE = 'map';