diff --git a/app/components/viewer/index.js b/app/components/viewer/index.js index f7a47f92983faf5e05f5a441848abe26d18aa0cb..c50f9f80917c0dd4d916fe65dd525f6592185881 100644 --- a/app/components/viewer/index.js +++ b/app/components/viewer/index.js @@ -146,7 +146,7 @@ export default class ViewerComponent extends Component { @action onThumbnailsPositionChanged({original_pos, drop_pos, page_ids}) { - let all_pages; + let all_pages = this.pages; console.log(`onThumbnailsPositionChanged`); console.log(`original_pos=${original_pos}, drop_pos=${drop_pos}, page_ids=${page_ids}`); diff --git a/app/utils/array.js b/app/utils/array.js index f80ba54bfbdb43e5b31b1361faf8ca3380e0fb2e..e3e968f61685ab91723814ad9fcbb3e18ba4cf5c 100644 --- a/app/utils/array.js +++ b/app/utils/array.js @@ -38,11 +38,14 @@ function merge_items(item_id, items) { } -function reposition_items({items, selected_ids, drop_pos}) { +function extract_selected_ids({items, selected_ids}) { + /** + * Extracts from ``items`` all objects whose ID attribute + * is in ``selected_id`` array. + */ let selected_items = [], - remaining_items = Array.from(items), - result = [], - i, j; + remaining_items = Array.from(items); + selected_ids.forEach(item_id => { let idx, extracted_item; @@ -54,6 +57,28 @@ function reposition_items({items, selected_ids, drop_pos}) { selected_items.push(...extracted_item); }); + return {selected_items, remaining_items}; +} + + +function reposition_items({items, selected_ids, drop_pos}) { + /** + * Returns reordered copy of ``items`` with selected items positioned + * starting with ``drop_pos``. + * + * ``items`` is an array of objects which have an ID attribute. + * ``selected_ids`` is an array of IDs (i.e. an array of strings) of the + * items which will be moved to different position. + * ``drop_pos`` is an integer. It is the target position where selected + * items will move. + */ + let result = [], + i, j; + + let { selected_items, remaining_items } = extract_selected_ids({ + items, selected_ids + }); + for (i=0, j=0; i < items.length;) { if (i == drop_pos) { result.push(...selected_items); @@ -73,5 +98,6 @@ function reposition_items({items, selected_ids, drop_pos}) { export { get_id, merge_items, + extract_selected_ids, reposition_items, } diff --git a/tests/unit/utils/array-test.js b/tests/unit/utils/array-test.js index 538e3ddfa549553c49d3619cba0d043146ce7687..049e938d5c6dffad8d75d7069e1f0f436dae58ba 100644 --- a/tests/unit/utils/array-test.js +++ b/tests/unit/utils/array-test.js @@ -1,5 +1,9 @@ import { module, test } from 'qunit'; -import { get_id, reposition_items } from 'papermerge/utils/array'; +import { + get_id, + extract_selected_ids, + reposition_items +} from 'papermerge/utils/array'; class Page { @@ -38,6 +42,41 @@ module('Unit | Utils | Array', function () { assert.strictEqual(get_id(item), 13); }); + test('extract_selected_ids 1', function(assert) { + let items, + selected_ids = ['2'], + expected_selected_items, + expected_remaining_items; + + items = [ + new Page('1'), new Page('2'), new Page('3'), new Page('4') + ]; + + expected_selected_items = [ + new Page('2') + ] + + expected_remaining_items = [ + new Page('1'), new Page('3'), new Page('4') + ] + + let {selected_items, remaining_items} = extract_selected_ids({ + items, selected_ids + }); + + assert.expect( + expected_selected_items.length + expected_remaining_items.length + ); + + expected_selected_items.forEach((page, index) => { + assert.strictEqual(page.id, selected_items[index].id); + }); + + expected_remaining_items.forEach((page, index) => { + assert.strictEqual(page.id, remaining_items[index].id); + }); + }); + test('reposition_items 1', function(assert) { let items, selected_ids = ['2'], @@ -64,7 +103,6 @@ module('Unit | Utils | Array', function () { }); }); - test('reposition_items 2', function(assert) { let items, selected_ids = ['2', '3'], @@ -91,7 +129,6 @@ module('Unit | Utils | Array', function () { }); }); - test('reposition_items 3', function(assert) { let items, selected_ids = ['1'],