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

96 lines
3.6 KiB
JavaScript
Raw Normal View History

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
mindplot.commands.GenericFunctionCommand = new Class(/** @lends GenericFunctionCommand */{
2011-07-26 20:07:53 +02:00
Extends:mindplot.Command,
/**
* @classdesc This command handles do/undo of different actions, e.g. moving topics to
* a different position, changing text or font,... (for full reference check the
* StandaloneActionDispatcher i.e. the ActionDispatcher subclass in use)
* @constructs
* @param {Function} commandFunc the function the command shall execute
* @param {String|Array<String>} topicsIds the ids of the topics affected
2015-04-12 05:15:12 +02:00
* @param {Object} [value] value arbitrary value necessary for the execution of the function,
* e.g. color, font family or text
* @extends mindplot.Command
*/
2012-10-02 04:26:28 +02:00
initialize:function (commandFunc, topicsIds, value) {
$assert(commandFunc, "commandFunc must be defined");
2011-11-30 04:59:36 +01:00
$assert($defined(topicsIds), "topicsIds must be defined");
this.parent();
2009-06-07 20:59:43 +02:00
this._value = value;
2012-09-27 13:12:03 +02:00
this._topicsId = topicsIds;
2009-06-07 20:59:43 +02:00
this._commandFunc = commandFunc;
this._oldValues = [];
},
2012-01-17 04:26:29 +01:00
/**
* Overrides abstract parent method
*/
2012-10-02 04:26:28 +02:00
execute:function (commandContext) {
2011-07-30 23:55:32 +02:00
if (!this.applied) {
2012-10-02 04:26:28 +02:00
2012-11-05 04:11:14 +01:00
var topics = null;
2012-10-02 04:26:28 +02:00
try {
topics = commandContext.findTopics(this._topicsId);
} catch (e) {
2012-11-05 04:11:14 +01:00
if (this._commandFunc.commandType != "changeTextToTopic") {
// Workaround: For some reason, there is a combination of events that involves
2012-11-05 04:51:06 +01:00
// making some modification and firing out of focus event. This is causing
2012-11-05 04:11:14 +01:00
// that a remove node try to be removed. In some other life, I will come with the solution.
2012-11-05 04:51:06 +01:00
// Almost aways occurs with IE9. I could be related with some change of order in sets o something similar.
2012-11-05 04:11:14 +01:00
throw e;
}
2012-10-02 04:26:28 +02:00
}
2012-11-05 04:11:14 +01:00
if (topics != null) {
var me = this;
_.each(topics, function (topic) {
var oldValue = me._commandFunc(topic, me._value);
me._oldValues.push(oldValue);
});
2012-11-05 04:11:14 +01:00
}
2009-06-07 20:59:43 +02:00
this.applied = true;
2012-11-05 04:11:14 +01:00
2011-07-30 23:55:32 +02:00
} else {
2009-06-07 20:59:43 +02:00
throw "Command can not be applied two times in a row.";
}
},
2012-01-17 04:26:29 +01:00
/**
* Overrides abstract parent method
* @see {@link mindplot.Command.undoExecute}
*/
2012-10-02 04:26:28 +02:00
undoExecute:function (commandContext) {
2011-07-30 23:55:32 +02:00
if (this.applied) {
2012-09-27 13:12:03 +02:00
var topics = commandContext.findTopics(this._topicsId);
var me = this;
_.each(topics, function (topic, index) {
me._commandFunc(topic, me._oldValues[index]);
2009-06-07 20:59:43 +02:00
});
2009-06-07 20:59:43 +02:00
this.applied = false;
this._oldValues = [];
2011-07-30 23:55:32 +02:00
} else {
2009-06-07 20:59:43 +02:00
throw "undo can not be applied.";
}
}
});