##// END OF EJS Templates
allow notebook tour instantiation to fail...
MinRK -
Show More
@@ -1,123 +1,127 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2011 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 // On document ready
10 10 //============================================================================
11 11
12 12 // for the time beeing, we have to pass marked as a parameter here,
13 13 // as injecting require.js make marked not to put itself in the globals,
14 14 // which make both this file fail at setting marked configuration, and textcell.js
15 15 // which search marked into global.
16 16 require(['components/marked/lib/marked',
17 17 'widgets/js/init',
18 18 'components/bootstrap-tour/build/js/bootstrap-tour.min'],
19 19
20 20 function (marked) {
21 21 "use strict";
22 22
23 23 window.marked = marked;
24 24
25 25 // monkey patch CM to be able to syntax highlight cell magics
26 26 // bug reported upstream,
27 27 // see https://github.com/marijnh/CodeMirror2/issues/670
28 28 if(CodeMirror.getMode(1,'text/plain').indent === undefined ){
29 29 console.log('patching CM for undefined indent');
30 30 CodeMirror.modes.null = function() {
31 31 return {token: function(stream) {stream.skipToEnd();},indent : function(){return 0;}};
32 32 };
33 33 }
34 34
35 35 CodeMirror.patchedGetMode = function(config, mode){
36 36 var cmmode = CodeMirror.getMode(config, mode);
37 37 if(cmmode.indent === null) {
38 38 console.log('patch mode "' , mode, '" on the fly');
39 39 cmmode.indent = function(){return 0;};
40 40 }
41 41 return cmmode;
42 42 };
43 43 // end monkey patching CodeMirror
44 44
45 45 IPython.mathjaxutils.init();
46 46
47 47 $('#ipython-main-app').addClass('border-box-sizing');
48 48 $('div#notebook_panel').addClass('border-box-sizing');
49 49
50 50 var opts = {
51 51 base_url : IPython.utils.get_body_data("baseUrl"),
52 52 notebook_path : IPython.utils.get_body_data("notebookPath"),
53 53 notebook_name : IPython.utils.get_body_data('notebookName')
54 54 };
55 55
56 56 IPython.page = new IPython.Page();
57 57 IPython.layout_manager = new IPython.LayoutManager();
58 58 IPython.pager = new IPython.Pager('div#pager', 'div#pager_splitter');
59 59 IPython.quick_help = new IPython.QuickHelp();
60 IPython.tour = new IPython.NotebookTour();
60 try {
61 IPython.tour = new IPython.NotebookTour();
62 } catch (e) {
63 console.log("Failed to instantiate Notebook Tour", e);
64 }
61 65 IPython.login_widget = new IPython.LoginWidget('span#login_widget', opts);
62 66 IPython.notebook = new IPython.Notebook('div#notebook', opts);
63 67 IPython.keyboard_manager = new IPython.KeyboardManager();
64 68 IPython.save_widget = new IPython.SaveWidget('span#save_widget');
65 69 IPython.menubar = new IPython.MenuBar('#menubar', opts);
66 70 IPython.toolbar = new IPython.MainToolBar('#maintoolbar-container');
67 71 IPython.tooltip = new IPython.Tooltip();
68 72 IPython.notification_area = new IPython.NotificationArea('#notification_area');
69 73 IPython.notification_area.init_notification_widgets();
70 74
71 75 IPython.layout_manager.do_resize();
72 76
73 77 $('body').append('<div id="fonttest"><pre><span id="test1">x</span>'+
74 78 '<span id="test2" style="font-weight: bold;">x</span>'+
75 79 '<span id="test3" style="font-style: italic;">x</span></pre></div>');
76 80 var nh = $('#test1').innerHeight();
77 81 var bh = $('#test2').innerHeight();
78 82 var ih = $('#test3').innerHeight();
79 83 if(nh != bh || nh != ih) {
80 84 $('head').append('<style>.CodeMirror span { vertical-align: bottom; }</style>');
81 85 }
82 86 $('#fonttest').remove();
83 87
84 88 IPython.page.show();
85 89
86 90 IPython.layout_manager.do_resize();
87 91 var first_load = function () {
88 92 IPython.layout_manager.do_resize();
89 93 var hash = document.location.hash;
90 94 if (hash) {
91 95 document.location.hash = '';
92 96 document.location.hash = hash;
93 97 }
94 98 IPython.notebook.set_autosave_interval(IPython.notebook.minimum_autosave_interval);
95 99 // only do this once
96 100 $([IPython.events]).off('notebook_loaded.Notebook', first_load);
97 101 };
98 102
99 103 $([IPython.events]).on('notebook_loaded.Notebook', first_load);
100 104 $([IPython.events]).trigger('app_initialized.NotebookApp');
101 105 IPython.notebook.load_notebook(opts.notebook_name, opts.notebook_path);
102 106
103 107 if (marked) {
104 108 marked.setOptions({
105 109 gfm : true,
106 110 tables: true,
107 111 langPrefix: "language-",
108 112 highlight: function(code, lang) {
109 113 if (!lang) {
110 114 // no language, no highlight
111 115 return code;
112 116 }
113 117 var highlighted;
114 118 try {
115 119 highlighted = hljs.highlight(lang, code, false);
116 120 } catch(err) {
117 121 highlighted = hljs.highlightAuto(code);
118 122 }
119 123 return highlighted.value;
120 124 }
121 125 });
122 126 }
123 127 });
@@ -1,335 +1,339 b''
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 //============================================================================
5 5 // MenuBar
6 6 //============================================================================
7 7
8 8 /**
9 9 * @module IPython
10 10 * @namespace IPython
11 11 * @submodule MenuBar
12 12 */
13 13
14 14
15 15 var IPython = (function (IPython) {
16 16 "use strict";
17 17
18 18 var utils = IPython.utils;
19 19
20 20 /**
21 21 * A MenuBar Class to generate the menubar of IPython notebook
22 22 * @Class MenuBar
23 23 *
24 24 * @constructor
25 25 *
26 26 *
27 27 * @param selector {string} selector for the menubar element in DOM
28 28 * @param {object} [options]
29 29 * @param [options.base_url] {String} String to use for the
30 30 * base project url. Default is to inspect
31 31 * $('body').data('baseUrl');
32 32 * does not support change for now is set through this option
33 33 */
34 34 var MenuBar = function (selector, options) {
35 35 options = options || {};
36 36 this.base_url = options.base_url || IPython.utils.get_body_data("baseUrl");
37 37 this.selector = selector;
38 38 if (this.selector !== undefined) {
39 39 this.element = $(selector);
40 40 this.style();
41 41 this.bind_events();
42 42 }
43 43 };
44 44
45 45 MenuBar.prototype.style = function () {
46 46 this.element.addClass('border-box-sizing');
47 47 this.element.find("li").click(function (event, ui) {
48 48 // The selected cell loses focus when the menu is entered, so we
49 49 // re-select it upon selection.
50 50 var i = IPython.notebook.get_selected_index();
51 51 IPython.notebook.select(i);
52 52 }
53 53 );
54 54 };
55 55
56 56 MenuBar.prototype._nbconvert = function (format, download) {
57 57 download = download || false;
58 58 var notebook_path = IPython.notebook.notebook_path;
59 59 var notebook_name = IPython.notebook.notebook_name;
60 60 if (IPython.notebook.dirty) {
61 61 IPython.notebook.save_notebook({async : false});
62 62 }
63 63 var url = utils.url_join_encode(
64 64 this.base_url,
65 65 'nbconvert',
66 66 format,
67 67 notebook_path,
68 68 notebook_name
69 69 ) + "?download=" + download.toString();
70 70
71 71 window.open(url);
72 72 };
73 73
74 74 MenuBar.prototype.bind_events = function () {
75 75 // File
76 76 var that = this;
77 77 this.element.find('#new_notebook').click(function () {
78 78 IPython.notebook.new_notebook();
79 79 });
80 80 this.element.find('#open_notebook').click(function () {
81 81 window.open(utils.url_join_encode(
82 82 IPython.notebook.base_url,
83 83 'tree',
84 84 IPython.notebook.notebook_path
85 85 ));
86 86 });
87 87 this.element.find('#copy_notebook').click(function () {
88 88 IPython.notebook.copy_notebook();
89 89 return false;
90 90 });
91 91 this.element.find('#download_ipynb').click(function () {
92 92 var base_url = IPython.notebook.base_url;
93 93 var notebook_path = IPython.notebook.notebook_path;
94 94 var notebook_name = IPython.notebook.notebook_name;
95 95 if (IPython.notebook.dirty) {
96 96 IPython.notebook.save_notebook({async : false});
97 97 }
98 98
99 99 var url = utils.url_join_encode(
100 100 base_url,
101 101 'files',
102 102 notebook_path,
103 103 notebook_name
104 104 );
105 105 window.location.assign(url);
106 106 });
107 107
108 108 this.element.find('#print_preview').click(function () {
109 109 that._nbconvert('html', false);
110 110 });
111 111
112 112 this.element.find('#download_py').click(function () {
113 113 that._nbconvert('python', true);
114 114 });
115 115
116 116 this.element.find('#download_html').click(function () {
117 117 that._nbconvert('html', true);
118 118 });
119 119
120 120 this.element.find('#download_rst').click(function () {
121 121 that._nbconvert('rst', true);
122 122 });
123 123
124 124 this.element.find('#download_pdf').click(function () {
125 125 that._nbconvert('pdf', true);
126 126 });
127 127
128 128 this.element.find('#rename_notebook').click(function () {
129 129 IPython.save_widget.rename_notebook();
130 130 });
131 131 this.element.find('#save_checkpoint').click(function () {
132 132 IPython.notebook.save_checkpoint();
133 133 });
134 134 this.element.find('#restore_checkpoint').click(function () {
135 135 });
136 136 this.element.find('#trust_notebook').click(function () {
137 137 IPython.notebook.trust_notebook();
138 138 });
139 139 $([IPython.events]).on('trust_changed.Notebook', function (event, trusted) {
140 140 if (trusted) {
141 141 that.element.find('#trust_notebook')
142 142 .addClass("disabled")
143 143 .find("a").text("Trusted Notebook");
144 144 } else {
145 145 that.element.find('#trust_notebook')
146 146 .removeClass("disabled")
147 147 .find("a").text("Trust Notebook");
148 148 }
149 149 });
150 150 this.element.find('#kill_and_exit').click(function () {
151 151 IPython.notebook.session.delete();
152 152 setTimeout(function(){
153 153 // allow closing of new tabs in Chromium, impossible in FF
154 154 window.open('', '_self', '');
155 155 window.close();
156 156 }, 500);
157 157 });
158 158 // Edit
159 159 this.element.find('#cut_cell').click(function () {
160 160 IPython.notebook.cut_cell();
161 161 });
162 162 this.element.find('#copy_cell').click(function () {
163 163 IPython.notebook.copy_cell();
164 164 });
165 165 this.element.find('#delete_cell').click(function () {
166 166 IPython.notebook.delete_cell();
167 167 });
168 168 this.element.find('#undelete_cell').click(function () {
169 169 IPython.notebook.undelete_cell();
170 170 });
171 171 this.element.find('#split_cell').click(function () {
172 172 IPython.notebook.split_cell();
173 173 });
174 174 this.element.find('#merge_cell_above').click(function () {
175 175 IPython.notebook.merge_cell_above();
176 176 });
177 177 this.element.find('#merge_cell_below').click(function () {
178 178 IPython.notebook.merge_cell_below();
179 179 });
180 180 this.element.find('#move_cell_up').click(function () {
181 181 IPython.notebook.move_cell_up();
182 182 });
183 183 this.element.find('#move_cell_down').click(function () {
184 184 IPython.notebook.move_cell_down();
185 185 });
186 186 this.element.find('#edit_nb_metadata').click(function () {
187 187 IPython.notebook.edit_metadata();
188 188 });
189 189
190 190 // View
191 191 this.element.find('#toggle_header').click(function () {
192 192 $('div#header').toggle();
193 193 IPython.layout_manager.do_resize();
194 194 });
195 195 this.element.find('#toggle_toolbar').click(function () {
196 196 $('div#maintoolbar').toggle();
197 197 IPython.layout_manager.do_resize();
198 198 });
199 199 // Insert
200 200 this.element.find('#insert_cell_above').click(function () {
201 201 IPython.notebook.insert_cell_above('code');
202 202 IPython.notebook.select_prev();
203 203 });
204 204 this.element.find('#insert_cell_below').click(function () {
205 205 IPython.notebook.insert_cell_below('code');
206 206 IPython.notebook.select_next();
207 207 });
208 208 // Cell
209 209 this.element.find('#run_cell').click(function () {
210 210 IPython.notebook.execute_cell();
211 211 });
212 212 this.element.find('#run_cell_select_below').click(function () {
213 213 IPython.notebook.execute_cell_and_select_below();
214 214 });
215 215 this.element.find('#run_cell_insert_below').click(function () {
216 216 IPython.notebook.execute_cell_and_insert_below();
217 217 });
218 218 this.element.find('#run_all_cells').click(function () {
219 219 IPython.notebook.execute_all_cells();
220 220 });
221 221 this.element.find('#run_all_cells_above').click(function () {
222 222 IPython.notebook.execute_cells_above();
223 223 });
224 224 this.element.find('#run_all_cells_below').click(function () {
225 225 IPython.notebook.execute_cells_below();
226 226 });
227 227 this.element.find('#to_code').click(function () {
228 228 IPython.notebook.to_code();
229 229 });
230 230 this.element.find('#to_markdown').click(function () {
231 231 IPython.notebook.to_markdown();
232 232 });
233 233 this.element.find('#to_raw').click(function () {
234 234 IPython.notebook.to_raw();
235 235 });
236 236 this.element.find('#to_heading1').click(function () {
237 237 IPython.notebook.to_heading(undefined, 1);
238 238 });
239 239 this.element.find('#to_heading2').click(function () {
240 240 IPython.notebook.to_heading(undefined, 2);
241 241 });
242 242 this.element.find('#to_heading3').click(function () {
243 243 IPython.notebook.to_heading(undefined, 3);
244 244 });
245 245 this.element.find('#to_heading4').click(function () {
246 246 IPython.notebook.to_heading(undefined, 4);
247 247 });
248 248 this.element.find('#to_heading5').click(function () {
249 249 IPython.notebook.to_heading(undefined, 5);
250 250 });
251 251 this.element.find('#to_heading6').click(function () {
252 252 IPython.notebook.to_heading(undefined, 6);
253 253 });
254 254
255 255 this.element.find('#toggle_current_output').click(function () {
256 256 IPython.notebook.toggle_output();
257 257 });
258 258 this.element.find('#toggle_current_output_scroll').click(function () {
259 259 IPython.notebook.toggle_output_scroll();
260 260 });
261 261 this.element.find('#clear_current_output').click(function () {
262 262 IPython.notebook.clear_output();
263 263 });
264 264
265 265 this.element.find('#toggle_all_output').click(function () {
266 266 IPython.notebook.toggle_all_output();
267 267 });
268 268 this.element.find('#toggle_all_output_scroll').click(function () {
269 269 IPython.notebook.toggle_all_output_scroll();
270 270 });
271 271 this.element.find('#clear_all_output').click(function () {
272 272 IPython.notebook.clear_all_output();
273 273 });
274 274
275 275 // Kernel
276 276 this.element.find('#int_kernel').click(function () {
277 277 IPython.notebook.session.interrupt_kernel();
278 278 });
279 279 this.element.find('#restart_kernel').click(function () {
280 280 IPython.notebook.restart_kernel();
281 281 });
282 282 // Help
283 this.element.find('#notebook_tour').click(function () {
284 IPython.tour.start();
285 });
283 if (IPython.tour) {
284 this.element.find('#notebook_tour').click(function () {
285 IPython.tour.start();
286 });
287 } else {
288 this.element.find('#notebook_tour').addClass("disabled");
289 }
286 290 this.element.find('#keyboard_shortcuts').click(function () {
287 291 IPython.quick_help.show_keyboard_shortcuts();
288 292 });
289 293
290 294 this.update_restore_checkpoint(null);
291 295
292 296 $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
293 297 that.update_restore_checkpoint(IPython.notebook.checkpoints);
294 298 });
295 299
296 300 $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
297 301 that.update_restore_checkpoint(IPython.notebook.checkpoints);
298 302 });
299 303 };
300 304
301 305 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
302 306 var ul = this.element.find("#restore_checkpoint").find("ul");
303 307 ul.empty();
304 308 if (!checkpoints || checkpoints.length === 0) {
305 309 ul.append(
306 310 $("<li/>")
307 311 .addClass("disabled")
308 312 .append(
309 313 $("<a/>")
310 314 .text("No checkpoints")
311 315 )
312 316 );
313 317 return;
314 318 }
315 319
316 320 checkpoints.map(function (checkpoint) {
317 321 var d = new Date(checkpoint.last_modified);
318 322 ul.append(
319 323 $("<li/>").append(
320 324 $("<a/>")
321 325 .attr("href", "#")
322 326 .text(d.format("mmm dd HH:MM:ss"))
323 327 .click(function () {
324 328 IPython.notebook.restore_checkpoint_dialog(checkpoint);
325 329 })
326 330 )
327 331 );
328 332 });
329 333 };
330 334
331 335 IPython.MenuBar = MenuBar;
332 336
333 337 return IPython;
334 338
335 339 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now