Show More
@@ -1,130 +1,129 b'' | |||||
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(["components/underscore/underscore-min", |
|
24 | define(["components/underscore/underscore-min", | |
25 | "components/backbone/backbone-min", |
|
25 | "components/backbone/backbone-min", | |
26 | ], function (underscore, backbone) { |
|
26 | ], function (underscore, backbone) { | |
27 |
|
27 | |||
28 | //-------------------------------------------------------------------- |
|
28 | //-------------------------------------------------------------------- | |
29 | // WidgetManager class |
|
29 | // WidgetManager class | |
30 | //-------------------------------------------------------------------- |
|
30 | //-------------------------------------------------------------------- | |
31 | var WidgetManager = function () { |
|
31 | var WidgetManager = function () { | |
32 | this.comm_manager = null; |
|
32 | this.comm_manager = null; | |
33 | this.widget_model_types = {}; |
|
33 | this.widget_model_types = {}; | |
34 | this.widget_view_types = {}; |
|
34 | this.widget_view_types = {}; | |
35 | this._model_instances = {}; |
|
35 | this._model_instances = {}; | |
36 |
|
36 | |||
37 | var that = this; |
|
|||
38 | Backbone.sync = function (method, model, options, error) { |
|
37 | Backbone.sync = function (method, model, options, error) { | |
39 | var result = model._handle_sync(method, options); |
|
38 | var result = model._handle_sync(method, options); | |
40 | if (options.success) { |
|
39 | if (options.success) { | |
41 | options.success(result); |
|
40 | options.success(result); | |
42 | } |
|
41 | } | |
43 | }; |
|
42 | }; | |
44 | }; |
|
43 | }; | |
45 |
|
44 | |||
46 |
|
45 | |||
47 | WidgetManager.prototype.attach_comm_manager = function (comm_manager) { |
|
46 | WidgetManager.prototype.attach_comm_manager = function (comm_manager) { | |
48 | this.comm_manager = comm_manager; |
|
47 | this.comm_manager = comm_manager; | |
49 |
|
48 | |||
50 | // Register already register widget model types with the comm manager. |
|
49 | // Register already register widget model types with the comm manager. | |
51 | for (var widget_model_name in this.widget_model_types) { |
|
50 | for (var widget_model_name in this.widget_model_types) { | |
52 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this)); |
|
51 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this)); | |
53 | } |
|
52 | } | |
54 | }; |
|
53 | }; | |
55 |
|
54 | |||
56 |
|
55 | |||
57 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { |
|
56 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | |
58 | // 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 | |
59 | // in so `this` works in the call back. |
|
58 | // in so `this` works in the call back. | |
60 | if (this.comm_manager !== null) { |
|
59 | if (this.comm_manager !== null) { | |
61 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this)); |
|
60 | this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_com_open, this)); | |
62 | } |
|
61 | } | |
63 | this.widget_model_types[widget_model_name] = widget_model_type; |
|
62 | this.widget_model_types[widget_model_name] = widget_model_type; | |
64 | }; |
|
63 | }; | |
65 |
|
64 | |||
66 |
|
65 | |||
67 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { |
|
66 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | |
68 | this.widget_view_types[widget_view_name] = widget_view_type; |
|
67 | this.widget_view_types[widget_view_name] = widget_view_type; | |
69 | }; |
|
68 | }; | |
70 |
|
69 | |||
71 |
|
70 | |||
72 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
71 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
73 | if (IPython.notebook !== undefined && IPython.notebook !== null) { |
|
72 | if (IPython.notebook !== undefined && IPython.notebook !== null) { | |
74 | return IPython.notebook.get_msg_cell(msg_id); |
|
73 | return IPython.notebook.get_msg_cell(msg_id); | |
75 | } |
|
74 | } | |
76 | }; |
|
75 | }; | |
77 |
|
76 | |||
78 |
|
77 | |||
79 | WidgetManager.prototype.get_model = function (widget_id) { |
|
78 | WidgetManager.prototype.get_model = function (widget_id) { | |
80 | var model = this._model_instances[widget_id]; |
|
79 | var model = this._model_instances[widget_id]; | |
81 | if (model !== undefined && model.id == widget_id) { |
|
80 | if (model !== undefined && model.id == widget_id) { | |
82 | return model; |
|
81 | return model; | |
83 | } |
|
82 | } | |
84 | return null; |
|
83 | return null; | |
85 | }; |
|
84 | }; | |
86 |
|
85 | |||
87 |
|
86 | |||
88 | WidgetManager.prototype.get_kernel = function () { |
|
87 | WidgetManager.prototype.get_kernel = function () { | |
89 | if (this.comm_manager === null) { |
|
88 | if (this.comm_manager === null) { | |
90 | return null; |
|
89 | return null; | |
91 | } else { |
|
90 | } else { | |
92 | return this.comm_manager.kernel; |
|
91 | return this.comm_manager.kernel; | |
93 | } |
|
92 | } | |
94 | }; |
|
93 | }; | |
95 |
|
94 | |||
96 |
|
95 | |||
97 | WidgetManager.prototype.on_create_widget = function (callback) { |
|
96 | WidgetManager.prototype.on_create_widget = function (callback) { | |
98 | this._create_widget_callback = callback; |
|
97 | this._create_widget_callback = callback; | |
99 | }; |
|
98 | }; | |
100 |
|
99 | |||
101 |
|
100 | |||
102 | WidgetManager.prototype._handle_create_widget = function (widget_model) { |
|
101 | WidgetManager.prototype._handle_create_widget = function (widget_model) { | |
103 | if (this._create_widget_callback) { |
|
102 | if (this._create_widget_callback) { | |
104 | try { |
|
103 | try { | |
105 | this._create_widget_callback(widget_model); |
|
104 | this._create_widget_callback(widget_model); | |
106 | } catch (e) { |
|
105 | } catch (e) { | |
107 | console.log("Exception in WidgetManager callback", e, widget_model); |
|
106 | console.log("Exception in WidgetManager callback", e, widget_model); | |
108 | } |
|
107 | } | |
109 | } |
|
108 | } | |
110 | }; |
|
109 | }; | |
111 |
|
110 | |||
112 |
|
111 | |||
113 | WidgetManager.prototype._handle_com_open = function (comm, msg) { |
|
112 | WidgetManager.prototype._handle_com_open = function (comm, msg) { | |
114 | var widget_type_name = msg.content.target_name; |
|
113 | var widget_type_name = msg.content.target_name; | |
115 | var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm); |
|
114 | var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm); | |
116 | this._model_instances[comm.comm_id] = widget_model; |
|
115 | this._model_instances[comm.comm_id] = widget_model; | |
117 | this._handle_create_widget(widget_model); |
|
116 | this._handle_create_widget(widget_model); | |
118 | }; |
|
117 | }; | |
119 |
|
118 | |||
120 | //-------------------------------------------------------------------- |
|
119 | //-------------------------------------------------------------------- | |
121 | // Init code |
|
120 | // Init code | |
122 | //-------------------------------------------------------------------- |
|
121 | //-------------------------------------------------------------------- | |
123 | IPython.WidgetManager = WidgetManager; |
|
122 | IPython.WidgetManager = WidgetManager; | |
124 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { |
|
123 | if (IPython.widget_manager === undefined || IPython.widget_manager === null) { | |
125 | IPython.widget_manager = new WidgetManager(); |
|
124 | IPython.widget_manager = new WidgetManager(); | |
126 | } |
|
125 | } | |
127 |
|
126 | |||
128 | return IPython.widget_manager; |
|
127 | return IPython.widget_manager; | |
129 | }); |
|
128 | }); | |
130 | }()); No newline at end of file |
|
129 | }()); |
General Comments 0
You need to be logged in to leave comments.
Login now