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

97 lines
3.4 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.
*/
2021-12-04 02:03:49 +01:00
import { $defined, $assert } from '@wisemapping/core-js';
import _ from '@libraries/underscore-min';
import Command from '../Command';
2021-07-16 16:41:58 +02:00
2021-12-05 00:39:20 +01:00
class GenericFunctionCommand extends Command {
2021-10-05 02:05:34 +02:00
/**
* @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
2021-07-16 16:41:58 +02:00
* 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
* @param {Object} [value] value arbitrary value necessary for the execution of the function,
* e.g. color, font family or text
* @extends mindplot.Command
*/
2021-12-05 00:39:20 +01:00
constructor(commandFunc, topicsIds, value) {
2021-10-05 02:05:34 +02:00
$assert(commandFunc, 'commandFunc must be defined');
$assert($defined(topicsIds), 'topicsIds must be defined');
2021-07-16 16:41:58 +02:00
2021-12-05 00:39:20 +01:00
super();
2021-10-05 02:05:34 +02:00
this._value = value;
this._topicsId = topicsIds;
this._commandFunc = commandFunc;
this._oldValues = [];
2021-12-05 00:39:20 +01:00
}
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) {
if (!this.applied) {
let topics = null;
try {
topics = commandContext.findTopics(this._topicsId);
} catch (e) {
if (this._commandFunc.commandType != 'changeTextToTopic') {
// Workaround: For some reason, there is a combination of events that involves
// making some modification and firing out of focus event. This is causing
// that a remove node try to be removed. In some other life, I will come with the solution.
// Almost aways occurs with IE9. I could be related with some change of order in sets o something similar.
throw e;
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
if (topics != null) {
const me = this;
_.each(topics, (topic) => {
const oldValue = me._commandFunc(topic, me._value);
me._oldValues.push(oldValue);
});
}
this.applied = true;
} else {
throw 'Command can not be applied two times in a row.';
}
2021-12-05 00:39:20 +01:00
}
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) {
if (this.applied) {
const topics = commandContext.findTopics(this._topicsId);
const me = this;
_.each(topics, (topic, index) => {
me._commandFunc(topic, me._oldValues[index]);
});
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
this.applied = false;
this._oldValues = [];
} else {
throw new Error('undo can not be applied.');
2021-07-16 16:41:58 +02:00
}
2021-12-05 00:39:20 +01:00
}
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
export default GenericFunctionCommand;