diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-07-21 08:57:10 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-07-21 08:57:10 -0400 |
commit | 9dbed48af7808efd693f214d6631388df4c32520 (patch) | |
tree | e0ab0d90181b992acd59dcd5831f38e53a80629d /src | |
parent | d339a8fe4768bf6b7df241af896f1113c3f3d89b (diff) | |
download | guff-9dbed48af7808efd693f214d6631388df4c32520.tar.bz2 guff-9dbed48af7808efd693f214d6631388df4c32520.zip |
add theme asset support to pages
Diffstat (limited to 'src')
-rw-r--r-- | src/guff/models/theme.cr | 66 | ||||
-rw-r--r-- | src/guff/theme/assets.cr | 5 | ||||
-rw-r--r-- | src/guff/views/page.cr | 22 | ||||
-rw-r--r-- | src/views/page.ecr | 9 |
4 files changed, 96 insertions, 6 deletions
diff --git a/src/guff/models/theme.cr b/src/guff/models/theme.cr index e2eda39..9639adf 100644 --- a/src/guff/models/theme.cr +++ b/src/guff/models/theme.cr @@ -108,14 +108,30 @@ class Guff::Models::ThemeModel < Guff::Models::Model WHERE a.theme_slug = ? AND b.file_path = ? ", + + assets: " + SELECT c.name AS type_name, + a.file_path + + FROM theme_files a + JOIN theme_assets b + ON (b.file_id = a.file_id) + JOIN theme_asset_types c + ON (b.type_id = a.type_id) + + WHERE a.theme_id = ? + + ORDER BY c.name, b.sort_order + ", } def initialize(context : Context) super(context) @cache = { - templates: {} of Int32 => Hash(String, String), + templates: {} of Int32 => Template::Cache, metadata: {} of Int32 => Hash(String, String), + assets: {} of Int32 => Theme::Assets, } end @@ -167,7 +183,7 @@ class Guff::Models::ThemeModel < Guff::Models::Model row = get(theme_id: theme_id) # load templates - @cache[:templates][theme_id] = (if row["is_system"] == "1" + @cache[:templates][theme_id] = Template::Cache.new(if row["is_system"] == "1" # read templates from templates dir read_templates(File.join( @context.system_dir, @@ -336,4 +352,50 @@ class Guff::Models::ThemeModel < Guff::Models::Model File.exists?(dst_path) ? dst_path : nil end end + + ########## + # assets # + ########## + + def assets(theme_id : Int32) : Theme::Assets + unless @cache[:assets].includes?(theme_id) + # get theme + theme = get(theme_id: theme_id) + + @cache[:assets][theme_id] = (if theme["is_system"].to_s == "1" + # load manifest for system theme + Theme::Packer::Manifest.load(File.join( + @context.config.system_dir, + "themes", + theme["theme_slug"].to_s + )).assets + else + # FIXME: wouldn't this belong somewhere else? + r = Theme::Assets.new + r.styles = [] of String + r.scripts = [] of String + + # get theme assets + @context.dbs.ro.all(SQL[:assets], [theme_id.to_s]) do |row| + key, val = %w{type_name file_path}.map { |k| row[k].to_s } + + case key + when "style" + r.styles << val + when "script" + r.scripts << val + else + # never reached + raise "Unknown asset type: #{key}" + end + end + + # return results + r + end) + end + + # return results + @cache[:assets][theme_id] + end end diff --git a/src/guff/theme/assets.cr b/src/guff/theme/assets.cr index 5f15106..6c4227d 100644 --- a/src/guff/theme/assets.cr +++ b/src/guff/theme/assets.cr @@ -1,6 +1,11 @@ class Guff::Theme::Assets getter :scripts, :styles + def initialize + @scripts = [] of String + @styles = [] of String + end + JSON.mapping( scripts: Array(String), styles: Array(String), diff --git a/src/guff/views/page.cr b/src/guff/views/page.cr index 16f9b05..eb53dec 100644 --- a/src/guff/views/page.cr +++ b/src/guff/views/page.cr @@ -1,6 +1,28 @@ class Guff::Views::PageView < Guff::Views::HTMLView def initialize(context : Context, @item : Hash(String, String)) super(context) + + # FIXME: remove -1 hack + @theme_id = -1_i32 + if tmp_theme_id = @item[%w{theme_id site_theme_id}.find { |k| + @item[k]? && @item[k].size > 0 + }] + @theme_id = tmp_theme_id.to_i32 + end + end + + private def scripts + super(theme_assets.scripts) + # TODO: allow page-specific scripts + end + + private def styles + super(theme_assets.styles) + # TODO: allow page-specific styles + end + + private def theme_assets + @context.models.theme.assets(@theme_id) end ECR.def_to_s("src/views/page.ecr") diff --git a/src/views/page.ecr b/src/views/page.ecr index 0c9332e..54f4c7b 100644 --- a/src/views/page.ecr +++ b/src/views/page.ecr @@ -3,11 +3,12 @@ <head> <meta charset="utf-8"/> <title><%= + # TODO: should call page title instead h(@item["name"]) %></title> - <% - # TODO: add theme styles + <%= + styles %> </head> @@ -22,7 +23,7 @@ </div> </body> - <% - # TODO: add theme scripts + <%= + scripts %> </html> |