2009-06-07 20:59:43 +02:00
|
|
|
/*
|
2015-04-12 05:15:12 +02:00
|
|
|
* Copyright [2015] [wisemapping]
|
2011-07-30 23:55:32 +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.
|
|
|
|
*/
|
2009-06-07 20:59:43 +02:00
|
|
|
|
2015-03-23 09:25:54 +01:00
|
|
|
mindplot.commands.DeleteCommand = new Class(/** @lends mindplot.commands.DeleteCommand */{
|
2011-07-26 20:07:53 +02:00
|
|
|
Extends:mindplot.Command,
|
2015-03-23 09:25:54 +01:00
|
|
|
/**
|
|
|
|
* @classdesc This command class handles do/undo of deleting a topic.
|
|
|
|
* @constructs
|
|
|
|
* @param {Array<String>} topicIds ids of the topics to delete
|
|
|
|
* @param {Array<String>} relIds ids of the relationships connected to the topics
|
|
|
|
* @extends mindplot.Command
|
|
|
|
*/
|
2012-07-07 02:21:56 +02:00
|
|
|
initialize:function (topicIds, relIds) {
|
2012-07-07 06:33:34 +02:00
|
|
|
$assert($defined(relIds), 'topicIds can not be null');
|
2011-11-30 04:56:21 +01:00
|
|
|
|
2012-02-27 23:25:14 +01:00
|
|
|
this.parent();
|
2011-09-06 06:03:27 +02:00
|
|
|
this._relIds = relIds;
|
|
|
|
this._topicIds = topicIds;
|
2009-06-07 20:59:43 +02:00
|
|
|
this._deletedTopicModels = [];
|
2012-07-07 06:33:34 +02:00
|
|
|
this._deletedRelModel = [];
|
2009-06-07 20:59:43 +02:00
|
|
|
this._parentTopicIds = [];
|
|
|
|
},
|
2011-08-25 04:41:39 +02:00
|
|
|
|
2015-03-23 09:25:54 +01:00
|
|
|
/**
|
|
|
|
* Overrides abstract parent method
|
|
|
|
*/
|
2012-07-07 02:21:56 +02:00
|
|
|
execute:function (commandContext) {
|
2012-07-07 16:52:23 +02:00
|
|
|
|
|
|
|
// If a parent has been selected for deletion, the children must be excluded from the delete ...
|
|
|
|
var topics = this._filterChildren(this._topicIds, commandContext);
|
|
|
|
|
2011-07-30 23:55:32 +02:00
|
|
|
if (topics.length > 0) {
|
2014-03-17 04:36:29 +01:00
|
|
|
_.each(topics, function (topic) {
|
2012-09-28 05:49:45 +02:00
|
|
|
// In case that it's editing text node, force close without update ...
|
|
|
|
topic.closeEditors();
|
|
|
|
|
2012-07-09 00:31:21 +02:00
|
|
|
var model = topic.getModel();
|
|
|
|
|
|
|
|
// Delete relationships
|
|
|
|
var relationships = this._collectInDepthRelationships(topic);
|
|
|
|
this._deletedRelModel.append(relationships.map(function (rel) {
|
|
|
|
return rel.getModel().clone();
|
|
|
|
}));
|
|
|
|
|
2014-03-17 04:36:29 +01:00
|
|
|
_.each(relationships, function (relationship) {
|
2012-07-09 00:31:21 +02:00
|
|
|
commandContext.deleteRelationship(relationship);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Store information for undo ...
|
|
|
|
var clonedModel = model.clone();
|
|
|
|
this._deletedTopicModels.push(clonedModel);
|
|
|
|
var outTopic = topic.getOutgoingConnectedTopic();
|
|
|
|
var outTopicId = null;
|
|
|
|
if (outTopic != null) {
|
|
|
|
outTopicId = outTopic.getId();
|
|
|
|
}
|
|
|
|
this._parentTopicIds.push(outTopicId);
|
|
|
|
|
|
|
|
// Finally, delete the topic from the workspace...
|
|
|
|
commandContext.deleteTopic(topic);
|
|
|
|
|
|
|
|
}, this);
|
2011-07-30 23:55:32 +02:00
|
|
|
}
|
2012-07-09 00:31:21 +02:00
|
|
|
|
2012-07-07 02:21:56 +02:00
|
|
|
var rels = commandContext.findRelationships(this._relIds);
|
|
|
|
if (rels.length > 0) {
|
2014-03-17 04:36:29 +01:00
|
|
|
_.each(rels, function (rel) {
|
2012-07-07 06:33:34 +02:00
|
|
|
this._deletedRelModel.push(rel.getModel().clone());
|
2012-07-07 02:21:56 +02:00
|
|
|
commandContext.deleteRelationship(rel);
|
2012-07-09 00:31:21 +02:00
|
|
|
}, this);
|
2010-12-22 23:34:24 +01:00
|
|
|
}
|
2009-06-07 20:59:43 +02:00
|
|
|
},
|
2011-11-29 04:38:11 +01:00
|
|
|
|
2015-03-23 09:25:54 +01:00
|
|
|
/**
|
|
|
|
* Overrides abstract parent method
|
|
|
|
* @see {@link mindplot.Command.undoExecute}
|
|
|
|
*/
|
2012-07-07 02:21:56 +02:00
|
|
|
undoExecute:function (commandContext) {
|
2009-06-07 20:59:43 +02:00
|
|
|
|
2012-09-24 19:54:19 +02:00
|
|
|
// Add all the topics ...
|
2014-03-17 04:36:29 +01:00
|
|
|
_.each(this._deletedTopicModels, function (model) {
|
2012-09-24 19:54:19 +02:00
|
|
|
commandContext.createTopic(model);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
// Do they need to be connected ?
|
2014-03-17 04:36:29 +01:00
|
|
|
_.each(this._deletedTopicModels, function (topicModel, index) {
|
2012-09-24 19:54:19 +02:00
|
|
|
var topics = commandContext.findTopics(topicModel.getId());
|
2012-07-09 00:31:21 +02:00
|
|
|
|
2012-09-24 19:54:19 +02:00
|
|
|
var parentId = this._parentTopicIds[index];
|
|
|
|
if (parentId) {
|
|
|
|
var parentTopics = commandContext.findTopics(parentId);
|
|
|
|
commandContext.connect(topics[0], parentTopics[0]);
|
2012-07-09 00:31:21 +02:00
|
|
|
}
|
|
|
|
}, this);
|
2012-07-07 06:42:55 +02:00
|
|
|
|
2012-09-24 19:54:19 +02:00
|
|
|
// Add rebuild relationships ...
|
2014-03-17 04:36:29 +01:00
|
|
|
_.each(this._deletedRelModel, function (model) {
|
2012-07-07 06:33:34 +02:00
|
|
|
commandContext.addRelationship(model);
|
2014-07-06 06:15:34 +02:00
|
|
|
});
|
2009-06-07 20:59:43 +02:00
|
|
|
|
2012-09-28 01:18:12 +02:00
|
|
|
// Finally display the topics ...
|
2014-03-17 04:36:29 +01:00
|
|
|
_.each(this._deletedTopicModels, function (topicModel) {
|
2012-09-28 01:18:12 +02:00
|
|
|
var topics = commandContext.findTopics(topicModel.getId());
|
|
|
|
topics[0].setBranchVisibility(true);
|
|
|
|
}, this);
|
2012-09-25 13:58:37 +02:00
|
|
|
|
|
|
|
// Focus on last recovered topic ..
|
|
|
|
if (this._deletedTopicModels.length > 0) {
|
|
|
|
var firstTopic = this._deletedTopicModels[0];
|
|
|
|
var topic = commandContext.findTopics(firstTopic.getId())[0];
|
|
|
|
topic.setOnFocus(true);
|
|
|
|
}
|
|
|
|
|
2009-06-07 20:59:43 +02:00
|
|
|
this._deletedTopicModels = [];
|
|
|
|
this._parentTopicIds = [];
|
2012-07-07 06:33:34 +02:00
|
|
|
this._deletedRelModel = [];
|
2012-07-07 16:52:23 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
_filterChildren:function (topicIds, commandContext) {
|
|
|
|
var topics = commandContext.findTopics(topicIds);
|
|
|
|
|
|
|
|
var result = [];
|
2014-03-17 04:36:29 +01:00
|
|
|
_.each(topics, function (topic) {
|
2012-07-07 16:52:23 +02:00
|
|
|
var parent = topic.getParent();
|
|
|
|
var found = false;
|
|
|
|
while (parent != null && !found) {
|
|
|
|
found = topicIds.contains(parent.getId());
|
|
|
|
if (found) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
parent = parent.getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
result.push(topic);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-07-09 00:31:21 +02:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
_collectInDepthRelationships:function (topic) {
|
|
|
|
var result = [];
|
2012-09-25 01:40:01 +02:00
|
|
|
result.append(topic.getRelationships());
|
|
|
|
|
2012-07-09 00:31:21 +02:00
|
|
|
var children = topic.getChildren();
|
2012-09-25 01:40:01 +02:00
|
|
|
var rels = children.map(function (topic) {
|
|
|
|
return this._collectInDepthRelationships(topic);
|
|
|
|
}, this);
|
|
|
|
result.append(rels.flatten());
|
2012-11-17 17:42:46 +01:00
|
|
|
|
2012-11-17 17:57:06 +01:00
|
|
|
if (result.length > 0) {
|
|
|
|
// Filter for unique ...
|
|
|
|
result = result.sort(function (a, b) {
|
|
|
|
return a.getModel().getId() - b.getModel().getId();
|
|
|
|
});
|
|
|
|
var ret = [result[0]];
|
|
|
|
for (var i = 1; i < result.length; i++) { // start loop at 1 as element 0 can never be a duplicate
|
|
|
|
if (result[i - 1] !== result[i]) {
|
|
|
|
ret.push(result[i]);
|
|
|
|
}
|
2012-11-17 17:42:46 +01:00
|
|
|
}
|
2012-11-17 17:57:06 +01:00
|
|
|
result = ret;
|
2012-11-17 17:42:46 +01:00
|
|
|
}
|
2012-11-17 17:57:06 +01:00
|
|
|
return result;
|
2009-06-07 20:59:43 +02:00
|
|
|
}
|
2012-07-09 00:31:21 +02:00
|
|
|
|
2009-06-07 20:59:43 +02:00
|
|
|
});
|