search.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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.addClass('status');
  119. $status.html(t('core', 'Searching other places')+'<img class="spinner" alt="search in progress" src="'+OC.webroot+'/core/img/loading.gif" />');
  120. // do the actual search query
  121. $.getJSON(OC.generateUrl('core/search'), {query:query, inApps:inApps, page:page, size:size }, function(results) {
  122. lastResults = results;
  123. if (page === 1) {
  124. showResults(results);
  125. } else {
  126. addResults(results);
  127. }
  128. });
  129. }, 500);
  130. }
  131. };
  132. //TODO should be a core method, see https://github.com/owncloud/core/issues/12557
  133. function getCurrentApp() {
  134. var content = document.getElementById('content');
  135. if (content) {
  136. var classList = document.getElementById('content').className.split(/\s+/);
  137. for (var i = 0; i < classList.length; i++) {
  138. if (classList[i].indexOf('app-') === 0) {
  139. return classList[i].substr(4);
  140. }
  141. }
  142. }
  143. return false;
  144. }
  145. var $status = $searchResults.find('#status');
  146. // summaryAndStatusHeight is a constant
  147. var summaryAndStatusHeight = 118;
  148. function isStatusOffScreen() {
  149. return $searchResults.position() && ($searchResults.position().top + summaryAndStatusHeight > window.innerHeight);
  150. }
  151. function placeStatus() {
  152. if (isStatusOffScreen()) {
  153. $status.addClass('fixed');
  154. } else {
  155. $status.removeClass('fixed');
  156. }
  157. }
  158. function showResults(results) {
  159. lastResults = results;
  160. $searchResults.find('tr.result').remove();
  161. $searchResults.removeClass('hidden');
  162. addResults(results);
  163. }
  164. function addResults(results) {
  165. var $template = $searchResults.find('tr.template');
  166. jQuery.each(results, function (i, result) {
  167. var $row = $template.clone();
  168. $row.removeClass('template');
  169. $row.addClass('result');
  170. $row.data('result', result);
  171. // generic results only have four attributes
  172. $row.find('td.info div.name').text(result.name);
  173. $row.find('td.info a').attr('href', result.link);
  174. /**
  175. * Give plugins the ability to customize the search results. see result.js for examples
  176. */
  177. if (self.hasRenderer(result.type)) {
  178. $row = self.getRenderer(result.type)($row, result);
  179. } else {
  180. // for backward compatibility add text div
  181. $row.find('td.info div.name').addClass('result');
  182. $row.find('td.result div.name').after('<div class="text"></div>');
  183. $row.find('td.result div.text').text(result.name);
  184. if (OC.search.customResults && OC.search.customResults[result.type]) {
  185. OC.search.customResults[result.type]($row, result);
  186. }
  187. }
  188. if ($row) {
  189. $searchResults.find('tbody').append($row);
  190. }
  191. });
  192. var count = $searchResults.find('tr.result').length;
  193. $status.data('count', count);
  194. if (count === 0) {
  195. $status.addClass('emptycontent').removeClass('status');
  196. $status.html('');
  197. $status.append('<div class="icon-search"></div>');
  198. $status.append('<h2>' + t('core', 'No search result in other places') + '</h2>');
  199. } else {
  200. $status.removeClass('emptycontent').addClass('status');
  201. $status.text(n('core', '{count} search result in other places', '{count} search results in other places', count, {count:count}));
  202. }
  203. }
  204. function renderCurrent() {
  205. var result = $searchResults.find('tr.result')[currentResult];
  206. if (result) {
  207. var $result = $(result);
  208. var currentOffset = $('#app-content').scrollTop();
  209. $('#app-content').animate({
  210. // Scrolling to the top of the new result
  211. scrollTop: currentOffset + $result.offset().top - $result.height() * 2
  212. }, {
  213. duration: 100
  214. });
  215. $searchResults.find('tr.result.current').removeClass('current');
  216. $result.addClass('current');
  217. }
  218. }
  219. this.hideResults = function() {
  220. $searchResults.addClass('hidden');
  221. $searchResults.find('tr.result').remove();
  222. lastQuery = false;
  223. };
  224. this.clear = function() {
  225. self.hideResults();
  226. if(self.hasFilter(getCurrentApp())) {
  227. self.getFilter(getCurrentApp())('');
  228. }
  229. $searchBox.val('');
  230. $searchBox.blur();
  231. };
  232. /**
  233. * Event handler for when scrolling the list container.
  234. * This appends/renders the next page of entries when reaching the bottom.
  235. */
  236. function onScroll(e) {
  237. if ($searchResults && lastQuery !== false && lastResults.length > 0) {
  238. var resultsBottom = $searchResults.offset().top + $searchResults.height();
  239. var containerBottom = $searchResults.offsetParent().offset().top + $searchResults.offsetParent().height();
  240. if ( resultsBottom < containerBottom * 1.2 ) {
  241. self.search(lastQuery, lastInApps, lastPage + 1);
  242. }
  243. placeStatus();
  244. }
  245. }
  246. $('#app-content').on('scroll', _.bind(onScroll, this));
  247. /**
  248. * scrolls the search results to the top
  249. */
  250. function scrollToResults() {
  251. setTimeout(function() {
  252. if (isStatusOffScreen()) {
  253. var newScrollTop = $('#app-content').prop('scrollHeight') - $searchResults.height();
  254. console.log('scrolling to ' + newScrollTop);
  255. $('#app-content').animate({
  256. scrollTop: newScrollTop
  257. }, {
  258. duration: 100,
  259. complete: function () {
  260. scrollToResults();
  261. }
  262. });
  263. }
  264. }, 150);
  265. }
  266. $('form.searchbox').submit(function(event) {
  267. event.preventDefault();
  268. });
  269. $searchBox.on('search', function (event) {
  270. if($searchBox.val() === '') {
  271. if(self.hasFilter(getCurrentApp())) {
  272. self.getFilter(getCurrentApp())('');
  273. }
  274. self.hideResults();
  275. }
  276. });
  277. $searchBox.keyup(function(event) {
  278. if (event.keyCode === 13) { //enter
  279. if(currentResult > -1) {
  280. var result = $searchResults.find('tr.result a')[currentResult];
  281. window.location = $(result).attr('href');
  282. }
  283. } else if(event.keyCode === 38) { //up
  284. if(currentResult > 0) {
  285. currentResult--;
  286. renderCurrent();
  287. }
  288. } else if(event.keyCode === 40) { //down
  289. if(lastResults.length > currentResult + 1){
  290. currentResult++;
  291. renderCurrent();
  292. }
  293. } else {
  294. var query = $searchBox.val();
  295. if (lastQuery !== query) {
  296. currentResult = -1;
  297. if (query.length > 2) {
  298. self.search(query);
  299. } else {
  300. self.hideResults();
  301. }
  302. if(self.hasFilter(getCurrentApp())) {
  303. self.getFilter(getCurrentApp())(query);
  304. }
  305. }
  306. }
  307. });
  308. $(document).keyup(function(event) {
  309. if(event.keyCode === 27) { //esc
  310. $searchBox.val('');
  311. if(self.hasFilter(getCurrentApp())) {
  312. self.getFilter(getCurrentApp())('');
  313. }
  314. self.hideResults();
  315. }
  316. });
  317. $searchResults.on('click', 'tr.result', function (event) {
  318. var $row = $(this);
  319. var item = $row.data('result');
  320. if(self.hasHandler(item.type)){
  321. var result = self.getHandler(item.type)($row, item, event);
  322. $searchBox.val('');
  323. if(self.hasFilter(getCurrentApp())) {
  324. self.getFilter(getCurrentApp())('');
  325. }
  326. self.hideResults();
  327. return result;
  328. }
  329. });
  330. $searchResults.on('click', '#status', function (event) {
  331. event.preventDefault();
  332. scrollToResults();
  333. return false;
  334. });
  335. placeStatus();
  336. OC.Plugins.attach('OCA.Search', this);
  337. }
  338. };
  339. OCA.Search = Search;
  340. })();
  341. $(document).ready(function() {
  342. var $searchResults = $('#searchresults');
  343. if ($searchResults.length) {
  344. $searchResults.addClass('hidden');
  345. $('#app-content')
  346. .find('.viewcontainer').css('min-height', 'initial');
  347. } else {
  348. $searchResults = $('<div id="searchresults" class="hidden"/>');
  349. $('#app-content')
  350. .append($searchResults)
  351. .find('.viewcontainer').css('min-height', 'initial');
  352. }
  353. $searchResults.load(OC.webroot + '/core/search/templates/part.results.html', function () {
  354. OC.Search = new OCA.Search($('#searchbox'), $('#searchresults'));
  355. });
  356. });
  357. /**
  358. * @deprecated use get/setRenderer() instead
  359. */
  360. OC.search.customResults = {};
  361. /**
  362. * @deprecated use get/setRenderer() instead
  363. */
  364. OC.search.resultTypes = {};