search.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. (function() {
  11. /**
  12. * Construct a new FileActions instance
  13. * @constructs Files
  14. */
  15. var Files = function() {
  16. this.initialize();
  17. };
  18. /**
  19. * @memberof OCA.Search
  20. */
  21. Files.prototype = {
  22. fileList: null,
  23. /**
  24. * Initialize the file search
  25. */
  26. initialize: function() {
  27. var self = this;
  28. this.fileAppLoaded = function() {
  29. return !!OCA.Files && !!OCA.Files.App;
  30. };
  31. function inFileList($row, result) {
  32. if (! self.fileAppLoaded()) {
  33. return false;
  34. }
  35. var dir = self.fileList.getCurrentDirectory().replace(/\/+$/,'');
  36. var resultDir = OC.dirname(result.path);
  37. return dir === resultDir && self.fileList.inList(result.name);
  38. }
  39. function updateLegacyMimetype(result) {
  40. // backward compatibility:
  41. if (!result.mime && result.mime_type) {
  42. result.mime = result.mime_type;
  43. }
  44. }
  45. function hideNoFilterResults() {
  46. var $nofilterresults = $('.nofilterresults');
  47. if ( ! $nofilterresults.hasClass('hidden') ) {
  48. $nofilterresults.addClass('hidden');
  49. }
  50. }
  51. this.renderFolderResult = function($row, result) {
  52. if (inFileList($row, result)) {
  53. return null;
  54. }
  55. hideNoFilterResults();
  56. /*render folder icon, show path beneath filename,
  57. show size and last modified date on the right */
  58. this.updateLegacyMimetype(result);
  59. var $pathDiv = $('<div class="path"></div>').text(result.path.substr(1, result.path.lastIndexOf("/")));
  60. $row.find('td.info div.name').after($pathDiv).text(result.name);
  61. $row.find('td.result a').attr('href', result.link);
  62. $row.find('td.icon').css('background-image', 'url(' + OC.MimeType.getIconUrl(result.mime) + ')');
  63. return $row;
  64. };
  65. this.renderFileResult = function($row, result) {
  66. if (inFileList($row, result)) {
  67. return null;
  68. }
  69. hideNoFilterResults();
  70. /*render preview icon, show path beneath filename,
  71. show size and last modified date on the right */
  72. this.updateLegacyMimetype(result);
  73. var $pathDiv = $('<div class="path"></div>').text(result.path.substr(1, result.path.lastIndexOf("/")));
  74. $row.find('td.info div.name').after($pathDiv).text(result.name);
  75. $row.find('td.result a').attr('href', result.link);
  76. if (self.fileAppLoaded()) {
  77. self.fileList.lazyLoadPreview({
  78. path: result.path,
  79. mime: result.mime,
  80. callback: function (url) {
  81. $row.find('td.icon').css('background-image', 'url(' + url + ')');
  82. }
  83. });
  84. } else {
  85. // FIXME how to get mime icon if not in files app
  86. var mimeicon = result.mime.replace('/', '-');
  87. $row.find('td.icon').css('background-image', 'url(' + OC.MimeType.getIconUrl(result.mime) + ')');
  88. var dir = OC.dirname(result.path);
  89. if (dir === '') {
  90. dir = '/';
  91. }
  92. $row.find('td.info a').attr('href',
  93. OC.generateUrl('/apps/files/?dir={dir}&scrollto={scrollto}', {dir: dir, scrollto: result.name})
  94. );
  95. }
  96. return $row;
  97. };
  98. this.handleFolderClick = function($row, result, event) {
  99. // open folder
  100. if (self.fileAppLoaded() && self.fileList.id === 'files') {
  101. self.fileList.changeDirectory(result.path);
  102. return false;
  103. } else {
  104. return true;
  105. }
  106. };
  107. this.handleFileClick = function($row, result, event) {
  108. if (self.fileAppLoaded() && self.fileList.id === 'files') {
  109. self.fileList.changeDirectory(OC.dirname(result.path));
  110. self.fileList.scrollTo(result.name);
  111. return false;
  112. } else {
  113. return true;
  114. }
  115. };
  116. this.updateLegacyMimetype = function (result) {
  117. // backward compatibility:
  118. if (!result.mime && result.mime_type) {
  119. result.mime = result.mime_type;
  120. }
  121. };
  122. this.setFileList = function (fileList) {
  123. this.fileList = fileList;
  124. };
  125. OC.Plugins.register('OCA.Search.Core', this);
  126. },
  127. attach: function(search) {
  128. var self = this;
  129. search.setFilter('files', function (query) {
  130. if (self.fileAppLoaded()) {
  131. self.fileList.setFilter(query);
  132. if (query.length > 2) {
  133. //search is not started until 500msec have passed
  134. window.setTimeout(function() {
  135. $('.nofilterresults').addClass('hidden');
  136. }, 500);
  137. }
  138. }
  139. });
  140. search.setRenderer('folder', this.renderFolderResult.bind(this));
  141. search.setRenderer('file', this.renderFileResult.bind(this));
  142. search.setRenderer('image', this.renderFileResult.bind(this));
  143. search.setRenderer('audio', this.renderFileResult.bind(this));
  144. search.setHandler('folder', this.handleFolderClick.bind(this));
  145. search.setHandler(['file', 'audio', 'image'], this.handleFileClick.bind(this));
  146. if (self.fileAppLoaded()) {
  147. // hide results when switching directory outside of search results
  148. $('#app-content').delegate('>div', 'changeDirectory', function() {
  149. search.clear();
  150. });
  151. }
  152. }
  153. };
  154. OCA.Search.Files = Files;
  155. OCA.Search.files = new Files();
  156. })();