wisemapping-frontend/packages/mindplot/src/components/widget/Menu.js

488 lines
15 KiB
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
2021-12-25 23:39:34 +01:00
* Copyright [2021] [wisemapping]
2021-07-16 16:41:58 +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.
*/
import $ from 'jquery';
2021-12-15 02:54:11 +01:00
import { $msg } from '../Messages';
2021-12-19 09:44:08 +01:00
import BootstrapDialogRequest from '../libraries/bootstrap/BootstrapDialogRequest';
import IMenu from './IMenu';
import FontFamilyPanel from './FontFamilyPanel';
import FontSizePanel from './FontSizePanel';
import TopicShapePanel from './TopicShapePanel';
import IconPanel from './IconPanel';
import ColorPalettePanel from './ColorPalettePanel';
import ToolbarItem from './ToolbarItem';
import KeyboardShortcutTooltip from './KeyboardShortcutTooltip';
2022-01-07 06:36:48 +01:00
import KeyboardShortcutDialog from './KeyboardShortcutDialog';
2021-07-16 16:41:58 +02:00
2021-12-04 01:11:17 +01:00
class Menu extends IMenu {
constructor(designer, containerId, mapId, readOnly, baseUrl = '') {
2021-12-04 01:11:17 +01:00
super(designer, containerId, mapId);
const saveElem = $('#save');
2021-10-05 02:05:34 +02:00
const widgetsBaseUrl = `${baseUrl}css/widget`;
// Stop event propagation ...
$(`#${this._containerId}`).bind('click', (event) => {
event.stopPropagation();
return false;
});
$(`#${this._containerId}`).bind('dblclick', (event) => {
event.stopPropagation();
return false;
});
// Create panels ...
const designerModel = designer.getModel();
const fontFamilyBtn = $('#fontFamily');
if (fontFamilyBtn) {
const fontFamilyModel = {
getValue() {
const nodes = designerModel.filterSelectedTopics();
let result = null;
for (let i = 0; i < nodes.length; i++) {
const fontFamily = nodes[i].getFontFamily();
2021-12-04 01:11:17 +01:00
if (result != null && result !== fontFamily) {
2021-10-05 02:05:34 +02:00
result = null;
break;
}
result = fontFamily;
}
return result;
},
setValue(value) {
designer.changeFontFamily(value);
},
};
this._toolbarElems.push(new FontFamilyPanel('fontFamily', fontFamilyModel));
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('fontFamily', $msg('FONT_FAMILY'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const fontSizeBtn = $('#fontSize');
if (fontSizeBtn) {
const fontSizeModel = {
getValue() {
const nodes = designerModel.filterSelectedTopics();
let result = null;
for (let i = 0; i < nodes.length; i++) {
const fontSize = nodes[i].getFontSize();
2021-12-04 01:11:17 +01:00
if (result != null && result !== fontSize) {
2021-10-05 02:05:34 +02:00
result = null;
break;
}
result = fontSize;
}
return result;
},
setValue(value) {
designer.changeFontSize(value);
},
};
this._toolbarElems.push(new FontSizePanel('fontSize', fontSizeModel));
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('fontSize', $msg('FONT_SIZE'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const topicShapeBtn = $('#topicShape');
if (topicShapeBtn) {
const topicShapeModel = {
getValue() {
const nodes = designerModel.filterSelectedTopics();
let result = null;
for (let i = 0; i < nodes.length; i++) {
const shapeType = nodes[i].getShapeType();
2021-12-04 01:11:17 +01:00
if (result != null && result !== shapeType) {
2021-10-05 02:05:34 +02:00
result = null;
break;
}
result = shapeType;
}
return result;
},
setValue(value) {
designer.changeTopicShape(value);
},
};
this._toolbarElems.push(new TopicShapePanel('topicShape', topicShapeModel));
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('topicShape', $msg('TOPIC_SHAPE'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const topicIconBtn = $('#topicIcon');
if (topicIconBtn) {
// Create icon panel dialog ...
const topicIconModel = {
getValue() {
return null;
},
setValue(value) {
designer.addIconType(value);
},
};
this._toolbarElems.push(new IconPanel('topicIcon', topicIconModel));
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('topicIcon', $msg('TOPIC_ICON'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Topic color item ...
const topicColorBtn = $('#topicColor');
if (topicColorBtn) {
const topicColorModel = {
getValue() {
const nodes = designerModel.filterSelectedTopics();
let result = null;
for (let i = 0; i < nodes.length; i++) {
const color = nodes[i].getBackgroundColor();
2021-12-04 01:11:17 +01:00
if (result != null && result !== color) {
2021-10-05 02:05:34 +02:00
result = null;
break;
}
result = color;
}
return result;
},
setValue(hex) {
designer.changeBackgroundColor(hex);
},
};
this._toolbarElems.push(new ColorPalettePanel('topicColor', topicColorModel, widgetsBaseUrl));
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('topicColor', $msg('TOPIC_COLOR'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Border color item ...
const topicBorderBtn = $('#topicBorder');
if (topicBorderBtn) {
const borderColorModel = {
getValue() {
const nodes = designerModel.filterSelectedTopics();
let result = null;
for (let i = 0; i < nodes.length; i++) {
const color = nodes[i].getBorderColor();
2021-12-04 01:11:17 +01:00
if (result != null && result !== color) {
2021-10-05 02:05:34 +02:00
result = null;
break;
}
result = color;
}
return result;
},
setValue(hex) {
designer.changeBorderColor(hex);
},
};
this._toolbarElems.push(new ColorPalettePanel('topicBorder', borderColorModel, widgetsBaseUrl));
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('topicBorder', $msg('TOPIC_BORDER_COLOR'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Font color item ...
const fontColorBtn = $('#fontColor');
if (fontColorBtn) {
const fontColorModel = {
getValue() {
let result = null;
const nodes = designerModel.filterSelectedTopics();
for (let i = 0; i < nodes.length; i++) {
const color = nodes[i].getFontColor();
2021-12-04 01:11:17 +01:00
if (result != null && result !== color) {
2021-10-05 02:05:34 +02:00
result = null;
break;
}
result = color;
}
return result;
},
setValue(hex) {
designer.changeFontColor(hex);
},
};
this._toolbarElems.push(new ColorPalettePanel('fontColor', fontColorModel, baseUrl));
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('fontColor', $msg('FONT_COLOR'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
this._addButton('export', false, false, () => {
// @Todo: this must be configured in the dialog...
2022-01-04 22:10:29 +01:00
const formatExtension = 'jpg';
2021-12-31 10:52:24 +01:00
designer.export(formatExtension)
2021-12-31 10:52:24 +01:00
.then((url) => {
// Create hidden anchor to force download ...
const anchor = document.createElement('a');
anchor.style = 'display: none';
anchor.download = `${mapId}.${formatExtension}`;
2021-12-31 10:52:24 +01:00
anchor.href = url;
document.body.appendChild(anchor);
// Trigger click ...
anchor.click();
// Clean up ...
2022-01-02 19:37:33 +01:00
URL.revokeObjectURL(url);
2021-12-31 10:52:24 +01:00
document.body.removeChild(anchor);
});
2021-12-31 07:25:25 +01:00
2021-12-31 08:47:47 +01:00
// Create anchor element ...
2021-10-05 02:05:34 +02:00
});
2021-12-31 10:18:21 +01:00
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('export', $msg('EXPORT'));
2021-10-05 02:05:34 +02:00
this._addButton('print', false, false, () => {
this.save(saveElem, designer, false);
const urlPrefix = window.location.href.substring(0, window.location.href.lastIndexOf('c/maps/'));
window.open(`${urlPrefix}c/maps/${mapId}/print`);
2021-10-05 02:05:34 +02:00
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('print', $msg('PRINT'));
2021-10-05 02:05:34 +02:00
this._addButton('zoomIn', false, false, () => {
designer.zoomIn();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('zoomIn', $msg('ZOOM_IN'));
2021-10-05 02:05:34 +02:00
this._addButton('zoomOut', false, false, () => {
designer.zoomOut();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('zoomOut', $msg('ZOOM_OUT'));
2021-10-05 02:05:34 +02:00
const undoButton = this._addButton('undoEdition', false, false, () => {
designer.undo();
});
if (undoButton) {
undoButton.disable();
}
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('undoEdition', $msg('UNDO'), 'meta+Z');
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const redoButton = this._addButton('redoEdition', false, false, () => {
designer.redo();
});
if (redoButton) {
redoButton.disable();
}
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('redoEdition', $msg('REDO'), 'meta+shift+Z');
2021-10-05 02:05:34 +02:00
if (redoButton && undoButton) {
designer.addEvent('modelUpdate', (event) => {
if (event.undoSteps > 0) {
undoButton.enable();
} else {
undoButton.disable();
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
if (event.redoSteps > 0) {
redoButton.enable();
} else {
redoButton.disable();
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
this._addButton('addTopic', true, false, () => {
designer.createSiblingForSelectedNode();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('addTopic', $msg('ADD_TOPIC'), 'Enter');
2021-10-05 02:05:34 +02:00
this._addButton('deleteTopic', true, true, () => {
designer.deleteSelectedEntities();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('deleteTopic', $msg('TOPIC_DELETE'), 'Delete');
2021-10-05 02:05:34 +02:00
this._addButton('topicLink', true, false, () => {
designer.addLink();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('topicLink', $msg('TOPIC_LINK'));
2021-10-05 02:05:34 +02:00
this._addButton('topicRelation', true, false, (event) => {
designer.showRelPivot(event);
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('topicRelation', $msg('TOPIC_RELATIONSHIP'));
2021-10-05 02:05:34 +02:00
this._addButton('topicNote', true, false, () => {
designer.addNote();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('topicNote', $msg('TOPIC_NOTE'));
2021-10-05 02:05:34 +02:00
this._addButton('fontBold', true, false, () => {
designer.changeFontWeight();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('fontBold', $msg('FONT_BOLD'), 'meta+B');
2021-10-05 02:05:34 +02:00
this._addButton('fontItalic', true, false, () => {
designer.changeFontStyle();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('fontItalic', $msg('FONT_ITALIC'), 'meta+I');
2021-10-05 02:05:34 +02:00
if (saveElem) {
this._addButton('save', false, false,
() => {
this.save(saveElem, designer, true);
2021-07-16 16:41:58 +02:00
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('save', $msg('SAVE'), 'meta+S');
2021-10-05 02:05:34 +02:00
if (!readOnly) {
// To prevent the user from leaving the page with changes ...
// Element.NativeEvents.unload = 1;
$(window).bind('unload', () => {
if (this.isSaveRequired()) {
this.save(saveElem, designer, false, true);
2021-10-05 02:05:34 +02:00
}
this.unlockMap(designer);
2021-07-16 16:41:58 +02:00
});
2021-10-05 02:05:34 +02:00
// Autosave on a fixed period of time ...
setInterval(
() => {
if (this.isSaveRequired()) {
this.save(saveElem, designer, false);
2021-10-05 02:05:34 +02:00
}
}, 30000,
);
}
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const discardElem = $('#discard');
if (discardElem) {
this._addButton('discard', false, false, () => {
this.discardChanges(designer);
2021-10-05 02:05:34 +02:00
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('discard', $msg('DISCARD_CHANGES'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const shareElem = $('#shareIt');
if (shareElem) {
this._addButton('shareIt', false, false, () => {
2021-12-05 00:39:20 +01:00
BootstrapDialogRequest.active = new BootstrapDialogRequest(`c/maps/${mapId}/sharef`, $msg('COLLABORATE'), {
2021-10-05 02:05:34 +02:00
closeButton: true,
cancelButton: true,
2021-07-16 16:41:58 +02:00
});
2021-10-05 02:05:34 +02:00
designer.onObjectFocusEvent();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('shareIt', $msg('COLLABORATE'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const publishElem = $('#publishIt');
if (publishElem) {
this._addButton('publishIt', false, false, () => {
2021-12-05 00:39:20 +01:00
BootstrapDialogRequest.active = new BootstrapDialogRequest(`c/maps/${mapId}/publishf`, $msg('PUBLISH'), {
2021-10-05 02:05:34 +02:00
closeButton: true,
cancelButton: true,
2021-07-16 16:41:58 +02:00
});
2021-10-05 02:05:34 +02:00
designer.onObjectFocusEvent();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('publishIt', $msg('PUBLISH'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const historyElem = $('#history');
if (historyElem) {
this._addButton('history', false, false, () => {
2021-12-05 00:39:20 +01:00
BootstrapDialogRequest.active = new BootstrapDialogRequest(`c/maps/${mapId}/historyf`, $msg('HISTORY'), {
2021-10-05 02:05:34 +02:00
closeButton: true,
cancelButton: true,
2021-07-16 16:41:58 +02:00
});
2021-10-05 02:05:34 +02:00
designer.onObjectFocusEvent();
});
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('history', $msg('HISTORY'));
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
this._registerEvents(designer);
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Keyboard Shortcuts Action ...
const keyboardShortcut = $('#keyboardShortcuts');
if (keyboardShortcut) {
keyboardShortcut.bind('click', (event) => {
2022-01-07 06:36:48 +01:00
BootstrapDialogRequest.active = new KeyboardShortcutDialog();
2021-10-05 02:05:34 +02:00
designer.onObjectFocusEvent();
event.preventDefault();
});
}
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
_registerEvents(designer) {
// Register on close events ...
2021-12-05 17:14:15 +01:00
this._toolbarElems.forEach((elem) => {
2021-10-05 02:05:34 +02:00
elem.addEvent('show', () => {
2022-01-07 06:36:48 +01:00
this.clear();
2021-10-05 02:05:34 +02:00
});
});
designer.addEvent('onblur', () => {
const topics = designer.getModel().filterSelectedTopics();
const rels = designer.getModel().filterSelectedRelationships();
2022-01-07 06:36:48 +01:00
this._toolbarElems.forEach((button) => {
2021-10-05 02:05:34 +02:00
const isTopicAction = button.isTopicAction();
const isRelAction = button.isRelAction();
if (isTopicAction || isRelAction) {
2021-12-05 17:14:15 +01:00
if ((isTopicAction && topics.length !== 0) || (isRelAction && rels.length !== 0)) {
2021-10-05 02:05:34 +02:00
button.enable();
} else {
button.disable();
}
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
designer.addEvent('onfocus', () => {
const topics = designer.getModel().filterSelectedTopics();
const rels = designer.getModel().filterSelectedRelationships();
2021-07-16 16:41:58 +02:00
2022-01-07 06:36:48 +01:00
this._toolbarElems.forEach((button) => {
2021-10-05 02:05:34 +02:00
const isTopicAction = button.isTopicAction();
const isRelAction = button.isRelAction();
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
if (isTopicAction || isRelAction) {
if (isTopicAction && topics.length > 0) {
button.enable();
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
if (isRelAction && rels.length > 0) {
button.enable();
}
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
});
});
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
_addButton(buttonId, topic, rel, fn) {
// Register Events ...
let result = null;
if ($(`#${buttonId}`)) {
const button = new ToolbarItem(buttonId, ((event) => {
fn(event);
2021-12-31 08:47:47 +01:00
this.clear();
2021-10-05 02:05:34 +02:00
}), { topicAction: topic, relAction: rel });
this._toolbarElems.push(button);
result = button;
}
return result;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2021-12-04 01:11:17 +01:00
static _registerTooltip(buttonId, text, shortcut) {
2021-10-05 02:05:34 +02:00
if ($(`#${buttonId}`)) {
let tooltip = text;
if (shortcut) {
const platformedShortcut = navigator.appVersion.indexOf('Mac') !== -1
? shortcut.replace('meta+', '⌘')
: shortcut.replace('meta+', 'ctrl+');
tooltip = `${tooltip} (${platformedShortcut})`;
2021-10-05 02:05:34 +02:00
}
2021-12-19 17:06:42 +01:00
return new KeyboardShortcutTooltip($(`#${buttonId}`), tooltip);
2021-07-16 16:41:58 +02:00
}
return undefined;
2021-12-04 01:11:17 +01:00
}
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
export default Menu;