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