Show More
@@ -1,97 +1,97 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.default_kernel = null; |
|
17 | this.default_kernel = null; | |
18 | this.kernelspecs = {}; |
|
18 | this.kernelspecs = {}; | |
19 | if (this.selector !== undefined) { |
|
19 | if (this.selector !== undefined) { | |
20 | this.element = $(selector); |
|
20 | this.element = $(selector); | |
21 | this.request_kernelspecs(); |
|
21 | this.request_kernelspecs(); | |
22 | } |
|
22 | } | |
23 | this.bind_events(); |
|
23 | this.bind_events(); | |
24 | }; |
|
24 | }; | |
25 |
|
25 | |||
26 | NewNotebookWidget.prototype.bind_events = function () { |
|
26 | NewNotebookWidget.prototype.bind_events = function () { | |
27 | var that = this; |
|
27 | var that = this; | |
28 | this.element.find('#new_notebook').click(function () { |
|
28 | this.element.find('#new_notebook').click(function () { | |
29 | that.new_notebook(); |
|
29 | that.new_notebook(); | |
30 | }); |
|
30 | }); | |
31 | }; |
|
31 | }; | |
32 |
|
32 | |||
33 | NewNotebookWidget.prototype.request_kernelspecs = function () { |
|
33 | NewNotebookWidget.prototype.request_kernelspecs = function () { | |
34 | /** request and then load kernel specs */ |
|
34 | /** request and then load kernel specs */ | |
35 | var url = utils.url_join_encode(this.base_url, 'api/kernelspecs'); |
|
35 | var url = utils.url_join_encode(this.base_url, 'api/kernelspecs'); | |
36 | utils.promising_ajax(url).then($.proxy(this._load_kernelspecs, this)); |
|
36 | utils.promising_ajax(url).then($.proxy(this._load_kernelspecs, this)); | |
37 | }; |
|
37 | }; | |
38 |
|
38 | |||
39 | NewNotebookWidget.prototype._load_kernelspecs = function (data) { |
|
39 | NewNotebookWidget.prototype._load_kernelspecs = function (data) { | |
40 | /** load kernelspec list */ |
|
40 | /** load kernelspec list */ | |
41 | var that = this; |
|
41 | var that = this; | |
42 | this.kernelspecs = data.kernelspecs; |
|
42 | this.kernelspecs = data.kernelspecs; | |
43 | var menu = this.element.find("#notebook-kernels"); |
|
43 | var menu = this.element.find("#notebook-kernels"); | |
44 | var keys = Object.keys(data.kernelspecs).sort(function (a, b) { |
|
44 | var keys = Object.keys(data.kernelspecs).sort(function (a, b) { | |
45 | var da = data.kernelspecs[a].display_name; |
|
45 | var da = data.kernelspecs[a].spec.display_name; | |
46 | var db = data.kernelspecs[b].display_name; |
|
46 | var db = data.kernelspecs[b].spec.display_name; | |
47 | if (da === db) { |
|
47 | if (da === db) { | |
48 | return 0; |
|
48 | return 0; | |
49 | } else if (da > db) { |
|
49 | } else if (da > db) { | |
50 | return 1; |
|
50 | return 1; | |
51 | } else { |
|
51 | } else { | |
52 | return -1; |
|
52 | return -1; | |
53 | } |
|
53 | } | |
54 | }); |
|
54 | }); | |
55 | for (var i = 0; i < keys.length; i++) { |
|
55 | for (var i = 0; i < keys.length; i++) { | |
56 | var ks = this.kernelspecs[keys[i]]; |
|
56 | var ks = this.kernelspecs[keys[i]]; | |
57 | var li = $("<li>") |
|
57 | var li = $("<li>") | |
58 | .attr("id", "kernel-" +ks.name) |
|
58 | .attr("id", "kernel-" +ks.name) | |
59 | .data('kernelspec', ks).append( |
|
59 | .data('kernelspec', ks).append( | |
60 | $('<a>') |
|
60 | $('<a>') | |
61 | .attr('href', '#') |
|
61 | .attr('href', '#') | |
62 | .click($.proxy(this.new_notebook, this, ks.name)) |
|
62 | .click($.proxy(this.new_notebook, this, ks.name)) | |
63 | .text(ks.display_name) |
|
63 | .text(ks.spec.display_name) | |
64 | .attr('title', 'Create a new notebook with ' + ks.display_name) |
|
64 | .attr('title', 'Create a new notebook with ' + ks.spec.display_name) | |
65 | ); |
|
65 | ); | |
66 | menu.after(li); |
|
66 | menu.after(li); | |
67 | } |
|
67 | } | |
68 | }; |
|
68 | }; | |
69 |
|
69 | |||
70 | NewNotebookWidget.prototype.new_notebook = function (kernel_name) { |
|
70 | NewNotebookWidget.prototype.new_notebook = function (kernel_name) { | |
71 | /** create and open a new notebook */ |
|
71 | /** create and open a new notebook */ | |
72 | var that = this; |
|
72 | var that = this; | |
73 | kernel_name = kernel_name || this.default_kernel; |
|
73 | kernel_name = kernel_name || this.default_kernel; | |
74 | var w = window.open(); |
|
74 | var w = window.open(); | |
75 | this.contents.new_untitled(that.notebook_path, {type: "notebook"}).then( |
|
75 | this.contents.new_untitled(that.notebook_path, {type: "notebook"}).then( | |
76 | function (data) { |
|
76 | function (data) { | |
77 | var url = utils.url_join_encode( |
|
77 | var url = utils.url_join_encode( | |
78 | that.base_url, 'notebooks', data.path |
|
78 | that.base_url, 'notebooks', data.path | |
79 | ); |
|
79 | ); | |
80 | if (kernel_name) { |
|
80 | if (kernel_name) { | |
81 | url += "?kernel_name=" + kernel_name; |
|
81 | url += "?kernel_name=" + kernel_name; | |
82 | } |
|
82 | } | |
83 | w.location = url; |
|
83 | w.location = url; | |
84 | }, |
|
84 | }, | |
85 | function (error) { |
|
85 | function (error) { | |
86 | w.close(); |
|
86 | w.close(); | |
87 | dialog.modal({ |
|
87 | dialog.modal({ | |
88 | title : 'Creating Notebook Failed', |
|
88 | title : 'Creating Notebook Failed', | |
89 | body : "The error was: " + error.message, |
|
89 | body : "The error was: " + error.message, | |
90 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
90 | buttons : {'OK' : {'class' : 'btn-primary'}} | |
91 | }); |
|
91 | }); | |
92 | } |
|
92 | } | |
93 | ); |
|
93 | ); | |
94 | }; |
|
94 | }; | |
95 |
|
95 | |||
96 | return {'NewNotebookWidget': NewNotebookWidget}; |
|
96 | return {'NewNotebookWidget': NewNotebookWidget}; | |
97 | }); |
|
97 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now