##// END OF EJS Templates
Added a line that was accidently deleted during merge of 4e813c5
Jonathan Frederic -
Show More
@@ -1,188 +1,189
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 ], function (Underscore, Backbone) {
26 27
27 28 //--------------------------------------------------------------------
28 29 // WidgetManager class
29 30 //--------------------------------------------------------------------
30 31 var WidgetManager = function (comm_manager) {
31 32 // Public constructor
32 33 WidgetManager._managers.push(this);
33 34
34 35 // Attach a comm manager to the
35 36 this.comm_manager = comm_manager;
36 37
37 38 // Register already-registered widget model types with the comm manager.
38 39 for (var name in WidgetManager._model_types) {
39 40 if (WidgetManager._model_types.hasOwnProperty(name)) {
40 41 this.comm_manager.register_target(name, $.proxy(this._handle_comm_open, this));
41 42
42 43 }
43 44 }
44 45 };
45 46
46 47 //--------------------------------------------------------------------
47 48 // Class level
48 49 //--------------------------------------------------------------------
49 50 WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */
50 51 WidgetManager._view_types = {}; /* Dictionary of view names and view types. */
51 52 WidgetManager._models = {}; /* Dictionary of model ids and model instances */
52 53 WidgetManager._managers = []; /* List of widget managers */
53 54
54 55 WidgetManager.register_widget_model = function (model_name, model_type) {
55 56 // Registers a widget model by name.
56 57 WidgetManager._model_types[model_name] = model_type;
57 58
58 59 // Register the widget with the comm manager. Make sure to pass this object's context
59 60 // in so `this` works in the call back.
60 61 for (var i = 0; i < WidgetManager._managers.length; i++) {
61 62 var instance = WidgetManager._managers[i];
62 63 if (instance.comm_manager !== null) {
63 64 instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
64 65 }
65 66 }
66 67 };
67 68
68 69 WidgetManager.register_widget_view = function (view_name, view_type) {
69 70 // Registers a widget view by name.
70 71 WidgetManager._view_types[view_name] = view_type;
71 72 };
72 73
73 74 //--------------------------------------------------------------------
74 75 // Instance level
75 76 //--------------------------------------------------------------------
76 77 WidgetManager.prototype.display_view = function(msg, model) {
77 78 var cell = this.get_msg_cell(msg.parent_header.msg_id);
78 79 if (cell === null) {
79 80 console.log("Could not determine where the display" +
80 81 " message was from. Widget will not be displayed");
81 82 } else {
82 83 var view = this.create_view(model, {cell: cell});
83 84 if (view === undefined) {
84 85 console.error("View creation failed", model);
85 86 }
86 87 if (cell.widget_subarea !== undefined
87 88 && cell.widget_subarea !== null) {
88 89
89 90 cell.widget_area.show();
90 91 cell.widget_subarea.append(view.$el);
91 92 }
92 93 }
93 94 },
94 95
95 96 WidgetManager.prototype.create_view = function(model, options) {
96 97 var view_name = model.get('view_name');
97 98 var ViewType = WidgetManager._view_types[view_name];
98 99 if (ViewType !== undefined && ViewType !== null) {
99 100 var parameters = {model: model, options: options};
100 101 var view = new ViewType(parameters);
101 102 view.render();
102 103 IPython.keyboard_manager.register_events(view.$el);
103 104 model.views.push(view);
104 105 model.on('destroy', view.remove, view);
105 106 return view;
106 107 }
107 108 },
108 109
109 110 WidgetManager.prototype.get_msg_cell = function (msg_id) {
110 111 var cell = null;
111 112 // First, check to see if the msg was triggered by cell execution.
112 113 if (IPython.notebook !== undefined && IPython.notebook !== null) {
113 114 cell = IPython.notebook.get_msg_cell(msg_id);
114 115 }
115 116 if (cell !== null) {
116 117 return cell
117 118 }
118 119 // Second, check to see if a get_cell callback was defined
119 120 // for the message. get_cell callbacks are registered for
120 121 // widget messages, so this block is actually checking to see if the
121 122 // message was triggered by a widget.
122 123 var kernel = this.comm_manager.kernel;
123 124 if (kernel !== undefined && kernel !== null) {
124 125 var callbacks = kernel.get_callbacks_for_msg(msg_id);
125 126 if (callbacks !== undefined &&
126 127 callbacks.iopub !== undefined &&
127 128 callbacks.iopub.get_cell !== undefined) {
128 129
129 130 return callbacks.iopub.get_cell();
130 131 }
131 132 }
132 133
133 134 // Not triggered by a cell or widget (no get_cell callback
134 135 // exists).
135 136 return null;
136 137 };
137 138
138 139 WidgetManager.prototype.callbacks = function (view) {
139 140 // callback handlers specific a view
140 141 var callbacks = {};
141 142 var cell = view.options.cell;
142 143 if (cell !== null) {
143 144 // Try to get output handlers
144 145 var handle_output = null;
145 146 var handle_clear_output = null;
146 147 if (cell.output_area !== undefined && cell.output_area !== null) {
147 148 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
148 149 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
149 150 }
150 151
151 152 // Create callback dict using what is known
152 153 var that = this;
153 154 callbacks = {
154 155 iopub : {
155 156 output : handle_output,
156 157 clear_output : handle_clear_output,
157 158
158 159 // Special function only registered by widget messages.
159 160 // Allows us to get the cell for a message so we know
160 161 // where to add widgets if the code requires it.
161 162 get_cell : function () {
162 163 return cell;
163 164 },
164 165 },
165 166 };
166 167 }
167 168 return callbacks;
168 169 };
169 170
170 171 WidgetManager.prototype.get_model = function (model_id) {
171 172 var model = WidgetManager._models[model_id];
172 173 if (model !== undefined && model.id == model_id) {
173 174 return model;
174 175 }
175 176 return null;
176 177 };
177 178
178 179 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
179 180 var model_id = comm.comm_id;
180 181 var widget_type_name = msg.content.target_name;
181 182 var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
182 183 WidgetManager._models[model_id] = widget_model;
183 184 };
184 185
185 186 IPython.WidgetManager = WidgetManager;
186 187 return IPython.WidgetManager;
187 188 });
188 189 }());
General Comments 0
You need to be logged in to leave comments. Login now