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