2018-05-16 00:53:27 +00:00
/* eslint-disable no-var */
/* globals svgEditor, svgedit, 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
2018-05-16 00:53:27 +00:00
svgEditor . addExtension ( 'server_opensave' , {
callback : function ( ) {
2013-10-29 06:54:31 +00:00
'use strict' ;
function getFileNameFromTitle ( ) {
var title = svgCanvas . getDocumentTitle ( ) ;
2014-02-01 16:13:51 +00:00
// We convert (to underscore) only those disallowed Win7 file name characters
2018-05-16 00:53:27 +00:00
return $ . trim ( title ) . replace ( /[/\\:*?"<>|]/g , '_' ) ;
2014-02-01 16:13:51 +00:00
}
2018-05-16 00:53:27 +00:00
function xhtmlEscape ( str ) {
2014-02-01 17:00:36 +00:00
return str . replace ( /&(?!amp;)/g , '&' ) . replace ( /"/g , '"' ) . replace ( /</g , '<' ) ; // < is actually disallowed above anyways
2013-10-29 06:54:31 +00:00
}
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 ;
}
}
2018-05-16 00:53:27 +00:00
var openSvgAction , importSvgAction , importImgAction ,
openSvgForm , importSvgForm , importImgForm ,
saveSvgAction = svgEditor . curConfig . extPath + 'filesave.php' ,
saveImgAction = svgEditor . curConfig . extPath + 'filesave.php' ,
2013-10-29 06:54:31 +00:00
// Create upload target (hidden iframe)
2014-05-22 10:21:29 +00:00
cancelled = false ,
Utils = svgedit . utilities ;
2018-05-16 00:53:27 +00:00
2013-10-29 06:54:31 +00:00
$ ( '<iframe name="output_frame" src="#"/>' ) . hide ( ) . appendTo ( 'body' ) ;
2010-07-06 13:57:05 +00:00
svgEditor . setCustomHandlers ( {
2018-05-16 00:53:27 +00:00
save : function ( win , data ) {
2014-02-02 01:06:25 +00:00
var svg = '<?xml version="1.0" encoding="UTF-8"?>\n' + data , // Firefox doesn't seem to know it is UTF-8 (no matter whether we use or skip the clientDownload code) despite the Content-Disposition header containing UTF-8, but adding the encoding works
2013-10-29 06:54:31 +00:00
filename = getFileNameFromTitle ( ) ;
2013-10-29 01:26:23 +00:00
2014-05-22 10:21:29 +00:00
if ( clientDownloadSupport ( filename , '.svg' , 'data:image/svg+xml;charset=UTF-8;base64,' + Utils . encode64 ( svg ) ) ) {
2013-10-29 06:54:31 +00:00
return ;
}
2014-02-01 16:13:51 +00:00
2013-10-29 01:26:23 +00:00
$ ( '<form>' ) . attr ( {
2010-07-06 13:57:05 +00:00
method : 'post' ,
2018-05-16 00:53:27 +00:00
action : saveSvgAction ,
2010-07-06 13:57:05 +00:00
target : 'output_frame'
2014-02-01 16:13:51 +00:00
} ) . append ( '<input type="hidden" name="output_svg" value="' + xhtmlEscape ( svg ) + '">' )
. append ( '<input type="hidden" name="filename" value="' + xhtmlEscape ( filename ) + '">' )
2010-07-06 13:57:05 +00:00
. appendTo ( 'body' )
. submit ( ) . remove ( ) ;
} ,
2014-06-13 14:58:46 +00:00
exportPDF : function ( win , data ) {
var filename = getFileNameFromTitle ( ) ,
datauri = data . dataurlstring ;
if ( clientDownloadSupport ( filename , '.pdf' , datauri ) ) {
return ;
}
$ ( '<form>' ) . attr ( {
method : 'post' ,
2018-05-16 00:53:27 +00:00
action : saveImgAction ,
2014-06-13 14:58:46 +00:00
target : 'output_frame'
} ) . append ( '<input type="hidden" name="output_img" value="' + datauri + '">' )
. append ( '<input type="hidden" name="mime" value="application/pdf">' )
. append ( '<input type="hidden" name="filename" value="' + xhtmlEscape ( filename ) + '">' )
. appendTo ( 'body' )
. submit ( ) . remove ( ) ;
} ,
2014-05-22 11:24:18 +00:00
// Todo: Integrate this extension with a new built-in exportWindowType, "download"
2018-05-16 00:53:27 +00:00
exportImage : function ( win , data ) {
2013-10-29 01:26:23 +00:00
var c ,
2013-10-29 06:54:31 +00:00
issues = data . issues ,
mimeType = data . mimeType ,
quality = data . quality ;
2018-05-16 00:53:27 +00:00
if ( ! $ ( '#export_canvas' ) . length ) {
2010-07-06 13:57:05 +00:00
$ ( '<canvas>' , { id : 'export_canvas' } ) . hide ( ) . appendTo ( 'body' ) ;
}
2013-10-29 01:26:23 +00:00
c = $ ( '#export_canvas' ) [ 0 ] ;
2018-05-16 00:53:27 +00:00
2010-07-06 13:57:05 +00:00
c . width = svgCanvas . contentW ;
c . height = svgCanvas . contentH ;
2014-05-22 10:21:29 +00:00
Utils . buildCanvgCallback ( function ( ) {
2018-05-16 00:53:27 +00:00
canvg ( c , data . svg , { renderCallback : function ( ) {
2014-05-22 02:56:00 +00:00
var pre , filename , suffix ,
datauri = quality ? c . toDataURL ( mimeType , quality ) : c . toDataURL ( mimeType ) ,
// uiStrings = svgEditor.uiStrings,
note = '' ;
2018-05-16 00:53:27 +00:00
2014-05-22 02:56:00 +00:00
// Check if there are issues
if ( issues . length ) {
2018-05-16 00:53:27 +00:00
pre = '\n \u2022 ' ;
note += ( '\n\n' + pre + issues . join ( pre ) ) ;
}
if ( note . length ) {
2014-05-22 02:56:00 +00:00
alert ( note ) ;
}
2018-05-16 00:53:27 +00:00
2014-05-22 02:56:00 +00:00
filename = getFileNameFromTitle ( ) ;
suffix = '.' + data . type . toLowerCase ( ) ;
2018-05-16 00:53:27 +00:00
2014-05-22 02:56:00 +00:00
if ( clientDownloadSupport ( filename , suffix , datauri ) ) {
return ;
}
2013-10-29 01:26:23 +00:00
2014-05-22 02:56:00 +00:00
$ ( '<form>' ) . attr ( {
method : 'post' ,
2018-05-16 00:53:27 +00:00
action : saveImgAction ,
2014-05-22 02:56:00 +00:00
target : 'output_frame'
} ) . append ( '<input type="hidden" name="output_img" value="' + datauri + '">' )
. append ( '<input type="hidden" name="mime" value="' + mimeType + '">' )
. append ( '<input type="hidden" name="filename" value="' + xhtmlEscape ( filename ) + '">' )
. appendTo ( 'body' )
. submit ( ) . remove ( ) ;
} } ) ;
} ) ( ) ;
2010-07-06 13:57:05 +00:00
}
} ) ;
2014-02-02 01:06:25 +00:00
2010-07-01 20:14:12 +00:00
// Do nothing if client support is found
2018-05-16 00:53:27 +00:00
if ( window . FileReader ) { return ; }
2010-07-01 20:14:12 +00:00
// Change these to appropriate script file
2018-05-16 00:53:27 +00:00
openSvgAction = svgEditor . curConfig . extPath + 'fileopen.php?type=load_svg' ;
importSvgAction = svgEditor . curConfig . extPath + 'fileopen.php?type=import_svg' ;
importImgAction = svgEditor . curConfig . extPath + 'fileopen.php?type=import_img' ;
2010-07-01 20:14:12 +00:00
// Set up function for PHP uploader to use
2018-05-16 00:53:27 +00:00
svgEditor . processFile = function ( str64 , type ) {
2013-10-29 06:54:31 +00:00
var xmlstr ;
2014-02-01 16:13:51 +00:00
if ( cancelled ) {
2010-07-05 15:38:06 +00:00
cancelled = false ;
return ;
}
2018-05-16 00:53:27 +00:00
2010-07-05 15:38:06 +00:00
$ ( '#dialog_box' ) . hide ( ) ;
2013-10-29 06:33:37 +00:00
2013-10-29 01:26:23 +00:00
if ( type !== 'import_img' ) {
2014-05-22 10:21:29 +00:00
xmlstr = Utils . decode64 ( str64 ) ;
2010-07-05 15:38:06 +00:00
}
2018-05-16 00:53:27 +00:00
2013-10-29 01:26:23 +00:00
switch ( type ) {
2018-05-16 00:53:27 +00:00
case 'load_svg' :
svgCanvas . clear ( ) ;
svgCanvas . setSvgString ( xmlstr ) ;
svgEditor . updateCanvas ( ) ;
break ;
case 'import_svg' :
svgCanvas . importSvgString ( xmlstr ) ;
svgEditor . updateCanvas ( ) ;
break ;
case 'import_img' :
svgCanvas . setGoodImage ( str64 ) ;
break ;
2010-07-01 20:14:12 +00:00
}
2013-10-29 01:26:23 +00:00
} ;
2018-05-16 00:53:27 +00:00
2010-07-01 20:14:12 +00:00
// Create upload form
2018-05-16 00:53:27 +00:00
openSvgForm = $ ( '<form>' ) ;
openSvgForm . attr ( {
2010-07-01 20:14:12 +00:00
enctype : 'multipart/form-data' ,
method : 'post' ,
2018-05-16 00:53:27 +00:00
action : openSvgAction ,
2010-07-06 13:57:05 +00:00
target : 'output_frame'
2010-07-01 20:14:12 +00:00
} ) ;
2018-05-16 00:53:27 +00:00
2010-07-01 20:14:12 +00:00
// Create import form
2018-05-16 00:53:27 +00:00
importSvgForm = openSvgForm . clone ( ) . attr ( 'action' , importSvgAction ) ;
2013-10-29 01:26:23 +00:00
2010-07-01 20:14:12 +00:00
// Create image form
2018-05-16 00:53:27 +00:00
importImgForm = openSvgForm . clone ( ) . attr ( 'action' , importImgAction ) ;
// It appears necessary to rebuild this input every time a file is
2010-07-01 20:14:12 +00:00
// selected so the same file can be picked and the change event can fire.
2018-05-16 00:53:27 +00:00
function rebuildInput ( form ) {
2010-07-01 20:14:12 +00:00
form . empty ( ) ;
var inp = $ ( '<input type="file" name="svg_file">' ) . appendTo ( form ) ;
2018-05-16 00:53:27 +00:00
function submit ( ) {
2014-02-18 23:57:49 +00:00
// This submits the form, which returns the file data using svgEditor.processFile()
2010-07-05 15:38:06 +00:00
form . submit ( ) ;
2018-05-16 00:53:27 +00:00
2010-07-05 15:38:06 +00:00
rebuildInput ( form ) ;
2018-05-16 00:53:27 +00:00
$ . process _cancel ( 'Uploading...' , function ( ) {
2010-07-05 15:38:06 +00:00
cancelled = true ;
$ ( '#dialog_box' ) . hide ( ) ;
} ) ;
}
2018-05-16 00:53:27 +00:00
if ( form [ 0 ] === openSvgForm [ 0 ] ) {
inp . change ( function ( ) {
2010-07-01 20:14:12 +00:00
// This takes care of the "are you sure" dialog box
2018-05-16 00:53:27 +00:00
svgEditor . openPrep ( function ( ok ) {
if ( ! ok ) {
2010-07-01 20:14:12 +00:00
rebuildInput ( form ) ;
return ;
}
2010-07-05 15:38:06 +00:00
submit ( ) ;
2010-07-01 20:14:12 +00:00
} ) ;
} ) ;
} else {
2018-05-16 00:53:27 +00:00
inp . change ( function ( ) {
2014-02-18 23:57:49 +00:00
// This submits the form, which returns the file data using svgEditor.processFile()
2010-07-05 15:38:06 +00:00
submit ( ) ;
2010-07-01 20:14:12 +00:00
} ) ;
}
}
2018-05-16 00:53:27 +00:00
2010-07-01 20:14:12 +00:00
// Create the input elements
2018-05-16 00:53:27 +00:00
rebuildInput ( openSvgForm ) ;
rebuildInput ( importSvgForm ) ;
rebuildInput ( importImgForm ) ;
2010-07-01 20:14:12 +00:00
// Add forms to buttons
2018-05-16 00:53:27 +00:00
$ ( '#tool_open' ) . show ( ) . prepend ( openSvgForm ) ;
$ ( '#tool_import' ) . show ( ) . prepend ( importSvgForm ) ;
$ ( '#tool_image' ) . prepend ( importImgForm ) ;
2010-07-01 20:14:12 +00:00
}
} ) ;