aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--data/assets/js/admin/dialogs/page-edit.js20
-rw-r--r--data/assets/js/util.js17
-rw-r--r--src/guff/apis.cr24
-rw-r--r--src/guff/models/page.cr49
4 files changed, 82 insertions, 28 deletions
diff --git a/data/assets/js/admin/dialogs/page-edit.js b/data/assets/js/admin/dialogs/page-edit.js
index 95c9a63..9e32d89 100644
--- a/data/assets/js/admin/dialogs/page-edit.js
+++ b/data/assets/js/admin/dialogs/page-edit.js
@@ -1,12 +1,18 @@
jQuery(function($) {
"use strict";
- var p = '#page-edit-';
+ var ASSET_TYPES = ['styles', 'scripts'],
+ p = '#page-edit-';
$(p + 'dialog').on('guff.loaded', function(ev) {
var r = ev.post_data;
$(p + 'theme').val(r.theme_id || 'site-default');
+
+ // set assets
+ $.each(ASSET_TYPES, function(i, key) {
+ $(p + key).val(r[key].join("\n"));
+ });
});
$(p + 'confirm').click(function() {
@@ -14,13 +20,21 @@ jQuery(function($) {
if (theme_id == 'site-default')
theme_id = null;
+ // get assets
+ var assets = ASSET_TYPES.reduce(function(r, key) {
+ var s = $(p + key).val().replace(/^\s+|\s$/mg, '');
+ r[key] = JSON.stringify((s.length > 0) ? s.split(/\s+/m) : []);
+
+ return r;
+ }, {});
+
$(p + 'dialog').trigger({
type: 'guff.save',
- post_data: {
+ post_data: $.extend({
have_theme_id: 't',
theme_id: theme_id,
- },
+ }, assets),
});
// stop event
diff --git a/data/assets/js/util.js b/data/assets/js/util.js
index cde0f25..7986120 100644
--- a/data/assets/js/util.js
+++ b/data/assets/js/util.js
@@ -1,20 +1,3 @@
-jQuery.fn.reduce = (function() {
- "use strict";
-
- if (Array.prototype.reduce) {
- return Array.prototype.reduce;
- } else {
- return function(a, cb, r) {
- a = [].concat(a);
-
- for (var i = 0, l = a.length; i < l; i++)
- r = cb(r, a[i]);
-
- return r;
- };
- }
-})();
-
jQuery(function($) {
"use strict";
diff --git a/src/guff/apis.cr b/src/guff/apis.cr
index 5d87af2..8a84c1d 100644
--- a/src/guff/apis.cr
+++ b/src/guff/apis.cr
@@ -40,18 +40,30 @@ module Guff::APIs
have_theme_id: true,
theme_id: (params["theme_id"]? && params["theme_id"].size > 0) ? params["theme_id"].to_i32 : nil,
+
+ styles: (params["styles"]? && params["styles"] =~ /\S/) ? Array(String).from_json(params["styles"]) : nil,
+ scripts: (params["scripts"]? && params["scripts"] =~ /\S/) ? Array(String).from_json(params["scripts"]) : nil,
)
nil
end
def do_page_get(params : HTTP::Params)
- @context.models.page.get(
- post_id: params["post_id"].to_i64
- ).reduce({} of String => String) do |r, kv|
- r[kv[0]] = kv[1].to_s
- r
- end
+ # cache post_id, get assets
+ post_id = params["post_id"].to_i64
+ assets = @context.models.page.assets(post_id)
+
+ ({
+ "styles" => assets.styles,
+ "scripts" => assets.scripts,
+ } of String => Array(String)).merge(
+ @context.models.page.get(
+ post_id: post_id
+ ).reduce({} of String => String) do |r, kv|
+ r[kv[0]] = kv[1].to_s
+ r
+ end
+ )
end
end
diff --git a/src/guff/models/page.cr b/src/guff/models/page.cr
index b7043a4..d01af01 100644
--- a/src/guff/models/page.cr
+++ b/src/guff/models/page.cr
@@ -117,6 +117,21 @@ class Guff::Models::PageModel < Guff::Models::Model
ORDER BY b.name, a.sort_order
",
+
+ set_assets_clear: "
+ DELETE FROM page_assets
+ WHERE post_id = ?
+ AND type_id = (SELECT type_id FROM asset_types WHERE name = ?)
+ ",
+
+ set_assets_add: "
+ INSERT INTO page_assets (
+ post_id,
+ type_id,
+ path,
+ sort_order
+ ) VALUES (?, (SELECT type_id FROM asset_types WHERE name = ?), ?, ?)
+ ",
}
LIMIT = 50
@@ -191,10 +206,16 @@ class Guff::Models::PageModel < Guff::Models::Model
have_theme_id : Bool = false,
theme_id : Int32? = nil,
+
+ styles : Array(String)? = nil,
+ scripts : Array(String)? = nil,
)
db = @context.dbs.rw
db.transaction do
+ # cache post_id string
+ id = post_id.to_s
+
@context.models.post.set(
post_id: post_id,
@@ -213,9 +234,33 @@ class Guff::Models::PageModel < Guff::Models::Model
if have_theme_id
if theme_id
- db.query(SQL[:set], [theme_id.to_s, post_id.to_s])
+ db.query(SQL[:set], [theme_id.to_s, id])
else
- db.query(SQL[:set_default], [post_id.to_s])
+ db.query(SQL[:set_default], [id])
+ end
+ end
+
+ set_assets(id, "style", styles)
+ set_assets(id, "script", scripts)
+ end
+ end
+
+ private def set_assets(
+ post_id : String,
+ type : String,
+ paths : Array(String)?,
+ )
+ if paths
+ # cache db handle
+ db = @context.dbs.rw
+
+ db.transaction do
+ # clear existing assets
+ db.query(SQL[:set_assets_clear], [post_id, type])
+
+ # add new assets
+ paths.not_nil!.each_with_index do |path, i|
+ db.query(SQL[:set_assets_add], [post_id, type, path, i.to_s])
end
end
end