diff src/app/query-select.component.ts @ 58:3b4046e0cc02 default

Merge from ng2-table branch. d7c947909ab888c013171b8c037e4f9fab30fe57
author casties
date Wed, 29 Mar 2017 17:19:12 +0200
parents d7c947909ab8
children 6adf95d9a190
line wrap: on
line diff
--- a/src/app/query-select.component.ts	Mon Mar 20 18:50:31 2017 +0100
+++ b/src/app/query-select.component.ts	Wed Mar 29 17:19:12 2017 +0200
@@ -17,7 +17,8 @@
     <form (ngSubmit)="onSubmit()">
         <select name="mode" (change)="onSelectMode($event)">
             <option></option>
-            <option *ngFor="let mode of getQueryModes()" [value]="mode.id">
+            <option *ngFor="let mode of getQueryModes()" 
+                [selected]="mode.id==selectedMode?.id" [value]="mode.id">
                 {{mode.label}}
             </option>
         </select>
@@ -80,9 +81,9 @@
     public queryStep: string;
     public index: number;
     public resultInfo: string;
-    public queryModes: QueryMode[];
+    public queryModes: Array<QueryMode>;
     public selectedMode: QueryMode;
-    public queryOptions: string[];
+    public queryOptions: any[];
     public selectedOption: string;
     public queryInput: string;
     public queryInput2: string;
@@ -98,9 +99,9 @@
     
     setup() {
         console.log("query-select setup step=", this.queryStep);
-        var step = this._queryService.state.steps[this.index-1];
+        var step = this._queryService.state.steps[this.index]; // i-1?
         if (step != null) {
-            this.resultInfo = step.resultInfo;
+            this.setQueryStep(step);
         }
     }
     
@@ -124,8 +125,26 @@
     
     onSubmit() {
         console.debug("Submit! selectedMode=", this.selectedMode, " selectedOption=", this.selectedOption, " queryInput=", this.queryInput);
-        var step: QueryStep;
-        if (this.selectedMode.id == 'type_is') {
+    
+        let step = this.getQueryStep();
+
+        /*
+         * set step and submit change event
+         */
+        if (step != null) {
+            this._queryService.setQueryStep(this.index, step);
+            this.queryChanged.emit(this._queryService.getState());
+        }
+        return false;
+    }
+    
+    /**
+     * Returns QueryStep from current form state.
+     */
+    getQueryStep(): QueryStep {
+        let step: QueryStep = null;
+
+        if (this.selectedMode.id === 'type_is') {
             /*
              * type_is
              */
@@ -133,7 +152,7 @@
             if (opt) {
                 step = new QueryStep(this.selectedMode, {'objectType': opt});
             }
-        } else if (this.selectedMode.id == 'relation_is') {
+        } else if (this.selectedMode.id === 'relation_is') {
             /*
              * relation_is
              */
@@ -142,7 +161,7 @@
                 let rel = getRelationByName(opt);
                 step = new QueryStep(this.selectedMode, {'relationType': rel});
             }
-        } else if (this.selectedMode.id == 'id_is') {
+        } else if (this.selectedMode.id === 'id_is') {
             /*
              * id is
              */
@@ -150,7 +169,7 @@
             if (val) {
                 step = new QueryStep(this.selectedMode, {'value': val});
             }
-        } else if (this.selectedMode.id == 'att_contains') {
+        } else if (this.selectedMode.id === 'att_contains') {
             /*
              * att_contains
              */
@@ -159,7 +178,7 @@
             if (att && val) {
                 step = new QueryStep(this.selectedMode, {'attribute': att, 'value': val});
             }
-        } else if (this.selectedMode.id == 'att_num_range') {
+        } else if (this.selectedMode.id === 'att_num_range') {
             /*
              * att_num_range
              */
@@ -169,7 +188,7 @@
             if (att && nlo && nhi) {
                 step = new QueryStep(this.selectedMode, {'attribute': att, 'numLo': nlo, 'numHi': nhi});
             }
-        } else if (this.selectedMode.id == 'att_contains_norm') {
+        } else if (this.selectedMode.id === 'att_contains_norm') {
             /*
              * att_contains_norm
              * 
@@ -191,17 +210,71 @@
                     err => console.error("openmind norm error=", err),
                     () => console.debug("openmind norm query Complete")
                 );
-                // query has not been set yet
-                return;
+                // query has not been set yet (gets set in callback)
+                return null;
            }
         }
-
-        /*
-         * set step and submit change event
+        return step;
+    }
+    
+    /**
+     * Sets form state from given QueryStep.
+     */
+    setQueryStep(step: QueryStep) {
+        let mode = step.mode;
+        this.selectedMode = mode;
+        if (mode.id === 'id_is') {
+            /*
+         * id_is
          */
-        if (step != null) {
-            this._queryService.setQueryStep(this.index, step);
-            this.queryChanged.emit(this._queryService.getState());
+            this.queryInput = step.params.value;
+            
+        } else if (mode.id === 'type_is') {
+            /*
+             * type_is
+             */
+            let name = step.params.objectType;
+            this.queryOptions = [name];
+            this.selectedOption = name;
+            
+        } else if (this.selectedMode.id === 'relation_is') {
+            /*
+             * relation_is
+             */
+            let name = step.params.relationType.name
+            let rel = getRelationByName(name);
+            this.queryOptions = [rel];
+            this.selectedOption = name;
+            
+        } else if (this.selectedMode.id === 'att_contains') {
+            /*
+             * att_contains
+             */
+            let name = step.params.attribute
+            this.queryOptions = [name];
+            this.selectedOption = name;
+            this.queryInput = step.params.value;
+            
+        } else if (this.selectedMode.id === 'att_num_range') {
+            /*
+             * att_num_range
+             */
+            let name = step.params.attribute
+            this.queryOptions = [name];
+            this.selectedOption = name;
+            this.queryInput = step.params.numLo;
+            this.queryInput2 = step.params.numHi;
+            
+        } else if (this.selectedMode.id === 'att_contains_norm') {
+            /*
+             * att_contains_norm
+             */
+            let name = step.params.attribute
+            this.queryOptions = [name];
+            this.selectedOption = name;
+            this.queryInput = step.params.value;
         }
+        // TODO: implement other modes
+        this.resultInfo = step.resultInfo;
     }
 }