Move loader to common code.

This commit is contained in:
Paulo Gustavo Veiga 2021-12-23 10:51:26 -08:00
parent 76dcadccd6
commit ddd4d91b22
7 changed files with 185 additions and 200 deletions

View File

@ -0,0 +1,115 @@
/*
* 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 { $assert } from '@wisemapping/core-js';
import $ from 'jquery';
import PersistenceManager from './PersistenceManager';
import Designer from './Designer';
import Menu from './widget/Menu';
import $notifyModal from './widget/ModalDialogNotifier';
import { $msg } from './Messages';
global.jQuery = $;
global.$ = $;
let designer = null;
export function buildDesigner(options) {
const container = $(`#${options.container}`);
$assert(container, 'container could not be null');
// Register load events ...
designer = new Designer(options, container);
designer.addEvent('loadSuccess', () => {
window.mindmapLoadReady = true;
console.log('Map loadded successfully');
});
const onerrorFn = (message, url, lineNo) => {
// Close loading dialog ...
if (window.waitDialog) {
window.waitDialog.close();
window.waitDialog = null;
}
// Open error dialog only in case of mindmap loading errors. The rest of the error are reported but not display the dialog.
// Remove this in the near future.
if (!window.mindmapLoadReady) {
$notifyModal($msg('UNEXPECTED_ERROR_LOADING'));
}
};
window.onerror = onerrorFn;
// Configure default persistence manager ...
const persistence = options.persistenceManager;
$assert(persistence, "persistence must be defined");
PersistenceManager.init(persistence);
// Register toolbar event ...
if ($('#toolbar').length) {
const menu = new Menu(designer, 'toolbar', options.mapId, '');
// If a node has focus, focus can be move to another node using the keys.
designer._cleanScreen = function _cleanScreen() {
menu.clear();
};
}
return designer;
}
export function buildDefaultOptions(persistence, readOnly = false) {
$assert(persistence, "persistence must be defined");
// Set workspace screen size as default. In this way, resize issues are solved.
const containerSize = {
height: Number.parseInt(window.screen.height, 10),
width: Number.parseInt(window.screen.width, 10),
};
const viewPort = {
height: Number.parseInt(window.innerHeight - 70, 10), // Footer and Header
width: Number.parseInt(window.innerWidth, 10),
};
return {
readOnly: readOnly,
zoom: 0.85,
saveOnLoad: true,
size: containerSize,
viewPort,
container: 'mindplot',
locale: 'en',
persistenceManager: persistence,
};
}
export async function loadOptions(jsonConf, persistence, readOnly = false) {
const result = await $.ajax({
url: jsonConf,
dataType: 'json',
method: 'get',
});
result.readOnly = readOnly;
result.persistenceManager = persistence;
return result;
}
export function loadExample(exampleFn) {
$(() => {
// Hack: load bootstrap first
import('@libraries/bootstrap').then(exampleFn);
});
}

View File

@ -1,9 +1,11 @@
import * as DesignerBuilder from './components/DesignerBuilder';
import Mindmap from './components/model/Mindmap'; import Mindmap from './components/model/Mindmap';
import PersistenceManager from './components/PersistenceManager'; import PersistenceManager from './components/PersistenceManager';
import Designer from './components/Designer'; import Designer from './components/Designer';
import LocalStorageManager from './components/LocalStorageManager'; import LocalStorageManager from './components/LocalStorageManager';
import Menu from './components/widget/Menu'; import Menu from './components/widget/Menu';
export { export {
Mindmap, PersistenceManager, Designer, LocalStorageManager, Menu, Mindmap, PersistenceManager, Designer, LocalStorageManager, Menu, DesignerBuilder,
}; };

View File

@ -0,0 +1,49 @@
import { $notify } from '@wisemapping/core-js';
import { buildDesigner, loadDesignerOptions } from './components/DesignerBuilder';
import LocalStorageManager from './components/LocalStorageManager';
import RESTPersistenceManager from './components/RestPersistenceManager';
import PersistenceManager from './components/PersistenceManager';
global.memoryPersistence = false;
global.readOnlyMode = false;
global.userOptions = {};
global.locale = 'us';
global.mindmapLocked = false;
global.mapLockedMessage = 'map locked';
global.lockSession = 111111;
global.lockTimestamp = 11111;
// Configure designer options ...
const options = loadDesignerOptions();
if (!global.memoryPersistence && !global.readOnlyMode) {
options.persistenceManager = new RESTPersistenceManager(
{
documentUrl: 'c/restful/maps/{id}/document',
revertUrl: 'c/restful/maps/{id}/history/latest',
lockUrl: 'c/restful/maps/{id}/lock',
timestamp: global.lockTimestamp,
session: global.lockSession,
},
);
} else {
// options.persistenceManager = new LocalStorageManager("c/restful/maps/{id}${hid != null ? '/' : ''}${hid != null ? hid : ''}/document/xml${principal != null ? '' : '-pub'}", true);
}
options.zoom = global.userOptions.zoom;
options.readOnly = false;
// Set map id ...
options.mapId = global.mapId;
// Build designer ...
const designer = buildDesigner(options);
// Load map from XML file persisted on disk...
const persistence = PersistenceManager.getInstance();
const mindmap = persistence.load(global.mapId);
designer.loadMap(mindmap);
if (global.mindmapLocked) {
$notify(global.mappL, false);
}

View File

@ -1,10 +1,10 @@
import '../css/editor.less'; import '../css/editor.less';
import { buildDesigner, loadDesignerOptions, loadExample } from './loader'; import { buildDesigner, buildDefaultOptions, loadExample } from '../../../../src/components/DesignerBuilder';
import { PersistenceManager } from '../../../../src'; import { PersistenceManager, LocalStorageManager } from '../../../../src';
const example = async () => { const example = async () => {
const mapId = 'welcome'; const p = new LocalStorageManager('samples/{id}.xml');
const options = await loadDesignerOptions(); const options = buildDefaultOptions(p, true);
const designer = buildDesigner(options); const designer = buildDesigner(options);
designer.addEvent('loadSuccess', () => { designer.addEvent('loadSuccess', () => {
@ -12,6 +12,7 @@ const example = async () => {
}); });
// Load map from XML file persisted on disk... // Load map from XML file persisted on disk...
const mapId = 'welcome';
const persistence = PersistenceManager.getInstance(); const persistence = PersistenceManager.getInstance();
const mindmap = persistence.load(mapId); const mindmap = persistence.load(mapId);
designer.loadMap(mindmap); designer.loadMap(mindmap);

View File

@ -1,27 +1,21 @@
import '../css/embedded.less'; import '../css/embedded.less';
import { buildDesigner, loadDesignerOptions, loadExample } from './loader'; import { buildDesigner, buildDefaultOptions, loadExample } from '../../../../src/components/DesignerBuilder';
import { Mindmap, PersistenceManager } from '../../../../src'; import { PersistenceManager, LocalStorageManager } from '../../../../src';
const example = async () => { const example = async () => {
const mapId = 'welcome';
// Options has been defined in by a external ile ? // Options has been defined in by a external ile ?
const queryString = window.location.search; const p = new LocalStorageManager('samples/{id}.xml');
const confUrl = queryString.replace('?confUrl=', ''); const options = buildDefaultOptions(p);
const options = await loadDesignerOptions(confUrl);
const designer = buildDesigner(options); const designer = buildDesigner(options);
designer.addEvent('loadSuccess', () => { designer.addEvent('loadSuccess', () => {
document.getElementById('mindplot').classList.add('ready'); document.getElementById('mindplot').classList.add('ready');
}); });
// Load map from XML file persisted on disk... // Load map from XML file persisted on disk...
const mapId = 'welcome';
const persistence = PersistenceManager.getInstance(); const persistence = PersistenceManager.getInstance();
let mindmap; const mindmap = persistence.load(mapId);
try {
mindmap = persistence.load(mapId);
} catch (e) {
console.error('The map could not be loaded, loading an empty map instead.', e);
mindmap = Mindmap.buildEmpty(mapId);
}
designer.loadMap(mindmap); designer.loadMap(mindmap);
}; };

View File

@ -1,170 +0,0 @@
/*
* 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 { $assert } from '@wisemapping/core-js';
import { PersistenceManager, Designer, LocalStorageManager, Menu } from '../../../../src/';
import * as mindplot from '../../../../src/';
import { $msg } from '../../../../src/components/Messages';
import $notifyModal from '../../../../src/components/widget/ModalDialogNotifier';
import $ from 'jquery';
global.jQuery = $;
global.$ = $;
let designer = null;
/*
* Disclaimer: this global variable is a temporary workaround to Mootools' Browser class
* We need to avoid browser detection and replace it with feature detection,
* jquery recommends: http://www.modernizr.com/
*/
global.Browser = {
firefox: window.globalStorage,
ie: document.all && !window.opera,
ie6: !window.XMLHttpRequest,
ie7: document.all && window.XMLHttpRequest && !XDomainRequest && !window.opera,
ie8: document.documentMode == 8,
ie11: document.documentMode == 11,
opera: Boolean(window.opera),
chrome: Boolean(window.chrome),
safari: window.getComputedStyle && !window.globalStorage && !window.opera,
Platform: {
mac: navigator.platform.indexOf('Mac') != -1,
},
};
export function buildDesigner(options) {
const container = $(`#${options.container}`);
$assert(container, 'container could not be null');
// Register load events ...
designer = new Designer(options, container);
designer.addEvent('loadSuccess', () => {
window.mindmapLoadReady = true;
console.log('Map loadded successfully');
});
const onerrorFn = function onerror(message, url, lineNo) {
// Ignore errors ...
if (message === 'Script error.' && lineNo == 0) {
// http://stackoverflow.com/questions/5913978/cryptic-script-error-reported-in-javascript-in-chrome-and-firefox
return;
}
// Transform error ...
let errorMsg = message;
if (typeof message === 'object' && message.srcElement && message.target) {
if (
message.srcElement == '[object HTMLScriptElement]' &&
message.target == '[object HTMLScriptElement]'
) {
errorMsg = 'Error loading script';
} else {
errorMsg = `Event Error - target:${message.target} srcElement:${message.srcElement}`;
}
}
errorMsg = errorMsg.toString();
// Close loading dialog ...
if (window.waitDialog) {
window.waitDialog.close();
window.waitDialog = null;
}
// Open error dialog only in case of mindmap loading errors. The rest of the error are reported but not display the dialog.
// Remove this in the near future.
if (!window.mindmapLoadReady) {
$notifyModal($msg('UNEXPECTED_ERROR_LOADING'));
}
console.error(errorMsg, 'line:', lineNo, 'url:', url);
};
window.onerror = onerrorFn;
// Configure default persistence manager ...
let persistence;
if (options.persistenceManager) {
if (typeof options.persistenceManager === 'string') {
const managerClass = /^mindplot\.(\w+)/.exec(options.persistenceManager);
if (managerClass) {
persistence = new mindplot[managerClass[1]]('samples/{id}.xml');
} else {
persistence = eval(`new ${options.persistenceManager}()`);
}
} else {
persistence = options.persistenceManager;
}
} else {
persistence = new LocalStorageManager('samples/{id}.xml');
}
PersistenceManager.init(persistence);
// Register toolbar event ...
if ($('#toolbar').length) {
const menu = new Menu(designer, 'toolbar', options.mapId, '');
// If a node has focus, focus can be move to another node using the keys.
designer._cleanScreen = function () {
menu.clear();
};
}
return designer;
}
export async function loadDesignerOptions(jsonConf) {
// Load map options ...
let result;
if (jsonConf) {
result = await $.ajax({
url: jsonConf,
dataType: 'json',
method: 'get',
});
} else {
// Set workspace screen size as default. In this way, resize issues are solved.
const containerSize = {
height: parseInt(screen.height),
width: parseInt(screen.width),
};
const viewPort = {
height: parseInt(window.innerHeight - 70), // Footer and Header
width: parseInt(window.innerWidth),
};
result = {
readOnly: false,
zoom: 0.85,
saveOnLoad: true,
size: containerSize,
viewPort,
container: 'mindplot',
locale: 'en',
};
}
return result;
}
export function loadExample(exampleFn) {
$(() => {
// Hack: load bootstrap first
import('@libraries/bootstrap').then(exampleFn);
});
}

View File

@ -1,25 +1,19 @@
import '../css/embedded.less'; import '../css/embedded.less';
import { buildDesigner, loadDesignerOptions, loadExample } from './loader'; import { buildDesigner, buildDefaultOptions, loadExample } from '../../../../src/components/DesignerBuilder';
import { Mindmap, PersistenceManager } from '../../../../src'; import { PersistenceManager, LocalStorageManager } from '../../../../src';
const example = async () => { const example = async () => {
const p = new LocalStorageManager('samples/{id}.xml');
const options = buildDefaultOptions(p, true);
const mapId = 'welcome'; const mapId = 'welcome';
// Set readonly option ...
const options = await loadDesignerOptions();
options.readOnly = true;
const designer = buildDesigner(options); const designer = buildDesigner(options);
designer.addEvent('loadSuccess', () => { designer.addEvent('loadSuccess', () => {
document.getElementById('mindplot').classList.add('ready'); document.getElementById('mindplot').classList.add('ready');
}); });
// Load map from XML file persisted on disk... // Load map from XML file persisted on disk...
const persistence = PersistenceManager.getInstance(); const persistence = PersistenceManager.getInstance();
let mindmap; const mindmap = persistence.load(mapId);
try {
mindmap = persistence.load(mapId);
} catch (e) {
console.error('The map could not be loaded, loading an empty map instead.', e);
mindmap = Mindmap.buildEmpty(mapId);
}
designer.loadMap(mindmap); designer.loadMap(mindmap);
}; };