test.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const Mn = require('backbone.marionette');
  2. const App = require('../../main');
  3. const template = require('./test.ejs');
  4. module.exports = Mn.View.extend({
  5. template: template,
  6. className: 'modal-dialog',
  7. ui: {
  8. waiting: '.waiting',
  9. error: '.error',
  10. success: '.success',
  11. close: 'button.cancel'
  12. },
  13. events: {
  14. 'click @ui.close': function (e) {
  15. e.preventDefault();
  16. if (this.model.get('back_to_add')) {
  17. App.Controller.showNginxCertificateForm(this.model);
  18. } else {
  19. App.UI.closeModal();
  20. }
  21. },
  22. },
  23. onRender: function () {
  24. this.ui.error.hide();
  25. this.ui.success.hide();
  26. App.Api.Nginx.Certificates.testHttpChallenge(this.model.get('domain_names'))
  27. .then((result) => {
  28. let allOk = true;
  29. let text = '';
  30. for (const domain in result) {
  31. const status = result[domain];
  32. if (status === 'ok') {
  33. text += `<p><strong>${domain}:</strong> ${App.i18n('certificates', 'reachability-ok')}</p>`;
  34. } else {
  35. allOk = false;
  36. if (status === 'no-host') {
  37. text += `<p><strong>${domain}:</strong> ${App.i18n('certificates', 'reachability-not-resolved')}</p>`;
  38. } else if (status === 'failed') {
  39. text += `<p><strong>${domain}:</strong> ${App.i18n('certificates', 'reachability-failed-to-check')}</p>`;
  40. } else if (status === '404') {
  41. text += `<p><strong>${domain}:</strong> ${App.i18n('certificates', 'reachability-404')}</p>`;
  42. } else if (status === 'wrong-data') {
  43. text += `<p><strong>${domain}:</strong> ${App.i18n('certificates', 'reachability-wrong-data')}</p>`;
  44. } else if (status.startsWith('other:')) {
  45. const code = status.substring(6);
  46. text += `<p><strong>${domain}:</strong> ${App.i18n('certificates', 'reachability-other', {code})}</p>`;
  47. } else {
  48. // This should never happen
  49. text += `<p><strong>${domain}:</strong> ?</p>`;
  50. }
  51. }
  52. }
  53. this.ui.waiting.hide();
  54. if (allOk) {
  55. this.ui.success.html(text).show();
  56. } else {
  57. this.ui.error.html(text).show();
  58. }
  59. this.ui.close.prop('disabled', false);
  60. })
  61. .catch((e) => {
  62. console.error(e);
  63. this.ui.waiting.hide();
  64. this.ui.error.text(App.i18n('certificates', 'reachability-failed-to-reach-api')).show();
  65. this.ui.close.prop('disabled', false);
  66. });
  67. }
  68. });