diff --git a/chat-demo/Procfile b/chat-demo/Procfile index e754de0..076f0d4 100644 --- a/chat-demo/Procfile +++ b/chat-demo/Procfile @@ -1 +1 @@ -web: lein trampoline run -m chat-demo.core +web: lein trampoline run -m chat-demo.server diff --git a/chat-demo/cljs/bc/core.cljs b/chat-demo/cljs/bc/core.cljs deleted file mode 100644 index f7159f9..0000000 --- a/chat-demo/cljs/bc/core.cljs +++ /dev/null @@ -1,52 +0,0 @@ -(ns bc.core - (:require - [bc.dom-helpers :as dom] - [goog.net.BrowserChannel :as goog-browserchannel] - [goog.events :as events] - [goog.events.KeyCodes :as key-codes] - [goog.events.KeyHandler :as key-handler])) - -(defn handler [] - (let [h (goog.net.BrowserChannel.Handler.)] - (set! (.-channelOpened h) - (fn [channel] - (enable-chat))) - (set! (.-channelHandleArray h) - (fn [x data] - (let [msg (aget data "msg")] - (dom/append (dom/get-element "room") (dom/element :div (str "MSG::" msg)))))) - h)) - -(defn say [text] - (.sendMap channel (doto (js-obj) - (aset "msg" text)) )) - -(defn enable-chat [] - (let [msg-input (dom/get-element "msg-input") - send-button (dom/get-element "send-button") - handler (fn [e] - (say (dom/value msg-input)) - (dom/set-value msg-input ""))] - (dom/set-disabled msg-input false) - (dom/set-disabled send-button false) - (events/listen (goog.events.KeyHandler. msg-input) - "key" - (fn [e] - (when (= (.-keyCode e) key-codes/ENTER) - (handler e)))) - (events/listen send-button - "click" - handler))) - -(def channel (goog.net.BrowserChannel.)) - -(defn ^:export run [] - (events/listen js/window "unload" #(do - (.disconnect channel ()) - (events/removeAll))) - (doto (.. channel getChannelDebug getLogger) - (.setLevel goog.debug.Logger.Level.OFF)) - (doto channel - (.setHandler (handler)) - (.connect "/channel/test" "/channel/bind") - )) diff --git a/chat-demo/cljs/bc/dom-helpers.cljs b/chat-demo/cljs/bc/dom-helpers.cljs deleted file mode 100644 index a65b80e..0000000 --- a/chat-demo/cljs/bc/dom-helpers.cljs +++ /dev/null @@ -1,141 +0,0 @@ -; Copyright (c) Rich Hickey. All rights reserved. -; The use and distribution terms for this software are covered by the -; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) -; which can be found in the file epl-v10.html at the root of this distribution. -; By using this software in any fashion, you are agreeing to be bound by -; the terms of this license. -; You must not remove this notice, or any other, from this software. - -(ns bc.dom-helpers - (:require [clojure.string :as string] - [goog.style :as style] - [goog.dom :as dom] - [goog.dom.classes :as classes] - [goog.dom.forms :as forms] - [goog.fx :as fx] - [goog.fx.dom :as fx-dom] - [goog.Timer :as timer] - )) - -(defn get-element - "Return the element with the passed id." - [id] - (dom/getElement (name id))) - -(defn show-element [e b] - (style/showElement e b)) - -(defn add-remove-class [e add-classes remove-classes] - (classes/addRemove e remove-classes add-classes)) - -(defn get-radio-value [form-name name] - (forms/getValueByName (get-element form-name) name)) - -(defn value [element] - (forms/getValue element)) - -(defn set-value [element] - (forms/setValue element)) - -(defn set-disabled [element disabled] - (forms/setDisabled element disabled)) - -(defn append - "Append all children to parent." - [parent & children] - (do (doseq [child children] - (dom/appendChild parent child)) - parent)) - -(defn set-text - "Set the text content for the passed element returning the - element. If a keyword is passed in the place of e, the element with - that id will be used and returned." - [e s] - (let [e (if (or (keyword? e) (string? e)) (get-element e) e)] - (doto e - (dom/setTextContent s)))) - -(defn normalize-args [tag args] - (let [parts (string/split tag #"(\.|#)") - [tag attrs] [(first parts) - (apply hash-map (map #(cond (= % ".") :class - (= % "#") :id - :else %) - (rest parts)))]] - (if (map? (first args)) - [tag (merge attrs (first args)) (rest args)] - [tag attrs args]))) - -;; TODO: replace call to .strobj with whatever we come up with for -;; creating js objects from Clojure maps. - -(defn element - "Create a dom element using a keyword for the element name and a map - for the attributes. Append all children to parent. If the first - child is a string then the string will be set as the text content of - the parent and all remaining children will be appended." - [tag & args] - (let [[tag attrs children] (normalize-args tag args) - ;; keyword/string mangling screws up (name tag) - parent (dom/createDom (subs tag 1) - (. (reduce (fn [m [k v]] - (assoc m k v)) - {} - (map #(vector (name %1) %2) - (keys attrs) - (vals attrs))) -strobj)) - [parent children] (if (string? (first children)) - [(set-text (element tag attrs) (first children)) - (rest children)] - [parent children])] - (apply append parent children))) - -(defn remove-children - "Remove all children from the element with the passed id." - [parent-el] - (dom/removeChildren parent-el)) - -(defn html - "Create a dom element from an html string." - [s] - (dom/htmlToDocumentFragment s)) - -(defn- element-arg? [x] - (or (keyword? x) - (map? x) - (string? x))) - -(defn build - "Build up a dom element from nested vectors." - [x] - (if (vector? x) - (let [[parent children] (if (keyword? (first x)) - [(apply element (take-while element-arg? x)) - (drop-while element-arg? x)] - [(first x) (rest x)]) - children (map build children)] - (apply append parent children)) - x)) - -(defn insert-at - "Insert a child element at a specific location." - [parent child index] - (dom/insertChildAt parent child index)) - -(defn set-timeout [func ttime] - (timer/callOnce func ttime)) - -(defn set-position [e x y] - (style/setPosition e x y)) - -(defn get-position [e] - (style/getPosition e)) - -(defn toggle-class [el classname] - (classes/toggle el classname)) - -(defn add-class [el classname] - (classes/add el classname)) -(defn remove-class [el classname] - (classes/remove el classname)) diff --git a/chat-demo/project.clj b/chat-demo/project.clj index 02a33c0..740d9de 100644 --- a/chat-demo/project.clj +++ b/chat-demo/project.clj @@ -1,11 +1,44 @@ (defproject chat-demo "0.0.1" - :description "Example for using BrowserChannel and a client side with ClojureScript" - :dependencies [[org.clojure/clojure "1.3.0"] - [ring/ring-core "1.1.0-SNAPSHOT" :exclusions [javax.servlet/servlet-api]] - [org.clojure/clojurescript "0.0-1011" :exclusions [org.clojure/google-closure-library]] - [net.thegeez/google-closure-library "0.0-1698"] - [net.thegeez/clj-browserchannel-server "0.0.4"] - [net.thegeez/clj-browserchannel-jetty-adapter "0.0.1"] - #_[net.thegeez/clj-browserchannel-netty-adapter "0.0.1"] - ] + :description "Example for using BrowserChannel and a client side with ClojureScript" + + :main chat-demo.server + + :dependencies [[org.clojure/clojure "1.8.0"] + [org.clojure/clojurescript "1.8.51"] + [ring/ring-defaults "0.2.0" :exclusions [javax.servlet/servlet-api]] + [ring/ring-devel "1.4.0"] + [compojure "1.4.0"] + [clj-pebble "0.2.0"] + [prismatic/dommy "1.1.0"] + [net.thegeez/clj-browserchannel-server "0.2.1"] + [net.thegeez/clj-browserchannel-jetty-adapter "0.0.8"] + [environ "1.0.3"]] + + :plugins [[lein-cljsbuild "1.1.3"] + [lein-environ "1.0.3"]] + + :clean-targets ^{:protect false} [:target-path + [:cljsbuild :builds :main :compiler :output-dir] + [:cljsbuild :builds :main :compiler :output-to]] + :cljsbuild {:builds {:main + {:source-paths ["src"] + :compiler {:output-to "resources/public/cljs/app.js" + :output-dir "resources/public/cljs/target" + :source-map true + :optimizations :none + :pretty-print true}}}} + + :profiles {:dev {:env {:dev "true"}} + + :uberjar {:env {} + :aot :all + :hooks [leiningen.cljsbuild] + :cljsbuild {:jar true + :builds {:main + {:compiler ^:replace {:output-to "resources/public/cljs/app.js" + :optimizations :advanced + :pretty-print false}}}}}} + + :aliases {"uberjar" ["do" ["clean"] ["uberjar"]]} + ) diff --git a/chat-demo/resources/dev/index-dev.html b/chat-demo/resources/dev/index-dev.html deleted file mode 100644 index 47cdc1a..0000000 --- a/chat-demo/resources/dev/index-dev.html +++ /dev/null @@ -1,37 +0,0 @@ - - -
- - -