From 74613c4d907c485040691ef4f863a92effa09080 Mon Sep 17 00:00:00 2001 From: "Pasha.K" Date: Wed, 14 Jul 2021 23:06:15 +0300 Subject: [PATCH] register-hash-cb function added, version bump --- README.md | 14 ++++++++++++++ omg.asd | 2 +- omgui.lisp | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 70fcbe3..44c8a03 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,20 @@ But many of useful JS and DOM-manipulating functions are provided by `omgui` pac - `(load-js-script url)` - load JS script from `url`. +- `(register-hash-cb hash cb)` - register a callback to call when URL hash part changes. With this function you can, for example, automatically mark session as debug one, when you open URL like `http://localhost:7500/#debug`: + + ``` + (defun-r debug-me () + (set-debug-session (current-session-id))) + + (defun-r my-boot () + (register-hash-cb "#debug" (lambda () (debug-me)))) + + (add-to-boot '(my-boot)) + ``` + + If the page is loaded with the registered hash part, the callback will be executed immediately during `register-hash-cb` call. + ### Modal dialogs You can display modal dialog in the browser using the `modal-dialog` macro: diff --git a/omg.asd b/omg.asd index c75e379..00db830 100644 --- a/omg.asd +++ b/omg.asd @@ -1,6 +1,6 @@ (defsystem "omg" :description "A Common Lisp library to build fully dynamic web interfaces" - :version "0.1.1" + :version "0.1.2" :author "Pavel Kaygorodov " :licence "GPLv3" :depends-on ("clack" "websocket-driver-server" "bordeaux-threads" "trivial-utf-8") diff --git a/omgui.lisp b/omgui.lisp index 9ddae3f..faba9f0 100644 --- a/omgui.lisp +++ b/omgui.lisp @@ -31,6 +31,7 @@ page-height parent-element prevent-page-close + register-hash-cb remove-element visible-width visible-height @@ -515,3 +516,19 @@ (load-js-script "https://www.youtube.com/iframe_api")) (make-player)))) nil) + + +(defparameter-f *hash-change-cbs* nil) + +(defun-f register-hash-cb (hsh cb) + (labels ((mcb (&optional ev) + (let* ((hs (jscl::oget (jscl::%js-vref "location") "hash")) + (cb (assoc hs *hash-change-cbs* :test #'equal))) + (jslog hs) + (if cb (funcall (cdr cb)))))) + (if (not *hash-change-cbs*) + (setf (jscl::oget (jscl::%js-vref "window") "onhashchange") + #'mcb)) + (push (cons hsh cb) *hash-change-cbs*) + (mcb)) + nil)