ToolbarHelper.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, 2019
  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(['underscore', '../lib/@waca/dashboard-common/dist/ui/AuthoringToolbar', 'jquery'], function (_, Toolbar, $) {
  14. var ToolbarHelper = function () {
  15. /**
  16. * The ToolbarHelper is is designed to hide common jquery authoring toolbar code
  17. * for things like the filter dialog or the slot action menu.
  18. */
  19. function ToolbarHelper(attributes) {
  20. _classCallCheck(this, ToolbarHelper);
  21. attributes = attributes || {};
  22. this._cbOnRemove = attributes.cbOnRemove || this._cbOnRemove;
  23. }
  24. ToolbarHelper.prototype.getToolbar = function getToolbar() {
  25. return this._actionMenuToolbar;
  26. };
  27. /**
  28. * build an action toolbar based on dashboard authoring toolbar class.
  29. * @param aActions - the actions to be added to the toolbarRoot
  30. * @param parentNode - the parentNode that contains the toolbar.
  31. * @param label - a label for the toolbar
  32. * @param userOptions - options such as placement and the attachPoint (class of the node where the toolbar will live)
  33. */
  34. ToolbarHelper.prototype.buildActionToolbar = function buildActionToolbar(aActions, parentNode, sLabel, userOptions) {
  35. var toolbarOptions = _.clone(userOptions);
  36. toolbarOptions.textOnly = true;
  37. toolbarOptions.container = $('body');
  38. toolbarOptions.notCentered = true;
  39. toolbarOptions.popoverClass = 'popover actionToolbarPopover text';
  40. //We expect to find a div with this class under the parent to root the menu at (by default, the className is menuroot )
  41. parentNode = $(parentNode).find(userOptions.attachPoint || '.toolbarRoot');
  42. var toolbar = new Toolbar(toolbarOptions);
  43. toolbar.setName(sLabel);
  44. toolbar.addItems(aActions);
  45. toolbar.setSelectionContext([parentNode]);
  46. toolbar.show(toolbarOptions.placement);
  47. parentNode.addClass('selected');
  48. var hideToolbar = function hideToolbar() {
  49. this._clearToolbar();
  50. setTimeout(this._cbOnRemove.bind(this), 100);
  51. };
  52. // This event is triggered only when user clicks away or press Escape to hide the toolbar.
  53. // We refresh the UI after that.
  54. // Other events like onPopupClose and bspopover.hidden are called when toolbar is changed.
  55. toolbar.on('flyout:hide', hideToolbar, this);
  56. toolbar.on('toolbar:remove', this._cbOnRemove.bind(this));
  57. this._actionMenuToolbar = toolbar;
  58. };
  59. //Default implementation when the toolbar is removed, do nothing.
  60. //A user can override this to perform some specific action at remove time in the arguments.
  61. ToolbarHelper.prototype._cbOnRemove = function _cbOnRemove() {};
  62. ToolbarHelper.prototype._clearToolbar = function _clearToolbar() {
  63. if (this._actionMenuToolbar) {
  64. _.each(this._actionMenuToolbar.selectionNodes, function (node) {
  65. $(node).removeClass('selected');
  66. });
  67. this._actionMenuToolbar.hide();
  68. this._actionMenuToolbar.remove();
  69. this._actionMenuToolbar = null;
  70. }
  71. };
  72. return ToolbarHelper;
  73. }();
  74. return ToolbarHelper;
  75. });
  76. //# sourceMappingURL=ToolbarHelper.js.map