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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* @memberof OCA.Versions
|
|
|
|
*/
|
2018-10-11 17:59:04 +03:00
|
|
|
var 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: {
|
2018-10-11 17:59:04 +03:00
|
|
|
'click .revertVersion': '_onClickRevertVersion'
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function() {
|
2015-09-28 13:30:12 +03:00
|
|
|
OCA.Files.DetailTabView.prototype.initialize.apply(this, arguments);
|
2015-09-01 20:29:55 +03:00
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
getLabel: function() {
|
|
|
|
return t('files_versions', 'Versions');
|
|
|
|
},
|
|
|
|
|
2018-11-01 06:10:55 +03:00
|
|
|
getIcon: function() {
|
|
|
|
return 'icon-history';
|
|
|
|
},
|
|
|
|
|
2015-09-01 20:29:55 +03:00
|
|
|
nextPage: function() {
|
2018-10-11 17:59:04 +03:00
|
|
|
if (this._loading) {
|
2015-09-01 20:29:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.collection.getFileInfo() && this.collection.getFileInfo().isDirectory()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-11 17:59:04 +03:00
|
|
|
this.collection.fetch();
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_onClickRevertVersion: function(ev) {
|
|
|
|
var self = this;
|
|
|
|
var $target = $(ev.target);
|
|
|
|
var fileInfoModel = this.collection.getFileInfo();
|
|
|
|
var revision;
|
|
|
|
if (!$target.is('li')) {
|
|
|
|
$target = $target.closest('li');
|
|
|
|
}
|
|
|
|
|
|
|
|
ev.preventDefault();
|
|
|
|
revision = $target.attr('data-revision');
|
|
|
|
|
|
|
|
var versionModel = this.collection.get(revision);
|
|
|
|
versionModel.revert({
|
|
|
|
success: function() {
|
|
|
|
// reset and re-fetch the updated collection
|
2015-10-01 13:10:50 +03:00
|
|
|
self.$versionsContainer.empty();
|
2015-09-01 20:29:55 +03:00
|
|
|
self.collection.setFileInfo(fileInfoModel);
|
2015-10-01 13:10:50 +03:00
|
|
|
self.collection.reset([], {silent: true});
|
2018-10-11 17:59:04 +03:00
|
|
|
self.collection.fetch();
|
2015-10-01 13:10:50 +03:00
|
|
|
|
|
|
|
self.$el.find('.versions').removeClass('hidden');
|
2015-09-01 20:29:55 +03:00
|
|
|
|
|
|
|
// update original model
|
|
|
|
fileInfoModel.trigger('busy', fileInfoModel, false);
|
|
|
|
fileInfoModel.set({
|
|
|
|
size: versionModel.get('size'),
|
|
|
|
mtime: versionModel.get('timestamp') * 1000,
|
|
|
|
// temp dummy, until we can do a PROPFIND
|
|
|
|
etag: versionModel.get('id') + versionModel.get('timestamp')
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
error: function() {
|
2016-06-28 13:09:58 +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(),
|
|
|
|
timestamp: OC.Util.formatDate(versionModel.get('timestamp') * 1000)
|
2017-02-14 23:26:00 +03:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
type: 'error'
|
|
|
|
}
|
2015-09-01 20:29:55 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// spinner
|
|
|
|
this._toggleLoading(true);
|
|
|
|
fileInfoModel.trigger('busy', fileInfoModel, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
_toggleLoading: function(state) {
|
|
|
|
this._loading = state;
|
|
|
|
this.$el.find('.loading').toggleClass('hidden', !state);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onRequest: function() {
|
|
|
|
this._toggleLoading(true);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onEndRequest: function() {
|
|
|
|
this._toggleLoading(false);
|
|
|
|
this.$el.find('.empty').toggleClass('hidden', !!this.collection.length);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onAddModel: function(model) {
|
2016-03-02 19:50:46 +03:00
|
|
|
var $el = $(this.itemTemplate(this._formatItem(model)));
|
|
|
|
this.$versionsContainer.append($el);
|
|
|
|
$el.find('.has-tooltip').tooltip();
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
template: function(data) {
|
2018-10-01 21:53:35 +03:00
|
|
|
return OCA.Versions.Templates['template'](data);
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
itemTemplate: function(data) {
|
2018-10-01 21:53:35 +03:00
|
|
|
return OCA.Versions.Templates['item'](data);
|
2015-09-01 20:29:55 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setFileInfo: function(fileInfo) {
|
|
|
|
if (fileInfo) {
|
|
|
|
this.render();
|
|
|
|
this.collection.setFileInfo(fileInfo);
|
2015-10-01 13:10:50 +03:00
|
|
|
this.collection.reset([], {silent: true});
|
2015-09-01 20:29:55 +03:00
|
|
|
this.nextPage();
|
|
|
|
} else {
|
|
|
|
this.render();
|
|
|
|
this.collection.reset();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_formatItem: function(version) {
|
|
|
|
var timestamp = version.get('timestamp') * 1000;
|
2016-11-02 17:17:16 +03:00
|
|
|
var size = version.has('size') ? version.get('size') : 0;
|
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),
|
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-10-11 17:59:04 +03:00
|
|
|
previewUrl: version.getPreviewUrl(),
|
2015-09-01 20:29:55 +03:00
|
|
|
revertLabel: t('files_versions', 'Restore'),
|
2016-06-28 13:09:58 +03:00
|
|
|
canRevert: (this.collection.getFileInfo().get('permissions') & OC.PERMISSION_UPDATE) !== 0
|
2015-09-01 20:29:55 +03:00
|
|
|
}, version.attributes);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders this details view
|
|
|
|
*/
|
|
|
|
render: function() {
|
|
|
|
this.$el.html(this.template({
|
2018-10-11 17:59:04 +03:00
|
|
|
emptyResultLabel: t('files_versions', 'No other versions available'),
|
2015-09-01 20:29:55 +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.
|
|
|
|
*
|
|
|
|
* @return {bool} true for files, false for folders
|
|
|
|
*/
|
|
|
|
canDisplay: function(fileInfo) {
|
|
|
|
if (!fileInfo) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !fileInfo.isDirectory();
|
2015-09-01 20:29:55 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
OCA.Versions = OCA.Versions || {};
|
|
|
|
|
|
|
|
OCA.Versions.VersionsTabView = VersionsTabView;
|
|
|
|
})();
|