forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcitheme.js
More file actions
75 lines (66 loc) · 1.85 KB
/
Copy pathcitheme.js
File metadata and controls
75 lines (66 loc) · 1.85 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
/*
* Add classes to the body of each page of the documentation.
*
* From: [...]/outgoing/api_responses.html
* Into: ci-outgoing ci-api-responses
*/
window.onload = function() {
// Regular expression for finding chapter and subject in the current url
const regexUrl = new RegExp(/\/([a-z0-9_.-]+)\/([a-z0-9_.-]+)\.html/);
// Get the current url
const currentUrl = window.location.href;
// Get the document body
const documentBody = document.body;
// Placeholder for documentation index
var index = null;
if ((index = regexUrl.exec(currentUrl)) !== null)
{
// Sanitize the documentation chapter and subject
var chapter = sanitizeClass(index[1]);
var subject = sanitizeClass(index[2]);
// Documentation are generated into an html-folder for developers.
// This aren't a valid chapter. We are on documentation index.
if (chapter === 'html')
{
index = null;
}
// Add chapter and subject className(s) to the document body
else
{
addClass(documentBody, chapter);
addClass(documentBody, subject);
}
}
// No chapter and subject found. We are on documentation index.
if (index === null)
{
addClass(documentBody, 'documentation');
addClass(documentBody, 'index');
}
}
/**
* Sanitize the string - removing invalid characters
*
* @param {string} className className to be sanitized
*
* @return {string}
*/
sanitizeClass = function(className) {
return className.replace(/_/g, '-').replace(/[^a-z0-9-]/g, '');
}
/**
* Add class to HTML DOM Element Object
*
* @param {object} el The HTML DOM Element Object
* @param {string} className className to be added
* @param {string} namePrefix namePrefix to be added to className
*
* @return {void}
*/
addClass = function(el, className, namePrefix) {
namePrefix = namePrefix || 'ci-';
if (el.classList && className.length > 0)
{
el.classList.add(namePrefix + className);
}
}