Cleanup
[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("");
53
54
55 constructor(http:Http, fb: FormBuilder) {
ebe03648 56 this.http = http;
9ce2e2a9 57 this.editmode = false;
ebaf2f58 58 this.active = false;
59ab0e7a 59 this.coordinatePoint = false;
fa052229 60 this.tempmarker = new EventEmitter();
59ab0e7a 61
8c91a910
EHF
62 this.form = fb.group({
63 "id": this.id,
64 "name": this.name,
65 "shortName": this.shortName,
66 "description": this.description,
67 "code": this.code,
68 "openingDate": this.openingDate,
69 "closedDate": this.closedDate,
70 "url": this.url,
71 "lat": this.lat,
72 "lng": this.lng,
73 "parent": this.parent,
74 "contactPerson": this.contactPerson,
75 "address": this.address,
76 "email": this.email,
77 "phoneNumber": this.phoneNumber
78 });
fa052229
EHF
79 let instance = this;
80 this.lat.valueChanges.observer({
81 next: (value) => {
82 if(instance.lng.value && value) {
83 let pos = {lat: value, lng: instance.lng.value};
84 this.tempmarker.next(pos);
85 }
86 }
87 });
88 this.lng.valueChanges.observer({
89 next: (value) => {
90 if(instance.lat.value && value) {
91 let pos = {lat: instance.lat.value, lng: value};
92 this.tempmarker.next(pos);
93 }
94 }
95 });
c1cf798d
EHF
96 }
97
9bdb78d0 98 update(orgunitId) {
f800869b 99 console.log("Skjer det noe her? ");
ebaf2f58 100 this.active = true;
8c91a910 101 this.newObject = false;
c1cf798d
EHF
102 this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
103 .map(res => res.json())
8c91a910 104 .subscribe(res => this.updateValues(res))
ebe03648 105 }
9ce2e2a9 106
8c91a910 107 updateValues(res){
59ab0e7a 108
8c91a910
EHF
109 for(control in this.form.controls){
110 if(res[control] !== undefined) {
111 this.form.controls[control].updateValue(res[control]);
112 }
113 else
114 this.form.controls[control].updateValue("");
115 }
59ab0e7a
EHF
116
117 if(res.featureType === "POINT"){
118 this.coordinatePoint = true;
119 let coord = new Object();
120 coord = JSON.parse(res["coordinates"]);
121 this.form.controls.lat.updateValue(coord[0]);
122 this.form.controls.lng.updateValue(coord[1]);
123 }
124 else{
125 this.coordinatePoint = false;
126 }
8c91a910 127 }
59ab0e7a
EHF
128
129
130
9bdb78d0
EHF
131 onSubmit() {
132 this.editmode = false;
133
134 let headers = new Headers();
135 headers.append('Accept', 'application/json');
59ab0e7a 136
9bdb78d0
EHF
137 headers.append('Content-Type', 'application/json');
138
8c91a910
EHF
139 let jsonObject = this.form.value;
140
141 $.each(jsonObject, function(key, value){
142 if (value === "" || value === null){
143 delete jsonObject[key];
144 }
145 });
146
147
148
149 console.log(this.form.value);
150
151 if (this.newObject) {
152 jsonObject.parent = {};
153 jsonObject.parent.id = this.form.controls.parent.value;
154 jsonObject.featureType="POINT";
155 jsonObject.coordinates="[" + this.form.controls.lat.value + ","+this.form.controls.lng.value+"]";
156 delete jsonObject["lat"];
157 delete jsonObject["lng"];
158 this.http.post(dhisAPI + "/api/organisationUnits/", JSON.stringify(jsonObject), {
9bdb78d0
EHF
159 headers: headers
160 })
161 .map(res => res.json())
162 .subscribe(res => console.log(res));
59ab0e7a
EHF
163 }else {
164 this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject), {
165 headers: headers
166 })
167 .map(res => res.json())
168 .subscribe(res => console.log(res));
9bdb78d0
EHF
169 }
170
9ce2e2a9 171 }
57c243b6
EHF
172
173 cancel(){
174 this.editmode = false;
57c243b6 175 }
ebaf2f58 176
fa052229 177
8c91a910 178 add(data){
59ab0e7a 179 this.coordinatePoint = true;
8c91a910 180 this.newObject=true;
59ab0e7a 181 this.active = true;
ebaf2f58 182 this.editmode = true;
8c91a910 183
fa052229
EHF
184 for(control in this.form.controls){
185 this.form.controls[control].updateValue("");
186 }
187
8c91a910
EHF
188 this.form.controls.lat.updateValue(data.location.lat);
189 this.form.controls.lng.updateValue(data.location.lng);
190 this.form.controls.parent.updateValue(data.parent);
191
ebaf2f58 192 }
ebe03648
EHF
193}
194