LassoController.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Licensed Materials - Property of IBM
  3. *
  4. * IBM Cognos Products: BI
  5. *
  6. * Copyright IBM Corp. 2017, 2019
  7. *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. /**
  11. * @file Controller to handle lasso related logic such as: lasso activation,
  12. * lasso co-oridinate provision etc.
  13. */
  14. 'use strict';
  15. define(['../../../lib/@waca/core-client/js/core-client/ui/core/Events', './LassoSelectView', 'dashboard-analytics/DynamicFileLoader'], function (Events, LassoSelectView, DynamicFileLoader) {
  16. var LassoController = Events.extend({
  17. /**
  18. * @param {Object} options.targetElement element (jquery)
  19. */
  20. init: function init(options) {
  21. var _views;
  22. LassoController.inherited('init', this, arguments);
  23. this.targetElement = options.targetElement;
  24. this.cordinateResolver = options.cordinateResolver;
  25. this.isDragging = options.isDragging;
  26. this._views = (_views = {}, _views[LassoController.DEFAULT_VIEW_ID] = { promise: Promise.resolve([LassoSelectView]), id: LassoController.DEFAULT_VIEW_ID }, _views); //Default view
  27. this.dashboard = options.dashboard;
  28. this._loadViewExts();
  29. },
  30. _loadViewExts: function _loadViewExts() {
  31. var _this = this;
  32. this.dashboard && this.dashboard.getCollectionExtension('com.ibm.bi.dashboard.live-lassoView').then(function (views) {
  33. return views.forEach(function (view) {
  34. return _this._views[view.id] = { promise: DynamicFileLoader.load([view.class]), css: view.css, id: view.id };
  35. });
  36. });
  37. },
  38. _loadCss: function _loadCss(href) {
  39. var head = document.getElementsByTagName('head')[0];
  40. var stylesheet = head.querySelectorAll('[href=\'' + href + '\']');
  41. if (!stylesheet.length) {
  42. var link = document.createElement('link');
  43. link.rel = 'stylesheet';
  44. link.type = 'text/css';
  45. link.href = href;
  46. link.media = 'all';
  47. head.appendChild(link);
  48. }
  49. },
  50. /**
  51. * Activate/Deactivate lasso selection mode
  52. * @param {boolean} [state=false] - state of lasso mode. True activates and
  53. * false deactivates the lasso mode. Defaults to false.
  54. */
  55. activateLassoMode: function activateLassoMode(state, viewId) {
  56. var _this2 = this;
  57. var _state = state || false;
  58. if (_state) {
  59. var _ref = this._views[viewId] || this._views[LassoController.DEFAULT_VIEW_ID],
  60. promise = _ref.promise,
  61. css = _ref.css,
  62. id = _ref.id;
  63. this.viewId = id;
  64. if (css) {
  65. this._loadCss(css);
  66. }
  67. return promise.then(function (modules) {
  68. _this2.view = new modules[0](_this2.targetElement, _this2.cordinateResolver, _this2.isDragging);
  69. _this2.view.on('all', _this2._handleViewEvent, _this2);
  70. });
  71. } else if (this.isLassoMode()) {
  72. this.view.off('all', this._handleViewEvent, this);
  73. this.view.remove();
  74. this.view = null;
  75. this.viewId = '';
  76. }
  77. return Promise.resolve();
  78. },
  79. /**
  80. * @return True if lasso mode is active, false otherwise.
  81. */
  82. isLassoMode: function isLassoMode() {
  83. return !!this.viewId;
  84. },
  85. /**
  86. * Handler view event
  87. */
  88. _handleViewEvent: function _handleViewEvent(payload, eventName) {
  89. this.trigger(eventName, payload);
  90. },
  91. getCoordinates: function getCoordinates() {
  92. return this.view && this.view.getCoordinates();
  93. },
  94. getCurrentViewId: function getCurrentViewId() {
  95. return this.viewId;
  96. },
  97. remove: function remove() {
  98. this.activateLassoMode(false);
  99. LassoController.inherited('remove', this, arguments);
  100. this.targetElement = null;
  101. }
  102. });
  103. LassoController.DEFAULT_VIEW_ID = 'lw-lasso-select';
  104. return LassoController;
  105. });
  106. //# sourceMappingURL=LassoController.js.map