versions.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. $(document).ready(function(){
  2. if ($('#isPublic').val()){
  3. // no versions actions in public mode
  4. // beware of https://github.com/owncloud/core/issues/4545
  5. // as enabling this might hang Chrome
  6. return;
  7. }
  8. if (typeof FileActions !== 'undefined') {
  9. // Add versions button to 'files/index.php'
  10. FileActions.register(
  11. 'file'
  12. , t('files_versions', 'Versions')
  13. , OC.PERMISSION_UPDATE
  14. , function() {
  15. // Specify icon for hitory button
  16. return OC.imagePath('core','actions/history');
  17. }
  18. ,function(filename){
  19. // Action to perform when clicked
  20. if (scanFiles.scanning){return;}//workaround to prevent additional http request block scanning feedback
  21. var file = $('#dir').val().replace(/(?!<=\/)$|\/$/, '/' + filename);
  22. var createDropDown = true;
  23. // Check if drop down is already visible for a different file
  24. if (($('#dropdown').length > 0) ) {
  25. if ( $('#dropdown').hasClass('drop-versions') && file == $('#dropdown').data('file')) {
  26. createDropDown = false;
  27. }
  28. $('#dropdown').remove();
  29. $('tr').removeClass('mouseOver');
  30. }
  31. if(createDropDown === true) {
  32. createVersionsDropdown(filename, file);
  33. }
  34. }
  35. );
  36. }
  37. $(document).on("click", 'span[class="revertVersion"]', function() {
  38. var revision = $(this).attr('id');
  39. var file = $(this).attr('value');
  40. revertFile(file, revision);
  41. });
  42. });
  43. function revertFile(file, revision) {
  44. $.ajax({
  45. type: 'GET',
  46. url: OC.linkTo('files_versions', 'ajax/rollbackVersion.php'),
  47. dataType: 'json',
  48. data: {file: file, revision: revision},
  49. async: false,
  50. success: function(response) {
  51. if (response.status === 'error') {
  52. OC.Notification.show( t('files_version', 'Failed to revert {file} to revision {timestamp}.', {file:file, timestamp:formatDate(revision * 1000)}) );
  53. } else {
  54. $('#dropdown').hide('blind', function() {
  55. $('#dropdown').remove();
  56. $('tr').removeClass('mouseOver');
  57. // TODO also update the modified time in the web ui
  58. });
  59. }
  60. }
  61. });
  62. }
  63. function goToVersionPage(url){
  64. window.location.assign(url);
  65. }
  66. function createVersionsDropdown(filename, files) {
  67. var start = 0;
  68. var html = '<div id="dropdown" class="drop drop-versions" data-file="'+escapeHTML(files)+'">';
  69. html += '<div id="private">';
  70. html += '<ul id="found_versions">';
  71. html += '</ul>';
  72. html += '</div>';
  73. html += '<input type="button" value="'+ t('files_versions', 'More versions...') + '" name="show-more-versions" id="show-more-versions" style="display: none;" />';
  74. if (filename) {
  75. $('tr').filterAttr('data-file',filename).addClass('mouseOver');
  76. $(html).appendTo($('tr').filterAttr('data-file',filename).find('td.filename'));
  77. } else {
  78. $(html).appendTo($('thead .share'));
  79. }
  80. getVersions(start);
  81. start = start + 5;
  82. $("#show-more-versions").click(function() {
  83. //get more versions
  84. getVersions(start);
  85. start = start + 5;
  86. });
  87. function getVersions(start) {
  88. $.ajax({
  89. type: 'GET',
  90. url: OC.filePath('files_versions', 'ajax', 'getVersions.php'),
  91. dataType: 'json',
  92. data: {source: files, start: start},
  93. async: false,
  94. success: function(result) {
  95. var versions = result.data.versions;
  96. if (result.data.endReached === true) {
  97. $("#show-more-versions").css("display", "none");
  98. } else {
  99. $("#show-more-versions").css("display", "block");
  100. }
  101. if (versions) {
  102. $.each(versions, function(index, row) {
  103. addVersion(row);
  104. });
  105. } else {
  106. $('<div style="text-align:center;">'+ t('files_versions', 'No other versions available') + '</div>').appendTo('#dropdown');
  107. }
  108. $('#found_versions').change(function() {
  109. var revision = parseInt($(this).val());
  110. revertFile(files, revision);
  111. });
  112. }
  113. });
  114. }
  115. function addVersion( revision ) {
  116. var title = formatDate(revision.version*1000);
  117. var name ='<span class="versionDate" title="' + title + '">' + revision.humanReadableTimestamp + '</span>';
  118. var path = OC.filePath('files_versions', '', 'download.php');
  119. var preview = '<img class="preview" src="'+revision.preview+'"/>';
  120. var download ='<a href="' + path + "?file=" + files + '&revision=' + revision.version + '">';
  121. download+='<img';
  122. download+=' src="' + OC.imagePath('core', 'actions/download') + '"';
  123. download+=' name="downloadVersion" />';
  124. download+=name;
  125. download+='</a>';
  126. var revert='<span class="revertVersion"';
  127. revert+=' id="' + revision.version + '"';
  128. revert+=' value="' + files + '">';
  129. revert+='<img';
  130. revert+=' src="' + OC.imagePath('core', 'actions/history') + '"';
  131. revert+=' name="revertVersion"';
  132. revert+='/>'+t('files_versions', 'Restore')+'</span>';
  133. var version=$('<li/>');
  134. version.attr('value', revision.version);
  135. version.html(preview + download + revert);
  136. version.appendTo('#found_versions');
  137. }
  138. $('tr').filterAttr('data-file',filename).addClass('mouseOver');
  139. $('#dropdown').show('blind');
  140. }
  141. $(this).click(
  142. function(event) {
  143. if ($('#dropdown').has(event.target).length === 0 && $('#dropdown').hasClass('drop-versions')) {
  144. $('#dropdown').hide('blind', function() {
  145. $('#dropdown').remove();
  146. $('tr').removeClass('mouseOver');
  147. });
  148. }
  149. }
  150. );