Start of work on supporting creating HTML elements.

This commit is contained in:
David Botton 2020-12-16 17:19:02 -05:00
parent 82b1b7b84d
commit d2186397c1
4 changed files with 64 additions and 29 deletions

View file

@ -57,7 +57,7 @@ script."
(defvar *client-handler* nil "Clack 'handler' for socket traffic")
(defvar *on-connect-handler* nil "New connection event handler.")
(defvar *new-id* 0 "Connection IDs")
(defvar *new-id* 0 "Last issued connection or script IDs")
(defvar *connections* (make-hash-table) "Connections to IDs")
(defvar *connection-ids* (make-hash-table) "IDs to connections")
@ -77,6 +77,7 @@ script."
(defun generate-id ()
"Generate unique ids for use in connections and sripts."
;; needs mutex or atomic
(incf *new-id*))
;;;;;;;;;;;;;;;;;;;;

View file

@ -13,6 +13,7 @@
(mgl-pax:define-package :clog
(:documentation "The Common List Omnificent GUI - Parent package")
(:local-nicknames (:cc :clog-connection))
(:use #:cl #:mgl-pax))
(in-package :clog)
@ -36,9 +37,13 @@ application."
(shutdown function)
(set-on-connect function)
"CLOG base class"
(clog class)
"CLOG Low Level bindings"
(attach function)
(create-with-html function)
(place-after function)
"CLOG utilities"
@ -48,23 +53,18 @@ application."
(open-browser function))
(defclass base ()
(defclass clog ()
((connection-id
:accessor connection-id
:initarg :connection-id)
(web-id
:accessor web-id
:initarg :web-id)))
(defun attach (connection-id web-id)
(make-instance 'base :connection-id connection-id :web-id web-id))
(html-id
:accessor html-id
:initarg :html-id)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Implementation - clog
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;
;; initialize ;;
;;;;;;;;;;;;;;;;
@ -78,7 +78,7 @@ application."
"Inititalze CLOG on a socket using HOST and PORT to serve BOOT-FILE as
the default route to establish web-socket connections and static files
located at STATIC-ROOT."
(clog-connection:initialize on-connect-handler
(cc:initialize on-connect-handler
:host host
:port port
:boot-file boot-file
@ -89,15 +89,50 @@ located at STATIC-ROOT."
;;;;;;;;;;;;;;
(defun shutdown ()
(clog-connection:shutdown))
"Shutdown CLOG."
(cc:shutdown))
;;;;;;;;;;;;
;; attach ;;
;;;;;;;;;;;;
(defun attach (connection-id id)
"Create a new clog object and attach an existing element with HTML-ID on
CONNECTION-ID to it. The HTML-ID must be unique."
(make-instance 'clog :connection-id connection-id :html-id id))
;;;;;;;;;;;;;;;;;;;;;;
;; create-with-html ;;
;;;;;;;;;;;;;;;;;;;;;;
(defun create-with-html (connection-id html)
"Create a new clog object and attach it to HTML on CONNECTION-ID. There must be
a single outer block that will be set to an internal id"
(let ((web-id (cc:generate-id)))
(cc:execute
connection-id (format nil "clog['~A']=$(\"~A\"); clog['~A'].first().prop('id','~A');"
web-id html web-id web-id))
(attach connection-id web-id)))
;;;;;;;;;;;;;;;;;
;; place-after ;;
;;;;;;;;;;;;;;;;;
(defun place-after (parent child)
(let ((jq (if parent
(format nil "$(clog['~A'])" (html-id parent))
(format nil "$('body')"))))
(cc:execute (connection-id child)
(format nil "~A.after(clog['~A'])" jq (html-id child))))
child)
;;;;;;;;;;;;;;;
;; alert-box ;;
;;;;;;;;;;;;;;;
(defun alert-box (id message)
(clog-connection:execute
id (format nil "alert('~A');" (clog-connection:escape-string message))))
(cc:execute
id (format nil "alert('~A');" (cc:escape-string message))))
;;;;;;;;;;;;;;;;;;
;; open-browser ;;

File diff suppressed because one or more lines are too long

View file

@ -5,11 +5,12 @@
(in-package :test-clog)
(defun on-connect (id)
(alert-box id "We are here"))
(place-after
(place-after nil (create-with-html id "<button>test</botton>"))
(create-with-html id "<H2>Cool!</H2>")))
(defun test ()
(print "Init connection")
(initialize #'on-connect :boot-file "/debug.html")
(print "Open browser")
(open-browser)
)
(open-browser))