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

93 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-04-27 00:24:21 +08:00
var MarvinJsEditor = (function() {
var marvinJsModal = $('#MarvinJsModal');
var marvinJsContainer = $('#marvinjs-editor');
var marvinJsObject = $('#marvinjs-sketch');
2019-04-27 04:59:38 +08:00
var emptySketch = "<cml><MDocument></MDocument></cml>"
2019-04-27 00:24:21 +08:00
2019-04-27 02:33:20 +08:00
var loadEditor = () => {
return MarvinJSUtil.getEditor('#marvinjs-sketch')
}
2019-04-27 04:59:38 +08:00
var loadPackages = () => {
return MarvinJSUtil.getPackage('#marvinjs-sketch')
}
function preloadActions(config) {
if (config.mode === 'new'){
loadEditor().then(function(sketcherInstance) {
sketcherInstance.importStructure("mrv",emptySketch)
})
}
}
function createExporter(marvin,imageType) {
var inputFormat = "mrv";
var params = {
'imageType': imageType,
'inputFormat': inputFormat
}
return new marvin.ImageExporter(params);
}
function assignImage(target,data){
console.log(data)
$(target).find('img').attr('src',data)
}
2019-04-27 00:24:21 +08:00
return Object.freeze({
2019-04-27 02:33:20 +08:00
open: function(config) {
2019-04-27 04:59:38 +08:00
preloadActions(config)
2019-04-27 00:24:21 +08:00
$(marvinJsModal).modal('show');
$(marvinJsObject)
.css('width', marvinJsContainer.width() + 'px')
.css('height', marvinJsContainer.height() + 'px')
2019-04-27 02:33:20 +08:00
marvinJsModal.find('.file-save-link').off('click').on('click', () => {
2019-04-27 04:59:38 +08:00
MarvinJsEditor().save(config)
2019-04-27 02:33:20 +08:00
})
},
initNewButton: function(selector) {
$(selector).off('click').on('click', function(){
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-27 02:33:20 +08:00
MarvinJsEditor().open({
mode: 'new',
objectId: objectId,
2019-04-27 04:59:38 +08:00
objectType: objectType,
marvinUrl: marvinUrl
2019-04-27 02:33:20 +08:00
})
})
},
2019-04-27 04:59:38 +08:00
save: function(config){
2019-04-27 02:33:20 +08:00
loadEditor().then(function(sketcherInstance) {
sketcherInstance.exportStructure("mrv").then(function(source) {
2019-04-27 04:59:38 +08:00
$.post(config.marvinUrl,{
description: source,
object_id: config.objectId,
object_type: config.objectType
}, function(result){
console.log(result)
$(marvinJsModal).modal('hide');
})
2019-04-27 02:33:20 +08:00
});
})
},
2019-04-27 04:59:38 +08:00
create_preview: function(target){
loadPackages().then(function (sketcherInstance) {
sketcherInstance.onReady(function() {
exporter = createExporter(sketcherInstance,'image/jpeg')
sketch_config = $(target).find('#description').val();
exporter.render(sketch_config).then(function(result){
assignImage(target,result)
});
});
});
2019-04-27 00:24:21 +08:00
}
});
});