mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-12 11:45:38 +08:00
jshint widget.js
This commit is contained in:
parent
d398ab6720
commit
3bc9d12eb8
@ -15,6 +15,7 @@
|
||||
* @submodule widget
|
||||
*/
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
// Use require.js 'define' method so that require.js is intelligent enough to
|
||||
@ -69,8 +70,8 @@ define(["components/underscore/underscore-min",
|
||||
// will insure that any python code triggered by the sent message
|
||||
// can create and display widgets and output.
|
||||
if (cell === undefined) {
|
||||
if (this.last_modified_view != undefined &&
|
||||
this.last_modified_view.cell != undefined) {
|
||||
if (this.last_modified_view !== undefined &&
|
||||
this.last_modified_view.cell !== undefined) {
|
||||
cell = this.last_modified_view.cell;
|
||||
}
|
||||
}
|
||||
@ -104,7 +105,7 @@ define(["components/underscore/underscore-min",
|
||||
this._custom_msg_callbacks.splice(found_index, 1);
|
||||
}
|
||||
} else {
|
||||
this._custom_msg_callbacks.push(callback)
|
||||
this._custom_msg_callbacks.push(callback);
|
||||
}
|
||||
},
|
||||
|
||||
@ -135,9 +136,9 @@ define(["components/underscore/underscore-min",
|
||||
|
||||
// Try to get the cell.
|
||||
var cell = this._get_msg_cell(msg.parent_header.msg_id);
|
||||
if (cell == null) {
|
||||
if (cell === null) {
|
||||
console.log("Could not determine where the display" +
|
||||
" message was from. Widget will not be displayed")
|
||||
" message was from. Widget will not be displayed");
|
||||
} else {
|
||||
this._display_view(msg.content.data.view_name,
|
||||
msg.content.data.parent,
|
||||
@ -198,10 +199,9 @@ define(["components/underscore/underscore-min",
|
||||
|
||||
// Send buffer if this message caused another message to be
|
||||
// throttled.
|
||||
if (this.msg_buffer != null &&
|
||||
if (this.msg_buffer !== null &&
|
||||
this.msg_throttle == this.pending_msgs) {
|
||||
|
||||
var cell = this._get_msg_cell(msg.parent_header.msg_id);
|
||||
var callbacks = this._make_callbacks(cell);
|
||||
var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
|
||||
this.comm.send(data, callbacks);
|
||||
@ -219,6 +219,7 @@ define(["components/underscore/underscore-min",
|
||||
// Custom syncronization logic.
|
||||
_handle_sync: function (method, options) {
|
||||
var model_json = this.toJSON();
|
||||
var attr;
|
||||
|
||||
// Only send updated state if the state hasn't been changed
|
||||
// during an update.
|
||||
@ -228,10 +229,10 @@ define(["components/underscore/underscore-min",
|
||||
// it can be sent once the kernel has finished processing
|
||||
// some of the existing messages.
|
||||
if (method=='patch') {
|
||||
if (this.msg_buffer == null) {
|
||||
if (this.msg_buffer === null) {
|
||||
this.msg_buffer = $.extend({}, model_json); // Copy
|
||||
}
|
||||
for (var attr in options.attrs) {
|
||||
for (attr in options.attrs) {
|
||||
this.msg_buffer[attr] = options.attrs[attr];
|
||||
}
|
||||
} else {
|
||||
@ -245,7 +246,7 @@ define(["components/underscore/underscore-min",
|
||||
var send_json = model_json;
|
||||
if (method=='patch') {
|
||||
send_json = {};
|
||||
for (var attr in options.attrs) {
|
||||
for (attr in options.attrs) {
|
||||
send_json[attr] = options.attrs[attr];
|
||||
}
|
||||
}
|
||||
@ -253,7 +254,7 @@ define(["components/underscore/underscore-min",
|
||||
var data = {method: 'backbone', sync_method: method, sync_data: send_json};
|
||||
|
||||
var cell = null;
|
||||
if (this.last_modified_view != undefined && this.last_modified_view != null) {
|
||||
if (this.last_modified_view !== undefined && this.last_modified_view !== null) {
|
||||
cell = this.last_modified_view.cell;
|
||||
}
|
||||
|
||||
@ -272,7 +273,7 @@ define(["components/underscore/underscore-min",
|
||||
_handle_view_displayed: function(view) {
|
||||
if (this._view_displayed_callback) {
|
||||
try {
|
||||
this._view_displayed_callback(view)
|
||||
this._view_displayed_callback(view);
|
||||
} catch (e) {
|
||||
console.log("Exception in widget model view displayed callback", e, view, this);
|
||||
}
|
||||
@ -302,19 +303,20 @@ define(["components/underscore/underscore-min",
|
||||
// Create view that represents the model.
|
||||
_display_view: function (view_name, parent_comm_id, cell) {
|
||||
var new_views = [];
|
||||
var view;
|
||||
|
||||
// Try creating and adding the view to it's parent.
|
||||
var displayed = false;
|
||||
if (parent_comm_id != undefined) {
|
||||
if (parent_comm_id !== undefined) {
|
||||
var parent_comm = this.comm_manager.comms[parent_comm_id];
|
||||
var parent_model = parent_comm.model;
|
||||
var parent_views = parent_model.views;
|
||||
for (var parent_view_index in parent_views) {
|
||||
var parent_view = parent_views[parent_view_index];
|
||||
if (parent_view.cell === cell) {
|
||||
if (parent_view.display_child != undefined) {
|
||||
var view = this._create_view(view_name, cell);
|
||||
if (view != null) {
|
||||
if (parent_view.display_child !== undefined) {
|
||||
view = this._create_view(view_name, cell);
|
||||
if (view !== null) {
|
||||
new_views.push(view);
|
||||
parent_view.display_child(view);
|
||||
displayed = true;
|
||||
@ -328,11 +330,11 @@ define(["components/underscore/underscore-min",
|
||||
// If no parent view is defined or exists. Add the view's
|
||||
// element to cell's widget div.
|
||||
if (!displayed) {
|
||||
var view = this._create_view(view_name, cell);
|
||||
if (view != null) {
|
||||
view = this._create_view(view_name, cell);
|
||||
if (view !== null) {
|
||||
new_views.push(view);
|
||||
|
||||
if (cell.widget_subarea != undefined && cell.widget_subarea != null) {
|
||||
if (cell.widget_subarea !== undefined && cell.widget_subarea !== null) {
|
||||
cell.widget_area.show();
|
||||
cell.widget_subarea.append(view.$el);
|
||||
this._handle_view_displayed(view);
|
||||
@ -342,7 +344,7 @@ define(["components/underscore/underscore-min",
|
||||
|
||||
// Force the new view(s) to update their selves
|
||||
for (var view_index in new_views) {
|
||||
var view = new_views[view_index];
|
||||
view = new_views[view_index];
|
||||
view.update();
|
||||
}
|
||||
},
|
||||
@ -350,9 +352,9 @@ define(["components/underscore/underscore-min",
|
||||
|
||||
// Create a view
|
||||
_create_view: function (view_name, cell) {
|
||||
var view_type = this.widget_manager.widget_view_types[view_name];
|
||||
if (view_type != undefined && view_type != null) {
|
||||
var view = new view_type({model: this});
|
||||
var ViewType = this.widget_manager.widget_view_types[view_name];
|
||||
if (ViewType !== undefined && ViewType !== null) {
|
||||
var view = new ViewType({model: this});
|
||||
view.render();
|
||||
this.views.push(view);
|
||||
view.cell = cell;
|
||||
@ -367,10 +369,10 @@ define(["components/underscore/underscore-min",
|
||||
view.remove(); // Clean-up view
|
||||
|
||||
// Close the comm if there are no views left.
|
||||
if (that.views.length()==0) {
|
||||
if (that.views.length() === 0) {
|
||||
if (that._close_callback) {
|
||||
try {
|
||||
that._close_callback(that)
|
||||
that._close_callback(that);
|
||||
} catch (e) {
|
||||
console.log("Exception in widget model close callback", e, that);
|
||||
}
|
||||
@ -388,12 +390,12 @@ define(["components/underscore/underscore-min",
|
||||
// Build a callback dict.
|
||||
_make_callbacks: function (cell) {
|
||||
var callbacks = {};
|
||||
if (cell != null) {
|
||||
if (cell !== null) {
|
||||
|
||||
// Try to get output handlers
|
||||
var handle_output = null;
|
||||
var handle_clear_output = null;
|
||||
if (cell.output_area != undefined && cell.output_area != null) {
|
||||
if (cell.output_area !== undefined && cell.output_area !== null) {
|
||||
handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
|
||||
handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
|
||||
}
|
||||
@ -428,7 +430,7 @@ define(["components/underscore/underscore-min",
|
||||
|
||||
// First, check to see if the msg was triggered by cell execution.
|
||||
var cell = this.widget_manager.get_msg_cell(msg_id);
|
||||
if (cell != null) {
|
||||
if (cell !== null) {
|
||||
return cell;
|
||||
}
|
||||
|
||||
@ -438,9 +440,9 @@ define(["components/underscore/underscore-min",
|
||||
// message was triggered by a widget.
|
||||
var kernel = this.comm_manager.kernel;
|
||||
var callbacks = kernel.get_callbacks_for_msg(msg_id);
|
||||
if (callbacks != undefined &&
|
||||
callbacks.iopub != undefined &&
|
||||
callbacks.iopub.get_cell != undefined) {
|
||||
if (callbacks !== undefined &&
|
||||
callbacks.iopub !== undefined &&
|
||||
callbacks.iopub.get_cell !== undefined) {
|
||||
|
||||
return callbacks.iopub.get_cell();
|
||||
}
|
||||
@ -483,7 +485,7 @@ define(["components/underscore/underscore-min",
|
||||
},
|
||||
|
||||
update: function() {
|
||||
if (this.model.get('visible') != undefined) {
|
||||
if (this.model.get('visible') !== undefined) {
|
||||
if (this.visible != this.model.get('visible')) {
|
||||
this.visible = this.model.get('visible');
|
||||
if (this.visible) {
|
||||
@ -494,7 +496,7 @@ define(["components/underscore/underscore-min",
|
||||
}
|
||||
}
|
||||
|
||||
if (this.model.css != undefined) {
|
||||
if (this.model.css !== undefined) {
|
||||
for (var selector in this.model.css) {
|
||||
if (this.model.css.hasOwnProperty(selector)) {
|
||||
|
||||
@ -519,8 +521,8 @@ define(["components/underscore/underscore-min",
|
||||
// the $el_to_style element is not defined, use apply the
|
||||
// style to the view's element.
|
||||
var elements = this.$el.find(selector);
|
||||
if (selector===undefined || selector===null || selector=='') {
|
||||
if (this.$el_to_style == undefined) {
|
||||
if (selector === undefined || selector === null || selector === '') {
|
||||
if (this.$el_to_style === undefined) {
|
||||
elements = this.$el;
|
||||
} else {
|
||||
elements = this.$el_to_style;
|
||||
@ -546,7 +548,7 @@ define(["components/underscore/underscore-min",
|
||||
options.success(result);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
|
||||
@ -556,34 +558,34 @@ define(["components/underscore/underscore-min",
|
||||
for (var widget_model_name in this.widget_model_types) {
|
||||
this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
|
||||
// Register the widget with the comm manager. Make sure to pass this object's context
|
||||
// in so `this` works in the call back.
|
||||
if (this.comm_manager!=null) {
|
||||
if (this.comm_manager !== null) {
|
||||
this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this));
|
||||
}
|
||||
this.widget_model_types[widget_model_name] = widget_model_type;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
|
||||
this.widget_view_types[widget_view_name] = widget_view_type;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
WidgetManager.prototype.get_msg_cell = function (msg_id) {
|
||||
if (IPython.notebook != undefined && IPython.notebook != null) {
|
||||
if (IPython.notebook !== undefined && IPython.notebook !== null) {
|
||||
return IPython.notebook.get_msg_cell(msg_id);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
WidgetManager.prototype.on_create_widget = function (callback) {
|
||||
this._create_widget_callback = callback;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
WidgetManager.prototype._handle_create_widget = function (widget_model) {
|
||||
@ -594,14 +596,14 @@ define(["components/underscore/underscore-min",
|
||||
console.log("Exception in WidgetManager callback", e, widget_model);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
WidgetManager.prototype._handle_com_open = function (comm, msg) {
|
||||
var widget_type_name = msg.content.target_name;
|
||||
var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, this);
|
||||
this._handle_create_widget(widget_model);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
@ -611,9 +613,10 @@ define(["components/underscore/underscore-min",
|
||||
IPython.WidgetModel = WidgetModel;
|
||||
IPython.WidgetView = WidgetView;
|
||||
|
||||
if (IPython.widget_manager==undefined || IPython.widget_manager==null) {
|
||||
if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
|
||||
IPython.widget_manager = new WidgetManager();
|
||||
}
|
||||
|
||||
return IPython.widget_manager;
|
||||
});
|
||||
}());
|
Loading…
Reference in New Issue
Block a user