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