git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2666 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Brett Zamir 2014-02-02 00:34:04 +00:00
parent adbec3e33e
commit a5083f0394
1 changed files with 60 additions and 58 deletions

View File

@ -1,3 +1,5 @@
/*globals $, svgEditor, svgedit, svgCanvas, DOMParser*/
/*jslint vars: true, eqeq: true, es5: true, todo: true */
/*
* ext-imagelib.js
*
@ -7,7 +9,7 @@
*
*/
svgEditor.addExtension("imagelib", function() {
svgEditor.addExtension("imagelib", function() {'use strict';
var uiStrings = svgEditor.uiStrings;
@ -59,50 +61,50 @@ svgEditor.addExtension("imagelib", function() {
var cur_meta;
var tranfer_stopped = false;
var pending = {};
window.addEventListener("message", function(evt) {
var preview, submit;
window.addEventListener("message", function(evt) {
// Receive postMessage data
var response = evt.data;
if(!response || typeof response !== "string") { // Todo: Should namespace postMessage API for this extension and filter out here
if (!response || typeof response !== "string") { // Todo: Should namespace postMessage API for this extension and filter out here
// Do nothing
return;
}
try { // This block can be removed if embedAPI moves away from a string to an object (if IE9 support not needed)
var res = JSON.parse(response);
if (res.namespace) { // Part of embedAPI communications
return;
}
}
catch (e) {}
try { // This block can be removed if embedAPI moves away from a string to an object (if IE9 support not needed)
var res = JSON.parse(response);
if (res.namespace) { // Part of embedAPI communications
return;
}
}
catch (e) {}
var char1 = response.charAt(0);
var id;
var svg_str;
var img_str;
if(char1 != "{" && tranfer_stopped) {
if (char1 != "{" && tranfer_stopped) {
tranfer_stopped = false;
return;
}
if(char1 == '|') {
if (char1 == '|') {
var secondpos = response.indexOf('|', 1);
var id = response.substr(1, secondpos-1);
id = response.substr(1, secondpos-1);
response = response.substr(secondpos+1);
char1 = response.charAt(0);
}
// Hide possible transfer dialog box
$('#dialog_box').hide();
var entry, cur_meta;
switch (char1) {
case '{':
// Metadata
tranfer_stopped = false;
var cur_meta = JSON.parse(response);
cur_meta = JSON.parse(response);
pending[cur_meta.id] = cur_meta;
@ -110,7 +112,7 @@ svgEditor.addExtension("imagelib", function() {
var message = uiStrings.notification.retrieving.replace('%s', name);
if(mode != 'm') {
if (mode != 'm') {
$.process_cancel(message, function() {
tranfer_stopped = true;
// Should a message be sent back to the frame?
@ -118,7 +120,7 @@ svgEditor.addExtension("imagelib", function() {
$('#dialog_box').hide();
});
} else {
var entry = $('<div>' + message + '</div>').data('id', cur_meta.id);
entry = $('<div>' + message + '</div>').data('id', cur_meta.id);
preview.append(entry);
cur_meta.entry = entry;
}
@ -128,46 +130,46 @@ svgEditor.addExtension("imagelib", function() {
svg_str = true;
break;
case 'd':
if(response.indexOf('data:image/svg+xml') === 0) {
if (response.indexOf('data:image/svg+xml') === 0) {
var pre = 'data:image/svg+xml;base64,';
var src = response.substring(pre.length);
response = svgedit.utilities.decode64(src);
svg_str = true;
break;
} else if(response.indexOf('data:image/') === 0) {
} 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,' + svgedit.utilities.encode64(response, true);
// var str = 'data:;base64,' + svgedit.utilities.encode64(response, true);
// Assume it's raw image data
// importImage(str);
// importImage(str);
// Don't give warning as postMessage may have been used by something else
if(mode !== 'm') {
if (mode !== 'm') {
closeBrowser();
} else {
pending[id].entry.remove();
}
// $.alert('Unexpected data was returned: ' + response, function() {
// if(mode !== 'm') {
// closeBrowser();
// } else {
// pending[id].entry.remove();
// }
// });
// $.alert('Unexpected data was returned: ' + response, function() {
// if (mode !== 'm') {
// closeBrowser();
// } else {
// pending[id].entry.remove();
// }
// });
return;
}
switch (mode) {
case 's':
// Import one
if(svg_str) {
if (svg_str) {
svgCanvas.importSvgString(response);
} else if(img_str) {
} else if (img_str) {
importImage(response);
}
closeBrowser();
@ -175,19 +177,20 @@ svgEditor.addExtension("imagelib", function() {
case 'm':
// Import multiple
multi_arr.push([(svg_str ? 'svg' : 'img'), response]);
var cur_meta = pending[id];
if(svg_str) {
if(cur_meta && cur_meta.name) {
var title = cur_meta.name;
} else {
var title;
cur_meta = pending[id];
if (svg_str) {
if (cur_meta && cur_meta.name) {
title = cur_meta.name;
} else {
// Try to find a title
var xml = new DOMParser().parseFromString(response, 'text/xml').documentElement;
var title = $(xml).children('title').first().text() || '(SVG #' + response.length + ')';
title = $(xml).children('title').first().text() || '(SVG #' + response.length + ')';
}
if(cur_meta) {
if (cur_meta) {
preview.children().each(function() {
if($(this).data('id') == id) {
if(cur_meta.preview_url) {
if ($(this).data('id') == id) {
if (cur_meta.preview_url) {
$(this).html('<img src="' + cur_meta.preview_url + '">' + title);
} else {
$(this).text(title);
@ -200,18 +203,18 @@ svgEditor.addExtension("imagelib", function() {
submit.removeAttr('disabled');
}
} else {
if(cur_meta && cur_meta.preview_url) {
var title = cur_meta.name || '';
if (cur_meta && cur_meta.preview_url) {
title = cur_meta.name || '';
}
if(cur_meta && cur_meta.preview_url) {
var entry = '<img src="' + cur_meta.preview_url + '">' + title;
if (cur_meta && cur_meta.preview_url) {
entry = '<img src="' + cur_meta.preview_url + '">' + title;
} else {
var entry = '<img src="' + response + '">';
entry = '<img src="' + response + '">';
}
if(cur_meta) {
if (cur_meta) {
preview.children().each(function() {
if($(this).data('id') == id) {
if ($(this).data('id') == id) {
$(this).html(entry);
submit.removeAttr('disabled');
}
@ -225,9 +228,9 @@ svgEditor.addExtension("imagelib", function() {
break;
case 'o':
// Open
if(!svg_str) break;
if (!svg_str) {break;}
svgEditor.openPrep(function(ok) {
if(!ok) return;
if (!ok) {return;}
svgCanvas.clear();
svgCanvas.setSvgString(response);
// updateCanvas();
@ -236,13 +239,11 @@ svgEditor.addExtension("imagelib", function() {
break;
}
}, true);
var preview, submit;
function toggleMulti(show) {
$('#lib_framewrap, #imglib_opts').css({right: (show ? 200 : 10)});
if(!preview) {
if (!preview) {
preview = $('<div id=imglib_preview>').css({
position: 'absolute',
top: 45,
@ -259,7 +260,7 @@ svgEditor.addExtension("imagelib", function() {
$.each(multi_arr, function(i) {
var type = this[0];
var data = this[1];
if(type == 'svg') {
if (type == 'svg') {
svgCanvas.importSvgString(data);
} else {
importImage(data);
@ -284,7 +285,7 @@ svgEditor.addExtension("imagelib", function() {
function showBrowser() {
var browser = $('#imgbrowse');
if(!browser.length) {
if (!browser.length) {
$('<div id=imgbrowse_holder><div id=imgbrowse class=toolbar_button>\
</div></div>').insertAfter('#svg_docprops');
browser = $('#imgbrowse');
@ -338,6 +339,7 @@ svgEditor.addExtension("imagelib", function() {
case 'm':
// Import multiple
toggleMulti(true);
break;
}
}).css({
'margin-top': 10
@ -453,6 +455,6 @@ svgEditor.addExtension("imagelib", function() {
}\
').appendTo('head');
}
}
};
});