From 0557ef64e2bd1161fd98a5dff6e45aea8e0d7567 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 30 Jul 2016 15:10:14 -0400 Subject: add site assets and remove set_default --- data/assets/css/admin.css | 2 +- data/assets/js/admin/dialogs/site-add.js | 25 +++++--- data/assets/js/util.js | 4 +- data/init.yaml | 19 ++++++ src/guff/apis.cr | 23 +++++-- src/guff/models/site.cr | 106 ++++++++++++++++++++++++++++--- src/views/dialogs/admin/page-edit.ecr | 4 +- src/views/dialogs/admin/site-add.ecr | 42 +++++++++++- 8 files changed, 199 insertions(+), 26 deletions(-) diff --git a/data/assets/css/admin.css b/data/assets/css/admin.css index ba65585..4ab39b3 100644 --- a/data/assets/css/admin.css +++ b/data/assets/css/admin.css @@ -2,7 +2,7 @@ cursor: pointer; } -#page-edit-styles, #page-edit-scripts { +textarea.asset-list { min-height: 200px; height: 20vh; } diff --git a/data/assets/js/admin/dialogs/site-add.js b/data/assets/js/admin/dialogs/site-add.js index 8cdc772..bf95b8e 100644 --- a/data/assets/js/admin/dialogs/site-add.js +++ b/data/assets/js/admin/dialogs/site-add.js @@ -1,8 +1,12 @@ jQuery(function($) { "use strict"; - var p = '#site-add-', - IDS = ['name', 'slug', 'body', 'lang', 'theme_id']; + var p = '#site-add-'; + + var FIELDS = { + text: ['name', 'slug', 'body', 'lang', 'theme_id'], + list: ['domains', 'styles', 'scripts'], + } var DEFAULTS = { lang: DATA.default_lang, @@ -10,12 +14,14 @@ jQuery(function($) { }; $(p + 'dialog').on('show.bs.modal', function() { - $.each(IDS, function(_, id) { + $.each(FIELDS.text, function(_, id) { $(p + id).val(DEFAULTS[id] || ''); }); - // clear list of domains - $(p + 'domains').val(''); + // clear list textareas + $.each(FIELDS.list, function(_, id) { + $(p + id).val(''); + }); }); $(p + 'dialog').on('shown.bs.modal', function() { @@ -54,13 +60,16 @@ jQuery(function($) { // toggle loading me.toggleClass('disabled').find('.loading').toggleClass('hidden'); - send('site/add', IDS.reduce(function(r, id) { + send('site/add', FIELDS.text.reduce(function(r, id) { r[id] = $(p + id).val(); return r; + }, FIELDS.list.reduce(function(r, id) { + r[id] = listify(p + id); + return r; }, { + // hard-code this for now is_full_feed: 't', - domains: listify(p + 'domains'), - })).always(function() { + }))).always(function() { // toggle loading me.toggleClass('disabled').find('.loading').toggleClass('hidden'); }).fail(function(r) { diff --git a/data/assets/js/util.js b/data/assets/js/util.js index 6379961..a812f1f 100644 --- a/data/assets/js/util.js +++ b/data/assets/js/util.js @@ -39,8 +39,8 @@ jQuery(function($) { // convert textarea to jsonified array of strings window.listify = function(el) { - var r = $(el).val().replace(/^\s+|\s+$/m, ''); - return JSON.stringify((r.length > 0) ? r.split(/\s+/m) : []); + var s = $(el).val().replace(/^\s+|\s+$/m, ''); + return JSON.stringify((s.length > 0) ? s.split(/\s+/m) : []); } diff --git a/data/init.yaml b/data/init.yaml index f9a8ac1..de78d23 100644 --- a/data/init.yaml +++ b/data/init.yaml @@ -266,6 +266,25 @@ init_sql: PRIMARY KEY (site_id, domain) ) + - | + CREATE TABLE site_assets ( + site_id INTEGER NOT NULL + REFERENCES sites(site_id), + + -- asset type + type_id INTEGER NOT NULL + REFERENCES asset_types(type_id), + + -- asset path + path TEXT NOT NULL + CHECK (LENGTH(path) > 0), + + -- load order + sort_order INTEGER NOT NULL, + + PRIMARY KEY (site_id, type_id, path) + ) + - | CREATE TABLE states ( state_id INTEGER PRIMARY KEY, diff --git a/src/guff/apis.cr b/src/guff/apis.cr index 3754545..5449c72 100644 --- a/src/guff/apis.cr +++ b/src/guff/apis.cr @@ -200,6 +200,9 @@ module Guff::APIs # get site_id site_id = params["site_id"].to_i64 + # get site assets + assets = @context.models.site.assets(site_id) + @context.models.site.get(site_id).reduce( {} of String => String ) do |r, kv| @@ -207,6 +210,8 @@ module Guff::APIs r end.merge({ "domains" => @context.models.site.domains(site_id), + "styles" => assets.styles, + "scripts" => assets.scripts, } of String => Array(String)) end @@ -217,9 +222,13 @@ module Guff::APIs slug: params["slug"], lang: params["lang"], body: params["body"], + is_full_feed: (params["is_full_feed"] == "t"), theme_id: params["theme_id"].to_i64, + domains: Array(String).from_json(params["domains"]), + styles: Array(String).from_json(params["styles"]), + scripts: Array(String).from_json(params["scripts"]), ) } end @@ -227,22 +236,28 @@ module Guff::APIs def do_site_set(params : HTTP::Params) @context.models.site.set( site_id: params["site_id"].to_i64, + name: params["name"]?, slug: params["slug"]?, lang: params["lang"]?, body: params["body"]?, is_full_feed: (params.includes?("is_full_key")) ? (params["is_full_feed"] == "t") : nil, theme_id: params["theme_id"]? ? params["theme_id"].to_i64 : nil, + domains: params["domains"]? ? Array(String).from_json(params["domains"]) : nil, + styles: params["styles"]? ? Array(String).from_json(params["styles"]) : nil, + scripts: params["scripts"]? ? Array(String).from_json(params["scripts"]) : nil, ) nil end - def do_site_set_default(params : HTTP::Params) - @context.models.site.set(params["site_id"].to_i64) - nil - end +# +# def do_site_set_default(params : HTTP::Params) +# @context.models.site.set(params["site_id"].to_i64) +# nil +# end +# end module FileAPI diff --git a/src/guff/models/site.cr b/src/guff/models/site.cr index 4ea639e..9eaed1a 100644 --- a/src/guff/models/site.cr +++ b/src/guff/models/site.cr @@ -68,11 +68,27 @@ class Guff::Models::SiteModel < Guff::Models::Model domains: " SELECT domain + FROM site_domains + WHERE site_id = ? + ORDER BY sort_order ", + assets: " + SELECT b.name AS type_name, + a.path + + FROM site_assets a + JOIN asset_types b + ON (b.type_id = a.type_id) + + WHERE a.site_id = ? + + ORDER BY a.sort_order + ", + get_name: " SELECT name @@ -105,10 +121,34 @@ class Guff::Models::SiteModel < Guff::Models::Model INSERT INTO site_domains(site_id, domain, sort_order) VALUES (?, ?, ?) ", - set_default: " - UPDATE sites - SET is_default = (site_id = ?) + set_assets_clear: " + DELETE FROM site_assets + WHERE site_id = ? + AND type_id = (SELECT type_id + FROM asset_types + WHERE name = ?) + ", + + set_assets_add: " + INSERT INTO site_assets ( + site_id, + type_id, + path, + sort_order + ) VALUES ( + ?, + (SELECT type_id FROM asset_types WHERE name = ?), + ?, + ? + ) ", + +# +# set_default: " +# UPDATE sites +# SET is_default = (site_id = ?) +# ", +# } def get_id(host : String?) : Int64? @@ -163,6 +203,8 @@ class Guff::Models::SiteModel < Guff::Models::Model is_full_feed : Bool? = nil, theme_id : Int64? = nil, domains : Array(String)? = nil, + styles : Array(String)? = nil, + scripts : Array(String)? = nil, ) : Int64 db = @context.dbs.rw site_id = -1_i64 @@ -177,6 +219,8 @@ class Guff::Models::SiteModel < Guff::Models::Model lang: lang, is_full_feed: is_full_feed, domains: domains, + styles: styles, + scripts: scripts, ) # create files directory @@ -200,6 +244,8 @@ class Guff::Models::SiteModel < Guff::Models::Model is_full_feed : Bool? = nil, theme_id : Int64? = nil, domains : Array(String)? = nil, + styles : Array(String)? = nil, + scripts : Array(String)? = nil, ) sql = [] of String args = [] of String @@ -238,6 +284,8 @@ class Guff::Models::SiteModel < Guff::Models::Model end set_domains(site_id, domains) + set_assets(site_id, "style", styles) + set_assets(site_id, "script", scripts) slug.try do |slug| # rename files directory @@ -276,14 +324,56 @@ class Guff::Models::SiteModel < Guff::Models::Model nil end - def set_default(site_id : Int64) - db = @context.dbs.rw + def assets(site_id : Int64) : Theme::Assets + r = Theme::Assets.new + + @context.dbs.ro.all(SQL[:assets], [site_id.to_s]) do |row| + case row["type_name"].to_s + when "script" + r.scripts << row["path"].to_s + when "style" + r.styles << row["path"].to_s + else + # never reached + raise "unknown type: #{row["type_name"]}" + end + end - db.transaction do - db.query(SQL[:set_default], [site_id.to_s]) - get_default_id + # return result + r + end + + private def set_assets( + site_id : Int64, + type : String, + paths : Array(String)? = nil, + ) + paths.try do |paths| + db = @context.dbs.rw + id = site_id.to_s + + db.transaction do + db.query(SQL[:set_assets_clear], [id, type]) + + paths.each_with_index do |path, i| + db.query(SQL[:set_assets_add], [id, type, path, i.to_s]) + end + end end nil end + +# +# def set_default(site_id : Int64) +# db = @context.dbs.rw +# +# db.transaction do +# db.query(SQL[:set_default], [site_id.to_s]) +# get_default_id +# end +# +# nil +# end +# end diff --git a/src/views/dialogs/admin/page-edit.ecr b/src/views/dialogs/admin/page-edit.ecr index e44cf86..1eb609a 100644 --- a/src/views/dialogs/admin/page-edit.ecr +++ b/src/views/dialogs/admin/page-edit.ecr @@ -207,7 +207,7 @@ @@ -224,7 +224,7 @@ diff --git a/src/views/dialogs/admin/site-add.ecr b/src/views/dialogs/admin/site-add.ecr index 86fd7df..0f99364 100644 --- a/src/views/dialogs/admin/site-add.ecr +++ b/src/views/dialogs/admin/site-add.ecr @@ -129,7 +129,7 @@ role='tabpanel' >
-
+
+
+
+ +
+
+
+ + + + +

+ Newline-delimited site styles. +

+
+
+ +
+
+ + + + +

+ Newline-delimited site scripts. +

+
-- cgit v1.2.3