Show More
@@ -1,117 +1,120 | |||||
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 | 'base/js/utils', |
|
6 | 'base/js/utils', | |
7 | 'jquery', |
|
7 | 'jquery', | |
8 | 'tree/js/notebooklist', |
|
8 | 'tree/js/notebooklist', | |
9 | ], function(IPython, utils, $, notebooklist) { |
|
9 | ], function(IPython, utils, $, notebooklist) { | |
10 | "use strict"; |
|
10 | "use strict"; | |
11 |
|
11 | |||
12 | var TerminalList = function (selector, options) { |
|
12 | var TerminalList = function (selector, options) { | |
13 | // Constructor |
|
13 | // Constructor | |
14 | // |
|
14 | // | |
15 | // Parameters: |
|
15 | // Parameters: | |
16 | // selector: string |
|
16 | // selector: string | |
17 | // options: dictionary |
|
17 | // options: dictionary | |
18 | // Dictionary of keyword arguments. |
|
18 | // Dictionary of keyword arguments. | |
19 | // base_url: string |
|
19 | // base_url: string | |
20 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); |
|
20 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | |
21 | this.element_name = options.element_name || 'terminal'; |
|
21 | this.element_name = options.element_name || 'terminal'; | |
22 | this.selector = selector; |
|
22 | this.selector = selector; | |
23 | this.terminals = []; |
|
23 | this.terminals = []; | |
24 | if (this.selector !== undefined) { |
|
24 | if (this.selector !== undefined) { | |
25 | this.element = $(selector); |
|
25 | this.element = $(selector); | |
26 | this.style(); |
|
26 | this.style(); | |
27 | this.bind_events(); |
|
27 | this.bind_events(); | |
28 | this.load_terminals(); |
|
28 | this.load_terminals(); | |
29 | } |
|
29 | } | |
30 | }; |
|
30 | }; | |
31 |
|
31 | |||
32 | TerminalList.prototype = Object.create(notebooklist.NotebookList.prototype); |
|
32 | TerminalList.prototype = Object.create(notebooklist.NotebookList.prototype); | |
33 |
|
33 | |||
34 | TerminalList.prototype.bind_events = function () { |
|
34 | TerminalList.prototype.bind_events = function () { | |
35 | var that = this; |
|
35 | var that = this; | |
36 | $('#refresh_' + this.element_name + '_list').click(function () { |
|
36 | $('#refresh_' + this.element_name + '_list').click(function () { | |
37 | that.load_terminals(); |
|
37 | that.load_terminals(); | |
38 | }); |
|
38 | }); | |
39 | $('#new_terminal').click($.proxy(this.new_terminal, this)); |
|
39 | $('#new_terminal').click($.proxy(this.new_terminal, this)); | |
40 | }; |
|
40 | }; | |
41 |
|
41 | |||
42 | TerminalList.prototype.new_terminal = function () { |
|
42 | TerminalList.prototype.new_terminal = function () { | |
43 | var w = window.open(); |
|
43 | var w = window.open(); | |
44 | var base_url = this.base_url; |
|
44 | var base_url = this.base_url; | |
45 | var settings = { |
|
45 | var settings = { | |
46 | type : "POST", |
|
46 | type : "POST", | |
47 | dataType: "json", |
|
47 | dataType: "json", | |
48 | success : function (data, status, xhr) { |
|
48 | success : function (data, status, xhr) { | |
49 | var name = data.name; |
|
49 | var name = data.name; | |
50 | w.location = utils.url_join_encode(base_url, 'terminals', name); |
|
50 | w.location = utils.url_join_encode(base_url, 'terminals', name); | |
51 | }, |
|
51 | }, | |
52 |
error : |
|
52 | error : function(jqXHR, status, error){ | |
|
53 | w.close(); | |||
|
54 | utils.log_ajax_error(jqXHR, status, error); | |||
|
55 | }, | |||
53 | }; |
|
56 | }; | |
54 | var url = utils.url_join_encode( |
|
57 | var url = utils.url_join_encode( | |
55 | this.base_url, |
|
58 | this.base_url, | |
56 | 'api/terminals' |
|
59 | 'api/terminals' | |
57 | ); |
|
60 | ); | |
58 | $.ajax(url, settings); |
|
61 | $.ajax(url, settings); | |
59 | }; |
|
62 | }; | |
60 |
|
63 | |||
61 | TerminalList.prototype.load_terminals = function() { |
|
64 | TerminalList.prototype.load_terminals = function() { | |
62 | var that = this; |
|
65 | var that = this; | |
63 | var url = utils.url_join_encode(this.base_url, 'api/terminals'); |
|
66 | var url = utils.url_join_encode(this.base_url, 'api/terminals'); | |
64 | $.ajax(url, { |
|
67 | $.ajax(url, { | |
65 | type: "GET", |
|
68 | type: "GET", | |
66 | cache: false, |
|
69 | cache: false, | |
67 | dataType: "json", |
|
70 | dataType: "json", | |
68 | success: $.proxy(this.terminals_loaded, this), |
|
71 | success: $.proxy(this.terminals_loaded, this), | |
69 | error : utils.log_ajax_error |
|
72 | error : utils.log_ajax_error | |
70 | }); |
|
73 | }); | |
71 | }; |
|
74 | }; | |
72 |
|
75 | |||
73 | TerminalList.prototype.terminals_loaded = function (data) { |
|
76 | TerminalList.prototype.terminals_loaded = function (data) { | |
74 | this.terminals = data; |
|
77 | this.terminals = data; | |
75 | this.clear_list(); |
|
78 | this.clear_list(); | |
76 | var item, path_name, term; |
|
79 | var item, path_name, term; | |
77 | for (var i=0; i < this.terminals.length; i++) { |
|
80 | for (var i=0; i < this.terminals.length; i++) { | |
78 | term = this.terminals[i]; |
|
81 | term = this.terminals[i]; | |
79 | item = this.new_item(-1); |
|
82 | item = this.new_item(-1); | |
80 | this.add_link(term.name, item); |
|
83 | this.add_link(term.name, item); | |
81 | this.add_shutdown_button(term.name, item); |
|
84 | this.add_shutdown_button(term.name, item); | |
82 | } |
|
85 | } | |
83 | $('#terminal_list_header').toggle(data.length === 0); |
|
86 | $('#terminal_list_header').toggle(data.length === 0); | |
84 | }; |
|
87 | }; | |
85 |
|
88 | |||
86 | TerminalList.prototype.add_link = function(name, item) { |
|
89 | TerminalList.prototype.add_link = function(name, item) { | |
87 | item.data('term-name', name); |
|
90 | item.data('term-name', name); | |
88 | item.find(".item_name").text("terminals/" + name); |
|
91 | item.find(".item_name").text("terminals/" + name); | |
89 | item.find(".item_icon").addClass("fa fa-terminal"); |
|
92 | item.find(".item_icon").addClass("fa fa-terminal"); | |
90 | var link = item.find("a.item_link") |
|
93 | var link = item.find("a.item_link") | |
91 | .attr('href', utils.url_join_encode(this.base_url, "terminals", name)); |
|
94 | .attr('href', utils.url_join_encode(this.base_url, "terminals", name)); | |
92 | link.attr('target', '_blank'); |
|
95 | link.attr('target', '_blank'); | |
93 | this.add_shutdown_button(name, item); |
|
96 | this.add_shutdown_button(name, item); | |
94 | }; |
|
97 | }; | |
95 |
|
98 | |||
96 | TerminalList.prototype.add_shutdown_button = function(name, item) { |
|
99 | TerminalList.prototype.add_shutdown_button = function(name, item) { | |
97 | var that = this; |
|
100 | var that = this; | |
98 | var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-xs btn-danger"). |
|
101 | var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-xs btn-danger"). | |
99 | click(function (e) { |
|
102 | click(function (e) { | |
100 | var settings = { |
|
103 | var settings = { | |
101 | processData : false, |
|
104 | processData : false, | |
102 | type : "DELETE", |
|
105 | type : "DELETE", | |
103 | dataType : "json", |
|
106 | dataType : "json", | |
104 | success : function () { |
|
107 | success : function () { | |
105 | that.load_terminals(); |
|
108 | that.load_terminals(); | |
106 | }, |
|
109 | }, | |
107 | error : utils.log_ajax_error, |
|
110 | error : utils.log_ajax_error, | |
108 | }; |
|
111 | }; | |
109 | var url = utils.url_join_encode(that.base_url, 'api/terminals', name); |
|
112 | var url = utils.url_join_encode(that.base_url, 'api/terminals', name); | |
110 | $.ajax(url, settings); |
|
113 | $.ajax(url, settings); | |
111 | return false; |
|
114 | return false; | |
112 | }); |
|
115 | }); | |
113 | item.find(".item_buttons").text("").append(shutdown_button); |
|
116 | item.find(".item_buttons").text("").append(shutdown_button); | |
114 | }; |
|
117 | }; | |
115 |
|
118 | |||
116 | return {TerminalList: TerminalList}; |
|
119 | return {TerminalList: TerminalList}; | |
117 | }); |
|
120 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now