view class doc
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146/// FOURJS_START_COPYRIGHT(D,2019)
/// Property of Four Js*
/// (c) Copyright Four Js 2019, 2022. All Rights Reserved.
/// * Trademark of Four Js Development Tools Europe Ltd
///   in the United States and elsewhere
///
/// This file can be modified by licensees according to the
/// product manual.
/// FOURJS_END_COPYRIGHT

"use strict";

modulum('RowBoundDecoratorWidget', ['WidgetBase', 'WidgetFactory'],
  function(context, cls) {

    /**
     * RowBound decorator.
     * @class RowBoundDecoratorWidget
     * @memberOf classes
     * @extends classes.WidgetBase
     * @publicdoc
     */
    cls.RowBoundDecoratorWidget = context.oo.Class(cls.WidgetBase, function($super) {
      return /** @lends classes.RowBoundDecoratorWidget.prototype */ {
        __name: "RowBoundDecoratorWidget",

        /** @type {classes.ContextMenuWidget} */
        _contextMenuWidget: null,

        /**
         * List of quick actions (available directly without context menu)
         * @type {Map<Element, classes.MenuLabelWidget>}
         */
        _quickActions: null,

        /** @type {Number} */
        _quickActionsNumber: 0,

        /** @type {Element} */
        _contextMenuIconElement: null,

        /**
         * @inheritDoc
         */
        _initElement: function() {
          this._ignoreLayout = true;
          $super._initElement.call(this);

          this._quickActions = new Map();

          this._contextMenuIconElement = this._element.getElementsByTagName('span')[0];
          this._contextMenuIconElement.toggleClass("hidden", true);
        },

        /**
         * @inheritDoc
         */
        destroy: function() {
          this._quickActions.clear();
          this._quickActions = null;

          $super.destroy.call(this);
        },

        /**
         * @inheritDoc
         */
        manageMouseClick: function(domEvent) {

          let rowBoundContainerWidget = this.getTableWidgetBase();
          if (!rowBoundContainerWidget && this.getParentWidget().isInstanceOf(cls.StretchableScrollGridLineWidget)) {
            rowBoundContainerWidget = this.getParentWidget().getParentWidget();
          }

          if (rowBoundContainerWidget) {
            // request focus
            if (this.getParentWidget().requestFocus) {
              this.getParentWidget().requestFocus(domEvent);
            }

            if (domEvent.target.isElementOrChildOf(this._contextMenuIconElement)) {
              // request open context  menu
              let rowBoundWidget = rowBoundContainerWidget.getRowBoundWidget();
              if (rowBoundWidget) {
                rowBoundWidget.parentElement = this._contextMenuIconElement;
                rowBoundWidget.reverseX = true;
                rowBoundContainerWidget.emit(context.constants.widgetEvents.rowBoundMenu);
                return false;
              }
            } else { // click on quick action
              for (const [elem, menuLabel] of this._quickActions) {
                if (domEvent.target.isElementOrChildOf(elem)) {
                  menuLabel.emit(context.constants.widgetEvents.click);
                  return false;
                }
              }
            }

          }
          return true;
        },

        /**
         * Set the reference to rowBound context menu
         * @param {classes.ContextMenuWidget} contextMenu
         */
        setContextMenuWidget: function(contextMenu) {
          this._contextMenuWidget = contextMenu;
        },

        /**
         * Update quick actions
         */
        updateQuickActions: function() {

          // Remove all quick actions
          if (this._element.hasChildNodes()) {
            let children = this._element.childNodes;
            while (children.length > 1) {
              this._element.removeChild(this._element.lastChild);
            }
          }
          this._quickActions.clear();

          // Build quick actions and hide them from context menu
          let actions = this._contextMenuWidget.getActionWidgets();
          let i = 0;
          for (const [name, menuLabel] of actions) {
            if (i < this._quickActionsNumber) {
              let clone = menuLabel._imageContainer.cloneNode(true);
              this._element.appendChild(clone);
              this._quickActions.set(clone, menuLabel);
              menuLabel.setHidden(true);
            }
            i++;
          }

          // hide context menu icon if context menu is empty
          this._contextMenuIconElement.toggleClass("hidden", i <= this._quickActionsNumber);

        }
      };
    });
    cls.WidgetFactory.registerBuilder('RowBoundDecorator', cls.RowBoundDecoratorWidget);
  });