|
| 1 | +/* ~~~~~~~~~~~~~~ |
| 2 | + * cloud.base.js |
| 3 | + *~~~~~~~~~~~~~~ |
| 4 | + * |
| 5 | + * Base JS utils needed by cloud js instrumentation. |
| 6 | + * Split out so they can be used by theme-independant extensions (e.g. auto_redirect) |
| 7 | + * |
| 8 | + * This initializes a "window.CSP.utils" object |
| 9 | + * |
| 10 | + * :copyright: Copyright 2017 by Assurance Technologies |
| 11 | + * :license: BSD |
| 12 | + */ |
| 13 | + |
| 14 | +// begin encapsulation |
| 15 | +(function (window, $, _) { |
| 16 | + |
| 17 | + /*========================================================================== |
| 18 | + * internal helpers |
| 19 | + *==========================================================================*/ |
| 20 | + var location = document.location, |
| 21 | + // hosturl is url to root of host, without trailing '/' |
| 22 | + // NOTE: regex allows netloc segment to be empty, to support 'file:///' urls |
| 23 | + hosturl = location.href.match(/^[a-z0-9]+:(?:\/\/)?(?:[^@/]*@)?[^/]*/)[0], |
| 24 | + docpath = location.pathname, |
| 25 | + sphinx = window.DOCUMENTATION_OPTIONS; |
| 26 | + |
| 27 | + /*========================================================================== |
| 28 | + * utils |
| 29 | + *==========================================================================*/ |
| 30 | + var utils = { |
| 31 | + |
| 32 | + /*========================================================================== |
| 33 | + * url helpers |
| 34 | + *==========================================================================*/ |
| 35 | + |
| 36 | + // helper to generate an absolute url path from a relative one. |
| 37 | + // absolute paths passed through unchanged. |
| 38 | + // paths treated as relative to <base>, |
| 39 | + // if base is omitted, uses directory of current document. |
| 40 | + abspath: function (path, base) { |
| 41 | + var parts = path.split("/"), |
| 42 | + stack = []; |
| 43 | + if (parts[0]) { |
| 44 | + // if path is relative, put base on stack |
| 45 | + stack = (base || location.pathname).split("/"); |
| 46 | + // remove blank from leading '/' |
| 47 | + if (!stack[0]) { |
| 48 | + stack.shift(); |
| 49 | + } |
| 50 | + // discard filename & blank from trailing '/' |
| 51 | + if (stack.length && !(base && stack[stack.length - 1])) { |
| 52 | + stack.pop(); |
| 53 | + } |
| 54 | + } |
| 55 | + for (var i = 0; i < parts.length; ++i) { |
| 56 | + if (parts[i] && parts[i] != '.') { |
| 57 | + if (parts[i] == '..') { |
| 58 | + stack.pop(); |
| 59 | + } else { |
| 60 | + stack.push(parts[i]); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return "/" + stack.join("/"); |
| 65 | + }, |
| 66 | + |
| 67 | + // return subpath of url, if it starts with base ("" or non-empty string) |
| 68 | + // returns undefined if url doesn't start with base. |
| 69 | + // base url search params & fragments are ignored. |
| 70 | + getSubUrl: function(url, base){ |
| 71 | + base = base.replace(/(?:\/|[#?].*)$/, ''); |
| 72 | + if(url.startsWith(base)) { |
| 73 | + var suffix = url.slice(base.length); |
| 74 | + if(suffix == '' || suffix.match(/^[/#?]/)){ return suffix; } |
| 75 | + } |
| 76 | + return; |
| 77 | + }, |
| 78 | + |
| 79 | + // helper to normalize urls for comparison |
| 80 | + // * strips current document's scheme, host, & path from local document links (just fragment will be left) |
| 81 | + // * strips current document's scheme & host from internal urls (just path + fragment will be left) |
| 82 | + // * makes all internal url paths absolute |
| 83 | + // * external urls returned unchanged. |
| 84 | + shortenUrl: function(url) { |
| 85 | + if (!url){ |
| 86 | + return ""; |
| 87 | + } else if (url.indexOf(hosturl) == 0) { |
| 88 | + // absolute path to same host |
| 89 | + url = url.substr(hosturl.length) || '/'; |
| 90 | + } else if (url[0] == '.') { |
| 91 | + // relative path |
| 92 | + url = utils.abspath(url); |
| 93 | + } else if (!url.match(/^[/#?]|[a-z0-9]+:/)) { |
| 94 | + // not abs path, or fragment, or query, or uri:// -- |
| 95 | + // another page in current dir |
| 96 | + url = utils.abspath('./' + url); |
| 97 | + } |
| 98 | + |
| 99 | + if (url.indexOf(docpath) == 0) { |
| 100 | + // strip current doc's url; only thing left will be e.g. #anchor |
| 101 | + url = url.substr(docpath.length); |
| 102 | + } |
| 103 | + if (url == "#" || url == "#top") { |
| 104 | + // normalize to empty string |
| 105 | + url = ""; |
| 106 | + } |
| 107 | + return url; |
| 108 | + }, |
| 109 | + |
| 110 | + // url w/ query params & hash stripped |
| 111 | + baseUrl: function(url){ |
| 112 | + return utils.shortenUrl(url).replace(/[#?].*$/, ''); |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + /*========================================================================== |
| 117 | + * misc es5 polyfills |
| 118 | + *==========================================================================*/ |
| 119 | + var StrProto = String.prototype; |
| 120 | + if (!StrProto.startsWith) { |
| 121 | + StrProto.startsWith = function(search, pos){ |
| 122 | + return this.substr(pos || 0, search.length) === search; |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + /*========================================================================== |
| 127 | + * jquery patches |
| 128 | + *==========================================================================*/ |
| 129 | + |
| 130 | + // custom helper to toggle visibility |
| 131 | + $.fn.toggleVis = function (state){ |
| 132 | + if(state) { this.show(); } else { this.hide(); } |
| 133 | + return this; |
| 134 | + }; |
| 135 | + |
| 136 | + /*========================================================================== |
| 137 | + * initialize namespace |
| 138 | + *==========================================================================*/ |
| 139 | + |
| 140 | + window.CST = window.CloudSphinxTheme = { |
| 141 | + // url to root of host, without trailing "/" |
| 142 | + hosturl: hosturl, |
| 143 | + // path to root of document dir, without trailing "/" or index.html |
| 144 | + rootpath: sphinx && utils.abspath(sphinx.URL_ROOT || ""), |
| 145 | + utils: utils |
| 146 | + }; |
| 147 | + |
| 148 | + /*========================================================================== |
| 149 | + * eof |
| 150 | + *==========================================================================*/ |
| 151 | + |
| 152 | +// end encapsulation |
| 153 | +// NOTE: sphinx provides underscore.js as $u |
| 154 | +}(window, jQuery, $u)); |
0 commit comments