[sidebar] Added comments, formatted code
authorErik Haider Forsén <erikhf@ifi.uio.no>
Thu, 10 Dec 2015 18:23:15 +0000 (19:23 +0100)
committerErik Haider Forsén <erikhf@ifi.uio.no>
Thu, 10 Dec 2015 18:23:25 +0000 (19:23 +0100)
src/components/sidebar/sidebar.ts

index 3adf82e..c9329f4 100644 (file)
@@ -1,50 +1,48 @@
 import {Component, NgFor, NgIf, EventEmitter, NgModel, Control, ControlGroup, ControlArray, Validators, FormBuilder, CORE_DIRECTIVES,FORM_DIRECTIVES} from 'angular2/angular2';
 import {Http, Headers} from 'angular2/http';
 
-declare var zone: Zone;
+declare var zone:Zone;
 
 @Component({
     selector: 'mou-sidebar',
     directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgModel, NgIf],
-    events: ['tempmarker','updateorg'],
+    events: ['tempmarker', 'updateorg'],
     templateUrl: './components/sidebar/sidebar.html'
 })
 
 export class Sidebar {
-
-
     http:Http;
-    newObject: boolean;
+    newObject:boolean;
     editmode:boolean;
-    active: boolean;
-    coordinatePoint: boolean;
-
-    groupSets: Array<any> = [];
-    groupsDoubleArray: any[][] = [];
-
-    id: Control = new Control("");
-    name: Control = new Control("", Validators.required);
-    shortName: Control = new Control("",Validators.required);
-    description: Control = new Control("");
-    code: Control = new Control("");
-    openingDate: Control = new Control("",Validators.required);
-    closedDate: Control = new Control("");
-    url: Control = new Control("");
-    lat: Control = new Control("");
-    lng: Control = new Control("");
-    parent: Control = new Control("");
-    contactPerson: Control = new Control("");
-    address: Control = new Control("");
-    email: Control = new Control("");
-    phoneNumber: Control = new Control("");
-    exitButton: any;
-    featureType: Control = new Control("");
-    coordinates: Control = new Control("");
-    ctrlGroups: Array<Control> = [new Control('')];
-    groupsArray: ControlArray = new ControlArray(this.ctrlGroups);
-
-
-    form: ControlGroup = new ControlGroup({
+    active:boolean;
+    coordinatePoint:boolean;
+
+    groupSets:Array<any> = [];
+    groupsDoubleArray:any[][] = [];
+
+    id:Control = new Control("");
+    name:Control = new Control("", Validators.required);
+    shortName:Control = new Control("", Validators.required);
+    description:Control = new Control("");
+    code:Control = new Control("");
+    openingDate:Control = new Control("", Validators.required);
+    closedDate:Control = new Control("");
+    url:Control = new Control("");
+    lat:Control = new Control("");
+    lng:Control = new Control("");
+    parent:Control = new Control("");
+    contactPerson:Control = new Control("");
+    address:Control = new Control("");
+    email:Control = new Control("");
+    phoneNumber:Control = new Control("");
+    exitButton:any;
+    featureType:Control = new Control("");
+    coordinates:Control = new Control("");
+    ctrlGroups:Array<Control> = [new Control('')];
+    groupsArray:ControlArray = new ControlArray(this.ctrlGroups);
+
+
+    form:ControlGroup = new ControlGroup({
         organisationUnitGroups: this.groupsArray,
         id: this.id,
         name: this.name,
@@ -65,7 +63,7 @@ export class Sidebar {
         coordinates: this.coordinates
     });
 
-    constructor(http:Http, fb: FormBuilder) {
+    constructor(http:Http, fb:FormBuilder) {
         this.http = http;
         this.editmode = false;
         this.active = false;
@@ -78,7 +76,7 @@ export class Sidebar {
         // listener for value change in coordinate input field
         this.lat.valueChanges.observer({
             next: (value) => {
-                if(instance.lng.value && value) {
+                if (instance.lng.value && value) {
                     let pos = {lat: value, lng: instance.lng.value};
                     this.tempmarker.next(pos);
                 }
@@ -86,7 +84,7 @@ export class Sidebar {
         });
         this.lng.valueChanges.observer({
             next: (value) => {
-                if(instance.lat.value && value) {
+                if (instance.lat.value && value) {
                     let pos = {lat: instance.lat.value, lng: value};
                     this.tempmarker.next(pos);
                 }
@@ -107,14 +105,14 @@ export class Sidebar {
     }
 
     // update form values with new information from http get result
-    updateValues(res){
+    updateValues(res) {
 
         // update the form controls with data from incoming json object
-        for(control in this.form.controls){
-            if(this.form.controls[control] instanceof ControlArray){
+        for (control in this.form.controls) {
+            if (this.form.controls[control] instanceof ControlArray) {
                 console.log("nothing to do here");
             }
-            else if(res[control] !== undefined) {
+            else if (res[control] !== undefined) {
                 this.form.controls[control].updateValue(res[control]);
             }
             else
@@ -123,31 +121,31 @@ export class Sidebar {
         }
 
         // Date fix:
-        if(res["openingDate"]){
-            this.form.controls["openingDate"].updateValue((new Date(res["openingDate"].substring(0,10))).toISOString().substring(0,10));
+        if (res["openingDate"]) {
+            this.form.controls["openingDate"].updateValue((new Date(res["openingDate"].substring(0, 10))).toISOString().substring(0, 10));
         }
-        if(res["closedDate"]){
-            this.form.controls["closedDate"].updateValue((new Date(res["closedDate"].substring(0,10))).toISOString().substring(0,10));
+        if (res["closedDate"]) {
+            this.form.controls["closedDate"].updateValue((new Date(res["closedDate"].substring(0, 10))).toISOString().substring(0, 10));
         }
 
         // we're only interested in coordinates if it's a featureType point. Since we want to use two different input fields for lat and lang (and the api uses a single object for both)
         // we need to have a separate data structure for coordinates, and update them manually
-        if(res.featureType === "POINT"){
+        if (res.featureType === "POINT") {
             this.coordinatePoint = true;
             let coord = new Object();
             coord = JSON.parse(res["coordinates"]);
             this.form.controls.lat.updateValue(coord[1]);
             this.form.controls.lng.updateValue(coord[0]);
         }
-        else{
+        else {
             this.coordinatePoint = false;
         }
 
         // Update organisationUnitGroups with correct values from api
-        for(var i = 0; i < this.groupsDoubleArray.length; i++){
-            for(var j = 0; j < this.groupsDoubleArray[i].length; j++){
-                for( group in res.organisationUnitGroups){
-                    if( res.organisationUnitGroups[group].id == this.groupsDoubleArray[i][j].id ){
+        for (var i = 0; i < this.groupsDoubleArray.length; i++) {
+            for (var j = 0; j < this.groupsDoubleArray[i].length; j++) {
+                for (group in res.organisationUnitGroups) {
+                    if (res.organisationUnitGroups[group].id == this.groupsDoubleArray[i][j].id) {
                         this.form.controls.organisationUnitGroups.controls[i].updateValue(this.groupsDoubleArray[i][j].name);
                     }
                 }
@@ -155,7 +153,6 @@ export class Sidebar {
         }
     }
 
-
     // called on form submit
     onSubmit() {
         this.editmode = false;
@@ -169,31 +166,30 @@ export class Sidebar {
         let jsonObject = this.form.value;
 
         // remove empty fields from the form object, no need to send empty values to the api
-        $.each(jsonObject, function(key, value){
-            if (value === "" || value === null){
+        $.each(jsonObject, function (key, value) {
+            if (value === "" || value === null) {
                 delete jsonObject[key];
             }
         });
 
         // we were unable to find a way to associate a new (or existing) organisation unit with one or more organisationUnitGroups, so we're removing the data before posting to API
-        $.each(jsonObject.organisationUnitGroups, function(key, value){
+        $.each(jsonObject.organisationUnitGroups, function (key, value) {
 //            if( value === "" || value === null){
-                delete jsonObject.organisationUnitGroups[key];
-  //          } else {
-  //              jsonObject.organisationUnitGroups[key].id = value;
-   //         }
+            delete jsonObject.organisationUnitGroups[key];
+            //          } else {
+            //              jsonObject.organisationUnitGroups[key].id = value;
+            //         }
         });
 
-
         jsonObject.openingDate = (new Date(this.form.value.openingDate)).toISOString();
 
-        if(this.form.value.closedDate){
+        if (this.form.value.closedDate) {
             jsonObject.closedDate = (new Date(this.form.value.closedDate)).toISOString();
         }
 
-        if(this.coordinatePoint){
-            jsonObject.featureType="POINT";
-            jsonObject.coordinates="[" + this.form.controls.lng.value + ","+this.form.controls.lat.value+"]";
+        if (this.coordinatePoint) {
+            jsonObject.featureType = "POINT";
+            jsonObject.coordinates = "[" + this.form.controls.lng.value + "," + this.form.controls.lat.value + "]";
         }
 
         // POST if the object is new, PUT if it's an update to an existing orgUnit
@@ -208,38 +204,35 @@ export class Sidebar {
                 })
                 .map(res => res.json())
                 .subscribe(res => this.emitNewUpdatedObject(res));
-        }else {
+        } else {
             this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject), {
                     headers: headers
                 })
                 .map(res => res.json())
                 .subscribe(res => console.log(res));
         }
-
-
-
-
     }
 
-    emitNewUpdatedObject(obj){
-        console.log(obj);
+    // emit an event that the current orgUnit has been updated (useful for map component)
+    emitNewUpdatedObject(obj) {
         this.updateorg.next(obj.response.lastImported);
     }
 
-    cancel(){
+    // cancel editing orgUnit
+    cancel() {
         this.editmode = false;
         this.tempmarker.next(null);
     }
 
-
-    add(data){
+    // Prepare sidebar for adding new object. Receiving location and parent for the new orgUnit
+    add(data) {
         this.coordinatePoint = true;
-        this.newObject=true;
+        this.newObject = true;
         this.active = true;
         this.editmode = true;
 
-        for(control in this.form.controls){
-            if(!(this.form.controls[control] instanceof ControlArray))
+        for (control in this.form.controls) {
+            if (!(this.form.controls[control] instanceof ControlArray))
                 this.form.controls[control].updateValue("");
         }
 
@@ -249,11 +242,13 @@ export class Sidebar {
 
     }
 
-    exit(){
+    // dismiss sidebar
+    exit() {
         this.active = false;
     }
 
-    findOrgUnitSets(){
+    // dynamically find all orgUnitSets for populating input selects
+    findOrgUnitSets() {
         let instance = this;
         this.http.get(dhisAPI + "/api/organisationUnitGroupSets?paging=false")
             .map(res => res.json())
@@ -261,10 +256,10 @@ export class Sidebar {
             .subscribe(res => this.addOrgUnitSets(instance, res))
     }
 
-    addOrgUnitSets(instance, res){
+    // adding the orgUnitSets from the api
+    addOrgUnitSets(instance, res) {
         //delete instance.ctrlGroups[0];
-        for( group in res){
-            console.log(instance.form.controls);
+        for (group in res) {
             instance.groupsArray.push(new Control(''));
             instance.groupSets.push(res[group]);
 
@@ -273,19 +268,16 @@ export class Sidebar {
                 .map(res => res.organisationUnitGroups)
                 .subscribe(res => this.addOrgUnitGroup(instance, res))
 
-            //    instance.form.push(new Control(""));
         }
-        console.log(instance.groupSets);
     }
 
-    addOrgUnitGroup(instance, res){
-        let ar: Array<any> = [];
-        for( group in res){
+    // add orgUnitGroup as option to input selects
+    addOrgUnitGroup(instance, res) {
+        let ar:Array<any> = [];
+        for (group in res) {
             ar.push(res[group]);
         }
         instance.groupsDoubleArray.push(ar);
-
-        console.log(instance.groupsDoubleArray);
     }
 }