learn.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Scrollbar Width function
  2. function getScrollBarWidth() {
  3. var inner = document.createElement('p');
  4. inner.style.width = "100%";
  5. inner.style.height = "200px";
  6. var outer = document.createElement('div');
  7. outer.style.position = "absolute";
  8. outer.style.top = "0px";
  9. outer.style.left = "0px";
  10. outer.style.visibility = "hidden";
  11. outer.style.width = "200px";
  12. outer.style.height = "150px";
  13. outer.style.overflow = "hidden";
  14. outer.appendChild(inner);
  15. document.body.appendChild(outer);
  16. var w1 = inner.offsetWidth;
  17. outer.style.overflow = 'scroll';
  18. var w2 = inner.offsetWidth;
  19. if (w1 == w2) w2 = outer.clientWidth;
  20. document.body.removeChild(outer);
  21. return (w1 - w2);
  22. };
  23. function fallbackMessage(action) {
  24. var actionMsg = '';
  25. var actionKey = (action === 'cut' ? 'X' : 'C');
  26. if (/iPhone|iPad/i.test(navigator.userAgent)) {
  27. actionMsg = 'No support :(';
  28. }
  29. else if (/Mac/i.test(navigator.userAgent)) {
  30. actionMsg = 'Press ⌘-' + actionKey + ' to ' + action;
  31. }
  32. else {
  33. actionMsg = 'Press Ctrl-' + actionKey + ' to ' + action;
  34. }
  35. return actionMsg;
  36. }
  37. // for the window resize
  38. $(window).resize(function() {
  39. });
  40. // debouncing function from John Hann
  41. // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  42. (function($, sr) {
  43. var debounce = function(func, threshold, execAsap) {
  44. var timeout;
  45. return function debounced() {
  46. var obj = this, args = arguments;
  47. function delayed() {
  48. if (!execAsap)
  49. func.apply(obj, args);
  50. timeout = null;
  51. };
  52. if (timeout)
  53. clearTimeout(timeout);
  54. else if (execAsap)
  55. func.apply(obj, args);
  56. timeout = setTimeout(delayed, threshold || 100);
  57. };
  58. }
  59. // smartresize
  60. jQuery.fn[sr] = function(fn) { return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
  61. })(jQuery, 'smartresize');
  62. jQuery(document).ready(function() {
  63. jQuery('#sidebar .category-icon').on('click', function() {
  64. $( this ).toggleClass("fa-angle-down fa-angle-right") ;
  65. $( this ).parent().parent().children('ul').toggle() ;
  66. return false;
  67. });
  68. var sidebarStatus = searchStatus = 'open';
  69. jQuery('#overlay').on('click', function() {
  70. jQuery(document.body).toggleClass('sidebar-hidden');
  71. sidebarStatus = (jQuery(document.body).hasClass('sidebar-hidden') ? 'closed' : 'open');
  72. return false;
  73. });
  74. jQuery('[data-sidebar-toggle]').on('click', function() {
  75. jQuery(document.body).toggleClass('sidebar-hidden');
  76. sidebarStatus = (jQuery(document.body).hasClass('sidebar-hidden') ? 'closed' : 'open');
  77. return false;
  78. });
  79. jQuery('[data-clear-history-toggle]').on('click', function() {
  80. sessionStorage.clear();
  81. location.reload();
  82. return false;
  83. });
  84. jQuery('[data-search-toggle]').on('click', function() {
  85. if (sidebarStatus == 'closed') {
  86. jQuery('[data-sidebar-toggle]').trigger('click');
  87. jQuery(document.body).removeClass('searchbox-hidden');
  88. searchStatus = 'open';
  89. return false;
  90. }
  91. jQuery(document.body).toggleClass('searchbox-hidden');
  92. searchStatus = (jQuery(document.body).hasClass('searchbox-hidden') ? 'closed' : 'open');
  93. return false;
  94. });
  95. var ajax;
  96. jQuery('[data-search-input]').on('input', function() {
  97. var input = jQuery(this),
  98. value = input.val(),
  99. items = jQuery('[data-nav-id]');
  100. items.removeClass('search-match');
  101. if (!value.length) {
  102. $('ul.topics').removeClass('searched');
  103. items.css('display', 'block');
  104. sessionStorage.removeItem('search-value');
  105. $(".highlightable").unhighlight({ element: 'mark' })
  106. return;
  107. }
  108. sessionStorage.setItem('search-value', value);
  109. $(".highlightable").unhighlight({ element: 'mark' }).highlight(value, { element: 'mark' });
  110. if (ajax && ajax.abort) ajax.abort();
  111. jQuery('[data-search-clear]').on('click', function() {
  112. jQuery('[data-search-input]').val('').trigger('input');
  113. sessionStorage.removeItem('search-input');
  114. $(".highlightable").unhighlight({ element: 'mark' })
  115. });
  116. });
  117. if (sessionStorage.getItem('search-value')) {
  118. jQuery(document.body).removeClass('searchbox-hidden');
  119. jQuery('[data-search-input]').val(sessionStorage.getItem('search-value'));
  120. jQuery('[data-search-input]').trigger('input');
  121. }
  122. // clipboard
  123. var clipInit = false;
  124. $('code').each(function() {
  125. var code = $(this),
  126. text = code.text();
  127. if (text.length > 5) {
  128. if (!clipInit) {
  129. var text, clip = new Clipboard('.copy-to-clipboard', {
  130. text: function(trigger) {
  131. text = $(trigger).prev('code').text();
  132. return text.replace(/^\$\s/gm, '');
  133. }
  134. });
  135. var inPre;
  136. clip.on('success', function(e) {
  137. e.clearSelection();
  138. inPre = $(e.trigger).parent().prop('tagName') == 'PRE';
  139. $(e.trigger).attr('aria-label', 'Copied to clipboard!').addClass('tooltipped tooltipped-' + (inPre ? 'w' : 's'));
  140. });
  141. clip.on('error', function(e) {
  142. inPre = $(e.trigger).parent().prop('tagName') == 'PRE';
  143. $(e.trigger).attr('aria-label', fallbackMessage(e.action)).addClass('tooltipped tooltipped-' + (inPre ? 'w' : 's'));
  144. $(document).one('copy', function(){
  145. $(e.trigger).attr('aria-label', 'Copied to clipboard!').addClass('tooltipped tooltipped-' + (inPre ? 'w' : 's'));
  146. });
  147. });
  148. clipInit = true;
  149. }
  150. code.after('<span class="copy-to-clipboard" title="Copy to clipboard" />');
  151. code.next('.copy-to-clipboard').on('mouseleave', function() {
  152. $(this).attr('aria-label', null).removeClass('tooltipped tooltipped-s tooltipped-w');
  153. });
  154. }
  155. });
  156. // allow keyboard control for prev/next links
  157. jQuery(function() {
  158. jQuery('.nav-prev').click(function(){
  159. location.href = jQuery(this).attr('href');
  160. });
  161. jQuery('.nav-next').click(function() {
  162. location.href = jQuery(this).attr('href');
  163. });
  164. });
  165. jQuery(document).keydown(function(e) {
  166. // prev links - left arrow key
  167. if(e.which == '37') {
  168. jQuery('.nav.nav-prev').click();
  169. }
  170. // next links - right arrow key
  171. if(e.which == '39') {
  172. jQuery('.nav.nav-next').click();
  173. }
  174. });
  175. $('#top-bar a:not(:has(img)):not(.btn)').addClass('highlight');
  176. $('#body-inner a:not(:has(img)):not(.btn)').addClass('highlight');
  177. $('#toc-menu').hover(function() {
  178. $('.progress').stop(true, false, true).fadeToggle(100);
  179. });
  180. $('.progress').hover(function() {
  181. $('.progress').stop(true, false, true).fadeToggle(100);
  182. });
  183. });
  184. jQuery(window).on('load', function() {
  185. function adjustForScrollbar() {
  186. if ((parseInt(jQuery('#body-inner').height()) + 83) >= jQuery('#body').height()) {
  187. jQuery('.nav.nav-next').css({ 'margin-right': getScrollBarWidth() });
  188. } else {
  189. jQuery('.nav.nav-next').css({ 'margin-right': 0 });
  190. }
  191. }
  192. // adjust sidebar for scrollbar
  193. adjustForScrollbar();
  194. jQuery(window).smartresize(function() {
  195. adjustForScrollbar();
  196. });
  197. // store this page in session
  198. sessionStorage.setItem(jQuery('body').data('url'), 1);
  199. // loop through the sessionStorage and see if something should be marked as visited
  200. for (var url in sessionStorage) {
  201. if (sessionStorage.getItem(url) == 1) jQuery('[data-nav-id="' + url + '"]').addClass('visited');
  202. }
  203. $(".highlightable").highlight(sessionStorage.getItem('search-value'), { element: 'mark' });
  204. });
  205. $(function() {
  206. $('a[rel="lightbox"]').featherlight({
  207. root: 'section#body'
  208. });
  209. });
  210. jQuery.extend({
  211. highlight: function(node, re, nodeName, className) {
  212. if (node.nodeType === 3) {
  213. var match = node.data.match(re);
  214. if (match) {
  215. var highlight = document.createElement(nodeName || 'span');
  216. highlight.className = className || 'highlight';
  217. var wordNode = node.splitText(match.index);
  218. wordNode.splitText(match[0].length);
  219. var wordClone = wordNode.cloneNode(true);
  220. highlight.appendChild(wordClone);
  221. wordNode.parentNode.replaceChild(highlight, wordNode);
  222. return 1; //skip added node in parent
  223. }
  224. } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
  225. !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
  226. !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
  227. for (var i = 0; i < node.childNodes.length; i++) {
  228. i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
  229. }
  230. }
  231. return 0;
  232. }
  233. });
  234. jQuery.fn.unhighlight = function(options) {
  235. var settings = {
  236. className: 'highlight',
  237. element: 'span'
  238. };
  239. jQuery.extend(settings, options);
  240. return this.find(settings.element + "." + settings.className).each(function() {
  241. var parent = this.parentNode;
  242. parent.replaceChild(this.firstChild, this);
  243. parent.normalize();
  244. }).end();
  245. };
  246. jQuery.fn.highlight = function(words, options) {
  247. var settings = {
  248. className: 'highlight',
  249. element: 'span',
  250. caseSensitive: false,
  251. wordsOnly: false
  252. };
  253. jQuery.extend(settings, options);
  254. if (!words) { return; }
  255. if (words.constructor === String) {
  256. words = [words];
  257. }
  258. words = jQuery.grep(words, function(word, i) {
  259. return word != '';
  260. });
  261. words = jQuery.map(words, function(word, i) {
  262. return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  263. });
  264. if (words.length == 0) { return this; }
  265. ;
  266. var flag = settings.caseSensitive ? "" : "i";
  267. var pattern = "(" + words.join("|") + ")";
  268. if (settings.wordsOnly) {
  269. pattern = "\\b" + pattern + "\\b";
  270. }
  271. var re = new RegExp(pattern, flag);
  272. return this.each(function() {
  273. jQuery.highlight(this, re, settings.element, settings.className);
  274. });
  275. };