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