2013-10-29 01:26:23 +00:00
|
|
|
/*globals svgEditor, svgCanvas, canvg, $*/
|
2010-07-01 20:14:12 +00:00
|
|
|
/*
|
2010-07-06 13:57:05 +00:00
|
|
|
* ext-server_opensave.js
|
2010-07-01 20:14:12 +00:00
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2010-07-01 20:14:12 +00:00
|
|
|
*
|
|
|
|
* Copyright(c) 2010 Alexis Deveria
|
|
|
|
*
|
|
|
|
*/
|
2010-07-06 13:57:05 +00:00
|
|
|
|
|
|
|
svgEditor.addExtension("server_opensave", {
|
2010-07-01 20:14:12 +00:00
|
|
|
callback: function() {
|
2013-10-29 01:26:23 +00:00
|
|
|
'use strict';
|
|
|
|
function getFileNameFromTitle () {
|
|
|
|
var title = svgCanvas.getDocumentTitle();
|
|
|
|
return $.trim(title).replace(/[^a-z0-9\.\_\-]+/gi, '_');
|
|
|
|
}
|
|
|
|
function clientDownloadSupport (filename, suffix, uri) {
|
|
|
|
var a,
|
|
|
|
support = $('<a>')[0].download === '';
|
|
|
|
if (support) {
|
|
|
|
a = $('<a>hidden</a>').attr({download: (filename || 'image') + suffix, href: uri}).css('display', 'none').appendTo('body');
|
|
|
|
a[0].click();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var open_svg_action, import_svg_action, import_img_action,
|
|
|
|
open_svg_form, import_svg_form, import_img_form,
|
|
|
|
save_svg_action = 'extensions/filesave.php',
|
|
|
|
save_img_action = 'extensions/filesave.php',
|
|
|
|
// Create upload target (hidden iframe)
|
|
|
|
cancelled = false;
|
2010-07-06 13:57:05 +00:00
|
|
|
|
2013-10-29 01:26:23 +00:00
|
|
|
$('<iframe name="output_frame" src="#"/>').hide().appendTo('body');
|
2010-07-06 13:57:05 +00:00
|
|
|
svgEditor.setCustomHandlers({
|
|
|
|
save: function(win, data) {
|
2013-10-29 01:26:23 +00:00
|
|
|
var svg = "<?xml version=\"1.0\"?>\n" + data,
|
|
|
|
filename = getFileNameFromTitle();
|
|
|
|
|
|
|
|
//if (clientDownloadSupport(filename, '.svg', 'data:image/svg+xml,' + encodeURI(data))) { // Firefox limits size of file
|
|
|
|
if (clientDownloadSupport(filename, '.svg', 'data:image/svg+xml;base64,' + window.btoa(data))) {
|
|
|
|
return;
|
|
|
|
}
|
2010-07-06 13:57:05 +00:00
|
|
|
|
2013-10-29 01:26:23 +00:00
|
|
|
$('<form>').attr({
|
2010-07-06 13:57:05 +00:00
|
|
|
method: 'post',
|
|
|
|
action: save_svg_action,
|
|
|
|
target: 'output_frame'
|
|
|
|
}) .append('<input type="hidden" name="output_svg" value="' + encodeURI(svg) + '">')
|
|
|
|
.append('<input type="hidden" name="filename" value="' + filename + '">')
|
|
|
|
.appendTo('body')
|
|
|
|
.submit().remove();
|
|
|
|
},
|
Support PNG, JPEG, BMP, WEBP export formats by change of "Export to PNG" menu item into "Export" with its own pull-down dialog (and a HTML5 range type input element for "quality" selection for JPEG or WEBP files), including changes for the server_opensave extension; change locale key from "export_png" to "export_img" and change the corresponding localized strings (currently CS, DE, ES, FR, IT, NL, PT-BR, RO, SK, all reset to English to avoid translation errors); within the setCustomHandlers() API, deprecate "pngsave" in favor of "exportImage" (chosen to avoid just using the simpler but reserved JS keyword "export") including use within the server_opensave extension; a few JSLint-friendly changes
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2602 eee81c28-f429-11dd-99c0-75d572ba1ddd
2013-10-28 03:53:30 +00:00
|
|
|
exportImage: function(win, data) {
|
2013-10-29 01:26:23 +00:00
|
|
|
var c,
|
|
|
|
issues = data.issues,
|
Support PNG, JPEG, BMP, WEBP export formats by change of "Export to PNG" menu item into "Export" with its own pull-down dialog (and a HTML5 range type input element for "quality" selection for JPEG or WEBP files), including changes for the server_opensave extension; change locale key from "export_png" to "export_img" and change the corresponding localized strings (currently CS, DE, ES, FR, IT, NL, PT-BR, RO, SK, all reset to English to avoid translation errors); within the setCustomHandlers() API, deprecate "pngsave" in favor of "exportImage" (chosen to avoid just using the simpler but reserved JS keyword "export") including use within the server_opensave extension; a few JSLint-friendly changes
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2602 eee81c28-f429-11dd-99c0-75d572ba1ddd
2013-10-28 03:53:30 +00:00
|
|
|
mimeType = data.mimeType,
|
|
|
|
quality = data.quality;
|
2010-07-06 13:57:05 +00:00
|
|
|
|
|
|
|
if(!$('#export_canvas').length) {
|
|
|
|
$('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
|
|
|
|
}
|
2013-10-29 01:26:23 +00:00
|
|
|
c = $('#export_canvas')[0];
|
2010-07-06 13:57:05 +00:00
|
|
|
|
|
|
|
c.width = svgCanvas.contentW;
|
|
|
|
c.height = svgCanvas.contentH;
|
2010-08-31 13:52:54 +00:00
|
|
|
canvg(c, data.svg, {renderCallback: function() {
|
2013-10-29 01:26:23 +00:00
|
|
|
var pre, filename, suffix,
|
|
|
|
datauri = quality ? c.toDataURL(mimeType, quality) : c.toDataURL(mimeType),
|
|
|
|
uiStrings = svgEditor.uiStrings,
|
|
|
|
note = '';
|
2010-08-18 16:45:02 +00:00
|
|
|
|
|
|
|
// Check if there's issues
|
|
|
|
if(issues.length) {
|
2013-10-29 01:26:23 +00:00
|
|
|
pre = "\n \u2022 ";
|
2010-08-18 16:45:02 +00:00
|
|
|
note += ("\n\n" + pre + issues.join(pre));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(note.length) {
|
|
|
|
alert(note);
|
|
|
|
}
|
|
|
|
|
2013-10-29 01:26:23 +00:00
|
|
|
filename = getFileNameFromTitle();
|
|
|
|
suffix = '.' + data.type.toLowerCase();
|
|
|
|
|
|
|
|
if (clientDownloadSupport(filename, suffix, datauri)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$('<form>').attr({
|
2010-08-18 16:45:02 +00:00
|
|
|
method: 'post',
|
Support PNG, JPEG, BMP, WEBP export formats by change of "Export to PNG" menu item into "Export" with its own pull-down dialog (and a HTML5 range type input element for "quality" selection for JPEG or WEBP files), including changes for the server_opensave extension; change locale key from "export_png" to "export_img" and change the corresponding localized strings (currently CS, DE, ES, FR, IT, NL, PT-BR, RO, SK, all reset to English to avoid translation errors); within the setCustomHandlers() API, deprecate "pngsave" in favor of "exportImage" (chosen to avoid just using the simpler but reserved JS keyword "export") including use within the server_opensave extension; a few JSLint-friendly changes
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2602 eee81c28-f429-11dd-99c0-75d572ba1ddd
2013-10-28 03:53:30 +00:00
|
|
|
action: save_img_action,
|
2010-08-18 16:45:02 +00:00
|
|
|
target: 'output_frame'
|
Support PNG, JPEG, BMP, WEBP export formats by change of "Export to PNG" menu item into "Export" with its own pull-down dialog (and a HTML5 range type input element for "quality" selection for JPEG or WEBP files), including changes for the server_opensave extension; change locale key from "export_png" to "export_img" and change the corresponding localized strings (currently CS, DE, ES, FR, IT, NL, PT-BR, RO, SK, all reset to English to avoid translation errors); within the setCustomHandlers() API, deprecate "pngsave" in favor of "exportImage" (chosen to avoid just using the simpler but reserved JS keyword "export") including use within the server_opensave extension; a few JSLint-friendly changes
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2602 eee81c28-f429-11dd-99c0-75d572ba1ddd
2013-10-28 03:53:30 +00:00
|
|
|
}) .append('<input type="hidden" name="output_img" value="' + datauri + '">')
|
|
|
|
.append('<input type="hidden" name="mime" value="' + mimeType + '">')
|
2010-08-18 16:45:02 +00:00
|
|
|
.append('<input type="hidden" name="filename" value="' + filename + '">')
|
|
|
|
.appendTo('body')
|
|
|
|
.submit().remove();
|
2010-08-31 13:52:54 +00:00
|
|
|
}});
|
2010-07-06 13:57:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-07-01 20:14:12 +00:00
|
|
|
// Do nothing if client support is found
|
2013-10-29 01:26:23 +00:00
|
|
|
if(window.FileReader) {return;}
|
2010-07-06 13:57:05 +00:00
|
|
|
|
2010-07-01 20:14:12 +00:00
|
|
|
// Change these to appropriate script file
|
2013-10-29 01:26:23 +00:00
|
|
|
open_svg_action = 'extensions/fileopen.php?type=load_svg';
|
|
|
|
import_svg_action = 'extensions/fileopen.php?type=import_svg';
|
|
|
|
import_img_action = 'extensions/fileopen.php?type=import_img';
|
2010-07-01 20:14:12 +00:00
|
|
|
|
|
|
|
// Set up function for PHP uploader to use
|
|
|
|
svgEditor.processFile = function(str64, type) {
|
2013-10-29 01:26:23 +00:00
|
|
|
var xmlstr;
|
2010-07-05 15:38:06 +00:00
|
|
|
if(cancelled) {
|
|
|
|
cancelled = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#dialog_box').hide();
|
|
|
|
|
2013-10-29 01:26:23 +00:00
|
|
|
if (type !== 'import_img') {
|
|
|
|
xmlstr = svgCanvas.Utils.decode64(str64);
|
2010-07-05 15:38:06 +00:00
|
|
|
}
|
2010-07-01 20:14:12 +00:00
|
|
|
|
2013-10-29 01:26:23 +00:00
|
|
|
switch (type) {
|
2010-07-01 20:14:12 +00:00
|
|
|
case 'load_svg':
|
|
|
|
svgCanvas.clear();
|
|
|
|
svgCanvas.setSvgString(xmlstr);
|
|
|
|
svgEditor.updateCanvas();
|
|
|
|
break;
|
|
|
|
case 'import_svg':
|
|
|
|
svgCanvas.importSvgString(xmlstr);
|
|
|
|
svgEditor.updateCanvas();
|
|
|
|
break;
|
2010-07-05 15:38:06 +00:00
|
|
|
case 'import_img':
|
|
|
|
svgCanvas.setGoodImage(str64);
|
|
|
|
break;
|
2010-07-01 20:14:12 +00:00
|
|
|
}
|
2013-10-29 01:26:23 +00:00
|
|
|
};
|
2010-07-01 20:14:12 +00:00
|
|
|
|
|
|
|
// Create upload form
|
2013-10-29 01:26:23 +00:00
|
|
|
open_svg_form = $('<form>');
|
2010-07-01 20:14:12 +00:00
|
|
|
open_svg_form.attr({
|
|
|
|
enctype: 'multipart/form-data',
|
|
|
|
method: 'post',
|
|
|
|
action: open_svg_action,
|
2010-07-06 13:57:05 +00:00
|
|
|
target: 'output_frame'
|
2010-07-01 20:14:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Create import form
|
2013-10-29 01:26:23 +00:00
|
|
|
import_svg_form = open_svg_form.clone().attr('action', import_svg_action);
|
|
|
|
|
2010-07-01 20:14:12 +00:00
|
|
|
// Create image form
|
2013-10-29 01:26:23 +00:00
|
|
|
import_img_form = open_svg_form.clone().attr('action', import_img_action);
|
2010-07-01 20:14:12 +00:00
|
|
|
|
|
|
|
// It appears necessory to rebuild this input every time a file is
|
|
|
|
// selected so the same file can be picked and the change event can fire.
|
|
|
|
function rebuildInput(form) {
|
|
|
|
form.empty();
|
|
|
|
var inp = $('<input type="file" name="svg_file">').appendTo(form);
|
|
|
|
|
2010-07-05 15:38:06 +00:00
|
|
|
|
|
|
|
function submit() {
|
|
|
|
// This submits the form, which returns the file data using svgEditor.uploadSVG
|
|
|
|
form.submit();
|
|
|
|
|
|
|
|
rebuildInput(form);
|
|
|
|
$.process_cancel("Uploading...", function() {
|
|
|
|
cancelled = true;
|
|
|
|
$('#dialog_box').hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-07-01 20:14:12 +00:00
|
|
|
if(form[0] == open_svg_form[0]) {
|
|
|
|
inp.change(function() {
|
|
|
|
// This takes care of the "are you sure" dialog box
|
|
|
|
svgEditor.openPrep(function(ok) {
|
|
|
|
if(!ok) {
|
|
|
|
rebuildInput(form);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-05 15:38:06 +00:00
|
|
|
submit();
|
2010-07-01 20:14:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
inp.change(function() {
|
|
|
|
// This submits the form, which returns the file data using svgEditor.uploadSVG
|
2010-07-05 15:38:06 +00:00
|
|
|
submit();
|
2010-07-01 20:14:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the input elements
|
|
|
|
rebuildInput(open_svg_form);
|
|
|
|
rebuildInput(import_svg_form);
|
|
|
|
rebuildInput(import_img_form);
|
|
|
|
|
|
|
|
// Add forms to buttons
|
|
|
|
$("#tool_open").show().prepend(open_svg_form);
|
|
|
|
$("#tool_import").show().prepend(import_svg_form);
|
2010-07-05 15:38:06 +00:00
|
|
|
$("#tool_image").prepend(import_img_form);
|
2010-07-01 20:14:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|