Show More
@@ -1,213 +1,209 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2013 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // WidgetModel, WidgetView, and WidgetManager |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * Base Widget classes |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule widget |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | (function () { |
|
19 | 19 | "use strict"; |
|
20 | 20 | |
|
21 | 21 | // Use require.js 'define' method so that require.js is intelligent enough to |
|
22 | 22 | // syncronously load everything within this file when it is being 'required' |
|
23 | 23 | // elsewhere. |
|
24 | 24 | define(["underscore", |
|
25 | 25 | "backbone", |
|
26 | 26 | ], function (underscore, backbone) { |
|
27 | 27 | |
|
28 | 28 | //-------------------------------------------------------------------- |
|
29 | 29 | // WidgetManager class |
|
30 | 30 | //-------------------------------------------------------------------- |
|
31 | 31 | var WidgetManager = function () { |
|
32 | 32 | this.comm_manager = null; |
|
33 | 33 | this.widget_model_types = {}; |
|
34 | 34 | this.widget_view_types = {}; |
|
35 | 35 | this._model_instances = {}; |
|
36 | 36 | |
|
37 | 37 | Backbone.sync = function (method, model, options, error) { |
|
38 | 38 | var result = model._handle_sync(method, options); |
|
39 | 39 | if (options.success) { |
|
40 | 40 | options.success(result); |
|
41 | 41 | } |
|
42 | 42 | }; |
|
43 | 43 | }; |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | WidgetManager.prototype.attach_comm_manager = function (comm_manager) { |
|
47 | 47 | this.comm_manager = comm_manager; |
|
48 | 48 | |
|
49 | 49 | // Register already-registered widget model types with the comm manager. |
|
50 | 50 | for (var widget_model_name in this.widget_model_types) { |
|
51 | 51 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); |
|
52 | 52 | } |
|
53 | 53 | }; |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { |
|
57 | 57 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
58 | 58 | // in so `this` works in the call back. |
|
59 | 59 | if (this.comm_manager !== null) { |
|
60 | 60 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this)); |
|
61 | 61 | } |
|
62 | 62 | this.widget_model_types[widget_model_name] = widget_model_type; |
|
63 | 63 | }; |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { |
|
67 | 67 | this.widget_view_types[widget_view_name] = widget_view_type; |
|
68 | 68 | }; |
|
69 | 69 | WidgetManager.prototype.handle_msg = function(msg, model) { |
|
70 | 70 | var method = msg.content.data.method; |
|
71 | 71 | switch (method) { |
|
72 | 72 | case 'display': |
|
73 | 73 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
74 | 74 | if (cell === null) { |
|
75 | 75 | console.log("Could not determine where the display" + |
|
76 | 76 | " message was from. Widget will not be displayed"); |
|
77 | 77 | } else { |
|
78 | 78 | var view = this.create_view(model, |
|
79 | 79 | msg.content.data.view_name, cell); |
|
80 | 80 | if (view !== undefined |
|
81 | 81 | && cell.widget_subarea !== undefined |
|
82 | 82 | && cell.widget_subarea !== null) { |
|
83 | 83 | cell.widget_area.show(); |
|
84 | 84 | cell.widget_subarea.append(view.$el); |
|
85 | 85 | } |
|
86 | 86 | } |
|
87 | 87 | break; |
|
88 | case 'set_snapshot': | |
|
89 | var cell = this.get_msg_cell(msg.parent_header.msg_id); | |
|
90 | cell.metadata.snapshot = msg.content.data.snapshot; | |
|
91 | break; | |
|
92 | 88 | } |
|
93 | 89 | } |
|
94 | 90 | |
|
95 | 91 | WidgetManager.prototype.create_view = function(model, view_name, cell) { |
|
96 | 92 | view_name = view_name || model.get('default_view_name'); |
|
97 | 93 | var ViewType = this.widget_view_types[view_name]; |
|
98 | 94 | if (ViewType !== undefined && ViewType !== null) { |
|
99 | 95 | var view = new ViewType({model: model, widget_manager: this, cell: cell}); |
|
100 | 96 | view.render(); |
|
101 | 97 | model.views.push(view); |
|
102 | 98 | /* |
|
103 | 99 | // jng: Handle when the view element is remove from the page. |
|
104 | 100 | // observe the view destruction event and do this. We may need |
|
105 | 101 | // to override the view's remove method to trigger this event. |
|
106 | 102 | var that = this; |
|
107 | 103 | view.$el.on("remove", function () { |
|
108 | 104 | var index = that.views.indexOf(view); |
|
109 | 105 | if (index > -1) { |
|
110 | 106 | that.views.splice(index, 1); |
|
111 | 107 | } |
|
112 | 108 | view.remove(); // Clean-up view |
|
113 | 109 | |
|
114 | 110 | // Close the comm if there are no views left. |
|
115 | 111 | if (that.views.length() === 0) { |
|
116 | 112 | //jng: trigger comm close event |
|
117 | 113 | } |
|
118 | 114 | |
|
119 | 115 | |
|
120 | 116 | if (that.comm !== undefined) { |
|
121 | 117 | that.comm.close(); |
|
122 | 118 | delete that.comm.model; // Delete ref so GC will collect widget model. |
|
123 | 119 | delete that.comm; |
|
124 | 120 | } |
|
125 | 121 | delete that.widget_id; // Delete id from model so widget manager cleans up. |
|
126 | 122 | }); |
|
127 | 123 | */ |
|
128 | 124 | return view; |
|
129 | 125 | } |
|
130 | 126 | }, |
|
131 | 127 | |
|
132 | 128 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
133 | 129 | var cell = null; |
|
134 | 130 | // First, check to see if the msg was triggered by cell execution. |
|
135 | 131 | if (IPython.notebook !== undefined && IPython.notebook !== null) { |
|
136 | 132 | cell = IPython.notebook.get_msg_cell(msg_id); |
|
137 | 133 | } |
|
138 | 134 | if (cell !== null) { |
|
139 | 135 | return cell |
|
140 | 136 | } |
|
141 | 137 | // Second, check to see if a get_cell callback was defined |
|
142 | 138 | // for the message. get_cell callbacks are registered for |
|
143 | 139 | // widget messages, so this block is actually checking to see if the |
|
144 | 140 | // message was triggered by a widget. |
|
145 | 141 | var kernel = this.get_kernel(); |
|
146 | 142 | if (kernel !== undefined && kernel !== null) { |
|
147 | 143 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
148 | 144 | if (callbacks !== undefined && |
|
149 | 145 | callbacks.iopub !== undefined && |
|
150 | 146 | callbacks.iopub.get_cell !== undefined) { |
|
151 | 147 | |
|
152 | 148 | return callbacks.iopub.get_cell(); |
|
153 | 149 | } |
|
154 | 150 | } |
|
155 | 151 | |
|
156 | 152 | // Not triggered by a cell or widget (no get_cell callback |
|
157 | 153 | // exists). |
|
158 | 154 | return null; |
|
159 | 155 | }; |
|
160 | 156 | |
|
161 | 157 | |
|
162 | 158 | WidgetManager.prototype.get_model = function (widget_id) { |
|
163 | 159 | var model = this._model_instances[widget_id]; |
|
164 | 160 | if (model !== undefined && model.id == widget_id) { |
|
165 | 161 | return model; |
|
166 | 162 | } |
|
167 | 163 | return null; |
|
168 | 164 | }; |
|
169 | 165 | |
|
170 | 166 | |
|
171 | 167 | WidgetManager.prototype.get_kernel = function () { |
|
172 | 168 | if (this.comm_manager === null) { |
|
173 | 169 | return null; |
|
174 | 170 | } else { |
|
175 | 171 | return this.comm_manager.kernel; |
|
176 | 172 | } |
|
177 | 173 | }; |
|
178 | 174 | |
|
179 | 175 | |
|
180 | 176 | WidgetManager.prototype.on_create_widget = function (callback) { |
|
181 | 177 | this._create_widget_callback = callback; |
|
182 | 178 | }; |
|
183 | 179 | |
|
184 | 180 | |
|
185 | 181 | WidgetManager.prototype._handle_create_widget = function (widget_model) { |
|
186 | 182 | if (this._create_widget_callback) { |
|
187 | 183 | try { |
|
188 | 184 | this._create_widget_callback(widget_model); |
|
189 | 185 | } catch (e) { |
|
190 | 186 | console.log("Exception in WidgetManager callback", e, widget_model); |
|
191 | 187 | } |
|
192 | 188 | } |
|
193 | 189 | }; |
|
194 | 190 | |
|
195 | 191 | |
|
196 | 192 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
197 | 193 | var widget_type_name = msg.content.target_name; |
|
198 | 194 | var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm); |
|
199 | 195 | this._model_instances[comm.comm_id] = widget_model; |
|
200 | 196 | this._handle_create_widget(widget_model); |
|
201 | 197 | }; |
|
202 | 198 | |
|
203 | 199 | //-------------------------------------------------------------------- |
|
204 | 200 | // Init code |
|
205 | 201 | //-------------------------------------------------------------------- |
|
206 | 202 | IPython.WidgetManager = WidgetManager; |
|
207 | 203 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { |
|
208 | 204 | IPython.widget_manager = new WidgetManager(); |
|
209 | 205 | } |
|
210 | 206 | |
|
211 | 207 | return IPython.widget_manager; |
|
212 | 208 | }); |
|
213 | 209 | }()); |
General Comments 0
You need to be logged in to leave comments.
Login now