Show More
@@ -1,189 +1,197 | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // WidgetModel, WidgetView, and WidgetManager |
|
9 | // WidgetModel, WidgetView, and WidgetManager | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 | /** |
|
11 | /** | |
12 | * Base Widget classes |
|
12 | * Base Widget classes | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule widget |
|
15 | * @submodule widget | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 | (function () { |
|
18 | (function () { | |
19 | "use strict"; |
|
19 | "use strict"; | |
20 |
|
20 | |||
21 | // Use require.js 'define' method so that require.js is intelligent enough to |
|
21 | // Use require.js 'define' method so that require.js is intelligent enough to | |
22 | // syncronously load everything within this file when it is being 'required' |
|
22 | // syncronously load everything within this file when it is being 'required' | |
23 | // elsewhere. |
|
23 | // elsewhere. | |
24 | define(["underscore", |
|
24 | define(["underscore", | |
25 | "backbone", |
|
25 | "backbone", | |
26 | ], function (Underscore, Backbone) { |
|
26 | ], function (Underscore, Backbone) { | |
27 |
|
27 | |||
28 | //-------------------------------------------------------------------- |
|
28 | //-------------------------------------------------------------------- | |
29 | // WidgetManager class |
|
29 | // WidgetManager class | |
30 | //-------------------------------------------------------------------- |
|
30 | //-------------------------------------------------------------------- | |
31 | var WidgetManager = function (comm_manager) { |
|
31 | var WidgetManager = function (comm_manager) { | |
32 | // Public constructor |
|
32 | // Public constructor | |
33 | WidgetManager._managers.push(this); |
|
33 | WidgetManager._managers.push(this); | |
34 |
|
34 | |||
35 | // Attach a comm manager to the |
|
35 | // Attach a comm manager to the | |
36 | this.comm_manager = comm_manager; |
|
36 | this.comm_manager = comm_manager; | |
37 | this._models = {}; /* Dictionary of model ids and model instances */ |
|
37 | this._models = {}; /* Dictionary of model ids and model instances */ | |
38 |
|
38 | |||
39 | // Register already-registered widget model types with the comm manager. |
|
39 | // Register already-registered widget model types with the comm manager. | |
40 | var that = this; |
|
40 | var that = this; | |
41 | _.each(WidgetManager._model_types, function(model_type, model_name) { |
|
41 | _.each(WidgetManager._model_types, function(model_type, model_name) { | |
42 | that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that)); |
|
42 | that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that)); | |
43 | }); |
|
43 | }); | |
44 | }; |
|
44 | }; | |
45 |
|
45 | |||
46 | //-------------------------------------------------------------------- |
|
46 | //-------------------------------------------------------------------- | |
47 | // Class level |
|
47 | // Class level | |
48 | //-------------------------------------------------------------------- |
|
48 | //-------------------------------------------------------------------- | |
49 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ |
|
49 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ | |
50 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ |
|
50 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ | |
51 | WidgetManager._managers = []; /* List of widget managers */ |
|
51 | WidgetManager._managers = []; /* List of widget managers */ | |
52 |
|
52 | |||
53 | WidgetManager.register_widget_model = function (model_name, model_type) { |
|
53 | WidgetManager.register_widget_model = function (model_name, model_type) { | |
54 | // Registers a widget model by name. |
|
54 | // Registers a widget model by name. | |
55 | WidgetManager._model_types[model_name] = model_type; |
|
55 | WidgetManager._model_types[model_name] = model_type; | |
56 |
|
56 | |||
57 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
57 | // Register the widget with the comm manager. Make sure to pass this object's context | |
58 | // in so `this` works in the call back. |
|
58 | // in so `this` works in the call back. | |
59 | _.each(WidgetManager._managers, function(instance, i) { |
|
59 | _.each(WidgetManager._managers, function(instance, i) { | |
60 | if (instance.comm_manager !== null) { |
|
60 | if (instance.comm_manager !== null) { | |
61 | instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); |
|
61 | instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); | |
62 | } |
|
62 | } | |
63 | }); |
|
63 | }); | |
64 | }; |
|
64 | }; | |
65 |
|
65 | |||
66 | WidgetManager.register_widget_view = function (view_name, view_type) { |
|
66 | WidgetManager.register_widget_view = function (view_name, view_type) { | |
67 | // Registers a widget view by name. |
|
67 | // Registers a widget view by name. | |
68 | WidgetManager._view_types[view_name] = view_type; |
|
68 | WidgetManager._view_types[view_name] = view_type; | |
69 | }; |
|
69 | }; | |
70 |
|
70 | |||
71 | //-------------------------------------------------------------------- |
|
71 | //-------------------------------------------------------------------- | |
72 | // Instance level |
|
72 | // Instance level | |
73 | //-------------------------------------------------------------------- |
|
73 | //-------------------------------------------------------------------- | |
74 | WidgetManager.prototype.display_view = function(msg, model) { |
|
74 | WidgetManager.prototype.display_view = function(msg, model) { | |
75 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
75 | var cell = this.get_msg_cell(msg.parent_header.msg_id); | |
76 | if (cell === null) { |
|
76 | if (cell === null) { | |
77 | console.log("Could not determine where the display" + |
|
77 | console.log("Could not determine where the display" + | |
78 | " message was from. Widget will not be displayed"); |
|
78 | " message was from. Widget will not be displayed"); | |
79 | } else { |
|
79 | } else { | |
80 | var view = this.create_view(model, {cell: cell}); |
|
80 | var view = this.create_view(model, {cell: cell}); | |
81 | if (view === null) { |
|
81 | if (view === null) { | |
82 | console.error("View creation failed", model); |
|
82 | console.error("View creation failed", model); | |
83 | } |
|
83 | } | |
84 | if (cell.widget_subarea !== undefined |
|
84 | if (cell.widget_subarea !== undefined | |
85 | && cell.widget_subarea !== null) { |
|
85 | && cell.widget_subarea !== null) { | |
86 |
|
86 | |||
87 | cell.widget_area.show(); |
|
87 | cell.widget_area.show(); | |
88 | cell.widget_subarea.append(view.$el); |
|
88 | cell.widget_subarea.append(view.$el); | |
89 | } |
|
89 | } | |
90 | } |
|
90 | } | |
91 | }, |
|
91 | }, | |
92 |
|
92 | |||
93 | WidgetManager.prototype.create_view = function(model, options) { |
|
93 | WidgetManager.prototype.create_view = function(model, options, view) { | |
94 | var view_name = model.get('view_name'); |
|
94 | var view_name = model.get('view_name'); | |
95 | var ViewType = WidgetManager._view_types[view_name]; |
|
95 | var ViewType = WidgetManager._view_types[view_name]; | |
96 | if (ViewType !== undefined && ViewType !== null) { |
|
96 | if (ViewType !== undefined && ViewType !== null) { | |
|
97 | ||||
|
98 | // If a view is passed into the method, use that view's cell as | |||
|
99 | // the cell for the view that is created. | |||
|
100 | options = options || {}; | |||
|
101 | if (view !== undefined) { | |||
|
102 | options.cell = view.options.cell; | |||
|
103 | } | |||
|
104 | ||||
97 | var parameters = {model: model, options: options}; |
|
105 | var parameters = {model: model, options: options}; | |
98 | var view = new ViewType(parameters); |
|
106 | var view = new ViewType(parameters); | |
99 | view.render(); |
|
107 | view.render(); | |
100 | IPython.keyboard_manager.register_events(view.$el); |
|
108 | IPython.keyboard_manager.register_events(view.$el); | |
101 | model.views.push(view); |
|
109 | model.views.push(view); | |
102 | model.on('destroy', view.remove, view); |
|
110 | model.on('destroy', view.remove, view); | |
103 | return view; |
|
111 | return view; | |
104 | } |
|
112 | } | |
105 | return null; |
|
113 | return null; | |
106 | }, |
|
114 | }, | |
107 |
|
115 | |||
108 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
116 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
109 | var cell = null; |
|
117 | var cell = null; | |
110 | // First, check to see if the msg was triggered by cell execution. |
|
118 | // First, check to see if the msg was triggered by cell execution. | |
111 | if (IPython.notebook !== undefined && IPython.notebook !== null) { |
|
119 | if (IPython.notebook !== undefined && IPython.notebook !== null) { | |
112 | cell = IPython.notebook.get_msg_cell(msg_id); |
|
120 | cell = IPython.notebook.get_msg_cell(msg_id); | |
113 | } |
|
121 | } | |
114 | if (cell !== null) { |
|
122 | if (cell !== null) { | |
115 | return cell |
|
123 | return cell | |
116 | } |
|
124 | } | |
117 | // Second, check to see if a get_cell callback was defined |
|
125 | // Second, check to see if a get_cell callback was defined | |
118 | // for the message. get_cell callbacks are registered for |
|
126 | // for the message. get_cell callbacks are registered for | |
119 | // widget messages, so this block is actually checking to see if the |
|
127 | // widget messages, so this block is actually checking to see if the | |
120 | // message was triggered by a widget. |
|
128 | // message was triggered by a widget. | |
121 | var kernel = this.comm_manager.kernel; |
|
129 | var kernel = this.comm_manager.kernel; | |
122 | if (kernel !== undefined && kernel !== null) { |
|
130 | if (kernel !== undefined && kernel !== null) { | |
123 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
131 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
124 | if (callbacks !== undefined && |
|
132 | if (callbacks !== undefined && | |
125 | callbacks.iopub !== undefined && |
|
133 | callbacks.iopub !== undefined && | |
126 | callbacks.iopub.get_cell !== undefined) { |
|
134 | callbacks.iopub.get_cell !== undefined) { | |
127 |
|
135 | |||
128 | return callbacks.iopub.get_cell(); |
|
136 | return callbacks.iopub.get_cell(); | |
129 | } |
|
137 | } | |
130 | } |
|
138 | } | |
131 |
|
139 | |||
132 | // Not triggered by a cell or widget (no get_cell callback |
|
140 | // Not triggered by a cell or widget (no get_cell callback | |
133 | // exists). |
|
141 | // exists). | |
134 | return null; |
|
142 | return null; | |
135 | }; |
|
143 | }; | |
136 |
|
144 | |||
137 | WidgetManager.prototype.callbacks = function (view) { |
|
145 | WidgetManager.prototype.callbacks = function (view) { | |
138 | // callback handlers specific a view |
|
146 | // callback handlers specific a view | |
139 | var callbacks = {}; |
|
147 | var callbacks = {}; | |
140 | var cell = view.options.cell; |
|
148 | var cell = view.options.cell; | |
141 | if (cell !== null) { |
|
149 | if (cell !== null) { | |
142 | // Try to get output handlers |
|
150 | // Try to get output handlers | |
143 | var handle_output = null; |
|
151 | var handle_output = null; | |
144 | var handle_clear_output = null; |
|
152 | var handle_clear_output = null; | |
145 | if (cell.output_area !== undefined && cell.output_area !== null) { |
|
153 | if (cell.output_area !== undefined && cell.output_area !== null) { | |
146 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); |
|
154 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
147 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); |
|
155 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | |
148 | } |
|
156 | } | |
149 |
|
157 | |||
150 | // Create callback dict using what is known |
|
158 | // Create callback dict using what is known | |
151 | var that = this; |
|
159 | var that = this; | |
152 | callbacks = { |
|
160 | callbacks = { | |
153 | iopub : { |
|
161 | iopub : { | |
154 | output : handle_output, |
|
162 | output : handle_output, | |
155 | clear_output : handle_clear_output, |
|
163 | clear_output : handle_clear_output, | |
156 |
|
164 | |||
157 | // Special function only registered by widget messages. |
|
165 | // Special function only registered by widget messages. | |
158 | // Allows us to get the cell for a message so we know |
|
166 | // Allows us to get the cell for a message so we know | |
159 | // where to add widgets if the code requires it. |
|
167 | // where to add widgets if the code requires it. | |
160 | get_cell : function () { |
|
168 | get_cell : function () { | |
161 | return cell; |
|
169 | return cell; | |
162 | }, |
|
170 | }, | |
163 | }, |
|
171 | }, | |
164 | }; |
|
172 | }; | |
165 | } |
|
173 | } | |
166 | return callbacks; |
|
174 | return callbacks; | |
167 | }; |
|
175 | }; | |
168 |
|
176 | |||
169 | WidgetManager.prototype.get_model = function (model_id) { |
|
177 | WidgetManager.prototype.get_model = function (model_id) { | |
170 | // Look-up a model instance by its id. |
|
178 | // Look-up a model instance by its id. | |
171 | var model = this._models[model_id]; |
|
179 | var model = this._models[model_id]; | |
172 | if (model !== undefined && model.id == model_id) { |
|
180 | if (model !== undefined && model.id == model_id) { | |
173 | return model; |
|
181 | return model; | |
174 | } |
|
182 | } | |
175 | return null; |
|
183 | return null; | |
176 | }; |
|
184 | }; | |
177 |
|
185 | |||
178 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
186 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
179 | // Handle when a comm is opened. |
|
187 | // Handle when a comm is opened. | |
180 | var model_id = comm.comm_id; |
|
188 | var model_id = comm.comm_id; | |
181 | var widget_type_name = msg.content.target_name; |
|
189 | var widget_type_name = msg.content.target_name; | |
182 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); |
|
190 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); | |
183 | this._models[model_id] = widget_model; |
|
191 | this._models[model_id] = widget_model; | |
184 | }; |
|
192 | }; | |
185 |
|
193 | |||
186 | IPython.WidgetManager = WidgetManager; |
|
194 | IPython.WidgetManager = WidgetManager; | |
187 | return IPython.WidgetManager; |
|
195 | return IPython.WidgetManager; | |
188 | }); |
|
196 | }); | |
189 | }()); |
|
197 | }()); |
@@ -1,411 +1,411 | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // Base Widget Model and View classes |
|
9 | // Base Widget Model and View classes | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 | define(["notebook/js/widgetmanager", |
|
17 | define(["notebook/js/widgetmanager", | |
18 | "underscore", |
|
18 | "underscore", | |
19 | "backbone"], |
|
19 | "backbone"], | |
20 | function(WidgetManager, Underscore, Backbone){ |
|
20 | function(WidgetManager, Underscore, Backbone){ | |
21 |
|
21 | |||
22 | var WidgetModel = Backbone.Model.extend({ |
|
22 | var WidgetModel = Backbone.Model.extend({ | |
23 | constructor: function (widget_manager, model_id, comm) { |
|
23 | constructor: function (widget_manager, model_id, comm) { | |
24 | // Construcctor |
|
24 | // Construcctor | |
25 | // |
|
25 | // | |
26 | // Creates a WidgetModel instance. |
|
26 | // Creates a WidgetModel instance. | |
27 | // |
|
27 | // | |
28 | // Parameters |
|
28 | // Parameters | |
29 | // ---------- |
|
29 | // ---------- | |
30 | // widget_manager : WidgetManager instance |
|
30 | // widget_manager : WidgetManager instance | |
31 | // model_id : string |
|
31 | // model_id : string | |
32 | // An ID unique to this model. |
|
32 | // An ID unique to this model. | |
33 | // comm : Comm instance (optional) |
|
33 | // comm : Comm instance (optional) | |
34 | this.widget_manager = widget_manager; |
|
34 | this.widget_manager = widget_manager; | |
35 | this.pending_msgs = 0; |
|
35 | this.pending_msgs = 0; | |
36 | this.msg_throttle = 2; |
|
36 | this.msg_throttle = 2; | |
37 | this.msg_buffer = null; |
|
37 | this.msg_buffer = null; | |
38 | this.key_value_lock = null; |
|
38 | this.key_value_lock = null; | |
39 | this.id = model_id; |
|
39 | this.id = model_id; | |
40 | this.views = []; |
|
40 | this.views = []; | |
41 |
|
41 | |||
42 | if (comm !== undefined) { |
|
42 | if (comm !== undefined) { | |
43 | // Remember comm associated with the model. |
|
43 | // Remember comm associated with the model. | |
44 | this.comm = comm; |
|
44 | this.comm = comm; | |
45 | comm.model = this; |
|
45 | comm.model = this; | |
46 |
|
46 | |||
47 | // Hook comm messages up to model. |
|
47 | // Hook comm messages up to model. | |
48 | comm.on_close($.proxy(this._handle_comm_closed, this)); |
|
48 | comm.on_close($.proxy(this._handle_comm_closed, this)); | |
49 | comm.on_msg($.proxy(this._handle_comm_msg, this)); |
|
49 | comm.on_msg($.proxy(this._handle_comm_msg, this)); | |
50 | } |
|
50 | } | |
51 | return Backbone.Model.apply(this); |
|
51 | return Backbone.Model.apply(this); | |
52 | }, |
|
52 | }, | |
53 |
|
53 | |||
54 | send: function (content, callbacks) { |
|
54 | send: function (content, callbacks) { | |
55 | // Send a custom msg over the comm. |
|
55 | // Send a custom msg over the comm. | |
56 | if (this.comm !== undefined) { |
|
56 | if (this.comm !== undefined) { | |
57 | var data = {method: 'custom', content: content}; |
|
57 | var data = {method: 'custom', content: content}; | |
58 | this.comm.send(data, callbacks); |
|
58 | this.comm.send(data, callbacks); | |
59 | } |
|
59 | } | |
60 | }, |
|
60 | }, | |
61 |
|
61 | |||
62 | _handle_comm_closed: function (msg) { |
|
62 | _handle_comm_closed: function (msg) { | |
63 | // Handle when a widget is closed. |
|
63 | // Handle when a widget is closed. | |
64 | this.trigger('comm:close'); |
|
64 | this.trigger('comm:close'); | |
65 | delete this.comm.model; // Delete ref so GC will collect widget model. |
|
65 | delete this.comm.model; // Delete ref so GC will collect widget model. | |
66 | delete this.comm; |
|
66 | delete this.comm; | |
67 | delete this.model_id; // Delete id from model so widget manager cleans up. |
|
67 | delete this.model_id; // Delete id from model so widget manager cleans up. | |
68 | // TODO: Handle deletion, like this.destroy(), and delete views, etc. |
|
68 | // TODO: Handle deletion, like this.destroy(), and delete views, etc. | |
69 | }, |
|
69 | }, | |
70 |
|
70 | |||
71 | _handle_comm_msg: function (msg) { |
|
71 | _handle_comm_msg: function (msg) { | |
72 | // Handle incoming comm msg. |
|
72 | // Handle incoming comm msg. | |
73 | var method = msg.content.data.method; |
|
73 | var method = msg.content.data.method; | |
74 | switch (method) { |
|
74 | switch (method) { | |
75 | case 'update': |
|
75 | case 'update': | |
76 | this.apply_update(msg.content.data.state); |
|
76 | this.apply_update(msg.content.data.state); | |
77 | break; |
|
77 | break; | |
78 | case 'custom': |
|
78 | case 'custom': | |
79 | this.trigger('msg:custom', msg.content.data.content); |
|
79 | this.trigger('msg:custom', msg.content.data.content); | |
80 | break; |
|
80 | break; | |
81 | case 'display': |
|
81 | case 'display': | |
82 | this.widget_manager.display_view(msg, this); |
|
82 | this.widget_manager.display_view(msg, this); | |
83 | break; |
|
83 | break; | |
84 | } |
|
84 | } | |
85 | }, |
|
85 | }, | |
86 |
|
86 | |||
87 | apply_update: function (state) { |
|
87 | apply_update: function (state) { | |
88 | // Handle when a widget is updated via the python side. |
|
88 | // Handle when a widget is updated via the python side. | |
89 | var that = this; |
|
89 | var that = this; | |
90 | _.each(state, function(value, key) { |
|
90 | _.each(state, function(value, key) { | |
91 | that.key_value_lock = [key, value]; |
|
91 | that.key_value_lock = [key, value]; | |
92 | try { |
|
92 | try { | |
93 | that.set(key, that._unpack_models(value)); |
|
93 | that.set(key, that._unpack_models(value)); | |
94 | } finally { |
|
94 | } finally { | |
95 | that.key_value_lock = null; |
|
95 | that.key_value_lock = null; | |
96 | } |
|
96 | } | |
97 | }); |
|
97 | }); | |
98 | }, |
|
98 | }, | |
99 |
|
99 | |||
100 | _handle_status: function (msg, callbacks) { |
|
100 | _handle_status: function (msg, callbacks) { | |
101 | // Handle status msgs. |
|
101 | // Handle status msgs. | |
102 |
|
102 | |||
103 | // execution_state : ('busy', 'idle', 'starting') |
|
103 | // execution_state : ('busy', 'idle', 'starting') | |
104 | if (this.comm !== undefined) { |
|
104 | if (this.comm !== undefined) { | |
105 | if (msg.content.execution_state ==='idle') { |
|
105 | if (msg.content.execution_state ==='idle') { | |
106 | // Send buffer if this message caused another message to be |
|
106 | // Send buffer if this message caused another message to be | |
107 | // throttled. |
|
107 | // throttled. | |
108 | if (this.msg_buffer !== null && |
|
108 | if (this.msg_buffer !== null && | |
109 | this.msg_throttle === this.pending_msgs) { |
|
109 | this.msg_throttle === this.pending_msgs) { | |
110 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; |
|
110 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; | |
111 | this.comm.send(data, callbacks); |
|
111 | this.comm.send(data, callbacks); | |
112 | this.msg_buffer = null; |
|
112 | this.msg_buffer = null; | |
113 | } else { |
|
113 | } else { | |
114 | --this.pending_msgs; |
|
114 | --this.pending_msgs; | |
115 | } |
|
115 | } | |
116 | } |
|
116 | } | |
117 | } |
|
117 | } | |
118 | }, |
|
118 | }, | |
119 |
|
119 | |||
120 | callbacks: function(view) { |
|
120 | callbacks: function(view) { | |
121 | // Create msg callbacks for a comm msg. |
|
121 | // Create msg callbacks for a comm msg. | |
122 | var callbacks = this.widget_manager.callbacks(view); |
|
122 | var callbacks = this.widget_manager.callbacks(view); | |
123 |
|
123 | |||
124 | if (callbacks.iopub === undefined) { |
|
124 | if (callbacks.iopub === undefined) { | |
125 | callbacks.iopub = {}; |
|
125 | callbacks.iopub = {}; | |
126 | } |
|
126 | } | |
127 |
|
127 | |||
128 | var that = this; |
|
128 | var that = this; | |
129 | callbacks.iopub.status = function (msg) { |
|
129 | callbacks.iopub.status = function (msg) { | |
130 | that._handle_status(msg, callbacks); |
|
130 | that._handle_status(msg, callbacks); | |
131 | } |
|
131 | } | |
132 | return callbacks; |
|
132 | return callbacks; | |
133 | }, |
|
133 | }, | |
134 |
|
134 | |||
135 | sync: function (method, model, options) { |
|
135 | sync: function (method, model, options) { | |
136 | // Handle sync to the back-end. Called when a model.save() is called. |
|
136 | // Handle sync to the back-end. Called when a model.save() is called. | |
137 |
|
137 | |||
138 | // Make sure a comm exists. |
|
138 | // Make sure a comm exists. | |
139 | var error = options.error || function() { |
|
139 | var error = options.error || function() { | |
140 | console.error('Backbone sync error:', arguments); |
|
140 | console.error('Backbone sync error:', arguments); | |
141 | } |
|
141 | } | |
142 | if (this.comm === undefined) { |
|
142 | if (this.comm === undefined) { | |
143 | error(); |
|
143 | error(); | |
144 | return false; |
|
144 | return false; | |
145 | } |
|
145 | } | |
146 |
|
146 | |||
147 | // Delete any key value pairs that the back-end already knows about. |
|
147 | // Delete any key value pairs that the back-end already knows about. | |
148 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); |
|
148 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); | |
149 | if (this.key_value_lock !== null) { |
|
149 | if (this.key_value_lock !== null) { | |
150 | var key = this.key_value_lock[0]; |
|
150 | var key = this.key_value_lock[0]; | |
151 | var value = this.key_value_lock[1]; |
|
151 | var value = this.key_value_lock[1]; | |
152 | if (attrs[key] === value) { |
|
152 | if (attrs[key] === value) { | |
153 | delete attrs[key]; |
|
153 | delete attrs[key]; | |
154 | } |
|
154 | } | |
155 | } |
|
155 | } | |
156 |
|
156 | |||
157 | // Only sync if there are attributes to send to the back-end. |
|
157 | // Only sync if there are attributes to send to the back-end. | |
158 | if (_.size(attrs) > 0) { |
|
158 | if (_.size(attrs) > 0) { | |
159 | var callbacks = options.callbacks || {}; |
|
159 | var callbacks = options.callbacks || {}; | |
160 | if (this.pending_msgs >= this.msg_throttle) { |
|
160 | if (this.pending_msgs >= this.msg_throttle) { | |
161 | // The throttle has been exceeded, buffer the current msg so |
|
161 | // The throttle has been exceeded, buffer the current msg so | |
162 | // it can be sent once the kernel has finished processing |
|
162 | // it can be sent once the kernel has finished processing | |
163 | // some of the existing messages. |
|
163 | // some of the existing messages. | |
164 |
|
164 | |||
165 | // Combine updates if it is a 'patch' sync, otherwise replace updates |
|
165 | // Combine updates if it is a 'patch' sync, otherwise replace updates | |
166 | switch (method) { |
|
166 | switch (method) { | |
167 | case 'patch': |
|
167 | case 'patch': | |
168 | this.msg_buffer = $.extend(this.msg_buffer || {}, attrs); |
|
168 | this.msg_buffer = $.extend(this.msg_buffer || {}, attrs); | |
169 | break; |
|
169 | break; | |
170 | case 'update': |
|
170 | case 'update': | |
171 | case 'create': |
|
171 | case 'create': | |
172 | this.msg_buffer = attrs; |
|
172 | this.msg_buffer = attrs; | |
173 | break; |
|
173 | break; | |
174 | default: |
|
174 | default: | |
175 | error(); |
|
175 | error(); | |
176 | return false; |
|
176 | return false; | |
177 | } |
|
177 | } | |
178 | this.msg_buffer_callbacks = callbacks; |
|
178 | this.msg_buffer_callbacks = callbacks; | |
179 |
|
179 | |||
180 | } else { |
|
180 | } else { | |
181 | // We haven't exceeded the throttle, send the message like |
|
181 | // We haven't exceeded the throttle, send the message like | |
182 | // normal. If this is a patch operation, just send the |
|
182 | // normal. If this is a patch operation, just send the | |
183 | // changes. |
|
183 | // changes. | |
184 | var data = {method: 'backbone', sync_data: attrs}; |
|
184 | var data = {method: 'backbone', sync_data: attrs}; | |
185 | this.comm.send(data, callbacks); |
|
185 | this.comm.send(data, callbacks); | |
186 | this.pending_msgs++; |
|
186 | this.pending_msgs++; | |
187 | } |
|
187 | } | |
188 | } |
|
188 | } | |
189 | // Since the comm is a one-way communication, assume the message |
|
189 | // Since the comm is a one-way communication, assume the message | |
190 | // arrived. Don't call success since we don't have a model back from the server |
|
190 | // arrived. Don't call success since we don't have a model back from the server | |
191 | // this means we miss out on the 'sync' event. |
|
191 | // this means we miss out on the 'sync' event. | |
192 | }, |
|
192 | }, | |
193 |
|
193 | |||
194 | save_changes: function(callbacks) { |
|
194 | save_changes: function(callbacks) { | |
195 | // Push this model's state to the back-end |
|
195 | // Push this model's state to the back-end | |
196 | // |
|
196 | // | |
197 | // This invokes a Backbone.Sync. |
|
197 | // This invokes a Backbone.Sync. | |
198 | this.save(this.changedAttributes(), {patch: true, callbacks: callbacks}); |
|
198 | this.save(this.changedAttributes(), {patch: true, callbacks: callbacks}); | |
199 | }, |
|
199 | }, | |
200 |
|
200 | |||
201 | _pack_models: function(value) { |
|
201 | _pack_models: function(value) { | |
202 | // Replace models with model ids recursively. |
|
202 | // Replace models with model ids recursively. | |
203 | if (value instanceof Backbone.Model) { |
|
203 | if (value instanceof Backbone.Model) { | |
204 | return value.id; |
|
204 | return value.id; | |
205 | } else if (value instanceof Object) { |
|
205 | } else if (value instanceof Object) { | |
206 | var packed = {}; |
|
206 | var packed = {}; | |
207 | var that = this; |
|
207 | var that = this; | |
208 | _.each(value, function(sub_value, key) { |
|
208 | _.each(value, function(sub_value, key) { | |
209 | packed[key] = that._pack_models(sub_value); |
|
209 | packed[key] = that._pack_models(sub_value); | |
210 | }); |
|
210 | }); | |
211 | return packed; |
|
211 | return packed; | |
212 | } else { |
|
212 | } else { | |
213 | return value; |
|
213 | return value; | |
214 | } |
|
214 | } | |
215 | }, |
|
215 | }, | |
216 |
|
216 | |||
217 | _unpack_models: function(value) { |
|
217 | _unpack_models: function(value) { | |
218 | // Replace model ids with models recursively. |
|
218 | // Replace model ids with models recursively. | |
219 | if (value instanceof Object) { |
|
219 | if (value instanceof Object) { | |
220 | var unpacked = {}; |
|
220 | var unpacked = {}; | |
221 | var that = this; |
|
221 | var that = this; | |
222 | _.each(value, function(sub_value, key) { |
|
222 | _.each(value, function(sub_value, key) { | |
223 | unpacked[key] = that._unpack_models(sub_value); |
|
223 | unpacked[key] = that._unpack_models(sub_value); | |
224 | }); |
|
224 | }); | |
225 | return unpacked; |
|
225 | return unpacked; | |
226 | } else { |
|
226 | } else { | |
227 | var model = this.widget_manager.get_model(value); |
|
227 | var model = this.widget_manager.get_model(value); | |
228 | if (model !== null) { |
|
228 | if (model !== null) { | |
229 | return model; |
|
229 | return model; | |
230 | } else { |
|
230 | } else { | |
231 | return value; |
|
231 | return value; | |
232 | } |
|
232 | } | |
233 | } |
|
233 | } | |
234 | }, |
|
234 | }, | |
235 |
|
235 | |||
236 | }); |
|
236 | }); | |
237 | WidgetManager.register_widget_model('WidgetModel', WidgetModel); |
|
237 | WidgetManager.register_widget_model('WidgetModel', WidgetModel); | |
238 |
|
238 | |||
239 |
|
239 | |||
240 | var WidgetView = Backbone.View.extend({ |
|
240 | var WidgetView = Backbone.View.extend({ | |
241 | initialize: function(parameters) { |
|
241 | initialize: function(parameters) { | |
242 | // Public constructor. |
|
242 | // Public constructor. | |
243 | this.model.on('change',this.update,this); |
|
243 | this.model.on('change',this.update,this); | |
244 | this.options = parameters.options; |
|
244 | this.options = parameters.options; | |
245 | this.child_views = []; |
|
245 | this.child_views = []; | |
246 | this.model.views.push(this); |
|
246 | this.model.views.push(this); | |
247 | }, |
|
247 | }, | |
248 |
|
248 | |||
249 | update: function(){ |
|
249 | update: function(){ | |
250 | // Triggered on model change. |
|
250 | // Triggered on model change. | |
251 | // |
|
251 | // | |
252 | // Update view to be consistent with this.model |
|
252 | // Update view to be consistent with this.model | |
253 | }, |
|
253 | }, | |
254 |
|
254 | |||
255 | create_child_view: function(child_model, options) { |
|
255 | create_child_view: function(child_model, options) { | |
256 | // Create and return a child view. |
|
256 | // Create and return a child view. | |
257 | // |
|
257 | // | |
258 | // -given a model and (optionally) a view name if the view name is |
|
258 | // -given a model and (optionally) a view name if the view name is | |
259 | // not given, it defaults to the model's default view attribute. |
|
259 | // not given, it defaults to the model's default view attribute. | |
260 |
|
260 | |||
261 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior |
|
261 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior | |
262 | // it would be great to have the widget manager add the cell metadata |
|
262 | // it would be great to have the widget manager add the cell metadata | |
263 | // to the subview without having to add it here. |
|
263 | // to the subview without having to add it here. | |
264 | var child_view = this.model.widget_manager.create_view(child_model, options || {}); |
|
264 | var child_view = this.model.widget_manager.create_view(child_model, options || {}, this); | |
265 | this.child_views[child_model.id] = child_view; |
|
265 | this.child_views[child_model.id] = child_view; | |
266 | return child_view; |
|
266 | return child_view; | |
267 | }, |
|
267 | }, | |
268 |
|
268 | |||
269 | delete_child_view: function(child_model, options) { |
|
269 | delete_child_view: function(child_model, options) { | |
270 | // Delete a child view that was previously created using create_child_view. |
|
270 | // Delete a child view that was previously created using create_child_view. | |
271 | var view = this.child_views[child_model.id]; |
|
271 | var view = this.child_views[child_model.id]; | |
272 | if (view !== undefined) { |
|
272 | if (view !== undefined) { | |
273 | delete this.child_views[child_model.id]; |
|
273 | delete this.child_views[child_model.id]; | |
274 | view.remove(); |
|
274 | view.remove(); | |
275 | } |
|
275 | } | |
276 | }, |
|
276 | }, | |
277 |
|
277 | |||
278 | do_diff: function(old_list, new_list, removed_callback, added_callback) { |
|
278 | do_diff: function(old_list, new_list, removed_callback, added_callback) { | |
279 | // Difference a changed list and call remove and add callbacks for |
|
279 | // Difference a changed list and call remove and add callbacks for | |
280 | // each removed and added item in the new list. |
|
280 | // each removed and added item in the new list. | |
281 | // |
|
281 | // | |
282 | // Parameters |
|
282 | // Parameters | |
283 | // ---------- |
|
283 | // ---------- | |
284 | // old_list : array |
|
284 | // old_list : array | |
285 | // new_list : array |
|
285 | // new_list : array | |
286 | // removed_callback : Callback(item) |
|
286 | // removed_callback : Callback(item) | |
287 | // Callback that is called for each item removed. |
|
287 | // Callback that is called for each item removed. | |
288 | // added_callback : Callback(item) |
|
288 | // added_callback : Callback(item) | |
289 | // Callback that is called for each item added. |
|
289 | // Callback that is called for each item added. | |
290 |
|
290 | |||
291 |
|
291 | |||
292 | // removed items |
|
292 | // removed items | |
293 | _.each(_.difference(old_list, new_list), function(item, index, list) { |
|
293 | _.each(_.difference(old_list, new_list), function(item, index, list) { | |
294 | removed_callback(item); |
|
294 | removed_callback(item); | |
295 | }, this); |
|
295 | }, this); | |
296 |
|
296 | |||
297 | // added items |
|
297 | // added items | |
298 | _.each(_.difference(new_list, old_list), function(item, index, list) { |
|
298 | _.each(_.difference(new_list, old_list), function(item, index, list) { | |
299 | added_callback(item); |
|
299 | added_callback(item); | |
300 | }, this); |
|
300 | }, this); | |
301 | }, |
|
301 | }, | |
302 |
|
302 | |||
303 | callbacks: function(){ |
|
303 | callbacks: function(){ | |
304 | // Create msg callbacks for a comm msg. |
|
304 | // Create msg callbacks for a comm msg. | |
305 | return this.model.callbacks(this); |
|
305 | return this.model.callbacks(this); | |
306 | }, |
|
306 | }, | |
307 |
|
307 | |||
308 | render: function(){ |
|
308 | render: function(){ | |
309 | // Render the view. |
|
309 | // Render the view. | |
310 | // |
|
310 | // | |
311 | // By default, this is only called the first time the view is created |
|
311 | // By default, this is only called the first time the view is created | |
312 | }, |
|
312 | }, | |
313 |
|
313 | |||
314 | send: function (content) { |
|
314 | send: function (content) { | |
315 | // Send a custom msg associated with this view. |
|
315 | // Send a custom msg associated with this view. | |
316 | this.model.send(content, this.callbacks()); |
|
316 | this.model.send(content, this.callbacks()); | |
317 | }, |
|
317 | }, | |
318 |
|
318 | |||
319 | touch: function () { |
|
319 | touch: function () { | |
320 | this.model.save_changes(this.callbacks()); |
|
320 | this.model.save_changes(this.callbacks()); | |
321 | }, |
|
321 | }, | |
322 | }); |
|
322 | }); | |
323 |
|
323 | |||
324 |
|
324 | |||
325 | var DOMWidgetView = WidgetView.extend({ |
|
325 | var DOMWidgetView = WidgetView.extend({ | |
326 | initialize: function (options) { |
|
326 | initialize: function (options) { | |
327 | // Public constructor |
|
327 | // Public constructor | |
328 |
|
328 | |||
329 | // In the future we may want to make changes more granular |
|
329 | // In the future we may want to make changes more granular | |
330 | // (e.g., trigger on visible:change). |
|
330 | // (e.g., trigger on visible:change). | |
331 | this.model.on('change', this.update, this); |
|
331 | this.model.on('change', this.update, this); | |
332 | this.model.on('msg:custom', this.on_msg, this); |
|
332 | this.model.on('msg:custom', this.on_msg, this); | |
333 | DOMWidgetView.__super__.initialize.apply(this, arguments); |
|
333 | DOMWidgetView.__super__.initialize.apply(this, arguments); | |
334 | }, |
|
334 | }, | |
335 |
|
335 | |||
336 | on_msg: function(msg) { |
|
336 | on_msg: function(msg) { | |
337 | // Handle DOM specific msgs. |
|
337 | // Handle DOM specific msgs. | |
338 | switch(msg.msg_type) { |
|
338 | switch(msg.msg_type) { | |
339 | case 'add_class': |
|
339 | case 'add_class': | |
340 | this.add_class(msg.selector, msg.class_list); |
|
340 | this.add_class(msg.selector, msg.class_list); | |
341 | break; |
|
341 | break; | |
342 | case 'remove_class': |
|
342 | case 'remove_class': | |
343 | this.remove_class(msg.selector, msg.class_list); |
|
343 | this.remove_class(msg.selector, msg.class_list); | |
344 | break; |
|
344 | break; | |
345 | } |
|
345 | } | |
346 | }, |
|
346 | }, | |
347 |
|
347 | |||
348 | add_class: function (selector, class_list) { |
|
348 | add_class: function (selector, class_list) { | |
349 | // Add a DOM class to an element. |
|
349 | // Add a DOM class to an element. | |
350 | this._get_selector_element(selector).addClass(class_list); |
|
350 | this._get_selector_element(selector).addClass(class_list); | |
351 | }, |
|
351 | }, | |
352 |
|
352 | |||
353 | remove_class: function (selector, class_list) { |
|
353 | remove_class: function (selector, class_list) { | |
354 | // Remove a DOM class from an element. |
|
354 | // Remove a DOM class from an element. | |
355 | this._get_selector_element(selector).removeClass(class_list); |
|
355 | this._get_selector_element(selector).removeClass(class_list); | |
356 | }, |
|
356 | }, | |
357 |
|
357 | |||
358 | update: function () { |
|
358 | update: function () { | |
359 | // Update the contents of this view |
|
359 | // Update the contents of this view | |
360 | // |
|
360 | // | |
361 | // Called when the model is changed. The model may have been |
|
361 | // Called when the model is changed. The model may have been | |
362 | // changed by another view or by a state update from the back-end. |
|
362 | // changed by another view or by a state update from the back-end. | |
363 | // The very first update seems to happen before the element is |
|
363 | // The very first update seems to happen before the element is | |
364 | // finished rendering so we use setTimeout to give the element time |
|
364 | // finished rendering so we use setTimeout to give the element time | |
365 | // to render |
|
365 | // to render | |
366 | var e = this.$el; |
|
366 | var e = this.$el; | |
367 | var visible = this.model.get('visible'); |
|
367 | var visible = this.model.get('visible'); | |
368 | setTimeout(function() {e.toggle(visible)},0); |
|
368 | setTimeout(function() {e.toggle(visible)},0); | |
369 |
|
369 | |||
370 | var css = this.model.get('_css'); |
|
370 | var css = this.model.get('_css'); | |
371 | if (css === undefined) {return;} |
|
371 | if (css === undefined) {return;} | |
372 | var that = this; |
|
372 | var that = this; | |
373 | _.each(css, function(css_traits, selector){ |
|
373 | _.each(css, function(css_traits, selector){ | |
374 | // Apply the css traits to all elements that match the selector. |
|
374 | // Apply the css traits to all elements that match the selector. | |
375 | var elements = that._get_selector_element(selector); |
|
375 | var elements = that._get_selector_element(selector); | |
376 | if (elements.length > 0) { |
|
376 | if (elements.length > 0) { | |
377 | _.each(css_traits, function(css_value, css_key){ |
|
377 | _.each(css_traits, function(css_value, css_key){ | |
378 | elements.css(css_key, css_value); |
|
378 | elements.css(css_key, css_value); | |
379 | }); |
|
379 | }); | |
380 | } |
|
380 | } | |
381 | }); |
|
381 | }); | |
382 |
|
382 | |||
383 | }, |
|
383 | }, | |
384 |
|
384 | |||
385 | _get_selector_element: function (selector) { |
|
385 | _get_selector_element: function (selector) { | |
386 | // Get the elements via the css selector. |
|
386 | // Get the elements via the css selector. | |
387 |
|
387 | |||
388 | // If the selector is blank, apply the style to the $el_to_style |
|
388 | // If the selector is blank, apply the style to the $el_to_style | |
389 | // element. If the $el_to_style element is not defined, use apply |
|
389 | // element. If the $el_to_style element is not defined, use apply | |
390 | // the style to the view's element. |
|
390 | // the style to the view's element. | |
391 | var elements; |
|
391 | var elements; | |
392 | if (selector === undefined || selector === null || selector === '') { |
|
392 | if (selector === undefined || selector === null || selector === '') { | |
393 | if (this.$el_to_style === undefined) { |
|
393 | if (this.$el_to_style === undefined) { | |
394 | elements = this.$el; |
|
394 | elements = this.$el; | |
395 | } else { |
|
395 | } else { | |
396 | elements = this.$el_to_style; |
|
396 | elements = this.$el_to_style; | |
397 | } |
|
397 | } | |
398 | } else { |
|
398 | } else { | |
399 | elements = this.$el.find(selector); |
|
399 | elements = this.$el.find(selector); | |
400 | } |
|
400 | } | |
401 | return elements; |
|
401 | return elements; | |
402 | }, |
|
402 | }, | |
403 | }); |
|
403 | }); | |
404 |
|
404 | |||
405 | IPython.WidgetModel = WidgetModel; |
|
405 | IPython.WidgetModel = WidgetModel; | |
406 | IPython.WidgetView = WidgetView; |
|
406 | IPython.WidgetView = WidgetView; | |
407 | IPython.DOMWidgetView = DOMWidgetView; |
|
407 | IPython.DOMWidgetView = DOMWidgetView; | |
408 |
|
408 | |||
409 | // Pass through WidgetManager namespace. |
|
409 | // Pass through WidgetManager namespace. | |
410 | return WidgetManager; |
|
410 | return WidgetManager; | |
411 | }); |
|
411 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now