Show More
@@ -1,133 +1,135 b'' | |||
|
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 | 'jquery', |
|
6 | 6 | 'base/js/namespace', |
|
7 | 7 | 'base/js/utils', |
|
8 | 8 | 'base/js/dialog', |
|
9 | 9 | ], function ($, IPython, utils, dialog) { |
|
10 | 10 | "use strict"; |
|
11 | 11 | |
|
12 | 12 | var NewNotebookWidget = function (selector, options) { |
|
13 | 13 | this.selector = selector; |
|
14 | 14 | this.base_url = options.base_url; |
|
15 | 15 | this.notebook_path = options.notebook_path; |
|
16 | 16 | this.contents = options.contents; |
|
17 | 17 | this.current_selection = null; |
|
18 | 18 | this.config = options.config; |
|
19 | 19 | this.kernelspecs = {}; |
|
20 | 20 | if (this.selector !== undefined) { |
|
21 | 21 | this.element = $(selector); |
|
22 | 22 | this.request_kernelspecs(); |
|
23 | 23 | } |
|
24 | 24 | this.bind_events(); |
|
25 | 25 | }; |
|
26 | 26 | |
|
27 | 27 | NewNotebookWidget.prototype.bind_events = function () { |
|
28 | 28 | var that = this; |
|
29 | 29 | this.element.find('#new_notebook').click(function () { |
|
30 | 30 | that.new_notebook(); |
|
31 | 31 | }); |
|
32 | 32 | }; |
|
33 | 33 | |
|
34 | 34 | NewNotebookWidget.prototype.request_kernelspecs = function () { |
|
35 | 35 | /** request and then load kernel specs */ |
|
36 | 36 | var url = utils.url_join_encode(this.base_url, 'api/kernelspecs'); |
|
37 | 37 | utils.promising_ajax(url).then($.proxy(this._load_kernelspecs, this)); |
|
38 | 38 | }; |
|
39 | 39 | |
|
40 | 40 | NewNotebookWidget.prototype._load_kernelspecs = function (data) { |
|
41 | 41 | /** load kernelspec list */ |
|
42 | 42 | var that = this; |
|
43 | 43 | this.kernelspecs = data.kernelspecs; |
|
44 | 44 | var menu = this.element.find("#new-notebook-menu"); |
|
45 | 45 | var keys = Object.keys(data.kernelspecs).sort(function (a, b) { |
|
46 | 46 | var da = data.kernelspecs[a].display_name; |
|
47 | 47 | var db = data.kernelspecs[b].display_name; |
|
48 | 48 | if (da === db) { |
|
49 | 49 | return 0; |
|
50 | 50 | } else if (da > db) { |
|
51 | 51 | return 1; |
|
52 | 52 | } else { |
|
53 | 53 | return -1; |
|
54 | 54 | } |
|
55 | 55 | }); |
|
56 | 56 | for (var i = 0; i < keys.length; i++) { |
|
57 | 57 | var ks = this.kernelspecs[keys[i]]; |
|
58 | 58 | var li = $("<li>") |
|
59 | 59 | .attr("id", "kernel-" +ks.name) |
|
60 | 60 | .data('kernelspec', ks).append( |
|
61 | 61 | $('<a>') |
|
62 | 62 | .attr('href', '#') |
|
63 | 63 | .click($.proxy(this.new_notebook, this, ks.name)) |
|
64 | 64 | .text(ks.display_name) |
|
65 | 65 | .attr('title', 'Create a new notebook with ' + ks.display_name) |
|
66 | 66 | ); |
|
67 | 67 | menu.append(li); |
|
68 | 68 | } |
|
69 | 69 | this.config.loaded.then(function () { |
|
70 | 70 | that._load_default_kernelspec(data['default']); |
|
71 | 71 | }); |
|
72 | 72 | }; |
|
73 | 73 | |
|
74 | 74 | NewNotebookWidget.prototype._load_default_kernelspec = function (default_name) { |
|
75 | 75 | /** load default kernelspec name from config, if defined */ |
|
76 | 76 | if (this.config.data.NewNotebookWidget && |
|
77 | 77 | this.config.data.NewNotebookWidget.current_selection && |
|
78 | 78 | this.kernelspecs[this.config.data.NewNotebookWidget.current_selection] !== undefined |
|
79 | 79 | ) { |
|
80 | 80 | default_name = this.config.data.NewNotebookWidget.current_selection; |
|
81 | 81 | } |
|
82 | 82 | this.set_default_kernel(default_name); |
|
83 | 83 | }; |
|
84 | 84 | |
|
85 | 85 | NewNotebookWidget.prototype.set_default_kernel = function (kernel_name) { |
|
86 | 86 | /** select the current default kernel */ |
|
87 | 87 | this.current_selection = kernel_name; |
|
88 | 88 | this.config.update({ |
|
89 | 89 | NewNotebookWidget: { |
|
90 | 90 | current_selection: kernel_name |
|
91 | 91 | } |
|
92 | 92 | }); |
|
93 | 93 | var spec = this.kernelspecs[kernel_name]; |
|
94 | 94 | var display_name; |
|
95 | 95 | if (spec) { |
|
96 | 96 | display_name = spec.display_name; |
|
97 |
this.element.find("#current-kernel") |
|
|
97 | this.element.find("#current-kernel") | |
|
98 | .text(display_name) | |
|
99 | .attr('title', display_name + " is the default kernel for new notebooks"); | |
|
98 | 100 | } else { |
|
99 | 101 | display_name = 'default kernel'; |
|
100 | 102 | } |
|
101 | 103 | this.element.find("#new_notebook").attr('title', |
|
102 | 104 | 'Create a new notebook with ' + display_name |
|
103 | 105 | ); |
|
104 | 106 | }; |
|
105 | 107 | |
|
106 | 108 | NewNotebookWidget.prototype.new_notebook = function (kernel_name) { |
|
107 | 109 | /** create and open a new notebook */ |
|
108 | 110 | var that = this; |
|
109 | 111 | kernel_name = kernel_name || this.current_selection; |
|
110 | 112 | var w = window.open(); |
|
111 | 113 | this.contents.new_untitled(that.notebook_path, {type: "notebook"}).then( |
|
112 | 114 | function (data) { |
|
113 | 115 | var url = utils.url_join_encode( |
|
114 | 116 | that.base_url, 'notebooks', data.path |
|
115 | 117 | ); |
|
116 | 118 | if (kernel_name) { |
|
117 | 119 | url += "?kernel_name=" + kernel_name; |
|
118 | 120 | } |
|
119 | 121 | w.location = url; |
|
120 | 122 | }, |
|
121 | 123 | function (error) { |
|
122 | 124 | w.close(); |
|
123 | 125 | dialog.modal({ |
|
124 | 126 | title : 'Creating Notebook Failed', |
|
125 | 127 | body : "The error was: " + error.message, |
|
126 | 128 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
127 | 129 | }); |
|
128 | 130 | } |
|
129 | 131 | ); |
|
130 | 132 | }; |
|
131 | 133 | |
|
132 | 134 | return {'NewNotebookWidget': NewNotebookWidget}; |
|
133 | 135 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now