Blame view

sources/apps/files_versions/js/versions.js 5.04 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  $(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;
  	}
  
  	if (typeof FileActions !== 'undefined') {
  		// Add versions button to 'files/index.php'
  		FileActions.register(
  			'file'
  			, t('files_versions', 'Versions')
  			, OC.PERMISSION_UPDATE
  			, function() {
  				// Specify icon for hitory button
  				return OC.imagePath('core','actions/history');
  			}
  			,function(filename){
  				// Action to perform when clicked
  				if (scanFiles.scanning){return;}//workaround to prevent additional http request block scanning feedback
31b7f2792   Kload   Upgrade to ownclo...
23
24
  				var file = $('#dir').val().replace(/(?!<=\/)$|\/$/, '/' + filename);
  				var createDropDown = true;
03e52840d   Kload   Init
25
  				// Check if drop down is already visible for a different file
31b7f2792   Kload   Upgrade to ownclo...
26
27
28
  				if (($('#dropdown').length > 0) ) {
  					if ( $('#dropdown').hasClass('drop-versions') && file == $('#dropdown').data('file')) {
  						createDropDown = false;
03e52840d   Kload   Init
29
  					}
31b7f2792   Kload   Upgrade to ownclo...
30
31
32
33
34
  					$('#dropdown').remove();
  					$('tr').removeClass('mouseOver');
  				}
  
  				if(createDropDown === true) {
03e52840d   Kload   Init
35
36
37
38
39
  					createVersionsDropdown(filename, file);
  				}
  			}
  		);
  	}
03e52840d   Kload   Init
40

31b7f2792   Kload   Upgrade to ownclo...
41
42
43
44
45
  	$(document).on("click", 'span[class="revertVersion"]', function() {
  		var revision = $(this).attr('id');
  		var file = $(this).attr('value');
  		revertFile(file, revision);
  	});
03e52840d   Kload   Init
46

31b7f2792   Kload   Upgrade to ownclo...
47
  });
03e52840d   Kload   Init
48

31b7f2792   Kload   Upgrade to ownclo...
49
  function revertFile(file, revision) {
03e52840d   Kload   Init
50
51
52
  
  	$.ajax({
  		type: 'GET',
31b7f2792   Kload   Upgrade to ownclo...
53
  		url: OC.linkTo('files_versions', 'ajax/rollbackVersion.php'),
03e52840d   Kload   Init
54
  		dataType: 'json',
31b7f2792   Kload   Upgrade to ownclo...
55
  		data: {file: file, revision: revision},
03e52840d   Kload   Init
56
  		async: false,
31b7f2792   Kload   Upgrade to ownclo...
57
58
59
60
61
62
63
64
65
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() {
  					$('#dropdown').remove();
  					$('tr').removeClass('mouseOver');
  					// TODO also update the modified time in the web ui
  				});
  			}
  		}
  	});
  
  }
03e52840d   Kload   Init
71

31b7f2792   Kload   Upgrade to ownclo...
72
73
74
  function goToVersionPage(url){
  	window.location.assign(url);
  }
03e52840d   Kload   Init
75

31b7f2792   Kload   Upgrade to ownclo...
76
  function createVersionsDropdown(filename, files) {
03e52840d   Kload   Init
77

31b7f2792   Kload   Upgrade to ownclo...
78
  	var start = 0;
a293d369c   Kload   Update sources to...
79
  	var fileEl;
03e52840d   Kload   Init
80

31b7f2792   Kload   Upgrade to ownclo...
81
82
83
84
85
86
  	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
87

31b7f2792   Kload   Upgrade to ownclo...
88
  	if (filename) {
a293d369c   Kload   Update sources to...
89
90
91
  		fileEl = FileList.findFileEl(filename);
  		fileEl.addClass('mouseOver');
  		$(html).appendTo(fileEl.find('td.filename'));
31b7f2792   Kload   Upgrade to ownclo...
92
93
94
  	} else {
  		$(html).appendTo($('thead .share'));
  	}
03e52840d   Kload   Init
95

31b7f2792   Kload   Upgrade to ownclo...
96
97
  	getVersions(start);
  	start = start + 5;
03e52840d   Kload   Init
98

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