Added more comments to setFilter()
[u/erikhf/frm.git] / src / components / sidebar / sidebar.ts
CommitLineData
fa052229 1import {Component, NgFor, NgIf, EventEmitter, NgModel, Control, ControlGroup, ControlArray, Validators, FormBuilder, CORE_DIRECTIVES,FORM_DIRECTIVES} from 'angular2/angular2';
9bdb78d0 2import {Http, Headers} from 'angular2/http';
ebe03648 3
ebaf2f58 4declare var zone: Zone;
ebe03648
EHF
5
6@Component({
7 selector: 'mou-sidebar',
9bdb78d0 8 directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgModel, NgIf],
1e22a4ed 9 events: ['tempmarker','updateorg'],
87bd5039 10 templateUrl: './components/sidebar/sidebar.html'
ebe03648 11})
8c91a910 12
ebe03648 13export class Sidebar {
fa052229 14
5539d095 15
9bdb78d0 16 http:Http;
8c91a910 17 newObject: boolean;
9bdb78d0 18 editmode:boolean;
ebaf2f58 19 active: boolean;
59ab0e7a 20 coordinatePoint: boolean;
8c91a910 21
5539d095
EHF
22 groupSets: Array<any> = [];
23 groupsDoubleArray: any[][] = [];
24
8c91a910
EHF
25 id: Control = new Control("");
26 name: Control = new Control("", Validators.required);
27 shortName: Control = new Control("",Validators.required);
28 description: Control = new Control("");
29 code: Control = new Control("");
30 openingDate: Control = new Control("",Validators.required);
31 closedDate: Control = new Control("");
32 url: Control = new Control("");
33 lat: Control = new Control("");
34 lng: Control = new Control("");
35 parent: Control = new Control("");
36 contactPerson: Control = new Control("");
37 address: Control = new Control("");
38 email: Control = new Control("");
39 phoneNumber: Control = new Control("");
1b806970 40 exitButton: any;
6a4e2ca7 41 featureType: Control = new Control("");
f03b7890 42 coordinates: Control = new Control("");
cad70644 43 ctrlGroups: Array<Control> = [new Control('')];
5539d095
EHF
44 groupsArray: ControlArray = new ControlArray(this.ctrlGroups);
45
46
47 form: ControlGroup = new ControlGroup({
48 organisationUnitGroups: this.groupsArray,
49 id: this.id,
50 name: this.name,
51 shortName: this.shortName,
52 description: this.description,
53 code: this.code,
54 openingDate: this.openingDate,
55 closedDate: this.closedDate,
56 url: this.url,
57 lat: this.lat,
58 lng: this.lng,
59 parent: this.parent,
60 contactPerson: this.contactPerson,
61 address: this.address,
62 email: this.email,
63 phoneNumber: this.phoneNumber,
64 featureType: this.featureType,
65 coordinates: this.coordinates
66 });
8c91a910
EHF
67
68 constructor(http:Http, fb: FormBuilder) {
ebe03648 69 this.http = http;
9ce2e2a9 70 this.editmode = false;
ebaf2f58 71 this.active = false;
59ab0e7a 72 this.coordinatePoint = false;
fa052229 73 this.tempmarker = new EventEmitter();
1e22a4ed 74 this.updateorg = new EventEmitter();
1b806970 75 this.exitButton = document.getElementById("slideout")
59ab0e7a 76
fa052229
EHF
77 let instance = this;
78 this.lat.valueChanges.observer({
79 next: (value) => {
80 if(instance.lng.value && value) {
81 let pos = {lat: value, lng: instance.lng.value};
82 this.tempmarker.next(pos);
83 }
84 }
85 });
86 this.lng.valueChanges.observer({
87 next: (value) => {
88 if(instance.lat.value && value) {
89 let pos = {lat: instance.lat.value, lng: value};
90 this.tempmarker.next(pos);
91 }
92 }
93 });
5539d095
EHF
94
95 this.findOrgUnitSets();
96
c1cf798d
EHF
97 }
98
9bdb78d0 99 update(orgunitId) {
f800869b 100 console.log("Skjer det noe her? ");
ebaf2f58 101 this.active = true;
8c91a910 102 this.newObject = false;
c1cf798d
EHF
103 this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
104 .map(res => res.json())
8c91a910 105 .subscribe(res => this.updateValues(res))
ebe03648 106 }
9ce2e2a9 107
8c91a910 108 updateValues(res){
59ab0e7a 109
8c91a910 110 for(control in this.form.controls){
5539d095
EHF
111 if(this.form.controls[control] instanceof ControlArray){
112 console.log("nothing to do here");
113 }
114 else if(res[control] !== undefined) {
8c91a910
EHF
115 this.form.controls[control].updateValue(res[control]);
116 }
117 else
118 this.form.controls[control].updateValue("");
5539d095 119
8c91a910 120 }
59ab0e7a 121
531b0170
EHF
122 // Date fix:
123 if(res["openingDate"]){
124 this.form.controls["openingDate"].updateValue((new Date(res["openingDate"].substring(0,10))).toISOString().substring(0,10));
125 }
126 if(res["closedDate"]){
127 this.form.controls["closedDate"].updateValue((new Date(res["closedDate"].substring(0,10))).toISOString().substring(0,10));
128 }
129
59ab0e7a
EHF
130 if(res.featureType === "POINT"){
131 this.coordinatePoint = true;
132 let coord = new Object();
133 coord = JSON.parse(res["coordinates"]);
1e22a4ed
EHF
134 this.form.controls.lat.updateValue(coord[1]);
135 this.form.controls.lng.updateValue(coord[0]);
59ab0e7a
EHF
136 }
137 else{
138 this.coordinatePoint = false;
139 }
5539d095
EHF
140
141 for(var i = 0; i < this.groupsDoubleArray.length; i++){
142 for(var j = 0; j < this.groupsDoubleArray[i].length; j++){
143 for( group in res.organisationUnitGroups){
144 if( res.organisationUnitGroups[group].id == this.groupsDoubleArray[i][j].id ){
145 this.form.controls.organisationUnitGroups.controls[i].updateValue(this.groupsDoubleArray[i][j].name);
146 }
147 }
148 }
149 }
150
151 console.log("faenskap");
8c91a910 152 }
59ab0e7a
EHF
153
154
155
9bdb78d0
EHF
156 onSubmit() {
157 this.editmode = false;
158
159 let headers = new Headers();
160 headers.append('Accept', 'application/json');
59ab0e7a 161
9bdb78d0
EHF
162 headers.append('Content-Type', 'application/json');
163
8c91a910
EHF
164 let jsonObject = this.form.value;
165
166 $.each(jsonObject, function(key, value){
167 if (value === "" || value === null){
168 delete jsonObject[key];
169 }
170 });
171
5539d095 172 $.each(jsonObject.organisationUnitGroups, function(key, value){
1e22a4ed 173// if( value === "" || value === null){
5539d095 174 delete jsonObject.organisationUnitGroups[key];
1e22a4ed
EHF
175 // } else {
176 // jsonObject.organisationUnitGroups[key].id = value;
177 // }
5539d095
EHF
178 });
179
180
531b0170
EHF
181 jsonObject.openingDate = (new Date(this.form.value.openingDate)).toISOString();
182
183 if(this.form.value.closedDate){
184 jsonObject.closedDate = (new Date(this.form.value.closedDate)).toISOString();
185 }
8c91a910
EHF
186
187
188 console.log(this.form.value);
189
190 if (this.newObject) {
191 jsonObject.parent = {};
192 jsonObject.parent.id = this.form.controls.parent.value;
193 jsonObject.featureType="POINT";
1e22a4ed 194 jsonObject.coordinates="[" + this.form.controls.lng.value + ","+this.form.controls.lat.value+"]";
8c91a910
EHF
195 delete jsonObject["lat"];
196 delete jsonObject["lng"];
197 this.http.post(dhisAPI + "/api/organisationUnits/", JSON.stringify(jsonObject), {
9bdb78d0
EHF
198 headers: headers
199 })
200 .map(res => res.json())
1e22a4ed 201 .subscribe(res => this.emitNewUpdatedObject(res));
59ab0e7a
EHF
202 }else {
203 this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject), {
204 headers: headers
205 })
206 .map(res => res.json())
207 .subscribe(res => console.log(res));
9bdb78d0
EHF
208 }
209
1e22a4ed
EHF
210
211
212
213 }
214
215 emitNewUpdatedObject(obj){
216 console.log(obj);
217 this.updateorg.next(obj.response.lastImported);
9ce2e2a9 218 }
57c243b6
EHF
219
220 cancel(){
221 this.editmode = false;
1e22a4ed 222 this.tempmarker.next(null);
57c243b6 223 }
ebaf2f58 224
fa052229 225
8c91a910 226 add(data){
59ab0e7a 227 this.coordinatePoint = true;
8c91a910 228 this.newObject=true;
59ab0e7a 229 this.active = true;
ebaf2f58 230 this.editmode = true;
8c91a910 231
fa052229 232 for(control in this.form.controls){
5539d095
EHF
233 if(!(this.form.controls[control] instanceof ControlArray))
234 this.form.controls[control].updateValue("");
fa052229
EHF
235 }
236
8c91a910
EHF
237 this.form.controls.lat.updateValue(data.location.lat);
238 this.form.controls.lng.updateValue(data.location.lng);
239 this.form.controls.parent.updateValue(data.parent);
240
ebaf2f58 241 }
5539d095 242
1b806970
YF
243 exit(){
244 this.active = false;
245 }
3012b771 246
5539d095
EHF
247 findOrgUnitSets(){
248 let instance = this;
249 this.http.get(dhisAPI + "/api/organisationUnitGroupSets?paging=false")
250 .map(res => res.json())
251 .map(res => res.organisationUnitGroupSets)
252 .subscribe(res => this.addOrgUnitSets(instance, res))
253 }
254
255 addOrgUnitSets(instance, res){
cad70644 256 //delete instance.ctrlGroups[0];
5539d095
EHF
257 for( group in res){
258 console.log(instance.form.controls);
259 instance.groupsArray.push(new Control(''));
260 instance.groupSets.push(res[group]);
261
262 this.http.get(dhisAPI + "/api/organisationUnitGroupSets/" + res[group].id)
263 .map(res => res.json())
264 .map(res => res.organisationUnitGroups)
265 .subscribe(res => this.addOrgUnitGroup(instance, res))
266
267 // instance.form.push(new Control(""));
268 }
269 console.log(instance.groupSets);
270 }
271
272 addOrgUnitGroup(instance, res){
273 let ar: Array<any> = [];
274 for( group in res){
275 ar.push(res[group]);
276 }
277 instance.groupsDoubleArray.push(ar);
278
279 console.log(instance.groupsDoubleArray);
280 }
ebe03648
EHF
281}
282