Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 80 lines (64 sloc) 2.457 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
(ns hello
  (:require
   [goog.dom :as dom]
   [goog.object :as goog-object]
   [goog.events.Event :as goog-event]
   [goog.events.EventType :as goog-event-type]
   [goog.ui.ColorButton :as color-button]
   [goog.ui.Tab :as gtab]
   [goog.ui.TabBar :as gtabb]))

;; -------------------------= State =-----------------------
(def database-connected? (atom false))
(def messaging-connected? (atom false))

;; --------------------------= Widgets =-----------------------------
(def database-button (doto (goog.ui.ColorButton. "Database")
                 (.setTooltip "Database Connection Status")
                 (.setValue "red")))

(def messaging-button (doto (goog.ui.ColorButton. "Messaging")
                        (.setTooltip "Messaging Connection Status")
                        (.setValue "red")))

(def tabbar (goog.ui.TabBar.))

;; -----------------------= Control =---------------------
;; Event handling for the UI
(def events (.getValues goog.object goog.ui.Component/EventType))

(defn handle-tab-select [tabbar e]
  (let [tab (.target e)
        content (.getElement goog.dom (str (. tabbar (getId)) "_content"))]
    (.setTextContent goog.dom content (. tab (getCaption)))))

(defn button-color
  "Associate a colour with the states"
  [x]
  (if x "green" "red"))

(defn toggle-state!
  "Simulate trivial connect/disconnect functionality for the buttions"
  [x]
  (swap! x not))

(defn handle-button-push [s e]
  (.setValue (.target e)
             (button-color (toggle-state! s))))

;; ---------------------= MAIN =------------------------
;; Main entry function
(defn ^:export main []
  ;; Populate a DOM via decoration.
  (.decorate tabbar (.getElement goog.dom "top"))

  ;; Populate a DOM via rendering.
  (.render database-button (.getElement goog.dom "buttons"))
  (.render messaging-button (.getElement goog.dom "buttons"))

  ;; Wire up the events
  ;; The tabbar event
  (.listen goog.events
           tabbar
           goog.ui.Component.EventType/SELECT
           (partial handle-tab-select tabbar))

  ;; The database button
  (.listen goog.events
           database-button
           goog.ui.Component.EventType/ACTION
           (partial handle-button-push
                    database-connected?))

  ;; The messaging button
  (.listen goog.events
           messaging-button
           goog.ui.Component.EventType/ACTION
           (partial handle-button-push
                    messaging-connected?)))

Something went wrong with that request. Please try again.