From f679554ce4802ca5894b494f978236a3a3b13ae4 Mon Sep 17 00:00:00 2001 From: Alexis Deveria Date: Thu, 1 Jul 2010 20:14:12 +0000 Subject: [PATCH] Added extension to offer server-based import/open ability to browsers without File API support git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1621 eee81c28-f429-11dd-99c0-75d572ba1ddd --- docs/index.html | 2 +- editor/extensions/ext-server_open.js | 94 ++++++++++++++++++++++++++++ editor/extensions/fileopen.php | 9 +++ editor/svg-editor.css | 6 +- editor/svg-editor.js | 24 +++---- editor/svgcanvas.js | 12 +++- 6 files changed, 131 insertions(+), 16 deletions(-) create mode 100644 editor/extensions/ext-server_open.js create mode 100644 editor/extensions/fileopen.php diff --git a/docs/index.html b/docs/index.html index 7e81af5e..635c3177 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/editor/extensions/ext-server_open.js b/editor/extensions/ext-server_open.js new file mode 100644 index 00000000..7cd23a5b --- /dev/null +++ b/editor/extensions/ext-server_open.js @@ -0,0 +1,94 @@ +/* + * ext-server_open.js + * + * Licensed under the Apache License, Version 2 + * + * Copyright(c) 2010 Alexis Deveria + * + */ + +svgEditor.addExtension("server_open", { + callback: function() { + // Do nothing if client support is found + if(window.FileReader) return; + + // Change these to appropriate script file + var open_svg_action = 'extensions/fileopen.php?type=load_svg'; + var import_svg_action = 'extensions/fileopen.php?type=import_svg'; + var import_img_action = 'extensions/fileopen.php?type=import_img'; + + // Set up function for PHP uploader to use + svgEditor.processFile = function(str64, type) { + var xmlstr = svgCanvas.Utils.decode64(str64); + + switch ( type ) { + case 'load_svg': + svgCanvas.clear(); + svgCanvas.setSvgString(xmlstr); + svgEditor.updateCanvas(); + break; + case 'import_svg': + svgCanvas.importSvgString(xmlstr); + svgEditor.updateCanvas(); + break; + } + } + + // Create upload form + var open_svg_form = $('
'); + open_svg_form.attr({ + enctype: 'multipart/form-data', + method: 'post', + action: open_svg_action, + target: 'upload_target' + }); + + // Create import form + var import_svg_form = open_svg_form.clone().attr('action', import_svg_action); + + // Create image form + var import_img_form = open_svg_form.clone().attr('action', import_img_action); + + // 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 = $('').appendTo(form); + + 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; + } + // This submits the form, which returns the file data using svgEditor.uploadSVG + form.submit(); + + rebuildInput(form); + }); + }); + } else { + inp.change(function() { + // This submits the form, which returns the file data using svgEditor.uploadSVG + form.submit(); + rebuildInput(form); + }); + } + } + + // Create the input elements + rebuildInput(open_svg_form); + rebuildInput(import_svg_form); + rebuildInput(import_img_form); + + // Create upload target (hidden iframe) + var target = $('