Show More
@@ -0,0 +1,112 b'' | |||||
|
1 | // Copyright (c) IPython Development Team. | |||
|
2 | // Distributed under the terms of the Modified BSD License. | |||
|
3 | ||||
|
4 | define([ | |||
|
5 | 'jquery', | |||
|
6 | 'base/js/namespace', | |||
|
7 | 'base/js/utils', | |||
|
8 | 'base/js/dialog', | |||
|
9 | ], function ($, IPython, utils, dialog) { | |||
|
10 | "use strict"; | |||
|
11 | ||||
|
12 | var NewNotebookWidget = function (selector, options) { | |||
|
13 | this.selector = selector; | |||
|
14 | this.base_url = options.base_url; | |||
|
15 | this.notebook_path = options.notebook_path; | |||
|
16 | this.contents = options.contents; | |||
|
17 | this.current_selection = null; | |||
|
18 | this.kernelspecs = {}; | |||
|
19 | if (this.selector !== undefined) { | |||
|
20 | this.element = $(selector); | |||
|
21 | this.request_kernelspecs(); | |||
|
22 | } | |||
|
23 | this.bind_events(); | |||
|
24 | }; | |||
|
25 | ||||
|
26 | NewNotebookWidget.prototype.bind_events = function () { | |||
|
27 | var that = this; | |||
|
28 | this.element.find('#new_notebook').click(function () { | |||
|
29 | that.new_notebook(); | |||
|
30 | }); | |||
|
31 | }; | |||
|
32 | ||||
|
33 | NewNotebookWidget.prototype.request_kernelspecs = function () { | |||
|
34 | /** request and then load kernel specs */ | |||
|
35 | var url = utils.url_join_encode(this.base_url, 'api/kernelspecs'); | |||
|
36 | utils.promising_ajax(url).then($.proxy(this._load_kernelspecs, this)); | |||
|
37 | }; | |||
|
38 | ||||
|
39 | NewNotebookWidget.prototype._load_kernelspecs = function (data) { | |||
|
40 | /** load kernelspec list */ | |||
|
41 | this.kernelspecs = {}; | |||
|
42 | var menu = this.element.find("#new-notebook-menu"); | |||
|
43 | for (var i = 0; i < data.length; i++) { | |||
|
44 | var ks = data[i]; | |||
|
45 | this.kernelspecs[ks.name] = ks; | |||
|
46 | var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>') | |||
|
47 | .attr('href', '#') | |||
|
48 | .click($.proxy(this.new_with_kernel, this, ks.name)) | |||
|
49 | .text(ks.display_name) | |||
|
50 | .attr('title', 'Create a new notebook with ' + ks.display_name) | |||
|
51 | ); | |||
|
52 | menu.append(ksentry); | |||
|
53 | } | |||
|
54 | this._load_default_kernelspec(); | |||
|
55 | }; | |||
|
56 | ||||
|
57 | NewNotebookWidget.prototype._load_default_kernelspec = function () { | |||
|
58 | /** load default kernelspec name from localStorage, if defined */ | |||
|
59 | this.select_kernel(localStorage.default_kernel_name); | |||
|
60 | }; | |||
|
61 | ||||
|
62 | NewNotebookWidget.prototype.select_kernel = function (kernel_name) { | |||
|
63 | /** select the current default kernel */ | |||
|
64 | this.current_selection = kernel_name; | |||
|
65 | var spec = this.kernelspecs[kernel_name]; | |||
|
66 | var display_name; | |||
|
67 | if (spec) { | |||
|
68 | display_name = spec.display_name; | |||
|
69 | localStorage.default_kernel_name = kernel_name; | |||
|
70 | } else { | |||
|
71 | display_name = 'default kernel'; | |||
|
72 | delete localStorage.default_kernel_name; | |||
|
73 | } | |||
|
74 | this.element.find("#new_notebook").attr('title', | |||
|
75 | 'Create a new notebook with ' + display_name | |||
|
76 | ); | |||
|
77 | }; | |||
|
78 | ||||
|
79 | NewNotebookWidget.prototype.new_with_kernel = function (kernel_name) { | |||
|
80 | /** record current selection and open a new notebook */ | |||
|
81 | this.select_kernel(kernel_name); | |||
|
82 | this.new_notebook(kernel_name); | |||
|
83 | }; | |||
|
84 | ||||
|
85 | NewNotebookWidget.prototype.new_notebook = function (kernel_name) { | |||
|
86 | /** create and open a new notebook */ | |||
|
87 | var that = this; | |||
|
88 | kernel_name = kernel_name || this.current_selection; | |||
|
89 | var w = window.open(); | |||
|
90 | this.contents.new_untitled(that.notebook_path, {type: "notebook"}).then( | |||
|
91 | function (data) { | |||
|
92 | var url = utils.url_join_encode( | |||
|
93 | that.base_url, 'notebooks', data.path | |||
|
94 | ); | |||
|
95 | if (kernel_name) { | |||
|
96 | url += "?kernel_name=" + kernel_name; | |||
|
97 | } | |||
|
98 | w.location = url; | |||
|
99 | }, | |||
|
100 | function (error) { | |||
|
101 | w.close(); | |||
|
102 | dialog.modal({ | |||
|
103 | title : 'Creating Notebook Failed', | |||
|
104 | body : "The error was: " + error.message, | |||
|
105 | buttons : {'OK' : {'class' : 'btn-primary'}} | |||
|
106 | }); | |||
|
107 | } | |||
|
108 | ); | |||
|
109 | }; | |||
|
110 | ||||
|
111 | return {'NewNotebookWidget': NewNotebookWidget}; | |||
|
112 | }); |
@@ -8164,6 +8164,10 b' input.engine_num_input {' | |||||
8164 | .file_icon:before.pull-right { |
|
8164 | .file_icon:before.pull-right { | |
8165 | margin-left: .3em; |
|
8165 | margin-left: .3em; | |
8166 | } |
|
8166 | } | |
|
8167 | ul#new-notebook-menu { | |||
|
8168 | left: auto; | |||
|
8169 | right: 0; | |||
|
8170 | } | |||
8167 | /*! |
|
8171 | /*! | |
8168 | * |
|
8172 | * | |
8169 | * IPython notebook |
|
8173 | * IPython notebook |
@@ -14,6 +14,7 b' require([' | |||||
14 | 'tree/js/sessionlist', |
|
14 | 'tree/js/sessionlist', | |
15 | 'tree/js/kernellist', |
|
15 | 'tree/js/kernellist', | |
16 | 'tree/js/terminallist', |
|
16 | 'tree/js/terminallist', | |
|
17 | 'tree/js/newnotebook', | |||
17 | 'auth/js/loginwidget', |
|
18 | 'auth/js/loginwidget', | |
18 | // only loaded, not used: |
|
19 | // only loaded, not used: | |
19 | 'jqueryui', |
|
20 | 'jqueryui', | |
@@ -32,6 +33,7 b' require([' | |||||
32 |
sesssionlist, |
|
33 | sesssionlist, | |
33 | kernellist, |
|
34 | kernellist, | |
34 | terminallist, |
|
35 | terminallist, | |
|
36 | newnotebook, | |||
35 | loginwidget){ |
|
37 | loginwidget){ | |
36 | "use strict"; |
|
38 | "use strict"; | |
37 |
|
39 | |||
@@ -63,24 +65,12 b' require([' | |||||
63 |
|
65 | |||
64 | var login_widget = new loginwidget.LoginWidget('#login_widget', common_options); |
|
66 | var login_widget = new loginwidget.LoginWidget('#login_widget', common_options); | |
65 |
|
67 | |||
66 | $('#new_notebook').click(function (e) { |
|
68 | var nnw = new newnotebook.NewNotebookWidget("#new-notebook-buttons", | |
67 | var w = window.open(); |
|
69 | $.extend( | |
68 | contents.new_untitled(common_options.notebook_path, {type: "notebook"}).then( |
|
70 | {contents: contents}, | |
69 | function (data) { |
|
71 | common_options | |
70 | w.location = utils.url_join_encode( |
|
72 | ) | |
71 | common_options.base_url, 'notebooks', data.path |
|
|||
72 | ); |
|
73 | ); | |
73 | }, |
|
|||
74 | function(error) { |
|
|||
75 | w.close(); |
|
|||
76 | dialog.modal({ |
|
|||
77 | title : 'Creating Notebook Failed', |
|
|||
78 | body : "The error was: " + error.message, |
|
|||
79 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
|||
80 | }); |
|
|||
81 | } |
|
|||
82 | ); |
|
|||
83 | }); |
|
|||
84 |
|
74 | |||
85 | var interval_id=0; |
|
75 | var interval_id=0; | |
86 | // auto refresh every xx secondes, no need to be fast, |
|
76 | // auto refresh every xx secondes, no need to be fast, |
@@ -154,3 +154,9 b' input.engine_num_input {' | |||||
154 | .file_icon:before { |
|
154 | .file_icon:before { | |
155 | .icon(@fa-var-file-o) |
|
155 | .icon(@fa-var-file-o) | |
156 | } |
|
156 | } | |
|
157 | ||||
|
158 | ul#new-notebook-menu { | |||
|
159 | // align right instead of left | |||
|
160 | left: auto; | |||
|
161 | right: 0; | |||
|
162 | } No newline at end of file |
@@ -44,7 +44,17 b' data-terminals-available="{{terminals_available}}"' | |||||
44 | </div> |
|
44 | </div> | |
45 | <div class="col-sm-4 no-padding tree-buttons"> |
|
45 | <div class="col-sm-4 no-padding tree-buttons"> | |
46 |
|
|
46 | <span id="notebook_buttons" class="pull-right"> | |
47 | <button id="new_notebook" title="Create new notebook" class="btn btn-default btn-xs">New Notebook</button> |
|
47 | <div id="new-notebook-buttons" class="btn-group"> | |
|
48 | <button id="new_notebook" class="btn btn-default btn-xs"> | |||
|
49 | New Notebook | |||
|
50 | </button> | |||
|
51 | <button class="dropdown-toggle btn btn-default btn-xs" data-toggle="dropdown"> | |||
|
52 | <span class="caret"></span> | |||
|
53 | </button> | |||
|
54 | <ul id="new-notebook-menu" class="dropdown-menu"></ul> | |||
|
55 | </div> | |||
|
56 | ||||
|
57 | ||||
48 |
|
|
58 | <button id="refresh_notebook_list" title="Refresh notebook list" class="btn btn-default btn-xs"><i class="fa fa-refresh"></i></button> | |
49 |
|
|
59 | </span> | |
50 | </div> |
|
60 | </div> |
General Comments 0
You need to be logged in to leave comments.
Login now