wisemapping-frontend/packages/mindplot/src/components/commands/DragTopicCommand.js

100 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
* Copyright [2015] [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.
*/
import Command from '../Command';
2021-07-16 16:41:58 +02:00
const DragTopicCommand = new Class(/** @lends DragTopicCommand */{
2021-10-05 02:05:34 +02:00
Extends: Command,
/**
2021-07-16 16:41:58 +02:00
* @classdesc This command class handles do/undo of dragging a topic to a new position.
* @constructs
* @param {String} topicId id of the topic to drag
* @param {Object} position
* @param {Number} order the order property (children of one node are displayed in order from 0 to n)
* @param {mindplot.Topic} parentTopic the topic to be made the dragged topic's new parent
* @extends mindplot.Command
*/
2021-10-05 02:05:34 +02:00
initialize(topicId, position, order, parentTopic) {
$assert(topicId, 'topicId must be defined');
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
this._topicsId = topicId;
if ($defined(parentTopic)) this._parentId = parentTopic.getId();
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
this.parent();
this._position = position;
this._order = order;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
* Overrides abstract parent method
2021-07-16 16:41:58 +02:00
*/
2021-10-05 02:05:34 +02:00
execute(commandContext) {
const topic = commandContext.findTopics(this._topicsId)[0];
topic.setVisibility(false);
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Save old position ...
const origParentTopic = topic.getOutgoingConnectedTopic();
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// In this case, topics are positioned using order ...
const origOrder = topic.getOrder();
const origPosition = topic.getPosition();
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Disconnect topic ..
if ($defined(origParentTopic) && origParentTopic !== this._parentId) {
2021-10-05 02:05:34 +02:00
commandContext.disconnect(topic);
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Set topic order ...
if (this._order != null) {
topic.setOrder(this._order);
} else if (this._position != null) {
commandContext.moveTopic(topic, this._position);
} else {
$assert('Illegal command state exception.');
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Finally, connect topic ...
if (origParentTopic !== this._parentId) {
2021-10-05 02:05:34 +02:00
if ($defined(this._parentId)) {
const parentTopic = commandContext.findTopics(this._parentId)[0];
commandContext.connect(topic, parentTopic);
}
// Backup old parent id ...
this._parentId = null;
if ($defined(origParentTopic)) {
this._parentId = origParentTopic.getId();
}
}
topic.setVisibility(true);
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Store for undo ...
this._order = origOrder;
this._position = origPosition;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* Overrides abstract parent method
2021-10-05 02:05:34 +02:00
* @see {@link mindplot.Command.undoExecute}
2021-07-16 16:41:58 +02:00
*/
2021-10-05 02:05:34 +02:00
undoExecute(commandContext) {
this.execute(commandContext);
},
2021-07-16 16:41:58 +02:00
});
export default DragTopicCommand;