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