Blame view

sources/apps/files_versions/js/versions.js 5.3 KB
6d9380f96   Cédric Dupont   Update sources OC...
1
2
3
4
5
6
7
8
9
10
11
  /*
   * Copyright (c) 2014
   *
   * This file is licensed under the Affero General Public License version 3
   * or later.
   *
   * See the COPYING-README file.
   *
   */
  
  /* global scanFiles, escapeHTML, formatDate */
03e52840d   Kload   Init
12
13
14
15
16
17
18
19
  $(document).ready(function(){
  
  	if ($('#isPublic').val()){
  		// no versions actions in public mode
  		// beware of https://github.com/owncloud/core/issues/4545
  		// as enabling this might hang Chrome
  		return;
  	}
6d9380f96   Cédric Dupont   Update sources OC...
20
  	if (OCA.Files) {
03e52840d   Kload   Init
21
  		// Add versions button to 'files/index.php'
6d9380f96   Cédric Dupont   Update sources OC...
22
23
24
25
26
  		OCA.Files.fileActions.register(
  			'file',
  			'Versions',
  			OC.PERMISSION_UPDATE,
  			function() {
03e52840d   Kload   Init
27
28
  				// Specify icon for hitory button
  				return OC.imagePath('core','actions/history');
6d9380f96   Cédric Dupont   Update sources OC...
29
  			}, function(filename, context){
03e52840d   Kload   Init
30
31
  				// Action to perform when clicked
  				if (scanFiles.scanning){return;}//workaround to prevent additional http request block scanning feedback
6d9380f96   Cédric Dupont   Update sources OC...
32
  				var file = context.dir.replace(/(?!<=\/)$|\/$/, '/' + filename);
31b7f2792   Kload   Upgrade to ownclo...
33
  				var createDropDown = true;
03e52840d   Kload   Init
34
  				// Check if drop down is already visible for a different file
31b7f2792   Kload   Upgrade to ownclo...
35
36
37
  				if (($('#dropdown').length > 0) ) {
  					if ( $('#dropdown').hasClass('drop-versions') && file == $('#dropdown').data('file')) {
  						createDropDown = false;
03e52840d   Kload   Init
38
  					}
31b7f2792   Kload   Upgrade to ownclo...
39
40
41
42
43
  					$('#dropdown').remove();
  					$('tr').removeClass('mouseOver');
  				}
  
  				if(createDropDown === true) {
6d9380f96   Cédric Dupont   Update sources OC...
44
  					createVersionsDropdown(filename, file, context.fileList);
03e52840d   Kload   Init
45
  				}
6d9380f96   Cédric Dupont   Update sources OC...
46
  			}, t('files_versions', 'Versions')
03e52840d   Kload   Init
47
48
  		);
  	}
03e52840d   Kload   Init
49

31b7f2792   Kload   Upgrade to ownclo...
50
51
52
53
54
  	$(document).on("click", 'span[class="revertVersion"]', function() {
  		var revision = $(this).attr('id');
  		var file = $(this).attr('value');
  		revertFile(file, revision);
  	});
03e52840d   Kload   Init
55

31b7f2792   Kload   Upgrade to ownclo...
56
  });
03e52840d   Kload   Init
57

31b7f2792   Kload   Upgrade to ownclo...
58
  function revertFile(file, revision) {
03e52840d   Kload   Init
59
60
61
  
  	$.ajax({
  		type: 'GET',
31b7f2792   Kload   Upgrade to ownclo...
62
  		url: OC.linkTo('files_versions', 'ajax/rollbackVersion.php'),
03e52840d   Kload   Init
63
  		dataType: 'json',
31b7f2792   Kload   Upgrade to ownclo...
64
  		data: {file: file, revision: revision},
03e52840d   Kload   Init
65
  		async: false,
31b7f2792   Kload   Upgrade to ownclo...
66
67
68
69
70
  		success: function(response) {
  			if (response.status === 'error') {
  				OC.Notification.show( t('files_version', 'Failed to revert {file} to revision {timestamp}.', {file:file, timestamp:formatDate(revision * 1000)}) );
  			} else {
  				$('#dropdown').hide('blind', function() {
6d9380f96   Cédric Dupont   Update sources OC...
71
  					$('#dropdown').closest('tr').find('.modified:first').html(relative_modified_date(revision));
31b7f2792   Kload   Upgrade to ownclo...
72
73
  					$('#dropdown').remove();
  					$('tr').removeClass('mouseOver');
31b7f2792   Kload   Upgrade to ownclo...
74
75
76
77
78
79
  				});
  			}
  		}
  	});
  
  }
03e52840d   Kload   Init
80

31b7f2792   Kload   Upgrade to ownclo...
81
82
83
  function goToVersionPage(url){
  	window.location.assign(url);
  }
03e52840d   Kload   Init
84

6d9380f96   Cédric Dupont   Update sources OC...
85
  function createVersionsDropdown(filename, files, fileList) {
03e52840d   Kload   Init
86

31b7f2792   Kload   Upgrade to ownclo...
87
  	var start = 0;
a293d369c   Kload   Update sources to...
88
  	var fileEl;
03e52840d   Kload   Init
89

31b7f2792   Kload   Upgrade to ownclo...
90
91
92
93
94
95
  	var html = '<div id="dropdown" class="drop drop-versions" data-file="'+escapeHTML(files)+'">';
  	html += '<div id="private">';
  	html += '<ul id="found_versions">';
  	html += '</ul>';
  	html += '</div>';
  	html += '<input type="button" value="'+ t('files_versions', 'More versions...') + '" name="show-more-versions" id="show-more-versions" style="display: none;" />';
03e52840d   Kload   Init
96

31b7f2792   Kload   Upgrade to ownclo...
97
  	if (filename) {
6d9380f96   Cédric Dupont   Update sources OC...
98
  		fileEl = fileList.findFileEl(filename);
a293d369c   Kload   Update sources to...
99
100
  		fileEl.addClass('mouseOver');
  		$(html).appendTo(fileEl.find('td.filename'));
31b7f2792   Kload   Upgrade to ownclo...
101
102
103
  	} else {
  		$(html).appendTo($('thead .share'));
  	}
03e52840d   Kload   Init
104

31b7f2792   Kload   Upgrade to ownclo...
105
106
  	getVersions(start);
  	start = start + 5;
03e52840d   Kload   Init
107

31b7f2792   Kload   Upgrade to ownclo...
108
109
110
111
  	$("#show-more-versions").click(function() {
  		//get more versions
  		getVersions(start);
  		start = start + 5;
03e52840d   Kload   Init
112
  	});
31b7f2792   Kload   Upgrade to ownclo...
113
  	function getVersions(start) {
03e52840d   Kload   Init
114
115
  		$.ajax({
  			type: 'GET',
31b7f2792   Kload   Upgrade to ownclo...
116
  			url: OC.filePath('files_versions', 'ajax', 'getVersions.php'),
03e52840d   Kload   Init
117
  			dataType: 'json',
31b7f2792   Kload   Upgrade to ownclo...
118
  			data: {source: files, start: start},
03e52840d   Kload   Init
119
  			async: false,
31b7f2792   Kload   Upgrade to ownclo...
120
121
122
123
  			success: function(result) {
  				var versions = result.data.versions;
  				if (result.data.endReached === true) {
  					$("#show-more-versions").css("display", "none");
03e52840d   Kload   Init
124
  				} else {
31b7f2792   Kload   Upgrade to ownclo...
125
126
127
128
129
  					$("#show-more-versions").css("display", "block");
  				}
  				if (versions) {
  					$.each(versions, function(index, row) {
  						addVersion(row);
03e52840d   Kload   Init
130
  					});
31b7f2792   Kload   Upgrade to ownclo...
131
132
  				} else {
  					$('<div style="text-align:center;">'+ t('files_versions', 'No other versions available') + '</div>').appendTo('#dropdown');
03e52840d   Kload   Init
133
  				}
31b7f2792   Kload   Upgrade to ownclo...
134
135
136
137
  				$('#found_versions').change(function() {
  					var revision = parseInt($(this).val());
  					revertFile(files, revision);
  				});
03e52840d   Kload   Init
138
139
  			}
  		});
03e52840d   Kload   Init
140
141
142
  	}
  
  	function addVersion( revision ) {
31b7f2792   Kload   Upgrade to ownclo...
143
144
145
146
147
148
  		var title = formatDate(revision.version*1000);
  		var name ='<span class="versionDate" title="' + title + '">' + revision.humanReadableTimestamp + '</span>';
  
  		var path = OC.filePath('files_versions', '', 'download.php');
  
  		var preview = '<img class="preview" src="'+revision.preview+'"/>';
a293d369c   Kload   Update sources to...
149
  		var download ='<a href="' + path + "?file=" + encodeURIComponent(files) + '&revision=' + revision.version + '">';
31b7f2792   Kload   Upgrade to ownclo...
150
151
152
153
154
155
156
  		download+='<img';
  		download+=' src="' + OC.imagePath('core', 'actions/download') + '"';
  		download+=' name="downloadVersion" />';
  		download+=name;
  		download+='</a>';
  
  		var revert='<span class="revertVersion"';
a293d369c   Kload   Update sources to...
157
  		revert+=' id="' + revision.version + '">';
31b7f2792   Kload   Upgrade to ownclo...
158
159
160
161
162
163
164
165
  		revert+='<img';
  		revert+=' src="' + OC.imagePath('core', 'actions/history') + '"';
  		revert+=' name="revertVersion"';
  		revert+='/>'+t('files_versions', 'Restore')+'</span>';
  
  		var version=$('<li/>');
  		version.attr('value', revision.version);
  		version.html(preview + download + revert);
a293d369c   Kload   Update sources to...
166
167
  		// add file here for proper name escaping
  		version.find('span.revertVersion').attr('value', files);
03e52840d   Kload   Init
168
169
170
  
  		version.appendTo('#found_versions');
  	}
03e52840d   Kload   Init
171
  	$('#dropdown').show('blind');
03e52840d   Kload   Init
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  }
  
  $(this).click(
  	function(event) {
  	if ($('#dropdown').has(event.target).length === 0 && $('#dropdown').hasClass('drop-versions')) {
  		$('#dropdown').hide('blind', function() {
  			$('#dropdown').remove();
  			$('tr').removeClass('mouseOver');
  		});
  	}
  
  
  	}
  );