Merge branch 'navbar'
[u/erikhf/frm.git] / src / components / sidebar / sidebar.ts
CommitLineData
8c91a910 1import {Component, NgFor, NgIf, 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
EHF
8 directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgModel, NgIf],
9 templateUrl: './components/sidebar/sidebar.html',
10 styles: [`
8c91a910 11 .ng-valid.ng-dirty {
9bdb78d0
EHF
12 border-left: 5px solid #42A948; /* green */
13 }
14 .ng-invalid {
15 border-left: 5px solid #a94442; /* red */
16 }
8c91a910
EHF
17 .form-background {
18 padding: 10px;
19 background: white;
20 }
9bdb78d0 21 `]
ebe03648 22})
8c91a910 23
ebe03648 24export class Sidebar {
8c91a910 25 form: ControlGroup;
9bdb78d0 26 http:Http;
8c91a910 27 newObject: boolean;
9bdb78d0 28 editmode:boolean;
ebaf2f58 29 active: boolean;
59ab0e7a 30 coordinatePoint: boolean;
8c91a910
EHF
31
32 id: Control = new Control("");
33 name: Control = new Control("", Validators.required);
34 shortName: Control = new Control("",Validators.required);
35 description: Control = new Control("");
36 code: Control = new Control("");
37 openingDate: Control = new Control("",Validators.required);
38 closedDate: Control = new Control("");
39 url: Control = new Control("");
40 lat: Control = new Control("");
41 lng: Control = new Control("");
42 parent: Control = new Control("");
43 contactPerson: Control = new Control("");
44 address: Control = new Control("");
45 email: Control = new Control("");
46 phoneNumber: Control = new Control("");
47
48
49 constructor(http:Http, fb: FormBuilder) {
ebe03648 50 this.http = http;
9ce2e2a9 51 this.editmode = false;
ebaf2f58 52 this.active = false;
59ab0e7a
EHF
53 this.coordinatePoint = false;
54
8c91a910
EHF
55 this.form = fb.group({
56 "id": this.id,
57 "name": this.name,
58 "shortName": this.shortName,
59 "description": this.description,
60 "code": this.code,
61 "openingDate": this.openingDate,
62 "closedDate": this.closedDate,
63 "url": this.url,
64 "lat": this.lat,
65 "lng": this.lng,
66 "parent": this.parent,
67 "contactPerson": this.contactPerson,
68 "address": this.address,
69 "email": this.email,
70 "phoneNumber": this.phoneNumber
71 });
c1cf798d
EHF
72 }
73
9bdb78d0 74 update(orgunitId) {
ebaf2f58 75 this.active = true;
8c91a910 76 this.newObject = false;
c1cf798d
EHF
77 this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
78 .map(res => res.json())
8c91a910 79 .subscribe(res => this.updateValues(res))
ebe03648 80 }
9ce2e2a9 81
8c91a910 82 updateValues(res){
59ab0e7a 83
8c91a910
EHF
84 for(control in this.form.controls){
85 if(res[control] !== undefined) {
86 this.form.controls[control].updateValue(res[control]);
87 }
88 else
89 this.form.controls[control].updateValue("");
90 }
59ab0e7a
EHF
91
92 if(res.featureType === "POINT"){
93 this.coordinatePoint = true;
94 let coord = new Object();
95 coord = JSON.parse(res["coordinates"]);
96 this.form.controls.lat.updateValue(coord[0]);
97 this.form.controls.lng.updateValue(coord[1]);
98 }
99 else{
100 this.coordinatePoint = false;
101 }
8c91a910 102 }
59ab0e7a
EHF
103
104
105
9bdb78d0
EHF
106 onSubmit() {
107 this.editmode = false;
108
109 let headers = new Headers();
110 headers.append('Accept', 'application/json');
59ab0e7a 111
9bdb78d0
EHF
112 headers.append('Content-Type', 'application/json');
113
8c91a910
EHF
114 let jsonObject = this.form.value;
115
116 $.each(jsonObject, function(key, value){
117 if (value === "" || value === null){
118 delete jsonObject[key];
119 }
120 });
121
122
123
124 console.log(this.form.value);
125
126 if (this.newObject) {
127 jsonObject.parent = {};
128 jsonObject.parent.id = this.form.controls.parent.value;
129 jsonObject.featureType="POINT";
130 jsonObject.coordinates="[" + this.form.controls.lat.value + ","+this.form.controls.lng.value+"]";
131 delete jsonObject["lat"];
132 delete jsonObject["lng"];
133 this.http.post(dhisAPI + "/api/organisationUnits/", JSON.stringify(jsonObject), {
9bdb78d0
EHF
134 headers: headers
135 })
136 .map(res => res.json())
137 .subscribe(res => console.log(res));
59ab0e7a
EHF
138 }else {
139 this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject), {
140 headers: headers
141 })
142 .map(res => res.json())
143 .subscribe(res => console.log(res));
9bdb78d0
EHF
144 }
145
9ce2e2a9 146 }
57c243b6
EHF
147
148 cancel(){
149 this.editmode = false;
57c243b6 150 }
ebaf2f58 151
8c91a910 152 add(data){
59ab0e7a 153 this.coordinatePoint = true;
8c91a910 154 this.newObject=true;
59ab0e7a 155 this.active = true;
ebaf2f58 156 this.editmode = true;
8c91a910
EHF
157
158 this.form.controls.lat.updateValue(data.location.lat);
159 this.form.controls.lng.updateValue(data.location.lng);
160 this.form.controls.parent.updateValue(data.parent);
161
ebaf2f58 162 }
ebe03648
EHF
163}
164