forked from phpredis/phpredis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoctum.js
More file actions
316 lines (302 loc) · 13.2 KB
/
doctum.js
File metadata and controls
316 lines (302 loc) · 13.2 KB
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
313
314
315
316
var Doctum = {
treeJson: {"tree":{"l":0,"n":"","p":"","c":[{"l":1,"n":"[Global Namespace]","p":"[Global_Namespace]","c":[{"l":2,"n":"Redis","p":"Redis"},{"l":2,"n":"RedisArray","p":"RedisArray"},{"l":2,"n":"RedisCluster","p":"RedisCluster"},{"l":2,"n":"RedisClusterException","p":"RedisClusterException"},{"l":2,"n":"RedisException","p":"RedisException"},{"l":2,"n":"RedisSentinel","p":"RedisSentinel"}]}]},"treeOpenLevel":2},
/** @var boolean */
treeLoaded: false,
/** @var boolean */
listenersRegistered: false,
autoCompleteData: null,
/** @var boolean */
autoCompleteLoading: false,
/** @var boolean */
autoCompleteLoaded: false,
/** @var string|null */
rootPath: null,
/** @var string|null */
autoCompleteDataUrl: null,
/** @var HTMLElement|null */
doctumSearchAutoComplete: null,
/** @var HTMLElement|null */
doctumSearchAutoCompleteProgressBarContainer: null,
/** @var HTMLElement|null */
doctumSearchAutoCompleteProgressBar: null,
/** @var number */
doctumSearchAutoCompleteProgressBarPercent: 0,
/** @var autoComplete|null */
autoCompleteJS: null,
querySearchSecurityRegex: /([^0-9a-zA-Z:\\\\_\s])/gi,
buildTreeNode: function (treeNode, htmlNode, treeOpenLevel) {
var ulNode = document.createElement('ul');
for (var childKey in treeNode.c) {
var child = treeNode.c[childKey];
var liClass = document.createElement('li');
var hasChildren = child.hasOwnProperty('c');
var nodeSpecialName = (hasChildren ? 'namespace:' : 'class:') + child.p.replace(/\//g, '_');
liClass.setAttribute('data-name', nodeSpecialName);
// Create the node that will have the text
var divHd = document.createElement('div');
var levelCss = child.l - 1;
divHd.className = hasChildren ? 'hd' : 'hd leaf';
divHd.style.paddingLeft = (hasChildren ? (levelCss * 18) : (8 + (levelCss * 18))) + 'px';
if (hasChildren) {
if (child.l <= treeOpenLevel) {
liClass.className = 'opened';
}
var spanIcon = document.createElement('span');
spanIcon.className = 'icon icon-play';
divHd.appendChild(spanIcon);
}
var aLink = document.createElement('a');
// Edit the HTML link to work correctly based on the current depth
aLink.href = Doctum.rootPath + child.p + '.html';
aLink.innerText = child.n;
divHd.appendChild(aLink);
liClass.appendChild(divHd);
// It has children
if (hasChildren) {
var divBd = document.createElement('div');
divBd.className = 'bd';
Doctum.buildTreeNode(child, divBd, treeOpenLevel);
liClass.appendChild(divBd);
}
ulNode.appendChild(liClass);
}
htmlNode.appendChild(ulNode);
},
initListeners: function () {
if (Doctum.listenersRegistered) {
// Quick exit, already registered
return;
}
Doctum.listenersRegistered = true;
},
loadTree: function () {
if (Doctum.treeLoaded) {
// Quick exit, already registered
return;
}
Doctum.rootPath = document.body.getAttribute('data-root-path');
Doctum.buildTreeNode(Doctum.treeJson.tree, document.getElementById('api-tree'), Doctum.treeJson.treeOpenLevel);
// Toggle left-nav divs on click
$('#api-tree .hd span').on('click', function () {
$(this).parent().parent().toggleClass('opened');
});
// Expand the parent namespaces of the current page.
var expected = $('body').attr('data-name');
if (expected) {
// Open the currently selected node and its parents.
var container = $('#api-tree');
var node = $('#api-tree li[data-name="' + expected + '"]');
// Node might not be found when simulating namespaces
if (node.length > 0) {
node.addClass('active').addClass('opened');
node.parents('li').addClass('opened');
var scrollPos = node.offset().top - container.offset().top + container.scrollTop();
// Position the item nearer to the top of the screen.
scrollPos -= 200;
container.scrollTop(scrollPos);
}
}
Doctum.treeLoaded = true;
},
pagePartiallyLoaded: function (event) {
Doctum.initListeners();
Doctum.loadTree();
Doctum.loadAutoComplete();
},
pageFullyLoaded: function (event) {
// it may not have received DOMContentLoaded event
Doctum.initListeners();
Doctum.loadTree();
Doctum.loadAutoComplete();
// Fire the event in the search page too
if (typeof DoctumSearch === 'object') {
DoctumSearch.pageFullyLoaded();
}
},
loadAutoComplete: function () {
if (Doctum.autoCompleteLoaded) {
// Quick exit, already loaded
return;
}
Doctum.autoCompleteDataUrl = document.body.getAttribute('data-search-index-url');
Doctum.doctumSearchAutoComplete = document.getElementById('doctum-search-auto-complete');
Doctum.doctumSearchAutoCompleteProgressBarContainer = document.getElementById('search-progress-bar-container');
Doctum.doctumSearchAutoCompleteProgressBar = document.getElementById('search-progress-bar');
if (Doctum.doctumSearchAutoComplete !== null) {
// Wait for it to be loaded
Doctum.doctumSearchAutoComplete.addEventListener('init', function (_) {
Doctum.autoCompleteLoaded = true;
Doctum.doctumSearchAutoComplete.addEventListener('selection', function (event) {
// Go to selection page
window.location = Doctum.rootPath + event.detail.selection.value.p;
});
Doctum.doctumSearchAutoComplete.addEventListener('navigate', function (event) {
// Set selection in text box
if (typeof event.detail.selection.value === 'object') {
Doctum.doctumSearchAutoComplete.value = event.detail.selection.value.n;
}
});
Doctum.doctumSearchAutoComplete.addEventListener('results', function (event) {
Doctum.markProgressFinished();
});
});
}
// Check if the lib is loaded
if (typeof autoComplete === 'function') {
Doctum.bootAutoComplete();
}
},
markInProgress: function () {
Doctum.doctumSearchAutoCompleteProgressBarContainer.className = 'search-bar';
Doctum.doctumSearchAutoCompleteProgressBar.className = 'progress-bar indeterminate';
if (typeof DoctumSearch === 'object' && DoctumSearch.pageFullyLoaded) {
DoctumSearch.doctumSearchPageAutoCompleteProgressBarContainer.className = 'search-bar';
DoctumSearch.doctumSearchPageAutoCompleteProgressBar.className = 'progress-bar indeterminate';
}
},
markProgressFinished: function () {
Doctum.doctumSearchAutoCompleteProgressBarContainer.className = 'search-bar hidden';
Doctum.doctumSearchAutoCompleteProgressBar.className = 'progress-bar';
if (typeof DoctumSearch === 'object' && DoctumSearch.pageFullyLoaded) {
DoctumSearch.doctumSearchPageAutoCompleteProgressBarContainer.className = 'search-bar hidden';
DoctumSearch.doctumSearchPageAutoCompleteProgressBar.className = 'progress-bar';
}
},
makeProgess: function () {
Doctum.makeProgressOnProgressBar(
Doctum.doctumSearchAutoCompleteProgressBarPercent,
Doctum.doctumSearchAutoCompleteProgressBar
);
if (typeof DoctumSearch === 'object' && DoctumSearch.pageFullyLoaded) {
Doctum.makeProgressOnProgressBar(
Doctum.doctumSearchAutoCompleteProgressBarPercent,
DoctumSearch.doctumSearchPageAutoCompleteProgressBar
);
}
},
loadAutoCompleteData: function (query) {
return new Promise(function (resolve, reject) {
if (Doctum.autoCompleteData !== null) {
resolve(Doctum.autoCompleteData);
return;
}
Doctum.markInProgress();
function reqListener() {
Doctum.autoCompleteLoading = false;
Doctum.autoCompleteData = JSON.parse(this.responseText).items;
Doctum.markProgressFinished();
setTimeout(function () {
resolve(Doctum.autoCompleteData);
}, 50);// Let the UI render once before sending the results for processing. This gives time to the progress bar to hide
}
function reqError(err) {
Doctum.autoCompleteLoading = false;
Doctum.autoCompleteData = null;
console.error(err);
reject(err);
}
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.onerror = reqError;
oReq.onprogress = function (pe) {
if (pe.lengthComputable) {
Doctum.doctumSearchAutoCompleteProgressBarPercent = parseInt(pe.loaded / pe.total * 100, 10);
Doctum.makeProgess();
}
};
oReq.onloadend = function (_) {
Doctum.markProgressFinished();
};
oReq.open('get', Doctum.autoCompleteDataUrl, true);
oReq.send();
});
},
/**
* Make some progress on a progress bar
*
* @param number percentage
* @param HTMLElement progressBar
* @return void
*/
makeProgressOnProgressBar: function(percentage, progressBar) {
progressBar.className = 'progress-bar';
progressBar.style.width = percentage + '%';
progressBar.setAttribute(
'aria-valuenow', percentage
);
},
searchEngine: function (query, record) {
if (typeof query !== 'string') {
return '';
}
// replace all (mode = g) spaces and non breaking spaces (\s) by pipes
// g = global mode to mark also the second word searched
// i = case insensitive
// how this function works:
// First: search if the query has the keywords in sequence
// Second: replace the keywords by a mark and leave all the text in between non marked
if (record.match(new RegExp('(' + query.replace(/\s/g, ').*(') + ')', 'gi')) === null) {
return '';// Does not match
}
var replacedRecord = record.replace(new RegExp('(' + query.replace(/\s/g, '|') + ')', 'gi'), function (group) {
return '<mark class="auto-complete-highlight">' + group + '</mark>';
});
if (replacedRecord !== record) {
return replacedRecord;// This should not happen but just in case there was no match done
}
return '';
},
/**
* Clean the search query
*
* @param string query
* @return string
*/
cleanSearchQuery: function (query) {
// replace any chars that could lead to injecting code in our regex
// remove start or end spaces
// replace backslashes by an escaped version, use case in search: \myRootFunction
return query.replace(Doctum.querySearchSecurityRegex, '').trim().replace(/\\/g, '\\\\');
},
bootAutoComplete: function () {
Doctum.autoCompleteJS = new autoComplete(
{
selector: '#doctum-search-auto-complete',
searchEngine: function (query, record) {
return Doctum.searchEngine(query, record);
},
submit: true,
data: {
src: function (q) {
Doctum.markInProgress();
return Doctum.loadAutoCompleteData(q);
},
keys: ['n'],// Data 'Object' key to be searched
cache: false, // Is not compatible with async fetch of data
},
query: (input) => {
return Doctum.cleanSearchQuery(input);
},
trigger: (query) => {
return Doctum.cleanSearchQuery(query).length > 0;
},
resultsList: {
tag: 'ul',
class: 'auto-complete-dropdown-menu',
destination: '#auto-complete-results',
position: 'afterbegin',
maxResults: 500,
noResults: false,
},
resultItem: {
tag: 'li',
class: 'auto-complete-result',
highlight: 'auto-complete-highlight',
selected: 'auto-complete-selected'
},
}
);
}
};
document.addEventListener('DOMContentLoaded', Doctum.pagePartiallyLoaded, false);
window.addEventListener('load', Doctum.pageFullyLoaded, false);