ソースを参照

use json records to override filter

Daniel Sheffield 8 ヶ月 前
コミット
0478cf98d9

+ 9 - 9
sqlpage/grocery/categories.sql

@@ -1,11 +1,11 @@
 SET title = 'Categories';
-SET filter_config = '{
-    "log_scale": {"type": "hidden"},
-    "start": {"type": "hidden"},
-    "end": {"type": "hidden"},
-    "unit_mass": {"type": "hidden"},
-    "unit_volume": {"type": "hidden"},
-    "unit_count[]": {"type": "hidden"},
-    "tags[]": {"type": "hidden"}
-}';
+SET filter_config = '[
+  {"name": "tags[]", "type": "hidden"},
+  {"name": "log_scale", "type": "hidden"},
+  {"name": "start", "type": "hidden"},
+  {"name": "end", "type": "hidden"},
+  {"name": "unit_mass", "type": "hidden"},
+  {"name": "unit_volume", "type": "hidden"},
+  {"name": "unit_count[]", "type": "hidden"}
+]';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 9 - 9
sqlpage/grocery/groups.sql

@@ -1,11 +1,11 @@
 SET title = 'Groups';
-SET filter_config = '{
-    "log_scale": {"type": "hidden"},
-    "start": {"type": "hidden"},
-    "end": {"type": "hidden"},
-    "unit_mass": {"type": "hidden"},
-    "unit_volume": {"type": "hidden"},
-    "unit_count[]": {"type": "hidden"},
-    "tags[]": {"type": "hidden"}
-}';
+SET filter_config = '[
+  {"name": "tags[]", "type": "hidden"},
+  {"name": "log_scale", "type": "hidden"},
+  {"name": "start", "type": "hidden"},
+  {"name": "end", "type": "hidden"},
+  {"name": "unit_mass", "type": "hidden"},
+  {"name": "unit_volume", "type": "hidden"},
+  {"name": "unit_count[]", "type": "hidden"}
+]';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 9 - 9
sqlpage/grocery/products.sql

@@ -1,11 +1,11 @@
 SET title = 'Products';
-SET filter_config = '{
-    "log_scale": {"type": "hidden"},
-    "start": {"type": "hidden"},
-    "end": {"type": "hidden"},
-    "unit_mass": {"type": "hidden"},
-    "unit_volume": {"type": "hidden"},
-    "unit_count[]": {"type": "hidden"},
-    "tags[]": {"type": "hidden"}
-}';
+SET filter_config = '[
+  {"name": "tags[]", "type": "hidden"},
+  {"name": "log_scale", "type": "hidden"},
+  {"name": "start", "type": "hidden"},
+  {"name": "end", "type": "hidden"},
+  {"name": "unit_mass", "type": "hidden"},
+  {"name": "unit_volume", "type": "hidden"},
+  {"name": "unit_count[]", "type": "hidden"}
+]';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 8 - 6
sqlpage/grocery/tags.sql

@@ -1,8 +1,10 @@
 SET title = 'Tags';
-SET filter_config = '{
-    "log_scale": {"type": "hidden"},
-    "unit_mass": {"type": "hidden"},
-    "unit_volume": {"type": "hidden"},
-    "unit_count[]": {"type": "hidden"}
-}';
+SET filter_config = '[
+  {"name": "start", "width": 4},
+  {"name": "end", "width": 4},
+  {"name": "log_scale", "type": "hidden"},
+  {"name": "unit_mass", "type": "hidden"},
+  {"name": "unit_volume", "type": "hidden"},
+  {"name": "unit_count[]", "type": "hidden"}
+]';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 5 - 3
sqlpage/grocery/transactions.sql

@@ -1,5 +1,7 @@
 SET title = 'Transactions';
-SET filter_config = '{
-    "log_scale": {"type": "hidden"}
-}';
+SET filter_config = '[
+  {"name": "start", "width": 6},
+  {"name": "end", "width": 6},
+  {"name": "log_scale", "type": "hidden"}
+]';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 5 - 0
sqlpage/grocery/trend.sql

@@ -1,2 +1,7 @@
 SET title = 'Trend';
+SET filter_config = '[
+  {"name": "start", "width": 4},
+  {"name": "end", "width": 4},
+  {"name": "log_scale", "width": 4}
+]';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 5 - 3
sqlpage/grocery/volume.sql

@@ -1,5 +1,7 @@
 SET title = 'Volume';
-SET filter_config = '{
-    "log_scale": {"type": "hidden"}
-}';
+SET filter_config = '[
+  {"name": "start", "width": 6},
+  {"name": "end", "width": 6},
+  {"name": "log_scale", "type": "hidden"}
+]';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 2 - 2
sqlpage/sqlpage/internal/entry.sql

@@ -1,5 +1,5 @@
-SET apply = CASE WHEN (COALESCE($apply, '') = 'Apply') THEN TRUE ELSE FALSE END;
-SET clear = CASE WHEN (COALESCE($clear, '') = 'Clear') THEN TRUE ELSE FALSE END;
+SET apply = CASE COALESCE($apply, '') WHEN 'Apply' THEN TRUE ELSE FALSE END;
+SET clear = CASE COALESCE($clear, '') WHEN 'Clear' THEN TRUE ELSE FALSE END;
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/cookie.sql') AS properties;
 SET products = sqlpage.cookie('products')::json;
 SET categories = sqlpage.cookie('categories')::json;

+ 16 - 25
sqlpage/sqlpage/internal/nav.sql

@@ -1,9 +1,3 @@
-SET input_log_scale = COALESCE($input_log_scale, 'checkbox');
-SET input_start = COALESCE($input_start, 'date');
-SET input_end = COALESCE($input_end, 'date');
-SET input_unit_mass = COALESCE($input_unit_mass, 'select');
-SET input_unit_volume = COALESCE($input_unit_volume, 'select');
-SET input_unit_count = COALESCE($input_unit_count, 'select');
 SELECT 'tab' AS component, TRUE AS center;
 
 SELECT title
@@ -26,29 +20,26 @@ SELECT 'hidden' AS type
 , $title AS value
 ;
 
-SELECT v#>>'{name}' AS name
+SELECT j.name
 --, fo.j#>>'{options}' AS label
-, v#>>'{label}' AS label
-, COALESCE(c.value#>>'{type}', v#>>'{type}') AS type
-, v#>>'{dropdown}' AS dropdown
-, v#>>'{multiple}' AS multiple
-, v#>>'{width}' AS width
+, j.label
+, COALESCE(c.type, j.type) AS type
+, j.dropdown AS dropdown
+, j.multiple
+, COALESCE(c.width, j.width) AS width
 , o.j#>'{options}' AS options
-, CASE v#>>'{type}'
-  WHEN 'checkbox' THEN 'true'
-  ELSE v.value#>>'{}'
-  END AS value
-, CASE v#>>'{type}'
-  WHEN 'checkbox' THEN (v.value#>>'{}')::bool
-  ELSE NULL
-END AS checked
-FROM json_array_elements(sqlpage.read_file_as_text('sqlpage/json/filters.json')::json) j(v)
-LEFT JOIN json_each($filter_config::json) c(k,value)
-ON c.k = (v#>>'{name}')
+, COALESCE(v.value#>>'{}', j.value) AS value
+, CASE j.type
+    WHEN 'checkbox' THEN (v.value#>>'{}')::bool
+    ELSE NULL
+  END AS checked
+FROM json_populate_recordset(null::sqlpage_filter_type, sqlpage.read_file_as_text('sqlpage/json/filters.json')::json) j
+LEFT JOIN json_populate_recordset(null::sqlpage_filter_type, $filter_config::json) c
+USING (name)
 LEFT JOIN json_array_elements($filter_options::json) o(j)
-ON (o.j#>>'{name}') = (v#>>'{name}')
+ON (o.j#>>'{name}') = j.name
 LEFT JOIN json_each(sqlpage.variables()::json) v(key, value)
-ON v.key = (v#>>'{name}')
+ON v.key = j.name
 ;
 
 SELECT 'Apply' AS value

+ 3 - 3
sqlpage/sqlpage/json/filters.json

@@ -15,11 +15,11 @@
     "type": "select", "dropdown": true, "multiple": true,
     "width": 4
   },
-  { "name": "unit_volume", "label": "Unit (vol.)",
+  { "name": "unit_volume", "label": "Unit (vol.)", "default": "L",
     "type": "select", "multiple": false, "dropdown": true,
     "width": 2
   },
-  { "name": "unit_mass", "label": "Unit (mass)",
+  { "name": "unit_mass", "label": "Unit (mass)", "default": "kg",
     "type": "select", "multiple": false, "dropdown": true,
     "width": 2
   },
@@ -35,7 +35,7 @@
     "type": "date",
     "width": 2
   },
-  { "name": "log_scale", "label": "Log Scale",
+  { "name": "log_scale", "label": "Log Scale", "value": "true",
     "type": "checkbox",
     "width": 2
   }

+ 10 - 0
sqlpage/sqlpage/migrations/001_filter_type.sql

@@ -0,0 +1,10 @@
+CREATE TYPE sqlpage_filter_type AS (
+  name text,
+  label text,
+  value text,
+  "default" text,
+  type text,
+  dropdown bool,
+  multiple bool,
+  width int
+);