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