##// END OF EJS Templates
Fixed indentation in widgetmanager.js
Jonathan Frederic -
Show More
@@ -1,210 +1,210 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
70 70
71 71 WidgetManager.prototype.handle_msg = function(msg, model) {
72 72 var method = msg.content.data.method;
73 73 switch (method) {
74 74 case 'display':
75 75 var cell = this.get_msg_cell(msg.parent_header.msg_id);
76 76 if (cell === null) {
77 77 console.log("Could not determine where the display" +
78 78 " message was from. Widget will not be displayed");
79 79 } else {
80 var view = this.create_view(model,
81 msg.content.data.view_name, cell);
80 var view = this.create_view(model, msg.content.data.view_name, cell);
82 81 if (view !== undefined
83 82 && cell.widget_subarea !== undefined
84 83 && cell.widget_subarea !== null) {
84
85 85 cell.widget_area.show();
86 86 cell.widget_subarea.append(view.$el);
87 87 }
88 88 }
89 89 break;
90 90 }
91 91 }
92 92
93 93 WidgetManager.prototype.create_view = function(model, view_name, cell) {
94 94 view_name = view_name || model.get('default_view_name');
95 95 var ViewType = this.widget_view_types[view_name];
96 96 if (ViewType !== undefined && ViewType !== null) {
97 97 var view = new ViewType({model: model, widget_manager: this, cell: cell});
98 98 view.render();
99 99 model.views.push(view);
100 100 model.on('destroy', view.remove, view);
101 101 /*
102 102 // TODO: handle view deletion. Don't forget to delete child views
103 103 var that = this;
104 104 view.$el.on("remove", function () {
105 105 var index = that.views.indexOf(view);
106 106 if (index > -1) {
107 107 that.views.splice(index, 1);
108 108 }
109 109 view.remove(); // Clean-up view
110 110
111 111 // Close the comm if there are no views left.
112 112 if (that.views.length() === 0) {
113 113 //trigger comm close event?
114 114 }
115 115
116 116
117 117 if (that.comm !== undefined) {
118 118 that.comm.close();
119 119 delete that.comm.model; // Delete ref so GC will collect widget model.
120 120 delete that.comm;
121 121 }
122 122 delete that.model_id; // Delete id from model so widget manager cleans up.
123 123 });
124 124 */
125 125 return view;
126 126 }
127 127 },
128 128
129 129 WidgetManager.prototype.get_msg_cell = function (msg_id) {
130 130 var cell = null;
131 131 // First, check to see if the msg was triggered by cell execution.
132 132 if (IPython.notebook !== undefined && IPython.notebook !== null) {
133 133 cell = IPython.notebook.get_msg_cell(msg_id);
134 134 }
135 135 if (cell !== null) {
136 136 return cell
137 137 }
138 138 // Second, check to see if a get_cell callback was defined
139 139 // for the message. get_cell callbacks are registered for
140 140 // widget messages, so this block is actually checking to see if the
141 141 // message was triggered by a widget.
142 142 var kernel = this.get_kernel();
143 143 if (kernel !== undefined && kernel !== null) {
144 144 var callbacks = kernel.get_callbacks_for_msg(msg_id);
145 145 if (callbacks !== undefined &&
146 146 callbacks.iopub !== undefined &&
147 147 callbacks.iopub.get_cell !== undefined) {
148 148
149 149 return callbacks.iopub.get_cell();
150 150 }
151 151 }
152 152
153 153 // Not triggered by a cell or widget (no get_cell callback
154 154 // exists).
155 155 return null;
156 156 };
157 157
158 158
159 159 WidgetManager.prototype.get_model = function (model_id) {
160 160 var model = this._model_instances[model_id];
161 161 if (model !== undefined && model.id == model_id) {
162 162 return model;
163 163 }
164 164 return null;
165 165 };
166 166
167 167
168 168 WidgetManager.prototype.get_kernel = function () {
169 169 if (this.comm_manager === null) {
170 170 return null;
171 171 } else {
172 172 return this.comm_manager.kernel;
173 173 }
174 174 };
175 175
176 176
177 177 WidgetManager.prototype.on_create_widget = function (callback) {
178 178 this._create_widget_callback = callback;
179 179 };
180 180
181 181
182 182 WidgetManager.prototype._handle_create_widget = function (widget_model) {
183 183 if (this._create_widget_callback) {
184 184 try {
185 185 this._create_widget_callback(widget_model);
186 186 } catch (e) {
187 187 console.log("Exception in WidgetManager callback", e, widget_model);
188 188 }
189 189 }
190 190 };
191 191
192 192
193 193 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
194 194 var widget_type_name = msg.content.target_name;
195 195 var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm);
196 196 this._model_instances[comm.comm_id] = widget_model; // comm_id == model_id
197 197 this._handle_create_widget(widget_model);
198 198 };
199 199
200 200 //--------------------------------------------------------------------
201 201 // Init code
202 202 //--------------------------------------------------------------------
203 203 IPython.WidgetManager = WidgetManager;
204 204 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
205 205 IPython.widget_manager = new WidgetManager();
206 206 }
207 207
208 208 return IPython.widget_manager;
209 209 });
210 210 }());
General Comments 0
You need to be logged in to leave comments. Login now