VidaSdkUtil.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Watson Analytics (C) Copyright IBM Corp. 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['jquery', 'underscore'], function ($, _) {
  9. var SDK_REQUIRE_CONTEXT = 'Dashboard.VidaSdkUtil';
  10. // eslint-disable-next-line no-undef
  11. var rjs = requirejs;
  12. /**
  13. * Deletes the new requirejs context and cleans up any new <script/> tags Require created
  14. */
  15. var cleanupRequireContext = function cleanupRequireContext() {
  16. delete rjs.s.contexts[SDK_REQUIRE_CONTEXT];
  17. $('[data-requirecontext="' + SDK_REQUIRE_CONTEXT + '"]').remove();
  18. };
  19. return function () {
  20. function VidaSdkUtil() {
  21. _classCallCheck(this, VidaSdkUtil);
  22. }
  23. VidaSdkUtil.isSDKActive = function isSDKActive(dashboardApi) {
  24. var visDefinitions = dashboardApi.getFeature('VisDefinitions');
  25. var visPreview = visDefinitions.getById('visualizationPreview');
  26. var visualizationPreviewHasBeenLoaded = visPreview && !visPreview.getState().getError();
  27. var promise = void 0;
  28. if (visualizationPreviewHasBeenLoaded) {
  29. // The SDK bundle is currently loaded... but the server may not be running any more.
  30. // This code reloads the manifest.xml to verify the servier is active... or not.
  31. promise = new Promise(function (resolve, reject) {
  32. require(['com/ibm/vida/control/vida'], function (vida) {
  33. // We need a fresh requirejs context here...
  34. // so we don't go undefining things in the global scope
  35. var globalRequireConifg = JSON.parse(JSON.stringify(rjs.s.contexts._.config));
  36. var minimalRequireConfig = {
  37. context: SDK_REQUIRE_CONTEXT,
  38. baseUrl: globalRequireConifg.baseUrl,
  39. urlArgs: 'v=' + Date.now(),
  40. paths: _.pick(globalRequireConifg.paths, 'text', vida.sdk.bundleLocation)
  41. };
  42. var req = require.config(minimalRequireConfig);
  43. var manifestPath = 'text!' + vida.sdk.bundleLocation + '/manifest.xml';
  44. // Require the manifest, and use resolve/reject as success/failure handlers
  45. req([manifestPath], function () {
  46. cleanupRequireContext();
  47. resolve();
  48. }, function (err) {
  49. cleanupRequireContext();
  50. reject(err);
  51. });
  52. }, reject);
  53. });
  54. } else {
  55. // The bundle is currently not loaded, attempt to load it.
  56. promise = Promise.race([
  57. // Note: This will fail to resolve on IE due to a Mixed/Content error, unless the user has accepted
  58. // the browser warning and refreshed the page...
  59. visDefinitions.loadById('visualizationPreview'),
  60. // Lets give IE 15 seconds?
  61. new Promise(function (resolve, reject) {
  62. setTimeout(function () {
  63. reject(new Error('Failed to load test visualization'));
  64. }, 15000);
  65. })]);
  66. }
  67. return promise.then(function () {
  68. return true;
  69. }).catch(function () {
  70. return false;
  71. });
  72. };
  73. return VidaSdkUtil;
  74. }();
  75. });
  76. //# sourceMappingURL=VidaSdkUtil.js.map