From 932cbac4f690118926c12614c8662bbe38809dd8 Mon Sep 17 00:00:00 2001 From: Benjamin Zaporzan Date: Thu, 7 Jun 2018 20:28:43 -0400 Subject: [PATCH] Code Cleanup --- src-cljs/flyer/core.cljs | 3 +++ src-cljs/flyer/storage.cljs | 11 ++++++++- src-cljs/flyer/traversal.cljs | 7 +++++- src-cljs/flyer/utils.cljs | 18 +++++++++----- src-cljs/flyer/window.cljs | 44 +++++++++++++++++------------------ src-cljs/flyer/wrapper.cljs | 3 +++ 6 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src-cljs/flyer/core.cljs b/src-cljs/flyer/core.cljs index 4ca8e0e..01921b7 100644 --- a/src-cljs/flyer/core.cljs +++ b/src-cljs/flyer/core.cljs @@ -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) diff --git a/src-cljs/flyer/storage.cljs b/src-cljs/flyer/storage.cljs index d4eeda0..c52081d 100644 --- a/src-cljs/flyer/storage.cljs +++ b/src-cljs/flyer/storage.cljs @@ -1,19 +1,25 @@ (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" [] @@ -21,12 +27,15 @@ (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))) diff --git a/src-cljs/flyer/traversal.cljs b/src-cljs/flyer/traversal.cljs index c1ac1c8..81cb2e5 100644 --- a/src-cljs/flyer/traversal.cljs +++ b/src-cljs/flyer/traversal.cljs @@ -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] @@ -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) diff --git a/src-cljs/flyer/utils.cljs b/src-cljs/flyer/utils.cljs index 12ff4e0..468891d 100644 --- a/src-cljs/flyer/utils.cljs +++ b/src-cljs/flyer/utils.cljs @@ -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))) diff --git a/src-cljs/flyer/window.cljs b/src-cljs/flyer/window.cljs index 0281bac..afee499 100644 --- a/src-cljs/flyer/window.cljs +++ b/src-cljs/flyer/window.cljs @@ -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))) diff --git a/src-cljs/flyer/wrapper.cljs b/src-cljs/flyer/wrapper.cljs index 2250166..ebea44b 100644 --- a/src-cljs/flyer/wrapper.cljs +++ b/src-cljs/flyer/wrapper.cljs @@ -1,6 +1,7 @@ (ns flyer.wrapper (:require [flyer.messaging :as msg])) + (defn ^:export apply-js-obj "used to apply javascript object to function parameters" [f obj] @@ -8,11 +9,13 @@ 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]