wisemapping-frontend/packages/mindplot/src/components/DesignerModel.js
Matias Arriola cb2ca74a20 Merged in web2d-coreJS-solutions (pull request #5)
Core-js, web2d and mindplot working baseline

* fix .eslintignore
remove Raphael dependency

* Fix to avoid crashes in  _plotPrediction whitout Raphael

* Fix minplot basic code inspections

* Fix last inspections errors

* Inital refactor copying files

* Clean up.

* Fix web2d cyclic dependencies
remove only-warn eslint plugin
set import/no-extraneous-dependencies to warn (incorrectly complaining about root package)

* Fix web2d Point references (no need to assign it to core)
Fix web2d imports in mindplot and update Point refs

* Merge 'feature/mindplot_tests' into web2d-coreJS-solutions

* mindplot fixes and add viewmode.html playground

setup playground config to run the map-render examples
fix mindplot components export
mootools Static was not working so refactored it
fix some references to _peer
fix messages __bundle undefined
add web2d missing export: Image
downgrade cypress to avoid SIGSEGV error


Approved-by: Paulo Veiga
2021-12-02 00:41:56 +00:00

190 lines
5.4 KiB
JavaScript

/*
* 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 Events from './Events';
const DesignerModel = new Class(/** @lends DesignerModel */{
Implements: [Events],
/**
* @implements {mindplot.Events}
* @constructs
* @param {{readOnly: Boolean, zoom: Number, saveOnLoad: Boolean, size: {width: Number,
* height: Number}, viewPort: {width: Number, height: Number}, container: String,
* persistenceManager: String, mapId: String, locale: String}} options
* options loaded from json config
* @see {@link ConfigParameters.md}
* @see {@link editor.html}
*/
initialize(options) {
this._zoom = options.zoom;
this._topics = [];
this._relationships = [];
},
/** @return {Number} zoom between 0.3 (largest text) and 1.9 */
getZoom() {
return this._zoom;
},
/** @param {Number} zoom number between 0.3 and 1.9 to set the zoom to */
setZoom(zoom) {
this._zoom = zoom;
},
/** @return {@link mindplot.Topic[]} all topics */
getTopics() {
return this._topics;
},
/** @return {mindplot.Relationship[]} all relationships */
getRelationships() {
return this._relationships;
},
/** @return {mindplot.CentralTopic} the central topic */
getCentralTopic() {
const topics = this.getTopics();
return topics[0];
},
/** @return {mindplot.Topic[]} selected topics */
filterSelectedTopics() {
const result = [];
for (let i = 0; i < this._topics.length; i++) {
if (this._topics[i].isOnFocus()) {
result.push(this._topics[i]);
}
}
return result;
},
/**
* @return {mindplot.Relationship[]} selected relationships
*/
filterSelectedRelationships() {
const result = [];
for (let i = 0; i < this._relationships.length; i++) {
if (this._relationships[i].isOnFocus()) {
result.push(this._relationships[i]);
}
}
return result;
},
/**
* @return {Array.<mindplot.Relationship, mindplot.Topic>} all topics and relationships
*/
getEntities() {
const result = [].append(this._topics);
result.append(this._relationships);
return result;
},
/**
* removes occurrences of the given topic from the topic array
* @param {mindplot.Topic} topic the topic to remove
*/
removeTopic(topic) {
$assert(topic, 'topic can not be null');
this._topics.erase(topic);
},
/**
* removes occurrences of the given relationship from the relationship array
* @param {mindplot.Relationship} rel the relationship to remove
*/
removeRelationship(rel) {
$assert(rel, 'rel can not be null');
this._relationships.erase(rel);
},
/**
* adds the given topic to the topic array
* @param {mindplot.Topic} topic the topic to add
* @throws will throw an error if topic is null or undefined
* @throws will throw an error if the topic's id is not a number
*/
addTopic(topic) {
$assert(topic, 'topic can not be null');
$assert(typeof topic.getId() === 'number', `id is not a number:${topic.getId()}`);
this._topics.push(topic);
},
/**
* adds the given relationship to the relationship array
* @param {mindplot.Relationship} rel the relationship to add
* @throws will throw an error if rel is null or undefined
*/
addRelationship(rel) {
$assert(rel, 'rel can not be null');
this._relationships.push(rel);
},
/**
* @param {Function=} validate a function to validate nodes
* @param {String=} errorMsg an error message to display if the validation fails
* @return {String} returns an array of the selected (and, if applicable, valid) topics' ids
*/
filterTopicsIds(validate, errorMsg) {
const result = [];
const topics = this.filterSelectedTopics();
let isValid = true;
for (let i = 0; i < topics.length; i++) {
const selectedNode = topics[i];
if ($defined(validate)) {
isValid = validate(selectedNode);
}
// Add node only if it's valid.
if (isValid) {
result.push(selectedNode.getId());
} else {
$notify(errorMsg);
}
}
return result;
},
/**
* @return {mindplot.Topic} the first selected topic if one or more are found by the
* filterSelectedTopics function, null otherwise
*/
selectedTopic() {
const topics = this.filterSelectedTopics();
return (topics.length > 0) ? topics[0] : null;
},
/**
* @param {String} id the id of the topic to be retrieved
* @return {mindplot.Topic} the topic with the respective id
*/
findTopicById(id) {
let result = null;
for (let i = 0; i < this._topics.length; i++) {
const topic = this._topics[i];
if (topic.getId() == id) {
result = topic;
break;
}
}
return result;
},
});
export default DesignerModel;