search.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /**
  2. * ownCloud - core
  3. *
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later. See the COPYING file.
  6. *
  7. * @author Jörn Friedrich Dreyer <jfd@owncloud.com>
  8. * @copyright Jörn Friedrich Dreyer 2014
  9. */
  10. (function () {
  11. /**
  12. * @class OCA.Search
  13. * @classdesc
  14. *
  15. * The Search class manages a search queries and their results
  16. *
  17. * @param $searchBox container element with existing markup for the #searchbox form
  18. * @param $searchResults container element for results und status message
  19. */
  20. var Search = function($searchBox, $searchResults) {
  21. this.initialize($searchBox, $searchResults);
  22. };
  23. /**
  24. * @memberof OC
  25. */
  26. Search.prototype = {
  27. /**
  28. * Initialize the search box
  29. *
  30. * @param $searchBox container element with existing markup for the #searchbox form
  31. * @param $searchResults container element for results und status message
  32. * @private
  33. */
  34. initialize: function($searchBox, $searchResults) {
  35. var self = this;
  36. /**
  37. * contains closures that are called to filter the current content
  38. */
  39. var filters = {};
  40. this.setFilter = function(type, filter) {
  41. filters[type] = filter;
  42. };
  43. this.hasFilter = function(type) {
  44. return typeof filters[type] !== 'undefined';
  45. };
  46. this.getFilter = function(type) {
  47. return filters[type];
  48. };
  49. /**
  50. * contains closures that are called to render search results
  51. */
  52. var renderers = {};
  53. this.setRenderer = function(type, renderer) {
  54. renderers[type] = renderer;
  55. };
  56. this.hasRenderer = function(type) {
  57. return typeof renderers[type] !== 'undefined';
  58. };
  59. this.getRenderer = function(type) {
  60. return renderers[type];
  61. };
  62. /**
  63. * contains closures that are called when a search result has been clicked
  64. */
  65. var handlers = {};
  66. this.setHandler = function(type, handler) {
  67. handlers[type] = handler;
  68. };
  69. this.hasHandler = function(type) {
  70. return typeof handlers[type] !== 'undefined';
  71. };
  72. this.getHandler = function(type) {
  73. return handlers[type];
  74. };
  75. var currentResult = -1;
  76. var lastQuery = '';
  77. var lastInApps = [];
  78. var lastPage = 0;
  79. var lastSize = 30;
  80. var lastResults = [];
  81. var timeoutID = null;
  82. this.getLastQuery = function() {
  83. return lastQuery;
  84. };
  85. /**
  86. * Do a search query and display the results
  87. * @param {string} query the search query
  88. */
  89. this.search = function(query, inApps, page, size) {
  90. if (query) {
  91. OC.addStyle('core/search','results');
  92. if (typeof page !== 'number') {
  93. page = 1;
  94. }
  95. if (typeof size !== 'number') {
  96. size = 30;
  97. }
  98. if (typeof inApps !== 'object') {
  99. var currentApp = getCurrentApp();
  100. if(currentApp) {
  101. inApps = [currentApp];
  102. } else {
  103. inApps = [];
  104. }
  105. }
  106. // prevent double pages
  107. if ($searchResults && query === lastQuery && page === lastPage && size === lastSize) {
  108. return;
  109. }
  110. window.clearTimeout(timeoutID);
  111. timeoutID = window.setTimeout(function() {
  112. lastQuery = query;
  113. lastInApps = inApps;
  114. lastPage = page;
  115. lastSize = size;
  116. //show spinner
  117. $searchResults.removeClass('hidden');
  118. $status.html(t('core', 'Searching other places')+'<img class="spinner" alt="search in progress" src="'+OC.webroot+'/core/img/loading.gif" />');
  119. // do the actual search query
  120. $.getJSON(OC.generateUrl('core/search'), {query:query, inApps:inApps, page:page, size:size }, function(results) {
  121. lastResults = results;
  122. if (page === 1) {
  123. showResults(results);
  124. } else {
  125. addResults(results);
  126. }
  127. });
  128. }, 500);
  129. }
  130. };
  131. //TODO should be a core method, see https://github.com/owncloud/core/issues/12557
  132. function getCurrentApp() {
  133. var content = document.getElementById('content');
  134. if (content) {
  135. var classList = document.getElementById('content').className.split(/\s+/);
  136. for (var i = 0; i < classList.length; i++) {
  137. if (classList[i].indexOf('app-') === 0) {
  138. return classList[i].substr(4);
  139. }
  140. }
  141. }
  142. return false;
  143. }
  144. var $status = $searchResults.find('#status');
  145. // summaryAndStatusHeight is a constant
  146. var summaryAndStatusHeight = 118;
  147. function isStatusOffScreen() {
  148. return $searchResults.position() && ($searchResults.position().top + summaryAndStatusHeight > window.innerHeight);
  149. }
  150. function placeStatus() {
  151. if (isStatusOffScreen()) {
  152. $status.addClass('fixed');
  153. } else {
  154. $status.removeClass('fixed');
  155. }
  156. }
  157. function showResults(results) {
  158. lastResults = results;
  159. $searchResults.find('tr.result').remove();
  160. $searchResults.removeClass('hidden');
  161. addResults(results);
  162. }
  163. function addResults(results) {
  164. var $template = $searchResults.find('tr.template');
  165. jQuery.each(results, function (i, result) {
  166. var $row = $template.clone();
  167. $row.removeClass('template');
  168. $row.addClass('result');
  169. $row.data('result', result);
  170. // generic results only have four attributes
  171. $row.find('td.info div.name').text(result.name);
  172. $row.find('td.info a').attr('href', result.link);
  173. /**
  174. * Give plugins the ability to customize the search results. see result.js for examples
  175. */
  176. if (self.hasRenderer(result.type)) {
  177. $row = self.getRenderer(result.type)($row, result);
  178. } else {
  179. // for backward compatibility add text div
  180. $row.find('td.info div.name').addClass('result');
  181. $row.find('td.result div.name').after('<div class="text"></div>');
  182. $row.find('td.result div.text').text(result.name);
  183. if (OC.search.customResults && OC.search.customResults[result.type]) {
  184. OC.search.customResults[result.type]($row, result);
  185. }
  186. }
  187. if ($row) {
  188. $searchResults.find('tbody').append($row);
  189. }
  190. });
  191. var count = $searchResults.find('tr.result').length;
  192. $status.data('count', count);
  193. if (count === 0) {
  194. $status.text(t('core', 'No search result in other places'));
  195. } else {
  196. $status.text(n('core', '{count} search result in other places', '{count} search results in other places', count, {count:count}));
  197. }
  198. }
  199. function renderCurrent() {
  200. var result = $searchResults.find('tr.result')[currentResult];
  201. if (result) {
  202. var $result = $(result);
  203. var currentOffset = $('#app-content').scrollTop();
  204. $('#app-content').animate({
  205. // Scrolling to the top of the new result
  206. scrollTop: currentOffset + $result.offset().top - $result.height() * 2
  207. }, {
  208. duration: 100
  209. });
  210. $searchResults.find('tr.result.current').removeClass('current');
  211. $result.addClass('current');
  212. }
  213. }
  214. this.hideResults = function() {
  215. $searchResults.addClass('hidden');
  216. $searchResults.find('tr.result').remove();
  217. lastQuery = false;
  218. };
  219. this.clear = function() {
  220. self.hideResults();
  221. if(self.hasFilter(getCurrentApp())) {
  222. self.getFilter(getCurrentApp())('');
  223. }
  224. $searchBox.val('');
  225. $searchBox.blur();
  226. };
  227. /**
  228. * Event handler for when scrolling the list container.
  229. * This appends/renders the next page of entries when reaching the bottom.
  230. */
  231. function onScroll(e) {
  232. if ($searchResults && lastQuery !== false && lastResults.length > 0) {
  233. var resultsBottom = $searchResults.offset().top + $searchResults.height();
  234. var containerBottom = $searchResults.offsetParent().offset().top + $searchResults.offsetParent().height();
  235. if ( resultsBottom < containerBottom * 1.2 ) {
  236. self.search(lastQuery, lastInApps, lastPage + 1);
  237. }
  238. placeStatus();
  239. }
  240. }
  241. $('#app-content').on('scroll', _.bind(onScroll, this));
  242. /**
  243. * scrolls the search results to the top
  244. */
  245. function scrollToResults() {
  246. setTimeout(function() {
  247. if (isStatusOffScreen()) {
  248. var newScrollTop = $('#app-content').prop('scrollHeight') - $searchResults.height();
  249. console.log('scrolling to ' + newScrollTop);
  250. $('#app-content').animate({
  251. scrollTop: newScrollTop
  252. }, {
  253. duration: 100,
  254. complete: function () {
  255. scrollToResults();
  256. }
  257. });
  258. }
  259. }, 150);
  260. }
  261. $('form.searchbox').submit(function(event) {
  262. event.preventDefault();
  263. });
  264. $searchBox.on('search', function (event) {
  265. if($searchBox.val() === '') {
  266. if(self.hasFilter(getCurrentApp())) {
  267. self.getFilter(getCurrentApp())('');
  268. }
  269. self.hideResults();
  270. }
  271. });
  272. $searchBox.keyup(function(event) {
  273. if (event.keyCode === 13) { //enter
  274. if(currentResult > -1) {
  275. var result = $searchResults.find('tr.result a')[currentResult];
  276. window.location = $(result).attr('href');
  277. }
  278. } else if(event.keyCode === 38) { //up
  279. if(currentResult > 0) {
  280. currentResult--;
  281. renderCurrent();
  282. }
  283. } else if(event.keyCode === 40) { //down
  284. if(lastResults.length > currentResult + 1){
  285. currentResult++;
  286. renderCurrent();
  287. }
  288. } else {
  289. var query = $searchBox.val();
  290. if (lastQuery !== query) {
  291. currentResult = -1;
  292. if (query.length > 2) {
  293. self.search(query);
  294. } else {
  295. self.hideResults();
  296. }
  297. if(self.hasFilter(getCurrentApp())) {
  298. self.getFilter(getCurrentApp())(query);
  299. }
  300. }
  301. }
  302. });
  303. $(document).keyup(function(event) {
  304. if(event.keyCode === 27) { //esc
  305. $searchBox.val('');
  306. if(self.hasFilter(getCurrentApp())) {
  307. self.getFilter(getCurrentApp())('');
  308. }
  309. self.hideResults();
  310. }
  311. });
  312. $searchResults.on('click', 'tr.result', function (event) {
  313. var $row = $(this);
  314. var item = $row.data('result');
  315. if(self.hasHandler(item.type)){
  316. var result = self.getHandler(item.type)($row, result, event);
  317. $searchBox.val('');
  318. if(self.hasFilter(getCurrentApp())) {
  319. self.getFilter(getCurrentApp())('');
  320. }
  321. self.hideResults();
  322. return result;
  323. }
  324. });
  325. $searchResults.on('click', '#status', function (event) {
  326. event.preventDefault();
  327. scrollToResults();
  328. return false;
  329. });
  330. placeStatus();
  331. OC.Plugins.attach('OCA.Search', this);
  332. }
  333. };
  334. OCA.Search = Search;
  335. })();
  336. $(document).ready(function() {
  337. var $searchResults = $('<div id="searchresults" class="hidden"/>');
  338. $('#app-content')
  339. .append($searchResults)
  340. .find('.viewcontainer').css('min-height', 'initial');
  341. $searchResults.load(OC.webroot + '/core/search/templates/part.results.html', function () {
  342. OC.Search = new OCA.Search($('#searchbox'), $('#searchresults'));
  343. });
  344. });
  345. /**
  346. * @deprecated use get/setRenderer() instead
  347. */
  348. OC.search.customResults = {};
  349. /**
  350. * @deprecated use get/setRenderer() instead
  351. */
  352. OC.search.resultTypes = {};