From 0c2d54c6159cf4b9e4c93e43c18ad4d08bf54faf Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Fri, 29 Jul 2016 17:10:50 -0400 Subject: enable generate secret button and add gripe() --- data/assets/js/admin/dialogs/post-edit.js | 10 +------ data/assets/js/admin/dialogs/project-edit.js | 16 +++++++++++ data/assets/js/admin/dialogs/user-add.js | 11 +------- data/assets/js/admin/dialogs/user-edit.js | 11 +------- data/assets/js/admin/tabs/files.js | 40 +++------------------------- data/assets/js/admin/tabs/posts.js | 12 +-------- data/assets/js/util.js | 20 ++++++++++++++ 7 files changed, 44 insertions(+), 76 deletions(-) (limited to 'data/assets/js') diff --git a/data/assets/js/admin/dialogs/post-edit.js b/data/assets/js/admin/dialogs/post-edit.js index 3b20c7a..9d90b99 100644 --- a/data/assets/js/admin/dialogs/post-edit.js +++ b/data/assets/js/admin/dialogs/post-edit.js @@ -109,15 +109,7 @@ jQuery(function($) { $(p + 'confirm').removeClass('disabled') .find('.loading').toggleClass('hidden'); }).fail(function(r) { - var error = r.responseText; - - try { - var data = $.parseJSON(r.responseText); - if (data.error) - error = data.error; - } catch (e) {} - - alert('Error: ' + error); + gripe(r, 'save changes'); }).done(function(r) { // reload posts $('#posts-reload').click(); diff --git a/data/assets/js/admin/dialogs/project-edit.js b/data/assets/js/admin/dialogs/project-edit.js index 17236f6..8df9364 100644 --- a/data/assets/js/admin/dialogs/project-edit.js +++ b/data/assets/js/admin/dialogs/project-edit.js @@ -21,4 +21,20 @@ jQuery(function($) { // stop event return false; }); + + $('#project-edit-generate-secret').click(function() { + var me = $(this); + + // disable button + me.toggleClass('disabled').find('.fa').toggleClass('fa-spin'); + send('project/generate_secret').always(function() { + // enable button + me.toggleClass('disabled').find('.fa').toggleClass('fa-spin'); + }).fail(function(r) { + gripe(r, 'generate secret'); + }).done(function(r) { + // update secret + $('#project-edit-secret').val(r.secret); + }); + }); }); diff --git a/data/assets/js/admin/dialogs/user-add.js b/data/assets/js/admin/dialogs/user-add.js index 16757bb..7e78923 100644 --- a/data/assets/js/admin/dialogs/user-add.js +++ b/data/assets/js/admin/dialogs/user-add.js @@ -43,16 +43,7 @@ jQuery(function($) { // toggle loading me.toggleClass('disabled').find('.loading').toggleClass('hidden'); }).fail(function(r) { - var error = r.responseText; - - try { - var data = $.parseJSON(r.responseText); - - if (data.error) - error = data.error; - } catch (e) {} - - alert('Error: ' + error); + gripe(r, 'add user'); }).done(function(r) { $('#users-reload').click(); $('#user-add-dialog').modal('hide'); diff --git a/data/assets/js/admin/dialogs/user-edit.js b/data/assets/js/admin/dialogs/user-edit.js index 038a587..70c1dc3 100644 --- a/data/assets/js/admin/dialogs/user-edit.js +++ b/data/assets/js/admin/dialogs/user-edit.js @@ -75,16 +75,7 @@ jQuery(function($) { // toggle loading me.toggleClass('disabled').find('.loading').toggleClass('hidden'); }).fail(function(r) { - var error = r.responseText; - - try { - // try to extract error message from json response - var data = $.parseJSON(r.responseText); - if ('error' in data) - error = data.error; - } catch (e) {} - - alert('Error: ' + error); + gripe(r, 'save user'); }).done(function(r) { $('#users-reload').click(); $('#user-edit-dialog').modal('hide'); diff --git a/data/assets/js/admin/tabs/files.js b/data/assets/js/admin/tabs/files.js index bfd1943..1c01476 100644 --- a/data/assets/js/admin/tabs/files.js +++ b/data/assets/js/admin/tabs/files.js @@ -151,15 +151,7 @@ jQuery(function($) { btns.toggleClass('disabled').find('.loading').toggleClass('hidden'); // list.html(''); }).fail(function(r) { - var error = r.responseText; - - try { - var data = $.parseJSON(r.responseText); - if (data.error) - error = data.error; - } catch (e) {} - - alert("Error: " + error); + gripe(r, 'list files'); }).done(function(r) { // disable action buttons $('#file-actions li').addClass('disabled'); @@ -273,15 +265,7 @@ jQuery(function($) { }).always(function() { // TODO: need loading handler }).fail(function(r) { - var error = r.responseText; - - try { - var data = $.parseJSON(r.responseText); - if (data.error) - error = data.error; - } catch (e) {} - - alert("Error: " + error); + gripe(r, 'move file'); }).done(function() { // reload file list reload(); @@ -295,15 +279,7 @@ jQuery(function($) { }).always(function() { // TODO: need loading handler }).fail(function(r) { - var error = r.responseText; - - try { - var data = $.parseJSON(r.responseText); - if (data.error) - error = data.error; - } catch (e) {} - - alert("Error: " + error); + gripe(r, 'delete file'); }).done(function() { // reload file list reload(); @@ -345,15 +321,7 @@ jQuery(function($) { // enable button, hide spinner me.toggleClass('disabled').find('.loading').toggleClass('hidden'); }).fail(function(r) { - var error = r.responseText; - - try { - var data = $.parseJSON(r.responseText); - if (data.error) - error = data.error; - } catch (e) {} - - alert("Error: " + error); + gripe(r, 'create folder'); }).done(function(r) { // reload file list reload(); diff --git a/data/assets/js/admin/tabs/posts.js b/data/assets/js/admin/tabs/posts.js index a1e73c9..25f659e 100644 --- a/data/assets/js/admin/tabs/posts.js +++ b/data/assets/js/admin/tabs/posts.js @@ -167,18 +167,8 @@ jQuery(function($) { $('body').trigger('click'); send(type + '/add').fail(function(r) { - var msg = r.responseText; - - try { - var data = $.parseJSON(r); - if (data.error) - msg = data.error; - } catch (e) {} - - alert('Error: ' + msg); + gripe(r, 'add ' + type); }).done(function(r) { - console.log(r); - // show edit dialog $('#' + type + '-edit-dialog').data('post_id', r.post_id).modal('show') }); diff --git a/data/assets/js/util.js b/data/assets/js/util.js index 3abaabe..cde0f25 100644 --- a/data/assets/js/util.js +++ b/data/assets/js/util.js @@ -27,6 +27,26 @@ jQuery(function($) { }); }; + window.gripe = function(r, action) { + // get error + var error = r.responseText; + + // handle json response + try { + var data = $.parseJSON(r.responseText); + if (data.error) + error = data.error; + } catch (e) {} + + // build prefix + var prefix = 'Error: '; + if (action) + prefix += " Couldn't " + action + ": "; + + // display error message + alert(prefix + error); + } + LuigiTemplate.FILTERS.json = function(val) { return JSON.stringify(val); }; -- cgit v1.2.3