wisemapping-frontend/packages/mindplot/src/components/persistence/Pela2TangoMigrator.ts

102 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
2021-12-25 23:39:34 +01:00
* Copyright [2021] [wisemapping]
2021-07-16 16:41:58 +02: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.
*/
2021-12-14 18:13:39 +01:00
import { $assert, $defined } from '@wisemapping/core-js';
2021-12-23 20:58:50 +01:00
import XMLSerializer from './XMLSerializerTango';
import ModelCodeName from './ModelCodeName';
2022-01-03 17:27:00 +01:00
import XMLMindmapSerializer from './XMLMindmapSerializer';
import Mindmap from '../model/Mindmap';
import NodeModel from '../model/NodeModel';
2021-07-16 16:41:58 +02:00
2022-01-03 17:27:00 +01:00
class Pela2TangoMigrator implements XMLMindmapSerializer {
private _pelaSerializer: XMLMindmapSerializer;
2022-01-03 17:27:00 +01:00
private _tangoSerializer: XMLSerializer;
2022-01-03 17:27:00 +01:00
constructor(pelaSerializer: XMLMindmapSerializer) {
2021-10-05 02:05:34 +02:00
this._pelaSerializer = pelaSerializer;
2021-12-19 17:06:42 +01:00
this._tangoSerializer = new XMLSerializer();
2021-12-05 00:39:20 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-03 17:27:00 +01:00
toXML(mindmap: Mindmap): Document {
2021-10-05 02:05:34 +02:00
return this._tangoSerializer.toXML(mindmap);
2021-12-05 00:39:20 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-03 17:27:00 +01:00
loadFromDom(dom: Document, mapId: string): Mindmap {
2021-10-05 02:05:34 +02:00
$assert($defined(mapId), 'mapId can not be null');
const mindmap = this._pelaSerializer.loadFromDom(dom, mapId);
mindmap.setVersion(ModelCodeName.TANGO);
this._fixOrder(mindmap);
this._fixPosition(mindmap);
return mindmap;
2021-12-05 00:39:20 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-03 17:27:00 +01:00
private _fixOrder(mindmap: Mindmap) {
2021-10-05 02:05:34 +02:00
// First level node policies has been changed.
2022-01-03 17:27:00 +01:00
const centralNode: NodeModel = mindmap.getBranches()[0];
const children: NodeModel[] = centralNode.getChildren();
const leftNodes: NodeModel[] = [];
const rightNodes: NodeModel[] = [];
children.forEach((child) => {
2021-10-05 02:05:34 +02:00
const position = child.getPosition();
if (position.x < 0) {
leftNodes.push(child);
} else {
rightNodes.push(child);
}
2022-01-03 17:27:00 +01:00
rightNodes.sort((a, b) => a.getOrder() - b.getOrder());
leftNodes.sort((a, b) => a.getOrder() - b.getOrder());
});
2021-07-16 16:41:58 +02:00
2021-12-19 09:44:08 +01:00
for (let i = 0; i < rightNodes.length; i++) {
2021-10-05 02:05:34 +02:00
rightNodes[i].setOrder(i * 2);
}
2021-07-16 16:41:58 +02:00
2021-12-19 09:44:08 +01:00
for (let i = 0; i < leftNodes.length; i++) {
2021-10-05 02:05:34 +02:00
leftNodes[i].setOrder(i * 2 + 1);
}
2021-12-05 00:39:20 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-03 17:27:00 +01:00
private _fixPosition(mindmap: Mindmap) {
2021-10-05 02:05:34 +02:00
// Position was not required in previous versions. Try to synthesize one .
const centralNode = mindmap.getBranches()[0];
const children = centralNode.getChildren();
for (let i = 0; i < children.length; i++) {
const child = children[i];
const position = child.getPosition();
this._fixNodePosition(child, position);
}
2021-12-05 00:39:20 +01:00
}
2021-12-19 09:44:08 +01:00
2022-01-24 20:24:16 +01:00
_fixNodePosition(node: NodeModel, parentPosition: {x:number, y:number}) {
2021-10-05 02:05:34 +02:00
// Position was not required in previous versions. Try to synthesize one .
let position = node.getPosition();
if (!position) {
position = { x: parentPosition.x + 30, y: parentPosition.y };
node.setPosition(position.x, position.y);
}
const children = node.getChildren();
for (let i = 0; i < children.length; i++) {
const child = children[i];
this._fixNodePosition(child, position);
2021-07-16 16:41:58 +02:00
}
2021-12-05 00:39:20 +01:00
}
}
2021-07-16 16:41:58 +02:00
export default Pela2TangoMigrator;