-
Notifications
You must be signed in to change notification settings - Fork 12
/
window.cljs
48 lines (42 loc) · 1.57 KB
/
window.cljs
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
(ns flyer.window
(:require [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 ^:export open [url name & options]
(let [options-str (cond
(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)))