aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--data/assets/js/admin/tabs/posts.js27
-rw-r--r--src/guff.cr105
-rw-r--r--src/views/admin-page.ecr30
-rw-r--r--src/views/dropdown/item.ecr28
-rw-r--r--src/views/dropdown/menu.ecr19
5 files changed, 201 insertions, 8 deletions
diff --git a/data/assets/js/admin/tabs/posts.js b/data/assets/js/admin/tabs/posts.js
index 83db403..6fc0afd 100644
--- a/data/assets/js/admin/tabs/posts.js
+++ b/data/assets/js/admin/tabs/posts.js
@@ -30,8 +30,9 @@ jQuery(function($) {
.find('.loading').toggleClass('hidden');
send('post/get_posts', {
- q: $('#posts-q').data('q'),
- page: 1,
+ post_type: $('#posts-filter-type li.active a').data('id'),
+ q: $('#posts-q').data('q'),
+ page: 1,
}).always(function() {
$('#posts-reload').removeClass('disabled')
.find('.loading').toggleClass('hidden');
@@ -98,6 +99,28 @@ jQuery(function($) {
reload();
});
+ $('.posts-filter-menu').on('click', 'a', function() {
+ var data = $(this).data(),
+ ul = $(this).parents('ul'),
+ is_default = (data.id == ul.data('default'));
+
+ ul.find('li').removeClass('active');
+ $(this).parents('li').addClass('active');
+ ul.prev('a')
+ .toggleClass('btn-default', is_default)
+ .toggleClass('btn-primary', !is_default)
+ .find('span').text(data.text);
+
+ // hide dropdown
+ $('body').trigger('click');
+
+ // reload posts
+ reload();
+
+ // stop event
+ return false;
+ });
+
$('#posts').on('click', 'a', function() {
var data = $(this).data();
diff --git a/src/guff.cr b/src/guff.cr
index b5ea71a..121f150 100644
--- a/src/guff.cr
+++ b/src/guff.cr
@@ -274,12 +274,14 @@ module Guff
JOIN states b
ON (b.state_id = a.state_id)
- LEFT JOIN blogs c
- ON (c.post_id = a.post_id)
- LEFT JOIN pages d
- ON (d.post_id = a.post_id)
- LEFT JOIN projects e
- ON (e.post_id = a.post_id)
+ JOIN sites c
+ ON (c.site_id = a.site_id)
+ LEFT JOIN blogs x
+ ON (x.post_id = a.post_id)
+ LEFT JOIN pages y
+ ON (y.post_id = a.post_id)
+ LEFT JOIN projects z
+ ON (z.post_id = a.post_id)
WHERE %s
",
@@ -485,6 +487,9 @@ module Guff
"y.post_id IS NOT NULL"
when "project"
"z.post_id IS NOT NULL"
+ when "all"
+ # allow "all"
+ "1"
else
raise "unknown post type: #{post_type}"
end
@@ -1648,6 +1653,73 @@ module Guff
ECR.def_to_s("src/views/tab.ecr")
end
+ module Dropdown
+ module Icon
+ ICON_TEMPLATE = "<i class='fa fa-fw %s'></i>"
+
+ def self.icon(id : String?)
+ if id && id.size > 0
+ ICON_TEMPLATE % [HTML.escape(id.not_nil!)]
+ else
+ ""
+ end
+ end
+ end
+
+ class ItemView < View
+ def initialize(
+ context : Context,
+ @active : Bool,
+ @item : Hash(Symbol, String)
+ )
+ super(context)
+ end
+
+ private def v(id : Symbol)
+ h(@item[id])
+ end
+
+ private def li_css
+ @active ? "class='active'" : ""
+ end
+
+ ECR.def_to_s("src/views/dropdown/item.ecr")
+ end
+
+ class MenuView < View
+ def initialize(
+ context : Context,
+ @id : String,
+ @name : String,
+ @text : String,
+ @css : String,
+ @icon : String,
+ @default : String,
+ @items : Array(Hash(Symbol, String))
+ )
+ super(context)
+
+ @default_name = @items.reduce("") do |r, row|
+ (row[:id]? == @default) ? row[:name] : r
+ end as String
+ end
+
+ private def items
+ String.build do |io|
+ @items.each do |item|
+ io << ItemView.new(
+ context: @context,
+ active: @default == item[:id]?,
+ item: item
+ ).to_s
+ end
+ end
+ end
+
+ ECR.def_to_s("src/views/dropdown/menu.ecr")
+ end
+ end
+
abstract class HTMLView < View
TEMPLATES = {
script: "<script type='text/javascript' src='%s'></script>",
@@ -1677,6 +1749,27 @@ module Guff
end
end
end
+
+ def dropdown(
+ id : String,
+ name : String,
+ text : String,
+ icon : String,
+ css : String,
+ default : String,
+ items : Array(Hash(Symbol, String))
+ )
+ Dropdown::MenuView.new(
+ context: @context,
+ id: id,
+ name: name,
+ text: text,
+ icon: icon,
+ css: css,
+ default: default,
+ items: items
+ ).to_s
+ end
end
class AdminPageView < HTMLView
diff --git a/src/views/admin-page.ecr b/src/views/admin-page.ecr
index 39b4de3..eedc41c 100644
--- a/src/views/admin-page.ecr
+++ b/src/views/admin-page.ecr
@@ -84,6 +84,36 @@
new_post_button
%></div><!-- btn-group -->
+ <div class='btn-group btn-group-sm'><%=
+ dropdown(
+ id: "posts-filter-type",
+ name: "Type",
+ css: "posts-filter-menu",
+ text: "Filter by type.",
+ icon: "",
+ default: "all",
+ items: [{
+ id: "all",
+ name: "All",
+ text: "Show all types.",
+ }, {
+ type: "divider",
+ }, {
+ id: "blog",
+ name: "Blog Posts",
+ text: "Show blog posts.",
+ }, {
+ id: "page",
+ name: "Pages",
+ text: "Show pages.",
+ }, {
+ id: "project",
+ name: "Projects",
+ text: "Show projects.",
+ }],
+ )
+ %></div><!-- btn-group -->
+
<div class='btn-group btn-group-sm pull-right'>
<a
href='#'
diff --git a/src/views/dropdown/item.ecr b/src/views/dropdown/item.ecr
new file mode 100644
index 0000000..34f04da
--- /dev/null
+++ b/src/views/dropdown/item.ecr
@@ -0,0 +1,28 @@
+<%
+case @item[:type]?
+when "divider"
+%>
+ <li class='divider' role='separator'></li>
+<%
+when "header"
+%>
+ <li class='dropdown-header' role='presentation'>
+ <%= v(:name) %>
+ </li>
+<%
+else
+%>
+<li <%= li_css %>>
+ <a
+ href='#'
+ title='<%= v(:text) %>'
+ data-id='<%= v(:id) %>'
+ data-name='<%= v(:name) %>'
+ >
+ <%= Icon.icon(@item[:icon]?) %>
+ <%= v(:name) %>
+ </a>
+</li>
+<%
+end
+%>
diff --git a/src/views/dropdown/menu.ecr b/src/views/dropdown/menu.ecr
new file mode 100644
index 0000000..ca39c1e
--- /dev/null
+++ b/src/views/dropdown/menu.ecr
@@ -0,0 +1,19 @@
+<a
+ href='#'
+ class='btn btn-default'
+ title='<%= h(@text) %>'
+ data-toggle='dropdown'
+>
+ <%= Icon.icon(@icon) %>
+ <%= h(@name) %>:
+ <span><%= h(@default_name) %></span>
+ <i class='fa fa-caret-down'></i>
+</a>
+
+<ul
+ id='<%= h(@id) %>'
+ class='dropdown-menu <%= h(@css) %>'
+ data-default='<%= h(@default) %>'
+><%=
+ items
+%></ul><!-- dropdown-menu -->