2015-09-01 20:29:55 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
import ItemTemplate from './templates/item.handlebars'
|
2019-04-05 10:23:43 +03:00
|
|
|
import Template from './templates/template.handlebars';
|
|
|
|
|
2015-09-01 20:29:55 +03:00
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* @memberof OCA.Versions
|
|
|
|
*/
|
2019-11-13 15:05:10 +03:00
|
|
|
const VersionsTabView = OCA.Files.DetailTabView.extend(/** @lends OCA.Versions.VersionsTabView.prototype */{
|
2015-09-01 20:29:55 +03:00
|
|
|
id: 'versionsTabView',
|
|
|
|
className: 'tab versionsTabView',
|
|
|
|
|
|
|
|
_template: null,
|
|
|
|
|
|
|
|
$versionsContainer: null,
|
|
|
|
|
|
|
|
events: {
|
2019-11-13 15:05:10 +03:00
|
|
|
'click .revertVersion': '_onClickRevertVersion',
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function() {
|
2019-09-25 19:19:42 +03:00
|
|
|
OCA.Files.DetailTabView.prototype.initialize.apply(this, arguments)
|
|
|
|
this.collection = new OCA.Versions.VersionCollection()
|
|
|
|
this.collection.on('request', this._onRequest, this)
|
|
|
|
this.collection.on('sync', this._onEndRequest, this)
|
|
|
|
this.collection.on('update', this._onUpdate, this)
|
|
|
|
this.collection.on('error', this._onError, this)
|
|
|
|
this.collection.on('add', this._onAddModel, this)
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getLabel: function() {
|
2019-09-25 19:19:42 +03:00
|
|
|
return t('files_versions', 'Versions')
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
2018-11-01 06:10:55 +03:00
|
|
|
getIcon: function() {
|
2019-09-25 19:19:42 +03:00
|
|
|
return 'icon-history'
|
2018-11-01 06:10:55 +03:00
|
|
|
},
|
|
|
|
|
2015-09-01 20:29:55 +03:00
|
|
|
nextPage: function() {
|
2018-10-11 17:59:04 +03:00
|
|
|
if (this._loading) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return
|
2015-09-01 20:29:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.collection.getFileInfo() && this.collection.getFileInfo().isDirectory()) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return
|
2015-09-01 20:29:55 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
this.collection.fetch()
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_onClickRevertVersion: function(ev) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const self = this
|
|
|
|
let $target = $(ev.target)
|
|
|
|
const fileInfoModel = this.collection.getFileInfo()
|
2015-09-01 20:29:55 +03:00
|
|
|
if (!$target.is('li')) {
|
2019-09-25 19:19:42 +03:00
|
|
|
$target = $target.closest('li')
|
2015-09-01 20:29:55 +03:00
|
|
|
}
|
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
ev.preventDefault()
|
2019-11-13 15:05:10 +03:00
|
|
|
const revision = $target.attr('data-revision')
|
2015-09-01 20:29:55 +03:00
|
|
|
|
2019-11-13 15:05:10 +03:00
|
|
|
const versionModel = this.collection.get(revision)
|
2015-09-01 20:29:55 +03:00
|
|
|
versionModel.revert({
|
|
|
|
success: function() {
|
|
|
|
// reset and re-fetch the updated collection
|
2019-09-25 19:19:42 +03:00
|
|
|
self.$versionsContainer.empty()
|
|
|
|
self.collection.setFileInfo(fileInfoModel)
|
|
|
|
self.collection.reset([], { silent: true })
|
|
|
|
self.collection.fetch()
|
2015-10-01 13:10:50 +03:00
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
self.$el.find('.versions').removeClass('hidden')
|
2015-09-01 20:29:55 +03:00
|
|
|
|
|
|
|
// update original model
|
2019-09-25 19:19:42 +03:00
|
|
|
fileInfoModel.trigger('busy', fileInfoModel, false)
|
2015-09-01 20:29:55 +03:00
|
|
|
fileInfoModel.set({
|
|
|
|
size: versionModel.get('size'),
|
|
|
|
mtime: versionModel.get('timestamp') * 1000,
|
|
|
|
// temp dummy, until we can do a PROPFIND
|
2019-11-13 15:05:10 +03:00
|
|
|
etag: versionModel.get('id') + versionModel.get('timestamp'),
|
2019-09-25 19:19:42 +03:00
|
|
|
})
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
error: function() {
|
2019-09-25 19:19:42 +03:00
|
|
|
fileInfoModel.trigger('busy', fileInfoModel, false)
|
|
|
|
self.$el.find('.versions').removeClass('hidden')
|
|
|
|
self._toggleLoading(false)
|
2018-10-11 17:59:04 +03:00
|
|
|
OC.Notification.show(t('files_version', 'Failed to revert {file} to revision {timestamp}.',
|
2017-02-14 23:26:00 +03:00
|
|
|
{
|
2015-09-01 20:29:55 +03:00
|
|
|
file: versionModel.getFullPath(),
|
2019-11-13 15:05:10 +03:00
|
|
|
timestamp: OC.Util.formatDate(versionModel.get('timestamp') * 1000),
|
2017-02-14 23:26:00 +03:00
|
|
|
}),
|
2019-09-25 19:19:42 +03:00
|
|
|
{
|
2019-11-13 15:05:10 +03:00
|
|
|
type: 'error',
|
2019-09-25 19:19:42 +03:00
|
|
|
}
|
|
|
|
)
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
})
|
2015-09-01 20:29:55 +03:00
|
|
|
|
|
|
|
// spinner
|
2019-09-25 19:19:42 +03:00
|
|
|
this._toggleLoading(true)
|
|
|
|
fileInfoModel.trigger('busy', fileInfoModel, true)
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_toggleLoading: function(state) {
|
2019-09-25 19:19:42 +03:00
|
|
|
this._loading = state
|
|
|
|
this.$el.find('.loading').toggleClass('hidden', !state)
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_onRequest: function() {
|
2019-09-25 19:19:42 +03:00
|
|
|
this._toggleLoading(true)
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_onEndRequest: function() {
|
2019-09-25 19:19:42 +03:00
|
|
|
this._toggleLoading(false)
|
|
|
|
this.$el.find('.empty').toggleClass('hidden', !!this.collection.length)
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_onAddModel: function(model) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const $el = $(this.itemTemplate(this._formatItem(model)))
|
2019-09-25 19:19:42 +03:00
|
|
|
this.$versionsContainer.append($el)
|
|
|
|
$el.find('.has-tooltip').tooltip()
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
template: function(data) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return Template(data)
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
itemTemplate: function(data) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return ItemTemplate(data)
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setFileInfo: function(fileInfo) {
|
|
|
|
if (fileInfo) {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.render()
|
|
|
|
this.collection.setFileInfo(fileInfo)
|
|
|
|
this.collection.reset([], { silent: true })
|
|
|
|
this.nextPage()
|
2015-09-01 20:29:55 +03:00
|
|
|
} else {
|
2019-09-25 19:19:42 +03:00
|
|
|
this.render()
|
|
|
|
this.collection.reset()
|
2015-09-01 20:29:55 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_formatItem: function(version) {
|
2019-11-13 15:05:10 +03:00
|
|
|
const timestamp = version.get('timestamp') * 1000
|
|
|
|
const size = version.has('size') ? version.get('size') : 0
|
|
|
|
const preview = OC.MimeType.getIconUrl(version.get('mimetype'))
|
|
|
|
const img = new Image()
|
2019-09-25 19:19:42 +03:00
|
|
|
img.onload = function() {
|
|
|
|
$('li[data-revision=' + version.get('id') + '] .preview').attr('src', version.getPreviewUrl())
|
|
|
|
}
|
|
|
|
img.src = version.getPreviewUrl()
|
2018-10-11 17:59:04 +03:00
|
|
|
|
2015-09-01 20:29:55 +03:00
|
|
|
return _.extend({
|
2018-10-11 17:59:04 +03:00
|
|
|
versionId: version.get('id'),
|
2015-09-01 20:29:55 +03:00
|
|
|
formattedTimestamp: OC.Util.formatDate(timestamp),
|
|
|
|
relativeTimestamp: OC.Util.relativeModifiedDate(timestamp),
|
2018-11-23 13:03:14 +03:00
|
|
|
millisecondsTimestamp: timestamp,
|
2016-11-02 17:17:16 +03:00
|
|
|
humanReadableSize: OC.Util.humanFileSize(size, true),
|
|
|
|
altSize: n('files', '%n byte', '%n bytes', size),
|
|
|
|
hasDetails: version.has('size'),
|
2015-09-01 20:29:55 +03:00
|
|
|
downloadUrl: version.getDownloadUrl(),
|
|
|
|
downloadIconUrl: OC.imagePath('core', 'actions/download'),
|
2018-10-11 17:59:04 +03:00
|
|
|
downloadName: version.get('name'),
|
2015-09-01 20:29:55 +03:00
|
|
|
revertIconUrl: OC.imagePath('core', 'actions/history'),
|
2018-11-23 13:21:47 +03:00
|
|
|
previewUrl: preview,
|
2015-09-01 20:29:55 +03:00
|
|
|
revertLabel: t('files_versions', 'Restore'),
|
2019-11-13 15:05:10 +03:00
|
|
|
canRevert: (this.collection.getFileInfo().get('permissions') & OC.PERMISSION_UPDATE) !== 0,
|
2019-09-25 19:19:42 +03:00
|
|
|
}, version.attributes)
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders this details view
|
|
|
|
*/
|
|
|
|
render: function() {
|
|
|
|
this.$el.html(this.template({
|
2019-11-13 15:05:10 +03:00
|
|
|
emptyResultLabel: t('files_versions', 'No other versions available'),
|
2019-09-25 19:19:42 +03:00
|
|
|
}))
|
|
|
|
this.$el.find('.has-tooltip').tooltip()
|
|
|
|
this.$versionsContainer = this.$el.find('ul.versions')
|
|
|
|
this.delegateEvents()
|
2015-09-25 14:23:39 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true for files, false for folders.
|
2019-09-25 19:19:42 +03:00
|
|
|
* @param {FileInfo} fileInfo fileInfo
|
|
|
|
* @returns {bool} true for files, false for folders
|
2015-09-25 14:23:39 +03:00
|
|
|
*/
|
|
|
|
canDisplay: function(fileInfo) {
|
|
|
|
if (!fileInfo) {
|
2019-09-25 19:19:42 +03:00
|
|
|
return false
|
2015-09-25 14:23:39 +03:00
|
|
|
}
|
2019-09-25 19:19:42 +03:00
|
|
|
return !fileInfo.isDirectory()
|
2019-11-13 15:05:10 +03:00
|
|
|
},
|
2019-09-25 19:19:42 +03:00
|
|
|
})
|
2015-09-01 20:29:55 +03:00
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
OCA.Versions = OCA.Versions || {}
|
2015-09-01 20:29:55 +03:00
|
|
|
|
2019-09-25 19:19:42 +03:00
|
|
|
OCA.Versions.VersionsTabView = VersionsTabView
|
|
|
|
})()
|