merge faardih
[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
5539d095 31
9bdb78d0 32 http:Http;
8c91a910 33 newObject: boolean;
9bdb78d0 34 editmode:boolean;
ebaf2f58 35 active: boolean;
59ab0e7a 36 coordinatePoint: boolean;
8c91a910 37
5539d095
EHF
38 groupSets: Array<any> = [];
39 groupsDoubleArray: any[][] = [];
40
8c91a910
EHF
41 id: Control = new Control("");
42 name: Control = new Control("", Validators.required);
43 shortName: Control = new Control("",Validators.required);
44 description: Control = new Control("");
45 code: Control = new Control("");
46 openingDate: Control = new Control("",Validators.required);
47 closedDate: Control = new Control("");
48 url: Control = new Control("");
49 lat: Control = new Control("");
50 lng: Control = new Control("");
51 parent: Control = new Control("");
52 contactPerson: Control = new Control("");
53 address: Control = new Control("");
54 email: Control = new Control("");
55 phoneNumber: Control = new Control("");
6a4e2ca7 56 featureType: Control = new Control("");
f03b7890 57 coordinates: Control = new Control("");
5539d095
EHF
58 ctrlGroups: Control[]= [new Control('')];
59 groupsArray: ControlArray = new ControlArray(this.ctrlGroups);
60
61
62 form: ControlGroup = new ControlGroup({
63 organisationUnitGroups: this.groupsArray,
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,
78 phoneNumber: this.phoneNumber,
79 featureType: this.featureType,
80 coordinates: this.coordinates
81 });
8c91a910
EHF
82
83 constructor(http:Http, fb: FormBuilder) {
ebe03648 84 this.http = http;
9ce2e2a9 85 this.editmode = false;
ebaf2f58 86 this.active = false;
59ab0e7a 87 this.coordinatePoint = false;
fa052229 88 this.tempmarker = new EventEmitter();
59ab0e7a 89
fa052229
EHF
90 let instance = this;
91 this.lat.valueChanges.observer({
92 next: (value) => {
93 if(instance.lng.value && value) {
94 let pos = {lat: value, lng: instance.lng.value};
95 this.tempmarker.next(pos);
96 }
97 }
98 });
99 this.lng.valueChanges.observer({
100 next: (value) => {
101 if(instance.lat.value && value) {
102 let pos = {lat: instance.lat.value, lng: value};
103 this.tempmarker.next(pos);
104 }
105 }
106 });
5539d095
EHF
107
108 this.findOrgUnitSets();
109
c1cf798d
EHF
110 }
111
9bdb78d0 112 update(orgunitId) {
f800869b 113 console.log("Skjer det noe her? ");
ebaf2f58 114 this.active = true;
8c91a910 115 this.newObject = false;
c1cf798d
EHF
116 this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
117 .map(res => res.json())
8c91a910 118 .subscribe(res => this.updateValues(res))
ebe03648 119 }
9ce2e2a9 120
8c91a910 121 updateValues(res){
59ab0e7a 122
8c91a910 123 for(control in this.form.controls){
5539d095
EHF
124 if(this.form.controls[control] instanceof ControlArray){
125 console.log("nothing to do here");
126 }
127 else if(res[control] !== undefined) {
8c91a910
EHF
128 this.form.controls[control].updateValue(res[control]);
129 }
130 else
131 this.form.controls[control].updateValue("");
5539d095 132
8c91a910 133 }
59ab0e7a 134
531b0170
EHF
135 // Date fix:
136 if(res["openingDate"]){
137 this.form.controls["openingDate"].updateValue((new Date(res["openingDate"].substring(0,10))).toISOString().substring(0,10));
138 }
139 if(res["closedDate"]){
140 this.form.controls["closedDate"].updateValue((new Date(res["closedDate"].substring(0,10))).toISOString().substring(0,10));
141 }
142
59ab0e7a
EHF
143 if(res.featureType === "POINT"){
144 this.coordinatePoint = true;
145 let coord = new Object();
146 coord = JSON.parse(res["coordinates"]);
147 this.form.controls.lat.updateValue(coord[0]);
148 this.form.controls.lng.updateValue(coord[1]);
149 }
150 else{
151 this.coordinatePoint = false;
152 }
5539d095
EHF
153
154 for(var i = 0; i < this.groupsDoubleArray.length; i++){
155 for(var j = 0; j < this.groupsDoubleArray[i].length; j++){
156 for( group in res.organisationUnitGroups){
157 if( res.organisationUnitGroups[group].id == this.groupsDoubleArray[i][j].id ){
158 this.form.controls.organisationUnitGroups.controls[i].updateValue(this.groupsDoubleArray[i][j].name);
159 }
160 }
161 }
162 }
163
164 console.log("faenskap");
8c91a910 165 }
59ab0e7a
EHF
166
167
168
9bdb78d0
EHF
169 onSubmit() {
170 this.editmode = false;
171
172 let headers = new Headers();
173 headers.append('Accept', 'application/json');
59ab0e7a 174
9bdb78d0
EHF
175 headers.append('Content-Type', 'application/json');
176
8c91a910
EHF
177 let jsonObject = this.form.value;
178
179 $.each(jsonObject, function(key, value){
180 if (value === "" || value === null){
181 delete jsonObject[key];
182 }
183 });
184
5539d095
EHF
185 $.each(jsonObject.organisationUnitGroups, function(key, value){
186 if( value === "" || value === null){
187 delete jsonObject.organisationUnitGroups[key];
188 } else {
189 jsonObject.organisationUnitGroups[key].id = value;
190 }
191 });
192
193
531b0170
EHF
194 jsonObject.openingDate = (new Date(this.form.value.openingDate)).toISOString();
195
196 if(this.form.value.closedDate){
197 jsonObject.closedDate = (new Date(this.form.value.closedDate)).toISOString();
198 }
8c91a910
EHF
199
200
201 console.log(this.form.value);
202
203 if (this.newObject) {
204 jsonObject.parent = {};
205 jsonObject.parent.id = this.form.controls.parent.value;
206 jsonObject.featureType="POINT";
207 jsonObject.coordinates="[" + this.form.controls.lat.value + ","+this.form.controls.lng.value+"]";
208 delete jsonObject["lat"];
209 delete jsonObject["lng"];
210 this.http.post(dhisAPI + "/api/organisationUnits/", JSON.stringify(jsonObject), {
9bdb78d0
EHF
211 headers: headers
212 })
213 .map(res => res.json())
214 .subscribe(res => console.log(res));
59ab0e7a
EHF
215 }else {
216 this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject), {
217 headers: headers
218 })
219 .map(res => res.json())
220 .subscribe(res => console.log(res));
9bdb78d0
EHF
221 }
222
9ce2e2a9 223 }
57c243b6
EHF
224
225 cancel(){
226 this.editmode = false;
57c243b6 227 }
ebaf2f58 228
fa052229 229
8c91a910 230 add(data){
59ab0e7a 231 this.coordinatePoint = true;
8c91a910 232 this.newObject=true;
59ab0e7a 233 this.active = true;
ebaf2f58 234 this.editmode = true;
8c91a910 235
fa052229 236 for(control in this.form.controls){
5539d095
EHF
237 if(!(this.form.controls[control] instanceof ControlArray))
238 this.form.controls[control].updateValue("");
fa052229
EHF
239 }
240
8c91a910
EHF
241 this.form.controls.lat.updateValue(data.location.lat);
242 this.form.controls.lng.updateValue(data.location.lng);
243 this.form.controls.parent.updateValue(data.parent);
244
ebaf2f58 245 }
5539d095
EHF
246
247 findOrgUnitSets(){
248 let instance = this;
249 this.http.get(dhisAPI + "/api/organisationUnitGroupSets?paging=false")
250 .map(res => res.json())
251 .map(res => res.organisationUnitGroupSets)
252 .subscribe(res => this.addOrgUnitSets(instance, res))
253 }
254
255 addOrgUnitSets(instance, res){
256 instance.ctrlArray.removeAt(0);
257 for( group in res){
258 console.log(instance.form.controls);
259 instance.groupsArray.push(new Control(''));
260 instance.groupSets.push(res[group]);
261
262 this.http.get(dhisAPI + "/api/organisationUnitGroupSets/" + res[group].id)
263 .map(res => res.json())
264 .map(res => res.organisationUnitGroups)
265 .subscribe(res => this.addOrgUnitGroup(instance, res))
266
267 // instance.form.push(new Control(""));
268 }
269 console.log(instance.groupSets);
270 }
271
272 addOrgUnitGroup(instance, res){
273 let ar: Array<any> = [];
274 for( group in res){
275 ar.push(res[group]);
276 }
277 instance.groupsDoubleArray.push(ar);
278
279 console.log(instance.groupsDoubleArray);
280 }
ebe03648
EHF
281}
282