forked from swlkr/janetdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.janet
118 lines (93 loc) · 3.79 KB
/
helpers.janet
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(import joy :prefix "")
(import uri)
(defn binding-header [binding]
[:vstack {:spacing "xs"}
[:h1 (binding :name)]
[:strong (get-in binding [:package :name] (binding :package))]
[:pre
[:code {:class "clojure"}
(binding :docstring)]]])
(defn binding-show-url [binding &opt url]
(default url "")
(def package (db/find :package (or (binding :package-id) 0)))
(def name (string/replace "?" "_q" (binding :name)))
(if package
(string "/" (package :name) "/" name url)
(string "/" (uri/escape name) url)))
(defn singularize [str n]
(if (one? n)
(string/trimr str "s")
str))
(defn confirm-modal [request & body]
[:div {:x-data "{ modalOpen: false, action: '' }"}
body
[:div {:class "md-modal" ::class "{'md-show': modalOpen}" :x-show "modalOpen" :@click.away "modalOpen = false"}
[:div {:class "md-content"}
[:vstack {:align-x "center"}
[:h3 "Are you sure?"]
[:hstack {:spacing "l" :align-x "center"}
(form-with request {:method "POST" :x-bind:action "action"}
[:input {:type "hidden" :name "_method" :value "DELETE"}]
[:button {:type "submit"}
"Yes, do it"])
[:a {:href "#" :@click.prevent "modalOpen = false"}
"No"]]]]]
[:div {:class "md-overlay" ::class "{'md-show': modalOpen}"}]])
(defn delete-button [request action]
(confirm-modal request
[:a {:href "#"
:@click.prevent (string/format "action = '%s'; modalOpen = true" action)}
"Delete"]))
(defn current-account [request]
(def login (get-in request [:session :login] ""))
(db/find-by :account :where {:login login}))
(defn menu [request]
(def session (get request :session {}))
[:vstack {:spacing "l"}
[:hstack {:stretch "" :spacing "s"}
[:a {:href (url-for :home/index)}
"JanetDocs"]
[:spacer]
[:a {:href (url-for :playground/home)} "Playground"]
[:a {:href (url-for :examples/random)}
"I'm feeling lucky"]
(if (get session :login)
[:hstack {:spacing "m"}
(form-with request (action-for :sessions/destroy)
[:input {:type "hidden" :name "_method" :value "delete"}]
[:input {:type "submit" :value "Sign out"}])]
[:hstack {:spacing "m"}
[:a {:href (string/format "https://github.com/login/oauth/authorize?client_id=%s"
(env :github-client-id))}
"GitHub sign in"]])]])
(defn layout [{:body body :request request}]
(text/html
(doctype :html5)
[:html {:lang "en"}
[:head
[:title "JanetDocs"]
[:meta {:charset "utf-8"}]
[:meta {:name "viewport" :content "width=device-width, initial-scale=1"}]
[:meta {:name "csrf-token" :content (authenticity-token request)}]
[:link {:rel "apple-touch-icon" :sizes "180x180" :href "/apple-touch-icon.png"}]
[:link {:rel "icon" :type "image/png" :sizes "32x32" :href "/favicon-32x32.png"}]
[:link {:rel "icon" :type "image/png" :sizes "16x16" :href "/favicon-16x16.png"}]
[:link {:rel "manifest" :href "/site.webmanifest"}]
[:link {:rel "stylesheet" :href "/css/atom-one-light.css" :media "(prefers-color-scheme: no-preference), (prefers-color-scheme: light)"}]
[:link {:rel "stylesheet" :href "/css/atom-one-dark.css" :media "(prefers-color-scheme: dark)"}]
[:link {:rel "stylesheet" :href "/_pylon.css"}]
[:link {:rel "stylesheet" :href "/_water.css"}]
[:link {:rel "stylesheet" :href "/app.css"}]
[:script {:src "/_highlight.pack.js" :defer ""}]
[:script {:src "/_app.js" :defer ""}]
[:script {:src "/alpine.min.js" :defer ""}]]
[:body
[:vstack {:spacing "xl"}
(menu request)
body
[:spacer]]]]))
(defn /404 [request]
(as-> (layout {:request request
:body [:center
[:h1 "Oops! 404!"]]}) ?
(put ? :status 404)))