diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-20 11:29:26 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-20 11:29:26 +0100 |
commit | 238cd9f7f09f11ecdb1717a5d07fc33642d29043 (patch) | |
tree | 72eac94839bda4f32cb41441b2b88532eed52603 /theme/javascript/utils | |
parent | bac25cdf297a7fa54f21977aa17d455e439cdf51 (diff) | |
download | gitbook-238cd9f7f09f11ecdb1717a5d07fc33642d29043.zip gitbook-238cd9f7f09f11ecdb1717a5d07fc33642d29043.tar.gz gitbook-238cd9f7f09f11ecdb1717a5d07fc33642d29043.tar.bz2 |
Add back theme
Diffstat (limited to 'theme/javascript/utils')
-rw-r--r-- | theme/javascript/utils/dropdown.js | 27 | ||||
-rwxr-xr-x | theme/javascript/utils/platform.js | 5 | ||||
-rwxr-xr-x | theme/javascript/utils/sharing.js | 36 | ||||
-rwxr-xr-x | theme/javascript/utils/storage.js | 31 | ||||
-rw-r--r-- | theme/javascript/utils/url.js | 33 |
5 files changed, 132 insertions, 0 deletions
diff --git a/theme/javascript/utils/dropdown.js b/theme/javascript/utils/dropdown.js new file mode 100644 index 0000000..fe4e1f4 --- /dev/null +++ b/theme/javascript/utils/dropdown.js @@ -0,0 +1,27 @@ +define([ + "jQuery" +], function($) { + + var toggleDropdown = function(e) { + var $dropdown = $(e.currentTarget).parent().find(".dropdown-menu"); + + $dropdown.toggleClass("open"); + e.stopPropagation(); + e.preventDefault(); + }; + + var closeDropdown = function(e) { + $(".dropdown-menu").removeClass("open"); + }; + + // Bind all dropdown + var init = function() { + $(document).on('click', ".toggle-dropdown", toggleDropdown); + $(document).on('click', ".dropdown-menu", function(e){ e.stopPropagation(); }); + $(document).on("click", closeDropdown); + }; + + return { + init: init + }; +}); diff --git a/theme/javascript/utils/platform.js b/theme/javascript/utils/platform.js new file mode 100755 index 0000000..ad5f3b4 --- /dev/null +++ b/theme/javascript/utils/platform.js @@ -0,0 +1,5 @@ +define([], function() { + return { + isMobile: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) + }; +});
\ No newline at end of file diff --git a/theme/javascript/utils/sharing.js b/theme/javascript/utils/sharing.js new file mode 100755 index 0000000..f27e4a4 --- /dev/null +++ b/theme/javascript/utils/sharing.js @@ -0,0 +1,36 @@ +define([ + "jQuery" +], function($) { + var types = { + "twitter": function($el) { + window.open("http://twitter.com/home?status="+encodeURIComponent($("title").text()+" "+location.href)) + }, + "facebook": function($el) { + window.open("http://www.facebook.com/sharer/sharer.php?s=100&p[url]="+encodeURIComponent(location.href)) + }, + "google-plus": function($el) { + window.open("https://plus.google.com/share?url="+encodeURIComponent(location.href)) + }, + "weibo": function($el) { + window.open("http://service.weibo.com/share/share.php?content=utf-8&url="+encodeURIComponent(location.href)+"&title="+encodeURIComponent($("title").text())) + }, + "instapaper": function($el) { + window.open("http://www.instapaper.com/text?u="+encodeURIComponent(location.href)); + } + }; + + + // Bind all sharing button + var init = function() { + $(document).on("click", "a[data-sharing],button[data-sharing]", function(e) { + if (e) e.preventDefault(); + var type = $(this).data("sharing"); + + types[type]($(this)); + }) + }; + + return { + init: init + }; +}); diff --git a/theme/javascript/utils/storage.js b/theme/javascript/utils/storage.js new file mode 100755 index 0000000..57f5878 --- /dev/null +++ b/theme/javascript/utils/storage.js @@ -0,0 +1,31 @@ +define(function(){ + var baseKey = ""; + + /* + * Simple module for storing data in the browser's local storage + */ + return { + setBaseKey: function(key) { + baseKey = key; + }, + set: function(key, value) { + key = baseKey+":"+key; + localStorage[key] = JSON.stringify(value); + }, + get: function(key, def) { + key = baseKey+":"+key; + if (localStorage[key] === undefined) return def; + try { + var v = JSON.parse(localStorage[key]); + return v == null ? def : v;; + } catch(err) { + console.error(err); + return localStorage[key] || def; + } + }, + remove: function(key) { + key = baseKey+":"+key; + localStorage.removeItem(key); + } + }; +});
\ 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..0254299 --- /dev/null +++ b/theme/javascript/utils/url.js @@ -0,0 +1,33 @@ +define([ + "URIjs/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 |