define([
    'bluebird',
    'kb_lib/widget/mount'
], function (
    Promise,
    mount
) {
    'use strict';

    class BodyWidget {
        constructor(config) {
            this.runtime = config.runtime;

            this.widgetMount = null;
            this.hostNode = null;
            this.container = null;
            this.routeListener = null;
        }

        attach(node) {
            this.hostNode = node;
            this.container = this.hostNode.appendChild(document.createElement('div'));
            this.container.style.display = 'flex';
            this.container.style.flex = '1 1 0px';
            this.container.style['flex-direction'] = 'column';
            this.container.style['overflow-y'] = 'auto';

            this.widgetMount = new mount.WidgetMount({
                widgetManager: this.runtime.service('widget').widgetManager,
                node: this.container
            });
        }

        start() {
            this.routeListener = this.runtime.recv('app', 'route-widget', (data) => {
                if (data.routeHandler.route.widget) {
                    this.widgetMount.unmount()
                        .then(() => {
                            return this.runtime.sendp('ui', 'clearButtons');
                        })
                        .then(() => {
                            return this.widgetMount.mount(data.routeHandler.route.widget, data.routeHandler.params);
                        })
                        .catch((err) => {
                            // need a catch-all widget to mount here??
                            console.error('ERROR mounting widget', err, data);
                            this.widgetMount.unmount()
                                .then(() => {
                                    // Note that 'error' is a globally defined widget dependency.
                                    return this.widgetMount.mountWidget('error', {
                                        title: 'ERROR 😞',
                                        error: err
                                    });
                                })
                                .catch((err2) => {
                                    console.error('ERROR mounting error widget!');
                                    console.error(err2);
                                    console.error(err);
                                });
                        });
                } else {
                    console.warn('No widget in route');
                }
            });
        }

        stop() {
            if (this.routeListener) {
                this.runtime.drop(this.routeListener);
            }
            if (this.hostNode && this.container) {
                this.hostNode.removeChild(this.container);
            }
        }
    }

    return {Widget: BodyWidget};
});

@@ -1,21 +1,16 @@
 define(%5B
-%0A    
 'bluebir
@@ -12,20 +12,16 @@
 uebird',
-%0A   
  'kb_lib
@@ -31,16 +31,16 @@
 dget
+2
 /mount'
-%0A
 %5D, f
@@ -52,21 +52,16 @@
 on (
-%0A    
 Promise,
 %0A   
@@ -60,19 +60,14 @@
 ise,
-%0A   
  mount
-%0A
 ) %7B%0A
@@ -1083,35 +1083,461 @@
     
-this.widgetMount.unmount()%0A
+if (%0A                        this.widgetMount.mountedWidget &&%0A                        data.routeHandler.route.widget === this.widgetMount.mountedWidget.widgetId &&%0A                        data.routeHandler.route.reentrant%0A                    ) %7B%0A                        this.widgetMount.mountedWidget.widget.run(data.routeHandler.params);%0A                    %7D else %7B%0A                        this.widgetMount%0A                            .unmount()%0A    
     
@@ -1562,32 +1562,36 @@
   .then(() =%3E %7B%0A
+    
                 
@@ -1667,35 +1667,43 @@
                 
-%7D)%0A
+    %7D)%0A    
                 
@@ -1716,32 +1716,36 @@
   .then(() =%3E %7B%0A
+    
                 
@@ -1861,32 +1861,36 @@
                 
+    
 %7D)%0A             
@@ -1892,32 +1892,36 @@
                 
+  
+  
 .catch((err) =%3E 
@@ -1918,24 +1918,28 @@
 ((err) =%3E %7B%0A
+    
             
@@ -2017,32 +2017,36 @@
                 
+   
+ 
 console.error('E
@@ -2100,32 +2100,36 @@
                 
+    
 this.widgetMount
@@ -2128,16 +2128,53 @@
 getMount
+%0A                                    
 .unmount
@@ -2200,32 +2200,36 @@
                 
+   
+ 
 .then(() =%3E %7B%0A  
@@ -2218,32 +2218,36 @@
   .then(() =%3E %7B%0A
+    
                 
@@ -2328,16 +2328,20 @@
 ndency.%0A
+    
         
@@ -2455,16 +2455,20 @@
         
+    
 title: '
@@ -2517,16 +2517,20 @@
         
+    
 error: e
@@ -2560,32 +2560,36 @@
                 
+ 
+   
 %7D);%0A            
@@ -2600,32 +2600,36 @@
                 
+    
 %7D)%0A             
@@ -2639,32 +2639,36 @@
                 
+  
+  
 .catch((err2) =%3E
@@ -2666,24 +2666,28 @@
 (err2) =%3E %7B%0A
+    
             
@@ -2785,32 +2785,36 @@
                 
+   
+ 
 console.error(er
@@ -2818,16 +2818,20 @@
 (err2);%0A
+    
         
@@ -2902,32 +2902,36 @@
                 
+    
 %7D);%0A            
@@ -2934,35 +2934,61 @@
                 
-%7D);
+    %7D);%0A                    %7D
 %0A               
@@ -3375,16 +3375,17 @@
 return %7B
+ 
 Widget: 
@@ -3394,15 +3394,16 @@
 dyWidget
+ 
 %7D;%0A%7D);%0A

support for re-entering mounted plugin panel