##// END OF EJS Templates
Correct initial state of kernel status indicator...
MinRK -
Show More
@@ -1,222 +1,232 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 var $kernel_ind_icon = $("#kernel_indicator_icon");
72 var $kernel_ind_icon = $("#kernel_indicator_icon");
73 var $modal_ind_icon = $("#modal_indicator_icon");
73 var $modal_ind_icon = $("#modal_indicator_icon");
74
74
75 // Command/Edit mode
75 // Command/Edit mode
76 $([IPython.events]).on('edit_mode.Notebook',function () {
76 $([IPython.events]).on('edit_mode.Notebook',function () {
77 IPython.save_widget.update_document_title();
77 IPython.save_widget.update_document_title();
78 $modal_ind_icon.attr('class','icon-pencil').attr('title','Edit Mode');
78 $modal_ind_icon.attr('class','icon-pencil').attr('title','Edit Mode');
79 });
79 });
80
80
81 $([IPython.events]).on('command_mode.Notebook',function () {
81 $([IPython.events]).on('command_mode.Notebook',function () {
82 IPython.save_widget.update_document_title();
82 IPython.save_widget.update_document_title();
83 $modal_ind_icon.attr('class','').attr('title','Command Mode');
83 $modal_ind_icon.attr('class','').attr('title','Command Mode');
84 });
84 });
85
85
86 // Kernel events
86 // Kernel events
87 $([IPython.events]).on('status_idle.Kernel',function () {
87 $([IPython.events]).on('status_idle.Kernel',function () {
88 IPython.save_widget.update_document_title();
88 IPython.save_widget.update_document_title();
89 $kernel_ind_icon.attr('class','icon-circle-blank').attr('title','Kernel Idle');
89 $kernel_ind_icon.attr('class','icon-circle-blank').attr('title','Kernel Idle');
90 });
90 });
91
91
92 $([IPython.events]).on('status_busy.Kernel',function () {
92 $([IPython.events]).on('status_busy.Kernel',function () {
93 window.document.title='(Busy) '+window.document.title;
93 window.document.title='(Busy) '+window.document.title;
94 $kernel_ind_icon.attr('class','icon-circle').attr('title','Kernel Busy');
94 $kernel_ind_icon.attr('class','icon-circle').attr('title','Kernel Busy');
95 });
95 });
96
96
97 $([IPython.events]).on('status_restarting.Kernel',function () {
97 $([IPython.events]).on('status_restarting.Kernel',function () {
98 IPython.save_widget.update_document_title();
98 IPython.save_widget.update_document_title();
99 knw.set_message("Restarting kernel", 2000);
99 knw.set_message("Restarting kernel", 2000);
100 });
100 });
101
101
102 $([IPython.events]).on('status_interrupting.Kernel',function () {
102 $([IPython.events]).on('status_interrupting.Kernel',function () {
103 knw.set_message("Interrupting kernel", 2000);
103 knw.set_message("Interrupting kernel", 2000);
104 });
104 });
105
105
106 // Start the kernel indicator in the busy state, and send a kernel_info request.
107 // When the kernel_info reply arrives, the kernel is idle.
108 $kernel_ind_icon.attr('class','icon-circle').attr('title','Kernel Busy');
109
110 $([IPython.events]).on('status_started.Kernel', function (evt, data) {
111 data.kernel.kernel_info(function () {
112 $([IPython.events]).trigger('status_idle.Kernel');
113 });
114 });
115
106 $([IPython.events]).on('status_dead.Kernel',function () {
116 $([IPython.events]).on('status_dead.Kernel',function () {
107 var msg = 'The kernel has died, and the automatic restart has failed.' +
117 var msg = 'The kernel has died, and the automatic restart has failed.' +
108 ' It is possible the kernel cannot be restarted.' +
118 ' It is possible the kernel cannot be restarted.' +
109 ' If you are not able to restart the kernel, you will still be able to save' +
119 ' If you are not able to restart the kernel, you will still be able to save' +
110 ' the notebook, but running code will no longer work until the notebook' +
120 ' the notebook, but running code will no longer work until the notebook' +
111 ' is reopened.';
121 ' is reopened.';
112
122
113 IPython.dialog.modal({
123 IPython.dialog.modal({
114 title: "Dead kernel",
124 title: "Dead kernel",
115 body : msg,
125 body : msg,
116 buttons : {
126 buttons : {
117 "Manual Restart": {
127 "Manual Restart": {
118 class: "btn-danger",
128 class: "btn-danger",
119 click: function () {
129 click: function () {
120 $([IPython.events]).trigger('status_restarting.Kernel');
130 $([IPython.events]).trigger('status_restarting.Kernel');
121 IPython.notebook.start_kernel();
131 IPython.notebook.start_kernel();
122 }
132 }
123 },
133 },
124 "Don't restart": {}
134 "Don't restart": {}
125 }
135 }
126 });
136 });
127 });
137 });
128
138
129 $([IPython.events]).on('websocket_closed.Kernel', function (event, data) {
139 $([IPython.events]).on('websocket_closed.Kernel', function (event, data) {
130 var kernel = data.kernel;
140 var kernel = data.kernel;
131 var ws_url = data.ws_url;
141 var ws_url = data.ws_url;
132 var early = data.early;
142 var early = data.early;
133 var msg;
143 var msg;
134 if (!early) {
144 if (!early) {
135 knw.set_message('Reconnecting WebSockets', 1000);
145 knw.set_message('Reconnecting WebSockets', 1000);
136 setTimeout(function () {
146 setTimeout(function () {
137 kernel.start_channels();
147 kernel.start_channels();
138 }, 5000);
148 }, 5000);
139 return;
149 return;
140 }
150 }
141 console.log('WebSocket connection failed: ', ws_url)
151 console.log('WebSocket connection failed: ', ws_url)
142 msg = "A WebSocket connection could not be established." +
152 msg = "A WebSocket connection could not be established." +
143 " You will NOT be able to run code. Check your" +
153 " You will NOT be able to run code. Check your" +
144 " network connection or notebook server configuration.";
154 " network connection or notebook server configuration.";
145 IPython.dialog.modal({
155 IPython.dialog.modal({
146 title: "WebSocket connection failed",
156 title: "WebSocket connection failed",
147 body: msg,
157 body: msg,
148 buttons : {
158 buttons : {
149 "OK": {},
159 "OK": {},
150 "Reconnect": {
160 "Reconnect": {
151 click: function () {
161 click: function () {
152 knw.set_message('Reconnecting WebSockets', 1000);
162 knw.set_message('Reconnecting WebSockets', 1000);
153 setTimeout(function () {
163 setTimeout(function () {
154 kernel.start_channels();
164 kernel.start_channels();
155 }, 5000);
165 }, 5000);
156 }
166 }
157 }
167 }
158 }
168 }
159 });
169 });
160 });
170 });
161
171
162
172
163 var nnw = this.new_notification_widget('notebook');
173 var nnw = this.new_notification_widget('notebook');
164
174
165 // Notebook events
175 // Notebook events
166 $([IPython.events]).on('notebook_loading.Notebook', function () {
176 $([IPython.events]).on('notebook_loading.Notebook', function () {
167 nnw.set_message("Loading notebook",500);
177 nnw.set_message("Loading notebook",500);
168 });
178 });
169 $([IPython.events]).on('notebook_loaded.Notebook', function () {
179 $([IPython.events]).on('notebook_loaded.Notebook', function () {
170 nnw.set_message("Notebook loaded",500);
180 nnw.set_message("Notebook loaded",500);
171 });
181 });
172 $([IPython.events]).on('notebook_saving.Notebook', function () {
182 $([IPython.events]).on('notebook_saving.Notebook', function () {
173 nnw.set_message("Saving notebook",500);
183 nnw.set_message("Saving notebook",500);
174 });
184 });
175 $([IPython.events]).on('notebook_saved.Notebook', function () {
185 $([IPython.events]).on('notebook_saved.Notebook', function () {
176 nnw.set_message("Notebook saved",2000);
186 nnw.set_message("Notebook saved",2000);
177 });
187 });
178 $([IPython.events]).on('notebook_save_failed.Notebook', function () {
188 $([IPython.events]).on('notebook_save_failed.Notebook', function () {
179 nnw.set_message("Notebook save failed");
189 nnw.set_message("Notebook save failed");
180 });
190 });
181
191
182 // Checkpoint events
192 // Checkpoint events
183 $([IPython.events]).on('checkpoint_created.Notebook', function (evt, data) {
193 $([IPython.events]).on('checkpoint_created.Notebook', function (evt, data) {
184 var msg = "Checkpoint created";
194 var msg = "Checkpoint created";
185 if (data.last_modified) {
195 if (data.last_modified) {
186 var d = new Date(data.last_modified);
196 var d = new Date(data.last_modified);
187 msg = msg + ": " + d.format("HH:MM:ss");
197 msg = msg + ": " + d.format("HH:MM:ss");
188 }
198 }
189 nnw.set_message(msg, 2000);
199 nnw.set_message(msg, 2000);
190 });
200 });
191 $([IPython.events]).on('checkpoint_failed.Notebook', function () {
201 $([IPython.events]).on('checkpoint_failed.Notebook', function () {
192 nnw.set_message("Checkpoint failed");
202 nnw.set_message("Checkpoint failed");
193 });
203 });
194 $([IPython.events]).on('checkpoint_deleted.Notebook', function () {
204 $([IPython.events]).on('checkpoint_deleted.Notebook', function () {
195 nnw.set_message("Checkpoint deleted", 500);
205 nnw.set_message("Checkpoint deleted", 500);
196 });
206 });
197 $([IPython.events]).on('checkpoint_delete_failed.Notebook', function () {
207 $([IPython.events]).on('checkpoint_delete_failed.Notebook', function () {
198 nnw.set_message("Checkpoint delete failed");
208 nnw.set_message("Checkpoint delete failed");
199 });
209 });
200 $([IPython.events]).on('checkpoint_restoring.Notebook', function () {
210 $([IPython.events]).on('checkpoint_restoring.Notebook', function () {
201 nnw.set_message("Restoring to checkpoint...", 500);
211 nnw.set_message("Restoring to checkpoint...", 500);
202 });
212 });
203 $([IPython.events]).on('checkpoint_restore_failed.Notebook', function () {
213 $([IPython.events]).on('checkpoint_restore_failed.Notebook', function () {
204 nnw.set_message("Checkpoint restore failed");
214 nnw.set_message("Checkpoint restore failed");
205 });
215 });
206
216
207 // Autosave events
217 // Autosave events
208 $([IPython.events]).on('autosave_disabled.Notebook', function () {
218 $([IPython.events]).on('autosave_disabled.Notebook', function () {
209 nnw.set_message("Autosave disabled", 2000);
219 nnw.set_message("Autosave disabled", 2000);
210 });
220 });
211 $([IPython.events]).on('autosave_enabled.Notebook', function (evt, interval) {
221 $([IPython.events]).on('autosave_enabled.Notebook', function (evt, interval) {
212 nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
222 nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
213 });
223 });
214
224
215 };
225 };
216
226
217 IPython.NotificationArea = NotificationArea;
227 IPython.NotificationArea = NotificationArea;
218
228
219 return IPython;
229 return IPython;
220
230
221 }(IPython));
231 }(IPython));
222
232
General Comments 0
You need to be logged in to leave comments. Login now