openmedialibrary/static/js/importExportDialog.js

411 lines
14 KiB
JavaScript
Raw Normal View History

2014-05-17 08:17:34 +00:00
'use strict';
oml.ui.importExportDialog = function(selected) {
var ui = oml.user.ui,
$bar = Ox.Bar({size: 24}),
$buttons = Ox.ButtonGroup({
buttons: [
{id: 'import', title: Ox._('Import Books')},
{id: 'export', title: Ox._('Export Books')}
],
min: 1,
max: 1,
selectable: true,
2016-01-12 13:32:27 +00:00
selected: selected,
style: 'squared'
2014-05-17 08:17:34 +00:00
})
.css({
width: '768px',
padding: '4px 0',
textAlign: 'center'
})
.bindEvent({
change: function(data) {
oml.UI.set({page: data.value});
}
})
.appendTo($bar),
$innerPanel = Ox.SlidePanel({
elements: [
{id: 'import', element: Ox.Element()},
{id: 'export', element: Ox.Element()}
],
orientation: 'horizontal',
selected: selected,
size: 512
}),
$outerPanel = Ox.SplitPanel({
elements: [
{element: $bar, size: 24},
{element: $innerPanel}
],
orientation: 'vertical'
}),
that = Ox.Dialog({
buttons: [
Ox.Button({
2014-05-18 23:24:04 +00:00
id: 'hide',
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-18 23:24:04 +00:00
title: Ox._('Hide')
2014-05-17 08:17:34 +00:00
})
.bindEvent({
click: function() {
that.close();
}
})
],
closeButton: true,
content: Ox.LoadingScreen().start(),
height: 144,
removeOnClose: true,
title: Ox._('Import & Export Books'),
width: 512
})
.bindEvent({
close: function() {
if (Ox.contains(['import', 'export'], ui.page)) {
oml.UI.set({page: ''});
}
},
oml_page: function(data) {
if (Ox.contains(['import', 'export'], data.value)) {
selected = data.value;
$buttons.options({selected: selected});
$innerPanel.options({selected: selected});
}
}
}),
$label = {},
$activityButton = {},
$progress = {},
$status = {},
$progressButton = {};
2014-05-18 23:24:04 +00:00
oml.getLists(function() {
2014-05-17 08:17:34 +00:00
oml.api.getActivity(function(result) {
var isActive = !Ox.isEmpty(result.data),
activity = result.data.activity;
/*
result.data = {
path: '/Users/rolux/Desktop/Books',
status: {},
progress: [0,42]
};
*/
$innerPanel
.replaceElement(0,
activity == 'import' ? renderActivity(result.data)
: renderForm('import', isActive)
)
.replaceElement(1,
activity == 'export' ? renderActivity(result.data)
: renderForm('export', isActive)
);
that.options({content: $outerPanel});
});
});
function getListItems(selected) {
var lists = ui._lists.filter(function(list) {
2014-10-31 11:46:32 +00:00
return list.user == '' && list.type != 'library' && (
2014-05-17 08:17:34 +00:00
selected == 'export' || list.type == 'static'
);
});
return [
2014-05-17 14:42:46 +00:00
{id: '', title: Ox._('Library')}
2014-05-17 08:17:34 +00:00
].concat(
lists.length ? [{}] : []
).concat(
lists.map(function(list) {
2016-01-10 09:47:41 +00:00
return {
id: Ox.encodeHTMLEntities(list.name),
title: Ox.encodeHTMLEntities(list.name)
};
2014-05-17 08:17:34 +00:00
})
).concat(selected == 'import' ? [
{},
2014-05-17 14:42:46 +00:00
{id: 'FIXME', title: Ox._('New List...')}
2014-05-17 08:17:34 +00:00
] : []);
}
function getListNames() {
return ui._lists.filter(function(list) {
2014-10-31 11:46:32 +00:00
return list.user == '';
2014-05-17 08:17:34 +00:00
}).map(function(list) {
return list.name;
});
}
function renderActivity(data) {
var $element = Ox.Element(),
$title = $('<div>')
.addClass('OxSelectable')
.css({
margin: '16px 16px 4px 16px',
width: '480px',
height: '16px',
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis'
})
.html(
Ox._(data.activity == 'import' ? 'Import from' : 'Export to')
+ ' <span class="OxMonospace">' + data.path + '</span>'
)
.appendTo($element);
$progress[data.activity] = Ox.Progressbar({
progress: -1,
showTooltips: true,
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 08:17:34 +00:00
width: 480
})
.css({margin: '4px 16px'})
.appendTo($element);
$status[data.activity] = $('<div>')
.addClass('OxSelectable')
.css({
margin: '6px 16px 4px 16px',
height: '16px'
})
.appendTo($element);
$progressButton[data.activity] = Ox.Button({
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 08:17:34 +00:00
title: '',
width: 128
})
.css({
position: 'absolute',
right: '16px',
bottom: '16px'
})
.bindEvent({
click: function() {
if (this.options('title') == Ox._('Done')) {
$innerPanel.replaceElement(0, renderForm(data.activity));
} else {
oml.api[
data.activity == 'import' ? 'cancelImport' : 'cancelExport'
](function() {
// ...
});
}
}
})
.appendTo($element);
if (data.progress) {
setProgress(data);
setStatus(data);
setButton(data);
}
return $element;
}
function renderForm(selected, isActive) {
var $element = Ox.Element(),
$form = Ox.Form({
items: selected == 'import' ? [
Ox.Input({
2014-05-18 23:24:04 +00:00
autocomplete: function(value, callback) {
oml.api.autocompleteFolder({path: value}, function(result) {
callback(result.data.items);
});
},
autocompleteSelect: true,
2014-05-17 08:17:34 +00:00
changeOnKeypress: true,
id: 'path',
label: 'Source Path',
labelWidth: 128,
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 08:17:34 +00:00
width: 480
}),
Ox.SelectInput({
id: 'list',
2014-05-18 23:24:04 +00:00
inputValue: oml.getValidName(Ox._('Untitled'), getListNames()),
2014-05-17 08:17:34 +00:00
inputWidth: 224,
items: getListItems('import'),
label: 'Destination',
labelWidth: 128,
max: 1,
min: 1,
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 14:42:46 +00:00
value: '',
2014-05-17 08:17:34 +00:00
width: 480
}),
Ox.Select({
id: 'mode',
items: [
2014-05-18 23:24:04 +00:00
{id: 'copy', title: Ox._('Copy (keep files in source path)')},
{id: 'move', title: Ox._('Move (delete files from source path)')}
2014-05-17 08:17:34 +00:00
],
label: Ox._('Import Mode'),
labelWidth: 128,
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 08:17:34 +00:00
width: 480
})
] : [
Ox.Input({
changeOnKeypress: true,
id: 'path',
label: 'Destination Path',
labelWidth: 128,
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 08:17:34 +00:00
width: 480
}),
Ox.Select({
id: 'list',
items: getListItems('export'),
label: 'Source',
labelWidth: 128,
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 08:17:34 +00:00
value: ':',
width: 480
}),
Ox.Select({
id: 'mode',
items: [
2014-05-21 00:02:39 +00:00
{id: 'add', title: Ox._('Add (keep all exisiting files in destination path)')},
{id: 'replace', title: Ox._('Replace (delete all existing files from destination path)')}
2014-05-17 08:17:34 +00:00
],
2014-05-18 23:24:04 +00:00
label: Ox._('Export Mode'),
2014-05-17 08:17:34 +00:00
labelWidth: 128,
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 08:17:34 +00:00
width: 480
})
]
})
.css({margin: '16px'})
.bindEvent({
change: function(data) {
var values = $form.values();
$activityButton[selected].options({
2014-05-18 23:24:04 +00:00
disabled: !values.path
2014-05-17 08:17:34 +00:00
});
}
})
.appendTo($element);
$label[selected] = Ox.Label({
//textAlign: 'center',
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 08:17:34 +00:00
title: Ox._(
'Waiting for '
+ (selected == 'import' ? 'export' : 'import')
+ ' to finish...'
),
width: 344
})
.css({
position: 'absolute',
bottom: '16px',
left: '16px'
})
[isActive ? 'show' : 'hide']()
.appendTo($element);
$activityButton[selected] = Ox.Button({
disabled: true,
id: 'import',
2016-01-12 13:32:27 +00:00
style: 'squared',
2014-05-17 08:17:34 +00:00
title: Ox._(selected == 'import' ? 'Import' : 'Export'),
width: 128
})
.css({
position: 'absolute',
right: '16px',
bottom: '16px'
})
.bindEvent({
click: function() {
2014-05-18 23:24:04 +00:00
var data = $form.values(),
addList = data.list && !Ox.contains(
oml.getOwnListNames(),
data.list
);
2014-05-17 08:17:34 +00:00
$innerPanel.replaceElement(0,
renderActivity({
activity: 'import',
path: data.path,
progress: [0, 0]
})
);
$label['export'].show();
2014-10-31 11:46:32 +00:00
(addList ? oml.addList : Ox.noop)(false, false, data.list, function(result) {
if (result) {
data.list = result.data.id
}
2014-05-18 23:24:04 +00:00
oml.api.import({
list: data.list,
mode: data.mode,
path: data.path,
}, function() {
// ...
});
});
2014-05-17 08:17:34 +00:00
}
})
.appendTo($element);
return $element;
}
function setButton(data) {
2015-03-07 20:17:16 +00:00
$progressButton[data.activity] && $progressButton[data.activity].options({
2014-05-17 08:17:34 +00:00
title: !data.status ? Ox._(
data.activity == 'import' ? 'Cancel Import' : 'Cancel Export'
) : Ox._('Done')
});
}
function setProgress(data) {
var progress = data.status ? 1
: !data.progress[0] || !data.progress[1] ? -1
: data.progress[0] / data.progress[1];
2014-10-31 11:46:32 +00:00
$progress[data.activity] && $progress[data.activity].options({progress: progress})
2014-05-17 08:17:34 +00:00
}
function setStatus(data) {
// FIXME: LOCALIZATION
var total = Ox.formatCount(data.progress[1], 'book').replace(/$no /, 'No'),
status = data.status && data.status.code
? (
data.status.code == 200 ? (
data.progress[1]
? 'Done. ' + total + ' ' + (selected == 'import' ? 'imported' : 'exported.')
: Ox._('No books found.')
)
: data.status.code == 404 ? Ox._((
selected == 'import' ? 'Source' : 'Destination'
) + ' path not found.')
: Ox._((selected == 'import' ? 'Import' : 'Export') + 'failed.')
)
: !data.progress[0] && data.progress[1] ? Ox._('Scanning: {0} found.', [total])
: data.progress[0] ? Ox._(selected == 'import' ? 'Importing:' : 'Exporting')
2014-05-25 20:59:03 +00:00
+ ' ' + Ox._('{0} of {1}', [Ox.formatNumber(data.progress[0]), total])
2014-05-17 08:17:34 +00:00
: '';
2014-11-15 01:14:02 +00:00
$status[data.activity] && $status[data.activity].html(status);
2014-05-17 08:17:34 +00:00
}
that.select = function(selected_) {
selected = selected_;
$buttons.options({selected: selected});
$innerPanel.options({selected: selected});
return that;
};
oml.bindEvent({
activity: function(data) {
setProgress(data);
setStatus(data);
setButton(data);
}
});
return that;
2014-05-17 15:41:26 +00:00
};