Added a filter method in livesearch
authorYrjan A. F. Fraschetti <yrjanaff@1x-193-157-198-230.uio.no>
Thu, 19 Nov 2015 14:47:10 +0000 (15:47 +0100)
committerYrjan A. F. Fraschetti <yrjanaff@1x-193-157-198-230.uio.no>
Thu, 19 Nov 2015 14:47:10 +0000 (15:47 +0100)
src/components/filter/filter.html
src/components/filter/filter.ts
src/components/map/map.ts
src/components/search/livesearch.ts
src/components/search/search.ts

index 2543c73..4588c8a 100644 (file)
@@ -1,9 +1,8 @@
-<div style="width: 500px; height:50px; background: cadetblue; ">
+<div style="width: 500px; height:50px; background: yellow; ">
 FILTER!!
-<!--
+
     <div *ng-for="#orgUnit of result.organisationUnits">
         {{orgUnit.name}}
     </div>
--->
 
 </div>
\ No newline at end of file
index a3a63b7..227e0d7 100644 (file)
@@ -1,9 +1,9 @@
-import {Component, View, CORE_DIRECTIVES} from 'angular2/angular2';
+import {Component, View, CORE_DIRECTIVES, NgFor} from 'angular2/angular2';
 import {Headers, Http} from 'angular2/http';
 
 @Component({
     selector: 'mou-filter',
-    directives: [CORE_DIRECTIVES],
+    directives: [CORE_DIRECTIVES, NgFor],
     templateUrl: './components/filter/filter.html'
 })
 
@@ -12,23 +12,26 @@ export class Filter {
 
     // Example HTTP request
 
-    /*
+
     constructor(http: Http) {
         var authHeader = new Headers();
         authHeader.append('Authorization', 'Basic YWRtaW46ZGlzdHJpY3Q=');
         this.result = {organisationUnits:[]};
-        http.get(dhisAPI+'/api/organisationUnits?paging=false', {headers: authHeader})
+        http.get('http://mydhis.com:8082/api/organisationUnits?paging=false', {headers: authHeader})
         //http.get('orgunit.json')
             .map(res => res.json()).subscribe(
             res => this.result = res,
             error => this.logError(error)
         );
+
+    }
+    
+    filterUnits(){
+        console.log(this.result);
     }
 
 
-    logError(error){
+    logError(errorm test){
         console.error(error);
     }
-
-    */
 }
index b926e92..8bed45f 100644 (file)
@@ -1,4 +1,5 @@
-import {Component, CORE_DIRECTIVES} from 'angular2/angular2';
+import {Component, CORE_DIRECTIVES,} from 'angular2/angular2';
+import {Headers, Http} from 'angular2/http';
 
 
 @Component({
@@ -9,13 +10,90 @@ import {Component, CORE_DIRECTIVES} from 'angular2/angular2';
 
 
 export class Map {
-    constructor(){
+    result:Object;
+    map:Object;
+    pos:Object;
+    marker:Object;
+
+    constructor(http:Http) {
         this.initMap();
+
+        var authHeader = new Headers();
+        authHeader.append('Authorization', 'Basic YWRtaW46ZGlzdHJpY3Q=');
+        this.result = {organisationUnits: []};
+        // http.get(dhisAPI+'/api/organisationUnits?paging=false', {headers: authHeader})
+        http.get('http://mydhis.com:8082/api/organisationUnits?paging=false', {headers: authHeader})
+            .map(res => res.json()).subscribe(
+            res => this.result = res,
+            error => this.logError(error)
+        );
     }
 
-    initMap(){
-        let map = new google.maps.Map(document.getElementById("map"),
-            {center:{lat:59,lng:11},zoom:12});
+
+    initMap() {
+
+        if (navigator.geolocation) {
+            navigator.geolocation.getCurrentPosition(function (position) {
+                    this.pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
+                    this.map = new google.maps.Map(document.getElementById("map"),
+                        {center: this.pos, zoom: 12});
+                    this.marker = new google.maps.Marker({
+                        position: this.pos,
+                        map: this.map,
+                        title: 'Me'
+                    });
+
+                    let infowindow = new google.maps.InfoWindow({
+                        content: "This is You"
+                    });
+                    this.marker.addListener('click', function () {
+                        infowindow.open(this.map, this.marker);
+                    });
+                   // this.map.addListener('click', this.addMarker(position.coords.latitude, position.coords.longitude));
+
+                }, function () {
+                    //handleNoGeolocation(true);
+                }
+            );
+        } else {
+            alert("You do not support geolocation");
+        }
+
+
+    }
+
+
+    //Other map functions
+   // addMarker(lat, lng) {
+
+     //   let marker = new google.maps.Marker({
+       //     position: {lat, lng},
+         //   map: this.map,
+        //    title: 'Me'
+        //});
+    //}
+
+    logError(error) {
+        console.error(error);
+
     }
 
 }
+
+
+/* showOnMap(){
+ var bermudaTriangle = new google.maps.Polygon({
+ paths: triangleCoords,
+ strokeColor: '#FF0000',
+ strokeOpacity: 0.8,
+ strokeWeight: 2,
+ fillColor: '#FF0000',
+ fillOpacity: 0.35
+ });
+ bermudaTriangle.setMap(this.map);
+
+ }*/
+
+
+
+
index 0158b24..2d64f62 100644 (file)
@@ -1,4 +1,5 @@
-import {Directive, View, EventEmitter, ElementRef} from 'angular2/angular2';
+import {CORE_DIRECTIVES, Directive, View, EventEmitter, ElementRef} from 'angular2/angular2';
+import {Http} from 'angular2/http';
 
 // RxJs
 import * as Rx from '@reactivex/rxjs/dist/cjs/Rx';
@@ -10,17 +11,17 @@ declare var zone: Zone;
 @Directive({
     selector: 'input[type=text][mou-live-search]',
     outputs: ['results', 'loading'],
-    providers: [SearchService]
+    providers: [CORE_DIRECTIVES, SearchService]
 })
 export class LiveSearch {
-    results: EventEmitter = new EventEmitter();
-    loading: EventEmitter = new EventEmitter();
+    results:EventEmitter = new EventEmitter();
+    loading:EventEmitter = new EventEmitter();
 
-    constructor( private el: ElementRef, public search: SearchService){
+    constructor(private el:ElementRef, public http:Http, public search:SearchService) {
 
     }
 
-    onInit(){
+    onInit() {
         console.log("starting");
         (<any>Rx).Observable.fromEvent(this.el.nativeElement, 'keyup')
             .map(e => e.target.value)
@@ -31,8 +32,9 @@ export class LiveSearch {
             .flatMap(query => this.search.search(query))
             .do(zone.bind(() => this.loading.next(false)))
             .subscribe(
-                zone.bind( orgunits => {
-                    this.results.next(orgunits);
+                zone.bind(orgunits => {
+                    //this.filterUnits(orgunits)
+                    this.results.next(this.filterUnits(orgunits));
                 }),
                 zone.bind(err => {
                     console.log(err);
@@ -43,5 +45,24 @@ export class LiveSearch {
                 }
             )
     }
+
+    filterUnits(orgunits) {
+        var filteredOrgunits: Array<any> = [];
+        for (var i = 0; i < orgunits.length; i++) {
+            this.http.get(orgunits[i].href)
+                .map(res => res.json())
+                .subscribe(
+                    zone.bind(orgunits => {
+                        for (var group in orgunits.organisationUnitGroups){
+                            if(orgunits.organisationUnitGroups[group].name == "Rural"){
+                                filteredOrgunits.push(orgunits);
+                                console.log(filteredOrgunits);
+                            }
+                        }
+                    })
+                )
+        }
+        return filteredOrgunits;
+    }
 }
 
index d9d90ae..0059a92 100644 (file)
@@ -13,6 +13,7 @@ export class Search {
 
     getMoreInfo(orgunit){
         console.log(orgunit.id);
+
     }