]> git.uio.no Git - u/erikhf/frm.git/blob - src/components/sidebar/sidebar.ts
9ca4bd003853f10f803f0b9a44b6c248c5856d7f
[u/erikhf/frm.git] / src / components / sidebar / sidebar.ts
1 import {Component, NgFor, NgIf, EventEmitter, NgModel, Control, ControlGroup, ControlArray, Validators, FormBuilder, CORE_DIRECTIVES,FORM_DIRECTIVES} from 'angular2/angular2';
2 import {Http, Headers} from 'angular2/http';
3
4 declare var zone: Zone;
5
6 @Component({
7     selector: 'mou-sidebar',
8     directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgModel, NgIf],
9     events: ['tempmarker','updateorg'],
10     templateUrl: './components/sidebar/sidebar.html'
11 })
12
13 export class Sidebar {
14
15
16     http:Http;
17     newObject: boolean;
18     editmode:boolean;
19     active: boolean;
20     coordinatePoint: boolean;
21
22     groupSets: Array<any> = [];
23     groupsDoubleArray: any[][] = [];
24
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("");
40     exitButton: any;
41     featureType: Control = new Control("");
42     coordinates: Control = new Control("");
43     ctrlGroups: Array<Control> = [new Control('')];
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     });
67
68     constructor(http:Http, fb: FormBuilder) {
69         this.http = http;
70         this.editmode = false;
71         this.active = false;
72         this.coordinatePoint = false;
73         this.tempmarker = new EventEmitter();
74         this.updateorg = new EventEmitter();
75         this.exitButton = document.getElementById("slideout")
76
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         });
94
95         this.findOrgUnitSets();
96
97     }
98
99     update(orgunitId) {
100         console.log("Skjer det noe her? ");
101         this.active = true;
102         this.newObject = false;
103         this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
104             .map(res => res.json())
105             .subscribe(res => this.updateValues(res))
106     }
107
108     updateValues(res){
109
110         for(control in this.form.controls){
111             if(this.form.controls[control] instanceof ControlArray){
112                 console.log("nothing to do here");
113             }
114             else if(res[control] !== undefined) {
115                 this.form.controls[control].updateValue(res[control]);
116             }
117             else
118                 this.form.controls[control].updateValue("");
119
120         }
121
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
130         if(res.featureType === "POINT"){
131             this.coordinatePoint = true;
132             let coord = new Object();
133             coord = JSON.parse(res["coordinates"]);
134             this.form.controls.lat.updateValue(coord[1]);
135             this.form.controls.lng.updateValue(coord[0]);
136         }
137         else{
138             this.coordinatePoint = false;
139         }
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");
152     }
153
154
155
156     onSubmit() {
157         this.editmode = false;
158
159         let headers = new Headers();
160         headers.append('Accept', 'application/json');
161
162         headers.append('Content-Type', 'application/json');
163
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
172         $.each(jsonObject.organisationUnitGroups, function(key, value){
173 //            if( value === "" || value === null){
174                 delete jsonObject.organisationUnitGroups[key];
175   //          } else {
176   //              jsonObject.organisationUnitGroups[key].id = value;
177    //         }
178         });
179
180
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         }
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";
194             jsonObject.coordinates="[" + this.form.controls.lng.value + ","+this.form.controls.lat.value+"]";
195             delete jsonObject["lat"];
196             delete jsonObject["lng"];
197             this.http.post(dhisAPI + "/api/organisationUnits/", JSON.stringify(jsonObject), {
198                     headers: headers
199                 })
200                 .map(res => res.json())
201                 .subscribe(res => this.emitNewUpdatedObject(res));
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));
208         }
209
210
211
212
213     }
214
215     emitNewUpdatedObject(obj){
216         console.log(obj);
217         this.updateorg.next(obj.response.lastImported);
218     }
219
220     cancel(){
221         this.editmode = false;
222         this.tempmarker.next(null);
223     }
224
225
226     add(data){
227         this.coordinatePoint = true;
228         this.newObject=true;
229         this.active = true;
230         this.editmode = true;
231
232         for(control in this.form.controls){
233             if(!(this.form.controls[control] instanceof ControlArray))
234                 this.form.controls[control].updateValue("");
235         }
236
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
241     }
242
243     exit(){
244         this.active = false;
245     }
246
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){
256         //delete instance.ctrlGroups[0];
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     }
281 }
282