ReactBoardModule.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: Dashboard
  7. *| (C) Copyright IBM Corp. 2018, 2020
  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(['jquery', 'underscore', '../../../lib/@waca/core-client/js/core-client/utils/ClassFactory', '../../layout/views/LayoutBaseView', 'react', 'react-dom', '../../layout/authoring/views/feature/CommonAuthoringFeature'], function ($, _, ClassFactory, LayoutBaseView, React, ReactDOM, CommonAuthoringFeature) {
  14. var makeReactLayoutClass = function makeReactLayoutClass(data) {
  15. var ReactLayout = null;
  16. ReactLayout = LayoutBaseView.extend({
  17. init: function init(options) {
  18. ReactLayout.inherited('init', this, arguments);
  19. this.model = options.model;
  20. this.content = options.content;
  21. this.layoutController = options.layoutController;
  22. this.services = options.services;
  23. this.commonAuthoringFeature = new CommonAuthoringFeature(this);
  24. },
  25. onShow: function onShow() {
  26. ReactLayout.inherited('onShow', this, arguments);
  27. if (this.layoutController) {
  28. var className = data.type + '-react-layout-view';
  29. var $root = null;
  30. if (this.domNode) {
  31. $root = $(this.domNode);
  32. } else {
  33. $root = this.layoutController.$el.find('.' + className);
  34. }
  35. if ($root.length > 0) {
  36. var existingDomNodes = this._collectExistingItemNodes($root);
  37. ReactDOM.render(React.createElement(data.Component, {
  38. items: this.model ? this.model.items : [],
  39. model: this.model,
  40. services: this.services,
  41. layoutController: this.layoutController
  42. }, null), $root.get(0));
  43. this._repositionExistingItemNodes($root, existingDomNodes);
  44. }
  45. }
  46. },
  47. _collectExistingItemNodes: function _collectExistingItemNodes($root) {
  48. var existingDomNodes = [];
  49. if (this.model && this.model.items) {
  50. _.each(this.model.items, function (item) {
  51. var $node = $root.find('#' + item.id);
  52. if ($node.length) {
  53. existingDomNodes.push({
  54. item: item,
  55. node: $node[0]
  56. });
  57. }
  58. });
  59. }
  60. return existingDomNodes;
  61. },
  62. _repositionExistingItemNodes: function _repositionExistingItemNodes($root, existingDomNodes) {
  63. _.each(existingDomNodes, function (o) {
  64. var $stub = $root.find('#' + o.item.id + '_stub');
  65. if ($stub.length) {
  66. $stub.append(o.node);
  67. }
  68. });
  69. },
  70. add: function add() /*layoutView, insertBeforeId*/{
  71. //no-op - expected by CommonAuthoringFeature.onModelAddItem
  72. },
  73. destroy: function destroy() {}
  74. });
  75. ReactLayout.getTemplate = function () {
  76. return ['<div id="{{id}}" tabindex="-1" class="page {{type}}-react-layout-view{{extraCSS}}"{{style}}>{{items}}</div>', '{{item}}'];
  77. };
  78. return ReactLayout;
  79. };
  80. var ReactBoardModule = function () {
  81. function ReactBoardModule(props) {
  82. _classCallCheck(this, ReactBoardModule);
  83. props = props || {};
  84. this._loader = props.loader || ClassFactory;
  85. this.logger = props.logger;
  86. }
  87. ReactBoardModule.prototype.load = function load(module) {
  88. var _this = this;
  89. var path = module.path || module;
  90. return this._loader.loadModule(path).then(function (Component) {
  91. var ModuleClass = makeReactLayoutClass({
  92. type: module.id,
  93. Component: Component
  94. });
  95. return {
  96. template: ModuleClass.getTemplate,
  97. module: ModuleClass
  98. };
  99. }).catch(function (error) {
  100. _this.logger.warn('[ReactBoardModule]: attempted to load module "' + path + ' and failed"', error);
  101. throw error;
  102. });
  103. };
  104. return ReactBoardModule;
  105. }();
  106. return ReactBoardModule;
  107. });
  108. //# sourceMappingURL=ReactBoardModule.js.map