result.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2014
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. //translations for result type ids, can be extended by apps
  11. OC.search.resultTypes={
  12. file: t('core','File'),
  13. folder: t('core','Folder'),
  14. image: t('core','Image'),
  15. audio: t('core','Audio')
  16. };
  17. OC.search.catagorizeResults=function(results){
  18. var types={};
  19. for(var i=0;i<results.length;i++){
  20. var type=results[i].type;
  21. if(!types[type]){
  22. types[type]=[];
  23. }
  24. types[type].push(results[i]);
  25. }
  26. return types;
  27. };
  28. OC.search.hide=function(){
  29. $('#searchresults').hide();
  30. if($('#searchbox').val().length>2){
  31. $('#searchbox').val('');
  32. if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
  33. FileList.unfilter();
  34. }
  35. };
  36. if ($('#searchbox').val().length === 0) {
  37. if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
  38. FileList.unfilter();
  39. }
  40. }
  41. };
  42. OC.search.showResults=function(results){
  43. if(results.length === 0){
  44. return;
  45. }
  46. if(!OC.search.showResults.loaded){
  47. var parent=$('<div/>');
  48. $('body').append(parent);
  49. parent.load(OC.filePath('search','templates','part.results.php'),function(){
  50. OC.search.showResults.loaded=true;
  51. $('#searchresults').click(function(event){
  52. OC.search.hide();
  53. event.stopPropagation();
  54. });
  55. $(document).click(function(event){
  56. OC.search.hide();
  57. if (FileList && typeof FileList.unfilter === 'function') { //TODO add hook system
  58. FileList.unfilter();
  59. }
  60. });
  61. OC.search.lastResults=results;
  62. OC.search.showResults(results);
  63. });
  64. }else{
  65. var types=OC.search.catagorizeResults(results);
  66. $('#searchresults').show();
  67. $('#searchresults tr.result').remove();
  68. var index=0;
  69. for(var typeid in types){
  70. var type=types[typeid];
  71. if(type.length>0){
  72. for(var i=0;i<type.length;i++){
  73. var row=$('#searchresults tr.template').clone();
  74. row.removeClass('template');
  75. row.addClass('result');
  76. row.data('type', typeid);
  77. row.data('name', type[i].name);
  78. row.data('text', type[i].text);
  79. row.data('index',index);
  80. if (i === 0){
  81. var typeName = OC.search.resultTypes[typeid];
  82. row.children('td.type').text(t('lib', typeName));
  83. }
  84. row.find('td.result div.name').text(type[i].name);
  85. row.find('td.result div.text').text(type[i].text);
  86. if (type[i].path) {
  87. var parent = OC.dirname(type[i].path);
  88. if (parent === '') {
  89. parent = '/';
  90. }
  91. var containerName = OC.basename(parent);
  92. if (containerName === '') {
  93. containerName = '/';
  94. }
  95. var containerLink = OC.linkTo('files', 'index.php')
  96. +'/?dir='+encodeURIComponent(parent)
  97. +'&scrollto='+encodeURIComponent(type[i].name);
  98. row.find('td.result a')
  99. .attr('href', containerLink)
  100. .attr('title', t('core', 'Show in {folder}', {folder: containerName}));
  101. } else {
  102. row.find('td.result a').attr('href', type[i].link);
  103. }
  104. index++;
  105. /**
  106. * Give plugins the ability to customize the search results. For example:
  107. * OC.search.customResults.file = function (row, item){
  108. * if(item.name.search('.json') >= 0) ...
  109. * };
  110. */
  111. if(OC.search.customResults[typeid]){
  112. OC.search.customResults[typeid](row, type[i]);
  113. }
  114. $('#searchresults tbody').append(row);
  115. }
  116. }
  117. }
  118. $('#searchresults').on('click', 'result', function () {
  119. if ($(this).data('type') === 'Files') {
  120. //FIXME use ajax to navigate to folder & highlight file
  121. }
  122. });
  123. }
  124. };
  125. OC.search.showResults.loaded=false;
  126. OC.search.renderCurrent=function(){
  127. if($('#searchresults tr.result')[OC.search.currentResult]){
  128. var result=$('#searchresults tr.result')[OC.search.currentResult];
  129. $('#searchresults tr.result').removeClass('current');
  130. $(result).addClass('current');
  131. }
  132. };