Files
wisemapping-open-source/mindplot/src/main/javascript/commands/AddTopicCommand.js

93 lines
3.2 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-30 18:55: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.
*/
2009-06-07 18:59:43 +00:00
mindplot.commands.AddTopicCommand = new Class(/** @lends AddTopicCommand */{
2012-03-07 20:35:06 -03:00
Extends:mindplot.Command,
/**
* @classdesc This command class handles do/undo of adding one or multiple topics to
* the mindmap.
* @constructs
* @param {Array<mindplot.model.NodeModel>} models one or multiple models
* @param {Array<String>} parentTopicsId ids of the parent topics to add the children to, or null
* when attaching a dragged node or a node/branch from clipboard
* @extends mindplot.Command
*/
2012-07-08 18:41:35 -03:00
initialize:function (models, parentTopicsId) {
$assert(models, 'models can not be null');
$assert(parentTopicsId == null || parentTopicsId.length == models.length, 'parents and models must have the same size');
2012-03-07 20:35:06 -03:00
this.parent();
2012-07-08 18:41:35 -03:00
this._models = models;
this._parentsIds = parentTopicsId;
2012-03-07 20:35:06 -03:00
},
/**
* Overrides abstract parent method
*/
2012-07-08 18:41:35 -03:00
execute:function (commandContext) {
var me = this;
_.each(this._models, function (model, index) {
2012-07-08 18:41:35 -03:00
// Add a new topic ...
var topic = commandContext.createTopic(model);
2012-07-08 18:41:35 -03:00
// Connect to topic ...
if (me._parentsIds) {
var parentId = me._parentsIds[index];
2012-07-08 18:41:35 -03:00
if ($defined(parentId)) {
var parentTopic = commandContext.findTopics(parentId)[0];
commandContext.connect(topic, parentTopic);
}
} else {
commandContext.addTopic(topic);
2012-07-08 18:41:35 -03:00
}
// Select just created node ...
2012-07-08 18:41:35 -03:00
var designer = commandContext._designer;
designer.onObjectFocusEvent(topic);
topic.setOnFocus(true);
// Render node ...
topic.setVisibility(true);
});
2012-03-07 20:35:06 -03:00
},
/**
* Overrides abstract parent method
* @see {@link mindplot.Command.undoExecute}
*/
2012-07-08 18:41:35 -03:00
undoExecute:function (commandContext) {
2013-03-03 00:51:14 -03:00
// Delete disconnected the nodes. Create a copy of the topics ...
var clonedModel = [];
_.each(this._models, function (model) {
2013-03-03 00:51:14 -03:00
clonedModel.push(model.clone());
});
// Finally, remove the nodes ...
_.each(this._models, function (model) {
2012-07-08 18:41:35 -03:00
var topicId = model.getId();
var topic = commandContext.findTopics(topicId)[0];
commandContext.deleteTopic(topic);
});
2013-03-03 00:51:14 -03:00
this._models = clonedModel;
2012-03-07 20:35:06 -03:00
}
});