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

455 lines
14 KiB
TypeScript
Raw Normal View History

/* eslint-disable no-new */
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';
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';
2022-01-08 02:27:00 +01:00
import AccountSettingsPanel from './AccountSettingsPanel';
2022-01-09 01:23:05 +01:00
import Designer from '../Designer';
2021-07-16 16:41:58 +02:00
2021-12-04 01:11:17 +01:00
class Menu extends IMenu {
constructor(designer: Designer, containerId: string, mapId: string, readOnly = false, 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`;
// 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();
2022-01-09 01:23:05 +01:00
2021-10-05 02:05:34 +02:00
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;
},
2022-01-26 07:33:39 +01:00
setValue(value:string) {
2021-10-05 02:05:34 +02:00
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;
},
2022-01-26 07:33:39 +01:00
setValue(value: string) {
2021-10-05 02:05:34 +02:00
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;
},
2022-01-26 07:33:39 +01:00
setValue(hex:string) {
2021-10-05 02:05:34 +02:00
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-12-04 01:11:17 +01:00
Menu._registerTooltip('export', $msg('EXPORT'));
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('zoom-plus', false, false, () => {
designer.zoomIn();
});
Menu._registerTooltip('zoom-plus', $msg('ZOOM_IN'));
this._addButton('zoom-minus', false, false, () => {
designer.zoomOut();
2021-10-05 02:05:34 +02:00
});
Menu._registerTooltip('zoom-minus', $msg('ZOOM_OUT'));
2021-10-05 02:05:34 +02:00
this._addButton('position', false, false, () => {
2022-01-10 19:52:11 +01:00
designer.zoomToFit();
});
Menu._registerTooltip('position', $msg('CENTER_POSITION'));
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.length !== 0) {
2021-10-05 02:05:34 +02:00
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.length !== 0) {
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.length !== 0) {
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.length !== 0) {
2021-12-04 01:11:17 +01:00
Menu._registerTooltip('history', $msg('HISTORY'));
2021-10-05 02:05:34 +02:00
}
2022-01-10 21:12:15 +01:00
2021-10-05 02:05:34 +02:00
// Keyboard Shortcuts Action ...
const keyboardShortcut = $('#keyboardShortcuts');
if (keyboardShortcut.length !== 0) {
2021-10-05 02:05:34 +02:00
keyboardShortcut.bind('click', (event) => {
new KeyboardShortcutDialog();
2021-10-05 02:05:34 +02:00
designer.onObjectFocusEvent();
event.preventDefault();
});
Menu._registerTooltip('keyboardShortcuts', $msg('KEYBOARD_SHOTCUTS'));
2021-10-05 02:05:34 +02:00
}
const backTolist = $('#backToList');
if (backTolist.length !== 0) {
backTolist.bind('click', (event) => {
event.stopPropagation();
2022-01-09 01:23:05 +01:00
window.location.href = '/c/maps/';
return false;
});
Menu._registerTooltip('backToList', $msg('BACK_TO_MAP_LIST'));
}
2022-01-08 02:27:00 +01:00
// Account dialog ...
const accountSettings = $('#account');
if (accountSettings.length !== 0) {
2022-01-08 02:27:00 +01:00
accountSettings.bind('click', (event) => {
event.preventDefault();
});
this._toolbarElems.push(new AccountSettingsPanel('account'));
2022-01-08 02:47:23 +01:00
Menu._registerTooltip('account', `${global.accountEmail}`);
2022-01-08 02:27:00 +01:00
}
this._registerEvents(designer);
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-10 21:12:15 +01:00
private _registerEvents(designer:Designer) {
2021-10-05 02:05:34 +02:00
// Register on close events ...
2022-01-08 02:27:00 +01:00
this._toolbarElems.forEach((panel) => {
panel.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-08 02:27:00 +01:00
this._toolbarElems.forEach((panel) => {
const isTopicAction = panel.isTopicAction();
const isRelAction = panel.isRelAction();
2021-10-05 02:05:34 +02:00
if (isTopicAction || isRelAction) {
2021-12-05 17:14:15 +01:00
if ((isTopicAction && topics.length !== 0) || (isRelAction && rels.length !== 0)) {
2022-01-08 02:27:00 +01:00
panel.enable();
2021-10-05 02:05:34 +02:00
} else {
2022-01-08 02:27:00 +01:00
panel.disable();
2021-10-05 02:05:34 +02: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
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
2022-01-10 21:12:15 +01:00
private _addButton(buttonId:string, isTopic:boolean, isRelationship:boolean, fn) {
2021-10-05 02:05:34 +02:00
// 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();
2022-01-10 21:12:15 +01:00
}), { topicAction: isTopic, relAction: isRelationship });
2021-10-05 02:05:34 +02:00
this._toolbarElems.push(button);
result = button;
}
return result;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-09 01:23:05 +01:00
static _registerTooltip(buttonId: string, text: string, shortcut: string = null) {
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;