From 139998172feeb4b667bb75060f0db82162954d7b Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 4 Sep 2020 11:25:11 -0700 Subject: [PATCH] Add spinner.BAIL for cancelling spinner without launching callback. --- .../etiquette_flask/static/js/spinner.js | 29 +++++++++++++++---- .../etiquette_flask/templates/clipboard.html | 3 +- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/frontends/etiquette_flask/static/js/spinner.js b/frontends/etiquette_flask/static/js/spinner.js index 81ba30a..eeacd22 100644 --- a/frontends/etiquette_flask/static/js/spinner.js +++ b/frontends/etiquette_flask/static/js/spinner.js @@ -1,5 +1,17 @@ var spinner = {}; +/* +In general, spinners are used for functions that launch a callback, and the +callback will close the spinner after it runs. But, if your initial function +decides not to launch the callback (insufficient parameters, failed clientside +checks, etc.), you can have it return spinner.BAIL and the spinners will close +immediately. Of course, you're always welcome to use +window[button.dataset.spinnerCloser](), but this return value means you don't +need to pull the button into a variable, as long as you weren't using the +return value anyway. +*/ +spinner.BAIL = "spinner.BAIL"; + spinner.Spinner = function Spinner(element) { @@ -32,10 +44,12 @@ function Spinner(element) spinner.spinner_button_index = 0; spinner.button_spinner_groups = {}; -// When a group member is closing, it will call the closer on all other members -// in the group. Of course, this would recurse forever without some kind of -// flagging, so this dict will hold group_id:true if a close is in progress, -// and be empty otherwise. +/* +When a group member is closing, it will call the closer on all other members +in the group. Of course, this would recurse forever without some kind of +flagging, so this dict will hold group_id:true if a close is in progress, +and be empty otherwise. +*/ spinner.spinner_group_closing = {}; spinner.add_to_spinner_group = @@ -161,7 +175,12 @@ function init_button_with_spinner() { window[button.dataset.spinnerOpener](); } - return wrapped_onclick(); + const ret = wrapped_onclick(); + if (ret === spinner.BAIL) + { + window[button.dataset.spinnerCloser](); + } + return ret; } spinner.spinner_button_index += 1; diff --git a/frontends/etiquette_flask/templates/clipboard.html b/frontends/etiquette_flask/templates/clipboard.html index 62ac71b..2695b4d 100644 --- a/frontends/etiquette_flask/templates/clipboard.html +++ b/frontends/etiquette_flask/templates/clipboard.html @@ -330,8 +330,7 @@ function refresh_metadata_form() { if (photo_clipboard.clipboard.size == 0) { - window[refresh_metadata_button.dataset.spinnerCloser](); - return; + return spinner.BAIL; } let photo_ids = Array.from(photo_clipboard.clipboard);