diff options
Diffstat (limited to 'theme/javascript')
-rwxr-xr-x | theme/javascript/core/navigation.js | 8 | ||||
-rw-r--r-- | theme/javascript/utils/path.js | 47 | ||||
-rw-r--r-- | theme/javascript/utils/url.js | 33 |
3 files changed, 37 insertions, 51 deletions
diff --git a/theme/javascript/core/navigation.js b/theme/javascript/core/navigation.js index 7eac392..e2bc60c 100755 --- a/theme/javascript/core/navigation.js +++ b/theme/javascript/core/navigation.js @@ -1,6 +1,6 @@ define([ "jQuery", - "utils/path", + "utils/url", "core/events", "core/state", "core/search", @@ -8,15 +8,15 @@ define([ "core/exercise", "core/quiz", "core/loading" -], function($, path, events, state, search, progress, exercises, quiz, loading) { +], function($, URL, events, state, search, progress, exercises, quiz, loading) { var prev, next; var githubCountStars, githubCountWatch; var usePushState = (typeof history.pushState !== "undefined"); var handleNavigation = function(relativeUrl, push) { - var url = path.isAbsolute(relativeUrl) ? relativeUrl : path.join(path.dirname(window.location.pathname), relativeUrl); - console.log("navigate to ", url, "baseurl="+relativeUrl); + var url = URL.join(window.location.pathname, relativeUrl); + console.log("navigate to ", url, "baseurl="+relativeUrl, "current="+window.location.pathname); if (!usePushState) { // Refresh the page to the new URL if pushState not supported diff --git a/theme/javascript/utils/path.js b/theme/javascript/utils/path.js deleted file mode 100644 index 2f4c49e..0000000 --- a/theme/javascript/utils/path.js +++ /dev/null @@ -1,47 +0,0 @@ -define([], function() { - // Joins path segments. Preserves initial "/" and resolves ".." and "." - // Does not support using ".." to go above/outside the root. - // This means that join("foo", "../../bar") will not resolve to "../bar" - function join(/* path segments */) { - // Split the inputs into a list of path commands. - var parts = []; - for (var i = 0, l = arguments.length; i < l; i++) { - parts = parts.concat(arguments[i].split("/")); - } - // Interpret the path commands to get the new resolved path. - var newParts = []; - for (i = 0, l = parts.length; i < l; i++) { - var part = parts[i]; - // Remove leading and trailing slashes - // Also remove "." segments - if (!part || part === ".") continue; - // Interpret ".." to pop the last segment - if (part === "..") newParts.pop(); - // Push new path segments. - else newParts.push(part); - } - // Preserve the initial slash if there was one. - if (parts[0] === "") newParts.unshift(""); - // Turn back into a single string path. - return newParts.join("/") || (newParts.length ? "/" : "."); - } - - // A simple function to get the dirname of a path - // Trailing slashes are ignored. Leading slash is preserved. - function dirname(path) { - return join(path, ".."); - } - - // test if a path or url is absolute - function isAbsolute(path) { - if (!path) return false; - - return (path[0] == "/" || path.indexOf("http://") == 0 || path.indexOf("https://") == 0); - } - - return { - dirname: dirname, - join: join, - isAbsolute: isAbsolute - }; -})
\ No newline at end of file diff --git a/theme/javascript/utils/url.js b/theme/javascript/utils/url.js new file mode 100644 index 0000000..dbb1570 --- /dev/null +++ b/theme/javascript/utils/url.js @@ -0,0 +1,33 @@ +define([ + "vendors/URIjs/src/URI" +], function(URI) { + // Joins path segments. Preserves initial "/" and resolves ".." and "." + // Does not support using ".." to go above/outside the root. + // This means that join("foo", "../../bar") will not resolve to "../bar" + function join(baseUrl, url) { + var theUrl = new URI(url); + if (theUrl.is("relative")) { + theUrl = theUrl.absoluteTo(baseUrl); + } + return theUrl.toString(); + } + + // A simple function to get the dirname of a path + // Trailing slashes are ignored. Leading slash is preserved. + function dirname(path) { + return join(path, ".."); + } + + // test if a path or url is absolute + function isAbsolute(path) { + if (!path) return false; + + return (path[0] == "/" || path.indexOf("http://") == 0 || path.indexOf("https://") == 0); + } + + return { + dirname: dirname, + join: join, + isAbsolute: isAbsolute + }; +})
\ No newline at end of file |