2010-07-14 18:05:03 +00:00
/ *
* 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'
} ,
{
2010-07-16 16:05:00 +00:00
name : 'IAN Symbol Libraries' ,
url : 'http://ian.umces.edu/symbols/catalog/svgedit/album_chooser.php' ,
2010-08-16 17:53:15 +00:00
description : 'Free library of illustrations'
2010-07-16 15:46:54 +00:00
}
2010-07-14 18:05:03 +00:00
] ;
2010-07-16 18:33:48 +00:00
var xlinkns = "http://www.w3.org/1999/xlink" ;
2010-07-14 18:05:03 +00:00
function closeBrowser ( ) {
$ ( '#imgbrowse_holder' ) . hide ( ) ;
}
2010-07-16 18:33:48 +00:00
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 ) ;
}
2010-07-14 18:05:03 +00:00
2010-09-16 20:10:12 +00:00
var mode = 's' ;
var multi _arr = [ ] ;
2010-09-17 17:13:15 +00:00
var cur _meta ;
var tranfer _stopped = false ;
var pending = { } ;
2010-07-14 18:05:03 +00:00
window . addEventListener ( "message" , function ( evt ) {
// Receive postMessage data
var response = evt . data ;
if ( ! response ) {
2010-09-20 18:55:10 +00:00
// Do nothing
2010-07-14 18:05:03 +00:00
return ;
}
var char1 = response . charAt ( 0 ) ;
2010-09-16 20:10:12 +00:00
var svg _str ;
var img _str ;
2010-09-17 17:13:15 +00:00
if ( char1 != "{" && tranfer _stopped ) {
tranfer _stopped = false ;
return ;
}
if ( char1 == '|' ) {
var secondpos = response . indexOf ( '|' , 1 ) ;
var id = response . substr ( 1 , secondpos - 1 ) ;
response = response . substr ( secondpos + 1 ) ;
char1 = response . charAt ( 0 ) ;
}
// Hide possible transfer dialog box
$ ( '#dialog_box' ) . hide ( ) ;
2010-07-14 18:05:03 +00:00
switch ( char1 ) {
2010-09-17 17:13:15 +00:00
case '{' :
// Metadata
tranfer _stopped = false ;
var cur _meta = JSON . parse ( response ) ;
pending [ cur _meta . id ] = cur _meta ;
var message = 'Retrieving "' + ( cur _meta . name || 'file' ) + '"...' ;
if ( mode != 'm' ) {
$ . process _cancel ( message , function ( ) {
tranfer _stopped = true ;
// Should a message be sent back to the frame?
$ ( '#dialog_box' ) . hide ( ) ;
} ) ;
} else {
var entry = $ ( '<div>' + message + '</div>' ) . data ( 'id' , cur _meta . id ) ;
preview . append ( entry ) ;
cur _meta . entry = entry ;
}
return ;
2010-07-14 18:05:03 +00:00
case '<' :
2010-09-16 20:10:12 +00:00
svg _str = true ;
2010-07-14 18:05:03 +00:00
break ;
case 'd' :
2010-07-16 18:33:48 +00:00
if ( response . indexOf ( 'data:image/svg+xml' ) === 0 ) {
2010-07-16 15:46:54 +00:00
var pre = 'data:image/svg+xml;base64,' ;
var src = response . substring ( pre . length ) ;
2010-09-16 20:10:12 +00:00
response = svgCanvas . Utils . decode64 ( src ) ;
svg _str = true ;
2010-07-14 18:05:03 +00:00
break ;
2010-07-16 18:33:48 +00:00
} else if ( response . indexOf ( 'data:image/' ) === 0 ) {
2010-09-16 20:10:12 +00:00
img _str = true ;
2010-07-16 18:33:48 +00:00
break ;
2010-07-14 18:05:03 +00:00
}
// Else fall through
default :
2010-07-16 18:33:48 +00:00
// 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);
2010-09-20 18:55:10 +00:00
// Don't give warning as postMessage may have been used by something else
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();
// }
// });
2010-07-14 18:05:03 +00:00
return ;
}
2010-09-16 20:10:12 +00:00
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 ] ) ;
2010-09-17 17:13:15 +00:00
var cur _meta = pending [ id ] ;
2010-09-16 20:10:12 +00:00
if ( svg _str ) {
2010-09-17 17:13:15 +00:00
if ( cur _meta && cur _meta . name ) {
2010-09-17 17:58:18 +00:00
var title = cur _meta . name ;
2010-09-17 17:13:15 +00:00
} 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 + ')' ;
}
if ( cur _meta ) {
preview . children ( ) . each ( function ( ) {
if ( $ ( this ) . data ( 'id' ) == id ) {
2010-09-17 17:58:18 +00:00
if ( cur _meta . preview _url ) {
$ ( this ) . html ( '<img src="' + cur _meta . preview _url + '">' + title ) ;
} else {
$ ( this ) . text ( title ) ;
}
2010-09-17 17:13:15 +00:00
}
} ) ;
} else {
preview . append ( '<div>' + title + '</div>' ) ;
}
2010-09-16 20:10:12 +00:00
} else {
2010-09-17 17:13:15 +00:00
if ( cur _meta && cur _meta . preview _url ) {
var title = cur _meta . name || '' ;
}
if ( cur _meta && cur _meta . preview _url ) {
var entry = '<img src="' + cur _meta . preview _url + '">' + title ;
} else {
var entry = '<img src="' + response + '">' ;
}
if ( cur _meta ) {
preview . children ( ) . each ( function ( ) {
if ( $ ( this ) . data ( 'id' ) == id ) {
$ ( this ) . html ( entry ) ;
}
} ) ;
} else {
preview . append ( $ ( '<div>' ) . append ( entry ) ) ;
}
2010-09-16 20:10:12 +00:00
}
break ;
case 'o' :
// Open
if ( ! svg _str ) break ;
svgEditor . openPrep ( function ( ok ) {
if ( ! ok ) return ;
svgCanvas . clear ( ) ;
svgCanvas . setSvgString ( response ) ;
// updateCanvas();
} ) ;
closeBrowser ( ) ;
break ;
}
2010-07-14 18:05:03 +00:00
} , true ) ;
2010-09-16 20:10:12 +00:00
var preview ;
function toggleMulti ( show ) {
var submit ;
$ ( '#lib_framewrap, #imglib_opts' ) . css ( { right : ( show ? 200 : 10 ) } ) ;
if ( ! preview ) {
preview = $ ( '<div id=imglib_preview>' ) . css ( {
position : 'absolute' ,
top : 45 ,
right : 10 ,
width : 180 ,
bottom : 45 ,
background : '#fff' ,
overflow : 'auto'
} ) . insertAfter ( '#lib_framewrap' ) ;
submit = $ ( '<button>Import selected</button>' ) . 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 ) ;
}
2010-09-16 20:25:44 +00:00
svgCanvas . moveSelectedElements ( i * 20 , i * 20 , false ) ;
2010-09-16 20:10:12 +00:00
} ) ;
preview . empty ( ) ;
multi _arr = [ ] ;
$ ( '#imgbrowse_holder' ) . hide ( ) ;
} ) . css ( {
position : 'absolute' ,
bottom : 10 ,
right : - 10
} ) ;
}
preview . toggle ( show ) ;
}
2010-07-14 18:05:03 +00:00
function showBrowser ( ) {
var browser = $ ( '#imgbrowse' ) ;
if ( ! browser . length ) {
2010-08-31 20:31:58 +00:00
$ ( ' < div id = imgbrowse _holder > < div id = imgbrowse class = toolbar _button > \
2010-07-14 18:05:03 +00:00
< / d i v > < / d i v > ' ) . i n s e r t A f t e r ( ' # s v g _ d o c p r o p s ' ) ;
browser = $ ( '#imgbrowse' ) ;
var all _libs = 'Select an image library' ;
var lib _opts = $ ( '<ul id=imglib_opts>' ) . appendTo ( browser ) ;
2010-09-16 20:10:12 +00:00
var frame = $ ( '<iframe/>' ) . prependTo ( browser ) . hide ( ) . wrap ( '<div id=lib_framewrap>' ) ;
2010-07-14 18:05:03 +00:00
2010-09-16 20:10:12 +00:00
var header = $ ( '<h1>' ) . prependTo ( browser ) . text ( all _libs ) . css ( {
position : 'absolute' ,
top : 0 ,
left : 0 ,
width : '100%'
} ) ;
2010-07-14 18:05:03 +00:00
2010-08-31 20:31:58 +00:00
var cancel = $ ( '<button>Cancel</button>' ) . appendTo ( browser ) . click ( function ( ) {
2010-07-14 18:05:03 +00:00
$ ( '#imgbrowse_holder' ) . hide ( ) ;
} ) . css ( {
position : 'absolute' ,
top : 5 ,
2010-08-31 20:31:58 +00:00
right : - 10
2010-07-14 18:05:03 +00:00
} ) ;
2010-09-16 20:10:12 +00:00
var leftBlock = $ ( '<span>' ) . css ( { position : 'absolute' , top : 5 , left : 10 } ) . appendTo ( browser ) ;
var back = $ ( '<button>Show libraries</button>' ) . appendTo ( leftBlock ) . click ( function ( ) {
2010-07-29 15:09:49 +00:00
frame . attr ( 'src' , 'about:blank' ) . hide ( ) ;
2010-07-14 18:05:03 +00:00
lib _opts . show ( ) ;
header . text ( all _libs ) ;
} ) . css ( {
2010-09-16 20:10:12 +00:00
'margin-right' : 5
} ) ;
2010-09-21 12:48:44 +00:00
var type = $ ( '<select><option value=s>Import single</option><option value=m>Import multiple</option><option value=o>Open as new document</option></select>' ) . appendTo ( leftBlock ) . change ( function ( ) {
2010-09-16 20:10:12 +00:00
mode = $ ( this ) . val ( ) ;
switch ( mode ) {
case 's' :
case 'o' :
toggleMulti ( false ) ;
break ;
case 'm' :
// Import multiple
toggleMulti ( true ) ;
}
} ) . css ( {
'margin-top' : 10
2010-07-14 18:05:03 +00:00
} ) ;
2010-08-31 20:31:58 +00:00
cancel . prepend ( $ . getSvgIcon ( 'cancel' , true ) ) ;
back . prepend ( $ . getSvgIcon ( 'tool_imagelib' , true ) ) ;
2010-07-14 18:05:03 +00:00
$ . each ( img _libs , function ( i , opts ) {
$ ( '<li>' ) . appendTo ( lib _opts ) . text ( opts . name ) . click ( function ( ) {
frame . attr ( 'src' , opts . url ) . show ( ) ;
2010-07-29 15:09:49 +00:00
header . text ( opts . name ) ;
2010-07-14 18:05:03 +00:00
lib _opts . hide ( ) ;
} ) . append ( '<span>' + opts . description + '</span>' ) ;
} ) ;
} else {
$ ( '#imgbrowse_holder' ) . show ( ) ;
}
}
return {
svgicons : "extensions/ext-imagelib.xml" ,
buttons : [ {
id : "tool_imagelib" ,
type : "app_menu" , // _flyout
position : 4 ,
title : "Image library" ,
events : {
"mouseup" : showBrowser
}
} ] ,
callback : function ( ) {
$ ( '<style>' ) . text ( ' \
# imgbrowse _holder { \
position : absolute ; \
top : 0 ; \
left : 0 ; \
width : 100 % ; \
height : 100 % ; \
background - color : rgba ( 0 , 0 , 0 , . 5 ) ; \
2010-07-29 15:09:49 +00:00
z - index : 5 ; \
2010-07-14 18:05:03 +00:00
} \
\
# imgbrowse { \
position : absolute ; \
top : 25 px ; \
left : 25 px ; \
right : 25 px ; \
bottom : 25 px ; \
min - width : 300 px ; \
min - height : 200 px ; \
background : # B0B0B0 ; \
border : 1 px outset # 777 ; \
} \
# imgbrowse h1 { \
font - size : 20 px ; \
margin : . 4 em ; \
text - align : center ; \
} \
2010-09-16 20:10:12 +00:00
# lib _framewrap , \
2010-07-14 18:05:03 +00:00
# imgbrowse > ul { \
position : absolute ; \
2010-08-31 20:31:58 +00:00
top : 45 px ; \
2010-07-14 18:05:03 +00:00
left : 10 px ; \
right : 10 px ; \
bottom : 10 px ; \
background : white ; \
margin : 0 ; \
padding : 0 ; \
2010-07-21 17:12:57 +00:00
} \
# imgbrowse > ul { \
2010-07-14 18:05:03 +00:00
overflow : auto ; \
} \
2010-09-16 20:10:12 +00:00
# imgbrowse > div { \
border : 1 px solid # 666 ; \
} \
# imglib _preview > div { \
padding : 5 px ; \
2010-09-17 17:13:15 +00:00
font - size : 12 px ; \
} \
# imglib _preview img { \
display : block ; \
margin : 0 auto ; \
max - height : 100 px ; \
2010-09-16 20:10:12 +00:00
} \
2010-07-14 18:05:03 +00:00
# imgbrowse li { \
list - style : none ; \
padding : . 5 em ; \
background : # E8E8E8 ; \
border - bottom : 1 px solid # B0B0B0 ; \
line - height : 1.2 em ; \
font - style : sans - serif ; \
} \
# imgbrowse li > span { \
color : # 666 ; \
font - size : 15 px ; \
display : block ; \
} \
# imgbrowse li : hover { \
background : # FFC ; \
cursor : pointer ; \
} \
# imgbrowse iframe { \
width : 100 % ; \
height : 100 % ; \
border : 0 ; \
} \
').appendTo(' head ' ) ;
}
}
} ) ;