clusterlist.js
198 lines
| 6.8 KiB
| application/javascript
|
JavascriptLexer
|
r6195 | //---------------------------------------------------------------------------- | ||
|
r13063 | // Copyright (C) 2011 The IPython Development Team | ||
|
r6195 | // | ||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// NotebookList | ||||
//============================================================================ | ||||
var IPython = (function (IPython) { | ||||
|
r13063 | "use strict"; | ||
var utils = IPython.utils; | ||||
|
r6195 | |||
|
r15234 | var ClusterList = function (selector, options) { | ||
|
r6195 | this.selector = selector; | ||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.style(); | ||||
this.bind_events(); | ||||
} | ||||
|
r15234 | options = options || {}; | ||
this.options = options; | ||||
|
r15240 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | ||
this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); | ||||
|
r9503 | }; | ||
|
r6195 | ClusterList.prototype.style = function () { | ||
|
r10920 | $('#cluster_list').addClass('list_container'); | ||
|
r6195 | $('#cluster_toolbar').addClass('list_toolbar'); | ||
$('#cluster_list_info').addClass('toolbar_info'); | ||||
$('#cluster_buttons').addClass('toolbar_buttons'); | ||||
}; | ||||
ClusterList.prototype.bind_events = function () { | ||||
var that = this; | ||||
$('#refresh_cluster_list').click(function () { | ||||
that.load_list(); | ||||
}); | ||||
}; | ||||
ClusterList.prototype.load_list = function () { | ||||
var settings = { | ||||
processData : false, | ||||
cache : false, | ||||
type : "GET", | ||||
dataType : "json", | ||||
|
r16445 | success : $.proxy(this.load_list_success, this), | ||
error : utils.log_ajax_error, | ||||
|
r6195 | }; | ||
|
r15238 | var url = utils.url_join_encode(this.base_url, 'clusters'); | ||
|
r6195 | $.ajax(url, settings); | ||
}; | ||||
ClusterList.prototype.clear_list = function () { | ||||
|
r10920 | this.element.children('.list_item').remove(); | ||
|
r13063 | }; | ||
|
r6195 | |||
ClusterList.prototype.load_list_success = function (data, status, xhr) { | ||||
this.clear_list(); | ||||
var len = data.length; | ||||
for (var i=0; i<len; i++) { | ||||
|
r10920 | var element = $('<div/>'); | ||
|
r15234 | var item = new ClusterItem(element, this.options); | ||
|
r6195 | item.update_state(data[i]); | ||
|
r10911 | element.data('item', item); | ||
this.element.append(element); | ||||
|
r13063 | } | ||
|
r6195 | }; | ||
|
r15234 | var ClusterItem = function (element, options) { | ||
|
r6195 | this.element = $(element); | ||
|
r15240 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | ||
this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); | ||||
|
r6195 | this.data = null; | ||
|
r6197 | this.style(); | ||
|
r6195 | }; | ||
|
r6197 | ClusterItem.prototype.style = function () { | ||
|
r16913 | this.element.addClass('list_item').addClass("row"); | ||
|
r13063 | }; | ||
|
r6197 | |||
|
r6195 | ClusterItem.prototype.update_state = function (data) { | ||
this.data = data; | ||||
if (data.status === 'running') { | ||||
this.state_running(); | ||||
} else if (data.status === 'stopped') { | ||||
this.state_stopped(); | ||||
|
r13063 | } | ||
}; | ||||
|
r6195 | |||
ClusterItem.prototype.state_stopped = function () { | ||||
|
r6197 | var that = this; | ||
|
r16921 | var profile_col = $('<div/>').addClass('profile_col col-md-4').text(this.data.profile); | ||
var status_col = $('<div/>').addClass('status_col col-md-3').text('stopped'); | ||||
var engines_col = $('<div/>').addClass('engine_col col-md-3'); | ||||
|
r9289 | var input = $('<input/>').attr('type','number') | ||
.attr('min',1) | ||||
.attr('size',3) | ||||
|
r16922 | .addClass('engine_num_input form-control'); | ||
|
r6201 | engines_col.append(input); | ||
|
r16913 | var start_button = $('<button/>').addClass("btn btn-default btn-xs").text("Start"); | ||
|
r16921 | var action_col = $('<div/>').addClass('action_col col-md-2').append( | ||
|
r10920 | $("<span/>").addClass("item_buttons btn-group").append( | ||
|
r10891 | start_button | ||
) | ||||
); | ||||
|
r10920 | this.element.empty() | ||
|
r10911 | .append(profile_col) | ||
|
r10920 | .append(status_col) | ||
|
r10911 | .append(engines_col) | ||
|
r10920 | .append(action_col); | ||
|
r6195 | start_button.click(function (e) { | ||
|
r6197 | var n = that.element.find('.engine_num_input').val(); | ||
|
r6199 | if (!/^\d+$/.test(n) && n.length>0) { | ||
|
r14634 | status_col.text('invalid engine #'); | ||
|
r6197 | } else { | ||
var settings = { | ||||
cache : false, | ||||
data : {n:n}, | ||||
type : "POST", | ||||
dataType : "json", | ||||
success : function (data, status, xhr) { | ||||
that.update_state(data); | ||||
}, | ||||
|
r16445 | error : function (xhr, status, error) { | ||
|
r14634 | status_col.text("error starting cluster"); | ||
|
r16445 | utils.log_ajax_error(xhr, status, error); | ||
|
r6197 | } | ||
}; | ||||
|
r14634 | status_col.text('starting'); | ||
|
r13693 | var url = utils.url_join_encode( | ||
|
r15238 | that.base_url, | ||
|
r13063 | 'clusters', | ||
that.data.profile, | ||||
'start' | ||||
); | ||||
|
r6197 | $.ajax(url, settings); | ||
|
r13063 | } | ||
|
r6195 | }); | ||
}; | ||||
|
r6197 | |||
|
r6195 | ClusterItem.prototype.state_running = function () { | ||
|
r6197 | var that = this; | ||
|
r16921 | var profile_col = $('<div/>').addClass('profile_col col-md-4').text(this.data.profile); | ||
var status_col = $('<div/>').addClass('status_col col-md-3').text('running'); | ||||
var engines_col = $('<div/>').addClass('engines_col col-md-3').text(this.data.n); | ||||
|
r16913 | var stop_button = $('<button/>').addClass("btn btn-default btn-xs").text("Stop"); | ||
|
r16921 | var action_col = $('<div/>').addClass('action_col col-md-2').append( | ||
|
r10920 | $("<span/>").addClass("item_buttons btn-group").append( | ||
|
r10891 | stop_button | ||
) | ||||
); | ||||
|
r10920 | this.element.empty() | ||
|
r10911 | .append(profile_col) | ||
|
r10920 | .append(status_col) | ||
|
r10911 | .append(engines_col) | ||
|
r10920 | .append(action_col); | ||
|
r6197 | stop_button.click(function (e) { | ||
var settings = { | ||||
cache : false, | ||||
type : "POST", | ||||
dataType : "json", | ||||
success : function (data, status, xhr) { | ||||
that.update_state(data); | ||||
}, | ||||
|
r16445 | error : function (xhr, status, error) { | ||
utils.log_ajax_error(xhr, status, error), | ||||
|
r14634 | status_col.text("error stopping cluster"); | ||
|
r6197 | } | ||
}; | ||||
|
r14634 | status_col.text('stopping'); | ||
|
r13693 | var url = utils.url_join_encode( | ||
|
r15238 | that.base_url, | ||
|
r13063 | 'clusters', | ||
that.data.profile, | ||||
'stop' | ||||
); | ||||
|
r6197 | $.ajax(url, settings); | ||
|
r6195 | }); | ||
}; | ||||
IPython.ClusterList = ClusterList; | ||||
IPython.ClusterItem = ClusterItem; | ||||
return IPython; | ||||
}(IPython)); | ||||