From e6ebc011f255596f09fec40f69049514d52bebef Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 30 Jul 2016 14:21:14 -0400 Subject: add tabs to site-add, refactor textarea handling, and fix site_id post edit dialogs --- data/assets/js/admin/dialogs/page-edit.js | 4 +- data/assets/js/admin/dialogs/post-edit.js | 11 +- data/assets/js/admin/dialogs/site-add.js | 43 +++-- data/assets/js/util.js | 14 ++ src/guff/views/dialogs/admin/site-add.cr | 24 +++ src/guff/views/dialogs/admin/site.cr | 4 +- src/views/dialogs/admin/site-add.ecr | 292 +++++++++++++++++------------- 7 files changed, 237 insertions(+), 155 deletions(-) diff --git a/data/assets/js/admin/dialogs/page-edit.js b/data/assets/js/admin/dialogs/page-edit.js index 9e32d89..0e7a5b3 100644 --- a/data/assets/js/admin/dialogs/page-edit.js +++ b/data/assets/js/admin/dialogs/page-edit.js @@ -22,9 +22,7 @@ jQuery(function($) { // 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) : []); - + r[key] = listify(p + key); return r; }, {}); diff --git a/data/assets/js/admin/dialogs/post-edit.js b/data/assets/js/admin/dialogs/post-edit.js index 9d90b99..d7eb120 100644 --- a/data/assets/js/admin/dialogs/post-edit.js +++ b/data/assets/js/admin/dialogs/post-edit.js @@ -5,13 +5,8 @@ jQuery(function($) { }); function update_slug(name, slug) { - if (slug.prop('disabled')) { - slug.val( - name.val().toLowerCase() - .replace(/[^a-z0-9_-]+/g, '-') - .replace(/^-+|-+$/g, '') - ); - } + if (slug.prop('disabled')) + slug.val(slugify(name.val())); } $.each(['blog', 'page', 'project'], function(i, id) { @@ -22,6 +17,7 @@ jQuery(function($) { slug_lock = (r.slug_lock == "1"); $(p + 'name').val(r.name); + $(p + 'site').val(r.site_id); $(p + 'slug').val(r.slug) .prop('disabled', slug_lock ? 'disabled' : null); @@ -100,6 +96,7 @@ jQuery(function($) { send(id + "/set", $.extend({ post_id: $(p + 'dialog').data('post_id'), + site_id: $(p + 'site').val(), name: $(p + 'name').val(), slug_lock: $(p + 'slug-lock').hasClass('btn-default') ? 't' : 'f', slug: $(p + 'slug').val(), diff --git a/data/assets/js/admin/dialogs/site-add.js b/data/assets/js/admin/dialogs/site-add.js index 9a3c02f..8cdc772 100644 --- a/data/assets/js/admin/dialogs/site-add.js +++ b/data/assets/js/admin/dialogs/site-add.js @@ -1,34 +1,35 @@ jQuery(function($) { "use strict"; - var IDS = ['name', 'slug', 'body', 'lang', 'theme_id']; + var p = '#site-add-', + IDS = ['name', 'slug', 'body', 'lang', 'theme_id']; + var DEFAULTS = { lang: DATA.default_lang, theme_id: DATA.default_theme_id, }; - function get_domains() { - var r = $('#site-add-domains').val().replace(/^\s+|\s+$/m, ''); - return (r.length > 0) ? r.split(/\s+/m) : []; - } - - $('#site-add-dialog').on('show.bs.modal', function() { + $(p + 'dialog').on('show.bs.modal', function() { $.each(IDS, function(_, id) { - $('#site-add-' + id).val(DEFAULTS[id] || ''); + $(p + id).val(DEFAULTS[id] || ''); }); // clear list of domains - $('#site-add-domains').val(''); + $(p + 'domains').val(''); }); - $('#site-add-dialog').on('shown.bs.modal', function() { - $('#site-add-name').focus(); + $(p + 'dialog').on('shown.bs.modal', function() { + // show content tab + $(p + 'tab-content').click(); + + // focus name field + $(p + 'name').focus(); }); - $('#site-add-dialog input[type="text"]').keydown(function(ev) { + $(p + 'dialog input[type="text"]').keydown(function(ev) { if (ev.which == 13) { setTimeout(function() { - $('#site-add-confirm').click(); + $(p + 'confirm').click(); }, 10); // stop event @@ -36,7 +37,15 @@ jQuery(function($) { } }); - $('#site-add-confirm').click(function() { + $(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')) @@ -46,11 +55,11 @@ jQuery(function($) { me.toggleClass('disabled').find('.loading').toggleClass('hidden'); send('site/add', IDS.reduce(function(r, id) { - r[id] = $('#site-add-' + id).val(); + r[id] = $(p + id).val(); return r; }, { is_full_feed: 't', - domains: JSON.stringify(get_domains()), + domains: listify(p + 'domains'), })).always(function() { // toggle loading me.toggleClass('disabled').find('.loading').toggleClass('hidden'); @@ -58,7 +67,7 @@ jQuery(function($) { gripe(r, 'add site'); }).done(function(r) { $('#sites-reload').click(); - $('#site-add-dialog').modal('hide'); + $(p + 'dialog').modal('hide'); }); // stop event diff --git a/data/assets/js/util.js b/data/assets/js/util.js index 7986120..6379961 100644 --- a/data/assets/js/util.js +++ b/data/assets/js/util.js @@ -28,8 +28,22 @@ jQuery(function($) { // display error message alert(prefix + error); + }; + + // slugify string + window.slugify = function(s) { + return s.toLowerCase() + .replace(/[^a-z0-9_-]+/g, '-') + .replace(/^-+|-+$/g, ''); + }; + + // convert textarea to jsonified array of strings + window.listify = function(el) { + var r = $(el).val().replace(/^\s+|\s+$/m, ''); + return JSON.stringify((r.length > 0) ? r.split(/\s+/m) : []); } + LuigiTemplate.FILTERS.json = function(val) { return JSON.stringify(val); }; diff --git a/src/guff/views/dialogs/admin/site-add.cr b/src/guff/views/dialogs/admin/site-add.cr index 894a34a..c124457 100644 --- a/src/guff/views/dialogs/admin/site-add.cr +++ b/src/guff/views/dialogs/admin/site-add.cr @@ -1,5 +1,29 @@ 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) + end + ECR.def_to_s("src/views/dialogs/admin/site-add.ecr") end diff --git a/src/guff/views/dialogs/admin/site.cr b/src/guff/views/dialogs/admin/site.cr index 1b6419b..c03cd0a 100644 --- a/src/guff/views/dialogs/admin/site.cr +++ b/src/guff/views/dialogs/admin/site.cr @@ -1,5 +1,5 @@ class Guff::Views::Dialogs::Admin::Site < Guff::Views::HTMLView - TEMPLATES = { + OPTION_TEMPLATES = { y: "", n: "", } @@ -15,7 +15,7 @@ class Guff::Views::Dialogs::Admin::Site < Guff::Views::HTMLView } }.map { |row| tmpl_id = (row[:slug] == "default") ? :y : :n - TEMPLATES[tmpl_id] % %i{id slug name}.map { |k| row[k] } + OPTION_TEMPLATES[tmpl_id] % %i{id slug name}.map { |k| row[k] } }.join("") end end diff --git a/src/views/dialogs/admin/site-add.ecr b/src/views/dialogs/admin/site-add.ecr index aae2f70..86fd7df 100644 --- a/src/views/dialogs/admin/site-add.ecr +++ b/src/views/dialogs/admin/site-add.ecr @@ -1,5 +1,5 @@