forked from express-monocle/express-monocle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jade
217 lines (176 loc) · 6.45 KB
/
index.jade
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
doctype html
html
head
title express-monoicle
link(rel='stylesheet', href='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css', integrity='sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi', crossorigin='anonymous')
style.
.container-fluid {
margin-top: 25px;
}
.card {
display: inline-block;
width: 98%;
}
.tag {
text-align: right;
}
.tag-method {
margin-right: 5px;
}
#routes-container {
box-shadow: 10px 10px 5px #f2f2f2;
}
#routes {
margin: 10px;
}
#routes li:hover {
cursor: pointer;
}
.pill-ctx {
margin: 0;
padding: 0;
}
#middlewares, #middlewares-header {
display: flex;
flex-flow: row wrap;
list-style-type: none;
}
#middlewares li, #middlewares-header li {
text-align: center;
width: 50%;
}
#middlewares-header li {
font-size: 2em;
font-weight: bolder;
}
body
.container-fluid
.col-xs-4
img.img-fluid(src="http://res.cloudinary.com/lc8bqflyz/image/upload/v1478126894/Screen_Shot_2016-11-02_at_2.43.29_PM_nmvgoc.png")
#routes-container
ul#routes.list-group
.col-xs-8
ul#middlewares-header
li request
li response
ul#middlewares
script.
//- Data from server
const data = !{JSON.stringify(globalMonoStore)}
// Set up references to DOM nodes
const routesContainerEl = document.getElementById('routes-container')
const routesListEl = document.getElementById('routes')
const middlewaresHeaderEl = document.getElementById('middlewares-header')
const middlewaresListEl = document.getElementById('middlewares')
const init = () => {
//- Dynamically sets the positioning of our elements
routesContainerEl.style.height = `${window.innerHeight - 200}px`
middlewaresHeaderEl.style['margin-top'] = `${routesContainerEl.offsetTop - 65}px`
for (let i = data.length - 1; i >= 0; i--) {
routesListEl.appendChild(createRouteEl(data[i], i))
}
}
routesListEl.addEventListener('click', e => {
if (e.target.localName === 'li') {
const idx = e.target.id.replace(/.+-/, '')
const { changes, path } = data[idx]
resetMiddlewares()
let count = 0
for (const key in changes.req) {
if (+key > count) count = +key
}
for (const key in changes.res) {
if (+key > count) count = +key
}
for (let position = 1; position <= count; position++) {
const fnName = changes.req[0].route.stack[position - 1].name
middlewaresListEl.appendChild(createMiddlewareEl(changes.req[position], fnName, position))
middlewaresListEl.appendChild(createMiddlewareEl(changes.res[position], fnName, position))
}
}
})
// DOM Helpers
const resetMiddlewares = () => middlewaresListEl.innerHTML = ''
// DOM Creators
const createRouteEl = ({ changes, method, path }, idx) => {
const routeEl = document.createElement('li')
const routeContent = document.createTextNode(path)
routeEl.id = `route-${idx}`
routeEl.className = 'list-group-item'
//- Left pills
const methodEl = document.createElement('span')
const methodContent = document.createTextNode(method)
switch (method) {
case 'GET':
methodEl.className = 'tag tag-success tag-method label-pill float-xs-left'
break
case 'POST':
methodEl.className = 'tag tag-warning tag-method label-pill float-xs-left'
break
case 'PUT':
methodEl.className = 'tag tag-info tag-method label-pill float-xs-left'
break
case 'DELETE':
methodEl.className = 'tag tag-primary tag-method label-pill float-xs-left'
break
default:
methodEl.className = 'tag tag-default tag-method label-pill float-xs-left'
}
methodEl.appendChild(methodContent)
//- Right pills
const counterEl = document.createElement('span')
const counterReqEl = document.createElement('p')
const counterReqContent = document.createTextNode(`req: ${Object.keys(changes.req).length - 1}`)
counterReqEl.className = 'pill-ctx'
counterReqEl.appendChild(counterReqContent)
//- Fix bug with including empty res object
let lastKey
let keyCount = 0
for (const key in changes.res) {
lastKey = key
keyCount++
}
const counterResEl = document.createElement('p')
let counterResContent;
if (Object.keys(changes.res[lastKey]).length < 1) {
counterResContent = document.createTextNode(`res: ${keyCount - 2}`)
} else {
counterResContent = document.createTextNode(`res: ${keyCount - 1}`)
}
counterResEl.className = 'pill-ctx'
counterResEl.appendChild(counterResContent)
counterEl.className = 'tag bg-primary label-pill float-xs-right'
counterEl.appendChild(counterReqEl)
counterEl.appendChild(counterResEl)
routeEl.appendChild(methodEl)
routeEl.appendChild(counterEl)
routeEl.appendChild(routeContent)
return routeEl
}
const createMiddlewareEl = (changes, path, position) => {
const middlewareEl = document.createElement('li')
const card = document.createElement('div')
card.className = 'card'
const cardBlock = document.createElement('div')
cardBlock.className = 'card-block'
//- Create req objects
const middlewareNameEl = document.createElement('h4')
const middlewareNameContent = document.createTextNode(`${path} [${position}]`)
middlewareNameEl.className = 'card-title'
middlewareNameEl.appendChild(middlewareNameContent)
cardBlock.appendChild(middlewareNameEl)
for (const prop in changes) {
//- Shows objects
let value = changes[prop]
if (typeof value === 'object') value = JSON.stringify(value)
const changedItemEl = document.createElement('p')
const changedItemContent = document.createTextNode(`${prop}: ${value}`)
changedItemEl.className = 'card-text'
changedItemEl.appendChild(changedItemContent)
cardBlock.appendChild(changedItemEl)
}
card.appendChild(cardBlock)
middlewareEl.appendChild(card)
return middlewareEl
}
init()