summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/htdocs/luci-static/resources/ui.js
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2019-08-14 22:54:59 +0200
committerJo-Philipp Wich <jo@mein.io>2019-08-14 22:58:15 +0200
commit7179d2e6dd1d9baf1f7e8f6862f702588eb5d69c (patch)
tree45d64c207c7fbc5330a20761153e244422b0f1dc /modules/luci-base/htdocs/luci-static/resources/ui.js
parent0d0882aea0e67faaef769c3517f17b1f650f0794 (diff)
luci-base: ui.js: add createHandlerFn() helper
The createHandlerFn() helper function is useful to construct onclick or similar event handling functions. It will add a "spinning" CSS class on the event target element and disable the element, wrap the given function with Promise.resolv() and re-enable the target element once the promise is settled. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base/htdocs/luci-static/resources/ui.js')
-rw-r--r--modules/luci-base/htdocs/luci-static/resources/ui.js23
1 files changed, 23 insertions, 0 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/ui.js b/modules/luci-base/htdocs/luci-static/resources/ui.js
index 23a9d7bfda..e008681710 100644
--- a/modules/luci-base/htdocs/luci-static/resources/ui.js
+++ b/modules/luci-base/htdocs/luci-static/resources/ui.js
@@ -2081,6 +2081,29 @@ return L.Class.extend({
catch (e) { }
},
+ createHandlerFn: function(ctx, fn /*, ... */) {
+ if (typeof(fn) == 'string')
+ fn = ctx[fn];
+
+ if (typeof(fn) != 'function')
+ return null;
+
+ return Function.prototype.bind.apply(function() {
+ var t = arguments[arguments.length - 1].target;
+
+ t.classList.add('spinning');
+ t.disabled = true;
+
+ if (t.blur)
+ t.blur();
+
+ Promise.resolve(fn.apply(ctx, arguments)).then(function() {
+ t.classList.remove('spinning');
+ t.disabled = false;
+ });
+ }, this.varargs(arguments, 2, ctx));
+ },
+
/* Widgets */
Textfield: UITextfield,
Checkbox: UICheckbox,