versionstabview.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2015
  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. * @memberof OCA.Versions
  13. */
  14. var VersionsTabView = OCA.Files.DetailTabView.extend(/** @lends OCA.Versions.VersionsTabView.prototype */{
  15. id: 'versionsTabView',
  16. className: 'tab versionsTabView',
  17. _template: null,
  18. $versionsContainer: null,
  19. events: {
  20. 'click .revertVersion': '_onClickRevertVersion'
  21. },
  22. initialize: function() {
  23. OCA.Files.DetailTabView.prototype.initialize.apply(this, arguments);
  24. this.collection = new OCA.Versions.VersionCollection();
  25. this.collection.on('request', this._onRequest, this);
  26. this.collection.on('sync', this._onEndRequest, this);
  27. this.collection.on('update', this._onUpdate, this);
  28. this.collection.on('error', this._onError, this);
  29. this.collection.on('add', this._onAddModel, this);
  30. },
  31. getLabel: function() {
  32. return t('files_versions', 'Versions');
  33. },
  34. getIcon: function() {
  35. return 'icon-history';
  36. },
  37. nextPage: function() {
  38. if (this._loading) {
  39. return;
  40. }
  41. if (this.collection.getFileInfo() && this.collection.getFileInfo().isDirectory()) {
  42. return;
  43. }
  44. this.collection.fetch();
  45. },
  46. _onClickRevertVersion: function(ev) {
  47. var self = this;
  48. var $target = $(ev.target);
  49. var fileInfoModel = this.collection.getFileInfo();
  50. var revision;
  51. if (!$target.is('li')) {
  52. $target = $target.closest('li');
  53. }
  54. ev.preventDefault();
  55. revision = $target.attr('data-revision');
  56. var versionModel = this.collection.get(revision);
  57. versionModel.revert({
  58. success: function() {
  59. // reset and re-fetch the updated collection
  60. self.$versionsContainer.empty();
  61. self.collection.setFileInfo(fileInfoModel);
  62. self.collection.reset([], {silent: true});
  63. self.collection.fetch();
  64. self.$el.find('.versions').removeClass('hidden');
  65. // update original model
  66. fileInfoModel.trigger('busy', fileInfoModel, false);
  67. fileInfoModel.set({
  68. size: versionModel.get('size'),
  69. mtime: versionModel.get('timestamp') * 1000,
  70. // temp dummy, until we can do a PROPFIND
  71. etag: versionModel.get('id') + versionModel.get('timestamp')
  72. });
  73. },
  74. error: function() {
  75. fileInfoModel.trigger('busy', fileInfoModel, false);
  76. self.$el.find('.versions').removeClass('hidden');
  77. self._toggleLoading(false);
  78. OC.Notification.show(t('files_version', 'Failed to revert {file} to revision {timestamp}.',
  79. {
  80. file: versionModel.getFullPath(),
  81. timestamp: OC.Util.formatDate(versionModel.get('timestamp') * 1000)
  82. }),
  83. {
  84. type: 'error'
  85. }
  86. );
  87. }
  88. });
  89. // spinner
  90. this._toggleLoading(true);
  91. fileInfoModel.trigger('busy', fileInfoModel, true);
  92. },
  93. _toggleLoading: function(state) {
  94. this._loading = state;
  95. this.$el.find('.loading').toggleClass('hidden', !state);
  96. },
  97. _onRequest: function() {
  98. this._toggleLoading(true);
  99. },
  100. _onEndRequest: function() {
  101. this._toggleLoading(false);
  102. this.$el.find('.empty').toggleClass('hidden', !!this.collection.length);
  103. },
  104. _onAddModel: function(model) {
  105. var $el = $(this.itemTemplate(this._formatItem(model)));
  106. this.$versionsContainer.append($el);
  107. $el.find('.has-tooltip').tooltip();
  108. },
  109. template: function(data) {
  110. return OCA.Versions.Templates['template'](data);
  111. },
  112. itemTemplate: function(data) {
  113. return OCA.Versions.Templates['item'](data);
  114. },
  115. setFileInfo: function(fileInfo) {
  116. if (fileInfo) {
  117. this.render();
  118. this.collection.setFileInfo(fileInfo);
  119. this.collection.reset([], {silent: true});
  120. this.nextPage();
  121. } else {
  122. this.render();
  123. this.collection.reset();
  124. }
  125. },
  126. _formatItem: function(version) {
  127. var timestamp = version.get('timestamp') * 1000;
  128. var size = version.has('size') ? version.get('size') : 0;
  129. var preview = OC.MimeType.getIconUrl(version.get('mimetype'));
  130. var img = new Image();
  131. img.onload = function () {
  132. $('li[data-revision=' + version.get('timestamp') + '] .preview').attr('src', version.getPreviewUrl());
  133. };
  134. img.src = version.getPreviewUrl();
  135. return _.extend({
  136. versionId: version.get('id'),
  137. formattedTimestamp: OC.Util.formatDate(timestamp),
  138. relativeTimestamp: OC.Util.relativeModifiedDate(timestamp),
  139. millisecondsTimestamp: timestamp,
  140. humanReadableSize: OC.Util.humanFileSize(size, true),
  141. altSize: n('files', '%n byte', '%n bytes', size),
  142. hasDetails: version.has('size'),
  143. downloadUrl: version.getDownloadUrl(),
  144. downloadIconUrl: OC.imagePath('core', 'actions/download'),
  145. downloadName: version.get('name'),
  146. revertIconUrl: OC.imagePath('core', 'actions/history'),
  147. previewUrl: preview,
  148. revertLabel: t('files_versions', 'Restore'),
  149. canRevert: (this.collection.getFileInfo().get('permissions') & OC.PERMISSION_UPDATE) !== 0
  150. }, version.attributes);
  151. },
  152. /**
  153. * Renders this details view
  154. */
  155. render: function() {
  156. this.$el.html(this.template({
  157. emptyResultLabel: t('files_versions', 'No other versions available'),
  158. }));
  159. this.$el.find('.has-tooltip').tooltip();
  160. this.$versionsContainer = this.$el.find('ul.versions');
  161. this.delegateEvents();
  162. },
  163. /**
  164. * Returns true for files, false for folders.
  165. *
  166. * @return {bool} true for files, false for folders
  167. */
  168. canDisplay: function(fileInfo) {
  169. if (!fileInfo) {
  170. return false;
  171. }
  172. return !fileInfo.isDirectory();
  173. }
  174. });
  175. OCA.Versions = OCA.Versions || {};
  176. OCA.Versions.VersionsTabView = VersionsTabView;
  177. })();