$(document).ready(function() {
// Setup initial state
var ids = [];
$.each(KT.repo_status, function(repo_id, status){
if (status.is_running) {
ids.push(repo_id);
KT.content.draw_syncing(repo_id, status.progress.progress, status.sync_id);
}
});
KT.content.reset_products(KT.repo_status);
KT.content_actions.addSyncing(ids);
$('#select_all').click(KT.content.select_all);
$('#select_none').click(KT.content.select_none);
$('#collapse_all').click(KT.content.collapse_all);
$('#expand_all').click(KT.content.expand_all);
KT.content.showAll();
KT.content.select_repo();
$("#products_table").delegate(".cancel_sync", "click", function(){
var repo_id = $(this).parents("tr").attr("data-id");
KT.content_actions.cancelSync(repo_id, $(this));
});
$('#sync_product_form').bind("ajax:success",
function(evt, data, status, xhr){
var ids = [];
$.each(data, function(index, item){
ids.push(item.id);
KT.content.draw_syncing(item.id, 0, undefined);
KT.content.updateProduct(item.product_id, false, 0);
});
KT.content_actions.addSyncing(ids);
})
.bind("ajax:beforeSend",
function(evt, data, status, xhr) {
if ($("input[name='repoids[]']:checked").length === 0) {
return false;
}
});
$("#sync_toggle").change(function(){
var img = "";
$("#sync_toggle_cont").append(img);
if ($(this).is(":checked")){
KT.content.showOnlySyncing();
}
else {
KT.content.showAll();
}
$("#sync_toggle_cont").find("img").remove();
});
$.each($("input[name='repoids[]']"), function(index, checkbox) {
$(checkbox).click(KT.content.select_repo);
});
});
KT.content_actions = (function(){
var syncing = [],
updater,
getOrg = function() {
return $('#organization_id').val();
},
addSyncing = function(repo_ids){
if (repo_ids.length === 0){
return;
}
//nothing in the list before adding and updater already exists
var start = syncing.length === 0 && updater;
$.each(repo_ids, function(index, id){
syncing.push(id + "");
});
if (!updater){
startUpdater();
}
else if (start){
updater.restart();
}
},
removeSyncing = function(repo_id){
syncing.splice($.inArray(repo_id + "", syncing), 1);
if (syncing.length === 0 && updater){
updater.stop();
}
},
getSyncing = function(){
return syncing;
},
cancelSync = function(repo_id){
var button = $("#repo-" + repo_id).find(".result .cancel_sync");
if( !$(button).hasClass('disabled') ){
button.addClass('disabled');
$.ajax({
type: 'DELETE',
url: foreman_url('/katello/sync_management/' + repo_id + '?organization_id=' + getOrg()),
dataType: 'json',
success: function(data) {
},
error: function(data) {
button.removeClass('disabled');
}
});
}
},
startUpdater = function(){
if (syncing.length ===0){
return;
}
var url = foreman_url('/katello/sync_management/sync_status');
updater = $.PeriodicalUpdater(url, {
data: function(){return {repoids:getSyncing(), organization_id:getOrg()}},
method: 'get',
type: 'json',
global: false
},
function(data,success) {
if (success === "notmodified") {
return;
}
$.each(data, function(index, repo){
// Only stop when we reach 100% and the finish_time is done sometimes they are not both complete
if (!repo.is_running && (repo.raw_state !== 'waiting')) {
removeSyncing(repo.id);
KT.content.updateRepo(repo.id, repo.start_time, repo.duration, repo.progress.progress, repo.display_size, repo.packages, repo.size, repo.sync_id);
KT.content.finishRepo(repo.id, repo.state, repo.duration, repo.raw_state, repo.error_details, repo.sync_id);
KT.content.updateProduct(repo.product_id, false, false, true);
}
else {
KT.content.updateRepo( repo.id,
repo.start_time,
repo.duration,
repo.progress.progress,
repo.display_size,
repo.packages,
repo.size,
repo.sync_id);
}
});
KT.content.reset_products(data);
},
function(){
updater.stop();
}
);
};
return {
cancelSync: cancelSync,
addSyncing: addSyncing,
startUpdater: startUpdater,
getSyncing: function(){return syncing}
};
})();
KT.content = (function(){
var draw_syncing = function(repo_id, progress, task_id){
var element = $("#repo-" + repo_id).find(".result"),
cancelButton = $('').attr("class", "cancel_sync").text(katelloI18n.cancel),
progressBar = $('').attr('class', 'progress').text(" ");
if(task_id !== undefined) {
progressBar.attr('href', foreman_url('/foreman_tasks/tasks/' + task_id));
}
progress = progress ? progress : 0;
progressBar.progressbar({
value: progress
});
element.find('.result-info').html("");
element.find('.result-info').append(progressBar);
element.find('.info-tipsy').addClass('hidden');
if( KT.permissions.syncable ){
element.find('.result-info').append(cancelButton);
}
},
updateRepo = function(repo_id, starttime, duration, progress, display_size, packages, size, task_id){
var repo = $("#repo-" + repo_id);
update_item(repo, starttime, duration, progress, display_size, packages, size, task_id);
},
finishRepo = function(repo_id, state, duration, raw_state, error_details, task_id){
var element = $("#repo-" + repo_id);
var messages = [];
var url = foreman_url('/foreman_tasks/tasks/' + task_id);
state = '' + state + '';
element.find(".result .result-info").html(state);
fadeUpdate(element.find(".duration"), duration);
if (raw_state === 'error') {
element.find('.result .info-tipsy').removeClass('hidden');
KT.utils.forEach(error_details['messages'], function (message) {
messages.push('