From f2e7cdbea8cc37428e2429a880afec5b57ac2423 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 29 Jul 2019 11:57:20 +0200 Subject: [PATCH] reuse empty items --- static/js/addFilesDialog.js | 47 +++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/static/js/addFilesDialog.js b/static/js/addFilesDialog.js index 6a31b26f..2a4e023c 100644 --- a/static/js/addFilesDialog.js +++ b/static/js/addFilesDialog.js @@ -149,6 +149,49 @@ pandora.ui.addFilesDialog = function(options) { $($select.find('.OxButton')[0]).css({margin: '-1px'}); $button.parent().parent().append($select); + function getNewOrEmptyItem(data, callback) { + pandora.api.find({ + query: { + conditions: [ + {key: 'title', value: data.title, operator: '=='} + ] + }, + keys: ['id'] + }, function(result) { + if (!result.data.items.length) { + pandora.api.add(data, callback) + } else { + var isNew = true + Ox.serialForEach(result.data.items, function(item, index, items, next) { + isNew && pandora.api.findMedia({ + query: { + conditions: [ + {key: 'id', value: item.id, operator: '=='} + ] + }, + keys: ['id'] + }, function(result) { + if (!result.data.items.length) { + isNew = false + callback({ + data: { + title: data.title, + id: item.id + } + }) + } + next() + }) + }, function() { + if (isNew) { + pandora.api.add(data, callback) + } + }) + + } + }) + } + function importVideos(callback) { var id, title; ($select.value() == 'add' ? pandora.api.get : Ox.noop)({ @@ -166,7 +209,7 @@ pandora.ui.addFilesDialog = function(options) { } else { title = items[$select.value() == 'one' ? 0 : index].title; } - (isNewItem ? pandora.api.add : Ox.noop)({ + (isNewItem ? getNewOrEmptyItem : Ox.noop)({ title: title }, function(result) { if (isNewItem) { @@ -206,7 +249,7 @@ pandora.ui.addFilesDialog = function(options) { } else { title = items[$select.value() == 'one' ? 0 : index].title; } - (isNewItem ? pandora.api.add : Ox.noop)({ + (isNewItem ? getNewOrEmptyItem : Ox.noop)({ title: title }, function(result) { if (isNewItem) {