DashboardGlassContext.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /*
  4. *+------------------------------------------------------------------------+
  5. *| Licensed Materials - Property of IBM
  6. *| IBM Cognos Products: Content Explorer
  7. *| (C) Copyright IBM Corp. 2018
  8. *|
  9. *| US Government Users Restricted Rights - Use, duplication or disclosure
  10. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  11. *+------------------------------------------------------------------------+
  12. */
  13. define([], function () {
  14. /**
  15. * Create a glass context instance that will simulate a dashboard perspective glass context from anywhere.
  16. * This class will wrap a given glass context, but adapt certain functions to behave as if we are in a dashboard perspective
  17. */
  18. var DashboardGlassContext = function () {
  19. function DashboardGlassContext(options) {
  20. _classCallCheck(this, DashboardGlassContext);
  21. this.model = options.model;
  22. this.impersonateGlassContext(options.glassContext);
  23. }
  24. DashboardGlassContext.prototype.setServices = function setServices(services) {
  25. this._services = services;
  26. };
  27. /**
  28. * Copy all function and members of the glass context to be part of this instance.
  29. * If we find the appController, we need to overrider with out own version
  30. */
  31. DashboardGlassContext.prototype.impersonateGlassContext = function impersonateGlassContext(glassContext) {
  32. for (var m in glassContext) {
  33. if (typeof glassContext[m] === 'function') {
  34. this[m] = glassContext[m].bind(glassContext);
  35. } else if (m === 'appController') {
  36. this[m] = this.impersonateAppController(glassContext[m]);
  37. } else {
  38. this[m] = glassContext[m];
  39. }
  40. }
  41. };
  42. /**
  43. * Create our own version of the app controller.
  44. * Copy all functions and memebers of the original appcontroller, but override:
  45. * - findCollection : return dashboard collections available in the dashboard model
  46. * - getCurrentContentView : return a call that simulates the dashbord perspective content view
  47. */
  48. DashboardGlassContext.prototype.impersonateAppController = function impersonateAppController(appController) {
  49. var newAppController = {};
  50. for (var m in appController) {
  51. if (typeof appController[m] === 'function') {
  52. if (m === 'findCollection') {
  53. newAppController[m] = function (id) {
  54. var _this = this;
  55. var whenReady = new Promise(function (resolve) {
  56. var collection = _this.model.collectionContainers.find(function (collection) {
  57. return collection.id === id;
  58. });
  59. resolve(collection ? collection.items : []);
  60. });
  61. return whenReady;
  62. }.bind(this);
  63. } else if (m === 'getCurrentContentView') {
  64. newAppController[m] = function () {
  65. return {
  66. services: this._services,
  67. getDashboardApi: function getDashboardApi() {
  68. return {
  69. getDashboardInfo: function getDashboardInfo() {
  70. return { type: 'exploration' };
  71. }
  72. };
  73. }
  74. };
  75. }.bind(this);
  76. } else {
  77. newAppController[m] = appController[m].bind(appController);
  78. }
  79. } else {
  80. newAppController[m] = appController[m];
  81. }
  82. }
  83. return newAppController;
  84. };
  85. return DashboardGlassContext;
  86. }();
  87. return DashboardGlassContext;
  88. });
  89. //# sourceMappingURL=DashboardGlassContext.js.map