From 1e65b74782afe08ba75b7d085058e6eca6fdd488 2014-05-08 18:46:48
From: Jonathan Frederic <jon.freder@gmail.com>
Date: 2014-05-08 18:46:48
Subject: [PATCH] Fixed bugs in displayed event triggering for containers

---

diff --git a/IPython/html/static/widgets/js/manager.js b/IPython/html/static/widgets/js/manager.js
index d1af78c..4cab8e3 100644
--- a/IPython/html/static/widgets/js/manager.js
+++ b/IPython/html/static/widgets/js/manager.js
@@ -123,6 +123,7 @@
                 model.on('destroy', view.remove, view);
                 return view;
             }
+            console.log('VIEW NOT REGISTERED?!');
             return null;
         };
 
diff --git a/IPython/html/static/widgets/js/widget_container.js b/IPython/html/static/widgets/js/widget_container.js
index 6fcbaca..c55f359 100644
--- a/IPython/html/static/widgets/js/widget_container.js
+++ b/IPython/html/static/widgets/js/widget_container.js
@@ -27,6 +27,18 @@ define(["widgets/js/widget"], function(WidgetManager) {
                 this.update_children(model.previous('_children'), value);
             }, this);
             this.update();
+
+            // Trigger model displayed events for any models that are child to 
+            // this model when this model is displayed.
+            var that = this;
+            this.model.on('displayed', function(){
+                that.is_displayed = true;
+                for (var property in that.child_views) {
+                    if (that.child_views.hasOwnProperty(property)) {
+                        that.child_views[property].model.trigger('displayed');
+                    }
+                }
+            });
         },
         
         update_children: function(old_list, new_list) {
@@ -47,6 +59,11 @@ define(["widgets/js/widget"], function(WidgetManager) {
             // Called when a model is added to the children list.
             var view = this.create_child_view(model);
             this.$el.append(view.$el);
+
+            // Trigger the displayed event if this model is displayed.
+            if (this.is_displayed) {
+                model.trigger('displayed');
+            }
         },
         
         update: function(){
@@ -81,7 +98,7 @@ define(["widgets/js/widget"], function(WidgetManager) {
             // need to know about all of the top-level widgets.  The IPython
             // widget manager uses this to register the elements with the
             // keyboard manager.
-            this.additional_elements = [this.$window]
+            this.additional_elements = [this.$window];
 
             this.$title_bar = $('<div />')
                 .addClass('popover-title')
@@ -169,6 +186,17 @@ define(["widgets/js/widget"], function(WidgetManager) {
                 this.update_children(model.previous('_children'), value);
             }, this);
             this.update();
+
+            // Trigger model displayed events for any models that are child to 
+            // this model when this model is displayed.
+            this.model.on('displayed', function(){
+                that.is_displayed = true;
+                for (var property in that.child_views) {
+                    if (that.child_views.hasOwnProperty(property)) {
+                        that.child_views[property].model.trigger('displayed');
+                    }
+                }
+            });
         },
         
         hide: function() {
@@ -228,6 +256,11 @@ define(["widgets/js/widget"], function(WidgetManager) {
             // Called when a child is added to children list.
             var view = this.create_child_view(model);
             this.$body.append(view.$el);
+
+            // Trigger the displayed event if this model is displayed.
+            if (this.is_displayed) {
+                model.trigger('displayed');
+            }
         },
         
         update: function(){
diff --git a/IPython/html/static/widgets/js/widget_selectioncontainer.js b/IPython/html/static/widgets/js/widget_selectioncontainer.js
index 2031356..a585162 100644
--- a/IPython/html/static/widgets/js/widget_selectioncontainer.js
+++ b/IPython/html/static/widgets/js/widget_selectioncontainer.js
@@ -35,8 +35,17 @@ define(["widgets/js/widget"], function(WidgetManager){
             this.model.on('change:_titles', function(model, value, options) {
                 this.update_titles(value);
             }, this);
+            var that = this;
             this.model.on('displayed', function() {
                 this.update_titles();
+                // Trigger model displayed events for any models that are child to 
+                // this model when this model is displayed.
+                that.is_displayed = true;
+                for (var property in that.child_views) {
+                    if (that.child_views.hasOwnProperty(property)) {
+                        that.child_views[property].model.trigger('displayed');
+                    }
+                }
             }, this);
         },
 
@@ -125,6 +134,11 @@ define(["widgets/js/widget"], function(WidgetManager){
 
             this.update();
             this.update_titles();
+
+            // Trigger the displayed event if this model is displayed.
+            if (this.is_displayed) {
+                model.trigger('displayed');
+            }
         },
     });
     WidgetManager.register_widget_view('AccordionView', AccordionView);
@@ -153,6 +167,17 @@ define(["widgets/js/widget"], function(WidgetManager){
             this.model.on('change:_children', function(model, value, options) {
                 this.update_children(model.previous('_children'), value);
             }, this);
+
+            // Trigger model displayed events for any models that are child to 
+            // this model when this model is displayed.
+            this.model.on('displayed', function(){
+                that.is_displayed = true;
+                for (var property in that.child_views) {
+                    if (that.child_views.hasOwnProperty(property)) {
+                        that.child_views[property].model.trigger('displayed');
+                    }
+                }
+            });
         },
 
         update_children: function(old_list, new_list) {
@@ -206,6 +231,11 @@ define(["widgets/js/widget"], function(WidgetManager){
                 .append(view.$el)
                 .appendTo(this.$tab_contents);
             view.parent_container = contents_div;
+
+            // Trigger the displayed event if this model is displayed.
+            if (this.is_displayed) {
+                model.trigger('displayed');
+            }
         },
 
         update: function(options) {