Added live filtering of search
authorYrjan A. F. Fraschetti <yrjanaff@1x-193-157-214-191.uio.no>
Thu, 3 Dec 2015 17:57:43 +0000 (18:57 +0100)
committerYrjan A. F. Fraschetti <yrjanaff@1x-193-157-214-191.uio.no>
Thu, 3 Dec 2015 17:57:43 +0000 (18:57 +0100)
src/components/search/livesearch.ts
src/components/search/search.html
src/components/search/search.ts

index 2567802..60c364f 100644 (file)
@@ -35,7 +35,7 @@ export class LiveSearch {
             .subscribe(
                 zone.bind(orgunits => {
                     //this.filterUnits(orgunits)
-                    this.results.next(this.filterUnits(orgunits));
+                    this.results.next(orgunits/*this.filterUnits(orgunits)*/);
                 }),
                 zone.bind(err => {
                     console.log(err);
@@ -97,7 +97,6 @@ export class LiveSearch {
                     })
                 )
         }
-        console.log(filteredOrgunits);
         return filteredOrgunits;
     }
 }
index 242625d..eba0856 100644 (file)
          src="https://www.brown.edu/sites/default/themes/pawtuxet/img/loader-larger.gif">
 
 
-    <div  class="divresult" id="divresult" [hidden]="!orgunits.length">
-        <ul class="dropdown-menu result"  >
-            <li *ng-for="#orgunit of orgunits" (click)="getMoreInfo(orgunit)">
+
+    <div class="divresult" id="divresult" [hidden]="checkOrgunits()" >
+        <ul class="dropdown-menu result">
+            <li *ng-for="#orgunit of filteredOrgunits" id="orglist" (click)="getMoreInfo(orgunit)" >
                 <a href="#">{{orgunit.name}}<br/><span>{{orgunit.lastUpdated}}</span></a>
             </li>
         </ul>
 
 
 
+<!--
+<a var-unit id="testunit" (click)="getMoreInfo(unit.id)" href="#"></a>
+*ng-for="#orgunit of filteredOrgunits"
+<a href="#">{{orgunit.name}}<br/><span>{{orgunit.lastUpdated}}</span></a>
+(click)="getMoreInfo(orgunit)"
+-->
+
 </div>
 
 <button type="button" class="knapp" data-toggle="collapse" data-target="#dropdown-menu">
index 10a50b7..a50872b 100644 (file)
@@ -14,15 +14,18 @@ declare var zone: Zone;
 })
 export class Search {
     orgunits: Array<any> = [];
+    filteredOrgunits: Array<any> = [];
     loading: boolean = false;
     groups: Array<any> = [];
     groupSet: Array<any> = [];
-    counter: number = 0;
     ownershipSelector: any;
     typeSelector: any;
     locationSelector: any;
     option: any;
     searchBar: any;
+    filterset: boolean = false;
+
+
 
     constructor(public http:Http) {
         this.newsearch = new EventEmitter();
@@ -32,10 +35,12 @@ export class Search {
         this.typeSelector = document.getElementById("typeSelector");
         this.locationSelector = document.getElementById("locationSelector");
         this.searchBar = document.getElementById("livesearch");
+        this.orglist = document.getElementById("orglist");
+        this.a = document.getElementById("testunit");
     }
 
     getMoreInfo(orgunit) {
-        console.log("yolo");
+        console.log();
         this.newsearch.next(orgunit.id);
     }
 
@@ -97,20 +102,72 @@ export class Search {
         selector.appendChild(this.option);
     }
 
-    setFilter(){
-        console.log("Dette er setFilter");
-        console.log(this.ownershipSelector.value);
-        var text = livesearch.value;
+    checkOrgunits(){
+        if(!this.orgunits.length == false && !this.filterset){
+            this.setFilter();
+            this.filterset = true;
+        }
+        else if(!this.orgunits.length){
+            this.filteredOrgunits = [];
+            if(this.filterset) {
+                this.filterset = false;
+            }
+
+        }
+        return !this.orgunits.length;
+    }
 
-        //livesearch.value = "";
-        //console.log(this.searchBar.key);
 
-        /*for(var i = 0; i < text.length; i++){
-            livesearch.value += text.charAt(i);
-        }*/
-        //this.searchBar.createEvent('keyup');
+    setFilter(){
+        this.filteredOrgunits = [];
+
+        for (var i = 0; i < this.orgunits.length; i++) {
+            this.http.get(this.orgunits[i].href)
+                .map(res => res.json())
+                .subscribe(
+                    zone.bind(orgunits => {
+                        if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
+                            this.filteredOrgunits.push(orgunits);
+                        }
+                        else {
+                            var os = false; var ls = false;var ts = false;
+                            for (var group in orgunits.organisationUnitGroups) {
+                                if (this.ownershipSelector.value != "") {
+                                    if (orgunits.organisationUnitGroups[group].name == this.ownershipSelector.value) {
+                                        os = true;
+                                    }
+                                }
+                                if (this.ownershipSelector.value == "") {
+                                    os = true;
+                                }
+                                if (this.typeSelector.value != "") {
+                                    if (orgunits.organisationUnitGroups[group].name == this.typeSelector.value) {
+                                        ts = true;
+                                    }
+                                }
+                                if (this.typeSelector.value == "") {
+                                    ts = true;
+                                }
+                                if (this.locationSelector.value != "") {
+                                    if (orgunits.organisationUnitGroups[group].name == this.locationSelector.value) {
+                                        ls = true;
+                                    }
+                                }
+                                if (this.locationSelector.value == "") {
+                                    ls = true;
+                                }
+                                if (os == true && ts == true && ls == true) {
+                                    this.filteredOrgunits.push(orgunits);
+                                    os = false;
+                                    ts = false;
+                                    ls = false;
 
-        this.searchBar.focus(true);
+                                }
+                            }
+                        }
+                    })
+                )
+        }
     }
 }