openmedialibrary/static/js/identifyDialog.js

266 lines
8.4 KiB
JavaScript
Raw Normal View History

2014-05-14 09:57:11 +00:00
'use strict';
oml.ui.identifyDialog = function(data) {
var ui = oml.user.ui,
2016-01-11 14:26:40 +00:00
$form = Ox.Element(),
2014-05-14 18:46:31 +00:00
2016-01-12 04:03:27 +00:00
$findSelect = Ox.Select({
2016-01-11 14:26:40 +00:00
items: [
{id: 'title', title: Ox._('Title, Author etc.')},
{id: 'isbn', title: Ox._('ISBN')},
{id: 'id', title: Ox._('ID')}
],
2014-05-21 00:02:39 +00:00
overlap: 'right',
max: 1,
min: 1,
2016-01-11 14:26:40 +00:00
style: 'squared',
2016-01-11 14:35:53 +00:00
value: [data.isbn ? 'isbn' : 'title'],
2014-05-21 00:02:39 +00:00
width: 128
})
.bindEvent({
change: function(data) {
2016-01-12 04:03:27 +00:00
$findInput.options({
2016-01-11 14:26:40 +00:00
value: getValue(data.value)
}).focusInput(true);
2014-05-21 00:02:39 +00:00
}
}),
2014-05-18 23:24:04 +00:00
2016-01-12 04:03:27 +00:00
$findInput = Ox.Input({
2016-01-11 14:26:40 +00:00
style: 'squared',
2016-01-12 04:03:27 +00:00
value: getValue($findSelect.value()),
2014-05-21 00:02:39 +00:00
width: 480
})
.bindEvent({
change: function(data) {
// ...
},
2016-01-11 14:26:40 +00:00
submit: findMetadata
2014-05-21 00:02:39 +00:00
}),
2014-05-14 23:28:49 +00:00
2016-01-12 04:03:27 +00:00
$findButton = Ox.Button({
2014-05-21 00:02:39 +00:00
overlap: 'left',
2016-01-11 14:26:40 +00:00
style: 'squared',
title: Ox._('Find'),
2014-05-21 00:02:39 +00:00
width: 128
})
.bindEvent({
2016-01-11 14:26:40 +00:00
click: findMetadata
2014-05-21 00:02:39 +00:00
}),
2014-05-14 23:28:49 +00:00
2016-01-11 14:26:40 +00:00
$formElement = Ox.FormElementGroup({
2014-05-21 00:02:39 +00:00
elements: [
Ox.FormElementGroup({
elements: [
2016-01-12 04:03:27 +00:00
$findSelect,
$findInput
2014-05-21 00:02:39 +00:00
],
float: 'left'
}),
2016-01-12 04:03:27 +00:00
$findButton
2014-05-21 00:02:39 +00:00
],
float: 'right'
})
2016-01-11 14:26:40 +00:00
.css({margin: '16px'})
.appendTo($form),
2014-05-14 09:57:11 +00:00
2016-01-11 14:26:40 +00:00
$list,
2014-05-14 09:57:11 +00:00
2016-01-12 04:03:27 +00:00
$innerPanel,
2014-05-14 09:57:11 +00:00
$outerPanel = Ox.SplitPanel({
elements: [
2016-01-11 14:26:40 +00:00
{element: $form, size: 48},
{element: renderResults([])}
2014-05-14 09:57:11 +00:00
],
orientation: 'vertical'
}),
2016-01-12 04:03:27 +00:00
$updateSelect = Ox.Select({
items: [
{id: 'replace', title: Ox._('Replace Metadata')},
{id: 'merge', title: Ox._('Merge Metadata')}
],
max: 1,
min: 1,
style: 'squared',
value: ui.updateMetadata,
width: 192
})
.css({margin: '4px'})
.bindEvent({
change: function(data) {
oml.UI.set({updateMetadata: data.value});
$list && $innerPanel.replaceElement(1, renderResult());
}
}),
2014-05-21 00:02:39 +00:00
$dontUpdateButton = Ox.Button({
id: 'dontupdate',
2016-01-11 14:26:40 +00:00
style: 'squared',
2014-05-21 00:02:39 +00:00
title: Ox._('No, Don\'t Update')
})
.bindEvent({
click: function() {
that.close();
}
}),
$updateButton = Ox.Button({
disabled: true,
id: 'update',
2016-01-11 14:26:40 +00:00
style: 'squared',
2014-05-21 00:02:39 +00:00
title: Ox._('Yes, Update')
})
.bindEvent({
click: function() {
that.options({content: Ox.LoadingScreen().start()});
that.disableButtons();
2016-01-12 04:03:27 +00:00
oml.api.edit(Ox.extend({
id: data.id
}, Ox.filter(
$list.value($list.options('selected')[0]),
function(value, key) {
return Ox.contains($list.options('keys'), key)
&& value != data[key]
}
)), function(result) {
Ox.Request.clearCache('find');
oml.$ui.info.updateElement();
oml.$ui.browser.reloadList(true);
oml.$ui.list.reloadList(true);
Ox.Request.clearCache(data.id);
oml.$ui.infoView.updateElement(data.id);
that.close();
2014-05-21 00:02:39 +00:00
});
}
}),
that = Ox.Dialog({
buttons: [
$dontUpdateButton,
$updateButton
2014-05-14 09:57:11 +00:00
],
closeButton: true,
content: $outerPanel,
fixedSize: true,
height: 384,
2014-05-18 23:24:04 +00:00
removeOnClose: true,
2014-05-14 09:57:11 +00:00
title: Ox._('Identify Book'),
width: 768
});
2016-01-12 04:03:27 +00:00
$($updateSelect.find('.OxButton')[0]).css({margin: 0});
$updateSelect.appendTo($(that.find('.OxBar')[1]));
2014-05-18 23:24:04 +00:00
function disableButtons() {
2016-01-12 04:03:27 +00:00
$findSelect.options('items').forEach(function(item) {
$findSelect.disableItem(item.id);
});
$findInput.options({disabled: true});
$findButton.options({disabled: true});
$updateSelect.options('items').forEach(function(item) {
$updateSelect.disableItem(item.id);
2014-05-14 18:46:31 +00:00
});
2014-05-21 00:02:39 +00:00
$updateButton.options({disabled: true});
2014-05-14 18:46:31 +00:00
}
2014-05-21 00:02:39 +00:00
function enableButtons() {
2016-01-12 04:03:27 +00:00
$findSelect.options('items').forEach(function(item) {
$findSelect.enableItem(item.id);
});
$findInput.options({disabled: false});
$findButton.options({disabled: false});
$updateSelect.options('items').forEach(function(item) {
$updateSelect.enableItem(item.id);
2014-05-21 00:02:39 +00:00
});
$updateButton.options({disabled: false});
2014-05-14 18:46:31 +00:00
}
2014-05-21 00:02:39 +00:00
function findMetadata() {
disableButtons();
2016-01-11 14:26:40 +00:00
$outerPanel.replaceElement(1, Ox.LoadingScreen().start());
2014-05-21 00:02:39 +00:00
oml.api.findMetadata({
2016-01-12 04:03:27 +00:00
key: $findSelect.value(),
value: $findInput.value()
2014-05-21 00:02:39 +00:00
}, function(result) {
var items = result.data.items.map(function(item, index) {
return Ox.extend({index: index.toString()}, item);
2014-05-14 23:28:49 +00:00
});
2014-05-21 00:02:39 +00:00
enableButtons();
$updateButton.options({disabled: !items.length});
2016-01-11 14:26:40 +00:00
$outerPanel.replaceElement(1, renderResults(items));
2014-05-14 18:46:31 +00:00
});
}
2016-01-11 14:26:40 +00:00
function getValue(key) {
return key == 'title' ? Ox.decodeHTMLEntities(
[data.title || ''].concat(data.author || []).join(' ')
)
2016-01-11 14:35:53 +00:00
: key == 'isbn' ? (data.isbn || '')
2016-01-11 14:26:40 +00:00
: data.id
2014-05-14 18:46:31 +00:00
}
2016-01-12 04:03:27 +00:00
function renderResult() {
var index = $list.options('selected')[0];
return oml.ui.infoView(
$updateSelect.value() == 'replace'
? $list.value(index)
: Ox.map($list.value(index), function(value, key) {
return value || data[key];
})
);
}
2014-05-14 18:46:31 +00:00
function renderResults(items) {
2014-05-21 00:02:39 +00:00
if (items.length) {
2016-01-11 14:26:40 +00:00
$list = Ox.TableList({
2014-05-21 00:02:39 +00:00
columns: [{
2014-05-16 08:06:11 +00:00
format: function(value, data) {
2016-01-11 14:26:40 +00:00
return '<b>' + data.title + '</b> ' + data.author
2014-05-16 08:06:11 +00:00
},
2014-05-21 00:02:39 +00:00
id: 'index',
2014-05-16 08:06:11 +00:00
visible: true,
2014-05-19 20:58:00 +00:00
width: 192 - Ox.UI.SCROLLBAR_SIZE
2014-05-21 00:02:39 +00:00
}],
items: items,
2016-01-11 14:26:40 +00:00
keys: [
2016-01-11 15:10:39 +00:00
'cover', 'title', 'author',
2016-01-11 14:26:40 +00:00
'publisher', 'place', 'date',
'series', 'edition', 'language', 'pages',
'categories', 'isbn', 'description', 'tableofcontents'
],
2014-05-21 00:02:39 +00:00
min: 1,
max: 1,
scrollbarVisible: true,
selected: ['0'],
sort: [{key: 'index', operator: '+'}],
unique: 'index'
})
.bindEvent({
select: function(data) {
2016-01-12 04:03:27 +00:00
$innerPanel.replaceElement(1, renderResult());
2014-05-19 20:58:00 +00:00
}
2016-01-12 04:03:27 +00:00
});
2016-01-11 14:26:40 +00:00
$innerPanel = Ox.SplitPanel({
2014-05-14 18:46:31 +00:00
elements: [
2016-01-11 14:26:40 +00:00
{element: $list || Ox.Element(), size: 192},
2014-05-16 08:06:11 +00:00
{element: Ox.Element()}
2014-05-14 18:46:31 +00:00
],
2014-05-14 23:28:49 +00:00
orientation: 'horizontal'
2014-05-14 18:46:31 +00:00
});
2014-05-21 00:02:39 +00:00
setTimeout(function() {
2016-01-11 14:26:40 +00:00
$list.triggerEvent('select', {ids: ['0']});
2014-05-21 00:02:39 +00:00
});
2014-05-18 23:24:04 +00:00
} else {
2016-01-11 14:26:40 +00:00
$list = null;
$innerPanel = Ox.Element();
2014-05-18 23:24:04 +00:00
}
2016-01-11 14:26:40 +00:00
return $innerPanel;
2014-05-14 18:46:31 +00:00
}
2014-05-14 09:57:11 +00:00
return that;
2014-05-18 03:01:24 +00:00
};