-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocdock.js
312 lines (268 loc) · 10.4 KB
/
docdock.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
300
301
302
303
304
305
306
307
308
309
310
311
312
// Get Parameters from some url
var getUrlParameter = function getUrlParameter(sPageURL) {
var url = sPageURL.split('?');
var obj = {};
if (url.length == 2) {
var sURLVariables = url[1].split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
obj[sParameterName[0]] = sParameterName[1];
}
return obj;
} else {
return undefined;
}
};
jQuery(document).ready(function () {
// Execute actions on images generated from Markdown pages
var images = $("article img").not(".inline");
// Wrap image inside a featherlight (to get a full size view in a popup)
images.wrap(function () {
var image = $(this);
if (!image.parent("a").length) {
return "<a href='" + image[0].src + "' data-featherlight='image'></a>";
}
});
// Change styles, depending on parameters set to the image
images.each(function (index) {
var image = $(this);
var o = getUrlParameter(image[0].src);
if (typeof o !== "undefined") {
var h = o["height"];
var w = o["width"];
var c = o["classes"];
image.css({
width: function () {
if (typeof w !== "undefined") {
return w;
}
},
height: function () {
if (typeof h !== "undefined") {
return h;
}
}
});
if (typeof c !== "undefined") {
var classes = c.split(',');
$.each(classes, function(i) {
image.addClass(classes[i]);
});
}
}
});
// Add link button for every
var text, clip = new Clipboard('.anchor');
$("h1~h2,h1~h3,h1~h4,h1~h5,h1~h6").append(function (index, html) {
var element = $(this);
var url = document.location.origin + document.location.pathname;
var link = url + "#" + element[0].id;
return " <span class='anchor' data-clipboard-text='" + link + "'>" +
"<i class='fa fa-link fa-lg'></i>" +
"</span>";
});
$(".anchor").on('mouseleave', function (e) {
$(this).attr('aria-label', null).removeClass('tooltipped tooltipped-s tooltipped-w');
});
clip.on('success', function (e) {
e.clearSelection();
$(e.trigger).attr('aria-label', 'Link copied to clipboard!').addClass('tooltipped tooltipped-s');
});
});
function fallbackMessage(action) {
var actionMsg = '';
var actionKey = (action === 'cut' ? 'X' : 'C');
if (/iPhone|iPad/i.test(navigator.userAgent)) {
actionMsg = 'No support :(';
}
else if (/Mac/i.test(navigator.userAgent)) {
actionMsg = 'Press ⌘-' + actionKey + ' to ' + action;
}
else {
actionMsg = 'Press Ctrl-' + actionKey + ' to ' + action;
}
return actionMsg;
}
jQuery(document).ready(function() {
baseurl = baseurl.endsWith('/')?baseurl:baseurl+'/';
jQuery('#sidebar .category-icon').on('click', function() {
$( this ).toggleClass("fa-angle-down fa-angle-right") ;
$( this ).parent().parent().children('ul').toggle() ;
return false;
});
jQuery('[data-clear-history-toggle]').on('click', function() {
sessionStorage.clear();
location.reload();
return false;
});
var ajax;
jQuery('[data-search-input]').on('input', function() {
var input = jQuery(this),
value = input.val(),
items = jQuery('[data-nav-id]');
items.removeClass('search-match');
if (!value.length) {
$('ul.topics').removeClass('searched');
items.css('display', 'block');
sessionStorage.removeItem('search-value');
$(".highlightable").unhighlight({ element: 'mark' })
return;
}
sessionStorage.setItem('search-value', value);
$(".highlightable").unhighlight({ element: 'mark' }).highlight(value, { element: 'mark' });
if (ajax && ajax.abort) ajax.abort();
jQuery('[data-search-clear]').on('click', function() {
jQuery('[data-search-input]').val('').trigger('input');
sessionStorage.removeItem('search-input');
$(".highlightable").unhighlight({ element: 'mark' })
});
});
$.expr[":"].contains = $.expr.createPseudo(function(arg) {
return function( elem ) {
return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
};
});
if (sessionStorage.getItem('search-value')) {
var searchValue = sessionStorage.getItem('search-value')
sessionStorage.removeItem('search-value');
var searchedElem = $('article').find(':contains(' + searchValue + ')').get(0);
searchedElem && searchedElem.scrollIntoView();
$(".highlightable").highlight(searchValue, { element: 'mark' });
}
// clipboard
var clipInit = false;
$('code').each(function() {
var code = $(this),
text = code.text();
if (text.length > 5) {
if (!clipInit) {
var text, clip = new Clipboard('.copy-to-clipboard', {
text: function(trigger) {
text = $(trigger).prev('code').text();
return text.replace(/^\$\s/gm, '');
}
});
var inPre;
clip.on('success', function(e) {
e.clearSelection();
inPre = $(e.trigger).parent().prop('tagName') == 'PRE';
$(e.trigger).attr('aria-label', 'Copied to clipboard!').addClass('tooltipped tooltipped-' + (inPre ? 'w' : 's'));
});
clip.on('error', function(e) {
inPre = $(e.trigger).parent().prop('tagName') == 'PRE';
$(e.trigger).attr('aria-label', fallbackMessage(e.action)).addClass('tooltipped tooltipped-' + (inPre ? 'w' : 's'));
$(document).one('copy', function(){
$(e.trigger).attr('aria-label', 'Copied to clipboard!').addClass('tooltipped tooltipped-' + (inPre ? 'w' : 's'));
});
});
clipInit = true;
}
code.after('<span class="copy-to-clipboard" title="Copy to clipboard"><object class="clippy-icon" type="image/svg+xml" data="' + baseurl + 'images/clippy.svg"/></span>');
code.next('.copy-to-clipboard').on('mouseleave', function() {
$(this).attr('aria-label', null).removeClass('tooltipped tooltipped-s tooltipped-w');
});
}
});
// allow keyboard control for prev/next links
jQuery(function() {
jQuery('.nav-prev').click(function(){
location.href = jQuery(this).attr('href');
});
jQuery('.nav-next').click(function() {
location.href = jQuery(this).attr('href');
});
});
jQuery(document).keydown(function(e) {
// prev links - left arrow key
if(e.which == '37') {
jQuery('.nav.nav-prev').click();
}
// next links - right arrow key
if(e.which == '39') {
jQuery('.nav.nav-next').click();
}
});
$('#top-bar a:not(:has(img)):not(.btn)').addClass('highlight');
$('article a:not(:has(img)):not(.btn)').addClass('highlight');
});
jQuery(window).on('load', function() {
// store this page in session
sessionStorage.setItem(jQuery('body').data('url'), 1);
// loop through the sessionStorage and see if something should be marked as visited
for (var url in sessionStorage) {
if (sessionStorage.getItem(url) == 1) jQuery('[data-nav-id="' + url + '"]').addClass('visited');
}
});
$(function() {
$('a[rel="lightbox"]').featherlight({
root: 'section#body'
});
});
jQuery.extend({
highlight: function(node, re, nodeName, className) {
if (node.nodeType === 3) {
var match = node.data.match(re);
if (match && !(node.parentNode.ownerSVGElement instanceof SVGElement)) {
var highlight = document.createElement(nodeName || 'span');
highlight.className = className || 'highlight';
var wordNode = node.splitText(match.index);
wordNode.splitText(match[0].length);
var wordClone = wordNode.cloneNode(true);
highlight.appendChild(wordClone);
wordNode.parentNode.replaceChild(highlight, wordNode);
return 1; //skip added node in parent
}
} else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
!/(script|style)/i.test(node.tagName) && // ignore script and style nodes
!(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
for (var i = 0; i < node.childNodes.length; i++) {
i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
}
}
return 0;
}
});
jQuery.fn.unhighlight = function(options) {
var settings = {
className: 'highlight',
element: 'span'
};
jQuery.extend(settings, options);
return this.find(settings.element + "." + settings.className).each(function() {
var parent = this.parentNode;
parent.replaceChild(this.firstChild, this);
parent.normalize();
}).end();
};
jQuery.fn.highlight = function(words, options) {
var settings = {
className: 'highlight',
element: 'span',
caseSensitive: false,
wordsOnly: false
};
jQuery.extend(settings, options);
if (!words) { return; }
if (words.constructor === String) {
words = [words];
}
words = jQuery.grep(words, function(word, i) {
return word != '';
});
words = jQuery.map(words, function(word, i) {
return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
});
if (words.length == 0) { return this; }
;
var flag = settings.caseSensitive ? "" : "i";
var pattern = "(" + words.join("|") + ")";
if (settings.wordsOnly) {
pattern = "\\b" + pattern + "\\b";
}
var re = new RegExp(pattern, flag);
return this.each(function() {
jQuery.highlight(this, re, settings.element, settings.className);
});
};