Make the show spaces function of the sql results page work
authorKarl O. Pinc <kop@karlpinc.com>
Wed, 25 Sep 2024 03:56:44 +0000 (22:56 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Wed, 25 Sep 2024 03:56:44 +0000 (22:56 -0500)
MANIFEST.in
src/pgwui_sql/static/pgwui_sql.css
src/pgwui_sql/static/pgwui_sql.js [new file with mode: 0644]
src/pgwui_sql/static/show_whitespace.css [new file with mode: 0644]
src/pgwui_sql/templates/sql.mak

index 53186a642cb5f541266c1d94ff8777fc19611d79..444fd9e2cf4d070c88a6defcc450d43e162f8e33 100644 (file)
@@ -6,5 +6,6 @@ include LICENSE.txt
 include Makefile
 include src/pgwui_sql/VERSION
 include src/pgwui_sql/static/*.css
+include src/pgwui_sql/static/*.js
 include src/pgwui_sql/templates/*.mak
 include tox.ini
index 458e3b93ce7fe66ca45307138dc24c9c73057eb8..bb547b4edeeff5dfbeebca578cfa398633c9f869 100644 (file)
@@ -1,6 +1,6 @@
 /* Copyright (C) 2024 The Meme Factory, Inc.  http://www.karlpinc.com/
  *
- * This file is part of PGWUI_Common.
+ * This file is part of PGWUI_SQL.
  *
  * This program is free software: you can redistribute it and/or
  * modify it under the terms of the GNU Affero General Public License
@@ -16,7 +16,7 @@
  * License along with this program.  If not, see
  * <http://www.gnu.org/licenses/>.
  *
- * Required style sheet for PGWUI programs.
+ * Required style sheet for PGWUI_SQL.
  *
  * Karl O. Pinc <kop@karlpinc.com>
  */
diff --git a/src/pgwui_sql/static/pgwui_sql.js b/src/pgwui_sql/static/pgwui_sql.js
new file mode 100644 (file)
index 0000000..66e1728
--- /dev/null
@@ -0,0 +1,54 @@
+/* Copyright (C) 2024 The Meme Factory, Inc.  http://www.karlpinc.com/
+     This file is part of PGWUI_SQL.
+    
+     This program is free software: you can redistribute it and/or
+     modify it under the terms of the GNU Affero General Public License
+     as published by the Free Software Foundation, either version 3 of
+     the License, or (at your option) any later version.
+    
+     This program is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public
+     License along with this program.  If not, see
+     <http://www.gnu.org/licenses/>.
+
+  Javascript for the sql results window.
+  Karl O. Pinc <kop@karlpinc.com>
+
+  See sql.mak for the script that defines
+*/
+
+function openSqlEdit() {
+         window.open(
+              pgwuiSqlEditRoute(),
+              'sql_edit',
+              'popup=true,width=' + window.innerWidth
+                + ',height=' + window.innerHeight
+                + ',left=' + (window.screenLeft + window.outerWidth)
+                + ',top=' + window.screenTop)
+         .focus();
+}
+function whitespaceDisplay(checked) {
+         if (checked) {
+           globalThis.tdSqltextSheet.disabled = false;
+         } else {
+           globalThis.tdSqltextSheet.disabled = true;
+         }
+}
+function getTdSqltextSheet() {
+    const url = pgwuiSqlShowWhitespaceURL();
+    for (const sheet of document.styleSheets) {
+        if (sheet.href === url) {
+            return sheet;
+        }}}
+
+globalThis.tdSqltextSheet = getTdSqltextSheet();
+const show_spaces = document.getElementById('show_spaces_id');
+if (show_spaces !== null) {
+    whitespaceDisplay(show_spaces.checked);
+}
diff --git a/src/pgwui_sql/static/show_whitespace.css b/src/pgwui_sql/static/show_whitespace.css
new file mode 100644 (file)
index 0000000..69c86f2
--- /dev/null
@@ -0,0 +1,27 @@
+/* Copyright (C) 2024 The Meme Factory, Inc.  http://www.karlpinc.com/
+ *
+ * This file is part of PGWUI_SQL.
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this program.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Style sheet that shows whitespace in the SQL results
+ *
+ * Karl O. Pinc <kop@karlpinc.com>
+ */
+
+td.sqltext { text-decoration-line: underline;
+             text-decoration-color: silver;
+             text-decoration-style: double; }
+
index 18adeba8afe5d66828c8764a4e886dc0f01dcfc8..0c11e8b8bea1ad50eb21066dbaea6203457f7db2 100644 (file)
 
 <%inherit file="${sql_base_mak}" />
 
+<script>
+  // Functions for delivering "static" content that comes from templating.
+  function pgwuiSqlEditRoute() {
+           return '${request.route_path("pgwui_sql_edit")|n}';
+  }
+  function pgwuiSqlShowWhitespaceURL() {
+           return '${request.static_url(
+                         "pgwui_sql:static/show_whitespace.css")}';
+  }
+  // Set window name so that the sql_edit window can vary the target
+  // by db/user/etc. and have multiple result windows open at once.
+  window.name='${self.sql_results_target()}';
+</script>
+
 <%block name="title" filter="trim">
   % if havecreds:
       sql -- Results
   % endif
 </%block>
 
+<%block name="stylesheet_links">
+  ${parent.stylesheet_links()}
+  <link rel="stylesheet"
+        href="${request.static_url('pgwui_sql:static/show_whitespace.css')}"
+        type="text/css" />
+</%block>
+
 <%block name="action_success">
   <p><em class="success">No errors</em>${
       f', from file ({filename})' if upload_sql else ''
       </self.lib:td_label>
       <self.lib:td_input tab_index="${tab_index}">
         <input name="show_spaces"
+               id="show_spaces_id"
                tabindex="${tab_index.val}"
                type="checkbox"
                ${show_spaces | n}
   </form>
 </%def>
 
-<script>
-  window.name='${self.sql_results_target()}';
-  function open_sql_edit() {
-           window.open(
-                '${request.route_path("pgwui_sql_edit")|n}',
-                'sql_edit',
-                'popup=true,width=' + window.innerWidth
-                  + ',height=' + window.innerHeight
-                  + ',left=' + (window.screenLeft + window.outerWidth)
-                  + ',top=' + window.screenTop)
-           .focus();
-  }
-  function whitespaceDisplay(checked) {
-           const tdSqltextSheet = document.adoptedStyleSheets[0];
-
-           if (checked) {
-             tdSqltextSheet.disabled = false;
-           } else {
-             tdSqltextSheet.disabled = true;
-           }
-  }
-
-  // See: https://web.dev/articles/constructable-stylesheets
-  function setupCSS() {
-    tdSqltextSheet = new CSSStyleSheet(disabled=true);
-    tdSqltextSheet.replaceSync(
-      'td.sqltext {
-         text-decoration-line: underline;
-         text-decoration-color: silver;
-         text-decoration-style: double; }');
-    document.adoptedStyleSheets = [tdSqltextSheet];
-  }
-
-  if (document.readyState === "loading") {
-    // Loading hasn't finished yet
-    document.addEventListener("DOMContentLoaded", setupCSS);
-  } else {
-    // `DOMContentLoaded` has already fired
-    setupCSS();
-  }
-</script>
-
 <noscript>
   <p class="error">ALERT: This page requires JavaScript be turned on.</p>
 </noscript>
      <input value="Login" tabindex="${tab_index.val}" type="submit" />
    % else:
      <button type="button" tabindex="${tab_index.val}"
-             onclick="open_sql_edit();"
+             onclick="openSqlEdit();"
      >New SQL Entry Window</button>
    % endif
    <% tab_index.inc() %>
 <% tab_index = self.attr.TabIndex() %>
 ${render_results()}
 ${self.main_form(tab_index)}
+
+<script defer
+        src="${request.static_url('pgwui_sql:static/pgwui_sql.js')}">
+</script>