mainfileinfodetailview.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015-2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. (function() {
  7. /**
  8. * @class OCA.Files.MainFileInfoDetailView
  9. * @classdesc
  10. *
  11. * Displays main details about a file
  12. *
  13. */
  14. var MainFileInfoDetailView = OCA.Files.DetailFileInfoView.extend(
  15. /** @lends OCA.Files.MainFileInfoDetailView.prototype */ {
  16. className: 'mainFileInfoView',
  17. /**
  18. * Associated file list instance, for file actions
  19. *
  20. * @type {OCA.Files.FileList}
  21. */
  22. _fileList: null,
  23. /**
  24. * File actions
  25. *
  26. * @type {OCA.Files.FileActions}
  27. */
  28. _fileActions: null,
  29. /**
  30. * @type {OCA.Files.SidebarPreviewManager}
  31. */
  32. _previewManager: null,
  33. events: {
  34. 'click a.action-favorite': '_onClickFavorite',
  35. 'click a.action-default': '_onClickDefaultAction',
  36. 'click a.permalink': '_onClickPermalink',
  37. 'focus .permalink-field>input': '_onFocusPermalink'
  38. },
  39. template: function(data) {
  40. return OCA.Files.Templates['mainfileinfodetailsview'](data);
  41. },
  42. initialize: function(options) {
  43. options = options || {};
  44. this._fileList = options.fileList;
  45. this._fileActions = options.fileActions;
  46. if (!this._fileList) {
  47. throw 'Missing required parameter "fileList"';
  48. }
  49. if (!this._fileActions) {
  50. throw 'Missing required parameter "fileActions"';
  51. }
  52. this._previewManager = new OCA.Files.SidebarPreviewManager(this._fileList);
  53. this._setupClipboard();
  54. },
  55. _setupClipboard: function() {
  56. var clipboard = new Clipboard('.permalink');
  57. clipboard.on('success', function(e) {
  58. OC.Notification.show(t('files', 'Direct link was copied (only works for people who have access to this file/folder)'), {type: 'success'});
  59. });
  60. clipboard.on('error', function(e) {
  61. var $row = this.$('.permalink-field');
  62. $row.toggleClass('hidden');
  63. if (!$row.hasClass('hidden')) {
  64. $row.find('>input').focus();
  65. }
  66. });
  67. },
  68. _onClickPermalink: function(e) {
  69. e.preventDefault();
  70. return;
  71. },
  72. _onFocusPermalink: function() {
  73. this.$('.permalink-field>input').select();
  74. },
  75. _onClickFavorite: function(event) {
  76. event.preventDefault();
  77. this._fileActions.triggerAction('Favorite', this.model, this._fileList);
  78. },
  79. _onClickDefaultAction: function(event) {
  80. event.preventDefault();
  81. this._fileActions.triggerAction(null, this.model, this._fileList);
  82. },
  83. _onModelChanged: function() {
  84. // simply re-render
  85. this.render();
  86. },
  87. _makePermalink: function(fileId) {
  88. var baseUrl = OC.getProtocol() + '://' + OC.getHost();
  89. return baseUrl + OC.generateUrl('/f/{fileId}', {fileId: fileId});
  90. },
  91. setFileInfo: function(fileInfo) {
  92. if (this.model) {
  93. this.model.off('change', this._onModelChanged, this);
  94. }
  95. this.model = fileInfo;
  96. if (this.model) {
  97. this.model.on('change', this._onModelChanged, this);
  98. }
  99. if (this.model) {
  100. var properties = [];
  101. if( !this.model.has('size') ) {
  102. properties.push(OC.Files.Client.PROPERTY_SIZE);
  103. properties.push(OC.Files.Client.PROPERTY_GETCONTENTLENGTH);
  104. }
  105. if( properties.length > 0){
  106. this.model.reloadProperties(properties);
  107. }
  108. }
  109. this.render();
  110. },
  111. /**
  112. * Renders this details view
  113. */
  114. render: function() {
  115. this.trigger('pre-render');
  116. if (this.model) {
  117. var isFavorite = (this.model.get('tags') || []).indexOf(OC.TAG_FAVORITE) >= 0;
  118. var availableActions = this._fileActions.get(
  119. this.model.get('mimetype'),
  120. this.model.get('type'),
  121. this.model.get('permissions'),
  122. this.model.get('name')
  123. );
  124. var hasFavoriteAction = 'Favorite' in availableActions;
  125. this.$el.html(this.template({
  126. type: this.model.isImage()? 'image': '',
  127. nameLabel: t('files', 'Name'),
  128. name: this.model.get('displayName') || this.model.get('name'),
  129. pathLabel: t('files', 'Path'),
  130. path: this.model.get('path'),
  131. hasSize: this.model.has('size'),
  132. sizeLabel: t('files', 'Size'),
  133. size: OC.Util.humanFileSize(this.model.get('size'), true, false),
  134. altSize: n('files', '%n byte', '%n bytes', this.model.get('size')),
  135. dateLabel: t('files', 'Modified'),
  136. altDate: OC.Util.formatDate(this.model.get('mtime')),
  137. timestamp: this.model.get('mtime'),
  138. date: OC.Util.relativeModifiedDate(this.model.get('mtime')),
  139. hasFavoriteAction: hasFavoriteAction,
  140. starAltText: isFavorite ? t('files', 'Favorited') : t('files', 'Favorite'),
  141. starClass: isFavorite ? 'icon-starred' : 'icon-star',
  142. permalink: this._makePermalink(this.model.get('id')),
  143. permalinkTitle: t('files', 'Copy direct link (only works for people who have access to this file/folder)')
  144. }));
  145. // TODO: we really need OC.Previews
  146. var $iconDiv = this.$el.find('.thumbnail');
  147. var $container = this.$el.find('.thumbnailContainer');
  148. if (!this.model.isDirectory()) {
  149. $iconDiv.addClass('icon-loading icon-32');
  150. this._previewManager.loadPreview(this.model, $iconDiv, $container);
  151. } else {
  152. var iconUrl = this.model.get('icon') || OC.MimeType.getIconUrl('dir');
  153. if (typeof this.model.get('mountType') !== 'undefined') {
  154. iconUrl = OC.MimeType.getIconUrl('dir-' + this.model.get('mountType'))
  155. }
  156. $iconDiv.css('background-image', 'url("' + iconUrl + '")');
  157. }
  158. } else {
  159. this.$el.empty();
  160. }
  161. this.delegateEvents();
  162. this.trigger('post-render');
  163. }
  164. });
  165. OCA.Files.MainFileInfoDetailView = MainFileInfoDetailView;
  166. })();