update.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.webroot+'/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. $('<span>').addClass('error').append('<a href=".">'+message+'</a><br />').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 span = $('<span>')
  63. .addClass('bold');
  64. if(message === 'Exception: Updates between multiple major versions and downgrades are unsupported.') {
  65. span.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'}));
  66. } else {
  67. span.append(t('core', 'The update was unsuccessful. ' +
  68. 'Please report this issue to the ' +
  69. '<a href="https://github.com/nextcloud/server/issues" target="_blank">Nextcloud community</a>.'));
  70. }
  71. span.appendTo($el);
  72. });
  73. updateEventSource.listen('done', function() {
  74. $(window).off('beforeunload.inprogress');
  75. $('#update-progress-message').hide();
  76. $('#update-progress-icon')
  77. .addClass('icon-checkmark-white')
  78. .removeClass('icon-loading-dark');
  79. if (hasWarnings) {
  80. $el.find('.update-show-detailed').before(
  81. $('<input type="button" class="update-continue" value="'+t('core', 'Continue to Nextcloud')+'">').on('click', function() {
  82. window.location.reload();
  83. })
  84. );
  85. } else {
  86. // FIXME: use product name
  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(OC.webroot + '/');
  95. }, 3000);
  96. }
  97. });
  98. },
  99. updateCountdown: function (i, total) {
  100. setTimeout(function(){
  101. $("#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));
  102. }, (total - i) * 1000);
  103. },
  104. setMessage: function(message) {
  105. $('#update-progress-message').html(message);
  106. $('#update-progress-detailed')
  107. .append($('<span>'))
  108. .append(message)
  109. .append($('<br>'));
  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($('<span>'))
  118. .append(message)
  119. .append($('<br>'));
  120. },
  121. setErrorMessage: function (message) {
  122. $('#update-progress-message-error')
  123. .show()
  124. .html(message);
  125. $('#update-progress-detailed')
  126. .append($('<span>'))
  127. .append(message)
  128. .append($('<br>'));
  129. }
  130. };
  131. })();
  132. $(document).ready(function() {
  133. $('.updateButton').on('click', function() {
  134. var $updateEl = $('.update');
  135. var $progressEl = $('.update-progress');
  136. $progressEl.removeClass('hidden');
  137. $('.updateOverview').addClass('hidden');
  138. $('#update-progress-message-error').hide();
  139. $('#update-progress-message-warnings').hide();
  140. OC.Update.start($progressEl, {
  141. productName: $updateEl.attr('data-productname'),
  142. version: $updateEl.attr('data-version')
  143. });
  144. return false;
  145. });
  146. $('.update-show-detailed').on('click', function() {
  147. $('#update-progress-detailed').toggleClass('hidden');
  148. return false;
  149. });
  150. });