update.js 5.0 KB

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