|
5 | 5 | * Into: ci-outgoing ci-api-responses |
6 | 6 | */ |
7 | 7 | window.onload = function() { |
8 | | - const regexUrl = new RegExp(/([a-z0-9_.-]+)\/([a-z0-9_.-]+)\.html/); |
| 8 | + // Regular expression for finding chapter and subject in the current url |
| 9 | + const regexUrl = new RegExp(/\/([a-z0-9_.-]+)\/([a-z0-9_.-]+)\.html/); |
| 10 | + |
| 11 | + // Get the current url |
9 | 12 | const currentUrl = window.location.href; |
10 | 13 |
|
11 | | - let urlMatch; |
| 14 | + // Get the document body |
| 15 | + const documentBody = document.body; |
12 | 16 |
|
13 | | - if ((urlMatch = regexUrl.exec(currentUrl)) !== null) { |
14 | | - if (urlMatch.length === 3) { |
15 | | - let parent = sanitizeClass(urlMatch[1]); |
16 | | - let current = sanitizeClass(urlMatch[2]); |
| 17 | + // Placeholder for documentation index |
| 18 | + var index = null; |
17 | 19 |
|
18 | | - if (parent === 'html' || parent.length === 0) { |
19 | | - parent = 'userguide'; |
20 | | - } |
| 20 | + if ((index = regexUrl.exec(currentUrl)) !== null) |
| 21 | + { |
| 22 | + // Sanitize the documentation chapter and subject |
| 23 | + var chapter = sanitizeClass(index[1]); |
| 24 | + var subject = sanitizeClass(index[2]); |
21 | 25 |
|
22 | | - addClass(parent); |
23 | | - addClass(current); |
| 26 | + // Documentation are generated into an html-folder for developers. |
| 27 | + // This aren't a valid chapter. We are on documentation index. |
| 28 | + if (chapter === 'html') |
| 29 | + { |
| 30 | + index = null; |
| 31 | + } |
| 32 | + // Add chapter and subject className(s) to the document body |
| 33 | + else |
| 34 | + { |
| 35 | + addClass(documentBody, chapter); |
| 36 | + addClass(documentBody, subject); |
24 | 37 | } |
25 | 38 | } |
| 39 | + |
| 40 | + // No chapter and subject found. We are on documentation index. |
| 41 | + if (index === null) |
| 42 | + { |
| 43 | + addClass(documentBody, 'documentation'); |
| 44 | + addClass(documentBody, 'index'); |
| 45 | + } |
26 | 46 | } |
27 | 47 |
|
28 | 48 | /** |
29 | 49 | * Sanitize the string - removing invalid characters |
30 | 50 | * |
31 | | - * @param {string} value Value to be sanitized |
| 51 | + * @param {string} className className to be sanitized |
32 | 52 | * |
33 | 53 | * @return {string} |
34 | 54 | */ |
35 | | -sanitizeClass = function(value) { |
36 | | - return value.replace(/_/g, '-').replace(/[^a-z0-9-]/g, ''); |
| 55 | +sanitizeClass = function(className) { |
| 56 | + return className.replace(/_/g, '-').replace(/[^a-z0-9-]/g, ''); |
37 | 57 | } |
38 | 58 |
|
39 | 59 | /** |
40 | | - * Add class to the document body |
| 60 | + * Add class to HTML DOM Element Object |
41 | 61 | * |
42 | | - * @param {string} value Value to be added |
43 | | - * @param {string} prefix Prefix to be added to all classnames |
| 62 | + * @param {object} el The HTML DOM Element Object |
| 63 | + * @param {string} className className to be added |
| 64 | + * @param {string} namePrefix namePrefix to be added to className |
44 | 65 | * |
45 | 66 | * @return {void} |
46 | 67 | */ |
47 | | -addClass = function(value, prefix) { |
48 | | - prefix = prefix || 'ci-'; |
| 68 | +addClass = function(el, className, namePrefix) { |
| 69 | + namePrefix = namePrefix || 'ci-'; |
49 | 70 |
|
50 | | - if(value.length > 0) { |
51 | | - document.body.classList.add(prefix + value); |
| 71 | + if (el.classList && className.length > 0) |
| 72 | + { |
| 73 | + el.classList.add(namePrefix + className); |
52 | 74 | } |
53 | 75 | } |
0 commit comments