From 2e7d1936b41bc2594ba895e56f642164a4201621 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Tue, 28 Jun 2016 16:40:30 +0000 Subject: [PATCH] documentsPanel: bulk-edit entities Fixes #2934 --- static/js/documentsPanel.js | 73 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/static/js/documentsPanel.js b/static/js/documentsPanel.js index 3380267..3e1a23a 100644 --- a/static/js/documentsPanel.js +++ b/static/js/documentsPanel.js @@ -627,11 +627,45 @@ pandora.ui.documentsPanel = function(options) { ); } + function pluck(xs, key) { + return xs.map(function(i) { return i[key]; }); + } + + function mergeDocuments(items) { + if (items.length === 1) { + return items[0]; + } else { + var ids = pluck(items, 'id'); + + return { + name: pluck(items, 'name').join(', '), + ids: ids, + id: ids.join(', '), + extension: Ox.unique(pluck(items, 'extension')).join(', '), + dimensions: Ox.sum(items.map(function(item) { + return Ox.isArray(item.dimensions) ? 1 : item.dimensions; + })), + size: Ox.sum(pluck(items, 'size')), + matches: Ox.sum(pluck(items, 'matches')), + user: Ox.unique(pluck(items, 'user')).join(', '), + description: pluck(items, 'description').join('\n\n'), + entities: pluck(items, 'entities').reduce(function(left, right) { + var ids = {}; + Ox.forEach(right, function(r) { ids[r.id] = true; }); + return left.filter(function(l) { + return Ox.hasOwn(ids, l.id); + }); + }) + }; + } + } + function renderForm() { - var item = $list.value($list.options('selected')[0]), - editable = item.user == pandora.user.username + var selected = $list.options('selected').map(function(i) { return $list.value(i); }), + item = mergeDocuments(selected), + editable = selected.length === 1 && (item.user == pandora.user.username || pandora.site.capabilities.canEditDocuments[pandora.user.level] - || options.editable, + || options.editable), labelWidth = 80, width = ui.documentSize - 16 - Ox.UI.SCROLLBAR_SIZE; var items = [ @@ -705,6 +739,19 @@ pandora.ui.documentsPanel = function(options) { ]; if (pandora.site.entities && pandora.site.entities.length) { + var addRemoveArgs = function(entity) { + if (item.ids) { + return { + ids: item.ids, + entity: entity + }; + } else { + return { + id: item.id, + entity: entity + }; + } + }; var change = function(data) { var remove = item.entities.filter(function(entity) { return data.value.filter(function(value) { @@ -723,10 +770,7 @@ pandora.ui.documentsPanel = function(options) { }); remove.length && remove.forEach(function(entity) { var id = entity.id; - pandora.api.removeDocument({ - id: item.id, - entity: id - }, function(result) { + pandora.api.removeDocument(addRemoveArgs(id), function(result) { if (result.status.code === 200) { item.entities = item.entities.filter(function(e) { return e.id !== id; @@ -734,6 +778,9 @@ pandora.ui.documentsPanel = function(options) { } else { // FIXME: indicate error } + + Ox.Request.clearCache('findDocuments'); + $list.reloadList(); }); }); add.length && add.forEach(function(entity) { @@ -750,10 +797,7 @@ pandora.ui.documentsPanel = function(options) { }, function(result) { if (result.data.items && result.data.items.length) { var id = result.data.items[0].id; - pandora.api.addDocument({ - id: item.id, - entity: id - }, function(result) { + pandora.api.addDocument(addRemoveArgs(id), function(result) { if (result.status.code === 200) { item.entities.push({ id: id, @@ -763,6 +807,9 @@ pandora.ui.documentsPanel = function(options) { } else { // FIXME: indicate error } + + Ox.Request.clearCache('findDocuments'); + $list.reloadList(); }); } }); @@ -847,6 +894,10 @@ pandora.ui.documentsPanel = function(options) { .css({margin: '12px 8px 8px 8px'}) .bindEvent({ change: function(event) { + if (event.id === undefined) { + return; + } + var data = Ox.extend({id: item.id}, event.id, event.data.value); if (isItemView) { data.item = ui.item; -- 1.9.1