Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

apps/files_versions/js/versionstabview.js (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
11
(function() {
12
	var TEMPLATE_ITEM =
13
		'<li data-revision="{{timestamp}}">' +
14
		'<img class="preview" src="{{previewUrl}}"/>' +
15
		'<a href="{{downloadUrl}}" class="downloadVersion"><img src="{{downloadIconUrl}}" />' +
16
		'<span class="versiondate has-tooltip" title="{{formattedTimestamp}}">{{relativeTimestamp}}</span>' +
17
		'</a>' +
18
		'<a href="#" class="revertVersion" title="{{revertLabel}}"><img src="{{revertIconUrl}}" /></a>' +
19
		'</li>';
20
21
	var TEMPLATE =
22
		'<ul class="versions"></ul>' +
23
		'<div class="clear-float"></div>' +
24
		'<div class="empty hidden">{{emptyResultLabel}}</div>' +
25
		'<input type="button" class="showMoreVersions hidden" value="{{moreVersionsLabel}}"' +
26
		' name="show-more-versions" id="show-more-versions" />' +
27
		'<div class="loading hidden" style="height: 50px"></div>';
28
29
	/**
30
	 * @memberof OCA.Versions
31
	 */
32
	var VersionsTabView = OCA.Files.DetailTabView.extend(
33
		/** @lends OCA.Versions.VersionsTabView.prototype */ {
34
		id: 'versionsTabView',
35
		className: 'tab versionsTabView',
36
37
		_template: null,
38
39
		$versionsContainer: null,
40
41
		events: {
42
			'click .revertVersion': '_onClickRevertVersion',
43
			'click .showMoreVersions': '_onClickShowMoreVersions'
44
		},
45
46
		initialize: function() {
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
56
		getLabel: function() {
57
			return t('files_versions', 'Versions');
58
		},
59
60
		nextPage: function() {
61
			if (this._loading || !this.collection.hasMoreResults()) {
62
				return;
63
			}
64
65
			if (this.collection.getFileInfo() && this.collection.getFileInfo().isDirectory()) {
66
				return;
67
			}
68
			this.collection.fetchNext();
69
		},
70
71
		_onClickShowMoreVersions: function(ev) {
72
			ev.preventDefault();
73
			this.nextPage();
74
		},
75
76
		_onClickRevertVersion: function(ev) {
77
			var self = this;
78
			var $target = $(ev.target);
79
			var fileInfoModel = this.collection.getFileInfo();
80
			var revision;
81
			if (!$target.is('li')) {
82
				$target = $target.closest('li');
83
			}
84
85
			ev.preventDefault();
86
			revision = $target.attr('data-revision');
87
88
			this.$el.find('.versions, .showMoreVersions').addClass('hidden');
89
90
			var versionModel = this.collection.get(revision);
91
			versionModel.revert({
92
				success: function() {
93
					// reset and re-fetch the updated collection
94
					self.$versionsContainer.empty();
95
					self.collection.setFileInfo(fileInfoModel);
96
					self.collection.reset([], {silent: true});
97
					self.collection.fetchNext();
98
99
					self.$el.find('.versions').removeClass('hidden');
100
101
					// update original model
102
					fileInfoModel.trigger('busy', fileInfoModel, false);
103
					fileInfoModel.set({
104
						size: versionModel.get('size'),
105
						mtime: versionModel.get('timestamp') * 1000,
106
						// temp dummy, until we can do a PROPFIND
107
						etag: versionModel.get('id') + versionModel.get('timestamp')
108
					});
109
				},
110
111
				error: function() {
112
					OC.Notification.showTemporary(
113
						t('files_version', 'Failed to revert {file} to revision {timestamp}.', {
114
							file: versionModel.getFullPath(),
115
							timestamp: OC.Util.formatDate(versionModel.get('timestamp') * 1000)
116
						})
117
					);
118
				}
119
			});
120
121
			// spinner
122
			this._toggleLoading(true);
123
			fileInfoModel.trigger('busy', fileInfoModel, true);
124
		},
125
126
		_toggleLoading: function(state) {
127
			this._loading = state;
128
			this.$el.find('.loading').toggleClass('hidden', !state);
129
		},
130
131
		_onRequest: function() {
132
			this._toggleLoading(true);
133
			this.$el.find('.showMoreVersions').addClass('hidden');
134
		},
135
136
		_onEndRequest: function() {
137
			this._toggleLoading(false);
138
			this.$el.find('.empty').toggleClass('hidden', !!this.collection.length);
139
			this.$el.find('.showMoreVersions').toggleClass('hidden', !this.collection.hasMoreResults());
140
		},
141
142
		_onAddModel: function(model) {
143
			var $el = $(this.itemTemplate(this._formatItem(model)));
144
			this.$versionsContainer.append($el);
145
			$el.find('.has-tooltip').tooltip();
146
		},
147
148
		template: function(data) {
149
			if (!this._template) {
150
				this._template = Handlebars.compile(TEMPLATE);
0 ignored issues
show
Handlebars does not seem to be defined.
Loading history...
151
			}
152
153
			return this._template(data);
154
		},
155
156
		itemTemplate: function(data) {
157
			if (!this._itemTemplate) {
158
				this._itemTemplate = Handlebars.compile(TEMPLATE_ITEM);
159
			}
160
161
			return this._itemTemplate(data);
162
		},
163
164
		setFileInfo: function(fileInfo) {
165
			if (fileInfo) {
166
				this.render();
167
				this.collection.setFileInfo(fileInfo);
168
				this.collection.reset([], {silent: true});
169
				this.nextPage();
170
			} else {
171
				this.render();
172
				this.collection.reset();
173
			}
174
		},
175
176
		_formatItem: function(version) {
177
			var timestamp = version.get('timestamp') * 1000;
178
			return _.extend({
179
				formattedTimestamp: OC.Util.formatDate(timestamp),
180
				relativeTimestamp: OC.Util.relativeModifiedDate(timestamp),
181
				downloadUrl: version.getDownloadUrl(),
182
				downloadIconUrl: OC.imagePath('core', 'actions/download'),
183
				revertIconUrl: OC.imagePath('core', 'actions/history'),
184
				previewUrl: version.getPreviewUrl(),
185
				revertLabel: t('files_versions', 'Restore'),
186
			}, version.attributes);
187
		},
188
189
		/**
190
		 * Renders this details view
191
		 */
192
		render: function() {
193
			this.$el.html(this.template({
194
				emptyResultLabel: t('files_versions', 'No other versions available'),
195
				moreVersionsLabel: t('files_versions', 'More versions...')
196
			}));
197
			this.$el.find('.has-tooltip').tooltip();
198
			this.$versionsContainer = this.$el.find('ul.versions');
199
			this.delegateEvents();
200
		},
201
202
		/**
203
		 * Returns true for files, false for folders.
204
		 *
205
		 * @return {bool} true for files, false for folders
206
		 */
207
		canDisplay: function(fileInfo) {
208
			if (!fileInfo) {
209
				return false;
210
			}
211
			return !fileInfo.isDirectory();
212
		}
213
	});
214
215
	OCA.Versions = OCA.Versions || {};
216
217
	OCA.Versions.VersionsTabView = VersionsTabView;
218
})();
219