-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
299 lines (272 loc) · 8.7 KB
/
main.js
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* refapp
* @author E-Com Club <[email protected]>
* @license MIT
*/
(function ($) {
'use strict'
// require 'partials/consume-refract.js'
/* global consumeRefract */
// setup as jQuery plugin
$.fn.refapp = function (refracts, Options) {
// main DOM element
var $app = this
// default options object
var options = {
// styles
asideClasses: '',
articleClasses: '',
// base URL hash
baseHash: '/',
// parse Markdown to HTML
mdParser: function (md) { return md },
// optional callback function for loaded refracts
refractCallback: null,
// callback function for endoint actions
actionCallback: function (req, res) { console.log(req, res) }
}
if (Options) {
Object.assign(options, Options)
}
// random base ID for elements
var elId = Math.floor(Math.random() * (9999 - 1000)) + 1000
// create DOM elements
// main app Components
var $article = $('<article>', {
'class': options.articleClasses
})
var $list = $('<div>', {
'class': 'list-group my-3 mr-md-5 pr-lg-3 pr-xl-5 ref-resources'
})
var $resources = []
var $ol = $('<ol>', {
'class': 'ref-anchors'
})
var $aside = $('<aside>', {
'class': options.asideClasses,
html: [
'<h5>Summary</h5>',
$ol,
'<h5>Reference</h5>',
$list
]
})
// console.log(this)
// console.log(refract)
// current resource anchor
var baseHash = options.baseHash
var currentAnchor, waitingHash
$(window).on('hashchange', function () {
if (currentAnchor && !(new RegExp('^#' + currentAnchor).test(location.hash))) {
// resource changed
// try to route
route()
}
})
var route = function () {
var hash = location.hash
if (hash) {
if (hash.slice(baseHash.length + 1).indexOf('/') === -1) {
// should have at least one bar
window.location.hash = hash + '/'
return route()
}
// test refract fragment route
for (var i = 0; i < $resources.length; i++) {
var $link = $resources[i]
if (new RegExp('^#' + $link.data('anchor')).test(hash)) {
// found
if (!$link.hasClass('active')) {
// save current hash for further update
waitingHash = hash
// start routing
$link.click()
}
return true
}
}
// rewrite Apiary default hashes
var parts = hash.match(/^#(reference|introduction)\/(.*)$/)
if (parts) {
window.location.hash = '#' + baseHash + parts[2]
// route again
return route()
}
}
// not routed
return false
}
// get each refract fragment
if (Array.isArray(refracts)) {
var firstRefract = true
var processRefract = function (Refract, anchor) {
// reset DOM
$ol.slideUp(200, function () {
$(this).html('')
// fade article content
$article.fadeOut(200, function () {
$(this).html('')
// start treating Refract JSON (Drafter output)
// API Elements format
/* Reference
https://github.com/apiaryio/drafter
https://api-elements.readthedocs.io/en/latest/
*/
// consume refract tree
var refract = Object.assign({}, Refract)
while (refract) {
// root API Element fixed
refract = consumeRefract(refract, anchor, options, $article, $ol)
/*
if (!options.apiTitle) {
// try to set API title
options.apiTitle = apiElementMeta(refract, 'title')
}
*/
}
// show content again
$article.fadeIn('slow', function () {
if (firstRefract) {
firstRefract = false
} else {
// scroll to content
$('html, body').animate({ scrollTop: $article.offset().top - 20 }, 'slow')
}
if (typeof options.refractCallback === 'function') {
// send refract object
options.refractCallback(Refract)
}
})
$ol.slideDown('slow', function () {
if (waitingHash) {
if (waitingHash !== location.hash) {
var $link = $(this).find('a[href="' + waitingHash + '"]')
if ($link.length) {
setTimeout(function () {
// need to call native DOM click()
// https://stackoverflow.com/questions/34174134
$link[0].click()
}, 100)
}
}
// reset
waitingHash = null
}
})
// set links to new browser tab
$article.find('a').filter(function () {
var attr = $(this).attr('href')
return (attr.charAt(0) !== '#' && attr !== 'javascript:;')
}).attr('target', '_blank')
})
})
}
var requestFailed = function (jqxhr, textStatus, err) {
// AJAX error
alert('Cannot GET Refract JSON: ' + textStatus)
console.error(err)
}
var getRefract = function (i, anchor) {
// try to GET JSON file
var url = refracts[i].src
if (typeof url === 'string' && url !== '') {
$.getJSON(url, function (data) { processRefract(data, anchor) })
.fail(requestFailed)
} else {
console.error(new Error('Invalid or undefined src string on refract (' + i + '), ignored'))
}
}
// list all fragments
for (var i = 0; i < refracts.length; i++) {
if (typeof refracts[i] === 'object' && refracts[i] !== null) {
var title = refracts[i].title
if (title) {
title = typeof title === 'object' ? title.content : title
if (typeof title === 'string' && title.trim() !== '') {
// generate anchor for this recfract fragment
var anchor = baseHash + title.toLowerCase().replace(/\s/g, '-') + '/'
$resources.push($('<a>', {
'class': 'list-group-item list-group-item-action',
href: 'javascript:;',
text: title,
'data-anchor': anchor,
click: (function (i, anchor) {
// local vars
return function () {
// clear last active
$list.find('a.active').removeClass('active')
$(this).addClass('active')
// update content
getRefract(i, anchor)
// scroll to top
$('html, body').animate({ scrollTop: 0 }, 'slow', 'swing', function () {
// update current anchor
currentAnchor = anchor
// update URL hash
window.location.hash = '#' + anchor
})
}
}(i, anchor))
}))
}
}
// add resources to list DOM
$list.append($resources)
}
}
}
// create collapsable elements for navs
var divId = 'ref-anchors-' + elId
$aside.addClass('collapse d-md-block').attr('id', divId)
var $sidebar = [
$('<a>', {
'class': 'btn btn-xl btn-outline-primary btn-block d-md-none mb-3',
'data-toggle': 'collapse',
'aria-expanded': 'false',
'aria-control': divId,
href: '#' + divId,
role: 'button',
html: '<i class="ti-angle-down mr-1"></i> Content'
}),
$aside
]
// Reference App body HTML
var body = []
// optional API title
if (options.apiTitle) {
body.push($('<h1>', {
'class': 'mt-3 mb-4 text-muted',
text: options.apiTitle
}))
}
body.push($article)
// update DOM
$app.html($('<div>', {
'class': 'container',
// compose Reference App layout
html: $('<div>', {
'class': 'row',
html: [
$('<div>', {
'class': 'col-md-5 col-xl-4 ref-sidebar',
html: [
$('<section>', {
'class': 'py-4 sticky-top',
html: $sidebar
})
]
}),
$('<div>', {
'class': 'col-md-7 col-xl-8 ref-body',
html: body
})
]
})
}))
// first route
if (!route()) {
// start with the first refract fragment
$resources[0].click()
}
}
}(jQuery))