versionstabview.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * Copyright (c) 2015
  3. *
  4. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Michael Jobst <mjobst+github@tecratech.de>
  8. * @author noveens <noveen.sachdeva@research.iiit.ac.in>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  11. *
  12. * @license AGPL-3.0-or-later
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. import ItemTemplate from './templates/item.handlebars'
  29. import Template from './templates/template.handlebars';
  30. (function() {
  31. if (!OCA.Files.DetailTabView) {
  32. // Only register the versions tab within the files app
  33. return
  34. }
  35. /**
  36. * @memberof OCA.Versions
  37. */
  38. const VersionsTabView = OCA.Files.DetailTabView.extend(/** @lends OCA.Versions.VersionsTabView.prototype */{
  39. id: 'versionsTabView',
  40. className: 'tab versionsTabView',
  41. _template: null,
  42. $versionsContainer: null,
  43. events: {
  44. 'click .revertVersion': '_onClickRevertVersion',
  45. },
  46. initialize() {
  47. OCA.Files.DetailTabView.prototype.initialize.apply(this, arguments)
  48. this.collection = new OCA.Versions.VersionCollection()
  49. this.collection.on('request', this._onRequest, this)
  50. this.collection.on('sync', this._onEndRequest, this)
  51. this.collection.on('update', this._onUpdate, this)
  52. this.collection.on('error', this._onError, this)
  53. this.collection.on('add', this._onAddModel, this)
  54. },
  55. getLabel() {
  56. return t('files_versions', 'Versions')
  57. },
  58. getIcon() {
  59. return 'icon-history'
  60. },
  61. nextPage() {
  62. if (this._loading) {
  63. return
  64. }
  65. if (this.collection.getFileInfo() && this.collection.getFileInfo().isDirectory()) {
  66. return
  67. }
  68. this.collection.fetch()
  69. },
  70. _onClickRevertVersion(ev) {
  71. const self = this
  72. let $target = $(ev.target)
  73. const fileInfoModel = this.collection.getFileInfo()
  74. if (!$target.is('li')) {
  75. $target = $target.closest('li')
  76. }
  77. ev.preventDefault()
  78. const revision = $target.attr('data-revision')
  79. const versionModel = this.collection.get(revision)
  80. versionModel.revert({
  81. success() {
  82. // reset and re-fetch the updated collection
  83. self.$versionsContainer.empty()
  84. self.collection.setFileInfo(fileInfoModel)
  85. self.collection.reset([], { silent: true })
  86. self.collection.fetch()
  87. self.$el.find('.versions').removeClass('hidden')
  88. // update original model
  89. fileInfoModel.trigger('busy', fileInfoModel, false)
  90. fileInfoModel.set({
  91. size: versionModel.get('size'),
  92. mtime: versionModel.get('timestamp') * 1000,
  93. // temp dummy, until we can do a PROPFIND
  94. etag: versionModel.get('id') + versionModel.get('timestamp'),
  95. })
  96. },
  97. error() {
  98. fileInfoModel.trigger('busy', fileInfoModel, false)
  99. self.$el.find('.versions').removeClass('hidden')
  100. self._toggleLoading(false)
  101. OC.Notification.show(t('files_version', 'Failed to revert {file} to revision {timestamp}.',
  102. {
  103. file: versionModel.getFullPath(),
  104. timestamp: OC.Util.formatDate(versionModel.get('timestamp') * 1000),
  105. }),
  106. {
  107. type: 'error',
  108. }
  109. )
  110. },
  111. })
  112. // spinner
  113. this._toggleLoading(true)
  114. fileInfoModel.trigger('busy', fileInfoModel, true)
  115. },
  116. _toggleLoading(state) {
  117. this._loading = state
  118. this.$el.find('.loading').toggleClass('hidden', !state)
  119. },
  120. _onRequest() {
  121. this._toggleLoading(true)
  122. },
  123. _onEndRequest() {
  124. this._toggleLoading(false)
  125. this.$el.find('.empty').toggleClass('hidden', !!this.collection.length)
  126. },
  127. _onAddModel(model) {
  128. const $el = $(this.itemTemplate(this._formatItem(model)))
  129. this.$versionsContainer.append($el)
  130. $el.find('.has-tooltip').tooltip()
  131. },
  132. template(data) {
  133. return Template(data)
  134. },
  135. itemTemplate(data) {
  136. return ItemTemplate(data)
  137. },
  138. setFileInfo(fileInfo) {
  139. if (fileInfo) {
  140. this.render()
  141. this.collection.setFileInfo(fileInfo)
  142. this.collection.reset([], { silent: true })
  143. this.nextPage()
  144. } else {
  145. this.render()
  146. this.collection.reset()
  147. }
  148. },
  149. _formatItem(version) {
  150. const timestamp = version.get('timestamp') * 1000
  151. const size = version.has('size') ? version.get('size') : 0
  152. const preview = OC.MimeType.getIconUrl(version.get('mimetype'))
  153. const img = new Image()
  154. img.onload = function() {
  155. $('li[data-revision=' + version.get('id') + '] .preview').attr('src', version.getPreviewUrl())
  156. }
  157. img.src = version.getPreviewUrl()
  158. return _.extend({
  159. versionId: version.get('id'),
  160. formattedTimestamp: OC.Util.formatDate(timestamp),
  161. relativeTimestamp: OC.Util.relativeModifiedDate(timestamp),
  162. millisecondsTimestamp: timestamp,
  163. humanReadableSize: OC.Util.humanFileSize(size, true),
  164. altSize: n('files', '%n byte', '%n bytes', size),
  165. hasDetails: version.has('size'),
  166. downloadUrl: version.getDownloadUrl(),
  167. downloadIconUrl: OC.imagePath('core', 'actions/download'),
  168. downloadName: version.get('name'),
  169. revertIconUrl: OC.imagePath('core', 'actions/history'),
  170. previewUrl: preview,
  171. revertLabel: t('files_versions', 'Restore'),
  172. canRevert: (this.collection.getFileInfo().get('permissions') & OC.PERMISSION_UPDATE) !== 0,
  173. }, version.attributes)
  174. },
  175. /**
  176. * Renders this details view
  177. */
  178. render() {
  179. this.$el.html(this.template({
  180. emptyResultLabel: t('files_versions', 'No other versions available'),
  181. }))
  182. this.$el.find('.has-tooltip').tooltip()
  183. this.$versionsContainer = this.$el.find('ul.versions')
  184. this.delegateEvents()
  185. },
  186. /**
  187. * Returns true for files, false for folders.
  188. *
  189. * @param {FileInfo} fileInfo fileInfo
  190. * @return {boolean} true for files, false for folders
  191. */
  192. canDisplay(fileInfo) {
  193. if (!fileInfo) {
  194. return false
  195. }
  196. return !fileInfo.isDirectory()
  197. },
  198. })
  199. OCA.Versions = OCA.Versions || {}
  200. OCA.Versions.VersionsTabView = VersionsTabView
  201. })()