[fix] merge fix
[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;
8c91a910
EHF
30
31 id: Control = new Control("");
32 name: Control = new Control("", Validators.required);
33 shortName: Control = new Control("",Validators.required);
34 description: Control = new Control("");
35 code: Control = new Control("");
36 openingDate: Control = new Control("",Validators.required);
37 closedDate: Control = new Control("");
38 url: Control = new Control("");
39 lat: Control = new Control("");
40 lng: Control = new Control("");
41 parent: Control = new Control("");
42 contactPerson: Control = new Control("");
43 address: Control = new Control("");
44 email: Control = new Control("");
45 phoneNumber: Control = new Control("");
46
47
48 constructor(http:Http, fb: FormBuilder) {
ebe03648 49 this.http = http;
9ce2e2a9 50 this.editmode = false;
ebaf2f58 51 this.active = false;
8c91a910
EHF
52 this.form = fb.group({
53 "id": this.id,
54 "name": this.name,
55 "shortName": this.shortName,
56 "description": this.description,
57 "code": this.code,
58 "openingDate": this.openingDate,
59 "closedDate": this.closedDate,
60 "url": this.url,
61 "lat": this.lat,
62 "lng": this.lng,
63 "parent": this.parent,
64 "contactPerson": this.contactPerson,
65 "address": this.address,
66 "email": this.email,
67 "phoneNumber": this.phoneNumber
68 });
c1cf798d
EHF
69 }
70
9bdb78d0 71 update(orgunitId) {
ebaf2f58 72 this.active = true;
8c91a910 73 this.newObject = false;
c1cf798d
EHF
74 this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
75 .map(res => res.json())
8c91a910 76 .subscribe(res => this.updateValues(res))
ebe03648 77 }
9ce2e2a9 78
8c91a910
EHF
79 updateValues(res){
80 for(control in this.form.controls){
81 if(res[control] !== undefined) {
82 this.form.controls[control].updateValue(res[control]);
83 }
84 else
85 this.form.controls[control].updateValue("");
86 }
87 }
9bdb78d0
EHF
88 onSubmit() {
89 this.editmode = false;
90
91 let headers = new Headers();
92 headers.append('Accept', 'application/json');
93 headers.append('Content-Type', 'application/json');
94
8c91a910
EHF
95 let jsonObject = this.form.value;
96
97 $.each(jsonObject, function(key, value){
98 if (value === "" || value === null){
99 delete jsonObject[key];
100 }
101 });
102
103
104
105 console.log(this.form.value);
106
107 if (this.newObject) {
108 jsonObject.parent = {};
109 jsonObject.parent.id = this.form.controls.parent.value;
110 jsonObject.featureType="POINT";
111 jsonObject.coordinates="[" + this.form.controls.lat.value + ","+this.form.controls.lng.value+"]";
112 delete jsonObject["lat"];
113 delete jsonObject["lng"];
114 this.http.post(dhisAPI + "/api/organisationUnits/", JSON.stringify(jsonObject), {
9bdb78d0
EHF
115 headers: headers
116 })
117 .map(res => res.json())
118 .subscribe(res => console.log(res));
8c91a910
EHF
119 }else{
120 this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject),{
121 headers: headers
122 })
123 .map(res => res.json())
124 .subscribe(res => console.log(res));
9bdb78d0
EHF
125 }
126
ebaf2f58
EHF
127
128
8c91a910
EHF
129
130 //console.log(this.form);
131
132
9ce2e2a9 133 }
57c243b6
EHF
134
135 cancel(){
136 this.editmode = false;
57c243b6 137 }
ebaf2f58 138
8c91a910
EHF
139 add(data){
140 this.newObject=true;
141 this.setActive();
ebaf2f58
EHF
142 this.editmode = true;
143 console.log(data);
8c91a910
EHF
144
145 this.form.controls.lat.updateValue(data.location.lat);
146 this.form.controls.lng.updateValue(data.location.lng);
147 this.form.controls.parent.updateValue(data.parent);
148
ebaf2f58 149 console.log("faen");
8c91a910
EHF
150
151 }
152
153 setActive(){
154 this.active = true;
ebaf2f58 155 }
ebe03648
EHF
156}
157