src/views/components/tab.lisp

DOWNLOAD
(defpackage murja.views.components.tab
  (:import-from :lisp-fixup :partial)
  (:export :tabs)
  (:use :cl :murja.newui)
  (:local-nicknames (:tabmodel :murja.models.tab)))

(in-package :murja.views.components.tab)

;; (defvar *selected-tab* nil)

(defun tabs (firstly-selected-title tablist tab-params ;; &key onleave ;; how did I plan to call js callbacks here again? 
	     )
  "Takes in a Title -> (Component | State mapping, returns a component that's supposed to look like the tab component used by the old Elm app"

  ;; (sort tablist (lisp-fixup:compose #'string-lessp #'tabmodel:title))
  (with-state ((selected-title firstly-selected-title)) ()
    (let ((selected-tab (first (remove-if-not (lambda (tab)
						(equalp (tabmodel:title tab)
							selected-title))
					      tablist))))
      (c :div (:id "topbar" :class "tabs")
	 (component :ul (:class "tab-headers")
		    (map 'list
			 (lambda (tab)
			   ;; what if we made (:li () (:a () ...) ...) that'd be changed into a (:li () ...) later if javascript exists?
			   (c :li (:class (format nil "tab-header delinkify ~a" (when (string= (tabmodel:title tab) selected-title) "tab-selected"))
				   :data-href (tabmodel:tab-route tab))
			      (c :a (:href (tabmodel:tab-route tab)
				     :onclick (e (lambda (unused)
						   (declare (ignore unused))
						   (setf selected-title (tabmodel:title tab)))
						 :post-js (format nil "history.pushState({}, '', '~a')" (tabmodel:tab-route tab))))
				 (tabmodel:title tab))))
			 tablist))
	 (progn
	   ;; (format t "selected-tab: ~a~%" selected-tab)
	   ;; (format t "tab-params: ~a~%" tab-params)
	   ;; (setf *selected-tab* selected-tab)
	   ;; (format t "selected tabs component ~a~%" (tabmodel:tab-component selected-tab))
	   (apply (tabmodel:tab-component selected-tab) tab-params))))))