From bb2a315df93c173db84d2d6fc0cf1b2bdbd931a7 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 30 Jul 2016 18:40:08 -0400 Subject: refactor site dialogs and add site editing --- data/assets/js/admin/dialogs/site-add.js | 93 ++--------- data/assets/js/admin/dialogs/site-edit.js | 18 ++ data/assets/js/admin/dialogs/site.js | 94 +++++++++++ data/assets/js/admin/tabs/sites.js | 10 +- src/guff/views/dialogs/admin/site-add.cr | 33 +--- src/guff/views/dialogs/admin/site-edit.cr | 14 ++ src/guff/views/dialogs/admin/site.cr | 37 +++++ src/guff/views/pages/admin.cr | 29 +++- src/views/dialogs/admin/site-add.ecr | 259 ----------------------------- src/views/dialogs/admin/site.ecr | 264 ++++++++++++++++++++++++++++++ 10 files changed, 482 insertions(+), 369 deletions(-) create mode 100644 data/assets/js/admin/dialogs/site-edit.js create mode 100644 data/assets/js/admin/dialogs/site.js create mode 100644 src/guff/views/dialogs/admin/site-edit.cr delete mode 100644 src/views/dialogs/admin/site-add.ecr create mode 100644 src/views/dialogs/admin/site.ecr diff --git a/data/assets/js/admin/dialogs/site-add.js b/data/assets/js/admin/dialogs/site-add.js index bf95b8e..2d96c9f 100644 --- a/data/assets/js/admin/dialogs/site-add.js +++ b/data/assets/js/admin/dialogs/site-add.js @@ -1,85 +1,20 @@ jQuery(function($) { "use strict"; - var p = '#site-add-'; - - var FIELDS = { - text: ['name', 'slug', 'body', 'lang', 'theme_id'], - list: ['domains', 'styles', 'scripts'], - } - - var DEFAULTS = { - lang: DATA.default_lang, - theme_id: DATA.default_theme_id, - }; - - $(p + 'dialog').on('show.bs.modal', function() { - $.each(FIELDS.text, function(_, id) { - $(p + id).val(DEFAULTS[id] || ''); + // cache fields + var FIELDS = DATA.site_dialogs.fields; + + $('#site-add-dialog').on('show.bs.modal', function() { + $(this).trigger({ + type: 'guff.loaded', + + site_data: FIELDS.text.reduce(function(r, id) { + r[id] = FIELDS.defaults[id] || ''; + return r; + }, FIELDS.list.reduce(function(r, id) { + r[id] = []; + return r; + }, {})), }); - - // clear list textareas - $.each(FIELDS.list, function(_, id) { - $(p + id).val(''); - }); - }); - - $(p + 'dialog').on('shown.bs.modal', function() { - // show content tab - $(p + 'tab-content').click(); - - // focus name field - $(p + 'name').focus(); - }); - - $(p + 'dialog input[type="text"]').keydown(function(ev) { - if (ev.which == 13) { - setTimeout(function() { - $(p + 'confirm').click(); - }, 10); - - // stop event - return false; - } - }); - - $(p + 'name').keydown(function(ev) { - var me = $(this); - - setTimeout(function() { - $(p + 'slug').val(slugify(me.val())); - }, 10); - }); - - $(p + 'confirm').click(function() { - var me = $(this); - - if (me.hasClass('disabled')) - return false; - - // toggle loading - me.toggleClass('disabled').find('.loading').toggleClass('hidden'); - - 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', - }))).always(function() { - // toggle loading - me.toggleClass('disabled').find('.loading').toggleClass('hidden'); - }).fail(function(r) { - gripe(r, 'add site'); - }).done(function(r) { - $('#sites-reload').click(); - $(p + 'dialog').modal('hide'); - }); - - // stop event - return false; }); }); diff --git a/data/assets/js/admin/dialogs/site-edit.js b/data/assets/js/admin/dialogs/site-edit.js new file mode 100644 index 0000000..5a35073 --- /dev/null +++ b/data/assets/js/admin/dialogs/site-edit.js @@ -0,0 +1,18 @@ +jQuery(function($) { + "use strict"; + + $('#site-edit-dialog').on('show.bs.modal', function() { + var me = $(this); + + send('site/get', { + site_id: me.data('site_id'), + }).fail(function(r) { + gripe(r, 'get site'); + }).done(function(r) { + me.trigger({ + type: 'guff.loaded', + site_data: r, + }); + }); + }); +}); diff --git a/data/assets/js/admin/dialogs/site.js b/data/assets/js/admin/dialogs/site.js new file mode 100644 index 0000000..e35d389 --- /dev/null +++ b/data/assets/js/admin/dialogs/site.js @@ -0,0 +1,94 @@ +jQuery(function($) { + "use strict"; + + // cache dialogs + var DIALOGS = DATA.site_dialogs; + + $.each(DIALOGS.paths, function(dialog_id, save_path) { + var p = '#site-' + dialog_id + '-'; + + $(p + 'dialog').on('show.bs.modal', function() { + var me = $(this); + + // show spinner + me.find('.modal-body').addClass('hidden'); + me.find('.modal-body.loading').removeClass('hidden'); + }).on('guff.loaded', function(ev) { + // populate text fields + $.each(DIALOGS.fields.text, function(_, id) { + $(p + id).val(ev.site_data[id]); + }); + + // populate textareas + $.each(DIALOGS.fields.list, function(_, id) { + $(p + id).val(ev.site_data[id].join("\n")); + }); + + // hide spinner, show body + $(this).find('.modal-body').toggleClass('hidden'); + }).on('shown.bs.modal', function() { + // show content tab + $(p + 'tab-content').click(); + + // focus name field + $(p + 'name').focus(); + }); + + $(p + 'dialog input[type="text"]').keydown(function(ev) { + if (ev.which == 13) { + setTimeout(function() { + $(p + 'confirm').click(); + }, 10); + + // stop event + return false; + } + }); + + $(p + 'name').keydown(function(ev) { + var me = $(this); + + setTimeout(function() { + $(p + 'slug').val(slugify(me.val())); + }, 10); + }); + + $(p + 'confirm').click(function() { + var me = $(this); + + if (me.hasClass('disabled')) + return false; + + // toggle loading + me.toggleClass('disabled').find('.loading').toggleClass('hidden'); + + // build args + var args = DIALOGS.fields.text.reduce(function(r, id) { + r[id] = $(p + id).val(); + return r; + }, DIALOGS.fields.list.reduce(function(r, id) { + r[id] = listify(p + id); + return r; + }, { + site_id: $(p + 'dialog').data('site_id'), + + // hard-code this for now + is_full_feed: 't', + })); + + // send request + send(save_path, args).always(function() { + // toggle loading + me.toggleClass('disabled').find('.loading').toggleClass('hidden'); + }).fail(function(r) { + gripe(r, dialog_id + ' site'); + }).done(function(r) { + $('#sites-reload').click(); + $(p + 'dialog').modal('hide'); + }); + + // stop event + return false; + }); + }); +}); diff --git a/data/assets/js/admin/tabs/sites.js b/data/assets/js/admin/tabs/sites.js index 8644705..1ed7b48 100644 --- a/data/assets/js/admin/tabs/sites.js +++ b/data/assets/js/admin/tabs/sites.js @@ -81,11 +81,12 @@ jQuery(function($) { })); }).done(function(r) { list.html($.map(r, function(row) { - var active = (row.is_active == '1'); + var is_default = !!+row.is_default; return TEMPLATES.run('site', $.extend({}, row, { q: [row.site_id, row.slug, row.name, row.body].join(' ').toLowerCase(), - badge_css: +row.is_default ? '' : 'hidden', + badge_css: is_default ? '' : 'hidden', + css: is_default ? 'list-group-item-warning' : '', })); }).join('')); @@ -108,9 +109,8 @@ jQuery(function($) { $('#sites .active').removeClass('active'); $(this).addClass('active'); - // TODO - // $('#site-edit-dialog').data('site_id', site_id).modal('show'); - alert('TODO: edit site ' + site_id); + // show dialog + $('#site-edit-dialog').data('site_id', site_id).modal('show'); // stop event return false; diff --git a/src/guff/views/dialogs/admin/site-add.cr b/src/guff/views/dialogs/admin/site-add.cr index c124457..50e051d 100644 --- a/src/guff/views/dialogs/admin/site-add.cr +++ b/src/guff/views/dialogs/admin/site-add.cr @@ -1,29 +1,14 @@ require "./site" class Guff::Views::Dialogs::Admin::SiteAdd < Guff::Views::Dialogs::Admin::Site - TABS = [{ - :id => "content", - :css => "active", - :icon => "fa-pencil", - :name => "Content", - :text => "View content tab.", - }, { - :id => "theme", - :css => "", - :icon => "fa-eye", - :name => "Theme", - :text => "View theme tab.", - }, { - :id => "other", - :css => "", - :icon => "fa-ellipsis-h", - :name => "Other", - :text => "View other tab.", - }] - - private def tabs - super("site-add", TABS) + def initialize(context : Context) + super( + context: context, + id: "site-add", + name: "New Site", + save_icon: "fa-plus-circle", + save_text: "Create site.", + save_name: "Create Site", + ) end - - ECR.def_to_s("src/views/dialogs/admin/site-add.ecr") end diff --git a/src/guff/views/dialogs/admin/site-edit.cr b/src/guff/views/dialogs/admin/site-edit.cr new file mode 100644 index 0000000..89dae69 --- /dev/null +++ b/src/guff/views/dialogs/admin/site-edit.cr @@ -0,0 +1,14 @@ +require "./site" + +class Guff::Views::Dialogs::Admin::SiteEdit < Guff::Views::Dialogs::Admin::Site + def initialize(context : Context) + super( + context: context, + id: "site-edit", + name: "Edit Site", + save_icon: "fa-save", + save_text: "Save changes.", + save_name: "Save Changes", + ) + end +end diff --git a/src/guff/views/dialogs/admin/site.cr b/src/guff/views/dialogs/admin/site.cr index c03cd0a..b916fd7 100644 --- a/src/guff/views/dialogs/admin/site.cr +++ b/src/guff/views/dialogs/admin/site.cr @@ -1,9 +1,44 @@ class Guff::Views::Dialogs::Admin::Site < Guff::Views::HTMLView + TABS = [{ + :id => "content", + :css => "active", + :icon => "fa-pencil", + :name => "Content", + :text => "View content tab.", + }, { + :id => "theme", + :css => "", + :icon => "fa-eye", + :name => "Theme", + :text => "View theme tab.", + }, { + :id => "other", + :css => "", + :icon => "fa-ellipsis-h", + :name => "Other", + :text => "View other tab.", + }] + OPTION_TEMPLATES = { y: "", n: "", } + def initialize( + context : Context, + @id : String, + @name : String, + @save_name : String, + @save_icon : String, + @save_text : String, + ) + super(context) + end + + protected def tabs + super(@id, TABS) + end + private def theme_options @context.models.theme.all.map { |row| { @@ -18,4 +53,6 @@ class Guff::Views::Dialogs::Admin::Site < Guff::Views::HTMLView OPTION_TEMPLATES[tmpl_id] % %i{id slug name}.map { |k| row[k] } }.join("") end + + ECR.def_to_s("src/views/dialogs/admin/site.ecr") end diff --git a/src/guff/views/pages/admin.cr b/src/guff/views/pages/admin.cr index 1c5f795..5ed95db 100644 --- a/src/guff/views/pages/admin.cr +++ b/src/guff/views/pages/admin.cr @@ -28,7 +28,9 @@ class Guff::Views::Pages::Admin < Guff::Views::HTMLView assets/js/admin/tabs/themes.js assets/js/admin/dialogs/user-add.js assets/js/admin/dialogs/user-edit.js + assets/js/admin/dialogs/site.js assets/js/admin/dialogs/site-add.js + assets/js/admin/dialogs/site-edit.js assets/js/admin/dialogs/post-edit.js assets/js/admin/dialogs/blog-edit.js assets/js/admin/dialogs/page-edit.js @@ -97,6 +99,7 @@ class Guff::Views::Pages::Admin < Guff::Views::HTMLView io << Dialogs::Admin::UserAdd.new(@context) io << Dialogs::Admin::UserEdit.new(@context) io << Dialogs::Admin::SiteAdd.new(@context) + io << Dialogs::Admin::SiteEdit.new(@context) io << Dialogs::Admin::BlogEdit.new(@context) io << Dialogs::Admin::PageEdit.new(@context) io << Dialogs::Admin::ProjectEdit.new(@context) @@ -104,12 +107,34 @@ class Guff::Views::Pages::Admin < Guff::Views::HTMLView end private def page_data + default_lang = "en-US" + default_theme_id = @context.models.theme.get_default_id + { post_types: POST_TYPES, sites: @context.models.site.get_sites, + + # TODO: refactor to defaults default_site_id: @context.models.site.get_default_id, - default_lang: "en-US", - default_theme_id: @context.models.theme.get_default_id, + default_lang: default_lang, + default_theme_id: default_theme_id, + + site_dialogs: { + paths: { + add: "site/add", + edit: "site/set", + }, + + fields: { + text: %w{name slug body lang theme_id}, + list: %w{domains styles scripts}, + + defaults: { + lang: default_lang, + theme_id: default_theme_id, + }, + }, + }, }.to_json end diff --git a/src/views/dialogs/admin/site-add.ecr b/src/views/dialogs/admin/site-add.ecr deleted file mode 100644 index 0f99364..0000000 --- a/src/views/dialogs/admin/site-add.ecr +++ /dev/null @@ -1,259 +0,0 @@ - diff --git a/src/views/dialogs/admin/site.ecr b/src/views/dialogs/admin/site.ecr new file mode 100644 index 0000000..fa5d7d7 --- /dev/null +++ b/src/views/dialogs/admin/site.ecr @@ -0,0 +1,264 @@ + -- cgit v1.2.3