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 +++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 79 deletions(-) create mode 100644 data/assets/js/admin/dialogs/site-edit.js create mode 100644 data/assets/js/admin/dialogs/site.js (limited to 'data/assets/js/admin/dialogs') 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; + }); + }); +}); -- cgit v1.2.3