2016-09-23 13:58:14 +02:00
|
|
|
/**
|
|
|
|
* Removes the task from the task list and shot list, and show the 'task-add-link'
|
|
|
|
* when this was the last task in its category.
|
|
|
|
*/
|
|
|
|
function _remove_task_from_list(task_id) {
|
|
|
|
var $task_link = $('#task-' + task_id)
|
|
|
|
var $task_link_parent = $task_link.parent();
|
|
|
|
$task_link.hideAndRemove(300, function() {
|
|
|
|
if ($task_link_parent.children('.task-link').length == 0) {
|
|
|
|
$task_link_parent.find('.task-add-link').removeClass('hidden');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:39:56 +02:00
|
|
|
/**
|
2016-09-23 14:53:03 +02:00
|
|
|
* Open an item such as tasks/shots in the #item-details div
|
2016-09-21 19:39:56 +02:00
|
|
|
*/
|
2016-10-05 10:30:10 +02:00
|
|
|
function item_open(item_id, item_type, pushState, project_url)
|
2016-09-23 14:53:03 +02:00
|
|
|
{
|
|
|
|
if (item_id === undefined || item_type === undefined) {
|
|
|
|
throw new ReferenceError("item_open(" + item_id + ", " + item_type + ") called.");
|
2016-09-21 19:39:56 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 10:28:47 +02:00
|
|
|
if (typeof project_url === 'undefined') {
|
2016-10-05 10:30:10 +02:00
|
|
|
project_url = ProjectUtils.projectUrl();
|
|
|
|
if (typeof project_url === 'undefined') {
|
|
|
|
throw new ReferenceError("ProjectUtils.projectUrl() undefined");
|
|
|
|
}
|
2016-09-23 10:28:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-04 16:11:19 +02:00
|
|
|
if ($(window).scrollTop() > 0) {
|
|
|
|
$("html, body").animate({scrollTop: 0 }, '500', 'swing');
|
|
|
|
}
|
|
|
|
|
2016-09-23 14:53:03 +02:00
|
|
|
$('#col_right .col_header span.header_text').text(item_type + ' details');
|
2016-09-22 17:22:43 +02:00
|
|
|
|
2016-09-23 14:53:03 +02:00
|
|
|
// Style elements starting with item_type and dash, e.g. "#shot-uuid"
|
|
|
|
$('[id^="' + item_type + '-"]').removeClass('active');
|
|
|
|
$('#' + item_type + '-' + item_id).addClass('active');
|
2016-09-21 19:39:56 +02:00
|
|
|
|
2016-09-23 14:53:03 +02:00
|
|
|
var item_url = '/attract/' + project_url + '/' + item_type + 's/' + item_id;
|
|
|
|
var push_url = item_url;
|
|
|
|
if (ProjectUtils.context() == 'shot' && item_type == 'task'){
|
|
|
|
push_url = '/attract/' + project_url + '/shots/with-task/' + item_id;
|
|
|
|
}
|
2016-10-11 15:53:19 +02:00
|
|
|
item_url += '?context=' + ProjectUtils.context();
|
2016-09-21 19:39:56 +02:00
|
|
|
|
2016-10-04 14:26:37 +02:00
|
|
|
$('#status-bar').text('Loading ' + item_type + '…');
|
|
|
|
|
2016-09-23 14:53:03 +02:00
|
|
|
$.get(item_url, function(item_data) {
|
2016-10-04 14:26:37 +02:00
|
|
|
$('#status-bar').text('');
|
2016-09-23 14:53:03 +02:00
|
|
|
$('#item-details').html(item_data);
|
2016-09-21 19:39:56 +02:00
|
|
|
}).fail(function(xhr) {
|
|
|
|
if (console) {
|
2016-09-23 14:53:03 +02:00
|
|
|
console.log('Error fetching task', item_id, 'from', item_url);
|
2016-09-21 19:39:56 +02:00
|
|
|
console.log('XHR:', xhr);
|
|
|
|
}
|
2016-10-04 14:24:27 +02:00
|
|
|
$('#status-bar').text('Opening ' + item_type + ' failed.');
|
|
|
|
if (xhr.status) {
|
|
|
|
$('#item-details').html(xhr.responseText);
|
|
|
|
} else {
|
|
|
|
$('#item-details').html('<p class="text-danger">Opening ' + item_type + ' failed. There possibly was ' +
|
|
|
|
'an error connecting to the server. Please check your network connection and ' +
|
|
|
|
'try again.</p>');
|
|
|
|
}
|
2016-09-21 19:39:56 +02:00
|
|
|
});
|
|
|
|
|
2016-09-23 14:53:03 +02:00
|
|
|
// Determine whether we should push the new state or not.
|
|
|
|
pushState = (typeof pushState !== 'undefined') ? pushState : true;
|
|
|
|
if (!pushState) return;
|
2016-09-22 15:29:18 +02:00
|
|
|
|
2016-09-23 14:53:03 +02:00
|
|
|
// Push the correct URL onto the history.
|
|
|
|
var push_state = {itemId: item_id, itemType: item_type};
|
2016-09-23 10:28:47 +02:00
|
|
|
|
2016-09-23 14:53:03 +02:00
|
|
|
window.history.pushState(
|
|
|
|
push_state,
|
|
|
|
item_type + ': ' + item_id,
|
|
|
|
push_url
|
|
|
|
);
|
|
|
|
}
|
2016-09-22 17:22:43 +02:00
|
|
|
|
2016-10-05 10:30:10 +02:00
|
|
|
// Fine if project_url is undefined, but that requires ProjectUtils.projectUrl().
|
|
|
|
function task_open(task_id, project_url)
|
2016-09-23 14:53:03 +02:00
|
|
|
{
|
2016-10-05 10:30:10 +02:00
|
|
|
item_open(task_id, 'task', true, project_url);
|
2016-10-05 10:29:46 +02:00
|
|
|
|
|
|
|
if (ProjectUtils.context() == 'shot'){
|
|
|
|
$('[id^="shot-"]').removeClass('active');
|
|
|
|
$(this).parent().parent().addClass('active');
|
|
|
|
}
|
2016-09-23 14:53:03 +02:00
|
|
|
}
|
2016-09-22 15:29:18 +02:00
|
|
|
|
2016-09-23 14:53:03 +02:00
|
|
|
function shot_open(shot_id)
|
|
|
|
{
|
|
|
|
item_open(shot_id, 'shot');
|
|
|
|
}
|
2016-09-22 15:29:18 +02:00
|
|
|
|
2016-09-23 14:53:03 +02:00
|
|
|
window.onpopstate = function(event)
|
|
|
|
{
|
|
|
|
var state = event.state;
|
|
|
|
|
|
|
|
item_open(state.itemId, state.itemType, false);
|
2016-09-22 15:29:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:38:57 +02:00
|
|
|
/**
|
|
|
|
* Create a task and show it in the #item-details div.
|
2016-09-29 15:36:02 +02:00
|
|
|
* NOTE: Not used at the moment, we're creating shots via Blender's VSE
|
2016-09-23 19:38:57 +02:00
|
|
|
*/
|
|
|
|
function shot_create(project_url)
|
|
|
|
{
|
|
|
|
if (project_url === undefined) {
|
|
|
|
throw new ReferenceError("shot_create(" + project_url+ ") called.");
|
|
|
|
}
|
|
|
|
var url = '/attract/' + project_url + '/shots/create';
|
|
|
|
|
|
|
|
data = {
|
|
|
|
project_url: project_url
|
|
|
|
};
|
|
|
|
|
|
|
|
$.post(url, data, function(shot_data) {
|
|
|
|
shot_open(shot_data.shot_id);
|
|
|
|
})
|
|
|
|
.fail(function(xhr) {
|
|
|
|
if (console) {
|
|
|
|
console.log('Error creating task');
|
|
|
|
console.log('XHR:', xhr);
|
|
|
|
}
|
|
|
|
$('#item-details').html(xhr.responseText);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-29 15:36:02 +02:00
|
|
|
/**
|
2016-10-04 15:35:42 +02:00
|
|
|
* Adds the task item to the shots/tasks list.
|
|
|
|
*
|
|
|
|
* 'shot_id' can be undefined if the task isn't attached to a shot.
|
2016-09-29 15:36:02 +02:00
|
|
|
*/
|
2016-10-04 14:37:39 +02:00
|
|
|
function task_add(shot_id, task_id, task_type)
|
|
|
|
{
|
2016-10-04 15:35:42 +02:00
|
|
|
if (task_id === undefined || task_type === undefined) {
|
2016-10-04 14:37:50 +02:00
|
|
|
throw new ReferenceError("task_add(" + shot_id + ", " + task_id + ", " + task_type + ") called.");
|
|
|
|
}
|
2016-09-29 15:36:02 +02:00
|
|
|
|
2016-10-04 14:37:39 +02:00
|
|
|
var project_url = ProjectUtils.projectUrl();
|
2016-09-29 15:36:02 +02:00
|
|
|
var url = '/attract/' + project_url + '/tasks/' + task_id;
|
2016-10-04 15:35:42 +02:00
|
|
|
var context = ProjectUtils.context();
|
2016-09-29 15:36:02 +02:00
|
|
|
|
2016-10-04 15:35:42 +02:00
|
|
|
if (context == 'task') {
|
|
|
|
/* WARNING: This is a copy of an element of attract/tasks/for_project #task-list.col-list
|
|
|
|
* If that changes, change this too. */
|
2016-09-29 15:36:02 +02:00
|
|
|
$('#task-list').append('\
|
2016-10-04 15:35:42 +02:00
|
|
|
<a class="col-list-item task-list-item status-todo task-link active"\
|
2016-09-29 15:36:02 +02:00
|
|
|
href="' + url + '"\
|
|
|
|
data-task-id="' + task_id + '"\
|
|
|
|
id="task-' + task_id + '">\
|
|
|
|
<span class="status-indicator"></span>\
|
2016-10-04 15:35:42 +02:00
|
|
|
<span class="name">-save your task first-</span>\
|
|
|
|
<span class="type">-</span>\
|
2016-09-29 15:36:02 +02:00
|
|
|
</a>\
|
|
|
|
');
|
2016-10-04 15:35:42 +02:00
|
|
|
} else if (context == 'shot') {
|
|
|
|
if (shot_id === undefined) {
|
|
|
|
throw new ReferenceError("task_add(" + shot_id + ", " + task_id + ", " + task_type + ") called in shot context.");
|
|
|
|
}
|
|
|
|
|
2016-09-29 15:36:02 +02:00
|
|
|
var $shot_cell = $('#shot-' + shot_id + ' .table-cell.task-type.' + task_type);
|
|
|
|
var url = '/attract/' + project_url + '/shots/with-task/' + task_id;
|
|
|
|
|
2016-10-04 15:35:42 +02:00
|
|
|
/* WARNING: This is a copy of an element of attract/shots/for_project #task-list.col-list
|
|
|
|
* If that changes, change this too. */
|
2016-09-29 15:36:02 +02:00
|
|
|
$shot_cell.append('\
|
2016-10-04 15:35:42 +02:00
|
|
|
<a class="status-todo task-link active"\
|
|
|
|
title="-save your task first-"\
|
2016-09-29 15:36:02 +02:00
|
|
|
href="' + url + '"\
|
|
|
|
data-task-id="' + task_id + '"\
|
|
|
|
id="task-' + task_id + '">\
|
|
|
|
</a>\
|
|
|
|
');
|
|
|
|
|
|
|
|
$shot_cell.find('.task-add.task-add-link').addClass('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:39:56 +02:00
|
|
|
/**
|
2016-09-23 14:53:03 +02:00
|
|
|
* Create a task and show it in the #item-details div.
|
2016-10-04 15:21:59 +02:00
|
|
|
*
|
|
|
|
* 'shot_id' may be undefined, in which case the task will not
|
|
|
|
* be attached to a shot.
|
2016-09-21 19:39:56 +02:00
|
|
|
*/
|
2016-10-04 14:37:39 +02:00
|
|
|
function task_create(shot_id, task_type)
|
2016-09-23 14:53:03 +02:00
|
|
|
{
|
2016-10-04 15:21:59 +02:00
|
|
|
if (task_type === undefined) {
|
2016-10-04 14:37:39 +02:00
|
|
|
throw new ReferenceError("task_create(" + shot_id + ", " + task_type + ") called.");
|
2016-09-22 09:27:28 +02:00
|
|
|
}
|
2016-09-29 15:36:02 +02:00
|
|
|
|
2016-10-04 14:37:39 +02:00
|
|
|
var project_url = ProjectUtils.projectUrl();
|
2016-09-22 10:34:51 +02:00
|
|
|
var url = '/attract/' + project_url + '/tasks/create';
|
2016-10-04 15:21:59 +02:00
|
|
|
var has_shot_id = typeof shot_id !== 'undefined';
|
2016-09-21 19:39:56 +02:00
|
|
|
|
2016-09-22 10:34:51 +02:00
|
|
|
data = {
|
|
|
|
task_type: task_type,
|
|
|
|
};
|
2016-10-04 15:21:59 +02:00
|
|
|
if (has_shot_id) data.parent = shot_id;
|
|
|
|
|
2016-09-22 10:34:51 +02:00
|
|
|
$.post(url, data, function(task_data) {
|
2016-10-04 15:35:42 +02:00
|
|
|
if (console) console.log('Task created:', task_data);
|
2016-10-04 14:37:39 +02:00
|
|
|
task_open(task_data.task_id);
|
2016-10-04 15:35:42 +02:00
|
|
|
task_add(shot_id, task_data.task_id, task_type);
|
2016-09-21 19:39:56 +02:00
|
|
|
})
|
|
|
|
.fail(function(xhr) {
|
|
|
|
if (console) {
|
|
|
|
console.log('Error creating task');
|
|
|
|
console.log('XHR:', xhr);
|
|
|
|
}
|
2016-09-23 14:53:03 +02:00
|
|
|
$('#item-details').html(xhr.responseText);
|
2016-09-29 15:36:02 +02:00
|
|
|
})
|
|
|
|
.done(function(){
|
|
|
|
$('#item-details input[name="name"]').focus();
|
2016-09-21 19:39:56 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-29 16:52:41 +02:00
|
|
|
function attract_form_save(form_id, item_id, item_save_url, options)
|
2016-09-22 15:29:18 +02:00
|
|
|
{
|
2016-09-23 09:36:06 +02:00
|
|
|
// Mandatory option.
|
2016-09-29 16:52:41 +02:00
|
|
|
if (typeof options === 'undefined' || typeof options.type === 'undefined') {
|
2016-09-23 11:50:02 +02:00
|
|
|
throw new ReferenceError('attract_form_save(): options.type is mandatory.');
|
2016-09-23 09:36:06 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 15:29:18 +02:00
|
|
|
var $form = $('#' + form_id);
|
2016-09-08 11:24:58 +02:00
|
|
|
var $button = $form.find("button[type='submit']");
|
2016-09-22 15:29:18 +02:00
|
|
|
|
2016-09-08 11:24:58 +02:00
|
|
|
var payload = $form.serialize();
|
2016-09-22 15:29:18 +02:00
|
|
|
var $item = $('#' + item_id);
|
2016-09-08 11:24:58 +02:00
|
|
|
|
|
|
|
$button.attr('disabled', true);
|
2016-09-22 15:29:18 +02:00
|
|
|
$item.addClass('processing');
|
2016-09-22 17:46:17 +02:00
|
|
|
$('#status-bar').text('Saving ' + options.type + '...');
|
2016-09-08 11:24:58 +02:00
|
|
|
|
|
|
|
if (console) console.log('Sending:', payload);
|
2016-09-21 14:02:05 +02:00
|
|
|
|
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);
|
2016-09-22 17:46:17 +02:00
|
|
|
$('#status-bar')
|
|
|
|
.text('Saved ' + options.type + '. ' + saved_item._updated);
|
2016-09-22 18:28:11 +02:00
|
|
|
$form.find("input[name='_etag']").val(saved_item._etag);
|
2016-09-21 14:02:05 +02:00
|
|
|
|
2016-09-22 15:29:18 +02:00
|
|
|
if (options.done) options.done($item, saved_item);
|
2016-09-08 11:24:58 +02:00
|
|
|
})
|
|
|
|
.fail(function(xhr_or_response_data) {
|
|
|
|
// jQuery sends the response data (if JSON), or an XHR object (if not JSON).
|
2016-09-23 09:36:06 +02:00
|
|
|
if (console) console.log('Failed saving', options.type, xhr_or_response_data);
|
2016-09-22 18:28:11 +02:00
|
|
|
|
|
|
|
$button.removeClass('btn-default').addClass('btn-danger');
|
|
|
|
$('#status-bar').text('Failed saving. ' + xhr_or_response_data.status);
|
2016-09-22 15:29:18 +02:00
|
|
|
|
|
|
|
if (options.fail) options.fail($item, xhr_or_response_data);
|
2016-09-08 11:24:58 +02:00
|
|
|
})
|
|
|
|
.always(function() {
|
|
|
|
$button.attr('disabled', false);
|
2016-09-22 15:29:18 +02:00
|
|
|
$item.removeClass('processing');
|
|
|
|
|
|
|
|
if (options.always) options.always($item);
|
2016-09-08 11:24:58 +02:00
|
|
|
})
|
2016-09-21 14:02:05 +02:00
|
|
|
;
|
2016-09-08 11:24:58 +02:00
|
|
|
|
|
|
|
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);
|
2016-09-23 19:39:14 +02:00
|
|
|
$task.find('span.type').text(saved_task.properties.task_type);
|
2016-09-22 15:29:18 +02:00
|
|
|
$task.find('span.status').text(saved_task.properties.status.replace('_', ' '));
|
|
|
|
|
|
|
|
$task
|
2016-09-22 15:44:24 +02:00
|
|
|
.removeClassPrefix('status-')
|
|
|
|
.addClass('status-' + saved_task.properties.status)
|
|
|
|
.flashOnce()
|
|
|
|
;
|
2016-10-04 14:37:58 +02:00
|
|
|
|
|
|
|
task_open(task_id);
|
2016-09-22 15:29:18 +02:00
|
|
|
},
|
|
|
|
fail: function($item, xhr_or_response_data) {
|
2016-09-22 18:28:11 +02:00
|
|
|
if (xhr_or_response_data.status == 412) {
|
|
|
|
// TODO: implement something nice here. Just make sure we don't throw
|
|
|
|
// away the user's edits. It's up to the user to handle this.
|
|
|
|
} else {
|
2016-09-23 14:53:03 +02:00
|
|
|
$('#item-details').html(xhr_or_response_data.responseText);
|
2016-09-22 18:28:11 +02:00
|
|
|
}
|
2016-09-22 17:46:17 +02:00
|
|
|
},
|
|
|
|
type: 'task'
|
2016-09-22 15:29:18 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2016-09-22 15:44:24 +02:00
|
|
|
$('.shot-name-' + saved_shot._id).text(saved_shot.name);
|
2016-09-22 15:29:18 +02:00
|
|
|
$shot
|
2016-09-22 15:44:24 +02:00
|
|
|
.removeClassPrefix('status-')
|
|
|
|
.addClass('status-' + saved_shot.properties.status)
|
|
|
|
.flashOnce()
|
|
|
|
;
|
2016-10-04 14:37:58 +02:00
|
|
|
shot_open(shot_id);
|
2016-09-22 15:29:18 +02:00
|
|
|
},
|
|
|
|
fail: function($item, xhr_or_response_data) {
|
2016-09-22 18:28:11 +02:00
|
|
|
if (xhr_or_response_data.status == 412) {
|
|
|
|
// TODO: implement something nice here. Just make sure we don't throw
|
|
|
|
// away the user's edits. It's up to the user to handle this.
|
|
|
|
} else {
|
2016-09-23 14:53:03 +02:00
|
|
|
$('#item-details').html(xhr_or_response_data.responseText);
|
2016-09-22 18:28:11 +02:00
|
|
|
}
|
2016-09-22 17:46:17 +02:00
|
|
|
},
|
|
|
|
type: 'shot'
|
2016-09-22 15:29:18 +02:00
|
|
|
});
|
|
|
|
}
|
2016-09-23 10:28:47 +02:00
|
|
|
|
2016-09-23 13:58:14 +02:00
|
|
|
function task_delete(task_id, task_etag, task_delete_url) {
|
|
|
|
if (task_id === undefined || task_etag === undefined || task_delete_url === undefined) {
|
|
|
|
throw new ReferenceError("task_delete(" + task_id + ", " + task_etag + ", " + task_delete_url + ") called.");
|
|
|
|
}
|
|
|
|
|
2016-09-29 15:35:31 +02:00
|
|
|
$('#task-' + task_id).addClass('processing');
|
|
|
|
|
2016-09-23 13:58:14 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'DELETE',
|
|
|
|
url: task_delete_url,
|
|
|
|
data: {'etag': task_etag}
|
|
|
|
})
|
|
|
|
.done(function(e) {
|
|
|
|
if (console) console.log('Task', task_id, 'was deleted.');
|
2016-09-29 15:35:31 +02:00
|
|
|
$('#item-details').fadeOutAndClear();
|
2016-09-23 13:58:14 +02:00
|
|
|
_remove_task_from_list(task_id);
|
|
|
|
})
|
|
|
|
.fail(function(xhr) {
|
|
|
|
$('#status-bar').text('Unable to delete task, code ' + xhr.status);
|
|
|
|
if (xhr.status == 412) {
|
|
|
|
alert('Someone else edited this task before you deleted it; refresh to try again.');
|
|
|
|
// TODO: implement something nice here. Just make sure we don't throw
|
|
|
|
// away the user's edits. It's up to the user to handle this.
|
|
|
|
// TODO: refresh activity feed and point user to it.
|
|
|
|
} else {
|
|
|
|
// TODO: find a better place to put this error message, without overwriting the
|
|
|
|
// task the user is looking at in-place.
|
|
|
|
$('#task-view-feed').html(xhr.responseText);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-23 10:28:47 +02:00
|
|
|
$(function() {
|
|
|
|
$("a.shot-link[data-shot-id]").click(function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
// delegateTarget is the thing the event hander was attached to,
|
|
|
|
// rather than the thing we clicked on.
|
|
|
|
var shot_id = e.delegateTarget.dataset.shotId;
|
|
|
|
shot_open(shot_id);
|
|
|
|
});
|
2016-09-29 14:29:09 +02:00
|
|
|
|
2016-09-23 10:28:47 +02:00
|
|
|
$("a.task-link[data-task-id]").click(function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var task_id = e.delegateTarget.dataset.taskId;
|
2016-10-05 10:30:10 +02:00
|
|
|
var project_url = e.delegateTarget.dataset.projectUrl; // fine if undefined
|
|
|
|
task_open(task_id, project_url);
|
2016-09-23 10:28:47 +02:00
|
|
|
});
|
|
|
|
});
|