From 9dbed48af7808efd693f214d6631388df4c32520 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Thu, 21 Jul 2016 08:57:10 -0400 Subject: add theme asset support to pages --- src/guff/models/theme.cr | 66 ++++++++++++++++++++++++++++++++++++++++++++++-- src/guff/theme/assets.cr | 5 ++++ src/guff/views/page.cr | 22 ++++++++++++++++ src/views/page.ecr | 9 ++++--- 4 files changed, 96 insertions(+), 6 deletions(-) (limited to 'src') 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 @@ <%= + # TODO: should call page title instead h(@item["name"]) %> - <% - # TODO: add theme styles + <%= + styles %> @@ -22,7 +23,7 @@ - <% - # TODO: add theme scripts + <%= + scripts %> -- cgit v1.2.3