-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
83 lines (82 loc) · 3.05 KB
/
scripts.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
// (c) AMWA 2019
$(document).ready(function() {
$('.page-header pre code, .top-resource-description pre code, .modal-body pre code').each(function(i, block) {
// check if the element contains JSON and store it as a data attribute
try {
var jsonObj = JSON.parse($(block).html());
$(block).data('json', jsonObj);
} catch (e) { }
hljs.highlightBlock(block);
});
$('[data-toggle]').click(function() {
var selector = $(this).data('target') + ' pre code';
$(selector).each(function(i, block) {
hljs.highlightBlock(block);
});
});
// open modal on hashes like #_action_get
$(window).bind('hashchange', function(e) {
var anchor_id = document.location.hash.substr(1); //strip #
var element = $('#' + anchor_id);
// do we have such element + is it a modal? --> show it
if (element.length && element.hasClass('modal')) {
// modal loaded event handler
element.on('show.bs.modal', function(e) {
// find JSON examples
element.find('pre code').each(function(index, el) {
// jQuery up the element
el = $(el);
// exit as json doesn't exist or element has already been formatted
if (el.data('json-formatted') === 'true' || el.data('json') === undefined) {
return;
}
// format the JSON
var formatter = new JSONFormatter.default(
el.data('json'),
3,
{
sortPropertiesBy: function(a, b) {
if (a === "title") { return -1; }
if (b === "title") { return 1; }
if (a === "description") { return -1; }
if (b === "description") { return 1; }
return a > b;
}
}
);
// clear the container
el.empty();
// render the formatted json
el.parent()[0].appendChild(formatter.render());
// prevent duplicate rendering if the modal is re-opened
el.data('json-formatted', 'true');
// create collapse and expand buttons
var collapseBtn = $('<button class="btn btn-default btn-sm" type="button">Collapse</button>');
var expandBtn = $('<button class="btn btn-default btn-sm" type="button">Expand</button>');
// collapse click handler
collapseBtn.on('click', function() {
formatter.openAtDepth(1);
});
// expand click handler
expandBtn.on('click', function() {
formatter.openAtDepth(Infinity);
});
// add expand and collapse buttons above the rendered JSON
el.parent().prepend(expandBtn);
el.parent().prepend(collapseBtn);
});
});
element.modal('show');
}
});
// execute hashchange on first page load
$(window).trigger('hashchange');
// remove url fragment on modal hide
$('.modal').on('hidden.bs.modal', function() {
try {
if (history && history.replaceState) {
history.replaceState({}, '', '#');
}
} catch (e) {}
});
});