##// END OF EJS Templates
fix logo on base_url prefix
Bussonnier Matthias -
Show More
@@ -1,159 +1,159 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 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 ], function(IPython, $, utils) {
8 ], function(IPython, $, utils) {
9 "use strict";
9 "use strict";
10
10
11 var KernelSelector = function(selector, notebook) {
11 var KernelSelector = function(selector, notebook) {
12 this.selector = selector;
12 this.selector = selector;
13 this.notebook = notebook;
13 this.notebook = notebook;
14 this.notebook.set_kernelselector(this);
14 this.notebook.set_kernelselector(this);
15 this.events = notebook.events;
15 this.events = notebook.events;
16 this.current_selection = null;
16 this.current_selection = null;
17 this.kernelspecs = {};
17 this.kernelspecs = {};
18 if (this.selector !== undefined) {
18 if (this.selector !== undefined) {
19 this.element = $(selector);
19 this.element = $(selector);
20 this.request_kernelspecs();
20 this.request_kernelspecs();
21 }
21 }
22 this.bind_events();
22 this.bind_events();
23 // Make the object globally available for user convenience & inspection
23 // Make the object globally available for user convenience & inspection
24 IPython.kernelselector = this;
24 IPython.kernelselector = this;
25 Object.seal(this);
25 Object.seal(this);
26 };
26 };
27
27
28 KernelSelector.prototype.request_kernelspecs = function() {
28 KernelSelector.prototype.request_kernelspecs = function() {
29 var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs');
29 var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs');
30 utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this));
30 utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this));
31 };
31 };
32
32
33 KernelSelector.prototype._got_kernelspecs = function(data) {
33 KernelSelector.prototype._got_kernelspecs = function(data) {
34 this.kernelspecs = data.kernelspecs;
34 this.kernelspecs = data.kernelspecs;
35 var menu = this.element.find("#kernel_selector");
35 var menu = this.element.find("#kernel_selector");
36 var change_kernel_submenu = $("#menu-change-kernel-submenu");
36 var change_kernel_submenu = $("#menu-change-kernel-submenu");
37 var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
37 var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
38 // sort by display_name
38 // sort by display_name
39 var da = data.kernelspecs[a].display_name;
39 var da = data.kernelspecs[a].display_name;
40 var db = data.kernelspecs[b].display_name;
40 var db = data.kernelspecs[b].display_name;
41 if (da === db) {
41 if (da === db) {
42 return 0;
42 return 0;
43 } else if (da > db) {
43 } else if (da > db) {
44 return 1;
44 return 1;
45 } else {
45 } else {
46 return -1;
46 return -1;
47 }
47 }
48 });
48 });
49 for (var i = 0; i < keys.length; i++) {
49 for (var i = 0; i < keys.length; i++) {
50 var ks = this.kernelspecs[keys[i]];
50 var ks = this.kernelspecs[keys[i]];
51 var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>')
51 var ksentry = $("<li>").attr("id", "kernel-" +ks.name).append($('<a>')
52 .attr('href', '#')
52 .attr('href', '#')
53 .click($.proxy(this.change_kernel, this, ks.name))
53 .click($.proxy(this.change_kernel, this, ks.name))
54 .text(ks.display_name));
54 .text(ks.display_name));
55 menu.append(ksentry);
55 menu.append(ksentry);
56
56
57 var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>')
57 var ks_submenu_entry = $("<li>").attr("id", "kernel-submenu-"+ks.name).append($('<a>')
58 .attr('href', '#')
58 .attr('href', '#')
59 .click($.proxy(this.change_kernel, this, ks.name))
59 .click($.proxy(this.change_kernel, this, ks.name))
60 .text(ks.display_name));
60 .text(ks.display_name));
61 change_kernel_submenu.append(ks_submenu_entry);
61 change_kernel_submenu.append(ks_submenu_entry);
62 }
62 }
63 };
63 };
64
64
65 KernelSelector.prototype.change_kernel = function(kernel_name) {
65 KernelSelector.prototype.change_kernel = function(kernel_name) {
66 /**
66 /**
67 * TODO, have a methods to set kernel spec directly ?
67 * TODO, have a methods to set kernel spec directly ?
68 **/
68 **/
69 var that = this;
69 var that = this;
70 if (kernel_name === this.current_selection) {
70 if (kernel_name === this.current_selection) {
71 return;
71 return;
72 }
72 }
73 var ks = this.kernelspecs[kernel_name];
73 var ks = this.kernelspecs[kernel_name];
74 var new_mode_url = 'kernelspecs/'+ks.name+'/kernel';
74 var new_mode_url = 'kernelspecs/'+ks.name+'/kernel';
75
75
76 var css_url = this.notebook.base_url+new_mode_url+'.css';
76 var css_url = this.notebook.base_url+new_mode_url+'.css';
77 $.ajax({
77 $.ajax({
78 type: 'HEAD',
78 type: 'HEAD',
79 url: css_url,
79 url: css_url,
80 success: function(){
80 success: function(){
81 $('#kernel-css')
81 $('#kernel-css')
82 .attr('href',css_url);
82 .attr('href',css_url);
83 },
83 },
84 error:function(){
84 error:function(){
85 console.info("No custom kernel.css at URL:", css_url)
85 console.info("No custom kernel.css at URL:", css_url)
86 }
86 }
87 });
87 });
88
88
89 try {
89 try {
90 this.notebook.start_session(kernel_name);
90 this.notebook.start_session(kernel_name);
91 } catch (e) {
91 } catch (e) {
92 if (e.name === 'SessionAlreadyStarting') {
92 if (e.name === 'SessionAlreadyStarting') {
93 console.log("Cannot change kernel while waiting for pending session start.");
93 console.log("Cannot change kernel while waiting for pending session start.");
94 } else {
94 } else {
95 // unhandled error
95 // unhandled error
96 throw e;
96 throw e;
97 }
97 }
98 // only trigger spec_changed if change was successful
98 // only trigger spec_changed if change was successful
99 return;
99 return;
100 }
100 }
101 this.events.trigger('spec_changed.Kernel', ks);
101 this.events.trigger('spec_changed.Kernel', ks);
102
102
103
103
104 // load new mode kernel.js if exist
104 // load new mode kernel.js if exist
105 require([new_mode_url],
105 require([new_mode_url],
106 // if new mode has custom.js
106 // if new mode has custom.js
107 function(new_mode){
107 function(new_mode){
108 that.lock_switch();
108 that.lock_switch();
109 if(new_mode && new_mode.onload){
109 if(new_mode && new_mode.onload){
110 new_mode.onload();
110 new_mode.onload();
111 } else {
111 } else {
112 console.warn("The current kernel defined a kernel.js file but does not contain "+
112 console.warn("The current kernel defined a kernel.js file but does not contain "+
113 "any asynchronous module definition. This is undefined behavior "+
113 "any asynchronous module definition. This is undefined behavior "+
114 "which is not recommended");
114 "which is not recommended");
115 }
115 }
116 },
116 },
117 function(err){
117 function(err){
118 // if new mode does not have custom.js
118 // if new mode does not have custom.js
119 console.info("No custom kernel.css at URL:", new_mode_url)
119 console.info("No custom kernel.css at URL:", new_mode_url)
120 }
120 }
121 );
121 );
122 };
122 };
123
123
124 KernelSelector.prototype.lock_switch = function() {
124 KernelSelector.prototype.lock_switch = function() {
125 // should set a flag and display warning+reload if user want to
125 // should set a flag and display warning+reload if user want to
126 // re-change kernel. As UI discussion never finish
126 // re-change kernel. As UI discussion never finish
127 // making that a separate PR.
127 // making that a separate PR.
128 console.warn('switching kernel is not guaranteed to work !');
128 console.warn('switching kernel is not guaranteed to work !');
129 };
129 };
130
130
131 KernelSelector.prototype.bind_events = function() {
131 KernelSelector.prototype.bind_events = function() {
132 var that = this;
132 var that = this;
133 this.events.on('spec_changed.Kernel', function(event, data) {
133 this.events.on('spec_changed.Kernel', function(event, data) {
134 that.current_selection = data.name;
134 that.current_selection = data.name;
135 that.element.find("#current_kernel_spec").find('.kernel_name').text(data.display_name);
135 that.element.find("#current_kernel_spec").find('.kernel_name').text(data.display_name);
136 that.element.find("#current_kernel_logo").attr("src", "/kernelspecs/"+data.name+"/logo-64x64.png");
136 that.element.find("#current_kernel_logo").attr("src", that.notebook.base_url+"kernelspecs/"+data.name+"/logo-64x64.png");
137 });
137 });
138
138
139 this.events.on('kernel_created.Session', function(event, data) {
139 this.events.on('kernel_created.Session', function(event, data) {
140 if (data.kernel.name !== that.current_selection) {
140 if (data.kernel.name !== that.current_selection) {
141 // If we created a 'python' session, we only know if it's Python
141 // If we created a 'python' session, we only know if it's Python
142 // 3 or 2 on the server's reply, so we fire the event again to
142 // 3 or 2 on the server's reply, so we fire the event again to
143 // set things up.
143 // set things up.
144 var ks = that.kernelspecs[data.kernel.name];
144 var ks = that.kernelspecs[data.kernel.name];
145 that.events.trigger('spec_changed.Kernel', ks);
145 that.events.trigger('spec_changed.Kernel', ks);
146 }
146 }
147 });
147 });
148
148
149 var logo_img = this.element.find("#current_kernel_logo")
149 var logo_img = this.element.find("#current_kernel_logo")
150 logo_img.on("load", function() {
150 logo_img.on("load", function() {
151 logo_img.show();
151 logo_img.show();
152 });
152 });
153 logo_img.on("error", function() {
153 logo_img.on("error", function() {
154 logo_img.hide();
154 logo_img.hide();
155 });
155 });
156 };
156 };
157
157
158 return {'KernelSelector': KernelSelector};
158 return {'KernelSelector': KernelSelector};
159 });
159 });
General Comments 0
You need to be logged in to leave comments. Login now