Fixed bugs where searchresult did not update, where searchlist got duplicated when...
[u/erikhf/frm.git] / src / components / search / search.ts
CommitLineData
1a0d2e35 1import {Component,EventEmitter, View, CORE_DIRECTIVES} from 'angular2/angular2';
7f3e9674 2import {Http} from 'angular2/http';
690e4045 3import {LiveSearch} from "./livesearch";
3562820e 4import {Sidebar} from "../sidebar/sidebar";
c489ddac
YF
5import * as Rx from '@reactivex/rxjs/dist/cjs/Rx';
6
7declare var zone: Zone;
b74efa44
EHF
8
9@Component({
10 selector: 'mou-search',
690e4045 11 directives: [CORE_DIRECTIVES, LiveSearch],
1a0d2e35 12 events: ['newsearch'],
21341a61
EHF
13 templateUrl: './components/search/search.html',
14 styleUrls: ['./components/search/search.css']
b74efa44 15})
6e8d36df 16export class Search {
690e4045 17 orgunits: Array<any> = [];
431ff5c5 18 filteredOrgunits: Array<any> = [];
690e4045 19 loading: boolean = false;
c489ddac
YF
20 groups: Array<any> = [];
21 groupSet: Array<any> = [];
37fb701b 22 ownershipSelector: any;
5cc1f6d0
YF
23 typeSelector: any;
24 locationSelector: any;
25 option: any;
26 searchBar: any;
431ff5c5 27 filterset: boolean = false;
3562820e
RM
28 emptySearch: any;
29 slide: any;
431ff5c5 30
7f3e9674
YF
31
32 constructor(public http:Http) {
1a0d2e35 33 this.newsearch = new EventEmitter();
27af2a91 34 this.visible = true;
3562820e 35 this.emptySearch = document.getElementById("divresult");
c489ddac 36 this.getUnitGroupSets();
37fb701b 37 this.ownershipSelector = document.getElementById("ownershipSelector");
5cc1f6d0
YF
38 this.typeSelector = document.getElementById("typeSelector");
39 this.locationSelector = document.getElementById("locationSelector");
40 this.searchBar = document.getElementById("livesearch");
431ff5c5
YF
41 this.orglist = document.getElementById("orglist");
42 this.a = document.getElementById("testunit");
6e8d36df 43 }
75f95f28 44
1a0d2e35 45 getMoreInfo(orgunit) {
0614791e 46 this.orgunits = [];
1a0d2e35
EHF
47 this.newsearch.next(orgunit.id);
48 }
75f95f28 49
1aec1256 50 //pil opp og ned
27af2a91 51
5bc68022
RM
52 toggle() {
53 this.visible = !this.visible;
14fb81ec
YF
54 if(this.visible){
55 this.ownershipSelector.selectedIndex = 0;
56 this.typeSelector.selectedIndex = 0;
57 this.locationSelector.selectedIndex = 0;
58 this.checkOrgunits();
59 }
5bc68022 60 }
75f95f28 61
18709ba8
RM
62 hideDiv(){
63 if(livesearch.value == "")
64 return true;
cb2667b5 65
18709ba8
RM
66 }
67
0614791e 68
3562820e
RM
69 emptyByClick(){
70 return this.emptySearch = document.getElementById("divresult").style.visibility = "hidden";
71 }
72
5cc1f6d0 73
c489ddac
YF
74 getUnitGroupSets(){
75 this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
76 .map(res => res.json())
77 .map(res => res.organisationUnitGroupSets)
78 .subscribe(
79 zone.bind( res =>{
37fb701b
YF
80 this.setOptionHeader(this.ownershipSelector, res[0].name);
81 this.setOptionHeader(this.typeSelector, res[1].name);
82 this.setOptionHeader(this.locationSelector, res[2].name);
83
c489ddac
YF
84 for(var i = 0; i < res.length; i++) {
85 this.http.get(res[i].href)
86 .map(result => result.json())
c489ddac
YF
87 .subscribe(
88 zone.bind(result => {
5cc1f6d0
YF
89 if(result.displayName == "Facility Ownership"){
90 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
37fb701b 91 this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
7f3e9674 92 }
c489ddac 93 }
5cc1f6d0
YF
94 else if(result.displayName == "Facility Type"){
95 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
37fb701b 96 this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
7f3e9674 97 }
c489ddac 98 }
5cc1f6d0
YF
99 else if(result.displayName == "Location Rural/Urban"){
100 for(var j = 0; j < result.organisationUnitGroups.length; j++) {
37fb701b 101 this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
c489ddac
YF
102 }
103 }
104 }));
105 }
106 })
107 )
7f3e9674
YF
108 }
109
37fb701b
YF
110 setOptionHeader(selector, value){
111 this.option = document.createElement("option");
112 this.option.text = "-- " + value + " --";
113 this.option.value = "";
114 selector.appendChild(this.option);
115 }
116
117 setOption(selector, value){
5cc1f6d0
YF
118 this.option = document.createElement("option");
119 this.option.text = value;
120 this.option.value = value;
37fb701b 121 selector.appendChild(this.option);
5cc1f6d0
YF
122 }
123
431ff5c5 124 checkOrgunits(){
14fb81ec
YF
125 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
126 this.filteredOrgunits = [];
127 for(var i = 0; i < this.orgunits.length; i++) {
128 this.filteredOrgunits.push(this.orgunits[i]);
129 }
130 }
131 else if(!this.orgunits.length == false && !this.filterset){
431ff5c5 132 this.setFilter();
431ff5c5
YF
133 }
134 else if(!this.orgunits.length){
135 this.filteredOrgunits = [];
136 if(this.filterset) {
137 this.filterset = false;
138 }
139
140 }
141 return !this.orgunits.length;
142 }
5cc1f6d0 143
5cc1f6d0 144
431ff5c5
YF
145 setFilter(){
146 this.filteredOrgunits = [];
14fb81ec 147 this.filterset = true;
431ff5c5
YF
148 for (var i = 0; i < this.orgunits.length; i++) {
149 this.http.get(this.orgunits[i].href)
150 .map(res => res.json())
151 .subscribe(
152 zone.bind(orgunits => {
153 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
154 this.filteredOrgunits.push(orgunits);
155 }
156 else {
157 var os = false; var ls = false;var ts = false;
14fb81ec 158 for (var j = 0; j < orgunits.organisationUnitGroups.length; j++) {
431ff5c5 159 if (this.ownershipSelector.value != "") {
14fb81ec 160 if (orgunits.organisationUnitGroups[j].name == this.ownershipSelector.value) {
431ff5c5
YF
161 os = true;
162 }
163 }
164 if (this.ownershipSelector.value == "") {
165 os = true;
166 }
167 if (this.typeSelector.value != "") {
14fb81ec 168 if (orgunits.organisationUnitGroups[j].name == this.typeSelector.value) {
431ff5c5
YF
169 ts = true;
170 }
171 }
172 if (this.typeSelector.value == "") {
173 ts = true;
174 }
175 if (this.locationSelector.value != "") {
14fb81ec 176 if (orgunits.organisationUnitGroups[j].name == this.locationSelector.value) {
431ff5c5
YF
177 ls = true;
178 }
179 }
180 if (this.locationSelector.value == "") {
181 ls = true;
182 }
183 if (os == true && ts == true && ls == true) {
184 this.filteredOrgunits.push(orgunits);
185 os = false;
186 ts = false;
187 ls = false;
431ff5c5
YF
188 }
189 }
190 }
191 })
192 )
193 }
5cc1f6d0 194 }
b74efa44 195}
6e8d36df
EHF
196
197