1 import {Component, NgFor, NgIf, EventEmitter, 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 events: ['tempmarker','updateorg'],
10 templateUrl: './components/sidebar/sidebar.html'
13 export class Sidebar {
20 coordinatePoint: boolean;
22 groupSets: Array<any> = [];
23 groupsDoubleArray: any[][] = [];
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("");
41 featureType: Control = new Control("");
42 coordinates: Control = new Control("");
43 ctrlGroups: Array<Control> = [new Control('')];
44 groupsArray: ControlArray = new ControlArray(this.ctrlGroups);
47 form: ControlGroup = new ControlGroup({
48 organisationUnitGroups: this.groupsArray,
51 shortName: this.shortName,
52 description: this.description,
54 openingDate: this.openingDate,
55 closedDate: this.closedDate,
60 contactPerson: this.contactPerson,
61 address: this.address,
63 phoneNumber: this.phoneNumber,
64 featureType: this.featureType,
65 coordinates: this.coordinates
68 constructor(http:Http, fb: FormBuilder) {
70 this.editmode = false;
72 this.coordinatePoint = false;
73 this.tempmarker = new EventEmitter();
74 this.updateorg = new EventEmitter();
75 this.exitButton = document.getElementById("slideout")
78 this.lat.valueChanges.observer({
80 if(instance.lng.value && value) {
81 let pos = {lat: value, lng: instance.lng.value};
82 this.tempmarker.next(pos);
86 this.lng.valueChanges.observer({
88 if(instance.lat.value && value) {
89 let pos = {lat: instance.lat.value, lng: value};
90 this.tempmarker.next(pos);
95 this.findOrgUnitSets();
100 console.log("Skjer det noe her? ");
102 this.newObject = false;
103 this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
104 .map(res => res.json())
105 .subscribe(res => this.updateValues(res))
110 for(control in this.form.controls){
111 if(this.form.controls[control] instanceof ControlArray){
112 console.log("nothing to do here");
114 else if(res[control] !== undefined) {
115 this.form.controls[control].updateValue(res[control]);
118 this.form.controls[control].updateValue("");
123 if(res["openingDate"]){
124 this.form.controls["openingDate"].updateValue((new Date(res["openingDate"].substring(0,10))).toISOString().substring(0,10));
126 if(res["closedDate"]){
127 this.form.controls["closedDate"].updateValue((new Date(res["closedDate"].substring(0,10))).toISOString().substring(0,10));
130 if(res.featureType === "POINT"){
131 this.coordinatePoint = true;
132 let coord = new Object();
133 coord = JSON.parse(res["coordinates"]);
134 this.form.controls.lat.updateValue(coord[1]);
135 this.form.controls.lng.updateValue(coord[0]);
138 this.coordinatePoint = false;
141 for(var i = 0; i < this.groupsDoubleArray.length; i++){
142 for(var j = 0; j < this.groupsDoubleArray[i].length; j++){
143 for( group in res.organisationUnitGroups){
144 if( res.organisationUnitGroups[group].id == this.groupsDoubleArray[i][j].id ){
145 this.form.controls.organisationUnitGroups.controls[i].updateValue(this.groupsDoubleArray[i][j].name);
151 console.log("faenskap");
157 this.editmode = false;
159 let headers = new Headers();
160 headers.append('Accept', 'application/json');
162 headers.append('Content-Type', 'application/json');
164 let jsonObject = this.form.value;
166 $.each(jsonObject, function(key, value){
167 if (value === "" || value === null){
168 delete jsonObject[key];
172 $.each(jsonObject.organisationUnitGroups, function(key, value){
173 // if( value === "" || value === null){
174 delete jsonObject.organisationUnitGroups[key];
176 // jsonObject.organisationUnitGroups[key].id = value;
181 jsonObject.openingDate = (new Date(this.form.value.openingDate)).toISOString();
183 if(this.form.value.closedDate){
184 jsonObject.closedDate = (new Date(this.form.value.closedDate)).toISOString();
188 console.log(this.form.value);
190 if (this.newObject) {
191 jsonObject.parent = {};
192 jsonObject.parent.id = this.form.controls.parent.value;
193 jsonObject.featureType="POINT";
194 jsonObject.coordinates="[" + this.form.controls.lng.value + ","+this.form.controls.lat.value+"]";
195 delete jsonObject["lat"];
196 delete jsonObject["lng"];
197 this.http.post(dhisAPI + "/api/organisationUnits/", JSON.stringify(jsonObject), {
200 .map(res => res.json())
201 .subscribe(res => this.emitNewUpdatedObject(res));
203 this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject), {
206 .map(res => res.json())
207 .subscribe(res => console.log(res));
215 emitNewUpdatedObject(obj){
217 this.updateorg.next(obj.response.lastImported);
221 this.editmode = false;
222 this.tempmarker.next(null);
227 this.coordinatePoint = true;
230 this.editmode = true;
232 for(control in this.form.controls){
233 if(!(this.form.controls[control] instanceof ControlArray))
234 this.form.controls[control].updateValue("");
237 this.form.controls.lat.updateValue(data.location.lat);
238 this.form.controls.lng.updateValue(data.location.lng);
239 this.form.controls.parent.updateValue(data.parent);
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))
255 addOrgUnitSets(instance, res){
256 //delete instance.ctrlGroups[0];
258 console.log(instance.form.controls);
259 instance.groupsArray.push(new Control(''));
260 instance.groupSets.push(res[group]);
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))
267 // instance.form.push(new Control(""));
269 console.log(instance.groupSets);
272 addOrgUnitGroup(instance, res){
273 let ar: Array<any> = [];
277 instance.groupsDoubleArray.push(ar);
279 console.log(instance.groupsDoubleArray);