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