aboutsummaryrefslogtreecommitdiff
path: root/html/DOM_helpers.js
diff options
context:
space:
mode:
Diffstat (limited to 'html/DOM_helpers.js')
-rw-r--r--html/DOM_helpers.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/html/DOM_helpers.js b/html/DOM_helpers.js
index 88092e5..9e64956 100644
--- a/html/DOM_helpers.js
+++ b/html/DOM_helpers.js
@@ -85,3 +85,50 @@ function clone_template(template_id)
return result_object;
}
#EXPORT clone_template
+
+function Showable(on_show_cb, on_hide_cb) {
+ this.shown = false;
+
+ /*
+ * Wrap the requested callback into one that only executes it if showable is
+ * not shown.
+ */
+ this.when_hidden = cb => {
+ const wrapped_cb = (...args) => {
+ if (!this.shown)
+ return cb(...args);
+ }
+ return wrapped_cb;
+ }
+
+ this.show = () => {
+ if (this.shown)
+ return false;
+
+ this.shown = true;
+
+ try {
+ on_show_cb();
+ } catch(e) {
+ console.error(e);
+ }
+
+ return true;
+ }
+
+ this.hide = () => {
+ if (!this.shown)
+ return false;
+
+ this.shown = false;
+
+ try {
+ on_hide_cb();
+ } catch(e) {
+ console.error(e);
+ }
+
+ return true;
+ }
+}
+#EXPORT Showable