From eb9d775e669fe17c17d05801a98c59d05ce72438 Mon Sep 17 00:00:00 2001
From: Frederic Bonifas <fredericbonifas@gmail.com>
Date: Tue, 11 Aug 2015 15:27:41 +0200
Subject: [PATCH] #50: export des filtres d'interaction

---
 app/scripts/controllers/commonmap.js  | 17 +++---
 app/scripts/controllers/globalmap.js  |  1 -
 app/scripts/services/exportservice.js | 88 +++++++++++++++++++++++++--
 app/scripts/services/sharesvg.js      | 24 +++++++-
 4 files changed, 113 insertions(+), 17 deletions(-)

diff --git a/app/scripts/controllers/commonmap.js b/app/scripts/controllers/commonmap.js
index 0a1cd98c..aacb2212 100644
--- a/app/scripts/controllers/commonmap.js
+++ b/app/scripts/controllers/commonmap.js
@@ -78,8 +78,8 @@ angular.module('accessimapEditeurDerApp')
         'deletable': false
       }];
 
-      var checkboxTemplate = '<input ng-if="row.entity.type === \'boolean\'" type="checkbox" value="{{row.entity[col.field]}}" ng-model="row.entity[col.field]">';
-      checkboxTemplate += '<div ng-if="row.entity.type !== \'boolean\'">{{row.entity[col.field]}}</div>';
+      var cellTemplate = '<input ng-if="row.entity.type === \'boolean\'" type="checkbox" value="{{row.entity[col.field]}}" ng-model="row.entity[col.field]">';
+      cellTemplate += '<div ng-if="row.entity.type !== \'boolean\'">{{row.entity[col.field]}}</div>';
 
       var removeTemplate = '<button ng-if="row.entity.deletable" class="btn btn-danger" ng-click="grid.appScope.removeRow(row.entity)"><i class="glyphicon glyphicon-remove"></i></button>';
 
@@ -105,7 +105,7 @@ angular.module('accessimapEditeurDerApp')
       var interactiveFiltersColumns = [
         { name: 'id', enableCellEdit: false, enableHiding: false, cellClass: cellClassId},
         { name: 'f0',
-          cellTemplate: checkboxTemplate,
+          cellTemplate: cellTemplate,
           menuItems: [
             {
               title: 'Supprimer cette colonne',
@@ -119,7 +119,7 @@ angular.module('accessimapEditeurDerApp')
           enableHiding: false, cellClass: cellClassId
         },
         { name: 'f1',
-          cellTemplate: checkboxTemplate,
+          cellTemplate: cellTemplate,
           menuItems: [
             {
               title: 'Supprimer cette colonne',
@@ -142,8 +142,8 @@ angular.module('accessimapEditeurDerApp')
         enableSorting: false,
         enableRowSelection: true,
         columnDefs: interactiveFiltersColumns,
-        onRegisterApi: function(gridApi){
-          $scope.gridApi = gridApi;
+        onRegisterApi: function(gridApi) {
+          shareSvg.addInteractions(gridApi);
         }
       };
 
@@ -153,7 +153,7 @@ angular.module('accessimapEditeurDerApp')
         var filterPosition = interactiveFiltersColumns.length - 1;
         interactiveFiltersColumns.splice(filterPosition, 0, {
           name: 'f' + $scope.nextFilterNumber,
-          cellTemplate: checkboxTemplate,
+          cellTemplate: cellTemplate,
           menuItems: [
             {
               title: 'Supprimer cette colonne',
@@ -431,9 +431,6 @@ angular.module('accessimapEditeurDerApp')
                   feature.attr(k, v);
                 }
               });
-              feature.append('actions')
-              .append('action')
-              .attr('id', '1');
            });
         }
         if ($scope.mode === 'circle') {
diff --git a/app/scripts/controllers/globalmap.js b/app/scripts/controllers/globalmap.js
index 417524df..03dbfb52 100644
--- a/app/scripts/controllers/globalmap.js
+++ b/app/scripts/controllers/globalmap.js
@@ -18,7 +18,6 @@ angular.module('accessimapEditeurDerApp')
         var reader = new FileReader();
         reader.readAsDataURL(svgFile); //readAsDataURL
         reader.onload = function(e) {
-          console.log(e)
           appendSvg(e.target.result);
         };
       };
diff --git a/app/scripts/services/exportservice.js b/app/scripts/services/exportservice.js
index 2bf7309a..2d4431a5 100644
--- a/app/scripts/services/exportservice.js
+++ b/app/scripts/services/exportservice.js
@@ -9,7 +9,8 @@
  * Service in the accessimapEditeurDerApp.
  */
 angular.module('accessimapEditeurDerApp')
-  .service('exportService', function() {
+  .service('exportService', ['shareSvg',
+  function(shareSvg) {
 
     this.mapExport = function(filename) {
       var zip = new JSZip();
@@ -25,8 +26,87 @@ angular.module('accessimapEditeurDerApp')
         zip.file('legende.svg', (new XMLSerializer()).serializeToString(legendNode));
       }
       zip.file('commentaires.txt', $('#comment').val());
-      var content = zip.generate({type: 'blob'});
-      saveAs(content, filename + '.zip');
+
+
+      shareSvg.getInteractions()
+      .then(function(data) {
+        var columns = [];
+        for (var i = 0; i < data.grid.columns.length; i++) {
+          columns.push({'field': data.grid.columns[i].field});
+        }
+
+        var rows = [];
+        for (var i = 0; i < data.grid.rows.length; i++) {
+          rows.push(data.grid.rows[i].entity);
+        }
+
+        // Remove the first and last columns
+        columns.shift();
+        columns.pop();
+
+        var filterName = rows[0];
+        var filterExpandable = rows[1];
+
+        for (var i = 0; i < columns.length; i++) {
+          var currentField = columns[i].field;
+          columns[i].name = filterName[currentField];
+          columns[i].expandable = filterExpandable[currentField];
+        }
+
+        var interactions = d3.select('#der').append('xml');
+        var config = interactions.append('config');
+        var filters = config.append('filters');
+        filters.selectAll('filter')
+          .data(columns)
+          .enter()
+          .append('filter')
+          .attr('id', function(d) {
+            return d.field;
+          })
+          .attr('name', function(d) {
+            return d.name;
+          })
+          .attr('expandable', function(d) {
+            return d.expandable;
+          });
+
+        // Remove the first two rows
+        rows.shift();
+        rows.shift();
+        var pois = config.append('pois');
+        pois.selectAll('poi')
+          .data(rows)
+          .enter()
+          .append('poi')
+          .attr('id', function(d) {
+            return d.id;
+          })
+          .each(function(d) {
+            var shapeD;
+            d3.select('#der').select('svg').selectAll('path')[0]
+              .forEach(function(shape) {
+                if ('poi-' + d3.select(shape).attr('iid') === d.id) {
+                  shapeD = d3.select(shape).attr('d');
+                }
+              });
+            var poi = d3.select(this);
+            poi.attr('id', d.id);
+            poi.attr('coord', shapeD);
+            var actions = poi.append('actions');
+            // loop through the keys - this assumes no extra data
+            d3.keys(d).forEach(function(key) {
+              if (key !== '$$hashKey' && key !== 'deletable' && key !== 'id') {
+                actions.append('action')
+                .attr('filter', key)
+                .attr('value', d[key]);
+              }
+            });
+          });
+        var interactionsNode = d3.select('#der').select('xml').node();
+        zip.file('interactions.xml', (new XMLSerializer()).serializeToString(interactionsNode));
+        var content = zip.generate({type: 'blob'});
+        saveAs(content, filename + '.zip');
+      });
     };
 
-  });
+  }]);
diff --git a/app/scripts/services/sharesvg.js b/app/scripts/services/sharesvg.js
index 8b8e65c0..36ed5a3a 100644
--- a/app/scripts/services/sharesvg.js
+++ b/app/scripts/services/sharesvg.js
@@ -10,7 +10,8 @@
 angular.module('accessimapEditeurDerApp')
   .service('shareSvg', ['$q', function($q) {
     var map,
-        legend;
+        legend,
+        interactions;
 
     var addMap = function(newMap) {
       var deferred = $q.defer();
@@ -46,10 +47,29 @@ angular.module('accessimapEditeurDerApp')
       return deferred.promise;
     };
 
+    var addInteractions = function(newInteractions) {
+      var deferred = $q.defer();
+      interactions = newInteractions;
+
+      deferred.resolve();
+
+      return deferred.promise;
+    };
+
+    var getInteractions = function() {
+      var deferred = $q.defer();
+
+      deferred.resolve(interactions);
+
+      return deferred.promise;
+    };
+
     return {
       addMap: addMap,
       getMap: getMap,
       addLegend: addLegend,
-      getLegend: getLegend
+      getLegend: getLegend,
+      addInteractions: addInteractions,
+      getInteractions: getInteractions
     };
   }]);