##// END OF EJS Templates
Import Tuple traitlet for Selection widget
Import Tuple traitlet for Selection widget

File last commit:

r18984:521c698c
r19060:15154a6c
Show More
widget.js
602 lines | 23.6 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Almost done!...
r17198 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546
Jonathan Frederic
Updated require references to point to new files
r15427 define(["widgets/js/manager",
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 "underscore",
Jason Grout
Load the utils module
r18892 "backbone",
"jquery",
"base/js/utils",
Jonathan Frederic
Fix all the tests
r17216 "base/js/namespace",
Jonathan Frederic
use es6
r18907 ], function(widgetmanager, _, Backbone, $, utils, IPython){
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 var WidgetModel = Backbone.Model.extend({
Jason Grout
More simplifications due to promises
r18888 constructor: function (widget_manager, model_id, comm) {
MinRK
quick review pass on javascript
r14792 // Constructor
Jonathan Frederic
Add constructor comment for widget model.
r14561 //
// Creates a WidgetModel instance.
//
// Parameters
// ----------
// widget_manager : WidgetManager instance
// model_id : string
// An ID unique to this model.
// comm : Comm instance (optional)
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 this.widget_manager = widget_manager;
Jonathan Frederic
use es6
r18907 this.state_change = Promise.resolve();
Jonathan Frederic
Keep a running diff instead of forcing a full state update
r15280 this._buffered_state_diff = {};
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 this.pending_msgs = 0;
this.msg_buffer = null;
sylvain.corlay
optimization of WidgetModel.apply_update
r17839 this.state_lock = null;
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 this.id = model_id;
Sylvain Corlay
Make Widget.views be an object indexed by view id
r18007 this.views = {};
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546
if (comm !== undefined) {
// Remember comm associated with the model.
this.comm = comm;
comm.model = this;
// Hook comm messages up to model.
comm.on_close($.proxy(this._handle_comm_closed, this));
comm.on_msg($.proxy(this._handle_comm_msg, this));
}
return Backbone.Model.apply(this);
},
send: function (content, callbacks) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Send a custom msg over the comm.
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 if (this.comm !== undefined) {
Jonathan Frederic
s/custom_content/content
r14655 var data = {method: 'custom', content: content};
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 this.comm.send(data, callbacks);
Jonathan Frederic
Fixed bug in throttling code.
r14741 this.pending_msgs++;
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 }
},
_handle_comm_closed: function (msg) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Handle when a widget is closed.
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 this.trigger('comm:close');
Sylvain Corlay
stop listening
r17857 this.stopListening();
Sylvain Corlay
simplification of trigger destroy
r17848 this.trigger('destroy', this);
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 delete this.comm.model; // Delete ref so GC will collect widget model.
delete this.comm;
delete this.model_id; // Delete id from model so widget manager cleans up.
Sylvain Corlay
Make Widget.views be an object indexed by view id
r18007 for (var id in this.views) {
sylvain.corlay
hasOwnProperty
r18033 if (this.views.hasOwnProperty(id)) {
this.views[id].remove();
}
Sylvain Corlay
Make Widget.views be an object indexed by view id
r18007 }
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
_handle_comm_msg: function (msg) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Handle incoming comm msg.
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 var method = msg.content.data.method;
Jason Grout
Fix some this vs. that errors, as well as returning some promises to wait for fulfillment
r18893 var that = this;
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 switch (method) {
case 'update':
Jason Grout
More simplifications due to promises
r18888 this.state_change = this.state_change.then(function() {
Jason Grout
Fix some this vs. that errors, as well as returning some promises to wait for fulfillment
r18893 return that.set_state(msg.content.data.state);
Jonathan Frederic
Don't throw Errors with Objects as the message/
r18902 }).catch(utils.reject("Couldn't process update msg for model id '" + String(that.id) + "'", true));
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 break;
case 'custom':
Jonathan Frederic
s/custom_content/content
r14655 this.trigger('msg:custom', msg.content.data.content);
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 break;
Jonathan Frederic
handle_msg a display_model method.
r14559 case 'display':
Jonathan Frederic
Address @takluyver 's review comments
r18935 this.widget_manager.display_view(msg, this);
Jonathan Frederic
handle_msg a display_model method.
r14559 break;
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 }
},
Jonathan Frederic
s/apply_update/set_state
r18072 set_state: function (state) {
Jason Grout
More simplifications due to promises
r18888 var that = this;
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Handle when a widget is updated via the python side.
Jason Grout
Fix some this vs. that errors, as well as returning some promises to wait for fulfillment
r18893 return this._unpack_models(state).then(function(state) {
Jason Grout
More simplifications due to promises
r18888 that.state_lock = state;
try {
Jason Grout
Fix some this vs. that errors, as well as returning some promises to wait for fulfillment
r18893 WidgetModel.__super__.set.call(that, state);
Jason Grout
More simplifications due to promises
r18888 } finally {
that.state_lock = null;
Jonathan Frederic
Add Promise support to models.
r18884 }
Jonathan Frederic
Finished adding error handling.
r18896 }, utils.reject("Couldn't set model state", true));
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
_handle_status: function (msg, callbacks) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Handle status msgs.
// execution_state : ('busy', 'idle', 'starting')
Jonathan Frederic
Fixed *almost* all of the test-detected bugs
r14596 if (this.comm !== undefined) {
if (msg.content.execution_state ==='idle') {
// Send buffer if this message caused another message to be
// throttled.
if (this.msg_buffer !== null &&
Jonathan Frederic
Added parentheses to emphasize order of op.
r15369 (this.get('msg_throttle') || 3) === this.pending_msgs) {
Jonathan Frederic
Fixed *almost* all of the test-detected bugs
r14596 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
MinRK
quick review pass on javascript
r14792 this.comm.send(data, callbacks);
Jonathan Frederic
Fixed *almost* all of the test-detected bugs
r14596 this.msg_buffer = null;
} else {
--this.pending_msgs;
}
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 }
}
},
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640 callbacks: function(view) {
Jason Grout
Rewrite widget syncing...
r14639 // Create msg callbacks for a comm msg.
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640 var callbacks = this.widget_manager.callbacks(view);
if (callbacks.iopub === undefined) {
callbacks.iopub = {};
}
Jason Grout
Rewrite widget syncing...
r14639 var that = this;
callbacks.iopub.status = function (msg) {
that._handle_status(msg, callbacks);
MinRK
quick review pass on javascript
r14792 };
Jason Grout
Rewrite widget syncing...
r14639 return callbacks;
},
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546
Jonathan Frederic
Only send diff message if diff isn't corrupt....
r15279 set: function(key, val, options) {
// Set a value.
Jonathan Frederic
Keep a running diff instead of forcing a full state update
r15280 var return_value = WidgetModel.__super__.set.apply(this, arguments);
// Backbone only remembers the diff of the most recent set()
Jonathan Frederic
Fixed typo
r15281 // operation. Calling set multiple times in a row results in a
Jonathan Frederic
Keep a running diff instead of forcing a full state update
r15280 // loss of diff information. Here we keep our own running diff.
this._buffered_state_diff = $.extend(this._buffered_state_diff, this.changedAttributes() || {});
return return_value;
Jonathan Frederic
Only send diff message if diff isn't corrupt....
r15279 },
Jason Grout
Rewrite widget syncing...
r14639 sync: function (method, model, options) {
Jonathan Frederic
Added some small comments to widget code
r14668 // Handle sync to the back-end. Called when a model.save() is called.
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640
// Make sure a comm exists.
var error = options.error || function() {
console.error('Backbone sync error:', arguments);
MinRK
quick review pass on javascript
r14792 };
Jason Grout
Rewrite widget syncing...
r14639 if (this.comm === undefined) {
error();
return false;
}
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640 // Delete any key value pairs that the back-end already knows about.
Jonathan Frederic
Revert "Fix incorrect usage of attrs"...
r15274 var attrs = (method === 'patch') ? options.attrs : model.toJSON(options);
sylvain.corlay
optimization of WidgetModel.apply_update
r17839 if (this.state_lock !== null) {
var keys = Object.keys(this.state_lock);
Sylvain Corlay
state_lock loop
r17925 for (var i=0; i<keys.length; i++) {
sylvain.corlay
optimization of WidgetModel.apply_update
r17839 var key = keys[i];
if (attrs[key] === this.state_lock[key]) {
delete attrs[key];
}
Jason Grout
Rewrite widget syncing...
r14639 }
}
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640 // Only sync if there are attributes to send to the back-end.
Jonathan Frederic
Only send diff message if diff isn't corrupt....
r15279 attrs = this._pack_models(attrs);
Jonathan Frederic
Fixed context errors and a couple of typos to get the tests working again
r14686 if (_.size(attrs) > 0) {
Jonathan Frederic
Fixed bug in throttling code.
r14741
// If this message was sent via backbone itself, it will not
// have any callbacks. It's important that we create callbacks
// so we can listen for status messages, etc...
var callbacks = options.callbacks || this.callbacks();
// Check throttle.
Jonathan Frederic
Added parentheses to emphasize order of op.
r15369 if (this.pending_msgs >= (this.get('msg_throttle') || 3)) {
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640 // The throttle has been exceeded, buffer the current msg so
// it can be sent once the kernel has finished processing
// some of the existing messages.
// Combine updates if it is a 'patch' sync, otherwise replace updates
switch (method) {
case 'patch':
Jonathan Frederic
Prefer JQuery to Underscore
r14662 this.msg_buffer = $.extend(this.msg_buffer || {}, attrs);
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640 break;
case 'update':
Jonathan Frederic
'create' should be handled in sync
r14661 case 'create':
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640 this.msg_buffer = attrs;
break;
default:
error();
return false;
}
this.msg_buffer_callbacks = callbacks;
} else {
// We haven't exceeded the throttle, send the message like
Jonathan Frederic
Fixed bug in throttling code.
r14741 // normal.
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640 var data = {method: 'backbone', sync_data: attrs};
this.comm.send(data, callbacks);
this.pending_msgs++;
}
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 }
// Since the comm is a one-way communication, assume the message
Jason Grout
Rewrite widget syncing...
r14639 // arrived. Don't call success since we don't have a model back from the server
// this means we miss out on the 'sync' event.
Jonathan Frederic
Keep a running diff instead of forcing a full state update
r15280 this._buffered_state_diff = {};
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Jason Grout
Rewrite widget syncing...
r14639 save_changes: function(callbacks) {
Jonathan Frederic
Remove residual tabs
r14651 // Push this model's state to the back-end
//
// This invokes a Backbone.Sync.
Jonathan Frederic
Keep a running diff instead of forcing a full state update
r15280 this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks});
Jason Grout
make the saving to python a method of the model, called with callbacks...
r14618 },
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 _pack_models: function(value) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Replace models with model ids recursively.
Jonathan Frederic
Done with major changes,...
r17199 var that = this;
var packed;
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 if (value instanceof Backbone.Model) {
Jason Grout
Prepend a sentinel value to model ids to distinguish them from normal UUIDs (from Sylvain Corlay).
r17262 return "IPY_MODEL_" + value.id;
Jonathan Frederic
Added ability to pack and unpack arrays.
r15063
} else if ($.isArray(value)) {
Jonathan Frederic
Done with major changes,...
r17199 packed = [];
Jonathan Frederic
Added ability to pack and unpack arrays.
r15063 _.each(value, function(sub_value, key) {
packed.push(that._pack_models(sub_value));
});
return packed;
Sylvain Corlay
A String is an object
r18821 } else if (value instanceof Date || value instanceof String) {
Sylvain Corlay
Dates are JavaScript objects, and _pack_models returns an empty object
r18820 return value;
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 } else if (value instanceof Object) {
Jonathan Frederic
Done with major changes,...
r17199 packed = {};
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 _.each(value, function(sub_value, key) {
Jonathan Frederic
Fixed context errors and a couple of typos to get the tests working again
r14686 packed[key] = that._pack_models(sub_value);
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 });
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 return packed;
Jonathan Frederic
Added ability to pack and unpack arrays.
r15063
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 } else {
return value;
}
},
_unpack_models: function(value) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Replace model ids with models recursively.
Jonathan Frederic
Almost done!...
r17198 var that = this;
var unpacked;
Jason Grout
More simplifications due to promises
r18888 if ($.isArray(value)) {
unpacked = [];
_.each(value, function(sub_value, key) {
unpacked.push(that._unpack_models(sub_value));
});
Jonathan Frederic
use es6
r18907 return Promise.all(unpacked);
Jason Grout
More simplifications due to promises
r18888 } else if (value instanceof Object) {
Jonathan Frederic
Finished adding error handling.
r18896 unpacked = {};
Jason Grout
More simplifications due to promises
r18888 _.each(value, function(sub_value, key) {
Jonathan Frederic
Finished adding error handling.
r18896 unpacked[key] = that._unpack_models(sub_value);
Jason Grout
More simplifications due to promises
r18888 });
Jonathan Frederic
Address @takluyver 's review comments
r18935 return utils.resolve_promises_dict(unpacked);
Jason Grout
More simplifications due to promises
r18888 } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") {
// get_model returns a promise already
return this.widget_manager.get_model(value.slice(10, value.length));
} else {
Jonathan Frederic
use es6
r18907 return Promise.resolve(value);
Jason Grout
More simplifications due to promises
r18888 }
Jonathan Frederic
Many checks off the todo list, test fixes
r14583 },
Sylvain Corlay
on_atomic_change -> on_some_change
r18008 on_some_change: function(keys, callback, context) {
// on_some_change(["key1", "key2"], foo, context) differs from
Sylvain Corlay
Adding a comment and renaming on_bulk_change to on_change
r17849 // on("change:key1 change:key2", foo, context).
// If the widget attributes key1 and key2 are both modified,
// the second form will result in foo being called twice
// while the first will call foo only once.
sylvain.corlay
Added on_bulk_change method in WidgetModel
r17838 this.on('change', function() {
if (keys.some(this.hasChanged, this)) {
callback.apply(context);
}
}, this);
},
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 });
Jonathan Frederic
Fix imports of "modules",...
r17202 widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel);
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546
Jonathan Frederic
s/BaseWidgetView/WidgetView and s/WidgetView/DOMWidgetView
r14564 var WidgetView = Backbone.View.extend({
Jonathan Frederic
un-nest options.options
r14565 initialize: function(parameters) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Public constructor.
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 this.model.on('change',this.update,this);
Jonathan Frederic
un-nest options.options
r14565 this.options = parameters.options;
Jonathan Frederic
Allow a widget to be displayed more than once within a parent widget.
r17172 this.child_model_views = {};
this.child_views = {};
Jason Grout
Load the utils module
r18892 this.id = this.id || utils.uuid();
Sylvain Corlay
Make Widget.views be an object indexed by view id
r18007 this.model.views[this.id] = this;
Sylvain Corlay
once-displayed
r17270 this.on('displayed', function() {
this.is_displayed = true;
}, this);
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
update: function(){
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Triggered on model change.
//
// Update view to be consistent with this.model
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Jonathan Frederic
Got containers and mutlicontainers working! Yay
r14598 create_child_view: function(child_model, options) {
Jason Grout
More simplifications due to promises
r18888 // Create and promise that resolves to a child view of a given model
Jonathan Frederic
Add Promise support to models.
r18884 var that = this;
Jason Grout
More simplifications due to promises
r18888 options = $.extend({ parent: this }, options || {});
return this.model.widget_manager.create_view(child_model, options).then(function(child_view) {
// Associate the view id with the model id.
if (that.child_model_views[child_model.id] === undefined) {
that.child_model_views[child_model.id] = [];
}
that.child_model_views[child_model.id].push(child_view.id);
// Remember the view by id.
that.child_views[child_view.id] = child_view;
return child_view;
Jonathan Frederic
Add a WrappedError class
r18895 }, utils.reject("Couldn't create child view"));
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Jonathan Frederic
Got containers and mutlicontainers working! Yay
r14598
Jonathan Frederic
Review comments
r17178 pop_child_view: function(child_model) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Delete a child view that was previously created using create_child_view.
Jonathan Frederic
Allow a widget to be displayed more than once within a parent widget.
r17172 var view_ids = this.child_model_views[child_model.id];
if (view_ids !== undefined) {
Jonathan Frederic
Fixed buggy behavior
r17173 // Only delete the first view in the list.
var view_id = view_ids[0];
var view = this.child_views[view_id];
delete this.child_views[view_id];
Jonathan Frederic
Bug fixes
r17175 view_ids.splice(0,1);
Sylvain Corlay
Make Widget.views be an object indexed by view id
r18007 delete child_model.views[view_id];
Jonathan Frederic
Fixed buggy behavior
r17173
// Remove the view list specific to this model if it is empty.
if (view_ids.length === 0) {
delete this.child_model_views[child_model.id];
Jonathan Frederic
Allow a widget to be displayed more than once within a parent widget.
r17172 }
Jonathan Frederic
Fixed buggy behavior
r17173 return view;
Jonathan Frederic
- remove second line in create_child_view...
r14667 }
Jonathan Frederic
Fixed buggy behavior
r17173 return null;
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Jonathan Frederic
Removed respect_order and finally removed the children dict of the containerview
r17176 do_diff: function(old_list, new_list, removed_callback, added_callback) {
Jonathan Frederic
Add a helper method that acts on the changes made to a list.
r14580 // Difference a changed list and call remove and add callbacks for
// each removed and added item in the new list.
//
// Parameters
// ----------
// old_list : array
// new_list : array
// removed_callback : Callback(item)
// Callback that is called for each item removed.
// added_callback : Callback(item)
// Callback that is called for each item added.
Jonathan Frederic
Removed respect_order and finally removed the children dict of the containerview
r17176 // Walk the lists until an unequal entry is found.
var i;
for (i = 0; i < new_list.length; i++) {
Sylvain Corlay
correction in widget.js
r17289 if (i >= old_list.length || new_list[i] !== old_list[i]) {
Jonathan Frederic
Removed respect_order and finally removed the children dict of the containerview
r17176 break;
Jonathan Frederic
Add order respecting method
r17174 }
Jonathan Frederic
Removed respect_order and finally removed the children dict of the containerview
r17176 }
Jonathan Frederic
Add a helper method that acts on the changes made to a list.
r14580
Jonathan Frederic
Removed respect_order and finally removed the children dict of the containerview
r17176 // Remove the non-matching items from the old list.
for (var j = i; j < old_list.length; j++) {
removed_callback(old_list[j]);
Jonathan Frederic
Add order respecting method
r17174 }
Jonathan Frederic
Add a helper method that acts on the changes made to a list.
r14580
Jonathan Frederic
Removed respect_order and finally removed the children dict of the containerview
r17176 // Add the rest of the new list items.
Sylvain Corlay
one more bit
r17327 for (; i < new_list.length; i++) {
Jonathan Frederic
Removed respect_order and finally removed the children dict of the containerview
r17176 added_callback(new_list[i]);
Jonathan Frederic
Allow a widget to be displayed more than once within a parent widget.
r17172 }
},
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 callbacks: function(){
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Create msg callbacks for a comm msg.
Jonathan Frederic
Change the callback logic so the order makes more sense:...
r14640 return this.model.callbacks(this);
Jonathan Frederic
Added missing comma
r14560 },
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546
render: function(){
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Render the view.
//
// By default, this is only called the first time the view is created
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609
Sylvain Corlay
Solving #5997: instances of WidgetView (that are not instances of DOMWidgetView) do not trigger widget_area.show anymore.
r17227 show: function(){
// Show the widget-area
if (this.options && this.options.cell &&
this.options.cell.widget_area !== undefined) {
this.options.cell.widget_area.show();
}
},
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 send: function (content) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Send a custom msg associated with this view.
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 this.model.send(content, this.callbacks());
},
touch: function () {
Jason Grout
Rewrite widget syncing...
r14639 this.model.save_changes(this.callbacks());
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Sylvain Corlay
once-displayed
r17270
Sylvain Corlay
renaming once_displayed into after_displayed
r17329 after_displayed: function (callback, context) {
Sylvain Corlay
once-displayed
r17270 // Calls the callback right away is the view is already displayed
// otherwise, register the callback to the 'displayed' event.
if (this.is_displayed) {
callback.apply(context);
} else {
this.on('displayed', callback, context);
}
},
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 });
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609
Jonathan Frederic
s/BaseWidgetView/WidgetView and s/WidgetView/DOMWidgetView
r14564 var DOMWidgetView = WidgetView.extend({
Sylvain Corlay
making the use of options explicit
r17448 initialize: function (parameters) {
Jonathan Frederic
Added PEP8 style comments to all of the JS code.
r14609 // Public constructor
Sylvain Corlay
making the use of options explicit
r17448 DOMWidgetView.__super__.initialize.apply(this, [parameters]);
Sylvain Corlay
Solving #5997: instances of WidgetView (that are not instances of DOMWidgetView) do not trigger widget_area.show anymore.
r17227 this.on('displayed', this.show, this);
sylvain.corlay
Partial updates of css and visible + simplification of widget_container
r17347 this.model.on('change:visible', this.update_visible, this);
this.model.on('change:_css', this.update_css, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Remove add/remove class methods,...
r17720 this.model.on('change:_dom_classes', function(model, new_classes) {
Jonathan Frederic
Fixed rebase bugs and other bugs.
r17731 var old_classes = model.previous('_dom_classes');
Jonathan Frederic
Remove add/remove class methods,...
r17720 this.update_classes(old_classes, new_classes);
}, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
s/fore_color/color...
r17860 this.model.on('change:color', function (model, value) {
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.update_attr('color', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
s/fore_color/color...
r17860 this.model.on('change:background_color', function (model, value) {
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.update_attr('background', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.model.on('change:width', function (model, value) {
this.update_attr('width', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.model.on('change:height', function (model, value) {
this.update_attr('height', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.model.on('change:border_color', function (model, value) {
this.update_attr('border-color', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.model.on('change:border_width', function (model, value) {
this.update_attr('border-width', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.model.on('change:border_style', function (model, value) {
this.update_attr('border-style', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.model.on('change:font_style', function (model, value) {
this.update_attr('font-style', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.model.on('change:font_weight', function (model, value) {
this.update_attr('font-weight', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.model.on('change:font_size', function (model, value) {
Jonathan Frederic
Address problems found in in-person review
r17947 this.update_attr('font-size', this._default_px(value)); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 this.model.on('change:font_family', function (model, value) {
this.update_attr('font-family', value); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
this.model.on('change:padding', function (model, value) {
this.update_attr('padding', value); }, this);
this.model.on('change:margin', function (model, value) {
Jonathan Frederic
Address problems found in in-person review
r17947 this.update_attr('margin', this._default_px(value)); }, this);
this.model.on('change:border_radius', function (model, value) {
this.update_attr('border-radius', this._default_px(value)); }, this);
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727
Sylvain Corlay
removing timout
r17386 this.after_displayed(function() {
this.update_visible(this.model, this.model.get("visible"));
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727 this.update_classes([], this.model.get('_dom_classes'));
Jonathan Frederic
Make sure DOMWidget _css is set after basic style traits
r18177
Jonathan Frederic
s/fore_color/color...
r17860 this.update_attr('color', this.model.get('color'));
this.update_attr('background', this.model.get('background_color'));
Jonathan Frederic
Fix some bugs found by the widget examples,...
r17727 this.update_attr('width', this.model.get('width'));
this.update_attr('height', this.model.get('height'));
this.update_attr('border-color', this.model.get('border_color'));
this.update_attr('border-width', this.model.get('border_width'));
this.update_attr('border-style', this.model.get('border_style'));
this.update_attr('font-style', this.model.get('font_style'));
this.update_attr('font-weight', this.model.get('font_weight'));
this.update_attr('font-size', this.model.get('font_size'));
this.update_attr('font-family', this.model.get('font_family'));
this.update_attr('padding', this.model.get('padding'));
this.update_attr('margin', this.model.get('margin'));
Jonathan Frederic
Address problems found in in-person review
r17947 this.update_attr('border-radius', this.model.get('border_radius'));
Jonathan Frederic
Make sure DOMWidget _css is set after basic style traits
r18177
this.update_css(this.model, this.model.get("_css"));
Sylvain Corlay
removing timout
r17386 }, this);
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Sylvain Corlay
removing timout
r17386
Jonathan Frederic
Address problems found in in-person review
r17947 _default_px: function(value) {
// Makes browser interpret a numerical string as a pixel value.
if (/^\d+\.?(\d+)?$/.test(value.trim())) {
return value.trim() + 'px';
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 }
Jonathan Frederic
Address problems found in in-person review
r17947 return value;
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Jonathan Frederic
Initial crack at using specific traits for styling.
r17722 update_attr: function(name, value) {
// Set a css attr of the widget view.
this.$el.css(name, value);
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Sylvain Corlay
removing timout
r17386
sylvain.corlay
Partial updates of css and visible + simplification of widget_container
r17347 update_visible: function(model, value) {
// Update visibility
Sylvain Corlay
removing timout
r17386 this.$el.toggle(value);
sylvain.corlay
Partial updates of css and visible + simplification of widget_container
r17347 },
update_css: function (model, css) {
sylvain.corlay
widget changes continued
r17353 // Update the css styling of this view.
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 var e = this.$el;
if (css === undefined) {return;}
Jonathan Frederic
Reverted strange change that I don't remember making to update method.
r17177 for (var i = 0; i < css.length; i++) {
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 // Apply the css traits to all elements that match the selector.
Jonathan Frederic
Reverted strange change that I don't remember making to update method.
r17177 var selector = css[i][0];
var elements = this._get_selector_element(selector);
Jonathan Frederic
Removed for () loops where necessary. Replaced with _.each
r14664 if (elements.length > 0) {
Jonathan Frederic
Reverted strange change that I don't remember making to update method.
r17177 var trait_key = css[i][1];
var trait_value = css[i][2];
elements.css(trait_key ,trait_value);
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 }
Jonathan Frederic
Reverted strange change that I don't remember making to update method.
r17177 }
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 },
Jonathan Frederic
Added Bootstrap specific classes,...
r17728 update_classes: function (old_classes, new_classes, $el) {
// Update the DOM classes applied to an element, default to this.$el.
if ($el===undefined) {
$el = this.$el;
}
Jonathan Frederic
Remove add/remove class methods,...
r17720 this.do_diff(old_classes, new_classes, function(removed) {
Jonathan Frederic
Added Bootstrap specific classes,...
r17728 $el.removeClass(removed);
Jonathan Frederic
Remove add/remove class methods,...
r17720 }, function(added) {
Jonathan Frederic
Added Bootstrap specific classes,...
r17728 $el.addClass(added);
Jonathan Frederic
Remove add/remove class methods,...
r17720 });
},
Jonathan Frederic
Added Bootstrap specific classes,...
r17728 update_mapped_classes: function(class_map, trait_name, previous_trait_value, $el) {
// Update the DOM classes applied to the widget based on a single
// trait's value.
//
// Given a trait value classes map, this function automatically
// handles applying the appropriate classes to the widget element
// and removing classes that are no longer valid.
//
// Parameters
// ----------
// class_map: dictionary
// Dictionary of trait values to class lists.
// Example:
// {
// success: ['alert', 'alert-success'],
// info: ['alert', 'alert-info'],
// warning: ['alert', 'alert-warning'],
// danger: ['alert', 'alert-danger']
// };
// trait_name: string
// Name of the trait to check the value of.
// previous_trait_value: optional string, default ''
// Last trait value
// $el: optional jQuery element handle, defaults to this.$el
// Element that the classes are applied to.
var key = previous_trait_value;
if (key === undefined) {
key = this.model.previous(trait_name);
}
var old_classes = class_map[key] ? class_map[key] : [];
key = this.model.get(trait_name);
var new_classes = class_map[key] ? class_map[key] : [];
this.update_classes(old_classes, new_classes, $el || this.$el);
},
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 _get_selector_element: function (selector) {
MinRK
quick review pass on javascript
r14792 // Get the elements via the css selector.
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 var elements;
MinRK
quick review pass on javascript
r14792 if (!selector) {
Jason Grout
Remove $el_to_style from the widget javascript code; '' now defaults to this.$el...
r17420 elements = this.$el;
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 } else {
Jason Grout
Remove $el_to_style from the widget javascript code; '' now defaults to this.$el...
r17420 elements = this.$el.find(selector).addBack(selector);
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 }
return elements;
},
});
Sylvain Corlay
register bare views
r17950
Jonathan Frederic
Fix all the tests
r17216 var widget = {
Jonathan Frederic
Almost done!...
r17198 'WidgetModel': WidgetModel,
'WidgetView': WidgetView,
'DOMWidgetView': DOMWidgetView,
};
Jonathan Frederic
Fix all the tests
r17216
// For backwards compatability.
$.extend(IPython, widget);
return widget;
Jonathan Frederic
renamed: basic_widgets.js -> init.js...
r14546 });