[sidebar] Tried to add organisationUnitGroups, unsuccessful addedit
authorErik Haider Forsén <erikhf@ifi.uio.no>
Sun, 6 Dec 2015 16:26:25 +0000 (17:26 +0100)
committerErik Haider Forsén <erikhf@ifi.uio.no>
Sun, 6 Dec 2015 16:26:25 +0000 (17:26 +0100)
package.json
src/components/sidebar/sidebar.html
src/components/sidebar/sidebar.ts

index aaef61f..9e6b6ec 100644 (file)
@@ -16,7 +16,7 @@
   "license": "ISC",
   "dependencies": {
     "@reactivex/rxjs": "^5.0.0-alpha.10",
-    "angular2": "2.0.0-alpha.44",
+    "angular2": "2.0.0-alpha.48",
     "systemjs": "0.19.2"
   },
   "devDependencies": {
index f019dd8..ba5e4ee 100644 (file)
@@ -1,6 +1,19 @@
 <div>
     <div [hidden]="!active" class="form-background" style="overflow-y:auto; position:relative; float:right; ">
-        <form *ng-if="editmode" [(ng-form-model)]="form" (ng-submit)="onSubmit()" #of="form">
+
+
+        <form [ng-form-model]="form">
+
+        </form>
+
+        <form [hidden]="!editmode" [ng-form-model]="form" (ng-submit)="onSubmit()" #of="form">
+
+<!--
+                <ul ng-control-group="groups">
+                    <li ng-for="#ctrl of groupsArray; #i = index"><input ng-control="{{i}}">
+                    </li>
+                </ul>
+-->
             <h2>{{newObject?"Add new Organizational Unit":"Update Organizational Unit"}}</h2>
 
             <div class="form-group">
                 </div>
             </fieldset>
 
+            <fieldset ng-control-group="organisationUnitGroups">
+                <legend>Groups</legend>
+                <div class="form-group" *ng-for="#groupSet of groupSets; #i = index">
+
+                    <label>{{groupSet.name}}</label>
+                    <select ng-control="{{i}}" class="form-control" disabled>
+                        <option>-------- not applicable --------</option>
+                        <option *ng-for="#group of groupsDoubleArray[i]">{{group.name}}</option>
+                    </select>
+                </div>
+            </fieldset>
 
             <fieldset class="form-group">
                 <legend>Contact information</legend>
 
         </form>
 
+        <!--
 
+        <form [ng-form-model]="form">
+            <ul ng-control-group="groups">
+                <li *ng-for="#group of ctrlGroups; #i = index"><input ng-control="{{i}}"></li>
+            </ul>
+        </form>
+-->
         <section *ng-if="!editmode">
             <h2>Organisation Unit</h2>
             <label for="actOrgUnitName">Name</label>
index 900b089..6e2beaa 100644 (file)
@@ -28,13 +28,16 @@ declare var zone: Zone;
 
 export class Sidebar {
 
-    form: ControlGroup;
+
     http:Http;
     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);
@@ -52,7 +55,30 @@ export class Sidebar {
     phoneNumber: Control = new Control("");
     featureType: Control = new Control("");
     coordinates: Control = new Control("");
-
+    ctrlGroups: Control[]= [new Control('')];
+    groupsArray: ControlArray = new ControlArray(this.ctrlGroups);
+
+
+    form: ControlGroup = new ControlGroup({
+        organisationUnitGroups: this.groupsArray,
+        id: this.id,
+        name: this.name,
+        shortName: this.shortName,
+        description: this.description,
+        code: this.code,
+        openingDate: this.openingDate,
+        closedDate: this.closedDate,
+        url: this.url,
+        lat: this.lat,
+        lng: this.lng,
+        parent: this.parent,
+        contactPerson: this.contactPerson,
+        address: this.address,
+        email: this.email,
+        phoneNumber: this.phoneNumber,
+        featureType: this.featureType,
+        coordinates: this.coordinates
+    });
 
     constructor(http:Http, fb: FormBuilder) {
         this.http = http;
@@ -61,25 +87,6 @@ export class Sidebar {
         this.coordinatePoint = false;
         this.tempmarker = new EventEmitter();
 
-        this.form = fb.group({
-            "id": this.id,
-            "name": this.name,
-            "shortName": this.shortName,
-            "description": this.description,
-            "code": this.code,
-            "openingDate": this.openingDate,
-            "closedDate": this.closedDate,
-            "url": this.url,
-            "lat": this.lat,
-            "lng": this.lng,
-            "parent": this.parent,
-            "contactPerson": this.contactPerson,
-            "address": this.address,
-            "email": this.email,
-            "phoneNumber": this.phoneNumber,
-            "featureType": this.featureType,
-            "coordinates": this.coordinates
-        });
         let instance = this;
         this.lat.valueChanges.observer({
             next: (value) => {
@@ -97,6 +104,9 @@ export class Sidebar {
                 }
             }
         });
+
+        this.findOrgUnitSets();
+
     }
 
     update(orgunitId) {
@@ -111,11 +121,15 @@ export class Sidebar {
     updateValues(res){
 
         for(control in this.form.controls){
-            if(res[control] !== undefined) {
+            if(this.form.controls[control] instanceof ControlArray){
+                console.log("nothing to do here");
+            }
+            else if(res[control] !== undefined) {
                 this.form.controls[control].updateValue(res[control]);
             }
             else
                 this.form.controls[control].updateValue("");
+
         }
 
         // Date fix:
@@ -136,6 +150,18 @@ export class Sidebar {
         else{
             this.coordinatePoint = false;
         }
+
+        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);
+                    }
+                }
+            }
+        }
+
+        console.log("faenskap");
     }
 
 
@@ -156,6 +182,15 @@ export class Sidebar {
             }
         });
 
+        $.each(jsonObject.organisationUnitGroups, function(key, value){
+            if( value === "" || value === null){
+                delete jsonObject.organisationUnitGroups[key];
+            } else {
+                jsonObject.organisationUnitGroups[key].id = value;
+            }
+        });
+
+
         jsonObject.openingDate = (new Date(this.form.value.openingDate)).toISOString();
 
         if(this.form.value.closedDate){
@@ -199,7 +234,8 @@ export class Sidebar {
         this.editmode = true;
 
         for(control in this.form.controls){
-            this.form.controls[control].updateValue("");
+            if(!(this.form.controls[control] instanceof ControlArray))
+                this.form.controls[control].updateValue("");
         }
 
         this.form.controls.lat.updateValue(data.location.lat);
@@ -207,5 +243,40 @@ export class Sidebar {
         this.form.controls.parent.updateValue(data.parent);
 
     }
+
+    findOrgUnitSets(){
+        let instance = this;
+        this.http.get(dhisAPI + "/api/organisationUnitGroupSets?paging=false")
+            .map(res => res.json())
+            .map(res => res.organisationUnitGroupSets)
+            .subscribe(res => this.addOrgUnitSets(instance, res))
+    }
+
+    addOrgUnitSets(instance, res){
+        instance.ctrlArray.removeAt(0);
+        for( group in res){
+            console.log(instance.form.controls);
+            instance.groupsArray.push(new Control(''));
+            instance.groupSets.push(res[group]);
+
+            this.http.get(dhisAPI + "/api/organisationUnitGroupSets/" + res[group].id)
+                .map(res => res.json())
+                .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){
+            ar.push(res[group]);
+        }
+        instance.groupsDoubleArray.push(ar);
+
+        console.log(instance.groupsDoubleArray);
+    }
 }