scinote-web/app/assets/javascripts/marvinjs_editor.js

362 lines
10 KiB
JavaScript
Raw Normal View History

2019-05-08 21:00:36 +08:00
/* global MarvinJSUtil, I18n, FilePreviewModal, tinymce */
2019-06-23 06:09:49 +08:00
/* global TinyMCE, ChemicalizeMarvinJs */
2019-04-29 01:11:41 +08:00
/* eslint-disable no-param-reassign */
/* eslint-disable wrap-iife */
2019-06-23 06:09:49 +08:00
/* eslint-disable no-use-before-define */
var marvinJsRemoteLastMrv;
var MarvinJsEditor = (function() {
2019-04-27 00:24:21 +08:00
var marvinJsModal = $('#MarvinJsModal');
var marvinJsContainer = $('#marvinjs-editor');
var marvinJsObject = $('#marvinjs-sketch');
2019-04-29 01:11:41 +08:00
var emptySketch = '<cml><MDocument></MDocument></cml>';
var sketchName = marvinJsModal.find('.file-name input');
2019-06-23 06:09:49 +08:00
var marvinJsMode = marvinJsContainer[0].dataset.marvinjsMode;
// Facade api actions
2019-06-23 06:09:49 +08:00
var marvinJsExport = (childFunction, options = {}) => {
if (marvinJsMode === 'remote') {
remoteExport(childFunction, options);
} else {
localExport(childFunction, options);
}
2019-06-23 06:09:49 +08:00
};
2019-06-23 06:09:49 +08:00
var marvinJsImage = (childFunction, source, options = {}) => {
if (marvinJsMode === 'remote') {
remoteImage(childFunction, source, options);
} else {
localImage(childFunction, source, options);
}
2019-06-23 06:09:49 +08:00
};
2019-04-27 00:24:21 +08:00
2019-06-23 06:09:49 +08:00
var marvinJsExportImage = (childFuction, options = {}) => {
if (marvinJsMode === 'remote') {
remoteExportImage(childFuction, options);
} else {
2019-06-23 06:09:49 +08:00
localExportImage(childFuction, options);
}
};
// ///////////////
var loadEditor = () => {
if (marvinJsMode === 'remote') {
marvinJsObject.find('iframe').remove();
return ChemicalizeMarvinJs.createEditor('#marvinjs-sketch');
2019-05-08 21:00:36 +08:00
}
2019-06-23 06:09:49 +08:00
return MarvinJSUtil.getEditor('#marvinjs-sketch');
2019-04-29 01:11:41 +08:00
};
2019-04-27 04:59:38 +08:00
var loadPackages = () => {
2019-04-29 01:11:41 +08:00
return MarvinJSUtil.getPackage('#marvinjs-sketch');
};
2019-04-27 04:59:38 +08:00
// Local marvinJS installation
2019-06-23 06:09:49 +08:00
var localExport = (childFuction, options = {}) => {
loadEditor().then(function(sketcherInstance) {
sketcherInstance.exportStructure('mrv').then(function(source) {
2019-06-23 06:09:49 +08:00
childFuction(source, options);
});
});
};
2019-06-23 06:09:49 +08:00
var localImage = (childFuction, source, options = {}) => {
loadPackages().then(function(sketcherInstance) {
sketcherInstance.onReady(function() {
var exporter = createExporter(sketcherInstance, 'image/jpeg');
exporter.render(source).then(function(image) {
2019-06-23 06:09:49 +08:00
childFuction(source, image, options);
});
});
});
2019-06-23 06:09:49 +08:00
};
2019-06-23 06:09:49 +08:00
var localExportImage = (childFuction, options = {}) => {
loadEditor().then(function(sketcherInstance) {
sketcherInstance.exportStructure('mrv').then(function(source) {
loadPackages().then(function(sketcherPackage) {
sketcherPackage.onReady(function() {
var exporter = createExporter(sketcherPackage, 'image/jpeg');
exporter.render(source).then(function(image) {
2019-06-23 06:09:49 +08:00
childFuction(source, image, options);
});
});
});
});
});
};
// Web services installation
2019-06-23 06:09:49 +08:00
var remoteExport = (childFuction, options = {}) => {
childFuction(marvinJsRemoteLastMrv, options);
};
2019-06-23 06:09:49 +08:00
var remoteImage = (childFuction, source, options = {}) => {
ChemicalizeMarvinJs.createEditor('#marvinjs-sketch').then(function(marvin) {
var params = {
carbonLabelVisible: false,
implicitHydrogen: 'TERMINAL_AND_HETERO',
displayMode: 'WIREFRAME',
width: 900,
height: 900
};
marvin.exportMrvToImageDataUri(source, 'jpeg', params).then(function(image) {
2019-06-23 06:09:49 +08:00
childFuction(source, image, options);
});
});
};
var remoteExportImage = (childFuction, options = {}) => {
remoteImage(childFuction, marvinJsRemoteLastMrv, options);
};
// Support actions
2019-04-27 04:59:38 +08:00
function preloadActions(config) {
loadEditor().then(function(marvin) {
if (config.mode === 'new' || config.mode === 'new-tinymce') {
marvin.importStructure('mrv', emptySketch);
2019-04-29 01:11:41 +08:00
sketchName.val(I18n.t('marvinjs.new_sketch'));
} else if (config.mode === 'edit') {
marvin.importStructure('mrv', config.data);
2019-04-29 01:11:41 +08:00
sketchName.val(config.name);
} else if (config.mode === 'edit-tinymce') {
2019-04-29 01:11:41 +08:00
$.get(config.marvinUrl, function(result) {
marvin.importStructure('mrv', result.description);
2019-04-29 01:11:41 +08:00
sketchName.val(result.name);
});
}
2019-06-23 06:09:49 +08:00
marvin.on('molchange', () => {
marvin.exportStructure('mrv').then(function(source) {
2019-06-23 06:09:49 +08:00
marvinJsRemoteLastMrv = source;
});
2019-04-29 01:11:41 +08:00
});
});
2019-04-27 04:59:38 +08:00
}
2019-04-29 01:11:41 +08:00
function createExporter(marvin, imageType) {
var inputFormat = 'mrv';
var settings = {
2019-04-29 01:11:41 +08:00
width: 900,
height: 900
};
2019-04-27 04:59:38 +08:00
var params = {
2019-04-29 01:11:41 +08:00
imageType: imageType,
settings: settings,
inputFormat: inputFormat
};
2019-04-27 04:59:38 +08:00
return new marvin.ImageExporter(params);
}
2019-06-23 06:09:49 +08:00
function assignImage(source, data, target) {
2019-04-29 01:11:41 +08:00
target.attr('src', data);
return data;
2019-04-27 04:59:38 +08:00
}
2019-04-28 04:54:59 +08:00
function TinyMceBuildHTML(json) {
var imgstr = "<img src='" + json.image.url + "'";
imgstr += " data-mce-token='" + json.image.token + "'";
2019-04-29 01:11:41 +08:00
imgstr += " data-source-id='" + json.image.source_id + "'";
imgstr += " data-source-type='" + json.image.source_type + "'";
2019-04-28 04:54:59 +08:00
imgstr += " alt='description-" + json.image.token + "' />";
return imgstr;
}
2019-06-23 06:09:49 +08:00
function saveFunction(source, config) {
$.post(config.marvinUrl, {
description: source,
object_id: config.objectId,
object_type: config.objectType,
name: sketchName.val()
}, function(result) {
var newAsset;
if (config.objectType === 'Step') {
newAsset = $(result.html);
newAsset.find('.file-preview-link').css('top', '-300px');
newAsset.addClass('new').prependTo($(config.container));
setTimeout(function() {
newAsset.find('.file-preview-link').css('top', '0px');
}, 200);
FilePreviewModal.init();
}
$(marvinJsModal).modal('hide');
});
}
2019-06-23 06:09:49 +08:00
function saveTinymceFunction(source, image, config) {
$.post(config.marvinUrl, {
description: source,
object_id: config.objectId,
object_type: config.objectType,
name: sketchName.val(),
image: image
}, function(result) {
var json = tinymce.util.JSON.parse(result);
config.editor.execCommand('mceInsertContent', false, TinyMceBuildHTML(json));
TinyMCE.updateImages(config.editor);
$(marvinJsModal).modal('hide');
});
}
2019-06-23 06:09:49 +08:00
function updateFunction(source, config) {
$.ajax({
url: config.marvinUrl,
data: {
description: source,
name: sketchName.val()
},
dataType: 'json',
type: 'PUT',
success: function(json) {
$(marvinJsModal).modal('hide');
config.reloadImage.src.val(json.description);
$(config.reloadImage.sketch).find('.attachment-label').text(json.name);
MarvinJsEditor().create_preview(
config.reloadImage.src,
$(config.reloadImage.sketch).find('img')
);
}
});
}
2019-06-23 06:09:49 +08:00
function updateTinymceFunction(source, image, config) {
$.ajax({
url: config.marvinUrl,
data: {
description: source,
name: sketchName.val(),
object_type: 'TinyMceAsset',
image: image
},
dataType: 'json',
type: 'PUT',
success: function(json) {
config.image[0].src = json.url;
$(marvinJsModal).modal('hide');
}
});
}
// MarvinJS Methods
2019-04-27 00:24:21 +08:00
return Object.freeze({
2019-04-27 02:33:20 +08:00
open: function(config) {
2019-04-29 01:11:41 +08:00
preloadActions(config);
2019-04-27 00:24:21 +08:00
$(marvinJsModal).modal('show');
$(marvinJsObject)
2019-06-23 06:09:49 +08:00
.css('width', marvinJsContainer.width() + 'px')
2019-04-29 01:11:41 +08:00
.css('height', marvinJsContainer.height() + 'px');
2019-04-27 02:33:20 +08:00
marvinJsModal.find('.file-save-link').off('click').on('click', () => {
2019-04-29 01:11:41 +08:00
if (config.mode === 'new') {
MarvinJsEditor().save(config);
} else if (config.mode === 'edit') {
MarvinJsEditor().update(config);
} else if (config.mode === 'new-tinymce') {
config.objectType = 'TinyMceAsset';
MarvinJsEditor().save_with_image(config);
} else if (config.mode === 'edit-tinymce') {
MarvinJsEditor().update_tinymce(config);
2019-04-28 01:08:40 +08:00
}
2019-04-29 01:11:41 +08:00
});
2019-04-27 02:33:20 +08:00
},
initNewButton: function(selector) {
2019-04-29 01:11:41 +08:00
$(selector).off('click').on('click', function() {
2019-04-27 02:33:20 +08:00
var objectId = this.dataset.objectId;
var objectType = this.dataset.objectType;
2019-04-27 04:59:38 +08:00
var marvinUrl = this.dataset.marvinUrl;
2019-04-29 01:11:41 +08:00
var container = this.dataset.sketchContainer;
2019-04-27 02:33:20 +08:00
MarvinJsEditor().open({
mode: 'new',
objectId: objectId,
2019-04-27 04:59:38 +08:00
objectType: objectType,
2019-04-28 01:08:40 +08:00
marvinUrl: marvinUrl,
container: container
2019-04-29 01:11:41 +08:00
});
});
2019-04-27 02:33:20 +08:00
},
2019-04-29 01:11:41 +08:00
save: function(config) {
2019-06-23 06:09:49 +08:00
marvinJsExport(saveFunction, config);
2019-04-27 02:33:20 +08:00
},
2019-04-29 01:11:41 +08:00
save_with_image: function(config) {
2019-06-23 06:09:49 +08:00
marvinJsExportImage(saveTinymceFunction, config);
2019-04-28 04:54:59 +08:00
},
2019-04-29 01:11:41 +08:00
update: function(config) {
2019-06-23 06:09:49 +08:00
marvinJsExport(updateFunction, config);
2019-04-28 22:16:31 +08:00
},
2019-04-29 01:11:41 +08:00
update_tinymce: function(config) {
2019-06-23 06:09:49 +08:00
marvinJsExportImage(updateTinymceFunction, config);
2019-04-28 01:08:40 +08:00
},
2019-04-29 01:11:41 +08:00
create_preview: function(source, target) {
2019-06-23 06:09:49 +08:00
marvinJsImage(assignImage, source.val(), target);
2019-04-27 19:51:35 +08:00
},
2019-04-29 01:11:41 +08:00
create_download_link: function(source, link, filename) {
2019-06-23 06:09:49 +08:00
var downloadLink = (mrv, image, option) => {
option.link.attr('href', image);
option.link.attr('download', option.filename);
2019-06-23 06:09:49 +08:00
};
marvinJsImage(downloadLink, source.val(), { link: link, filename: filename });
2019-04-28 01:08:40 +08:00
},
2019-04-29 01:11:41 +08:00
delete_sketch: function(url, object) {
2019-04-27 19:51:35 +08:00
$.ajax({
url: url,
dataType: 'json',
type: 'DELETE',
2019-04-29 01:11:41 +08:00
success: function() {
$(object).remove();
2019-04-27 19:51:35 +08:00
}
});
2019-04-27 00:24:21 +08:00
}
});
});
2019-04-28 04:54:59 +08:00
(function() {
'use strict';
tinymce.PluginManager.requireLangPack('MarvinJsPlugin');
tinymce.create('tinymce.plugins.MarvinJsPlugin', {
2019-04-29 01:11:41 +08:00
MarvinJsPlugin: function(ed) {
2019-04-28 04:54:59 +08:00
var editor = ed;
2019-04-29 01:11:41 +08:00
function openMarvinJs() {
2019-04-28 04:54:59 +08:00
MarvinJsEditor().open({
mode: 'new-tinymce',
marvinUrl: '/marvin_js_assets',
editor: editor
2019-04-29 01:11:41 +08:00
});
2019-04-28 04:54:59 +08:00
}
// Add a button that opens a window
editor.addButton('marvinjsplugin', {
tooltip: I18n.t('marvinjs.new_button'),
icon: 'file-invoice',
onclick: openMarvinJs
});
// Adds a menu item to the tools menu
editor.addMenuItem('marvinjsplugin', {
text: I18n.t('marvinjs.new_button'),
icon: 'file-invoice',
context: 'insert',
onclick: openMarvinJs
});
}
});
tinymce.PluginManager.add(
'marvinjsplugin',
tinymce.plugins.MarvinJsPlugin
);
})();