PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/themes/cakephp/static/app.js

https://github.com/jrbasso/docs
JavaScript | 245 lines | 198 code | 35 blank | 12 comment | 16 complexity | 66052fe985357cd866a7662f21e83922 MD5 | raw file
  1. if (/cakephp\.org/.test(document.domain)) {
  2. document.domain = 'cakephp.org';
  3. }
  4. App = {};
  5. App.config = {
  6. url: 'http://search.cakephp.org/search',
  7. version: '2-2'
  8. };
  9. App.Book = (function() {
  10. function init() {
  11. // Make top nav responsive.
  12. $('#cakephp-global-navigation').menuSelect({'class': 'nav-select'});
  13. // Make side navigation go into a lightbox.
  14. $('#tablet-nav').bind('click', function (e) {
  15. e.preventDefault();
  16. // Squirt the nav and page contents into the modal.
  17. var contents = $('#sidebar-navigation').html();
  18. var localToc = $('.page-contents').html();
  19. var modal = $('#nav-modal').html(contents);
  20. modal.append(localToc);
  21. modal.append('<a href="#" class="close-reveal-modal">&#215;</a>');
  22. modal.reveal({
  23. animation: 'fade'
  24. });
  25. });
  26. // Make dropdowns work with keyboard input.
  27. var dropdown = $('.dropdown');
  28. dropdown.find('> a').bind('focus', function () {
  29. $(this).parents('.dropdown').find('ul').show();
  30. });
  31. dropdown.find('li:last-child a').bind('blur', function () {
  32. $(this).parents('.dropdown').find('ul').css('display', '');
  33. });
  34. }
  35. function compare_scores(a, b) {
  36. return b.score - a.score;
  37. }
  38. return {
  39. init : init
  40. };
  41. })();
  42. // Inline search form, and standalone search form.
  43. App.InlineSearch = (function () {
  44. var segments = location.pathname.split('/');
  45. var base = location.href.replace(location.protocol + '//' + location.host, '').split('/').slice(0, 2).join('/') + '/';
  46. var searchResults;
  47. var searchInput;
  48. var doSearch;
  49. var delay = (function(){
  50. var timer;
  51. return function(callback, ms){
  52. clearTimeout(timer);
  53. timer = setTimeout(callback, ms);
  54. };
  55. })();
  56. var positionElement = function (to, element) {
  57. var position = to.offset();
  58. var height = to.outerHeight();
  59. var width = to.outerWidth();
  60. element.css({
  61. position: 'absolute',
  62. top: position.top + height + window.scrollY + 'px',
  63. left: position.left + 'px',
  64. width: width + 'px'
  65. });
  66. };
  67. var handleKeyEvent = function (event) {
  68. if ($(this).val() === '') {
  69. searchResults.fadeOut('fast');
  70. } else {
  71. var _this = $(this);
  72. delay(function() {
  73. positionElement(_this, searchResults);
  74. searchResults.show();
  75. doSearch(_this.val());
  76. }, 200);
  77. }
  78. };
  79. // escape key
  80. var handleEscape = function (event) {
  81. if (event.keyCode == 27) {
  82. searchResults.fadeOut('fast');
  83. }
  84. };
  85. // Returns a function that is used for executing searches.
  86. // Allows re-use between inline and standalone search page.
  87. var createSearch = function (searchResult, limit) {
  88. return function (value) {
  89. executeSearch(value, searchResult, limit);
  90. };
  91. };
  92. var executeSearch = function (value, searchResults, limit, page) {
  93. var query = {lang: window.lang, q: value, version: App.config.version};
  94. if (page) {
  95. query.page = page;
  96. }
  97. var url = App.config.url + '?' + jQuery.param(query);
  98. var xhr = $.ajax({
  99. url: url,
  100. dataType: 'json',
  101. type: 'GET'
  102. });
  103. xhr.done(function (response) {
  104. var results;
  105. searchResults.empty().append('<ul></ul>');
  106. results = response.data;
  107. if (limit) {
  108. results = response.data.slice(0, limit);
  109. }
  110. var ul = searchResults.find('ul');
  111. $.each(results, function(index, item) {
  112. var li = $('<li></li>');
  113. var link = $('<a></a>');
  114. link.attr('href', base + item.url);
  115. if (item.title) {
  116. link.append('<strong>' + item.title + '</strong><br />');
  117. }
  118. var span = $('<span></span>');
  119. span.text(item.contents.join("\n"));
  120. link.append(span);
  121. li.append(link);
  122. ul.append(li);
  123. });
  124. $(document).trigger('search.complete', [response]);
  125. });
  126. var _gaq = _gaq || [];
  127. _gaq.push(['_trackEvent', 'Search', 'Search in ' + window.lang, value]);
  128. };
  129. var init = function () {
  130. searchInput = $('.masthead .search-input');
  131. searchResults = $('#inline-search-results');
  132. searchInput.keyup(handleKeyEvent);
  133. $(document).keyup(handleEscape);
  134. doSearch = createSearch(searchResults, 10);
  135. };
  136. return {
  137. init: init,
  138. delay: delay,
  139. searchUrl: App.config.url,
  140. createSearch: createSearch,
  141. executeSearch: executeSearch
  142. };
  143. })();
  144. // http://stackoverflow.com/questions/967096/using-jquery-to-test-if-an-input-has-focus
  145. jQuery.extend(jQuery.expr[':'], {
  146. focus: function(element) {
  147. return element == document.activeElement;
  148. }
  149. });
  150. (function ($) {
  151. var defaults = {
  152. 'class': ''
  153. };
  154. $.fn.menuSelect = function (options) {
  155. options = $.extend({}, defaults, options || {});
  156. // Append the li & children to target.
  157. var appendOpt = function (li, target, prefix) {
  158. li = $(li);
  159. prefix = prefix || '';
  160. if (li.find('> ul').length) {
  161. prefix = li.find('> a').text() + ' - ';
  162. li.find('li').each(function () {
  163. appendOpt(this, target, prefix);
  164. });
  165. } else {
  166. var a = li.find('a');
  167. opt = $('<option />', {
  168. text: prefix + a.text(),
  169. value: a.attr('href')
  170. });
  171. target.append(opt);
  172. }
  173. };
  174. // Convert the ul + li elements into
  175. // an optgroup + option elements.
  176. var convert = function (element) {
  177. var select = $('<select />', options);
  178. select.appendTo(element);
  179. var option = $('<option />', {
  180. text: 'Go to..',
  181. selected: 'selected'
  182. });
  183. select.append(option);
  184. $(element).find('> ul > li').each(function () {
  185. var opt, optgroup;
  186. var li = $(this);
  187. if (li.find('ul').length > 0) {
  188. optgroup = $('<optgroup />', {
  189. label: li.find('> a').text()
  190. });
  191. select.append(optgroup);
  192. li.find('> ul > li').each(function () {
  193. appendOpt(this, optgroup);
  194. });
  195. } else {
  196. appendOpt(li, select);
  197. }
  198. });
  199. var handleChange = function (event) {
  200. window.location = $(this).val();
  201. };
  202. select.bind('change', handleChange);
  203. };
  204. return this.each(function () {
  205. convert(this);
  206. });
  207. };
  208. } (jQuery));
  209. $(App.Book.init);
  210. $(App.InlineSearch.init);