From 93c4415bb54aafeaff93521787d93103601bded3 Mon Sep 17 00:00:00 2001
From: Jaden Diefenbaugh <blakcap@users.noreply.github.com>
Date: Tue, 14 Feb 2017 14:45:31 +0100
Subject: [PATCH] finish adding groups to report API, now saving/loading
 appropriately

---
 .../app/controllers/groupsController.js       |  2 --
 .../app/controllers/reportController.js       |  3 ++
 .../reports/app/directives/saveReportItems.js |  8 ++++--
 .../reports/app/services/reportService.js     | 28 +++++++++++++++++--
 4 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/beat/web/reports/static/reports/app/controllers/groupsController.js b/beat/web/reports/static/reports/app/controllers/groupsController.js
index 4038f2032..50ee63707 100644
--- a/beat/web/reports/static/reports/app/controllers/groupsController.js
+++ b/beat/web/reports/static/reports/app/controllers/groupsController.js
@@ -24,8 +24,6 @@ angular.module('reportApp').controller('GroupsController', ['$scope', 'ReportSer
 	let vm = this;
 
 	// testing stuffs
-	ReportService.createGroup('great');
-	ReportService.addExperimentToGroup('exp1', 'great');
 	vm.test = 'test';
 	vm.report = $scope.report;
 
diff --git a/beat/web/reports/static/reports/app/controllers/reportController.js b/beat/web/reports/static/reports/app/controllers/reportController.js
index 25df17675..ec53e8589 100644
--- a/beat/web/reports/static/reports/app/controllers/reportController.js
+++ b/beat/web/reports/static/reports/app/controllers/reportController.js
@@ -98,6 +98,9 @@ angular.module('reportApp').controller('reportController',['$scope', 'reportFact
 		if($scope.report.content.alias_experiments != undefined){
 			mutateSave($scope.report_experiments_alias, $scope.report.content.alias_experiments);
 		}
+
+		// save groups data to the model
+		ReportService.loadGroups($scope.report.content.groups);
 	}
 
 	function getReportData(user, report_id){
diff --git a/beat/web/reports/static/reports/app/directives/saveReportItems.js b/beat/web/reports/static/reports/app/directives/saveReportItems.js
index e4bb830fe..50b5ae957 100644
--- a/beat/web/reports/static/reports/app/directives/saveReportItems.js
+++ b/beat/web/reports/static/reports/app/directives/saveReportItems.js
@@ -21,7 +21,7 @@
  */
 
 //Directive for opening smart_selector list on "Add a report item" button click that displays options
-angular.module('reportApp').directive("savereportitems", function($compile){
+angular.module('reportApp').directive("savereportitems", ['$compile', 'ReportService', function($compile, ReportService){
 	return function(scope, element, attrs){
 		//add new report item
 		element.bind("click", function(){
@@ -57,6 +57,10 @@ angular.module('reportApp').directive("savereportitems", function($compile){
 				savecontent["sorted_tables_keys_reverse"] = scope.sorted_experiments_keys_reverse;
 				savecontent["sorted_tables_sortkey"] = scope.sorted_experiments_keys_tables_sortkey;
 			}
+
+			// save group data
+			savecontent['groups'] = ReportService.serializeGroups();
+
 			//call set report content from factory
 			let mydict = {};
 			mydict["experiments"] = scope.report.experiments;
@@ -84,6 +88,6 @@ angular.module('reportApp').directive("savereportitems", function($compile){
 
 		}
 	};
-});
+}]);
 
 
diff --git a/beat/web/reports/static/reports/app/services/reportService.js b/beat/web/reports/static/reports/app/services/reportService.js
index cbf7a0ac7..95dcc89a4 100644
--- a/beat/web/reports/static/reports/app/services/reportService.js
+++ b/beat/web/reports/static/reports/app/services/reportService.js
@@ -69,6 +69,17 @@ angular.module('reportApp').factory('ReportService', ['$http', function($http){
 	// gets groups
 	reportServiceInstance.groups = groupData;
 
+	// serializes groups as an object with form:
+	// {
+	// 	<group name 1>: <array of exp names for group 1>,
+	// 	...
+	// }
+	reportServiceInstance.serializeGroups = () => {
+		return groupData
+		.map(g => { return { [g.name]: g.experiments }; })
+		.reduce((o, g) => Object.assign(o, g), {});
+	};
+
 	// create a new group for the report
 	// returns false if it already exists
 	// returns true if it adds a new group
@@ -93,9 +104,20 @@ angular.module('reportApp').factory('ReportService', ['$http', function($http){
 		}
 	};
 
-	// fetch group info from the api
-	reportServiceInstance.fetchGroups = (reportName) => {
-		console.warn(`not implemented: reportServiceInstance.fetchGroups(${JSON.stringify(reportName)})`);
+	// load group info from the serialized format:
+	// {
+	// 	<group name 1>: <array of exp names for group 1>,
+	// 	...
+	// }
+	reportServiceInstance.loadGroups = (data) => {
+		// wipe data and load groups
+		groupData.splice(0, groupData.length);
+		Object.entries(data)
+		.forEach(([groupName, expNames]) => {
+			let g = new Group(groupName);
+			expNames.forEach(n => g.addExperiment(n));
+			groupData.push(g);
+		});
 	};
 
 	// save group info via sending to server
-- 
GitLab