##// END OF EJS Templates
Bubble event and implement logic in CodeCell
Jonathan Frederic -
Show More
@@ -117,6 +117,7 b' define(['
117 this.last_msg_id = null;
117 this.last_msg_id = null;
118 this.completer = null;
118 this.completer = null;
119 this.widget_views = [];
119 this.widget_views = [];
120 this._widgets_live = true;
120
121
121 Cell.apply(this,[{
122 Cell.apply(this,[{
122 config: $.extend({}, CodeCell.options_default),
123 config: $.extend({}, CodeCell.options_default),
@@ -224,7 +225,12 b' define(['
224 .click(function() {
225 .click(function() {
225 widget_area.slideUp('', function(){
226 widget_area.slideUp('', function(){
226 for (var i = 0; i < that.widget_views.length; i++) {
227 for (var i = 0; i < that.widget_views.length; i++) {
227 that.widget_views[i].remove();
228 var view = that.widget_views[i];
229 view.remove();
230
231 // Remove widget live events.
232 view.off('comm:live', that._widget_live);
233 view.off('comm:dead', that._widget_dead);
228 }
234 }
229 that.widget_views = [];
235 that.widget_views = [];
230 widget_subarea.html('');
236 widget_subarea.html('');
@@ -258,10 +264,49 b' define(['
258 that.widget_area.show();
264 that.widget_area.show();
259 dummy.replaceWith(view.$el);
265 dummy.replaceWith(view.$el);
260 that.widget_views.push(view);
266 that.widget_views.push(view);
267
268 // Check the live state of the view's model.
269 if (view.model.comm_live) {
270 that._widget_live(view);
271 } else {
272 that._widget_dead(view);
273 }
274
275 // Listen to comm live events for the view.
276 view.on('comm:live', that._widget_live, that);
277 view.on('comm:dead', that._widget_dead, that);
261 return view;
278 return view;
262 });
279 });
263 };
280 };
264
281
282 /**
283 * Handles when a widget loses it's comm connection.
284 * @param {WidgetView} view
285 */
286 CodeCell.prototype._widget_dead = function(view) {
287 if (this._widgets_live) {
288 this._widgets_live = false;
289 this.widget_area.addClass('connection-problems');
290 }
291
292 };
293
294 /**
295 * Handles when a widget is connected to a live comm.
296 * @param {WidgetView} view
297 */
298 CodeCell.prototype._widget_live = function(view) {
299 if (!this._widgets_live) {
300 // Check that the other widgets are live too. O(N) operation.
301 // Abort the function at the first dead widget found.
302 for (var i = 0; i < this.widget_views.length; i++) {
303 if (!this.widget_views[i].model.comm_live) return;
304 }
305 this._widgets_live = true;
306 this.widget_area.removeClass('connection-problems');
307 }
308 };
309
265 /** @method bind_events */
310 /** @method bind_events */
266 CodeCell.prototype.bind_events = function () {
311 CodeCell.prototype.bind_events = function () {
267 Cell.prototype.bind_events.apply(this);
312 Cell.prototype.bind_events.apply(this);
@@ -375,7 +420,12 b' define(['
375
420
376 // Clear widget area
421 // Clear widget area
377 for (var i = 0; i < this.widget_views.length; i++) {
422 for (var i = 0; i < this.widget_views.length; i++) {
378 this.widget_views[i].remove();
423 var view = this.widget_views[i];
424 view.remove();
425
426 // Remove widget live events.
427 view.off('comm:live', this._widget_live);
428 view.off('comm:dead', this._widget_dead);
379 }
429 }
380 this.widget_views = [];
430 this.widget_views = [];
381 this.widget_subarea.html('');
431 this.widget_subarea.html('');
@@ -395,11 +395,13 b' define(["widgets/js/manager",'
395 * Public constructor.
395 * Public constructor.
396 */
396 */
397 this.model.on('change',this.update,this);
397 this.model.on('change',this.update,this);
398
399 // Bubble the comm live events.
398 this.model.on('comm:live', function() {
400 this.model.on('comm:live', function() {
399 this.$el.removeClass('comm-dead');
401 this.trigger('comm:live', this);
400 }, this);
402 }, this);
401 this.model.on('comm:dead', function() {
403 this.model.on('comm:dead', function() {
402 this.$el.addClass('comm-dead');
404 this.trigger('comm:dead', this);
403 }, this);
405 }, this);
404
406
405 this.options = parameters.options;
407 this.options = parameters.options;
@@ -26,6 +26,18 b''
26 .box-flex2();
26 .box-flex2();
27 .align-start();
27 .align-start();
28 }
28 }
29
30 &.connection-problems .prompt:after {
31 content: '\f127';
32 font-family: 'FontAwesome';
33 color: @brand-danger;
34 padding: 4px;
35 position: relative;
36 border-radius: 2px;
37 font-size: 14px;
38 left: 0px;
39 top: 3px;
40 }
29 }
41 }
30
42
31 /* THE CLASSES BELOW CAN APPEAR ANYWHERE IN THE DOM (POSSIBLEY OUTSIDE OF
43 /* THE CLASSES BELOW CAN APPEAR ANYWHERE IN THE DOM (POSSIBLEY OUTSIDE OF
General Comments 0
You need to be logged in to leave comments. Login now