IconPicker.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Cognos Products: Dashboard
  6. * (C) Copyright IBM Corp. 2019
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['react', 'react-dom', './IconPickerComponent'], function (React, ReactDOM, IconPickerComponent) {
  10. /**
  11. * @class IconPicker
  12. * @classdesc Displays a flyout containing selectable icons
  13. * The icon picker flyout will automatically close when a selection is made or user clicks off/presses esc
  14. */
  15. var IconPicker = function () {
  16. /**
  17. * @param {Object} options The options object
  18. * @param {Array[Object]} options.icons An array of icon objects to display in icon picker flyout.
  19. * @param {String} options.icons.id The id reference of the icon that is loaded in dom.
  20. * @param {String} options.icons.label The accessible label to display for the icon.
  21. * @param {Object} options.containerNode The container node to attach the flyout to.
  22. * @param {Object} options.onIconSelected A function which is called when an icon is selected in the flyout.
  23. * @param {Object} options.onClose A function which is called when the flyout is closing.
  24. */
  25. function IconPicker(options) {
  26. _classCallCheck(this, IconPicker);
  27. this._icons = options.icons;
  28. this._containerNode = options.containerNode || document.body;
  29. this._onIconSelectedCallback = options.onIconSelected;
  30. this._onCloseCallback = options.onClose;
  31. this._flyoutHolderNode = null;
  32. this._handleIconSelected = this._handleIconSelected.bind(this);
  33. this.close = this.close.bind(this);
  34. }
  35. /**
  36. * @function IconPicker#open
  37. * @description Opens the icon picker flyout
  38. * @param {Object} triggerNode Node to orient the flyout to.
  39. * @return {Promise} Promise is resolved when icon picker flyout is rendered
  40. */
  41. IconPicker.prototype.open = function open(triggerNode) {
  42. var _this = this;
  43. if (this.isOpen()) {
  44. // All ready open
  45. return Promise.resolve();
  46. }
  47. return new Promise(function (resolve) {
  48. _this._flyoutHolderNode = document.createElement('div');
  49. _this._flyoutHolderNode.classList.add('flyout-holder');
  50. _this._containerNode.appendChild(_this._flyoutHolderNode);
  51. ReactDOM.render(React.createElement(IconPickerComponent, {
  52. icons: _this._icons,
  53. onIconSelected: _this._handleIconSelected,
  54. onClose: _this.close,
  55. orientation: 'bottomLeft',
  56. triggerNode: triggerNode,
  57. domNodeToAttachTo: _this._containerNode
  58. }), _this._flyoutHolderNode, function () {
  59. resolve();
  60. });
  61. });
  62. };
  63. /**
  64. * @function IconPicker#isOpen
  65. * @description Returns the current state of flyout
  66. * @return {Boolen} True if is open
  67. */
  68. IconPicker.prototype.isOpen = function isOpen() {
  69. return this._flyoutHolderNode != null;
  70. };
  71. /**
  72. * @function IconPicker#close
  73. * @description Closes the icon picker flyout. Automatically destroys the flyout.
  74. */
  75. IconPicker.prototype.close = function close() {
  76. if (this._onCloseCallback) {
  77. this._onCloseCallback();
  78. }
  79. this.destroy();
  80. };
  81. /**
  82. * @function IconPicker#destroy
  83. * @description Destroys the flyout and cleanup any resources
  84. */
  85. IconPicker.prototype.destroy = function destroy() {
  86. if (this._flyoutHolderNode) {
  87. ReactDOM.unmountComponentAtNode(this._flyoutHolderNode);
  88. this._containerNode.removeChild(this._flyoutHolderNode);
  89. this._flyoutHolderNode = null;
  90. }
  91. };
  92. IconPicker.prototype._handleIconSelected = function _handleIconSelected(icon) {
  93. if (this._onIconSelectedCallback) {
  94. this._onIconSelectedCallback(icon);
  95. }
  96. this.close();
  97. };
  98. return IconPicker;
  99. }();
  100. return IconPicker;
  101. });
  102. //# sourceMappingURL=IconPicker.js.map