update.js 5.0 KB

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