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