From 06ea78b0d89dc32a9f0872894c93e3ea9ee171b3 Mon Sep 17 00:00:00 2001 From: Georg Fischer Date: Mon, 16 Dec 2013 15:08:38 +0100 Subject: [PATCH] store name of uploaded file --- scripts/src/export-button.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/scripts/src/export-button.js b/scripts/src/export-button.js index e6437cb..86e5709 100644 --- a/scripts/src/export-button.js +++ b/scripts/src/export-button.js @@ -7,21 +7,24 @@ define( var png_link; var default_file_name; var file_name; + var download_file_name; var file_suffix = '.png'; + var file_suffix_regex = /(\.)(jpg|jpeg|png|gif|bmp)/ig; function init( shared ) { signals = shared.signals; 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; + default_file_name = png_link.getAttribute( 'download' ).replace( file_suffix_regex, '' ); + file_name = default_file_name; + download_file_name = default_file_name; export_button.addEventListener( 'click', exportButtonClicked, false ); png_link.addEventListener( 'click', hidePNGLink, false ); - signals['control-updated'].add( updateFileName ); + signals['load-file'].add( updateFileName ); + signals['control-updated'].add( updateDownloadFileName ); } function exportButtonClicked( event ) @@ -31,15 +34,26 @@ define( signals['image-data-url-requested'].dispatch( updatePNGLinkAddress ); } - function updateFileName( options ) + function updateFileName( file ) { - file_name = default_file_name + '-' + objToString( options ) + '.png'; + if ( + file && + typeof file.name === 'string' + ) + { + file_name = file.name.replace( file_suffix_regex, '' ); + } + } + + function updateDownloadFileName( options ) + { + download_file_name = file_name + '-' + objToString( options ) + file_suffix; } function updatePNGLinkAddress( data_url ) { png_link.href = data_url; - png_link.setAttribute( 'download', file_name ); + png_link.setAttribute( 'download', download_file_name ); png_link.classList.add( 'is-active' ); } @@ -54,7 +68,7 @@ define( for ( var key in obj ) { - result.push( key + '' + obj[key] ); + result.push( key[0] + '' + obj[key] ); } return result.join( '-' );