1 import {Component, NgFor, NgIf, NgModel, Control, ControlGroup, ControlArray, Validators, FormBuilder, CORE_DIRECTIVES,FORM_DIRECTIVES} from 'angular2/angular2';
2 import {Http, Headers} from 'angular2/http';
4 declare var zone: Zone;
7 selector: 'mou-sidebar',
8 directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgModel, NgIf],
9 templateUrl: './components/sidebar/sidebar.html',
12 border-left: 5px solid #42A948; /* green */
15 border-left: 5px solid #a94442; /* red */
24 export class Sidebar {
30 coordinatePoint: boolean;
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("");
49 constructor(http:Http, fb: FormBuilder) {
51 this.editmode = false;
53 this.coordinatePoint = false;
55 this.form = fb.group({
58 "shortName": this.shortName,
59 "description": this.description,
61 "openingDate": this.openingDate,
62 "closedDate": this.closedDate,
66 "parent": this.parent,
67 "contactPerson": this.contactPerson,
68 "address": this.address,
70 "phoneNumber": this.phoneNumber
76 this.newObject = false;
77 this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
78 .map(res => res.json())
79 .subscribe(res => this.updateValues(res))
84 for(control in this.form.controls){
85 if(res[control] !== undefined) {
86 this.form.controls[control].updateValue(res[control]);
89 this.form.controls[control].updateValue("");
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]);
100 this.coordinatePoint = false;
107 this.editmode = false;
109 let headers = new Headers();
110 headers.append('Accept', 'application/json');
112 headers.append('Content-Type', 'application/json');
114 let jsonObject = this.form.value;
116 $.each(jsonObject, function(key, value){
117 if (value === "" || value === null){
118 delete jsonObject[key];
124 console.log(this.form.value);
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), {
136 .map(res => res.json())
137 .subscribe(res => console.log(res));
139 this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject), {
142 .map(res => res.json())
143 .subscribe(res => console.log(res));
149 this.editmode = false;
153 this.coordinatePoint = true;
156 this.editmode = true;
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);