refactor media view

This commit is contained in:
j 2017-07-20 12:36:43 +02:00
parent 73b6cfc6c2
commit 24dbcae172

View file

@ -6,6 +6,10 @@ pandora.ui.mediaView = function(options) {
var canRemove = pandora.site.capabilities.canRemoveItems[pandora.user.level] || options.editable, var canRemove = pandora.site.capabilities.canRemoveItems[pandora.user.level] || options.editable,
self = {}, self = {},
keys = ['title', 'director', 'year', 'id'],
listKeys = keys.filter(function(key) {
return Ox.isArray(Ox.getObjectById(pandora.site.itemKeys, key).type);
}),
that = Ox.Element({}, self) that = Ox.Element({}, self)
.defaults({ .defaults({
id: '' id: ''
@ -390,7 +394,7 @@ pandora.ui.mediaView = function(options) {
}) })
.css({margin: '8px'}); .css({margin: '8px'});
['title', 'director', 'year', 'id'].forEach(function(key) { keys.forEach(function(key) {
var itemKey = Ox.getObjectById(pandora.site.itemKeys, key); var itemKey = Ox.getObjectById(pandora.site.itemKeys, key);
self['$' + key + 'Input'] = Ox.Input({ self['$' + key + 'Input'] = Ox.Input({
label: Ox._(key == 'id' ? 'ID' label: Ox._(key == 'id' ? 'ID'
@ -412,10 +416,10 @@ pandora.ui.mediaView = function(options) {
} }
if (data.value.length) { if (data.value.length) {
conditions = {}; conditions = {};
['id', 'title', 'director', 'year'].map(function(key) { keys.map(function(key) {
var value = self['$' + key + 'Input'].value(); var value = self['$' + key + 'Input'].value();
if (value.length) { if (value.length) {
conditions[key] = key == 'director' ? value.split(', ') : value; conditions[key] = Ox.contains(listKeys, key) ? value.split(', ') : value;
} }
}); });
pandora.api.findId(conditions, function(result) { pandora.api.findId(conditions, function(result) {
@ -425,9 +429,9 @@ pandora.ui.mediaView = function(options) {
self.$idInput.value(''); self.$idInput.value('');
} }
} else if (result.data.items.length == 1) { } else if (result.data.items.length == 1) {
['title', 'director', 'year', 'id'].forEach(function(key) { keys.forEach(function(key) {
self['$' + key + 'Input'].value( self['$' + key + 'Input'].value(
key == 'director' Ox.contains(listKeys, key)
? result.data.items[0][key].join(', ') ? result.data.items[0][key].join(', ')
: result.data.items[0][key] : result.data.items[0][key]
); );
@ -449,13 +453,11 @@ pandora.ui.mediaView = function(options) {
}); });
self.$movieForm = Ox.Form({ self.$movieForm = Ox.Form({
items: [ items: keys.map(function(key) {
self.$titleInput, return self['$' + key + 'Input'];
self.$directorInput, }).concat([
self.$yearInput,
self.$idInput,
self.$switch self.$switch
], ]),
width: 240 width: 240
}) })
.css({margin: '8px'}); .css({margin: '8px'});
@ -467,7 +469,7 @@ pandora.ui.mediaView = function(options) {
.css({margin: '0 4px 4px 8px'}) .css({margin: '0 4px 4px 8px'})
.bindEvent({ .bindEvent({
click: function() { click: function() {
['title', 'director', 'year', 'id'].forEach(function(key) { keys.forEach(function(key) {
self['$' + key + 'Input'].value(''); self['$' + key + 'Input'].value('');
}); });
} }
@ -550,10 +552,9 @@ pandora.ui.mediaView = function(options) {
function moveFiles(data) { function moveFiles(data) {
var data = { var data = {
ids: self.selected, ids: self.selected,
item: self.$idInput.value()
}; };
['title', 'director', 'year'].forEach(function(key) { keys.forEach(function(key) {
data[key] = self['$' + key + 'Input'].value(); data[key == 'id' ? 'item' : key] = self['$' + key + 'Input'].value();
}); });
self.$moveButton.options( self.$moveButton.options(
{disabled: true, title: Ox._('Moving Files...')} {disabled: true, title: Ox._('Moving Files...')}
@ -583,10 +584,10 @@ pandora.ui.mediaView = function(options) {
path: self.$instancesList.value(data.ids[0], 'path') path: self.$instancesList.value(data.ids[0], 'path')
}, function(result) { }, function(result) {
self.$idInput.value(''); self.$idInput.value('');
['title', 'director', 'year'].forEach(function(key) { keys.forEach(function(key) {
if (result.data[key]) { if (result.data[key]) {
self['$' + key + 'Input'].value( self['$' + key + 'Input'].value(
key == 'director' Ox.contains(listKeys, key)
? result.data[key].join(', ') ? result.data[key].join(', ')
: result.data[key] : result.data[key]
); );