##// END OF EJS Templates
more cleanup
Matthias BUSSONNIER -
Show More
@@ -1,244 +1,244
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'base/js/dialog',
8 'base/js/dialog',
9 'notebook/js/notificationwidget',
9 'notebook/js/notificationwidget',
10 ], function(IPython, $, utils, dialog, notificationwidget) {
10 ], function(IPython, $, utils, dialog, notificationwidget) {
11 "use strict";
11 "use strict";
12
12
13 var NotificationArea = function (selector, options) {
13 var NotificationArea = function (selector, options) {
14 // Constructor
14 // Constructor
15 //
15 //
16 // Parameters:
16 // Parameters:
17 // selector: string
17 // selector: string
18 // options: dictionary
18 // options: dictionary
19 // Dictionary of keyword arguments.
19 // Dictionary of keyword arguments.
20 // notebook: Notebook instance
20 // notebook: Notebook instance
21 // events: $(Events) instance
21 // events: $(Events) instance
22 // save_widget: SaveWidget instance
22 // save_widget: SaveWidget instance
23 this.selector = selector;
23 this.selector = selector;
24 this.events = options.events;
24 this.events = options.events;
25 this.save_widget = options.save_widget;
25 this.save_widget = options.save_widget;
26 this.notebook = options.notebook;
26 this.notebook = options.notebook;
27 this.keyboard_manager = options.keyboard_manager;
27 this.keyboard_manager = options.keyboard_manager;
28 if (this.selector !== undefined) {
28 if (this.selector !== undefined) {
29 this.element = $(selector);
29 this.element = $(selector);
30 }
30 }
31 this.widget_dict = {};
31 this.widget_dict = {};
32 };
32 };
33
33
34 NotificationArea.prototype.temp_message = function (msg, timeout, css_class) {
34 NotificationArea.prototype.temp_message = function (msg, timeout, css_class) {
35 if( css_class == 'danger') {css_class = 'ui-state-error';}
35 if( css_class == 'danger') {css_class = 'ui-state-error';}
36 if( css_class == 'warning') {css_class = 'ui-state-highlight';}
36 if( css_class == 'warning') {css_class = 'ui-state-highlight';}
37 var tdiv = $('<div>')
37 var tdiv = $('<div>')
38 .addClass('notification_widget ui-widget ui-widget-content ui-corner-all')
38 .addClass('notification_widget')
39 .addClass(css_class)
39 .addClass(css_class)
40 .hide()
40 .hide()
41 .text(msg);
41 .text(msg);
42
42
43 $(this.selector).append(tdiv);
43 $(this.selector).append(tdiv);
44 var tmout = Math.max(1500,(timeout||1500));
44 var tmout = Math.max(1500,(timeout||1500));
45 tdiv.fadeIn(100);
45 tdiv.fadeIn(100);
46
46
47 setTimeout(function () {
47 setTimeout(function () {
48 tdiv.fadeOut(100, function () {tdiv.remove();});
48 tdiv.fadeOut(100, function () {tdiv.remove();});
49 }, tmout);
49 }, tmout);
50 };
50 };
51
51
52 NotificationArea.prototype.widget = function(name) {
52 NotificationArea.prototype.widget = function(name) {
53 if(this.widget_dict[name] === undefined) {
53 if(this.widget_dict[name] === undefined) {
54 return this.new_notification_widget(name);
54 return this.new_notification_widget(name);
55 }
55 }
56 return this.get_widget(name);
56 return this.get_widget(name);
57 };
57 };
58
58
59 NotificationArea.prototype.get_widget = function(name) {
59 NotificationArea.prototype.get_widget = function(name) {
60 if(this.widget_dict[name] === undefined) {
60 if(this.widget_dict[name] === undefined) {
61 throw('no widgets with this name');
61 throw('no widgets with this name');
62 }
62 }
63 return this.widget_dict[name];
63 return this.widget_dict[name];
64 };
64 };
65
65
66 NotificationArea.prototype.new_notification_widget = function(name) {
66 NotificationArea.prototype.new_notification_widget = function(name) {
67 if(this.widget_dict[name] !== undefined) {
67 if(this.widget_dict[name] !== undefined) {
68 throw('widget with that name already exists ! ');
68 throw('widget with that name already exists ! ');
69 }
69 }
70 var div = $('<div/>').attr('id','notification_'+name);
70 var div = $('<div/>').attr('id','notification_'+name);
71 $(this.selector).append(div);
71 $(this.selector).append(div);
72 this.widget_dict[name] = new notificationwidget.NotificationWidget('#notification_'+name);
72 this.widget_dict[name] = new notificationwidget.NotificationWidget('#notification_'+name);
73 return this.widget_dict[name];
73 return this.widget_dict[name];
74 };
74 };
75
75
76 NotificationArea.prototype.init_notification_widgets = function() {
76 NotificationArea.prototype.init_notification_widgets = function() {
77 var that = this;
77 var that = this;
78 var knw = this.new_notification_widget('kernel');
78 var knw = this.new_notification_widget('kernel');
79 var $kernel_ind_icon = $("#kernel_indicator_icon");
79 var $kernel_ind_icon = $("#kernel_indicator_icon");
80 var $modal_ind_icon = $("#modal_indicator_icon");
80 var $modal_ind_icon = $("#modal_indicator_icon");
81
81
82 // Command/Edit mode
82 // Command/Edit mode
83 this.events.on('edit_mode.Notebook',function () {
83 this.events.on('edit_mode.Notebook',function () {
84 that.save_widget.update_document_title();
84 that.save_widget.update_document_title();
85 $modal_ind_icon.attr('class','edit_mode_icon').attr('title','Edit Mode');
85 $modal_ind_icon.attr('class','edit_mode_icon').attr('title','Edit Mode');
86 });
86 });
87
87
88 this.events.on('command_mode.Notebook',function () {
88 this.events.on('command_mode.Notebook',function () {
89 that.save_widget.update_document_title();
89 that.save_widget.update_document_title();
90 $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode');
90 $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode');
91 });
91 });
92
92
93 // Implicitly start off in Command mode, switching to Edit mode will trigger event
93 // Implicitly start off in Command mode, switching to Edit mode will trigger event
94 $modal_ind_icon.attr('class','command-mode_icon').attr('title','Command Mode');
94 $modal_ind_icon.attr('class','command-mode_icon').attr('title','Command Mode');
95
95
96 // Kernel events
96 // Kernel events
97 this.events.on('status_idle.Kernel',function () {
97 this.events.on('status_idle.Kernel',function () {
98 that.save_widget.update_document_title();
98 that.save_widget.update_document_title();
99 $kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle');
99 $kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle');
100 });
100 });
101
101
102 this.events.on('status_busy.Kernel',function () {
102 this.events.on('status_busy.Kernel',function () {
103 window.document.title='(Busy) '+window.document.title;
103 window.document.title='(Busy) '+window.document.title;
104 $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
104 $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
105 });
105 });
106
106
107 this.events.on('status_restarting.Kernel',function () {
107 this.events.on('status_restarting.Kernel',function () {
108 that.save_widget.update_document_title();
108 that.save_widget.update_document_title();
109 knw.set_message("Restarting kernel", 2000);
109 knw.set_message("Restarting kernel", 2000);
110 });
110 });
111
111
112 this.events.on('status_interrupting.Kernel',function () {
112 this.events.on('status_interrupting.Kernel',function () {
113 knw.set_message("Interrupting kernel", 2000);
113 knw.set_message("Interrupting kernel", 2000);
114 });
114 });
115
115
116 // Start the kernel indicator in the busy state, and send a kernel_info request.
116 // Start the kernel indicator in the busy state, and send a kernel_info request.
117 // When the kernel_info reply arrives, the kernel is idle.
117 // When the kernel_info reply arrives, the kernel is idle.
118 $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
118 $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
119
119
120 this.events.on('status_started.Kernel', function (evt, data) {
120 this.events.on('status_started.Kernel', function (evt, data) {
121 data.kernel.kernel_info(function () {
121 data.kernel.kernel_info(function () {
122 that.events.trigger('status_idle.Kernel');
122 that.events.trigger('status_idle.Kernel');
123 });
123 });
124 });
124 });
125
125
126 this.events.on('status_dead.Kernel',function () {
126 this.events.on('status_dead.Kernel',function () {
127 var msg = 'The kernel has died, and the automatic restart has failed.' +
127 var msg = 'The kernel has died, and the automatic restart has failed.' +
128 ' It is possible the kernel cannot be restarted.' +
128 ' It is possible the kernel cannot be restarted.' +
129 ' If you are not able to restart the kernel, you will still be able to save' +
129 ' If you are not able to restart the kernel, you will still be able to save' +
130 ' the notebook, but running code will no longer work until the notebook' +
130 ' the notebook, but running code will no longer work until the notebook' +
131 ' is reopened.';
131 ' is reopened.';
132
132
133 dialog.modal({
133 dialog.modal({
134 title: "Dead kernel",
134 title: "Dead kernel",
135 body : msg,
135 body : msg,
136 keyboard_manager: that.keyboard_manager,
136 keyboard_manager: that.keyboard_manager,
137 notebook: that.notebook,
137 notebook: that.notebook,
138 buttons : {
138 buttons : {
139 "Manual Restart": {
139 "Manual Restart": {
140 class: "btn-danger",
140 class: "btn-danger",
141 click: function () {
141 click: function () {
142 that.events.trigger('status_restarting.Kernel');
142 that.events.trigger('status_restarting.Kernel');
143 that.notebook.start_kernel();
143 that.notebook.start_kernel();
144 }
144 }
145 },
145 },
146 "Don't restart": {}
146 "Don't restart": {}
147 }
147 }
148 });
148 });
149 });
149 });
150
150
151 this.events.on('websocket_closed.Kernel', function (event, data) {
151 this.events.on('websocket_closed.Kernel', function (event, data) {
152 var kernel = data.kernel;
152 var kernel = data.kernel;
153 var ws_url = data.ws_url;
153 var ws_url = data.ws_url;
154 var early = data.early;
154 var early = data.early;
155 var msg;
155 var msg;
156 if (!early) {
156 if (!early) {
157 knw.set_message('Reconnecting WebSockets', 1000);
157 knw.set_message('Reconnecting WebSockets', 1000);
158 setTimeout(function () {
158 setTimeout(function () {
159 kernel.start_channels();
159 kernel.start_channels();
160 }, 5000);
160 }, 5000);
161 return;
161 return;
162 }
162 }
163 console.log('WebSocket connection failed: ', ws_url);
163 console.log('WebSocket connection failed: ', ws_url);
164 msg = "A WebSocket connection could not be established." +
164 msg = "A WebSocket connection could not be established." +
165 " You will NOT be able to run code. Check your" +
165 " You will NOT be able to run code. Check your" +
166 " network connection or notebook server configuration.";
166 " network connection or notebook server configuration.";
167 dialog.modal({
167 dialog.modal({
168 title: "WebSocket connection failed",
168 title: "WebSocket connection failed",
169 body: msg,
169 body: msg,
170 keyboard_manager: that.keyboard_manager,
170 keyboard_manager: that.keyboard_manager,
171 notebook: that.notebook,
171 notebook: that.notebook,
172 buttons : {
172 buttons : {
173 "OK": {},
173 "OK": {},
174 "Reconnect": {
174 "Reconnect": {
175 click: function () {
175 click: function () {
176 knw.set_message('Reconnecting WebSockets', 1000);
176 knw.set_message('Reconnecting WebSockets', 1000);
177 setTimeout(function () {
177 setTimeout(function () {
178 kernel.start_channels();
178 kernel.start_channels();
179 }, 5000);
179 }, 5000);
180 }
180 }
181 }
181 }
182 }
182 }
183 });
183 });
184 });
184 });
185
185
186
186
187 var nnw = this.new_notification_widget('notebook');
187 var nnw = this.new_notification_widget('notebook');
188
188
189 // Notebook events
189 // Notebook events
190 this.events.on('notebook_loading.Notebook', function () {
190 this.events.on('notebook_loading.Notebook', function () {
191 nnw.set_message("Loading notebook",500);
191 nnw.set_message("Loading notebook",500);
192 });
192 });
193 this.events.on('notebook_loaded.Notebook', function () {
193 this.events.on('notebook_loaded.Notebook', function () {
194 nnw.set_message("Notebook loaded",500);
194 nnw.set_message("Notebook loaded",500);
195 });
195 });
196 this.events.on('notebook_saving.Notebook', function () {
196 this.events.on('notebook_saving.Notebook', function () {
197 nnw.set_message("Saving notebook",500);
197 nnw.set_message("Saving notebook",500);
198 });
198 });
199 this.events.on('notebook_saved.Notebook', function () {
199 this.events.on('notebook_saved.Notebook', function () {
200 nnw.set_message("Notebook saved",2000);
200 nnw.set_message("Notebook saved",2000);
201 });
201 });
202 this.events.on('notebook_save_failed.Notebook', function (evt, xhr, status, data) {
202 this.events.on('notebook_save_failed.Notebook', function (evt, xhr, status, data) {
203 nnw.warning(data || "Notebook save failed");
203 nnw.warning(data || "Notebook save failed");
204 });
204 });
205
205
206 // Checkpoint events
206 // Checkpoint events
207 this.events.on('checkpoint_created.Notebook', function (evt, data) {
207 this.events.on('checkpoint_created.Notebook', function (evt, data) {
208 var msg = "Checkpoint created";
208 var msg = "Checkpoint created";
209 if (data.last_modified) {
209 if (data.last_modified) {
210 var d = new Date(data.last_modified);
210 var d = new Date(data.last_modified);
211 msg = msg + ": " + d.format("HH:MM:ss");
211 msg = msg + ": " + d.format("HH:MM:ss");
212 }
212 }
213 nnw.set_message(msg, 2000);
213 nnw.set_message(msg, 2000);
214 });
214 });
215 this.events.on('checkpoint_failed.Notebook', function () {
215 this.events.on('checkpoint_failed.Notebook', function () {
216 nnw.warning("Checkpoint failed");
216 nnw.warning("Checkpoint failed");
217 });
217 });
218 this.events.on('checkpoint_deleted.Notebook', function () {
218 this.events.on('checkpoint_deleted.Notebook', function () {
219 nnw.set_message("Checkpoint deleted", 500);
219 nnw.set_message("Checkpoint deleted", 500);
220 });
220 });
221 this.events.on('checkpoint_delete_failed.Notebook', function () {
221 this.events.on('checkpoint_delete_failed.Notebook', function () {
222 nnw.warning("Checkpoint delete failed");
222 nnw.warning("Checkpoint delete failed");
223 });
223 });
224 this.events.on('checkpoint_restoring.Notebook', function () {
224 this.events.on('checkpoint_restoring.Notebook', function () {
225 nnw.set_message("Restoring to checkpoint...", 500);
225 nnw.set_message("Restoring to checkpoint...", 500);
226 });
226 });
227 this.events.on('checkpoint_restore_failed.Notebook', function () {
227 this.events.on('checkpoint_restore_failed.Notebook', function () {
228 nnw.warning("Checkpoint restore failed");
228 nnw.warning("Checkpoint restore failed");
229 });
229 });
230
230
231 // Autosave events
231 // Autosave events
232 this.events.on('autosave_disabled.Notebook', function () {
232 this.events.on('autosave_disabled.Notebook', function () {
233 nnw.set_message("Autosave disabled", 2000);
233 nnw.set_message("Autosave disabled", 2000);
234 });
234 });
235 this.events.on('autosave_enabled.Notebook', function (evt, interval) {
235 this.events.on('autosave_enabled.Notebook', function (evt, interval) {
236 nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
236 nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
237 });
237 });
238
238
239 };
239 };
240
240
241 IPython.NotificationArea = NotificationArea;
241 IPython.NotificationArea = NotificationArea;
242
242
243 return {'NotificationArea': NotificationArea};
243 return {'NotificationArea': NotificationArea};
244 });
244 });
General Comments 0
You need to be logged in to leave comments. Login now