Files
attract/attract/static/js/tasks.js

94 lines
3.1 KiB
JavaScript
Raw Normal View History

/**
* Shows a task in the #task-details div.
*/
function task_open(task_id, project_url) {
if (task_id === undefined) {
if (console) console.log("task_open(undefined) called.");
return;
}
console.log('before anything');
$('#task-list').find('a').removeClass('active');
$('#task-' + task_id).addClass('active');
var task_url = '/attract/' + project_url + '/tasks/' + task_id;
console.log('task_url is ' + task_url);
$.get(task_url, function(task_data) {
$('#task-details').html(task_data);
}).fail(function(xhr) {
if (console) {
console.log('Error fetching task', task_id, 'from', task_url);
console.log('XHR:', xhr);
}
$('#task-details').html(xhr.responseText);
});
}
/**
* Create a task and show it in the #task-details div.
*/
function task_create(shot_id, project_url, task_type) {
var base_url = '/attract/' + project_url + '/tasks/create';
if (task_type != undefined) {
base_url += '/' + task_type;
}
$.post(base_url, function(task_data) {
task_open(task_data.task_id, project_url);
})
.fail(function(xhr) {
if (console) {
console.log('Error creating task');
console.log('XHR:', xhr);
}
$('#task-details').html(xhr.responseText);
});
}
function task_save(task_id, task_url) {
console.log('Saving task to', task_url);
var $form = $('#task-view form');
var $button = $form.find("button[type='submit']");
var payload = $form.serialize();
var task = '#task-' + task_id;
$button.attr('disabled', true);
$(task).addClass('processing');
$('#status-bar').text('Saving task...');
if (console) console.log('Sending:', payload);
$.post(task_url, payload)
.done(function(data) {
if (console) console.log('Done saving', data);
// Update the task list.
// NOTE: this is tightly linked to the HTML of the task list in for_project.jade.
$(task + ' span.name').text($form.find("input[name='name']").val());
$(task + ' span.type').text($form.find("select[name='task_type']").val());
$(task + ' span.status').text($form.find("select[name='status']").val().replace('_', ' '));
$(task)
.removeClass('col-list-item task-list-item')
.addClass('col-list-item task-list-item status-' + $form.find("select[name='status']").val());
$('#status-bar').text('Saved task. ' + data.time);
})
.fail(function(xhr_or_response_data) {
// jQuery sends the response data (if JSON), or an XHR object (if not JSON).
if (console) console.log('Failed saving', xhr_or_response_data);
2016-09-20 17:39:30 +02:00
$('#task-details').html(xhr_or_response_data.responseText);
2016-09-21 15:14:42 +02:00
$('#status-bar').text('Failed saving. ' + xhr_or_response_data.responseText);
})
.always(function() {
$button.attr('disabled', false);
$(task).removeClass('processing');
})
;
return false; // prevent synchronous POST to current page.
}