-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWrapper.wisp
64 lines (56 loc) · 2.16 KB
/
Wrapper.wisp
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
(defn- sanitize [record & spaces]
(spaces.reduce (fn [r space] (do
(if (aget r space) nil (set! (aget r space) {}))
(aget r space)))
record))
(defn- createNode [elementType]
(let [n (document.createElement elementType)] (do
(set! n.style.padding 0)
(set! n.style.margin 0)
(set! n.style.position :relative)
n)))
(defn- setWrapSize [wrap wh] (let
[setWH (fn [w*, h*, x] (do
(set! (.-width x) (+ w* "px"))
(set! (.-height x) (+ h* "px"))))
ratio (if window.devicePixelRatio window.devicePixelRatio 1)
canvas wrap.firstChild]
(do (setWH (* wh.w ratio) (* wh.h ratio) canvas)
(setWH wh.w wh.h wrap.style)
(setWH wh.w wh.h canvas.style))))
(defn- update [type] (fn [wrap _ model] (do
(setWrapSize wrap model)
(if wrap.__chart (do (wrap.__chart.clear) (wrap.__chart.destroy)))
(set! wrap.__chart
((aget (Chart. (wrap.firstChild.getContext :2d)) type)
model.data model.options))
wrap)))
(defn- render [type NativeElement] (fn [model]
(let [wrap (createNode :div)
canvas (NativeElement.createNode :canvas)]
(do (wrap.appendChild canvas)
(setWrapSize wrap model)
(setTimeout (fn [] ((update type) wrap model model)) 0)
wrap))))
(defn- showRGBA [c]
(+ "rgba(" c._0 "," c._1 "," c._2 "," c._3 ")"))
(defn- chartRaw [NativeElement] (fn [type, w, h, data, options]
(A3 NativeElement.newElement w h {
:ctor "Custom"
:type "Chart"
:render (render type NativeElement)
:update (update type)
:model {:w w :h h :data data :options options}})))
(defn- make [localRuntime] (let
[NativeElement (Elm.Native.Graphics.Element.make localRuntime)
toArray (:toArray (Elm.Native.List.make localRuntime))]
(do (sanitize localRuntime :Native :Chartjs)
(if localRuntime.Native.Chartjs.values
localRuntime.Native.Chartjs.values
(set! localRuntime.Native.Chartjs.values {
:toArray toArray
:showRGBA showRGBA
:chartRaw (F5 (chartRaw NativeElement))})))))
(sanitize Elm :Native :Chartjs)
(set! Elm.Native.Chartjs.make make)
(set! Chart.defaults.global.animation false)