icarus 255 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. commit d1776a09295b043a603e4dfb4588adbc05971d31
  2. Author: ppoffice <ppoffice_2008@163.com>
  3. Date: Fri Apr 8 13:37:05 2016 +0800
  4. feat: add shortcut keys for insight search
  5. diff --git a/source/css/_partial/insight.styl b/source/css/_partial/insight.styl
  6. index 39c3d1d..8b38631 100644
  7. --- a/source/css/_partial/insight.styl
  8. +++ b/source/css/_partial/insight.styl
  9. @@ -88,6 +88,7 @@ $ins-full-screen
  10. position: absolute
  11. .ins-section-container
  12. + position: relative
  13. background: ins-background-grey
  14. .ins-section
  15. @@ -120,7 +121,8 @@ $ins-full-screen
  16. font-size: 12px
  17. color: ins-text-grey
  18. margin: 5px 0 0 20px
  19. - &:hover
  20. + &:hover,
  21. + &.active
  22. color: white
  23. background: ins-background-blue
  24. .ins-slug,
  25. diff --git a/source/js/insight.js b/source/js/insight.js
  26. index add29a4..5a9d6b1 100644
  27. --- a/source/js/insight.js
  28. +++ b/source/js/insight.js
  29. @@ -3,20 +3,13 @@
  30. * @author PPOffice { @link https://github.com/ppoffice }
  31. */
  32. (function ($, CONFIG) {
  33. - $main = $('.ins-search');
  34. - $container = $('.ins-section-container');
  35. + var $main = $('.ins-search');
  36. + var $input = $main.find('.ins-search-input');
  37. + var $wrapper = $main.find('.ins-section-wrapper');
  38. + var $container = $main.find('.ins-section-container');
  39. $main.parent().remove('.ins-search');
  40. $('body').append($main);
  41. - $(document).on('click focus', '.search-form-input', function () {
  42. - $main.addClass('show');
  43. - $main.find('.ins-search-input').focus();
  44. - }).on('click', '.ins-search-item', function () {
  45. - location.href=$(this).attr('data-url');
  46. - }).on('click', '.ins-close', function () {
  47. - $main.removeClass('show');
  48. - });
  49. -
  50. function section (title) {
  51. return $('<section>').addClass('ins-section')
  52. .append($('<header>').addClass('ins-section-header').text(title));
  53. @@ -33,7 +26,7 @@
  54. function sectionFactory (type, array) {
  55. var sectionTitle;
  56. var $searchItems;
  57. - if (array.length == 0) return null;
  58. + if (array.length === 0) return null;
  59. sectionTitle = CONFIG.TRANSLATION[type];
  60. switch (type) {
  61. case 'POSTS':
  62. @@ -46,7 +39,7 @@
  63. case 'CATEGORIES':
  64. case 'TAGS':
  65. $searchItems = array.map(function (item) {
  66. - return searchItem(type == 'CATEGORIES' ? 'folder' : 'tag', item.name, item.slug, null, item.permalink);
  67. + return searchItem(type === 'CATEGORIES' ? 'folder' : 'tag', item.name, item.slug, null, item.permalink);
  68. });
  69. break;
  70. default:
  71. @@ -99,7 +92,7 @@
  72. return true;
  73. return false;
  74. });
  75. - return containKeywords.length == keywordArray.length;
  76. + return containKeywords.length === keywordArray.length;
  77. }
  78. function filterFactory (keywords) {
  79. @@ -178,14 +171,70 @@
  80. }
  81. }
  82. + function scrollTo ($item) {
  83. + if ($item.length === 0) return;
  84. + var wrapperHeight = $wrapper[0].clientHeight;
  85. + var itemTop = $item.position().top - $wrapper.scrollTop();
  86. + var itemBottom = $item[0].clientHeight + $item.position().top;
  87. + if (itemBottom > wrapperHeight + $wrapper.scrollTop()) {
  88. + $wrapper.scrollTop(itemBottom - $wrapper[0].clientHeight);
  89. + }
  90. + if (itemTop < 0) {
  91. + $wrapper.scrollTop($item.position().top);
  92. + }
  93. + }
  94. +
  95. + function selectItemByDiff (value) {
  96. + var $items = $.makeArray($container.find('.ins-selectable'));
  97. + var prevPosition = -1;
  98. + $items.forEach(function (item, index) {
  99. + if ($(item).hasClass('active')) {
  100. + prevPosition = index;
  101. + return;
  102. + }
  103. + });
  104. + var nextPosition = ($items.length + prevPosition + value) % $items.length;
  105. + $($items[prevPosition]).removeClass('active');
  106. + $($items[nextPosition]).addClass('active');
  107. + scrollTo($($items[nextPosition]));
  108. + }
  109. +
  110. + function gotoLink ($item) {
  111. + if ($item && $item.length) {
  112. + location.href = $item.attr('data-url');
  113. + }
  114. + }
  115. +
  116. $.getJSON(CONFIG.CONTENT_URL, function (json) {
  117. - if (location.hash.trim() == '#ins-search') {
  118. + if (location.hash.trim() === '#ins-search') {
  119. $main.addClass('show');
  120. }
  121. - $('.ins-search-input').on('input', function () {
  122. + $input.on('input', function () {
  123. var keywords = $(this).val();
  124. searchResultToDOM(search(json, keywords));
  125. });
  126. - $('.ins-search-input').trigger('input');
  127. + $input.trigger('input');
  128. + });
  129. +
  130. +
  131. + $(document).on('click focus', '.search-form-input', function () {
  132. + $main.addClass('show');
  133. + $main.find('.ins-search-input').focus();
  134. + }).on('click', '.ins-search-item', function () {
  135. + gotoLink($(this));
  136. + }).on('click', '.ins-close', function () {
  137. + $main.removeClass('show');
  138. + }).on('keydown', function (e) {
  139. + if (!$main.hasClass('show')) return;
  140. + switch (e.keyCode) {
  141. + case 27: // ESC
  142. + $main.removeClass('show'); break;
  143. + case 38: // UP
  144. + selectItemByDiff(-1); break;
  145. + case 40: // DOWN
  146. + selectItemByDiff(1); break;
  147. + case 13: //ENTER
  148. + gotoLink($container.find('.ins-selectable.active').eq(0)); break;
  149. + }
  150. });
  151. })(jQuery, window.INSIGHT_CONFIG);
  152. \ No newline at end of file