1
0

sidebarpreviewtext.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2016
  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. var SidebarPreview = function () {
  12. };
  13. SidebarPreview.prototype = {
  14. attach: function (manager) {
  15. manager.addPreviewHandler('text', this.handlePreview.bind(this));
  16. },
  17. handlePreview: function (model, $thumbnailDiv, $thumbnailContainer, fallback) {
  18. var previewWidth = $thumbnailContainer.parent().width() + 50; // 50px for negative margins
  19. var previewHeight = previewWidth / (16 / 9);
  20. this.getFileContent(model.getFullPath()).then(function (content) {
  21. $thumbnailDiv.removeClass('icon-loading icon-32');
  22. $thumbnailContainer.addClass('large');
  23. $thumbnailContainer.addClass('text');
  24. var $textPreview = $('<pre></pre>').text(content);
  25. $thumbnailDiv.children('.stretcher').remove();
  26. $thumbnailDiv.append($textPreview);
  27. $thumbnailContainer.css("max-height", previewHeight);
  28. }, function () {
  29. fallback();
  30. });
  31. },
  32. getFileContent: function (path) {
  33. return $.ajax({
  34. url: OC.linkToRemoteBase('files' + path),
  35. headers: {
  36. 'Range': 'bytes=0-10240'
  37. }
  38. });
  39. }
  40. };
  41. OC.Plugins.register('OCA.Files.SidebarPreviewManager', new SidebarPreview());
  42. })();