2011-01-23 20:34:05 -03:00
/ * C o p y r i g h t [ 2 0 1 1 ] [ w i s e m a p p i n g ]
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 .
* /
2012-02-25 23:32:15 -03:00
mindplot . persistence . XMLSerializer _Beta = new Class ( {
2011-07-27 14:53:32 -03:00
2012-08-26 16:53:33 -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' , name ) ;
2009-06-07 18:59:43 +00:00
}
2011-07-27 14:53:32 -03:00
document . appendChild ( mapElem ) ;
// 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 ) ;
2009-06-07 18:59:43 +00:00
}
2011-07-27 14:53:32 -03:00
return document ;
} ,
2012-08-26 16:53:33 -03:00
_topicToXML : function ( document , topic ) {
2011-07-27 14:53:32 -03:00
var parentTopic = document . createElement ( "topic" ) ;
// Set topic attributes...
2011-09-08 10:03:42 -03:00
if ( topic . getType ( ) == mindplot . model . INodeModel . CENTRAL _TOPIC _TYPE ) {
2011-07-27 14:53:32 -03:00
parentTopic . setAttribute ( "central" , true ) ;
} else {
var parent = topic . getParent ( ) ;
2011-09-08 10:03:42 -03:00
if ( parent == null || parent . getType ( ) == mindplot . model . INodeModel . CENTRAL _TOPIC _TYPE ) {
2011-07-27 14:53:32 -03:00
var pos = topic . getPosition ( ) ;
parentTopic . setAttribute ( "position" , pos . x + ',' + pos . y ) ;
} else {
var order = topic . getOrder ( ) ;
parentTopic . setAttribute ( "order" , order ) ;
}
}
var text = topic . getText ( ) ;
if ( $defined ( text ) ) {
parentTopic . setAttribute ( 'text' , text ) ;
}
var shape = topic . getShapeType ( ) ;
if ( $defined ( shape ) ) {
parentTopic . setAttribute ( 'shape' , shape ) ;
}
2012-01-11 23:30:26 -03:00
if ( topic . areChildrenShrunken ( ) ) {
2011-07-27 14:53:32 -03:00
parentTopic . setAttribute ( 'shrink' , true ) ;
}
// Font properties ...
var font = "" ;
var fontFamily = topic . getFontFamily ( ) ;
font += ( fontFamily ? fontFamily : '' ) + ';' ;
var fontSize = topic . getFontSize ( ) ;
font += ( fontSize ? fontSize : '' ) + ';' ;
var fontColor = topic . getFontColor ( ) ;
font += ( fontColor ? fontColor : '' ) + ';' ;
var fontWeight = topic . getFontWeight ( ) ;
font += ( fontWeight ? fontWeight : '' ) + ';' ;
var fontStyle = topic . getFontStyle ( ) ;
font += ( fontStyle ? fontStyle : '' ) + ';' ;
if ( $defined ( fontFamily ) || $defined ( fontSize ) || $defined ( fontColor )
|| $defined ( fontWeight ) || $defined ( fontStyle ) ) {
parentTopic . setAttribute ( 'fontStyle' , font ) ;
}
var bgColor = topic . getBackgroundColor ( ) ;
if ( $defined ( bgColor ) ) {
parentTopic . setAttribute ( 'bgColor' , bgColor ) ;
}
var brColor = topic . getBorderColor ( ) ;
if ( $defined ( brColor ) ) {
parentTopic . setAttribute ( 'brColor' , brColor ) ;
}
//ICONS
2012-09-01 11:35:07 -03:00
var i ;
2011-07-27 14:53:32 -03:00
var icons = topic . getIcons ( ) ;
2012-09-01 11:35:07 -03:00
for ( i = 0 ; i < icons . length ; i ++ ) {
2011-07-27 14:53:32 -03:00
var icon = icons [ i ] ;
var iconDom = this . _iconToXML ( document , icon ) ;
parentTopic . appendChild ( iconDom ) ;
}
//LINKS
var links = topic . getLinks ( ) ;
2012-09-01 11:35:07 -03:00
for ( i = 0 ; i < links . length ; i ++ ) {
2011-07-27 14:53:32 -03:00
var link = links [ i ] ;
var linkDom = this . _linkToXML ( document , link ) ;
parentTopic . appendChild ( linkDom ) ;
}
var notes = topic . getNotes ( ) ;
2012-09-01 11:35:07 -03:00
for ( i = 0 ; i < notes . length ; i ++ ) {
2011-07-27 14:53:32 -03:00
var note = notes [ i ] ;
var noteDom = this . _noteToXML ( document , note ) ;
parentTopic . appendChild ( noteDom ) ;
}
//CHILDREN TOPICS
var childTopics = topic . getChildren ( ) ;
2012-09-01 11:35:07 -03:00
for ( i = 0 ; i < childTopics . length ; i ++ ) {
2011-07-27 14:53:32 -03:00
var childTopic = childTopics [ i ] ;
var childDom = this . _topicToXML ( document , childTopic ) ;
parentTopic . appendChild ( childDom ) ;
}
return parentTopic ;
} ,
2012-08-26 16:53:33 -03:00
_iconToXML : function ( document , icon ) {
2011-07-27 14:53:32 -03:00
var iconDom = document . createElement ( "icon" ) ;
iconDom . setAttribute ( 'id' , icon . getIconType ( ) ) ;
return iconDom ;
} ,
2012-08-26 16:53:33 -03:00
_linkToXML : function ( document , link ) {
2011-07-27 14:53:32 -03:00
var linkDom = document . createElement ( "link" ) ;
linkDom . setAttribute ( 'url' , link . getUrl ( ) ) ;
return linkDom ;
} ,
2012-08-26 16:53:33 -03:00
_noteToXML : function ( document , note ) {
2011-07-27 14:53:32 -03:00
var noteDom = document . createElement ( "note" ) ;
noteDom . setAttribute ( 'text' , note . getText ( ) ) ;
return noteDom ;
} ,
2012-08-26 16:53:33 -03:00
loadFromDom : function ( dom , mapId ) {
2011-07-27 14:53:32 -03:00
$assert ( dom , "Dom can not be null" ) ;
2011-10-02 14:47:54 -03:00
$assert ( mapId , "mapId can not be null" ) ;
2012-09-23 10:59:05 -03:00
// Is a valid object ?
var documentElement = dom . documentElement ;
$assert ( documentElement . nodeName != "parsererror" , "Error while parsing: '" + documentElement . childNodes [ 0 ] . nodeValue ) ;
2011-07-27 14:53:32 -03:00
// Is a wisemap?.
2012-09-23 10:59:05 -03:00
$assert ( documentElement . tagName == mindplot . persistence . XMLSerializer _Beta . MAP _ROOT _NODE , "This seem not to be a map document. Root Tag: '" + documentElement . tagName ) ;
2011-07-27 14:53:32 -03:00
// Start the loading process ...
2012-09-23 10:59:05 -03:00
var version = documentElement . getAttribute ( "version" ) ;
2012-02-25 23:32:15 -03:00
version = ! $defined ( version ) ? mindplot . persistence . ModelCodeName . BETA : version ;
2011-11-30 18:06:45 -03:00
var mindmap = new mindplot . model . Mindmap ( mapId , version ) ;
2011-07-27 14:53:32 -03:00
2012-09-23 10:59:05 -03:00
var children = documentElement . childNodes ;
2011-07-27 14:53:32 -03:00
for ( var i = 0 ; i < children . length ; i ++ ) {
var child = children [ i ] ;
if ( child . nodeType == 1 ) {
var topic = this . _deserializeNode ( child , mindmap ) ;
mindmap . addBranch ( topic ) ;
}
}
2011-10-02 14:47:54 -03:00
mindmap . setId ( mapId ) ;
2011-07-27 14:53:32 -03:00
return mindmap ;
} ,
2012-08-26 16:53:33 -03:00
_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-07-27 14:53:32 -03:00
var topic = mindmap . createNode ( type ) ;
// Load attributes...
var text = domElem . getAttribute ( 'text' ) ;
if ( $defined ( text ) ) {
topic . setText ( text ) ;
2009-06-07 18:59:43 +00:00
}
2011-07-27 14:53:32 -03:00
var order = domElem . getAttribute ( 'order' ) ;
if ( $defined ( order ) ) {
2012-06-23 19:51:14 -03:00
topic . setOrder ( parseInt ( order ) ) ;
2011-07-27 14:53:32 -03:00
}
var shape = domElem . getAttribute ( 'shape' ) ;
if ( $defined ( shape ) ) {
topic . setShapeType ( shape ) ;
}
var isShrink = domElem . getAttribute ( 'shrink' ) ;
if ( $defined ( isShrink ) ) {
2012-01-11 23:30:26 -03:00
topic . setChildrenShrunken ( isShrink ) ;
2011-07-27 14:53:32 -03:00
}
var fontStyle = domElem . getAttribute ( 'fontStyle' ) ;
if ( $defined ( 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 ] ) ;
}
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 ) ;
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
}
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 == 1 ) {
$assert ( child . tagName == "topic" || child . tagName == "icon" || child . tagName == "link" || child . tagName == "note" , 'Illegal node type:' + child . tagName ) ;
if ( child . tagName == "topic" ) {
var childTopic = this . _deserializeNode ( child , mindmap ) ;
childTopic . connectTo ( topic ) ;
} else if ( child . tagName == "icon" ) {
var icon = this . _deserializeIcon ( child , topic ) ;
2012-02-27 18:17:15 -03:00
topic . addFeature ( icon ) ;
2011-07-27 14:53:32 -03:00
} else if ( child . tagName == "link" ) {
var link = this . _deserializeLink ( child , topic ) ;
2012-02-27 18:17:15 -03:00
topic . addFeature ( link ) ;
2011-07-27 14:53:32 -03:00
} else if ( child . tagName == "note" ) {
var note = this . _deserializeNote ( child , topic ) ;
2012-02-27 18:17:15 -03:00
topic . addFeature ( note ) ;
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 ;
} ,
2012-09-01 11:35:07 -03:00
_deserializeIcon : function ( domElem ) {
2011-11-27 21:39:49 -03:00
var icon = domElem . getAttribute ( "id" ) ;
icon = icon . replace ( "images/" , "icons/legacy/" ) ;
2012-08-26 16:53:33 -03:00
return mindplot . TopicFeature . createModel ( mindplot . TopicFeature . Icon . id , { id : icon } ) ;
2011-07-27 14:53:32 -03:00
} ,
2012-09-01 11:35:07 -03:00
_deserializeLink : function ( domElem ) {
2012-08-26 16:53:33 -03:00
return mindplot . TopicFeature . createModel ( mindplot . TopicFeature . Link . id , { url : domElem . getAttribute ( "url" ) } ) ;
2011-07-27 14:53:32 -03:00
} ,
2012-09-01 11:35:07 -03:00
_deserializeNote : function ( domElem ) {
2012-06-23 17:10:58 -03:00
var text = domElem . getAttribute ( "text" ) ;
2012-08-26 16:53:33 -03:00
return mindplot . TopicFeature . createModel ( mindplot . TopicFeature . Note . id , { text : text == null ? " " : text } ) ;
2011-07-27 14:53:32 -03:00
} } ) ;
2009-06-07 18:59:43 +00:00
2012-02-25 23:32:15 -03:00
mindplot . persistence . XMLSerializer _Beta . MAP _ROOT _NODE = 'map' ;