move HomeButton widget programmatically

1608
4
Jump to solution
07-27-2016 12:13 AM
NatashaManzuiga
Occasional Contributor

Hi all,

at the top of my HomeButton I have a custom widget that can grow....

and I wish I could set top position value to HomeButton.

I am able to get the height of the custom widget

ar cs = domStyle.getComputedStyle(divCustomWidget);

var h = cs.height;

but how can I set the top distance at the HomeButton widget?

Thanks,

Naty

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Naty,

  You just need to get a reference to the widget:

Requires:

'jimu/WidgetManager',
'dojo/_base/html',
WidgetManager,
html,

main code

        var widgetCfg = this._getWidgetConfig('HomeButton');
        if(widgetCfg){
          var hbWidget = WidgetManager.getInstance().getWidgetByLabel(widgetCfg.label);
          html.setStyle(hbWidget.domNode, "top", "90px");
        }

support function:

_getWidgetConfig: function(widgetName){
        var widgetCnfg = null;
        array.some(WidgetManager.getInstance().appConfig.widgetPool.widgets, function(aWidget) {
          if(aWidget.name == widgetName) {
            widgetCnfg = aWidget;
            return true;
          }
          return false;
        });
        if(!widgetCnfg){
          /*Check OnScreen widgets if not found in widgetPool*/
          array.some(WidgetManager.getInstance().appConfig.widgetOnScreen.widgets, function(aWidget) {
            if(aWidget.name == widgetName) {
              widgetCnfg = aWidget;
              return true;
            }
            return false;
          });
        }
        return widgetCnfg;
      },

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Emeritus

Naty,

  You just need to get a reference to the widget:

Requires:

'jimu/WidgetManager',
'dojo/_base/html',
WidgetManager,
html,

main code

        var widgetCfg = this._getWidgetConfig('HomeButton');
        if(widgetCfg){
          var hbWidget = WidgetManager.getInstance().getWidgetByLabel(widgetCfg.label);
          html.setStyle(hbWidget.domNode, "top", "90px");
        }

support function:

_getWidgetConfig: function(widgetName){
        var widgetCnfg = null;
        array.some(WidgetManager.getInstance().appConfig.widgetPool.widgets, function(aWidget) {
          if(aWidget.name == widgetName) {
            widgetCnfg = aWidget;
            return true;
          }
          return false;
        });
        if(!widgetCnfg){
          /*Check OnScreen widgets if not found in widgetPool*/
          array.some(WidgetManager.getInstance().appConfig.widgetOnScreen.widgets, function(aWidget) {
            if(aWidget.name == widgetName) {
              widgetCnfg = aWidget;
              return true;
            }
            return false;
          });
        }
        return widgetCnfg;
      },
NatashaManzuiga
Occasional Contributor

Robert, your code perfectly works.

But if I resize the window, the HomeWidget reset the top distance to the one I set in config.json...

.....

Thanks,

Naty

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Naty,

  So you just have to add code to handle the re-layout in the LayoutManager.js for it then (lines 21-23):

_onLayoutChange: function(appConfig){
      this._changeMapPosition(appConfig);

      //relayout placehoder
      array.forEach(this.widgetPlaceholders, function(placeholder){
        placeholder.moveTo(appConfig.getConfigElementById(placeholder.configId).position);
      }, this);

      //relayout icons
      array.forEach(this.preloadWidgetIcons, function(icon){
        icon.moveTo(appConfig.getConfigElementById(icon.configId).position);
      }, this);

      //relayout paneless widget
      array.forEach(this.widgetManager.getOnScreenOffPanelWidgets(), function(widget){
        if(widget.closeable){
          //this widget position is controlled by icon
          return;
        }
        var position = appConfig.getConfigElementById(widget.id).position;
        if(widget.id.indexOf('HomeButton') > -1){
          position.top = 90;
        }
        widget.setPosition(position);
      }, this);

      //relayout groups
      array.forEach(this.preloadGroupPanels, function(panel){
        var position = appConfig.getConfigElementById(panel.config.id).panel.position;
        panel.setPosition(position);
      }, this);
    },
NatashaManzuiga
Occasional Contributor

Many thanks Robert!

Naty

0 Kudos