pandora/static/js/tasksDialog.js

350 lines
12 KiB
JavaScript
Raw Permalink Normal View History

2016-08-13 11:56:53 +00:00
'use strict';
2016-09-15 15:20:24 +00:00
pandora.ui.tasksDialog = function(options) {
options = Ox.extend({
tasks: 'all'
}, options || {});
2016-08-13 11:56:53 +00:00
var canceling = [],
2016-09-15 15:20:24 +00:00
clientItems = [], serverItems = [], listItems = [],
clientTimeout, serverTimeout,
2016-08-13 11:56:53 +00:00
$list = Ox.TableList({
columns: [
{
id: 'id',
title: Ox._('ID'),
visible: false
},
2016-09-15 15:20:24 +00:00
{
format: function(value) {
return Ox.encodeHTMLEntities(value);
},
id: 'user',
operator: '+',
title: Ox._('User'),
visible: false,
width: 144
},
2016-08-13 11:56:53 +00:00
{
format: function(value) {
return Ox.encodeHTMLEntities(value);
},
id: 'title',
operator: '+',
title: Ox._('Title'),
visible: true,
2016-09-15 15:20:24 +00:00
width: 288
2016-08-13 11:56:53 +00:00
},
{
format: function(value) {
return Ox.formatDate(value, '%Y-%m-%d %H:%M:%S');
},
id: 'started',
operator: '-',
title: Ox._('Started'),
visible: true,
width: 144
},
{
format: function(value) {
return value
? Ox.formatDate(value, '%Y-%m-%d %H:%M:%S')
: '';
},
id: 'ended',
operator: '-',
sort: function(value, data) {
return value || 1e13 + data.started;
},
title: Ox._('Ended'),
visible: true,
width: 144
},
{
2016-09-15 15:20:24 +00:00
format: function(value, data) {
return {
'pending': 'Uploading (Queued)',
'uploading': 'Uploading' + (
data.progress === void 0 ? '' : ' (' + (
data.progress === 0 ? 'Queued'
: data.progress + '%'
) + ')'
),
'queued': $tasksSelect.value() == 'all'
? 'Processing (Queued)'
: 'Finished',
'processing': 'Processing',
'canceled': 'Canceled',
'failed': 'Failed',
'finished': 'Finished'
}[value] || value;
2016-08-13 11:56:53 +00:00
},
id: 'status',
operator: '+',
sort: function(value) {
return [
2016-09-15 15:20:24 +00:00
'pending', 'uploading', 'queued', 'processing',
2016-08-13 11:56:53 +00:00
'canceled', 'failed', 'finished'
].indexOf(value);
},
title: Ox._('Status'),
visible: true,
2016-09-25 13:22:27 +00:00
width: 176 - Ox.SCROLLBAR_SIZE
2016-09-15 15:20:24 +00:00
},
{
id: 'progress',
title: Ox._('Progress'),
visible: false
2016-08-13 11:56:53 +00:00
}
],
columnsVisible: true,
2016-09-15 15:20:24 +00:00
items: listItems,
2016-09-23 20:59:08 +00:00
scrollbarVisible: true,
2016-08-13 11:56:53 +00:00
sort: [{key: 'ended', operator: '-'}],
unique: 'id'
}).bindEvent({
2016-09-15 15:20:24 +00:00
select: updateButton,
open: function(data) {
var item;
if (data.ids.length == 1) {
item = listItems.filter(function(item) {
return item.id == data.ids[0];
})[0];
if (item && item.item) {
that.close();
pandora.UI.set({
item: item.item,
itemView: 'info'
});
}
}
}
2016-08-13 11:56:53 +00:00
}),
$sidebar = Ox.Element().css({
margin: '4px'
}),
2016-09-15 15:20:24 +00:00
$tasksSelect = Ox.Select({
items: [
{id: 'uploads', title: Ox._('Show Uploads')},
{id: 'all', title: Ox._('Show All Tasks')}
],
value: options.tasks,
width: 128
2016-08-13 11:56:53 +00:00
}).css({
2016-09-15 15:20:24 +00:00
margin: '4px'
}).bindEvent({
change: updateList
})
.appendTo($sidebar),
$usersSelect = Ox.Select({
items: [
{id: 'my', title: Ox._('Show My Tasks')},
{id: 'all', title: Ox._('Show All Users')}
],
width: 128
}).css({
2017-11-04 09:53:27 +00:00
display: pandora.hasCapability('canSeeAllTasks') ? 'block' : 'none',
2016-09-15 15:20:24 +00:00
margin: '8px 4px 4px 4px'
2016-08-13 11:56:53 +00:00
}).bindEvent({
2016-09-15 15:20:24 +00:00
change: function(data) {
$list[data.value == 'all' ? 'addColumn' : 'removeColumn']('user');
$list.resizeColumn('title', data.value == 'all' ? 144 : 288);
setTimeout(updateList);
}
2016-08-13 11:56:53 +00:00
})
.appendTo($sidebar),
$button = Ox.Button({
disabled: true,
title: Ox._('Cancel Task'),
2016-09-15 15:20:24 +00:00
width: 128
2016-08-13 11:56:53 +00:00
}).css({
margin: '4px',
}).bindEvent({
click: function() {
2016-09-15 15:20:24 +00:00
$button.options({disabled: true});
2016-08-13 11:56:53 +00:00
var ids = $list.options('selected').filter(canBeCanceled);
canceling.push(ids);
2016-09-15 15:20:24 +00:00
ids.forEach(function(id) {
if (Ox.contains(
['pending', 'uploading'], $list.value(id, 'status')
)) {
pandora.uploadQueue.remove(id);
}
})
2016-08-13 11:56:53 +00:00
pandora.api.cancelTask({
id: ids
}, function() {
2016-09-15 15:20:24 +00:00
canceling = canceling.filter(function(id) {
return !Ox.contains(ids, id);
});
getServerItems();
2016-08-13 11:56:53 +00:00
});
}
}).appendTo($sidebar),
$panel = Ox.SplitPanel({
elements: [
{
element: $list,
2016-09-15 15:20:24 +00:00
size: 752
2016-08-13 11:56:53 +00:00
},
{
element: $sidebar,
}
],
orientation: 'horizontal'
}),
that = Ox.Dialog({
buttons: [
Ox.Button({
id: 'done',
title: Ox._('Done')
}).bindEvent({
click: function() {
that.close();
}
})
],
closeButton: true,
content: $panel,
height: 384,
2016-09-19 21:13:02 +00:00
removeOnClose: true,
2016-08-13 11:56:53 +00:00
title: Ox._('Tasks'),
2016-09-15 15:20:24 +00:00
width: 896
2016-08-13 11:56:53 +00:00
})
.bindEvent({
close: function() {
2016-09-15 15:20:24 +00:00
clientTimeout && clearTimeout(clientTimeout);
2016-09-19 21:13:02 +00:00
serverTimeout && clearTimeout(serverTimeout);
2016-08-13 11:56:53 +00:00
},
2016-09-15 15:20:24 +00:00
open: function() {
getClientItems();
getServerItems();
}
2016-08-13 11:56:53 +00:00
});
function canBeCanceled(id) {
return !Ox.contains(
['cancelled', 'failed', 'finished'],
$list.value(id, 'status')
) && !Ox.contains(canceling, id);
}
2016-09-15 15:20:24 +00:00
function getClientItems(callback) {
clearTimeout(clientTimeout);
var uploads = pandora.uploadQueue.get();
var uploadsById = {};
uploads.forEach(function(upload) {
uploadsById[upload.item.id] = (
uploadsById[upload.item.id] || []
).concat(upload);
});
clientItems = []
Ox.forEach(uploadsById, function(uploads, id) {
// FIXME: include upload.file.size
var progress = Math.round(Ox.sum(uploads.map(function(upload) {
return upload.data.progress / uploads.length;
})) * 100);
var status = uploads.map(function(upload) {
return upload.data.status;
});
clientItems.push({
ended: uploads.every(function(upload) {
return upload.data.ended;
}) ? Ox.max(uploads.map(function(upload) {
return upload.data.ended;
})) : '',
id: id,
progress: progress,
started: Ox.min(uploads.map(function(upload) {
return upload.data.started;
})),
status: Ox.contains(status, 'uploading') ? 'uploading'
: Ox.contains(status, 'pending') ? 'pending'
: Ox.contains(status, 'canceled') ? 'canceled'
: 'queued',
title: uploads[0].item.title,
user: pandora.user.username
});
});
clientTimeout = setTimeout(getClientItems, 1000);
updateValues();
callback && callback();
}
function getListItems() {
var allTasks = $tasksSelect.value() == 'all',
uploading = allTasks
? ['pending', 'uploading']
: ['pending', 'uploading', 'queued'],
items = clientItems.filter(function(item) {
return Ox.contains(uploading, item.status);
}),
items = serverItems.filter(function(item) {
return allTasks ? (
Ox.getIndex(items, 'id', item.item) == -1
|| item.user != pandora.user.username
) : (
Ox.getIndex(items, 'id', item.item) == -1
&& Ox.getIndex(clientItems, 'id', item.item) > -1
);
}).concat(items);
return items;
}
function getServerItems(callback) {
clearTimeout(serverTimeout);
pandora.api.getTasks($usersSelect.value() == 'all' ? {} : {
2016-08-13 11:56:53 +00:00
user: pandora.user.username
}, function(result) {
2016-09-15 15:20:24 +00:00
serverItems = result.data.items;
serverTimeout = setTimeout(getServerItems, 10000);
getClientItems(callback);
2016-08-13 11:56:53 +00:00
});
}
function updateButton() {
var ids = $list.options('selected').filter(canBeCanceled);
$button.options({
disabled: ids.length == 0,
title: ids.length < 2 ? 'Cancel Task' : 'Cancel Tasks'
});
}
2016-09-15 15:20:24 +00:00
function updateList() {
getServerItems(function() {
listItems= getListItems();
$list.options({items: listItems});
updateButton();
});
}
function updateValues() {
var currentListItems = getListItems(),
hasNewItems = currentListItems.length != listItems.length;
!hasNewItems && currentListItems.forEach(function(item) {
if (Ox.getIndexById(listItems, item.id) == -1) {
hasNewItems = true;
} else if (!hasNewItems) {
2016-09-19 23:17:22 +00:00
['progress', 'status'].concat(
item.ended ? ['ended'] : []
).forEach(function(key) {
$list.value(item.id, key, item[key]);
});
2016-09-15 15:20:24 +00:00
}
});
hasNewItems && updateList();
}
2016-08-13 11:56:53 +00:00
return that;
2016-08-17 12:37:59 +00:00
};