Show More
@@ -0,0 +1,131 b'' | |||||
|
1 | //---------------------------------------------------------------------------- | |||
|
2 | // Copyright (C) 2013 The IPython Development Team | |||
|
3 | // | |||
|
4 | // Distributed under the terms of the BSD License. The full license is in | |||
|
5 | // the file COPYING, distributed as part of this software. | |||
|
6 | //---------------------------------------------------------------------------- | |||
|
7 | ||||
|
8 | //============================================================================ | |||
|
9 | // Cell | |||
|
10 | //============================================================================ | |||
|
11 | /** | |||
|
12 | * An extendable module that provide base functionnality to create cell for notebook. | |||
|
13 | * @module IPython | |||
|
14 | * @namespace IPython | |||
|
15 | * @submodule widget | |||
|
16 | */ | |||
|
17 | ||||
|
18 | var IPython = (function (IPython) { | |||
|
19 | "use strict"; | |||
|
20 | ||||
|
21 | //----------------------------------------------------------------------- | |||
|
22 | // WidgetManager class | |||
|
23 | //----------------------------------------------------------------------- | |||
|
24 | ||||
|
25 | var WidgetManager = function (kernel) { | |||
|
26 | this.widgets = {}; | |||
|
27 | this.widget_types = {widget : Widget}; | |||
|
28 | if (kernel !== undefined) { | |||
|
29 | this.init_kernel(kernel); | |||
|
30 | } | |||
|
31 | }; | |||
|
32 | ||||
|
33 | WidgetManager.prototype.init_kernel = function (kernel) { | |||
|
34 | this.kernel = kernel; | |||
|
35 | var msg_types = ['widget_create', 'widget_destroy', 'widget_update']; | |||
|
36 | for (var i = 0; i < msg_types.length; i++) { | |||
|
37 | var msg_type = msg_types[i]; | |||
|
38 | kernel.register_iopub_handler(msg_type, $.proxy(this['handle_' + msg_type], this)); | |||
|
39 | } | |||
|
40 | }; | |||
|
41 | ||||
|
42 | WidgetManager.prototype.register_widget_type = function (widget_type, constructor) { | |||
|
43 | // Register a constructor for a given widget type name | |||
|
44 | this.widget_types[widget_type] = constructor; | |||
|
45 | }; | |||
|
46 | ||||
|
47 | WidgetManager.prototype.handle_widget_create = function (msg) { | |||
|
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]; | |||
|
52 | if (constructor === undefined) { | |||
|
53 | console.log("No such widget type registered: ", content.widget_type); | |||
|
54 | console.log("Available widget types are: ", this.widget_types); | |||
|
55 | return; | |||
|
56 | } | |||
|
57 | var widget = new constructor(this.kernel, content); | |||
|
58 | this.widgets[content.widget_id] = widget; | |||
|
59 | }; | |||
|
60 | ||||
|
61 | WidgetManager.prototype.handle_widget_destroy = function (msg) { | |||
|
62 | var content = msg.content; | |||
|
63 | console.log("handle destroy", content); | |||
|
64 | var widget = this.widgets[content.widget_id]; | |||
|
65 | if (widget === undefined) { | |||
|
66 | return; | |||
|
67 | } | |||
|
68 | delete this.widgets[content.widget_id]; | |||
|
69 | widget.handle_destroy(content.data); | |||
|
70 | }; | |||
|
71 | ||||
|
72 | WidgetManager.prototype.handle_widget_update = function (msg) { | |||
|
73 | var content = msg.content; | |||
|
74 | console.log("handle update", content); | |||
|
75 | var widget = this.widgets[content.widget_id]; | |||
|
76 | if (widget === undefined) { | |||
|
77 | return; | |||
|
78 | } | |||
|
79 | widget.handle_update(content.data); | |||
|
80 | }; | |||
|
81 | ||||
|
82 | //----------------------------------------------------------------------- | |||
|
83 | // Widget base class | |||
|
84 | //----------------------------------------------------------------------- | |||
|
85 | ||||
|
86 | var Widget = function (kernel, content) { | |||
|
87 | console.log('widget!', this, kernel, content); | |||
|
88 | this.kernel = kernel; | |||
|
89 | this.widget_id = content.widget_id; | |||
|
90 | this.handle_create(content.data); | |||
|
91 | }; | |||
|
92 | ||||
|
93 | Widget.prototype.handle_create = function (data) { | |||
|
94 | // base class init does nothing | |||
|
95 | console.log("handle_create", this, data); | |||
|
96 | }; | |||
|
97 | ||||
|
98 | Widget.prototype.handle_update = function (data) { | |||
|
99 | console.log("handle_update", this, data); | |||
|
100 | }; | |||
|
101 | ||||
|
102 | Widget.prototype.handle_destroy = function (data) { | |||
|
103 | console.log("handle_destroy", this, data); | |||
|
104 | }; | |||
|
105 | ||||
|
106 | Widget.prototype.update = function (data) { | |||
|
107 | console.log("update", this, data); | |||
|
108 | var content = { | |||
|
109 | widget_id : this.widget_id, | |||
|
110 | data : data, | |||
|
111 | }; | |||
|
112 | this.kernel.send_shell_message("widget_update", content); | |||
|
113 | }; | |||
|
114 | ||||
|
115 | ||||
|
116 | Widget.prototype.destroy = function (data) { | |||
|
117 | console.log("destroy", this, data); | |||
|
118 | var content = { | |||
|
119 | widget_id : this.widget_id, | |||
|
120 | data : data, | |||
|
121 | }; | |||
|
122 | this.kernel.send_shell_message("widget_destroy", content); | |||
|
123 | }; | |||
|
124 | ||||
|
125 | IPython.WidgetManager = WidgetManager; | |||
|
126 | IPython.Widget = Widget; | |||
|
127 | ||||
|
128 | return IPython; | |||
|
129 | ||||
|
130 | }(IPython)); | |||
|
131 |
@@ -68,6 +68,7 b' function (marked) {' | |||||
68 | IPython.tooltip = new IPython.Tooltip() |
|
68 | IPython.tooltip = new IPython.Tooltip() | |
69 | IPython.notification_area = new IPython.NotificationArea('#notification_area') |
|
69 | IPython.notification_area = new IPython.NotificationArea('#notification_area') | |
70 | IPython.notification_area.init_notification_widgets(); |
|
70 | IPython.notification_area.init_notification_widgets(); | |
|
71 | IPython.widget_manager = new IPython.WidgetManager(); | |||
71 |
|
72 | |||
72 | IPython.layout_manager.do_resize(); |
|
73 | IPython.layout_manager.do_resize(); | |
73 |
|
74 | |||
@@ -94,9 +95,10 b' function (marked) {' | |||||
94 | } |
|
95 | } | |
95 | IPython.notebook.set_autosave_interval(IPython.notebook.minimum_autosave_interval); |
|
96 | IPython.notebook.set_autosave_interval(IPython.notebook.minimum_autosave_interval); | |
96 | // only do this once |
|
97 | // only do this once | |
|
98 | IPython.widget_manager.init_kernel(IPython.notebook.kernel); | |||
97 | $([IPython.events]).off('notebook_loaded.Notebook', first_load); |
|
99 | $([IPython.events]).off('notebook_loaded.Notebook', first_load); | |
98 | }; |
|
100 | }; | |
99 |
|
101 | |||
100 | $([IPython.events]).on('notebook_loaded.Notebook', first_load); |
|
102 | $([IPython.events]).on('notebook_loaded.Notebook', first_load); | |
101 | $([IPython.events]).trigger('app_initialized.NotebookApp'); |
|
103 | $([IPython.events]).trigger('app_initialized.NotebookApp'); | |
102 | IPython.notebook.load_notebook(notebookName, notebookPath); |
|
104 | IPython.notebook.load_notebook(notebookName, notebookPath); |
@@ -253,6 +253,7 b' class="notebook_app"' | |||||
253 | <script src="{{ static_url("notebook/js/notificationarea.js") }}" type="text/javascript" charset="utf-8"></script> |
|
253 | <script src="{{ static_url("notebook/js/notificationarea.js") }}" type="text/javascript" charset="utf-8"></script> | |
254 | <script src="{{ static_url("notebook/js/tooltip.js") }}" type="text/javascript" charset="utf-8"></script> |
|
254 | <script src="{{ static_url("notebook/js/tooltip.js") }}" type="text/javascript" charset="utf-8"></script> | |
255 | <script src="{{ static_url("notebook/js/config.js") }}" type="text/javascript" charset="utf-8"></script> |
|
255 | <script src="{{ static_url("notebook/js/config.js") }}" type="text/javascript" charset="utf-8"></script> | |
|
256 | <script src="{{ static_url("notebook/js/widget.js") }}" type="text/javascript" charset="utf-8"></script> | |||
256 | <script src="{{ static_url("notebook/js/main.js") }}" type="text/javascript" charset="utf-8"></script> |
|
257 | <script src="{{ static_url("notebook/js/main.js") }}" type="text/javascript" charset="utf-8"></script> | |
257 |
|
258 | |||
258 | <script src="{{ static_url("notebook/js/contexthint.js") }}" charset="utf-8"></script> |
|
259 | <script src="{{ static_url("notebook/js/contexthint.js") }}" charset="utf-8"></script> |
General Comments 0
You need to be logged in to leave comments.
Login now