MT#7867 Repeat behavior of embedded image on "Change Image" button.

Also added functionality to convert link to embed image, but it
doesn't work on cross-domain links.
gjungwirth/voicemail_number
Irina Peshinskaya 12 years ago
parent d74f2653f1
commit dd06b7fce4

@ -29,8 +29,8 @@ svgEditor.addExtension("server_opensave", {
return true;
}
}
var open_svg_action, import_svg_action, import_img_action,
open_svg_form, import_svg_form, import_img_form,
var open_svg_action, import_svg_action, import_img_action, reimport_img_action,
open_svg_form, import_svg_form, import_img_form, reimport_img_form,
save_svg_action = svgEditor.curConfig.extPath + 'filesave.php',
save_img_action = svgEditor.curConfig.extPath + 'filesave.php',
// Create upload target (hidden iframe)
@ -114,6 +114,7 @@ svgEditor.addExtension("server_opensave", {
import_svg_action = svgEditor.curConfig.extPath + 'fileopen.php?type=import_svg';
//import_img_action = svgEditor.curConfig.extPath + 'fileopen.php?type=import_img';
import_img_action = '/invoicetemplate/embedimage?type=import_img';
reimport_img_action = '/invoicetemplate/embedimage?type=reimport_img';
// Set up function for PHP uploader to use
svgEditor.processFile = function(str64, type) {
@ -124,7 +125,7 @@ svgEditor.addExtension("server_opensave", {
}
$('#dialog_box').hide();
if (type !== 'import_img') {
if (type !== 'import_img' && type !== 'reimport_img') {
xmlstr = svgedit.utilities.decode64(str64);
}
@ -141,6 +142,9 @@ svgEditor.addExtension("server_opensave", {
case 'import_img':
svgCanvas.setGoodImage(str64);
break;
case 'reimport_img':
svgEditor.setImageURL(str64);
break;
}
};
@ -158,6 +162,7 @@ svgEditor.addExtension("server_opensave", {
// Create image form
import_img_form = open_svg_form.clone().attr('action', import_img_action);
reimport_img_form = open_svg_form.clone().attr({'action': reimport_img_action,style: "visibility:hidden;width: 20px; height: 1px;"});
// It appears necessary to rebuild this input every time a file is
// selected so the same file can be picked and the change event can fire.
@ -200,11 +205,13 @@ svgEditor.addExtension("server_opensave", {
rebuildInput(open_svg_form);
rebuildInput(import_svg_form);
rebuildInput(import_img_form);
rebuildInput(reimport_img_form);
// Add forms to buttons
$("#tool_open").show().prepend(open_svg_form);
$("#tool_import").show().prepend(import_svg_form);
$("#tool_image").prepend(import_img_form);
$("#change_image_url").prepend(reimport_img_form);
}
});

@ -293,12 +293,13 @@ by creating the following file and adding by calls to svgEditor.setConfig -->
</label>
</div>
<div class="toolset">
<label id="tool_image_url">url:
<label id="tool_image_url" style="display:none;">url:
<input id="image_url" type="text" title="Change URL" size="35"/>
</label>
<label id="tool_change_image">
<button id="change_image_url" style="display:none;">Change Image</button>
<span id="url_notice" title="NOTE: This image cannot be embedded. It will depend on this path to be displayed"></span>
<label id="tool_change_image" style="border: solid 1px black;">Change Image
<!--button id="change_image_url" style="display:none;">Change Image</button-->
<div id="change_image_url"></div>
<!--span id="url_notice" title="NOTE: This image cannot be embedded. It will depend on this path to be displayed"></span-->
</label>
</div>
</div>

@ -1144,27 +1144,114 @@ TO-DOS
}
};
var setImageURL = editor.setImageURL = function(url) {
var setImageURL = editor.setImageURL = function(url, useImageData) {
if (!url) {
url = defaultImageURL;
}
svgCanvas.setImageURL(url);
$('#image_url').val(url);
if (url.indexOf('data:') === 0) {
// data URI found
$('#image_url').hide();
$('#change_image_url').show();
} else {
// regular URL
svgCanvas.embedImage(url, function(dataURI) {
// Couldn't embed, so show warning
$('#url_notice').toggle(!dataURI);
defaultImageURL = url;
});
$('#image_url').show();
$('#change_image_url').hide();
}
useImageData = 0;
var setImageUrlOrData = function(url){
svgCanvas.setImageURL(url);
$('#image_url').val(url);
if (url.indexOf('data:') === 0) {
// data URI found
$('#image_url').hide();
$('#change_image_url').show();
} else {
// regular URL
svgCanvas.embedImage(url, function(dataURI) {
// Couldn't embed, so show warning
$('#url_notice').toggle(!dataURI);
defaultImageURL = url;
});
$('#image_url').show();
$('#change_image_url').hide();
}
};
//all solution ideas are from https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data
//It is more reliable not to store original link and get image content on stage od saving to DB or even generation, as backend and moreover generation script may doesn't have access to the image url (which can be accessed using cookies and so on)
if(useImageData){
var loadBinary = function(url,callback) {
var xmlHttpReq = false;
//alert(q);
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open('GET', url, true);
//XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
//this doesn't work - data are broken anyway
//if(xmlHttpReq.overrideMimeType){
// xmlHttpReq.overrideMimeType('text\/plain; charset=x-user-defined');
//}
xmlHttpReq.responseType = "arraybuffer";
if(callback){
xmlHttpReq.onreadystatechange = function(){
if (xmlHttpReq.readyState == 4) {
if(typeof callback == 'function'){
callback(xmlHttpReq);
}else{
eval(callback);
}
}
}
}
xmlHttpReq.send(null);
};
//two functions from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding#Solution_.232_.E2.80.93_rewriting_atob()_and_btoa()_using_TypedArrays_and_UTF-8
function uint6ToB64 (nUint6) {
return nUint6 < 26 ?
nUint6 + 65
: nUint6 < 52 ?
nUint6 + 71
: nUint6 < 62 ?
nUint6 - 4
: nUint6 === 62 ?
43
: nUint6 === 63 ?
47
:
65;
};
function base64EncArr (aBytes) {
var nMod3 = 2, sB64Enc = "";
for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
nUint24 = 0;
}
}
return sB64Enc.substr(0, sB64Enc.length - 2 + nMod3) + (nMod3 === 2 ? '' : nMod3 === 1 ? '=' : '==');
};
var imageDataCallback = function(request){
var contentType = ''+request.getResponseHeader('Content-Type');
contentType = contentType.replace(/image\/x\-/i,'image/');
var content = '';
//if(request.responseBody){//ie
// content = request.responseBody;
// url = 'data:'+contentType+';base64,'+svgedit.utilities.encode64(content);
//}else if(request.response){
content = request.response;
var byteArray = new Uint8Array(content);
url = 'data:'+contentType+';base64,'+base64EncArr(byteArray);
//}else{
// content = request.responseText;
// url = 'data:'+contentType+';base64,'+svgedit.utilities.encode64(content);
//}
setImageUrlOrData(url);
};
loadBinary(url,imageDataCallback);
}else{
setImageUrlOrData(url);
}
};
function setBackground (color, url) {
@ -1180,7 +1267,7 @@ TO-DOS
var curhref = svgCanvas.getHref(selectedElement);
curhref = curhref.indexOf('data:') === 0 ? '' : curhref;
$.prompt(uiStrings.notification.enterNewImgURL, curhref, function(url) {
if (url) {setImageURL(url);}
if (url) {setImageURL(url, 1);}//useImageData=1
});
}
@ -2962,7 +3049,7 @@ TO-DOS
});
$('#image_url').change(function() {
setImageURL(this.value);
setImageURL(this.value, 1);//useImageData = 1
});
$('#link_url').change(function() {
@ -3920,7 +4007,7 @@ TO-DOS
$.alert(this.title);
});
$('#change_image_url').click(promptImgURL);
//$('#change_image_url').click(promptImgURL);
// added these event handlers for all the push buttons so they
// behave more like buttons being pressed-in and not images

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<script>
window.parent.window.svgEditor.processFile("[%IF 'import_img' == in.type%]data:[%out.image_content_mimetype%];base64,[%END%][%out.image_content_base64%]", "[%in.type%]");
window.parent.window.svgEditor.processFile("[%IF 'import_img' == in.type || 'reimport_img' == in.type%]data:[%out.image_content_mimetype%];base64,[%END%][%out.image_content_base64%]", "[%in.type%]");
</script>
</head><body></body>
</html>

Loading…
Cancel
Save