##// END OF EJS Templates
fix js/Python WidgetManager symmetry...
MinRK -
Show More
@@ -1,131 +1,121 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 // Cell
9 // Widget and WidgetManager bases
10 //============================================================================
10 //============================================================================
11 /**
11 /**
12 * An extendable module that provide base functionnality to create cell for notebook.
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 var IPython = (function (IPython) {
18 var IPython = (function (IPython) {
19 "use strict";
19 "use strict";
20
20
21 //-----------------------------------------------------------------------
21 //-----------------------------------------------------------------------
22 // WidgetManager class
22 // WidgetManager class
23 //-----------------------------------------------------------------------
23 //-----------------------------------------------------------------------
24
24
25 var WidgetManager = function (kernel) {
25 var WidgetManager = function (kernel) {
26 this.widgets = {};
26 this.widgets = {};
27 this.widget_types = {widget : Widget};
27 this.widget_types = {widget : Widget};
28 if (kernel !== undefined) {
28 if (kernel !== undefined) {
29 this.init_kernel(kernel);
29 this.init_kernel(kernel);
30 }
30 }
31 };
31 };
32
32
33 WidgetManager.prototype.init_kernel = function (kernel) {
33 WidgetManager.prototype.init_kernel = function (kernel) {
34 this.kernel = kernel;
34 this.kernel = kernel;
35 var msg_types = ['widget_create', 'widget_destroy', 'widget_update'];
35 var msg_types = ['widget_create', 'widget_destroy', 'widget_update'];
36 for (var i = 0; i < msg_types.length; i++) {
36 for (var i = 0; i < msg_types.length; i++) {
37 var msg_type = msg_types[i];
37 var msg_type = msg_types[i];
38 kernel.register_iopub_handler(msg_type, $.proxy(this['handle_' + msg_type], this));
38 kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this));
39 }
39 }
40 };
40 };
41
41
42 WidgetManager.prototype.register_widget_type = function (widget_type, constructor) {
42 WidgetManager.prototype.register_widget_type = function (widget_type, constructor) {
43 // Register a constructor for a given widget type name
43 // Register a constructor for a given widget type name
44 this.widget_types[widget_type] = constructor;
44 this.widget_types[widget_type] = constructor;
45 };
45 };
46
46
47 WidgetManager.prototype.handle_widget_create = function (msg) {
47 WidgetManager.prototype.widget_create = function (msg) {
48 var content = msg.content;
48 var content = msg.content;
49 console.log("handle create", content);
50 console.log(this.widget_types);
51 var constructor = this.widget_types[content.widget_type];
49 var constructor = this.widget_types[content.widget_type];
52 if (constructor === undefined) {
50 if (constructor === undefined) {
53 console.log("No such widget type registered: ", content.widget_type);
51 console.log("No such widget type registered: ", content.widget_type);
54 console.log("Available widget types are: ", this.widget_types);
52 console.log("Available widget types are: ", this.widget_types);
55 return;
53 return;
56 }
54 }
57 var widget = new constructor(this.kernel, content);
55 var widget = new constructor(this.kernel, content);
58 this.widgets[content.widget_id] = widget;
56 this.widgets[content.widget_id] = widget;
59 };
57 };
60
58
61 WidgetManager.prototype.handle_widget_destroy = function (msg) {
59 WidgetManager.prototype.widget_destroy = function (msg) {
62 var content = msg.content;
60 var content = msg.content;
63 console.log("handle destroy", content);
64 var widget = this.widgets[content.widget_id];
61 var widget = this.widgets[content.widget_id];
65 if (widget === undefined) {
62 if (widget === undefined) {
66 return;
63 return;
67 }
64 }
68 delete this.widgets[content.widget_id];
65 delete this.widgets[content.widget_id];
69 widget.handle_destroy(content.data);
66 widget.handle_destroy(content.data);
70 };
67 };
71
68
72 WidgetManager.prototype.handle_widget_update = function (msg) {
69 WidgetManager.prototype.widget_update = function (msg) {
73 var content = msg.content;
70 var content = msg.content;
74 console.log("handle update", content);
75 var widget = this.widgets[content.widget_id];
71 var widget = this.widgets[content.widget_id];
76 if (widget === undefined) {
72 if (widget === undefined) {
77 return;
73 return;
78 }
74 }
79 widget.handle_update(content.data);
75 widget.handle_update(content.data);
80 };
76 };
81
77
82 //-----------------------------------------------------------------------
78 //-----------------------------------------------------------------------
83 // Widget base class
79 // Widget base class
84 //-----------------------------------------------------------------------
80 //-----------------------------------------------------------------------
85
81
86 var Widget = function (kernel, content) {
82 var Widget = function (kernel, content) {
87 console.log('widget!', this, kernel, content);
88 this.kernel = kernel;
83 this.kernel = kernel;
84 if (!content) return;
89 this.widget_id = content.widget_id;
85 this.widget_id = content.widget_id;
90 this.handle_create(content.data);
86 this.handle_create(content.data);
91 };
87 };
92
88
93 Widget.prototype.handle_create = function (data) {
89 Widget.prototype.handle_create = function (data) {
94 // base class init does nothing
95 console.log("handle_create", this, data);
96 };
90 };
97
91
98 Widget.prototype.handle_update = function (data) {
92 Widget.prototype.handle_update = function (data) {
99 console.log("handle_update", this, data);
100 };
93 };
101
94
102 Widget.prototype.handle_destroy = function (data) {
95 Widget.prototype.handle_destroy = function (data) {
103 console.log("handle_destroy", this, data);
104 };
96 };
105
97
106 Widget.prototype.update = function (data) {
98 Widget.prototype.update = function (data) {
107 console.log("update", this, data);
108 var content = {
99 var content = {
109 widget_id : this.widget_id,
100 widget_id : this.widget_id,
110 data : data,
101 data : data,
111 };
102 };
112 this.kernel.send_shell_message("widget_update", content);
103 this.kernel.send_shell_message("widget_update", content);
113 };
104 };
114
105
115
106
116 Widget.prototype.destroy = function (data) {
107 Widget.prototype.destroy = function (data) {
117 console.log("destroy", this, data);
118 var content = {
108 var content = {
119 widget_id : this.widget_id,
109 widget_id : this.widget_id,
120 data : data,
110 data : data,
121 };
111 };
122 this.kernel.send_shell_message("widget_destroy", content);
112 this.kernel.send_shell_message("widget_destroy", content);
123 };
113 };
124
114
125 IPython.WidgetManager = WidgetManager;
115 IPython.WidgetManager = WidgetManager;
126 IPython.Widget = Widget;
116 IPython.Widget = Widget;
127
117
128 return IPython;
118 return IPython;
129
119
130 }(IPython));
120 }(IPython));
131
121
General Comments 0
You need to be logged in to leave comments. Login now