[bugfix] If "featureType" is not present in PUT request, it will be set to "NONE...
[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],
fa052229 9 events: ['tempmarker'],
9bdb78d0
EHF
10 templateUrl: './components/sidebar/sidebar.html',
11 styles: [`
8c91a910 12 .ng-valid.ng-dirty {
9bdb78d0
EHF
13 border-left: 5px solid #42A948; /* green */
14 }
15 .ng-invalid {
16 border-left: 5px solid #a94442; /* red */
17 }
8c91a910
EHF
18 .form-background {
19 padding: 10px;
20 background: white;
f800869b
EHF
21 width: 100%;
22 float: right;
23 height: 100vh;
24 z-index: 5;
8c91a910 25 }
9bdb78d0 26 `]
ebe03648 27})
8c91a910 28
ebe03648 29export class Sidebar {
fa052229 30
8c91a910 31 form: ControlGroup;
9bdb78d0 32 http:Http;
8c91a910 33 newObject: boolean;
9bdb78d0 34 editmode:boolean;
ebaf2f58 35 active: boolean;
59ab0e7a 36 coordinatePoint: boolean;
8c91a910
EHF
37
38 id: Control = new Control("");
39 name: Control = new Control("", Validators.required);
40 shortName: Control = new Control("",Validators.required);
41 description: Control = new Control("");
42 code: Control = new Control("");
43 openingDate: Control = new Control("",Validators.required);
44 closedDate: Control = new Control("");
45 url: Control = new Control("");
46 lat: Control = new Control("");
47 lng: Control = new Control("");
48 parent: Control = new Control("");
49 contactPerson: Control = new Control("");
50 address: Control = new Control("");
51 email: Control = new Control("");
52 phoneNumber: Control = new Control("");
6a4e2ca7 53 featureType: Control = new Control("");
8c91a910
EHF
54
55
56 constructor(http:Http, fb: FormBuilder) {
ebe03648 57 this.http = http;
9ce2e2a9 58 this.editmode = false;
ebaf2f58 59 this.active = false;
59ab0e7a 60 this.coordinatePoint = false;
fa052229 61 this.tempmarker = new EventEmitter();
59ab0e7a 62
8c91a910
EHF
63 this.form = fb.group({
64 "id": this.id,
65 "name": this.name,
66 "shortName": this.shortName,
67 "description": this.description,
68 "code": this.code,
69 "openingDate": this.openingDate,
70 "closedDate": this.closedDate,
71 "url": this.url,
72 "lat": this.lat,
73 "lng": this.lng,
74 "parent": this.parent,
75 "contactPerson": this.contactPerson,
76 "address": this.address,
77 "email": this.email,
6a4e2ca7
EHF
78 "phoneNumber": this.phoneNumber,
79 "featureType": this.featureType
8c91a910 80 });
fa052229
EHF
81 let instance = this;
82 this.lat.valueChanges.observer({
83 next: (value) => {
84 if(instance.lng.value && value) {
85 let pos = {lat: value, lng: instance.lng.value};
86 this.tempmarker.next(pos);
87 }
88 }
89 });
90 this.lng.valueChanges.observer({
91 next: (value) => {
92 if(instance.lat.value && value) {
93 let pos = {lat: instance.lat.value, lng: value};
94 this.tempmarker.next(pos);
95 }
96 }
97 });
c1cf798d
EHF
98 }
99
9bdb78d0 100 update(orgunitId) {
f800869b 101 console.log("Skjer det noe her? ");
ebaf2f58 102 this.active = true;
8c91a910 103 this.newObject = false;
c1cf798d
EHF
104 this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
105 .map(res => res.json())
8c91a910 106 .subscribe(res => this.updateValues(res))
ebe03648 107 }
9ce2e2a9 108
8c91a910 109 updateValues(res){
59ab0e7a 110
8c91a910
EHF
111 for(control in this.form.controls){
112 if(res[control] !== undefined) {
113 this.form.controls[control].updateValue(res[control]);
114 }
115 else
116 this.form.controls[control].updateValue("");
117 }
59ab0e7a
EHF
118
119 if(res.featureType === "POINT"){
120 this.coordinatePoint = true;
121 let coord = new Object();
122 coord = JSON.parse(res["coordinates"]);
123 this.form.controls.lat.updateValue(coord[0]);
124 this.form.controls.lng.updateValue(coord[1]);
125 }
126 else{
127 this.coordinatePoint = false;
128 }
8c91a910 129 }
59ab0e7a
EHF
130
131
132
9bdb78d0
EHF
133 onSubmit() {
134 this.editmode = false;
135
136 let headers = new Headers();
137 headers.append('Accept', 'application/json');
59ab0e7a 138
9bdb78d0
EHF
139 headers.append('Content-Type', 'application/json');
140
8c91a910
EHF
141 let jsonObject = this.form.value;
142
143 $.each(jsonObject, function(key, value){
144 if (value === "" || value === null){
145 delete jsonObject[key];
146 }
147 });
148
149
150
151 console.log(this.form.value);
152
153 if (this.newObject) {
154 jsonObject.parent = {};
155 jsonObject.parent.id = this.form.controls.parent.value;
156 jsonObject.featureType="POINT";
157 jsonObject.coordinates="[" + this.form.controls.lat.value + ","+this.form.controls.lng.value+"]";
158 delete jsonObject["lat"];
159 delete jsonObject["lng"];
160 this.http.post(dhisAPI + "/api/organisationUnits/", JSON.stringify(jsonObject), {
9bdb78d0
EHF
161 headers: headers
162 })
163 .map(res => res.json())
164 .subscribe(res => console.log(res));
59ab0e7a
EHF
165 }else {
166 this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject), {
167 headers: headers
168 })
169 .map(res => res.json())
170 .subscribe(res => console.log(res));
9bdb78d0
EHF
171 }
172
9ce2e2a9 173 }
57c243b6
EHF
174
175 cancel(){
176 this.editmode = false;
57c243b6 177 }
ebaf2f58 178
fa052229 179
8c91a910 180 add(data){
59ab0e7a 181 this.coordinatePoint = true;
8c91a910 182 this.newObject=true;
59ab0e7a 183 this.active = true;
ebaf2f58 184 this.editmode = true;
8c91a910 185
fa052229
EHF
186 for(control in this.form.controls){
187 this.form.controls[control].updateValue("");
188 }
189
8c91a910
EHF
190 this.form.controls.lat.updateValue(data.location.lat);
191 this.form.controls.lng.updateValue(data.location.lng);
192 this.form.controls.parent.updateValue(data.parent);
193
ebaf2f58 194 }
ebe03648
EHF
195}
196