search.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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);
  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.imagePath('core', 'filetypes/folder') + ')');
  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);
  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.imagePath('core', 'filetypes/' + mimeicon) + ')');
  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.renderAudioResult = function($row, result) {
  99. /*render preview icon, show path beneath filename,
  100. show size and last modified date on the right
  101. show Artist and Album */
  102. $row = this.renderFileResult($row, result);
  103. if ($row) {
  104. $row.find('td.icon').css('background-image', 'url(' + OC.imagePath('core', 'filetypes/audio') + ')');
  105. }
  106. return $row;
  107. };
  108. this.renderImageResult = function($row, result) {
  109. /*render preview icon, show path beneath filename,
  110. show size and last modified date on the right
  111. show width and height */
  112. $row = this.renderFileResult($row, result);
  113. if ($row && !self.fileAppLoaded()) {
  114. $row.find('td.icon').css('background-image', 'url(' + OC.imagePath('core', 'filetypes/image') + ')');
  115. }
  116. return $row;
  117. };
  118. this.handleFolderClick = function($row, result, event) {
  119. // open folder
  120. if (self.fileAppLoaded() && self.fileList.id === 'files') {
  121. self.fileList.changeDirectory(result.path);
  122. return false;
  123. } else {
  124. return true;
  125. }
  126. };
  127. this.handleFileClick = function($row, result, event) {
  128. if (self.fileAppLoaded() && self.fileList.id === 'files') {
  129. self.fileList.changeDirectory(OC.dirname(result.path));
  130. self.fileList.scrollTo(result.name);
  131. return false;
  132. } else {
  133. return true;
  134. }
  135. };
  136. this.updateLegacyMimetype = function (result) {
  137. // backward compatibility:
  138. if (!result.mime && result.mime_type) {
  139. result.mime = result.mime_type;
  140. }
  141. };
  142. this.setFileList = function (fileList) {
  143. this.fileList = fileList;
  144. };
  145. OC.Plugins.register('OCA.Search', this);
  146. },
  147. attach: function(search) {
  148. var self = this;
  149. search.setFilter('files', function (query) {
  150. if (self.fileAppLoaded()) {
  151. self.fileList.setFilter(query);
  152. if (query.length > 2) {
  153. //search is not started until 500msec have passed
  154. window.setTimeout(function() {
  155. $('.nofilterresults').addClass('hidden');
  156. }, 500);
  157. }
  158. }
  159. });
  160. search.setRenderer('folder', this.renderFolderResult.bind(this));
  161. search.setRenderer('file', this.renderFileResult.bind(this));
  162. search.setRenderer('audio', this.renderAudioResult.bind(this));
  163. search.setRenderer('image', this.renderImageResult.bind(this));
  164. search.setHandler('folder', this.handleFolderClick.bind(this));
  165. search.setHandler(['file', 'audio', 'image'], this.handleFileClick.bind(this));
  166. if (self.fileAppLoaded()) {
  167. // hide results when switching directory outside of search results
  168. $('#app-content').delegate('>div', 'changeDirectory', function() {
  169. search.clear();
  170. });
  171. }
  172. }
  173. };
  174. OCA.Search.Files = Files;
  175. OCA.Search.files = new Files();
  176. })();