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

180 lines
5.9 KiB
JavaScript
Raw Normal View History

(function ( $ ) {
$.fn.flashOnce = function() {
var target = this;
this
.addClass('flash-on')
.delay(200) // this delay is linked to the transition in the flash-on CSS class.
.queue(function() {
target
.removeClass('flash-on')
.addClass('flash-off')
.dequeue()
;})
.delay(1000) // this delay is just to clean up the flash-X classes.
.queue(function() {
target
.removeClass('flash-on flash-off')
.dequeue()
;})
;
return this;
};
}(jQuery));
/**
* Shows a task in the #task-details div.
*/
function task_open(task_id, project_url) {
if (task_id === undefined || project_url === undefined) {
if (console) console.log("task_open(", task_id, project_url, ") called.");
return;
}
$('#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);
});
}
2016-09-22 15:29:18 +02:00
/**
* Shows a shot in the #task-details div.
*/
function shot_open(shot_id, project_url) {
if (shot_id === undefined || project_url === undefined) {
if (console) console.log("shot_open(", shot_id, project_url, ") called.");
return;
}
$('#shot-list').find('a').removeClass('active');
$('#shot-link' + shot_id).addClass('active');
var shot_url = '/attract/' + project_url + '/shots/' + shot_id;
console.log('shot_url is ' + shot_url);
$.get(shot_url, function(shot_data) {
$('#task-details').html(shot_data);
}).fail(function(xhr) {
if (console) {
console.log('Error fetching shot', shot_id, 'from', shot_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) {
if (shot_id === undefined || project_url === undefined || task_type === undefined) {
if (console) console.log("task_create(", shot_id, project_url, task_type, ") called.");
return;
}
var url = '/attract/' + project_url + '/tasks/create';
data = {
task_type: task_type,
parent: shot_id,
};
$.post(url, data, 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);
});
}
2016-09-22 15:29:18 +02:00
function attract_form_save(form_id, item_id, item_save_url, options={})
{
var $form = $('#' + form_id);
var $button = $form.find("button[type='submit']");
2016-09-22 15:29:18 +02:00
var payload = $form.serialize();
2016-09-22 15:29:18 +02:00
var $item = $('#' + item_id);
$button.attr('disabled', true);
2016-09-22 15:29:18 +02:00
$item.addClass('processing');
$('#status-bar').text('Saving task...');
if (console) console.log('Sending:', payload);
2016-09-22 15:29:18 +02:00
$.post(item_save_url, payload)
.done(function(saved_item) {
if (console) console.log('Done saving', saved_item);
$('#status-bar').text('Saved item. ' + saved_item._updated);
2016-09-22 15:29:18 +02:00
if (options.done) options.done($item, saved_item);
})
.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-21 15:14:42 +02:00
$('#status-bar').text('Failed saving. ' + xhr_or_response_data.responseText);
2016-09-22 15:29:18 +02:00
if (options.fail) options.fail($item, xhr_or_response_data);
})
.always(function() {
$button.attr('disabled', false);
2016-09-22 15:29:18 +02:00
$item.removeClass('processing');
if (options.always) options.always($item);
})
;
return false; // prevent synchronous POST to current page.
}
2016-09-22 15:29:18 +02:00
function task_save(task_id, task_url) {
return attract_form_save('task_form', 'task-' + task_id, task_url, {
done: function($task, saved_task) {
// Update the task list.
// NOTE: this is tightly linked to the HTML of the task list in for_project.jade.
$('.task-name-' + saved_task._id).text(saved_task.name).flashOnce();
$task.find('span.name').text(saved_task.name);
$task.find('span.type').text(saved_task.task_type);
$task.find('span.status').text(saved_task.properties.status.replace('_', ' '));
// FIXME: remove all existing status-XXX classes.
$task
.removeClass('col-list-item task-list-item')
.addClass('col-list-item task-list-item status-' + saved_task.properties.status);
},
fail: function($item, xhr_or_response_data) {
$('#task-details').html(xhr_or_response_data.responseText);
}
});
}
function shot_save(shot_id, shot_url) {
return attract_form_save('shot_form', 'shot-' + shot_id, shot_url, {
done: function($shot, saved_shot) {
// Update the shot list.
$('.shot-name-' + saved_shot._id).text(saved_shot.name).flashOnce();
// FIXME: remove all existing status-XXX classes.
$shot
.removeClass('col-list-item shot-list-item')
.addClass('col-list-item shot-list-item status-' + saved_shot.properties.status);
},
fail: function($item, xhr_or_response_data) {
$('#task-details').html(xhr_or_response_data.responseText);
}
});
}