From 711f1e14570bc60e9b99490b04e3b8c14b83ee42 Mon Sep 17 00:00:00 2001
From: Jaden Diefenbaugh <blakcap@users.noreply.github.com>
Date: Tue, 16 May 2017 15:21:02 +0200
Subject: [PATCH] download link downloads all plots, fix weird angular
 undefined variables issues, fixes #15

---
 .../reports/app/directives/downloadLink.js    | 30 +++++++++++--------
 .../reports/app/directives/edit/plotItem.js   | 15 +++++-----
 .../reports/app/directives/view/plotItem.js   | 15 +++++-----
 3 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/beat/web/reports/static/reports/app/directives/downloadLink.js b/beat/web/reports/static/reports/app/directives/downloadLink.js
index 213e368e0..49342b12c 100644
--- a/beat/web/reports/static/reports/app/directives/downloadLink.js
+++ b/beat/web/reports/static/reports/app/directives/downloadLink.js
@@ -30,30 +30,34 @@ angular.module('reportApp')
 	return {
 		scope: {
 			getDownloadHref: '&',
-			domId: '=',
-			imgName: '='
+			domId: '@',
+			imgName: '@'
 		},
 		link: function(scope){
-			scope.downloadLinkId = `${scope.domId}-download`;
+			scope.downloadLinkId = () => `${scope.domId}-download`;
+
 			scope.setupDownloadLinkTest = (event) => {
-				const a = document.querySelector(`#${scope.downloadLinkId}`);
-				const href = scope.getDownloadHref()();
+				const a = document.querySelector(`#${scope.downloadLinkId()}`);
+				const hrefs = scope.getDownloadHref()();
 
-				if(!href){
-					event.preventDefault();
-					return false;
-				}
+				event.preventDefault();
 
-				a.href = href;
+				hrefs.forEach((href, i) => {
+					a.href = href;
+					a.download = `${scope.imgName}-${i}.png`;
+					a.click();
+					a.href = '';
+					a.download = '';
+				})
+				;
 			};
 		},
 		template: `
 <a
-	id='{{ downloadLinkId }}'
+	id='{{ downloadLinkId() }}'
 	style='color: white;'
 	class='btn btn-primary'
-	ng-click='setupDownloadLinkTest()'
-	download='img.png'>
+	ng-click='setupDownloadLinkTest($event)'>
 	<i class='fa fa-download fa-lg'></i>
 	Download
 </a>
diff --git a/beat/web/reports/static/reports/app/directives/edit/plotItem.js b/beat/web/reports/static/reports/app/directives/edit/plotItem.js
index 79c6df93d..e2e320b21 100644
--- a/beat/web/reports/static/reports/app/directives/edit/plotItem.js
+++ b/beat/web/reports/static/reports/app/directives/edit/plotItem.js
@@ -84,12 +84,13 @@ angular.module('reportApp')
 			);
 
 			scope.exportSrcFunc = () => () => {
-				const el = document.querySelector(`#${scope.renderDivId} img`);
-				if(!el){
-					return false;
-				}
+				const el = document.querySelectorAll(`#${scope.renderDivId} img`);
+				const srcs = Array.from(el)
+				.map(e => e.src)
+				//.map(src => src.replace(/^data:undefined/, 'data:application/octet-stream'));
+				;
 
-				return el.src;
+				return srcs;
 			};
 		},
 		template: `
@@ -118,8 +119,8 @@ angular.module('reportApp')
 			</span>
 		</div>
 		<download-link
-			domId='domId'
-			imgName='"test.png"'
+			dom-id='{{ domId }}'
+			img-name='{{ renderDivId }}'
 			get-download-href='exportSrcFunc()'
 			>
 			</download-link>
diff --git a/beat/web/reports/static/reports/app/directives/view/plotItem.js b/beat/web/reports/static/reports/app/directives/view/plotItem.js
index 2b268fffc..a13535e30 100644
--- a/beat/web/reports/static/reports/app/directives/view/plotItem.js
+++ b/beat/web/reports/static/reports/app/directives/view/plotItem.js
@@ -43,19 +43,20 @@ angular.module('reportApp')
 			});
 
 			scope.exportSrcFunc = () => () => {
-				const el = document.querySelector(`#${scope.renderDivId} img`);
-				if(!el){
-					return false;
-				}
+				const el = document.querySelectorAll(`#${scope.renderDivId} img`);
+				const srcs = Array.from(el)
+				.map(e => e.src)
+				//.map(src => src.replace(/^data:undefined/, 'data:application/octet-stream'));
+				;
 
-				return el.src;
+				return srcs;
 			};
 		},
 		template: `
 <download-link
 	class='pull-right'
-	domId='domId'
-	imgName='"test.png"'
+	dom-id='{{ domId }}'
+	img-name='{{ renderDivId }}'
 	get-download-href='exportSrcFunc()'
 	>
 	</download-link>
-- 
GitLab