diff src/app/query-app.component.ts @ 57:d7c947909ab8 ng2-table

renamed query-app.module to app.module. loading query from url fragment works now.
author casties
date Wed, 29 Mar 2017 17:16:10 +0200
parents b22e52a128a8
children
line wrap: on
line diff
--- a/src/app/query-app.component.ts	Tue Mar 28 20:03:36 2017 +0200
+++ b/src/app/query-app.component.ts	Wed Mar 29 17:16:10 2017 +0200
@@ -20,6 +20,8 @@
         <div>
           <button (click)="addQueryStep()">add step</button>
           <button (click)="removeQueryStep()">remove step</button>
+          <button (click)="resetQuery()">reset query</button>
+          <button (click)="showQueryUrl()">get query url</button>
         </div>
     </div>
     <div class="container">
@@ -37,9 +39,11 @@
         
     constructor(private _queryService: QueryService) {
         console.debug("QueryAppComponent constructor!");
-        let newState = this.getStateStringFromFragment();
+        let newState = this.getStateStringFromUrlFragment();
+        // initialize query service using external state
         this._queryService.setup(newState);
         this.queryStepList = [];
+        // set state in queryStepList
         if (this._queryService.state.getNumSteps() > 0) {
             // use state from URL
             this._queryService.state.steps
@@ -50,17 +54,32 @@
         }
     }
     
-    getStateStringFromFragment(): string {
+    getStateStringFromUrlFragment(): string {
         let hash: string = window.location.hash;
         if (hash) {
-            let frag: string = hash.substr(1);
-            frag = decodeURIComponent(frag);
+            let fragb: string = hash.substr(1);
             // base64 decode
+            let fragu = window.atob(fragb);
+            // url decode
+            let frag = decodeURIComponent(fragu);
+            // reset hash
+            window.location.hash = '';
             return frag;
         }
         return null;
     }
     
+    getUrlFragmentFromState() {
+        let stateStr = this._queryService.state.getStateAsString();
+        let frag = '#';
+        if (stateStr.length > 0) {
+            let fragu = encodeURIComponent(stateStr);
+            let fragb = window.btoa(fragu);
+            frag += fragb;
+        }
+        return frag;
+    }
+    
     addQueryStep() {
         this.queryStepList.push('step');
     }
@@ -70,6 +89,17 @@
         this._queryService.state.steps.pop();
     }
     
+    resetQuery() {
+        // reset everything by reloading
+        window.location.reload();
+    }
+    
+    showQueryUrl() {
+        let url = window.location.href 
+        url = url.replace(/#.*/, '') + this.getUrlFragmentFromState();
+        window.prompt("URL to current query state", url);
+    }
+    
     onQueryChanged(event: any) {
         console.debug("app.onquerychanged! event=", event);
         this._queryService.runQuery();