From faf48e47831c281c876d51f4b3c7fa8c3960a709 Mon Sep 17 00:00:00 2001
From: Matthew Schranz <mschranz@oicr.on.ca>
Date: Fri, 13 Mar 2015 13:32:48 -0400
Subject: [PATCH] fix(login): Filter terms for projects.

- Filters terms on any project filter in UI to show
  only appropriate projects.
- Adds My Projects column to Search.Participants.
- Fixes errors from tableicious for compile step when
  missing data.

Closes #542
---
 .../components/facets/facets.controllers.ts   | 15 ++++++++++--
 .../tables/tableicious.cell.directive.ts      | 20 ++++++++++++----
 app/scripts/components/user/user.services.ts  | 24 ++++++++++++++++++-
 .../search/search.participants.table.model.ts | 21 +++++++++++++++-
 4 files changed, 71 insertions(+), 9 deletions(-)

diff --git a/app/scripts/components/facets/facets.controllers.ts b/app/scripts/components/facets/facets.controllers.ts
index 0c6d53671..403e8a587 100644
--- a/app/scripts/components/facets/facets.controllers.ts
+++ b/app/scripts/components/facets/facets.controllers.ts
@@ -30,7 +30,8 @@ module ngApp.components.facets.controllers {
     inactives: string[] = [];
 
     /* @ngInject */
-    constructor(private $scope: IFacetScope, private FacetService: IFacetService) {
+    constructor(private $scope: IFacetScope, private FacetService: IFacetService,
+                private UserService: IUserService) {
       this.collapsed = !!$scope.collapsed;
       this.expanded = !!$scope.expanded;
       this.displayCount = this.originalDisplayCount = $scope.displayCount || 5;
@@ -58,6 +59,16 @@ module ngApp.components.facets.controllers {
     }
 
     refresh(terms) {
+      var projectCodeKeys = [
+        "project_id",
+        "participants.project.project_id",
+        "project.project_id"
+      ];
+
+      if (projectCodeKeys.indexOf(this.name) !== -1) {
+        terms = this.UserService.setUserProjectsTerms(terms);
+      }
+
       this.terms = terms;
       this.actives = this.FacetService.getActives(this.name, terms);
       this.inactives = _.difference(terms, this.actives);
@@ -199,7 +210,7 @@ module ngApp.components.facets.controllers {
 
   }
 
-  angular.module("facets.controllers", ["facets.services"])
+  angular.module("facets.controllers", ["facets.services", "user.services"])
       .controller("currentFiltersCtrl", CurrentFiltersController)
       .controller("freeTextCtrl", FreeTextController)
       .controller("termsCtrl", TermsController);
diff --git a/app/scripts/components/tables/tableicious.cell.directive.ts b/app/scripts/components/tables/tableicious.cell.directive.ts
index d17244158..55f1f8cfa 100644
--- a/app/scripts/components/tables/tableicious.cell.directive.ts
+++ b/app/scripts/components/tables/tableicious.cell.directive.ts
@@ -8,13 +8,23 @@ module ngApp.components.tables.directives.tableicious {
                     if ($scope.heading.compile) {
                         $element.empty();
                         $scope.row = $scope.$parent.datum;
+
+                        var files = _.find($scope.row, (item) => {
+                            return item.id === "files";
+                        });
+
                         var htm;
-                        try {
-                            htm = $scope.heading.compile($scope);
-                        } catch (e) {
-                            htm = '<span>?</span>';
-                            $log.error(e);
+                        if (!files) {
+                            htm = "<span>--</span>";
+                        } else {
+                            try {
+                                htm = $scope.heading.compile($scope);
+                            } catch (e) {
+                                htm = '<span>?</span>';
+                                $log.error(e);
+                            }
                         }
+
                         var compiled = $compile(htm)($scope);
                         $element.append(compiled);
                     }
diff --git a/app/scripts/components/user/user.services.ts b/app/scripts/components/user/user.services.ts
index cda9ac1b3..6a6c2e82f 100644
--- a/app/scripts/components/user/user.services.ts
+++ b/app/scripts/components/user/user.services.ts
@@ -55,11 +55,33 @@ module ngApp.components.user.services {
     }
 
     isUserProject(file: IFile): boolean {
+      if (!this.currentUser) {
+        return false;
+      }
+
       var projectIds = _.unique(_.map(file.participants, (participant) => {
         return participant.project.project_id;
       }));
       return _.some(projectIds, (id) => {
-        return this.currentUser.projects.gdc_ids.indexOf(id);
+        return this.currentUser.projects.gdc_ids.indexOf(id) !== -1;
+      });
+    }
+
+    setUserProjectsTerms(terms) {
+      if (!this.currentUser || !this.currentUser.isFiltered) {
+        return terms;
+      }
+
+      return _.filter(terms, (term) => {
+        return this.isUserProject({
+          participants: [
+            {
+              project: {
+                project_id: term.key
+              }
+            }
+          ]
+        });
       });
     }
     
diff --git a/app/scripts/search/search.participants.table.model.ts b/app/scripts/search/search.participants.table.model.ts
index 524d7af05..890f95fad 100644
--- a/app/scripts/search/search.participants.table.model.ts
+++ b/app/scripts/search/search.participants.table.model.ts
@@ -55,7 +55,26 @@ module ngApp.search.models {
                 return htm;
 
             }
-        },{
+        }, {
+            displayName: "My Projects",
+            id: "my_projects",
+            enabled: function (scope) {
+              return scope.UserService.currentUser;
+            },
+            icon: function (field, row, scope) {
+              var project = _.find(row, function (elem) {
+                return elem.id === 'project'
+              }).val;
+              var UserService: IUserService = scope.UserService;
+              return UserService.isUserProject({
+                participants: [
+                    {
+                        project: project
+                    }
+                ]
+              }) ? 'check' : 'close';
+            }
+        }, {
             displayName: "Participant ID",
             id: "participant_id",
             enabled: true,