openmedialibrary/static/js/identifyDialog.js

326 lines
10 KiB
JavaScript
Raw Permalink 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-24 13:32:54 +00:00
arrayKeys = ['author', 'publisher', 'place', 'language'],
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: [
2016-01-12 06:24:48 +00:00
{id: 'title', title: Ox._('Title')},
2016-01-11 14:26:40 +00:00
{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'],
2016-01-12 06:24:48 +00:00
width: 96
2014-05-21 00:02:39 +00:00
})
.bindEvent({
change: function(data) {
2016-01-12 06:24:48 +00:00
$innerElement.replaceElement(1, $findInput[data.value]);
(
data.value == 'title' ? $titleInput : $findInput[data.value]
).focusInput(true);
2014-05-21 00:02:39 +00:00
}
}),
2014-05-18 23:24:04 +00:00
2016-01-12 06:24:48 +00:00
$titleInput = Ox.Input({
2016-01-11 14:26:40 +00:00
style: 'squared',
2016-01-12 06:24:48 +00:00
value: Ox.decodeHTMLEntities(data.title || ''),
width: 224
})
.bindEvent({
change: findMetadata,
2016-01-12 06:24:48 +00:00
submit: findMetadata
}),
$authorInput = Ox.Input({
style: 'squared',
value: Ox.decodeHTMLEntities((data.author || []).join('; ')),
width: 224
2014-05-21 00:02:39 +00:00
})
.bindEvent({
change: findMetadata,
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 06:24:48 +00:00
$findInput = {
title: Ox.InputGroup({
inputs: [$titleInput, $authorInput],
separators: [{
title: Ox._('Author'),
width: 96
}],
style: 'squared',
width: 544
}),
isbn: Ox.Input({
style: 'squared',
value: Ox.formatISBN(data.isbn || '', 13, true),
2016-01-12 06:24:48 +00:00
width: 544
})
.bindEvent({
change: findMetadata,
2016-01-12 06:24:48 +00:00
submit: findMetadata
}),
id: Ox.Input({
style: 'squared',
value: data.id,
width: 544
})
.bindEvent({
change: findMetadata,
2016-01-12 06:24:48 +00:00
submit: findMetadata
})
},
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'),
2016-01-12 06:24:48 +00:00
width: 96
2014-05-21 00:02:39 +00:00
})
.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-12 06:24:48 +00:00
$innerElement = Ox.FormElementGroup({
elements: [
$findSelect,
$findInput[$findSelect.value()]
],
float: 'left'
}),
$outerElement = Ox.FormElementGroup({
2014-05-21 00:02:39 +00:00
elements: [
2016-01-12 06:24:48 +00:00
$innerElement,
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(
2016-01-24 13:32:54 +00:00
Ox.map(
$list.value($list.options('selected')[0]),
function(value, key) {
return value || (
$updateSelect.value() == 'replace'
? (Ox.contains(arrayKeys, key) ? [] : '')
: data[key]
);
}
),
2016-01-12 04:03:27 +00:00
function(value, key) {
return Ox.contains($list.options('keys'), key)
2016-01-24 13:32:54 +00:00
&& !Ox.isEqual(value, data[key]);
2016-01-12 04:03:27 +00:00
}
)), function(result) {
Ox.Request.clearCache('find');
oml.$ui.info.updateElement();
2016-01-12 08:52:20 +00:00
oml.reloadLists();
2016-01-12 04:03:27 +00:00
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);
});
2016-01-12 06:24:48 +00:00
$findInput[$findSelect.value()].options({disabled: true});
2016-01-12 04:03:27 +00:00
$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);
});
2016-01-12 06:24:48 +00:00
$findInput[$findSelect.value()].options({disabled: false});
2016-01-12 04:03:27 +00:00
$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() {
2016-01-12 06:24:48 +00:00
var key = $findSelect.value();
2014-05-21 00:02:39 +00:00
disableButtons();
2016-01-11 14:26:40 +00:00
$outerPanel.replaceElement(1, Ox.LoadingScreen().start());
2016-01-12 06:24:48 +00:00
oml.api.findMetadata(
key == 'title' ? {
title: $titleInput.value(),
author: $authorInput.value()
} : key == 'isbn' ? {
2016-01-16 15:08:01 +00:00
isbn: Ox.formatISBN($findInput.isbn.value(), 13)
2016-01-12 06:24:48 +00:00
} : {
id: $findInput.id.value()
},
function(result) {
var items = result.data.items.map(function(item, index) {
return Ox.extend({index: index.toString()}, item);
});
enableButtons();
$updateButton.options({disabled: !items.length});
$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-12 06:24:48 +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-02-14 10:35:35 +00:00
return '<b>' + Ox.encodeHTMLEntities(data.title) + '</b> '
+ Ox.encodeHTMLEntities(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
};