changeset 55:308c96f734c8 ng2-table

first steps to importing state from URL.
author casties
date Mon, 27 Mar 2017 14:17:55 +0200
parents 4341c1b4e3ae
children b22e52a128a8
files src/app/query-app.component.ts src/app/query-mode.ts src/app/query-state.ts src/app/query.service.ts
diffstat 4 files changed, 67 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/query-app.component.ts	Thu Mar 23 18:57:01 2017 +0100
+++ b/src/app/query-app.component.ts	Mon Mar 27 14:17:55 2017 +0200
@@ -1,5 +1,4 @@
 import {Component} from '@angular/core';
-//import {HTTP_PROVIDERS} from '@angular/http';
 
 import {QueryState} from './query-state';
 import {QueryStep} from './query-step';
@@ -7,10 +6,6 @@
 import {QueryService} from './query.service';
 import {NormalizationService} from './normalization.service';
 
-import {QuerySelectComponent} from './query-select.component';
-import {QueryResultComponent} from './query-result.component';
-//import {QueryResultTableComponent} from './query-result-table.component';
-
 @Component({
     selector: 'query-app',
     template: `
@@ -34,8 +29,6 @@
         </query-result>
     </div>
         `,
-    //directives: [QuerySelectComponent, QueryResultTableComponent],
-    //providers: [QueryService, NormalizationService, HTTP_PROVIDERS]
 })
     
 export class QueryAppComponent { 
@@ -43,11 +36,27 @@
     public queryStepList: string[];
         
     constructor(private _queryService: QueryService) {
-        this._queryService.setup();
+        console.debug("QueryAppComponent constructor!");
+        let newState = this.getStateStringFromFragment();
+        this._queryService.setup(newState);
         this.queryStepList = [];
+        if (this._queryService.state.getNumSteps() > 0) {
+            this._queryService.state.steps
+                .forEach((elem) => this.queryStepList.push('param'));
+        }
         this.addQueryStep();
     }
     
+    getStateStringFromFragment(): string {
+        let hash: string = window.location.hash;
+        if (hash) {
+            let frag: string = hash.substr(1);
+            // base64 decode
+            return frag;
+        }
+        return null;
+    }
+    
     addQueryStep() {
         this.queryStepList.push('step');
     }
--- a/src/app/query-mode.ts	Thu Mar 23 18:57:01 2017 +0100
+++ b/src/app/query-mode.ts	Mon Mar 27 14:17:55 2017 +0200
@@ -3,6 +3,10 @@
     label: string;
 }
 
+export function getQueryModeById(id: string) {
+    return QUERY_MODES.find((elem) => elem.id === id);
+}
+
 export var QUERY_MODES: QueryMode[] = [
     {id: 'type_is', label:'Object type is'},
     {id: 'att_contains', label: 'Attribute (contains)'},
--- a/src/app/query-state.ts	Thu Mar 23 18:57:01 2017 +0100
+++ b/src/app/query-state.ts	Mon Mar 27 14:17:55 2017 +0200
@@ -1,5 +1,6 @@
 import {QueryStep} from './query-step';
 import {ResultType} from './result-type';
+import {QueryMode, getQueryModeById} from './query-mode';
 
 export class QueryState {
     public steps: QueryStep[] = [];
@@ -15,10 +16,45 @@
     public resultTypes: string;
     public resultType: ResultType;
     public resultInfo: string;
-    public resultAttributes: string[];    
+    public resultAttributes: string[];
     public resultRelations: any[];
     public resultColumns: any[];
     
+
+    setStateFromString(newStateString: string) {
+        try {
+            // state string is json
+            let newState = JSON.parse(newStateString);
+            // state should be list of steps
+            if (!Array.isArray(newState)) return;
+            let newSteps: QueryStep[] = [];
+            newState.forEach((elem) => {
+                // step is an array [mode, params]
+                if (!Array.isArray(elem)) return;
+                let mode = elem[0];
+                let qm: QueryMode = getQueryModeById(mode);
+                let params = elem[1];
+                if (qm != null && params != null) {
+                    let qs = new QueryStep(qm, params);
+                    newSteps.push(qs);
+                }
+            });
+            if (newSteps.length > 0) {
+                // set state
+                this.steps = newSteps;
+            }
+        } catch (e) {
+            console.error("Unable to set state from string: "+newStateString);
+        }
+    }
+
+    getNumSteps() {
+        return this.steps.length;
+    }
+    
+    /**
+     * Returns the cypher query as text for display.
+     */
     getQueryText() {
         let text = this.resultCypherQuery;
         let hasParams = false;
--- a/src/app/query.service.ts	Thu Mar 23 18:57:01 2017 +0100
+++ b/src/app/query.service.ts	Mon Mar 27 14:17:55 2017 +0200
@@ -25,8 +25,13 @@
         this.state = new QueryState();
     }
     
-    setup() {
+    setup(newStateString: string) {
+        // get list of object types
         this.setupObjectTypes();
+        // get state from string
+        if (newStateString) {
+            this.state.setStateFromString(newStateString);
+        }
     }
     
     getState() {
@@ -74,7 +79,9 @@
         res.subscribe(
             data => {
                 console.debug("neo4j data=", data);
-                this.objectTypes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_");
+                this.objectTypes = data.results[0].data
+                    .map(elem => elem.row[0])
+                    .filter(elem => elem[0] != "_");
                 console.debug("object types=", this.objectTypes);
                 },
             err => console.error("neo4j error=", err),