Skip to content

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benzap committed Jun 8, 2018
1 parent 8237015 commit 932cbac
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src-cljs/flyer/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
(:require [flyer.utils :as utils]
[flyer.wrapper]))


(defonce VERSION "1.1.2")


;;if the window is external, we should re-register it for cases where
;;the window gets refreshed
(when (not (nil? (.-opener js/window)))
(register-external))


;;places the flyer.wrapper.subscribe and flyer.wrapper.broadcast
;;functions within flyer namespace.
(js/goog.exportProperty (aget js/window "flyer") "subscribe", flyer.wrapper/subscribe)
Expand Down
11 changes: 10 additions & 1 deletion src-cljs/flyer/storage.cljs
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
(ns flyer.storage
"includes functions for storing window information"
"includes functions for storing window information. This window
information is stored in the parent window under the
`window-list-key`."
(:require [flyer.utils :as utils]))

(declare get-window-refs)


(def storage (utils/get-main-parent))


(def window-list-key "flyer_WindowReferences")


(defn init-window-refs
"Initializes our window references"
[]
(when (nil? (get-window-refs))
(aset storage window-list-key (set nil))))


(defn get-window-refs
"Returns the window references, or an empty set"
[]
(or
(aget storage window-list-key)
nil))


(defn insert-window-ref! [window]
(init-window-refs)
(aset storage window-list-key
(conj (get-window-refs) window)))


(defn remove-window-ref! [window]
(init-window-refs)
(aset storage window-list-key
(disj (get-window-refs) window)))

7 changes: 6 additions & 1 deletion src-cljs/flyer/traversal.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[flyer.utils :as utils]
[flyer.storage :as storage]))


(defn list-frame-windows
"returns a list of all of the frames that the provided window has"
[window]
Expand All @@ -17,14 +18,18 @@
(conj list (aget framelist i)))
list))))


(defn list-external-windows
"returns a list of all external windows linked to the current
window"
[]
(vec (storage/get-window-refs)))


(defn generate-broadcast-list
"generates a list of windows that we wish to send the message to"
"generates a list of windows that we wish to send the message to.
TODO: Replace with mapcat? Consider using specter for traversal?"
([current-window]
(let [current-frame-list
(list-frame-windows current-window)
Expand Down
18 changes: 12 additions & 6 deletions src-cljs/flyer/utils.cljs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
(ns flyer.utils)


(defn is-frame?
"Determines whether the current window is a frame within a set of
frames currently showing."
frames currently showing."
[window]
(let [parent-window (.-parent window)
current-location (.-location window)
parent-location (.-location parent-window)]
(not= current-location parent-location)))


(defn is-external-window?
"Determines whether the current window was opened externally"
[window]
(not (nil? (.-opener window))))


(defn get-main-parent
"Finds the main parent by traversing down till it has been
determined that it is the parent"
determined that it is the parent"
([window]
(cond
(is-external-window? window) (get-main-parent (.-opener window))
(is-frame? window) (get-main-parent (.-parent window))
:else window))
(loop [window window]
(if (is-external-window? window)
(recur (.-opener window))

(if (is-frame? window)
(recur (.-parent window))
window))))
([] (get-main-parent js/window)))
44 changes: 22 additions & 22 deletions src-cljs/flyer/window.cljs
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
(ns flyer.window
(:require [flyer.storage :as storage]
[flyer.utils :as utils]
[goog.events :as events]))
(:require
[clojure.string :as str]
[flyer.storage :as storage]
[flyer.utils :as utils]
[goog.events :as events]))


(def this-window js/window)


(def external-window-list
"based on the current window, it will store references to any
external windows that are opened using the 'open' function"
(atom {}))

(defn gen-window-options [& options]
(if (even? (count options))
(let [options-twos
(loop [options options
options-vonc []]
(if (not (empty? options))
(recur (rest (rest options))
(conj options-vonc
(map #(if (keyword? %) (name %) %)
(take 2 options))))
options-vonc))
options-inter
(map #(apply str (interpose "=" %)) options-twos)]
(apply str (interpose ", " options-inter)))
(.error js/console "options needs an even number of terms")))

(defn gen-window-options
[& options]
(as-> options $
(partition 2 $)
(map (fn [[x y]] (str/join "=" [(name x) y])) $)
(str/join ", " $)))


(defn ^:export open [url name & options]
(let [options-str (cond
(and (= (count options) 1)
(= (type (first options)) js/String))
(let [options-str (if (and (= (count options) 1)
(= (type (first options)) js/String))
(first options)
:else
(apply gen-window-options options))

window (.open this-window url name options-str)]

(storage/insert-window-ref! window)

(events/listen
window (.-BEFOREUNLOAD events/EventType)
(fn [event]
(storage/remove-window-ref! window)
nil))

window))


(defn register-external
([window] (storage/insert-window-ref! window))
([] (register-external js/window)))
Expand Down
3 changes: 3 additions & 0 deletions src-cljs/flyer/wrapper.cljs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
(ns flyer.wrapper
(:require [flyer.messaging :as msg]))


(defn ^:export apply-js-obj
"used to apply javascript object to function parameters"
[f obj]
(let [obj (js->clj obj :keywordize-keys true)
obj-vec (reduce concat (vec obj))]
(apply f obj-vec)))


(defn ^:export broadcast
"Wrapper around broadcast for javascript"
[obj]
(apply-js-obj msg/broadcast obj))


(defn ^:export subscribe
"Wrapper around subscribe for javascript"
[obj]
Expand Down

0 comments on commit 932cbac

Please sign in to comment.