You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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(OC, OCA, $) {
  11. "use strict";
  12. /**
  13. * Construct a new FileActions instance
  14. * @constructs Files
  15. */
  16. var Comment = function() {
  17. this.initialize();
  18. };
  19. Comment.prototype = {
  20. fileList: null,
  21. /**
  22. * Initialize the file search
  23. */
  24. initialize: function() {
  25. var self = this;
  26. this.fileAppLoaded = function() {
  27. return !!OCA.Files && !!OCA.Files.App;
  28. };
  29. function inFileList($row, result) {
  30. return false;
  31. if (! self.fileAppLoaded()) {
  32. return false;
  33. }
  34. var dir = self.fileList.getCurrentDirectory().replace(/\/+$/,'');
  35. var resultDir = OC.dirname(result.path);
  36. return dir === resultDir && self.fileList.inList(result.name);
  37. }
  38. function hideNoFilterResults() {
  39. var $nofilterresults = $('.nofilterresults');
  40. if ( ! $nofilterresults.hasClass('hidden') ) {
  41. $nofilterresults.addClass('hidden');
  42. }
  43. }
  44. /**
  45. *
  46. * @param {jQuery} $row
  47. * @param {Object} result
  48. * @param {int} result.id
  49. * @param {string} result.comment
  50. * @param {string} result.authorId
  51. * @param {string} result.authorName
  52. * @param {string} result.link
  53. * @param {string} result.fileName
  54. * @param {string} result.path
  55. * @returns {*}
  56. */
  57. this.renderCommentResult = function($row, result) {
  58. if (inFileList($row, result)) {
  59. return null;
  60. }
  61. hideNoFilterResults();
  62. /*render preview icon, show path beneath filename,
  63. show size and last modified date on the right */
  64. this.updateLegacyMimetype(result);
  65. var $pathDiv = $('<div>').addClass('path').text(result.path);
  66. var $avatar = $('<div>');
  67. $avatar.addClass('avatar')
  68. .css('display', 'inline-block')
  69. .css('vertical-align', 'middle')
  70. .css('margin', '0 5px 2px 3px');
  71. if (result.authorName) {
  72. $avatar.avatar(result.authorId, 21, undefined, false, undefined, result.authorName);
  73. } else {
  74. $avatar.avatar(result.authorId, 21);
  75. }
  76. $row.find('td.info div.name').after($pathDiv).text(result.comment).prepend($('<span>').addClass('path').css('margin-right', '5px').text(result.authorName)).prepend($avatar);
  77. $row.find('td.result a').attr('href', result.link);
  78. $row.find('td.icon')
  79. .css('background-image', 'url(' + OC.imagePath('core', 'actions/comment') + ')')
  80. .css('opacity', '.4');
  81. var dir = OC.dirname(result.path);
  82. // "result.path" does not include a leading "/", so "OC.dirname"
  83. // returns the path itself for files or folders in the root.
  84. if (dir === result.path) {
  85. dir = '/';
  86. }
  87. $row.find('td.info a').attr('href',
  88. OC.generateUrl('/apps/files/?dir={dir}&scrollto={scrollto}', {dir: dir, scrollto: result.fileName})
  89. );
  90. return $row;
  91. };
  92. this.handleCommentClick = function($row, result, event) {
  93. if (self.fileAppLoaded() && self.fileList.id === 'files') {
  94. self.fileList.changeDirectory(OC.dirname(result.path));
  95. self.fileList.scrollTo(result.name);
  96. return false;
  97. } else {
  98. return true;
  99. }
  100. };
  101. this.updateLegacyMimetype = function (result) {
  102. // backward compatibility:
  103. if (!result.mime && result.mime_type) {
  104. result.mime = result.mime_type;
  105. }
  106. };
  107. this.setFileList = function (fileList) {
  108. this.fileList = fileList;
  109. };
  110. OC.Plugins.register('OCA.Search.Core', this);
  111. },
  112. attach: function(search) {
  113. search.setRenderer('comment', this.renderCommentResult.bind(this));
  114. search.setHandler('comment', this.handleCommentClick.bind(this));
  115. }
  116. };
  117. OCA.Search.comment = new Comment();
  118. })(OC, OCA, $);