You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.4 KiB

  1. 'use strict';
  2. window.onload = function() {
  3. manageWebbyDisplay();
  4. new MutationObserver(function (mutations, self) {
  5. const op = document.getElementById(`operations-${data.shortName}-${data.operationId}`);
  6. if (!op) return;
  7. self.disconnect();
  8. op.querySelector('.opblock-summary').click();
  9. const tryOutObserver = new MutationObserver(function (mutations, self) {
  10. const tryOut = op.querySelector('.try-out__btn');
  11. if (!tryOut) return;
  12. self.disconnect();
  13. tryOut.click();
  14. if (data.id) {
  15. const inputId = op.querySelector('.parameters input[placeholder="id"]');
  16. inputId.value = data.id;
  17. reactTriggerChange(inputId);
  18. }
  19. for (const input of op.querySelectorAll('.parameters input')) {
  20. if (input.placeholder in data.queryParameters) {
  21. input.value = data.queryParameters[input.placeholder];
  22. reactTriggerChange(input);
  23. }
  24. }
  25. // Wait input values to be populated before executing the query
  26. setTimeout(function(){
  27. op.querySelector('.execute').click();
  28. op.scrollIntoView();
  29. }, 500);
  30. });
  31. tryOutObserver.observe(document, {childList: true, subtree: true});
  32. }).observe(document, {childList: true, subtree: true});
  33. const data = JSON.parse(document.getElementById('swagger-data').innerText);
  34. const ui = SwaggerUIBundle({
  35. spec: data.spec,
  36. dom_id: '#swagger-ui',
  37. validatorUrl: null,
  38. oauth2RedirectUrl: data.oauth.redirectUrl,
  39. presets: [
  40. SwaggerUIBundle.presets.apis,
  41. SwaggerUIStandalonePreset,
  42. ],
  43. plugins: [
  44. SwaggerUIBundle.plugins.DownloadUrl,
  45. ],
  46. layout: 'StandaloneLayout',
  47. });
  48. if (data.oauth.enabled) {
  49. ui.initOAuth({
  50. clientId: data.oauth.clientId,
  51. clientSecret: data.oauth.clientSecret,
  52. realm: data.oauth.type,
  53. appName: data.spec.info.title,
  54. scopeSeparator: ' ',
  55. additionalQueryStringParams: {}
  56. });
  57. }
  58. // Workaround for https://github.com/swagger-api/swagger-ui/issues/3028
  59. // Adapted from https://github.com/vitalyq/react-trigger-change/blob/master/lib/change.js
  60. // Copyright (c) 2017 Vitaly Kuznetsov
  61. // MIT License
  62. function reactTriggerChange(node) {
  63. // Do not try to delete non-configurable properties.
  64. // Value and checked properties on DOM elements are non-configurable in PhantomJS.
  65. function deletePropertySafe(elem, prop) {
  66. const desc = Object.getOwnPropertyDescriptor(elem, prop);
  67. if (desc && desc.configurable) {
  68. delete elem[prop];
  69. }
  70. }
  71. // React 16
  72. // Cache artificial value property descriptor.
  73. // Property doesn't exist in React <16, descriptor is undefined.
  74. const descriptor = Object.getOwnPropertyDescriptor(node, 'value');
  75. // React 0.14: IE9
  76. // React 15: IE9-IE11
  77. // React 16: IE9
  78. // Dispatch focus.
  79. const focusEvent = document.createEvent('UIEvents');
  80. focusEvent.initEvent('focus', false, false);
  81. node.dispatchEvent(focusEvent);
  82. // React 0.14: IE9
  83. // React 15: IE9-IE11
  84. // React 16
  85. // In IE9-10 imperative change of node value triggers propertychange event.
  86. // Update inputValueTracking cached value.
  87. // Remove artificial value property.
  88. // Restore initial value to trigger event with it.
  89. const initialValue = node.value;
  90. node.value = initialValue + '#';
  91. deletePropertySafe(node, 'value');
  92. node.value = initialValue;
  93. // React 15: IE11
  94. // For unknown reason React 15 added listener for propertychange with addEventListener.
  95. // This doesn't work, propertychange events are deprecated in IE11,
  96. // but allows us to dispatch fake propertychange which is handled by IE11.
  97. const propertychangeEvent = document.createEvent('HTMLEvents');
  98. propertychangeEvent.initEvent('propertychange', false, false);
  99. propertychangeEvent.propertyName = 'value';
  100. node.dispatchEvent(propertychangeEvent);
  101. // React 0.14: IE10-IE11, non-IE
  102. // React 15: non-IE
  103. // React 16: IE10-IE11, non-IE
  104. const inputEvent = document.createEvent('HTMLEvents');
  105. inputEvent.initEvent('input', true, false);
  106. node.dispatchEvent(inputEvent);
  107. // React 16
  108. // Restore artificial value property descriptor.
  109. if (descriptor) {
  110. Object.defineProperty(node, 'value', descriptor);
  111. }
  112. }
  113. function manageWebbyDisplay() {
  114. const webby = document.getElementsByClassName('webby')[0];
  115. if (!webby) return;
  116. const web = document.getElementsByClassName('web')[0];
  117. webby.classList.add('calm');
  118. web.classList.add('calm');
  119. webby.addEventListener('click', () => {
  120. if (webby.classList.contains('frighten')) {
  121. return;
  122. }
  123. webby.classList.replace('calm', 'frighten');
  124. web.classList.replace('calm', 'frighten');
  125. setTimeout(() => {
  126. webby.classList.replace('frighten', 'calm');
  127. web.classList.replace('frighten', 'calm');
  128. }, 10000);
  129. });
  130. }
  131. };