From 2e4e5103412ad96fdecc714163b8d2a31b706ae0 Mon Sep 17 00:00:00 2001 From: Georg Fischer Date: Sun, 15 Dec 2013 20:25:46 +0100 Subject: [PATCH] include used options to file name. --- scripts/src/export-button.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/scripts/src/export-button.js b/scripts/src/export-button.js index 64b3c28..e6437cb 100644 --- a/scripts/src/export-button.js +++ b/scripts/src/export-button.js @@ -5,6 +5,9 @@ define( var signals; var export_button; var png_link; + var default_file_name; + var file_name; + var file_suffix = '.png'; function init( shared ) { @@ -12,20 +15,31 @@ define( export_button = document.getElementById( 'export-button' ); png_link = document.getElementById( 'png-button' ); + default_file_name = png_link.getAttribute( 'download' ).split( file_suffix )[0]; + file_name = default_file_name + file_suffix; + export_button.addEventListener( 'click', exportButtonClicked, false ); png_link.addEventListener( 'click', hidePNGLink, false ); + + signals['control-updated'].add( updateFileName ); } function exportButtonClicked( event ) { event.preventDefault(); - signals['image-data-url-requested'].dispatch( upldatePNGLinkAddress ); + signals['image-data-url-requested'].dispatch( updatePNGLinkAddress ); } - function upldatePNGLinkAddress( data_url ) + function updateFileName( options ) + { + file_name = default_file_name + '-' + objToString( options ) + '.png'; + } + + function updatePNGLinkAddress( data_url ) { png_link.href = data_url; + png_link.setAttribute( 'download', file_name ); png_link.classList.add( 'is-active' ); } @@ -34,6 +48,18 @@ define( png_link.classList.remove( 'is-active' ); } + function objToString( obj ) + { + var result = [ ]; + + for ( var key in obj ) + { + result.push( key + '' + obj[key] ); + } + + return result.join( '-' ); + } + return { init: init }; } ); \ No newline at end of file