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

311 lines
10 KiB
JavaScript
Raw Normal View History

2019-04-30 23:32:55 +08:00
/* global MarvinJSUtil, I18n, FilePreviewModal, tinymce, TinyMCE PerfectScrollbar */
2019-04-29 01:11:41 +08:00
/* eslint-disable no-param-reassign */
/* eslint-disable wrap-iife */
2019-04-27 00:24:21 +08:00
2019-04-29 01:11:41 +08:00
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-04-27 00:24:21 +08:00
2019-04-27 02:33:20 +08:00
var loadEditor = () => {
2019-04-29 01:11:41 +08:00
return MarvinJSUtil.getEditor('#marvinjs-sketch');
};
2019-04-27 02:33:20 +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
function preloadActions(config) {
2019-04-29 01:11:41 +08:00
if (config.mode === 'new' || config.mode === 'new-tinymce') {
2019-04-27 04:59:38 +08:00
loadEditor().then(function(sketcherInstance) {
2019-04-29 01:11:41 +08:00
sketcherInstance.importStructure('mrv', emptySketch);
sketchName.val(I18n.t('marvinjs.new_sketch'));
});
} else if (config.mode === 'edit') {
2019-04-28 01:08:40 +08:00
loadEditor().then(function(sketcherInstance) {
2019-04-29 01:11:41 +08:00
sketcherInstance.importStructure('mrv', config.data);
sketchName.val(config.name);
});
} else if (config.mode === 'edit-tinymce') {
2019-04-28 22:16:31 +08:00
loadEditor().then(function(sketcherInstance) {
2019-04-29 01:11:41 +08:00
$.get(config.marvinUrl, function(result) {
sketcherInstance.importStructure('mrv', result.description);
sketchName.val(result.name);
});
});
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-04-29 01:11:41 +08:00
function assignImage(target, data) {
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-04-27 00:24:21 +08:00
return Object.freeze({
2019-04-27 02:33:20 +08:00
open: function(config) {
2019-04-30 23:32:55 +08:00
MarvinJsEditor().team_sketches();
2019-04-29 01:11:41 +08:00
preloadActions(config);
2019-04-27 00:24:21 +08:00
$(marvinJsModal).modal('show');
$(marvinJsObject)
2019-04-30 23:32:55 +08:00
.css('width', (marvinJsContainer.width() - 200) + '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-04-27 02:33:20 +08:00
loadEditor().then(function(sketcherInstance) {
2019-04-29 01:11:41 +08:00
sketcherInstance.exportStructure('mrv').then(function(source) {
$.post(config.marvinUrl, {
2019-04-27 04:59:38 +08:00
description: source,
object_id: config.objectId,
2019-04-27 19:51:35 +08:00
object_type: config.objectType,
name: sketchName.val()
2019-04-29 01:11:41 +08:00
}, 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();
2019-04-28 01:08:40 +08:00
}
2019-04-27 04:59:38 +08:00
$(marvinJsModal).modal('hide');
2019-04-29 01:11:41 +08:00
});
2019-04-27 02:33:20 +08:00
});
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_with_image: function(config) {
2019-04-28 04:54:59 +08:00
loadEditor().then(function(sketcherInstance) {
2019-04-29 01:11:41 +08:00
sketcherInstance.exportStructure('mrv').then(function(mrvDescription) {
loadPackages().then(function(sketcherPackage) {
2019-04-28 04:54:59 +08:00
sketcherPackage.onReady(function() {
2019-04-29 01:11:41 +08:00
var exporter = createExporter(sketcherPackage, 'image/jpeg');
exporter.render(mrvDescription).then(function(image) {
$.post(config.marvinUrl, {
description: mrvDescription,
2019-04-28 04:54:59 +08:00
object_id: config.objectId,
object_type: config.objectType,
name: sketchName.val(),
image: image
2019-04-29 01:11:41 +08:00
}, function(result) {
2019-04-28 04:54:59 +08:00
var json = tinymce.util.JSON.parse(result);
config.editor.execCommand('mceInsertContent', false, TinyMceBuildHTML(json));
2019-04-29 01:11:41 +08:00
TinyMCE.updateImages(config.editor);
2019-04-28 04:54:59 +08:00
$(marvinJsModal).modal('hide');
2019-04-29 01:11:41 +08:00
});
2019-04-28 04:54:59 +08:00
});
});
});
});
2019-04-29 01:11:41 +08:00
});
2019-04-28 04:54:59 +08:00
},
2019-04-29 01:11:41 +08:00
update: function(config) {
2019-04-28 01:08:40 +08:00
loadEditor().then(function(sketcherInstance) {
2019-04-29 01:11:41 +08:00
sketcherInstance.exportStructure('mrv').then(function(source) {
2019-04-28 01:08:40 +08:00
$.ajax({
url: config.marvinUrl,
data: {
description: source,
name: sketchName.val()
},
dataType: 'json',
type: 'PUT',
success: function(json) {
$(marvinJsModal).modal('hide');
2019-04-29 01:11:41 +08:00
config.reloadImage.src.val(json.description);
$(config.reloadImage.sketch).find('.attachment-label').text(json.name);
2019-04-28 01:08:40 +08:00
MarvinJsEditor().create_preview(
2019-04-29 01:11:41 +08:00
config.reloadImage.src,
2019-04-28 01:08:40 +08:00
$(config.reloadImage.sketch).find('img')
2019-04-29 01:11:41 +08:00
);
2019-04-28 01:08:40 +08:00
}
});
2019-04-28 22:16:31 +08:00
});
2019-04-29 01:11:41 +08:00
});
2019-04-28 22:16:31 +08:00
},
2019-04-29 01:11:41 +08:00
update_tinymce: function(config) {
2019-04-28 22:16:31 +08:00
loadEditor().then(function(sketcherInstance) {
2019-04-29 01:11:41 +08:00
sketcherInstance.exportStructure('mrv').then(function(mrvDescription) {
loadPackages().then(function(sketcherPackage) {
2019-04-28 22:16:31 +08:00
sketcherPackage.onReady(function() {
2019-04-29 01:11:41 +08:00
var exporter = createExporter(sketcherPackage, 'image/jpeg');
exporter.render(mrvDescription).then(function(image) {
2019-04-28 22:16:31 +08:00
$.ajax({
url: config.marvinUrl,
data: {
2019-04-29 01:11:41 +08:00
description: mrvDescription,
2019-04-28 22:16:31 +08:00
name: sketchName.val(),
object_type: 'TinyMceAsset',
image: image
},
dataType: 'json',
type: 'PUT',
success: function(json) {
2019-04-29 01:11:41 +08:00
config.image[0].src = json.url;
2019-04-28 22:16:31 +08:00
$(marvinJsModal).modal('hide');
}
});
});
});
});
2019-04-28 01:08:40 +08:00
});
2019-04-29 01:11:41 +08:00
});
2019-04-28 01:08:40 +08:00
},
2019-04-29 01:11:41 +08:00
create_preview: function(source, target) {
loadPackages().then(function(sketcherInstance) {
2019-04-27 04:59:38 +08:00
sketcherInstance.onReady(function() {
2019-04-29 01:11:41 +08:00
var exporter = createExporter(sketcherInstance, 'image/jpeg');
var sketchConfig = source.val();
exporter.render(sketchConfig).then(function(result) {
assignImage(target, result);
2019-04-27 04:59:38 +08:00
});
});
});
2019-04-27 19:51:35 +08:00
},
2019-04-29 01:11:41 +08:00
create_download_link: function(source, link, filename) {
loadPackages().then(function(sketcherInstance) {
2019-04-28 01:08:40 +08:00
sketcherInstance.onReady(function() {
2019-04-29 01:11:41 +08:00
var exporter = createExporter(sketcherInstance, 'image/jpeg');
var sketchConfig = source.val();
exporter.render(sketchConfig).then(function(result) {
2019-04-28 01:08:40 +08:00
link.attr('href', result);
link.attr('download', filename);
});
});
});
},
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-30 23:32:55 +08:00
},
team_sketches: function() {
var ps = new PerfectScrollbar(marvinJsContainer.find('.marvinjs-team-sketch')[0]);
marvinJsContainer.find('.sketch-container').remove();
$.get('/marvin_js_assets/team_sketches', function(result) {
$(result.html).appendTo(marvinJsContainer.find('.marvinjs-team-sketch'));
$.each(result.sketches, function(i, sketch) {
var sketchObj = marvinJsContainer.find('.marvinjs-team-sketch .sketch-container[data-sketch-id="' + sketch + '"]');
var src = sketchObj.find('#description');
var dest = sketchObj.find('img');
MarvinJsEditor().create_preview(src, dest);
setTimeout(() => { ps.update(); }, 500);
marvinJsContainer.find('.sketch-container').click(function() {
var sketchContainer = $(this);
loadEditor().then(function(sketcherInstance) {
sketcherInstance.importStructure('mrv', sketchContainer.find('#description').val());
});
});
});
});
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
);
})();