|
1 | 1 | /**
|
2 | 2 | * Rundiz\Profiler JS.
|
3 |
| - * |
4 |
| - * @author Vee W. |
5 | 3 | */
|
6 | 4 |
|
7 | 5 |
|
8 |
| -/** |
9 |
| - * Load CSS in the `rundizProfilerCss` variable into HTML head section by create `<style>` element and set content into it. |
10 |
| - * |
11 |
| - * @returns {undefined} |
12 |
| - */ |
13 |
| -function rundizProfilerLoadCSS() { |
14 |
| - if (typeof(rundizProfilerCss) !== 'undefined') { |
15 |
| - var sheet = document.createElement("style"); |
16 |
| - sheet.setAttribute("type", "text/css"); |
17 |
| - sheet.innerHTML = rundizProfilerCss; |
18 |
| - document.getElementsByTagName("head")[0].appendChild(sheet); |
19 |
| - } |
20 |
| -}// rundizProfilerLoadCSS |
| 6 | +class RundizProfiler { |
| 7 | + |
| 8 | + /** |
| 9 | + * Load CSS in the `rundizProfilerCss` variable into HTML head section by create `<style>` element and set content into it. |
| 10 | + * |
| 11 | + * @returns {undefined} |
| 12 | + */ |
| 13 | + loadCss() { |
| 14 | + // rundizProfilerCss variable is defined in Rundiz/Profiler/views/functions.php. |
| 15 | + if (typeof(rundizProfilerCss) !== 'undefined') { |
| 16 | + var sheet = document.createElement("style"); |
| 17 | + sheet.setAttribute("type", "text/css"); |
| 18 | + sheet.innerHTML = rundizProfilerCss; |
| 19 | + document.getElementsByTagName("head")[0].appendChild(sheet); |
| 20 | + } |
| 21 | + }// loadCss |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Scroll to each class in `matchKey` argument. |
| 26 | + * |
| 27 | + * To use, use in a link with return. Example: `<a href="#" onclick="return RundizProfiler.scrollTo('#section', '.matchKey', jQuery(this));">link</a>` |
| 28 | + * |
| 29 | + * @param {string} section The section id. |
| 30 | + * @param {string} matchKey The matchKey class. |
| 31 | + * @param {object} thisobj jQuery(this) object. |
| 32 | + * @returns {Boolean} Always return false to prevent any click link action. |
| 33 | + */ |
| 34 | + static scrollTo(section, matchKey, thisobj) { |
| 35 | + let $ = jQuery.noConflict(); |
| 36 | + let $Container = $(section); |
| 37 | + let $CurrentElement = thisobj.parents('li'); |
| 38 | + let $ScrollTo; |
| 39 | + |
| 40 | + if ($CurrentElement.prevAll(matchKey).length) { |
| 41 | + // if previous matchKey exists, use that one. |
| 42 | + $ScrollTo = $CurrentElement.prevAll(matchKey).offset().top; |
| 43 | + //console.log('use previous matchKey.'); |
| 44 | + //console.log(($ScrollTo - $Container.offset().top + $Container.scrollTop())); |
| 45 | + } else if ($CurrentElement.nextAll(matchKey).length) { |
| 46 | + // if next matchKey exists, use that one. |
| 47 | + $ScrollTo = $CurrentElement.nextAll(matchKey).offset().top; |
| 48 | + //console.log('use next matchKey.'); |
| 49 | + //console.log(($ScrollTo - $Container.offset().top + $Container.scrollTop())); |
| 50 | + } |
| 51 | + |
| 52 | + if (typeof($ScrollTo) !== 'undefined') { |
| 53 | + $Container.scroll(); |
| 54 | + $Container.animate({ |
| 55 | + scrollTop: ($ScrollTo - $Container.offset().top + $Container.scrollTop()) |
| 56 | + }, 'fast'); |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + }// scrollTo |
21 | 61 |
|
22 | 62 |
|
| 63 | + /** |
| 64 | + * Set class to body and element height value to global vairable. |
| 65 | + * |
| 66 | + * @returns {undefined} |
| 67 | + */ |
| 68 | + setClassAndValue() { |
| 69 | + if (document.body) { |
| 70 | + document.body.classList.add('body-contain-rdprofiler'); |
| 71 | + } |
| 72 | + |
| 73 | + if (document.querySelector('.rdprofiler')) { |
| 74 | + rundizProfilerElementHeight = document.querySelector('.rdprofiler').offsetHeight; |
| 75 | + } |
| 76 | + }// setClassAndValue |
| 77 | + |
| 78 | + |
| 79 | +}// RundizProfiler |
| 80 | + |
| 81 | + |
| 82 | +// Run on page loaded --------------------------------------------------------------------- |
23 | 83 | /**
|
24 |
| - * Scroll to each class in `matchKey` argument. |
25 |
| - * |
26 |
| - * To use, use in a link with return. Example: `<a href="#" onclick="return rundizProfilerScrollTo('#section', '.matchKey', jQuery(this));">link</a>` |
27 |
| - * |
28 |
| - * @param {string} section The section id. |
29 |
| - * @param {string} matchKey The matchKey class. |
30 |
| - * @param {object} thisobj jQuery(this) object. |
31 |
| - * @returns {Boolean} Always return false to prevent any click link action. |
| 84 | + * @type Integer rundizProfilerElementHeight The rundiz profiler element height. |
32 | 85 | */
|
33 |
| -function rundizProfilerScrollTo(section, matchKey, thisobj) { |
34 |
| - $ = jQuery.noConflict(); |
35 |
| - $Container = $(section); |
36 |
| - $CurrentElement = thisobj.parents('li'); |
37 |
| - |
38 |
| - if ($CurrentElement.prevAll(matchKey).length) { |
39 |
| - // if previous matchKey exists, use that one. |
40 |
| - $ScrollTo = $CurrentElement.prevAll(matchKey).offset().top; |
41 |
| - //console.log('use previous matchKey.'); |
42 |
| - //console.log(($ScrollTo - $Container.offset().top + $Container.scrollTop())); |
43 |
| - } else if ($CurrentElement.nextAll(matchKey).length) { |
44 |
| - // if next matchKey exists, use that one. |
45 |
| - $ScrollTo = $CurrentElement.nextAll(matchKey).offset().top; |
46 |
| - //console.log('use next matchKey.'); |
47 |
| - //console.log(($ScrollTo - $Container.offset().top + $Container.scrollTop())); |
48 |
| - } |
49 |
| - |
50 |
| - if (typeof($ScrollTo) !== 'undefined') { |
51 |
| - $Container.scroll(); |
52 |
| - $Container.animate({ |
53 |
| - scrollTop: ($ScrollTo - $Container.offset().top + $Container.scrollTop()) |
54 |
| - }, 'fast'); |
55 |
| - } |
56 |
| - |
57 |
| - delete $Container; |
58 |
| - delete $CurrentElement; |
59 |
| - delete $ScrollTo; |
60 |
| - |
61 |
| - return false; |
62 |
| -}// rundizProfilerScrollTo |
| 86 | +var rundizProfilerElementHeight; |
63 | 87 |
|
64 | 88 |
|
65 |
| -// Run on page loaded --------------------------------------------------------------------- |
66 |
| -jQuery.noConflict(); |
67 | 89 | jQuery(document).ready(function($) {
|
68 |
| - rundizProfilerLoadCSS(); |
| 90 | + let rundizProfiler = new RundizProfiler(); |
| 91 | + |
| 92 | + // load CSS into HTML > head |
| 93 | + rundizProfiler.loadCss(); |
| 94 | + // set class to body and element height value to var. |
| 95 | + rundizProfiler.setClassAndValue(); |
69 | 96 |
|
70 | 97 | // set active class to show details panel on click, unset active on click again.
|
71 | 98 | $('.rdprofiler-see-details').on('click', 'a.see-details', function() {
|
|
0 commit comments