update.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2014
  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. (function() {
  11. OC.Update = {
  12. _started : false,
  13. /**
  14. * Start the update process.
  15. *
  16. * @param $el progress list element
  17. */
  18. start: function($el, options) {
  19. if (this._started) {
  20. return;
  21. }
  22. var hasWarnings = false;
  23. this.$el = $el;
  24. this._started = true;
  25. var self = this;
  26. $(window).on('beforeunload.inprogress', function () {
  27. return t('core', 'The update is in progress, leaving this page might interrupt the process in some environments.');
  28. });
  29. $('#update-progress-title').html(t(
  30. 'core',
  31. 'Update to {version}', {
  32. version: options.version
  33. })
  34. );
  35. var updateEventSource = new OC.EventSource(OC.getRootPath()+'/core/ajax/update.php');
  36. updateEventSource.listen('success', function(message) {
  37. self.setMessage(message);
  38. });
  39. updateEventSource.listen('notice', function(message) {
  40. self.setPermanentMessage(message);
  41. hasWarnings = true;
  42. });
  43. updateEventSource.listen('error', function(message) {
  44. $('#update-progress-message').hide();
  45. $('#update-progress-icon')
  46. .addClass('icon-error-white')
  47. .removeClass('icon-loading-dark');
  48. message = message || t('core', 'An error occurred.');
  49. $(window).off('beforeunload.inprogress');
  50. self.setErrorMessage(message);
  51. message = t('core', 'Please reload the page.');
  52. $('<p>').append('<a href=".">'+message+'</a>').appendTo($el);
  53. updateEventSource.close();
  54. });
  55. updateEventSource.listen('failure', function(message) {
  56. $(window).off('beforeunload.inprogress');
  57. $('#update-progress-message').hide();
  58. $('#update-progress-icon')
  59. .addClass('icon-error-white')
  60. .removeClass('icon-loading-dark');
  61. self.setErrorMessage(message);
  62. var updateUnsuccessful = $('<p>');
  63. if(message === 'Exception: Updates between multiple major versions and downgrades are unsupported.') {
  64. updateUnsuccessful.append(t('core', 'The update was unsuccessful. For more information <a href="{url}">check our forum post</a> covering this issue.', {'url': 'https://help.nextcloud.com/t/updates-between-multiple-major-versions-are-unsupported/7094'}));
  65. } else {
  66. updateUnsuccessful.append(t('core', 'The update was unsuccessful. ' +
  67. 'Please report this issue to the ' +
  68. '<a href="https://github.com/nextcloud/server/issues" target="_blank">Nextcloud community</a>.'));
  69. }
  70. updateUnsuccessful.appendTo($el);
  71. });
  72. updateEventSource.listen('done', function() {
  73. $(window).off('beforeunload.inprogress');
  74. $('#update-progress-message').hide();
  75. $('#update-progress-icon')
  76. .addClass('icon-checkmark-white')
  77. .removeClass('icon-loading-dark');
  78. if (hasWarnings) {
  79. $el.find('.update-show-detailed').before(
  80. $('<input type="button" class="update-continue" value="'+t('core', 'Continue to Nextcloud')+'">').on('click', function() {
  81. window.location.reload();
  82. })
  83. );
  84. } else {
  85. // FIXME: use product name
  86. $el.find('.update-show-detailed').before(
  87. $('<p id="redirect-countdown"></p>')
  88. );
  89. for(var i = 0; i <= 4; i++){
  90. self.updateCountdown(i, 4);
  91. }
  92. setTimeout(function () {
  93. OC.redirect(window.location.href);
  94. }, 3000);
  95. }
  96. });
  97. },
  98. updateCountdown: function (i, total) {
  99. setTimeout(function(){
  100. $("#redirect-countdown").text(n('core', 'The update was successful. Redirecting you to Nextcloud in %n second.', 'The update was successful. Redirecting you to Nextcloud in %n seconds.', i));
  101. }, (total - i) * 1000);
  102. },
  103. setMessage: function(message) {
  104. $('#update-progress-message').html(message);
  105. $('#update-progress-detailed')
  106. .append('<p>' + message + '</p>');
  107. },
  108. setPermanentMessage: function(message) {
  109. $('#update-progress-message').html(message);
  110. $('#update-progress-message-warnings')
  111. .show()
  112. .append($('<ul>').append(message));
  113. $('#update-progress-detailed')
  114. .append('<p>' + message + '</p>');
  115. },
  116. setErrorMessage: function (message) {
  117. $('#update-progress-message-error')
  118. .show()
  119. .html(message);
  120. $('#update-progress-detailed')
  121. .append('<p>' + message + '</p>');
  122. }
  123. };
  124. })();
  125. window.addEventListener('DOMContentLoaded', function() {
  126. $('.updateButton').on('click', function() {
  127. var $updateEl = $('.update');
  128. var $progressEl = $('.update-progress');
  129. $progressEl.removeClass('hidden');
  130. $('.updateOverview').addClass('hidden');
  131. $('#update-progress-message-error').hide();
  132. $('#update-progress-message-warnings').hide();
  133. OC.Update.start($progressEl, {
  134. productName: $updateEl.attr('data-productname'),
  135. version: $updateEl.attr('data-version')
  136. });
  137. return false;
  138. });
  139. $('.update-show-detailed').on('click', function() {
  140. $('#update-progress-detailed').toggleClass('hidden');
  141. return false;
  142. });
  143. });