Commits

Kirill Simonov committed c7f3690

Running a query updates the browser url.

  • Participants
  • Parent commits ab65a9e

Comments (0)

Files changed (4)

src/htsql_tweak/shell/command.py

 
 class ShellCmd(Command):
 
-    def __init__(self, query=None):
+    def __init__(self, query=None, is_implicit=False):
         assert isinstance(query, maybe(str))
+        assert isinstance(is_implicit, bool)
         self.query = query
+        self.is_implicit = is_implicit
 
 
 class EvaluateCmd(Command):
         else:
             query_on_start = '/'
             evaluate_on_start = 'false'
+        implicit = str(self.command.is_implicit).lower()
         data = resource.data
         data = self.patch(data, 'base href', resource_root)
         data = self.patch(data, 'data-database-name', database_name)
         data = self.patch(data, 'data-server-root', server_root)
         data = self.patch(data, 'data-query-on-start', query_on_start)
         data = self.patch(data, 'data-evaluate-on-start', evaluate_on_start)
+        data = self.patch(data, 'data-implicit-shell', implicit)
         status = '200 OK'
         headers = [('Content-Type', 'text/html; charset=UTF-8')]
         body = [data]

src/htsql_tweak/shell/default/act.py

         try:
             syntax = parse(self.command.query)
             if addon.on_root and syntax.segment.branch is None:
-                command = ShellCmd()
+                command = ShellCmd(is_implicit=True)
             else:
                 binding = bind(syntax)
                 command = lookup_command(binding)
                     if (addon.on_default and
                             syntax.segment.branch is not None):
                         query = unquote(self.command.query)
-                        command = ShellCmd(query)
+                        command = ShellCmd(query, is_implicit=True)
                     else:
                         command = DefaultCmd(binding)
             return act(command, self.action)
             if not addon.on_error:
                 raise
             query = unquote(self.command.query)
-            command = ShellCmd(query)
+            command = ShellCmd(query, is_implicit=True)
             return act(command, self.action)
 
 

src/htsql_tweak/shell/static/index.html

   <body data-database-name="HTSQL"
         data-server-root="http://localhost:8080"
         data-query-on-start="/"
-        data-evaluate-on-start="false">
+        data-evaluate-on-start="false"
+        data-implicit-shell="false">
 
     <div id="viewport">
       <div class="heading-area">

src/htsql_tweak/shell/static/shell.js

             databaseName: $body.attr('data-database-name') || "",
             serverRoot: $body.attr('data-server-root') || "",
             queryOnStart: $body.attr('data-query-on-start') || "/",
-            evaluateOnStart: ($body.attr('data-evaluate-on-start') == 'true')
+            evaluateOnStart: ($body.attr('data-evaluate-on-start') == 'true'),
+            implicitShell: ($body.attr('data-implicit-shell') == 'true')
         };
     }
 
     }
 
     function getQuery() {
-        var query = editor.getValue();
-        query = query.replace(/^\s+|\s+$/g, "");
+        var orig_query = editor.getValue();
+        var query = orig_query.replace(/^\s+|\s+$/g, "");
+        if (query != orig_query) {
+            editor.setValue(query);
+        }
         if (!/^\//.test(query)) {
             return;
         }
 
     function clickRun() {
         var query = getQuery();
-        run(query);
+        pushHistory(query)
+        run(query, null);
     }
 
     function clickExpand(dir) {
         $gridHead.scrollLeft($gridBody.scrollLeft());
     }
 
+    function popstateWindow(event) {
+        var data = event.originalEvent.state;
+        if (!data) {
+            return;
+        }
+        var query = data.query;
+        if (query == getQuery()) {
+            return;
+        }
+        if (state.waiting) {
+            return
+        }
+        setQuery(query);
+        if (state.$panel)
+            state.$panel.hide();
+        state.$panel = null;
+        updateTitle();
+    }
+
+    function pushHistory(query, replace) {
+        if (!history.pushState || !history.replaceState)
+            return;
+        if (!query || !config.serverRoot || state.waiting)
+            return;
+        var data = { query: query };
+        var title = 'HTSQL';
+        if (!config.implicitShell) {
+            if (query == '/')
+                query = "/shell()";
+            else
+                query = "/shell('" + query.replace(/'/g,"''") + "')";
+        }
+        var url = config.serverRoot+escape(query);
+        if (replace)
+            history.replaceState(data, title, url);
+        else
+            history.pushState(data, title, url);
+    }
+
     function run(query, action) {
         if (!query)
             return;
     $('#popups').click(clickPopups);
     $('#shrink').click(function() { return clickExpand(-1); });
     $('#expand').click(function() { return clickExpand(+1); });
+    $(window).bind('popstate', popstateWindow);
 
     $('#schema').hide();
     $('#help').hide();
     $('#close-sql').hide();
 
-    $($database).text(config.databaseName);
+    $database.text(config.databaseName);
     updateTitle();
 
+    pushHistory(config.queryOnStart, true);
     if (config.evaluateOnStart) {
-        $('#run').click();
+        run(config.queryOnStart);
     }
 
 });