/* * ext-imagelib.js * * Licensed under the Apache License, Version 2 * * Copyright(c) 2010 Alexis Deveria * */ svgEditor.addExtension("imagelib", function() { var img_libs = [{ name: 'Demo library (local)', url: 'extensions/imagelib/index.html', description: 'Demonstration library for SVG-edit on this server' }, { name: 'IAN Symbol Libraries', url: 'http://ian.umces.edu/symbols/catalog/svgedit/album_chooser.php', description: 'Free library of illustrations' } ]; var xlinkns = "http://www.w3.org/1999/xlink"; function closeBrowser() { $('#imgbrowse_holder').hide(); } function importImage(url) { var newImage = svgCanvas.addSvgElementFromJson({ "element": "image", "attr": { "x": 0, "y": 0, "width": 0, "height": 0, "id": svgCanvas.getNextId(), "style": "pointer-events:inherit" } }); svgCanvas.clearSelection(); svgCanvas.addToSelection([newImage]); svgCanvas.setImageURL(url); } var mode = 's'; var multi_arr = []; window.addEventListener("message", function(evt) { // Receive postMessage data var response = evt.data; if(!response) { $.alert('No data was given', closeBrowser); return; } var char1 = response.charAt(0); var svg_str; var img_str; switch (char1) { case '<': svg_str = true; break; case 'd': if(response.indexOf('data:image/svg+xml') === 0) { var pre = 'data:image/svg+xml;base64,'; var src = response.substring(pre.length); response = svgCanvas.Utils.decode64(src); svg_str = true; break; } else if(response.indexOf('data:image/') === 0) { img_str = true; break; } // Else fall through default: // TODO: See if there's a way to base64 encode the binary data stream // var str = 'data:;base64,' + svgCanvas.Utils.encode64(response, true); // Assume it's raw image data // importImage(str); $.alert('Unexpected data was returned: ' + response, closeBrowser); return; } switch (mode) { case 's': // Import one if(svg_str) { svgCanvas.importSvgString(response); } else if(img_str) { importImage(response); } closeBrowser(); break; case 'm': // Import multiple multi_arr.push([(svg_str ? 'svg' : 'img'), response]); if(svg_str) { var xml = new DOMParser().parseFromString(response, 'text/xml').documentElement; var title = $(xml).children('title').first().text() || '(SVG #' + response.length + ')'; preview.append('
'+title+'
'); } else { preview.append('
Raster image #' + response.length + '
'); } break; case 'o': // Open if(!svg_str) break; svgEditor.openPrep(function(ok) { if(!ok) return; svgCanvas.clear(); svgCanvas.setSvgString(response); // updateCanvas(); }); closeBrowser(); break; } }, true); var preview; function toggleMulti(show) { var submit; $('#lib_framewrap, #imglib_opts').css({right: (show ? 200 : 10)}); if(!preview) { preview = $('
').css({ position: 'absolute', top: 45, right: 10, width: 180, bottom: 45, background: '#fff', overflow: 'auto' }).insertAfter('#lib_framewrap'); submit = $('').appendTo('#imgbrowse').click(function() { $.each(multi_arr, function(i) { var type = this[0]; var data = this[1]; if(type == 'svg') { svgCanvas.importSvgString(data); } else { importImage(data); } svgCanvas.moveSelectedElements(i*20, i*20, false); }); preview.empty(); multi_arr = []; $('#imgbrowse_holder').hide(); }).css({ position: 'absolute', bottom: 10, right: -10 }); } preview.toggle(show); } function showBrowser() { var browser = $('#imgbrowse'); if(!browser.length) { $('
\
').insertAfter('#svg_docprops'); browser = $('#imgbrowse'); var all_libs = 'Select an image library'; var lib_opts = $('