Show More
@@ -1,150 +1,151 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | require([ |
|
5 | 5 | 'jquery', |
|
6 | 6 | 'base/js/namespace', |
|
7 | 7 | 'base/js/dialog', |
|
8 | 8 | 'base/js/events', |
|
9 | 9 | 'base/js/page', |
|
10 | 10 | 'base/js/utils', |
|
11 | 11 | 'services/config', |
|
12 | 12 | 'contents', |
|
13 | 13 | 'tree/js/notebooklist', |
|
14 | 14 | 'tree/js/clusterlist', |
|
15 | 15 | 'tree/js/sessionlist', |
|
16 | 16 | 'tree/js/kernellist', |
|
17 | 17 | 'tree/js/terminallist', |
|
18 | 18 | 'tree/js/newnotebook', |
|
19 | 19 | 'auth/js/loginwidget', |
|
20 | 20 | // only loaded, not used: |
|
21 | 21 | 'jqueryui', |
|
22 | 22 | 'bootstrap', |
|
23 | 23 | 'custom/custom', |
|
24 | 24 | ], function( |
|
25 | 25 | $, |
|
26 | 26 | IPython, |
|
27 | 27 | dialog, |
|
28 | 28 | events, |
|
29 | 29 | page, |
|
30 | 30 | utils, |
|
31 | 31 | config, |
|
32 | 32 | contents_service, |
|
33 | 33 | notebooklist, |
|
34 | 34 | clusterlist, |
|
35 | 35 | sesssionlist, |
|
36 | 36 | kernellist, |
|
37 | 37 | terminallist, |
|
38 | 38 | newnotebook, |
|
39 | 39 | loginwidget){ |
|
40 | 40 | "use strict"; |
|
41 | 41 | |
|
42 | 42 | page = new page.Page(); |
|
43 | 43 | |
|
44 | 44 | var common_options = { |
|
45 | 45 | base_url: utils.get_body_data("baseUrl"), |
|
46 | 46 | notebook_path: utils.get_body_data("notebookPath"), |
|
47 | 47 | }; |
|
48 | 48 | var cfg = new config.ConfigSection('tree', common_options); |
|
49 | 49 | cfg.load(); |
|
50 | 50 | common_options.config = cfg; |
|
51 | 51 | |
|
52 | 52 | var session_list = new sesssionlist.SesssionList($.extend({ |
|
53 | 53 | events: events}, |
|
54 | 54 | common_options)); |
|
55 | 55 | var contents = new contents_service.Contents($.extend({ |
|
56 | 56 | events: events}, |
|
57 | 57 | common_options)); |
|
58 | 58 | var notebook_list = new notebooklist.NotebookList('#notebook_list', $.extend({ |
|
59 | 59 | contents: contents, |
|
60 | 60 | session_list: session_list}, |
|
61 | 61 | common_options)); |
|
62 | 62 | var cluster_list = new clusterlist.ClusterList('#cluster_list', common_options); |
|
63 | 63 | var kernel_list = new kernellist.KernelList('#running_list', $.extend({ |
|
64 | 64 | session_list: session_list}, |
|
65 | 65 | common_options)); |
|
66 | 66 | |
|
67 | 67 | var terminal_list; |
|
68 | 68 | if (utils.get_body_data("terminalsAvailable") === "True") { |
|
69 | 69 | terminal_list = new terminallist.TerminalList('#terminal_list', common_options); |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | var login_widget = new loginwidget.LoginWidget('#login_widget', common_options); |
|
73 | 73 | |
|
74 | 74 | var nnw = new newnotebook.NewNotebookWidget("#new-notebook-buttons", |
|
75 | 75 | $.extend( |
|
76 | 76 | {contents: contents}, |
|
77 | 77 | common_options |
|
78 | 78 | ) |
|
79 | 79 | ); |
|
80 | 80 | |
|
81 | 81 | var interval_id=0; |
|
82 | 82 | // auto refresh every xx secondes, no need to be fast, |
|
83 | 83 | // update is done at least when page get focus |
|
84 | 84 | var time_refresh = 60; // in sec |
|
85 | 85 | |
|
86 | 86 | var enable_autorefresh = function(){ |
|
87 | 87 | /** |
|
88 | 88 | *refresh immediately , then start interval |
|
89 | 89 | */ |
|
90 | 90 | session_list.load_sessions(); |
|
91 | 91 | cluster_list.load_list(); |
|
92 | 92 | if (terminal_list) { |
|
93 | 93 | terminal_list.load_terminals(); |
|
94 | 94 | } |
|
95 | 95 | if (!interval_id){ |
|
96 | 96 | interval_id = setInterval(function(){ |
|
97 | 97 | session_list.load_sessions(); |
|
98 | 98 | cluster_list.load_list(); |
|
99 | 99 | if (terminal_list) { |
|
100 | 100 | terminal_list.load_terminals(); |
|
101 | 101 | } |
|
102 | 102 | }, time_refresh*1000); |
|
103 | 103 | } |
|
104 | 104 | }; |
|
105 | 105 | |
|
106 | 106 | var disable_autorefresh = function(){ |
|
107 | 107 | clearInterval(interval_id); |
|
108 | 108 | interval_id = 0; |
|
109 | 109 | }; |
|
110 | 110 | |
|
111 | 111 | // stop autorefresh when page lose focus |
|
112 | 112 | $(window).blur(function() { |
|
113 | 113 | disable_autorefresh(); |
|
114 | 114 | }); |
|
115 | 115 | |
|
116 | 116 | //re-enable when page get focus back |
|
117 | 117 | $(window).focus(function() { |
|
118 | 118 | enable_autorefresh(); |
|
119 | 119 | }); |
|
120 | 120 | |
|
121 | 121 | // finally start it, it will refresh immediately |
|
122 | 122 | enable_autorefresh(); |
|
123 | 123 | |
|
124 | 124 | page.show(); |
|
125 | 125 | |
|
126 | 126 | // For backwards compatability. |
|
127 | 127 | IPython.page = page; |
|
128 | 128 | IPython.notebook_list = notebook_list; |
|
129 | 129 | IPython.cluster_list = cluster_list; |
|
130 | 130 | IPython.session_list = session_list; |
|
131 | 131 | IPython.kernel_list = kernel_list; |
|
132 | 132 | IPython.login_widget = login_widget; |
|
133 | IPython.new_notebook_widget = nnw; | |
|
133 | 134 | |
|
134 | 135 | events.trigger('app_initialized.DashboardApp'); |
|
135 | 136 | |
|
136 | 137 | // bound the upload method to the on change of the file select list |
|
137 | 138 | $("#alternate_upload").change(function (event){ |
|
138 | 139 | notebook_list.handleFilesUpload(event,'form'); |
|
139 | 140 | }); |
|
140 | 141 | |
|
141 | 142 | // set hash on tab click |
|
142 | 143 | $("#tabs").find("a").click(function() { |
|
143 | 144 | window.location.hash = $(this).attr("href"); |
|
144 | 145 | }); |
|
145 | 146 | |
|
146 | 147 | // load tab if url hash |
|
147 | 148 | if (window.location.hash) { |
|
148 | 149 | $("#tabs").find("a[href=" + window.location.hash + "]").click(); |
|
149 | 150 | } |
|
150 | 151 | }); |
@@ -1,158 +1,133 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 |
$('<a>') |
|
|
62 | .addClass('kernel-menu-icon fa') | |
|
63 | .attr('href', '#') | |
|
64 | .click($.proxy(this.select_kernel, this, ks.name)) | |
|
65 | ).append($('<span>') | |
|
61 | $('<a>') | |
|
66 | 62 | .attr('href', '#') |
|
67 | 63 | .click($.proxy(this.new_notebook, this, ks.name)) |
|
68 | 64 | .text(ks.display_name) |
|
69 | 65 | .attr('title', 'Create a new notebook with ' + ks.display_name) |
|
70 |
|
|
|
71 | ); | |
|
66 | ); | |
|
72 | 67 | menu.append(li); |
|
73 | 68 | } |
|
74 | 69 | this.config.loaded.then(function () { |
|
75 | 70 | that._load_default_kernelspec(data['default']); |
|
76 | 71 | }); |
|
77 | 72 | }; |
|
78 | 73 | |
|
79 | 74 | NewNotebookWidget.prototype._load_default_kernelspec = function (default_name) { |
|
80 |
/** load default kernelspec name from |
|
|
75 | /** load default kernelspec name from config, if defined */ | |
|
81 | 76 | if (this.config.data.NewNotebookWidget && |
|
82 | 77 | this.config.data.NewNotebookWidget.current_selection && |
|
83 | 78 | this.kernelspecs[this.config.data.NewNotebookWidget.current_selection] !== undefined |
|
84 | 79 | ) { |
|
85 | 80 | default_name = this.config.data.NewNotebookWidget.current_selection; |
|
86 | 81 | } |
|
87 |
this.sel |
|
|
82 | this.set_default_kernel(default_name); | |
|
88 | 83 | }; |
|
89 | 84 | |
|
90 |
NewNotebookWidget.prototype.sel |
|
|
85 | NewNotebookWidget.prototype.set_default_kernel = function (kernel_name) { | |
|
91 | 86 | /** select the current default kernel */ |
|
92 | 87 | this.current_selection = kernel_name; |
|
93 | 88 | this.config.update({ |
|
94 | 89 | NewNotebookWidget: { |
|
95 | 90 | current_selection: kernel_name |
|
96 | 91 | } |
|
97 | 92 | }); |
|
98 | 93 | var spec = this.kernelspecs[kernel_name]; |
|
99 | 94 | var display_name; |
|
100 | 95 | if (spec) { |
|
101 | 96 | display_name = spec.display_name; |
|
102 | localStorage.default_kernel_name = kernel_name; | |
|
103 | 97 | this.element.find("#current-kernel").text(display_name); |
|
104 | 98 | } else { |
|
105 | 99 | display_name = 'default kernel'; |
|
106 | delete localStorage.default_kernel_name; | |
|
107 | 100 | } |
|
108 | 101 | this.element.find("#new_notebook").attr('title', |
|
109 | 102 | 'Create a new notebook with ' + display_name |
|
110 | 103 | ); |
|
111 | this.element.find("li").map(function (i, li) { | |
|
112 | li = $(li); | |
|
113 | var ks = li.data('kernelspec'); | |
|
114 | if (ks.name == kernel_name) { | |
|
115 | li.find(".kernel-menu-icon") | |
|
116 | .attr('title', display_name + ' is the default kernel') | |
|
117 | .addClass("kernel-menu-icon-current"); | |
|
118 | } else { | |
|
119 | li.find(".kernel-menu-icon") | |
|
120 | .attr('title', 'Make ' + ks.display_name + ' the default kernel') | |
|
121 | .removeClass("kernel-menu-icon-current"); | |
|
122 | } | |
|
123 | }); | |
|
124 | }; | |
|
125 | ||
|
126 | NewNotebookWidget.prototype.new_with_kernel = function (kernel_name) { | |
|
127 | /** record current selection and open a new notebook */ | |
|
128 | this.new_notebook(kernel_name); | |
|
129 | 104 | }; |
|
130 | 105 | |
|
131 | 106 | NewNotebookWidget.prototype.new_notebook = function (kernel_name) { |
|
132 | 107 | /** create and open a new notebook */ |
|
133 | 108 | var that = this; |
|
134 | 109 | kernel_name = kernel_name || this.current_selection; |
|
135 | 110 | var w = window.open(); |
|
136 | 111 | this.contents.new_untitled(that.notebook_path, {type: "notebook"}).then( |
|
137 | 112 | function (data) { |
|
138 | 113 | var url = utils.url_join_encode( |
|
139 | 114 | that.base_url, 'notebooks', data.path |
|
140 | 115 | ); |
|
141 | 116 | if (kernel_name) { |
|
142 | 117 | url += "?kernel_name=" + kernel_name; |
|
143 | 118 | } |
|
144 | 119 | w.location = url; |
|
145 | 120 | }, |
|
146 | 121 | function (error) { |
|
147 | 122 | w.close(); |
|
148 | 123 | dialog.modal({ |
|
149 | 124 | title : 'Creating Notebook Failed', |
|
150 | 125 | body : "The error was: " + error.message, |
|
151 | 126 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
152 | 127 | }); |
|
153 | 128 | } |
|
154 | 129 | ); |
|
155 | 130 | }; |
|
156 | 131 | |
|
157 | 132 | return {'NewNotebookWidget': NewNotebookWidget}; |
|
158 | 133 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now