Show More
@@ -1,210 +1,210 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2012 The IPython Development Team |
|
2 | // Copyright (C) 2012 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 | // Notification widget |
|
9 | // Notification widget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | var IPython = (function (IPython) { |
|
12 | var IPython = (function (IPython) { | |
13 | "use strict"; |
|
13 | "use strict"; | |
14 | var utils = IPython.utils; |
|
14 | var utils = IPython.utils; | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | var NotificationArea = function (selector) { |
|
17 | var NotificationArea = function (selector) { | |
18 | this.selector = selector; |
|
18 | this.selector = selector; | |
19 | if (this.selector !== undefined) { |
|
19 | if (this.selector !== undefined) { | |
20 | this.element = $(selector); |
|
20 | this.element = $(selector); | |
21 | } |
|
21 | } | |
22 | this.widget_dict = {}; |
|
22 | this.widget_dict = {}; | |
23 | }; |
|
23 | }; | |
24 |
|
24 | |||
25 | NotificationArea.prototype.temp_message = function (msg, timeout, css_class) { |
|
25 | NotificationArea.prototype.temp_message = function (msg, timeout, css_class) { | |
26 | var uuid = utils.uuid(); |
|
26 | var uuid = utils.uuid(); | |
27 | if( css_class == 'danger') {css_class = 'ui-state-error';} |
|
27 | if( css_class == 'danger') {css_class = 'ui-state-error';} | |
28 | if( css_class == 'warning') {css_class = 'ui-state-highlight';} |
|
28 | if( css_class == 'warning') {css_class = 'ui-state-highlight';} | |
29 | var tdiv = $('<div>') |
|
29 | var tdiv = $('<div>') | |
30 | .attr('id',uuid) |
|
30 | .attr('id',uuid) | |
31 | .addClass('notification_widget ui-widget ui-widget-content ui-corner-all') |
|
31 | .addClass('notification_widget ui-widget ui-widget-content ui-corner-all') | |
32 | .addClass('border-box-sizing') |
|
32 | .addClass('border-box-sizing') | |
33 | .addClass(css_class) |
|
33 | .addClass(css_class) | |
34 | .hide() |
|
34 | .hide() | |
35 | .text(msg); |
|
35 | .text(msg); | |
36 |
|
36 | |||
37 | $(this.selector).append(tdiv); |
|
37 | $(this.selector).append(tdiv); | |
38 | var tmout = Math.max(1500,(timeout||1500)); |
|
38 | var tmout = Math.max(1500,(timeout||1500)); | |
39 | tdiv.fadeIn(100); |
|
39 | tdiv.fadeIn(100); | |
40 |
|
40 | |||
41 | setTimeout(function () { |
|
41 | setTimeout(function () { | |
42 | tdiv.fadeOut(100, function () {tdiv.remove();}); |
|
42 | tdiv.fadeOut(100, function () {tdiv.remove();}); | |
43 | }, tmout); |
|
43 | }, tmout); | |
44 | }; |
|
44 | }; | |
45 |
|
45 | |||
46 | NotificationArea.prototype.widget = function(name) { |
|
46 | NotificationArea.prototype.widget = function(name) { | |
47 | if(this.widget_dict[name] == undefined) { |
|
47 | if(this.widget_dict[name] == undefined) { | |
48 | return this.new_notification_widget(name); |
|
48 | return this.new_notification_widget(name); | |
49 | } |
|
49 | } | |
50 | return this.get_widget(name); |
|
50 | return this.get_widget(name); | |
51 | }; |
|
51 | }; | |
52 |
|
52 | |||
53 | NotificationArea.prototype.get_widget = function(name) { |
|
53 | NotificationArea.prototype.get_widget = function(name) { | |
54 | if(this.widget_dict[name] == undefined) { |
|
54 | if(this.widget_dict[name] == undefined) { | |
55 | throw('no widgets with this name'); |
|
55 | throw('no widgets with this name'); | |
56 | } |
|
56 | } | |
57 | return this.widget_dict[name]; |
|
57 | return this.widget_dict[name]; | |
58 | }; |
|
58 | }; | |
59 |
|
59 | |||
60 | NotificationArea.prototype.new_notification_widget = function(name) { |
|
60 | NotificationArea.prototype.new_notification_widget = function(name) { | |
61 | if(this.widget_dict[name] != undefined) { |
|
61 | if(this.widget_dict[name] != undefined) { | |
62 | throw('widget with that name already exists ! '); |
|
62 | throw('widget with that name already exists ! '); | |
63 | } |
|
63 | } | |
64 | var div = $('<div/>').attr('id','notification_'+name); |
|
64 | var div = $('<div/>').attr('id','notification_'+name); | |
65 | $(this.selector).append(div); |
|
65 | $(this.selector).append(div); | |
66 | this.widget_dict[name] = new IPython.NotificationWidget('#notification_'+name); |
|
66 | this.widget_dict[name] = new IPython.NotificationWidget('#notification_'+name); | |
67 | return this.widget_dict[name]; |
|
67 | return this.widget_dict[name]; | |
68 | }; |
|
68 | }; | |
69 |
|
69 | |||
70 | NotificationArea.prototype.init_notification_widgets = function() { |
|
70 | NotificationArea.prototype.init_notification_widgets = function() { | |
71 | var knw = this.new_notification_widget('kernel'); |
|
71 | var knw = this.new_notification_widget('kernel'); | |
72 |
|
72 | |||
73 | // Kernel events |
|
73 | // Kernel events | |
74 | $([IPython.events]).on('status_idle.Kernel',function () { |
|
74 | $([IPython.events]).on('status_idle.Kernel',function () { | |
75 | IPython.save_widget.update_document_title(); |
|
75 | IPython.save_widget.update_document_title(); | |
76 | knw.set_message('Kernel Idle',200); |
|
76 | knw.set_message('Kernel Idle',200); | |
77 | } |
|
77 | } | |
78 | ); |
|
78 | ); | |
79 |
|
79 | |||
80 | $([IPython.events]).on('status_busy.Kernel',function () { |
|
80 | $([IPython.events]).on('status_busy.Kernel',function () { | |
81 | window.document.title='(Busy) '+window.document.title; |
|
81 | window.document.title='(Busy) '+window.document.title; | |
82 | knw.set_message("Kernel busy"); |
|
82 | knw.set_message("Kernel busy"); | |
83 | }); |
|
83 | }); | |
84 |
|
84 | |||
85 | $([IPython.events]).on('status_restarting.Kernel',function () { |
|
85 | $([IPython.events]).on('status_restarting.Kernel',function () { | |
86 | IPython.save_widget.update_document_title(); |
|
86 | IPython.save_widget.update_document_title(); | |
87 | knw.set_message("Restarting kernel", 2000); |
|
87 | knw.set_message("Restarting kernel", 2000); | |
88 | }); |
|
88 | }); | |
89 |
|
89 | |||
90 | $([IPython.events]).on('status_interrupting.Kernel',function () { |
|
90 | $([IPython.events]).on('status_interrupting.Kernel',function () { | |
91 | knw.set_message("Interrupting kernel", 2000); |
|
91 | knw.set_message("Interrupting kernel", 2000); | |
92 | }); |
|
92 | }); | |
93 |
|
93 | |||
94 | $([IPython.events]).on('status_dead.Kernel',function () { |
|
94 | $([IPython.events]).on('status_dead.Kernel',function () { | |
95 | var msg = 'The kernel has died, and the automatic restart has failed.' + |
|
95 | var msg = 'The kernel has died, and the automatic restart has failed.' + | |
96 | ' It is possible the kernel cannot be restarted.' + |
|
96 | ' It is possible the kernel cannot be restarted.' + | |
97 | ' If you are not able to restart the kernel, you will still be able to save' + |
|
97 | ' If you are not able to restart the kernel, you will still be able to save' + | |
98 | ' the notebook, but running code will no longer work until the notebook' + |
|
98 | ' the notebook, but running code will no longer work until the notebook' + | |
99 | ' is reopened.'; |
|
99 | ' is reopened.'; | |
100 |
|
100 | |||
101 | IPython.dialog.modal({ |
|
101 | IPython.dialog.modal({ | |
102 | title: "Dead kernel", |
|
102 | title: "Dead kernel", | |
103 | body : msg, |
|
103 | body : msg, | |
104 | buttons : { |
|
104 | buttons : { | |
105 | "Manual Restart": { |
|
105 | "Manual Restart": { | |
106 | class: "btn-danger", |
|
106 | class: "btn-danger", | |
107 | click: function () { |
|
107 | click: function () { | |
108 | $([IPython.events]).trigger('status_restarting.Kernel'); |
|
108 | $([IPython.events]).trigger('status_restarting.Kernel'); | |
109 | IPython.notebook.start_kernel(); |
|
109 | IPython.notebook.start_kernel(); | |
110 | } |
|
110 | } | |
111 | }, |
|
111 | }, | |
112 | "Don't restart": {} |
|
112 | "Don't restart": {} | |
113 | } |
|
113 | } | |
114 | }); |
|
114 | }); | |
115 | }); |
|
115 | }); | |
116 |
|
116 | |||
117 | $([IPython.events]).on('websocket_closed.Kernel', function (event, data) { |
|
117 | $([IPython.events]).on('websocket_closed.Kernel', function (event, data) { | |
118 | var kernel = data.kernel; |
|
118 | var kernel = data.kernel; | |
119 | var ws_url = data.ws_url; |
|
119 | var ws_url = data.ws_url; | |
120 | var early = data.early; |
|
120 | var early = data.early; | |
121 | var msg; |
|
121 | var msg; | |
122 | if (!early) { |
|
122 | if (!early) { | |
123 | knw.set_message('Reconnecting WebSockets', 1000); |
|
123 | knw.set_message('Reconnecting WebSockets', 1000); | |
124 | setTimeout(function () { |
|
124 | setTimeout(function () { | |
125 | kernel.start_channels(); |
|
125 | kernel.start_channels(); | |
126 | }, 5000); |
|
126 | }, 5000); | |
127 | return; |
|
127 | return; | |
128 | } |
|
128 | } | |
129 | console.log('WebSocket connection failed: ', ws_url) |
|
129 | console.log('WebSocket connection failed: ', ws_url) | |
130 |
msg = "A WebSocket connection |
|
130 | msg = "A WebSocket connection could not be established." + | |
131 | " You will NOT be able to run code. Check your" + |
|
131 | " You will NOT be able to run code. Check your" + | |
132 | " network connection or notebook server configuration."; |
|
132 | " network connection or notebook server configuration."; | |
133 | IPython.dialog.modal({ |
|
133 | IPython.dialog.modal({ | |
134 | title: "WebSocket connection failed", |
|
134 | title: "WebSocket connection failed", | |
135 | body: msg, |
|
135 | body: msg, | |
136 | buttons : { |
|
136 | buttons : { | |
137 | "OK": {}, |
|
137 | "OK": {}, | |
138 | "Reconnect": { |
|
138 | "Reconnect": { | |
139 | click: function () { |
|
139 | click: function () { | |
140 | knw.set_message('Reconnecting WebSockets', 1000); |
|
140 | knw.set_message('Reconnecting WebSockets', 1000); | |
141 | setTimeout(function () { |
|
141 | setTimeout(function () { | |
142 | kernel.start_channels(); |
|
142 | kernel.start_channels(); | |
143 | }, 5000); |
|
143 | }, 5000); | |
144 | } |
|
144 | } | |
145 | } |
|
145 | } | |
146 | } |
|
146 | } | |
147 | }); |
|
147 | }); | |
148 | }); |
|
148 | }); | |
149 |
|
149 | |||
150 |
|
150 | |||
151 | var nnw = this.new_notification_widget('notebook'); |
|
151 | var nnw = this.new_notification_widget('notebook'); | |
152 |
|
152 | |||
153 | // Notebook events |
|
153 | // Notebook events | |
154 | $([IPython.events]).on('notebook_loading.Notebook', function () { |
|
154 | $([IPython.events]).on('notebook_loading.Notebook', function () { | |
155 | nnw.set_message("Loading notebook",500); |
|
155 | nnw.set_message("Loading notebook",500); | |
156 | }); |
|
156 | }); | |
157 | $([IPython.events]).on('notebook_loaded.Notebook', function () { |
|
157 | $([IPython.events]).on('notebook_loaded.Notebook', function () { | |
158 | nnw.set_message("Notebook loaded",500); |
|
158 | nnw.set_message("Notebook loaded",500); | |
159 | }); |
|
159 | }); | |
160 | $([IPython.events]).on('notebook_saving.Notebook', function () { |
|
160 | $([IPython.events]).on('notebook_saving.Notebook', function () { | |
161 | nnw.set_message("Saving notebook",500); |
|
161 | nnw.set_message("Saving notebook",500); | |
162 | }); |
|
162 | }); | |
163 | $([IPython.events]).on('notebook_saved.Notebook', function () { |
|
163 | $([IPython.events]).on('notebook_saved.Notebook', function () { | |
164 | nnw.set_message("Notebook saved",2000); |
|
164 | nnw.set_message("Notebook saved",2000); | |
165 | }); |
|
165 | }); | |
166 | $([IPython.events]).on('notebook_save_failed.Notebook', function () { |
|
166 | $([IPython.events]).on('notebook_save_failed.Notebook', function () { | |
167 | nnw.set_message("Notebook save failed"); |
|
167 | nnw.set_message("Notebook save failed"); | |
168 | }); |
|
168 | }); | |
169 |
|
169 | |||
170 | // Checkpoint events |
|
170 | // Checkpoint events | |
171 | $([IPython.events]).on('checkpoint_created.Notebook', function (evt, data) { |
|
171 | $([IPython.events]).on('checkpoint_created.Notebook', function (evt, data) { | |
172 | var msg = "Checkpoint created"; |
|
172 | var msg = "Checkpoint created"; | |
173 | if (data.last_modified) { |
|
173 | if (data.last_modified) { | |
174 | var d = new Date(data.last_modified); |
|
174 | var d = new Date(data.last_modified); | |
175 | msg = msg + ": " + d.format("HH:MM:ss"); |
|
175 | msg = msg + ": " + d.format("HH:MM:ss"); | |
176 | } |
|
176 | } | |
177 | nnw.set_message(msg, 2000); |
|
177 | nnw.set_message(msg, 2000); | |
178 | }); |
|
178 | }); | |
179 | $([IPython.events]).on('checkpoint_failed.Notebook', function () { |
|
179 | $([IPython.events]).on('checkpoint_failed.Notebook', function () { | |
180 | nnw.set_message("Checkpoint failed"); |
|
180 | nnw.set_message("Checkpoint failed"); | |
181 | }); |
|
181 | }); | |
182 | $([IPython.events]).on('checkpoint_deleted.Notebook', function () { |
|
182 | $([IPython.events]).on('checkpoint_deleted.Notebook', function () { | |
183 | nnw.set_message("Checkpoint deleted", 500); |
|
183 | nnw.set_message("Checkpoint deleted", 500); | |
184 | }); |
|
184 | }); | |
185 | $([IPython.events]).on('checkpoint_delete_failed.Notebook', function () { |
|
185 | $([IPython.events]).on('checkpoint_delete_failed.Notebook', function () { | |
186 | nnw.set_message("Checkpoint delete failed"); |
|
186 | nnw.set_message("Checkpoint delete failed"); | |
187 | }); |
|
187 | }); | |
188 | $([IPython.events]).on('checkpoint_restoring.Notebook', function () { |
|
188 | $([IPython.events]).on('checkpoint_restoring.Notebook', function () { | |
189 | nnw.set_message("Restoring to checkpoint...", 500); |
|
189 | nnw.set_message("Restoring to checkpoint...", 500); | |
190 | }); |
|
190 | }); | |
191 | $([IPython.events]).on('checkpoint_restore_failed.Notebook', function () { |
|
191 | $([IPython.events]).on('checkpoint_restore_failed.Notebook', function () { | |
192 | nnw.set_message("Checkpoint restore failed"); |
|
192 | nnw.set_message("Checkpoint restore failed"); | |
193 | }); |
|
193 | }); | |
194 |
|
194 | |||
195 | // Autosave events |
|
195 | // Autosave events | |
196 | $([IPython.events]).on('autosave_disabled.Notebook', function () { |
|
196 | $([IPython.events]).on('autosave_disabled.Notebook', function () { | |
197 | nnw.set_message("Autosave disabled", 2000); |
|
197 | nnw.set_message("Autosave disabled", 2000); | |
198 | }); |
|
198 | }); | |
199 | $([IPython.events]).on('autosave_enabled.Notebook', function (evt, interval) { |
|
199 | $([IPython.events]).on('autosave_enabled.Notebook', function (evt, interval) { | |
200 | nnw.set_message("Saving every " + interval / 1000 + "s", 1000); |
|
200 | nnw.set_message("Saving every " + interval / 1000 + "s", 1000); | |
201 | }); |
|
201 | }); | |
202 |
|
202 | |||
203 | }; |
|
203 | }; | |
204 |
|
204 | |||
205 | IPython.NotificationArea = NotificationArea; |
|
205 | IPython.NotificationArea = NotificationArea; | |
206 |
|
206 | |||
207 | return IPython; |
|
207 | return IPython; | |
208 |
|
208 | |||
209 | }(IPython)); |
|
209 | }(IPython)); | |
210 |
|
210 |
General Comments 0
You need to be logged in to leave comments.
Login now