From 5882e97608c0600d1136a41749c6be6f48410608 Mon Sep 17 00:00:00 2001 From: rlx Date: Sat, 16 Jan 2016 12:49:08 +0530 Subject: [PATCH] correctly handle download/edit/delete for selections with mixed mediastate --- static/js/deleteItemsDialog.js | 4 ++- static/js/editDialog.js | 4 ++- static/js/list.js | 11 ++++-- static/js/mainMenu.js | 61 ++++++++++++++++++++-------------- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/static/js/deleteItemsDialog.js b/static/js/deleteItemsDialog.js index 6fbc0eb..01f8a93 100644 --- a/static/js/deleteItemsDialog.js +++ b/static/js/deleteItemsDialog.js @@ -4,7 +4,9 @@ oml.ui.deleteItemsDialog = function() { var ui = oml.user.ui, - items = ui.listSelection, + items = ui.listSelection.filter(function(id) { + return oml.$ui.list.value(id, 'mediastate') == 'available'; + }), itemsName = Ox._(items.length == 1 ? 'Item' : 'Items'), theseItemsName = items.length == 1 ? Ox._('this item') diff --git a/static/js/editDialog.js b/static/js/editDialog.js index f08a734..1b3aebf 100644 --- a/static/js/editDialog.js +++ b/static/js/editDialog.js @@ -7,7 +7,9 @@ oml.ui.editDialog = function() { 'author', 'place', 'publisher', 'language' ], hasChanged = false, - ids = ui.listSelection, + ids = ui.listSelection.filter(function(id) { + return oml.$ui.list.value(id, 'mediastate') == 'available'; + }), keys = [ 'title', 'author', 'publisher', 'place', 'date', diff --git a/static/js/list.js b/static/js/list.js index 864de93..9de1ece 100644 --- a/static/js/list.js +++ b/static/js/list.js @@ -57,8 +57,9 @@ oml.ui.list = function() { oml.$ui.statusbar.set('total', data); }, key_control_delete: function() { - var listData = oml.getListData(); - if (listData.own) { + if (that.options('selected').filter(function(id) { + return that.value(id, 'mediastate') == 'available'; + }).length) { oml.ui.deleteItemsDialog().open(); } }, @@ -71,6 +72,12 @@ oml.ui.list = function() { }); } }, + load: function() { + if (oml._updateEditMenu) { + oml.$ui.mainMenu.updateElement('editMenu'); + oml._udpateEditMenu = false; + } + }, open: function(data) { oml.UI.set({ item: data.ids[0], diff --git a/static/js/mainMenu.js b/static/js/mainMenu.js index 7c7014b..26a251b 100644 --- a/static/js/mainMenu.js +++ b/static/js/mainMenu.js @@ -399,10 +399,7 @@ oml.ui.mainMenu = function() { }); } } else if (id == 'deletefromlibrary') { - var listData = oml.getListData(); - if (listData.own) { - oml.ui.deleteItemsDialog().open(); - } + oml.ui.deleteItemsDialog().open(); } else if (id == 'editlist') { oml.ui.listDialog.open(); } else if (id == 'deletelist') { @@ -417,6 +414,16 @@ oml.ui.mainMenu = function() { oml.UI.set({listSelection: []}); } else if (id == 'invertselection') { oml.$ui.list.invertSelection(); + } else if (id == 'download') { + oml.api.addListItems({ + items: ui.listSelection.filter(function(id) { + return oml.$ui.list.value(id, 'mediastate') == 'unavailable'; + }), + list: ':' + }, function(result) { + Ox.Request.clearCache(); + // FIXME: reload? + }); } else if (Ox.contains(['cut', 'cutadd'], id)) { var action = data.id == 'cut' ? 'copy' : 'add'; fromMenu = true; @@ -478,17 +485,6 @@ oml.ui.mainMenu = function() { )).open(); } else if (id == 'advancedfind') { oml.$ui.findDialog = oml.ui.findDialog().open(); - } else if (id == 'download') { - var ids = (ui.item ? [ui.item] : ui.listSelection).filter(function(id) { - return oml.$ui.list && oml.$ui.list.value(id, 'mediastate') == 'unavailable' - }); - ids.length && oml.api.addListItems({ - items: ids, - list: ':' - }, function(result) { - Ox.Request.clearCache(); - //FIXME: reload? - }); } else { Ox.print('MAIN MENU DOES NOT YET HANDLE', id); } @@ -628,13 +624,27 @@ oml.ui.mainMenu = function() { function getEditMenu() { var listData = oml.getListData(), + isLocal = listData.user === '' && listData.type != 'smart', selectionItems = ui.listSelection.length, + availableItems = isLocal + ? selectionItems + : oml.$ui.list + ? ui.listSelection.filter(function(id) { + return oml.$ui.list.value(id, 'mediastate') == 'available'; + }).length + : 0, + unavailableItems = selectionItems - availableItems, selectionItemName = ( selectionItems > 1 ? Ox.formatNumber(selectionItems) + ' ' : '' ) + Ox._(selectionItems == 1 ? 'Book' : 'Books'), + downloadItemName = ( + selectionItems > 1 && unavailableItems ? Ox.formatNumber(unavailableItems) + ' ' + ( + availableItems ? ' of ' + Ox.formatNumber(selectionItems) + ' ' : '' + ) : '') + Ox._(selectionItems == 1 ? 'Book' : 'Books'), editItemName = ( - selectionItems > 0 ? Ox.formatNumber(selectionItems) + ' ' : '' - ) + Ox._(selectionItems == 1 ? 'Book' : 'Books'), + selectionItems > 1 && availableItems ? Ox.formatNumber(availableItems) + ' ' + ( + unavailableItems ? ' of ' + Ox.formatNumber(selectionItems) + ' ' : '' + ) : '') + Ox._(selectionItems == 1 ? 'Book' : 'Books'), clipboardItems = oml.clipboard.items(), clipboardType = oml.clipboard.type(), clipboardItemName = !clipboardItems ? '' @@ -642,18 +652,19 @@ oml.ui.mainMenu = function() { clipboardItems > 1 ? Ox.formatNumber(clipboardItems) + ' ' : '' ) + Ox._(clipboardItems == 1 ? 'Book' : 'Books'), canSelect = !ui.item, - canDownload = ui.listSelection.filter(function(id) { - return oml.$ui.list && oml.$ui.list.value(id, 'mediastate') == 'unavailable' - }).length, + canDownload = !!unavailableItems, canCopy = canSelect && selectionItems, canCut = canCopy && listData.editable, canPaste = listData.editable && clipboardItems, canAdd = canCopy && clipboardItems && clipboardType == 'book', - canDelete = listData.user == '' && selectionItems, - canEdit = listData.user == '' && selectionItems, + canDelete = !!availableItems, + canEdit = !!availableItems, historyItems = oml.history.items(), undoText = oml.history.undoText(), redoText = oml.history.redoText(); + if (!isLocal && !oml.$ui.list) { + oml._updateEditMenu = true; + } return { id: 'editMenu', title: Ox._('Edit'), @@ -688,7 +699,7 @@ oml.ui.mainMenu = function() { {}, { id: 'download', - title: Ox._('Download {0}', [canDownload]), + title: Ox._('Download {0}', [downloadItemName]), disabled: !canDownload, keyboard: 'control d' }, @@ -736,7 +747,7 @@ oml.ui.mainMenu = function() { }, { id: 'deletefromlibrary', - title: Ox._('Delete {0} from Library...', [selectionItemName]), + title: Ox._('Delete {0} from Library...', [editItemName]), disabled: !canDelete, keyboard: 'control delete' }, @@ -919,7 +930,7 @@ oml.ui.mainMenu = function() { ( menu ? Ox.makeArray(menu) : ['listMenu', 'editMenu', 'findMenu'] ).forEach(function(menu) { - that.updateMenu( + that.replaceMenu( menu, menu == 'listMenu' ? getListMenu() : menu == 'editMenu' ? getEditMenu()