]> git.uio.no Git - u/erikhf/frm.git/blame - src/components/search/search.ts
Cleanup of code in search
[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;
ef90c499 28
431ff5c5 29
7f3e9674
YF
30
31 constructor(public http:Http) {
1a0d2e35 32 this.newsearch = new EventEmitter();
27af2a91 33 this.visible = true;
c489ddac 34 this.getUnitGroupSets();
37fb701b 35 this.ownershipSelector = document.getElementById("ownershipSelector");
5cc1f6d0
YF
36 this.typeSelector = document.getElementById("typeSelector");
37 this.locationSelector = document.getElementById("locationSelector");
38 this.searchBar = document.getElementById("livesearch");
6e8d36df 39 }
75f95f28 40
1a0d2e35 41 getMoreInfo(orgunit) {
0614791e 42 this.orgunits = [];
1a0d2e35 43 this.newsearch.next(orgunit.id);
270ddb67
RM
44 return document.getElementById("myForm").reset();
45
1a0d2e35 46 }
75f95f28 47
1aec1256 48 //pil opp og ned
5bc68022
RM
49 toggle() {
50 this.visible = !this.visible;
27cd7c38
YF
51 if (this.visible) {
52 this.resetSelector();
14fb81ec 53 }
5bc68022 54 }
75f95f28 55
27cd7c38
YF
56 resetSelector(){
57 this.ownershipSelector.selectedIndex = 0;
58 this.typeSelector.selectedIndex = 0;
59 this.locationSelector.selectedIndex = 0;
60 this.checkOrgunits();
61 }
62
63 hideDiv() {
d93bc644 64 if (this.searchBar.value == "")
18709ba8 65 return true;
4b7e7545 66
18709ba8
RM
67 }
68
0614791e 69
3562820e 70 emptyByClick(){
270ddb67
RM
71 this.orgunits = [];
72 return document.getElementById("myForm").reset();
3562820e
RM
73 }
74
27cd7c38 75 getUnitGroupSets() {
c489ddac 76 this.http.get(dhisAPI + "/api/organisationUnitGroupSets")
27cd7c38
YF
77 .map(res => res.json())
78 .map(res => res.organisationUnitGroupSets)
79 .subscribe(
80 zone.bind(res => {
81 this.setOptionHeader(this.ownershipSelector, res[0].name);
82 this.setOptionHeader(this.typeSelector, res[1].name);
83 this.setOptionHeader(this.locationSelector, res[2].name);
84
85 for (var i = 0; i < res.length; i++) {
86 this.http.get(res[i].href)
87 .map(result => result.json())
88 .subscribe(
89 zone.bind(result => {
90 if (result.displayName == "Facility Ownership") {
91 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
92 this.setOption(this.ownershipSelector, result.organisationUnitGroups[j].name);
93 }
94 }
95 else if (result.displayName == "Facility Type") {
96 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
97 this.setOption(this.typeSelector, result.organisationUnitGroups[j].name);
98 }
99 }
100 else if (result.displayName == "Location Rural/Urban") {
101 for (var j = 0; j < result.organisationUnitGroups.length; j++) {
102 this.setOption(this.locationSelector, result.organisationUnitGroups[j].name);
103 }
104 }
1b806970
YF
105 })
106 );
27cd7c38
YF
107 }
108 })
109 )
7f3e9674
YF
110 }
111
27cd7c38 112 setOptionHeader(selector, value) {
37fb701b 113 this.option = document.createElement("option");
270ddb67 114 this.option.text = "All";
37fb701b
YF
115 this.option.value = "";
116 selector.appendChild(this.option);
117 }
118
27cd7c38 119 setOption(selector, value) {
5cc1f6d0
YF
120 this.option = document.createElement("option");
121 this.option.text = value;
122 this.option.value = value;
37fb701b 123 selector.appendChild(this.option);
5cc1f6d0
YF
124 }
125
27cd7c38 126 checkOrgunits() {
14fb81ec
YF
127 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
128 this.filteredOrgunits = [];
27cd7c38 129 for (var i = 0; i < this.orgunits.length; i++) {
14fb81ec
YF
130 this.filteredOrgunits.push(this.orgunits[i]);
131 }
132 }
27cd7c38 133 else if (!this.orgunits.length == false && !this.filterset) {
431ff5c5 134 this.setFilter();
431ff5c5 135 }
27cd7c38 136 else if (!this.orgunits.length) {
431ff5c5 137 this.filteredOrgunits = [];
27cd7c38 138 if (this.filterset) {
431ff5c5
YF
139 this.filterset = false;
140 }
431ff5c5 141 }
d93bc644 142 if(this.filteredOrgunits.length == 0){
27cd7c38 143 return false;
d93bc644 144 }
d93bc644
YF
145 else{
146 return !this.orgunits.length;
147 }
431ff5c5 148 }
5cc1f6d0 149
5cc1f6d0 150
27cd7c38 151 setFilter() {
431ff5c5 152 this.filteredOrgunits = [];
14fb81ec 153 this.filterset = true;
431ff5c5
YF
154 for (var i = 0; i < this.orgunits.length; i++) {
155 this.http.get(this.orgunits[i].href)
156 .map(res => res.json())
157 .subscribe(
158 zone.bind(orgunits => {
159 if (this.ownershipSelector.value == "" && this.typeSelector.value == "" && this.locationSelector.value == "") {
160 this.filteredOrgunits.push(orgunits);
161 }
162 else {
27cd7c38
YF
163 var os = false;
164 var ls = false;
165 var ts = false;
14fb81ec 166 for (var j = 0; j < orgunits.organisationUnitGroups.length; j++) {
431ff5c5 167 if (this.ownershipSelector.value != "") {
14fb81ec 168 if (orgunits.organisationUnitGroups[j].name == this.ownershipSelector.value) {
431ff5c5
YF
169 os = true;
170 }
171 }
172 if (this.ownershipSelector.value == "") {
173 os = true;
174 }
175 if (this.typeSelector.value != "") {
14fb81ec 176 if (orgunits.organisationUnitGroups[j].name == this.typeSelector.value) {
431ff5c5
YF
177 ts = true;
178 }
179 }
180 if (this.typeSelector.value == "") {
181 ts = true;
182 }
183 if (this.locationSelector.value != "") {
14fb81ec 184 if (orgunits.organisationUnitGroups[j].name == this.locationSelector.value) {
431ff5c5
YF
185 ls = true;
186 }
187 }
188 if (this.locationSelector.value == "") {
189 ls = true;
190 }
191 if (os == true && ts == true && ls == true) {
192 this.filteredOrgunits.push(orgunits);
193 os = false;
194 ts = false;
195 ls = false;
431ff5c5
YF
196 }
197 }
198 }
199 })
200 )
201 }
5cc1f6d0 202 }
b74efa44 203}
6e8d36df
EHF
204
205