openmedialibrary/static/js/identifyDialog.js

240 lines
7.6 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-11 14:26:40 +00:00
$select = Ox.Select({
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-11 14:26:40 +00:00
$input.options({
value: getValue(data.value)
}).focusInput(true);
2014-05-21 00:02:39 +00:00
}
}),
2014-05-18 23:24:04 +00:00
2016-01-11 14:26:40 +00:00
$input = Ox.Input({
style: 'squared',
value: getValue($select.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-11 14:26:40 +00:00
$button = 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-11 14:26:40 +00:00
$select,
$input
2014-05-21 00:02:39 +00:00
],
float: 'left'
}),
2016-01-11 14:26:40 +00:00
$button
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
$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'
}),
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() {
// FIXME: Wrong if user messes with lookup elements before clicking update button
var primaryId;
if (selected == 'lookup') {
primaryId = [
$lookupSelect.value(),
$lookupInput.value()
];
} else {
2016-01-11 14:26:40 +00:00
primaryId = $list.value(
$list.options('selected')[0],
2014-05-21 00:02:39 +00:00
'primaryid'
2014-05-14 23:28:49 +00:00
);
2014-05-21 00:02:39 +00:00
}
that.options({content: Ox.LoadingScreen().start()});
that.disableButtons();
oml.api.edit({
id: data.id,
primaryid: primaryId
}, function(result) {
(
$metadataSelect.value() == 'original'
? oml.api.resetMetadata : Ox.noop
)({id: ui.item}, function(result) {
2014-05-16 08:06:11 +00:00
Ox.Request.clearCache('find');
2016-01-07 06:07:35 +00:00
oml.$ui.info.updateElement();
2014-05-16 08:06:11 +00:00
oml.$ui.browser.reloadList(true);
oml.$ui.list.reloadList(true);
2014-05-16 08:06:11 +00:00
Ox.Request.clearCache(data.id);
2014-05-19 20:58:00 +00:00
oml.$ui.infoView.updateElement(data.id);
2015-04-21 17:57:27 +00:00
that.close();
2014-05-14 23:28:49 +00:00
});
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
});
2014-05-18 23:24:04 +00:00
function disableButtons() {
2016-01-11 14:26:40 +00:00
$select.options('items').forEach(function(item) {
$select.disableItem(item.id);
2014-05-14 18:46:31 +00:00
});
2016-01-11 14:26:40 +00:00
$input.options({disabled: true});
$button.options({disabled: true});
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-11 14:26:40 +00:00
$select.options('items').forEach(function(item) {
2016-01-11 14:40:29 +00:00
$select.enableItem(item.id);
2014-05-21 00:02:39 +00:00
});
2016-01-11 14:26:40 +00:00
$input.options({disabled: false});
$button.options({disabled: false});
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-11 14:26:40 +00:00
key: $select.value(),
value: $input.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
}
function renderResults(items) {
2016-01-11 14:26:40 +00:00
var $innerPanel;
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-11 14:26:40 +00:00
var index = data.ids[0];
$innerPanel.replaceElement(1, oml.ui.infoView($list.value(index)));
2014-05-19 20:58:00 +00:00
}
2014-05-21 00:02:39 +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
};