|
| 1 | +/*! |
| 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { DialogBuilder, FilePickerBuilder } from '@nextcloud/dialogs' |
| 7 | +import { n, t } from '@nextcloud/l10n' |
| 8 | +import { placePhotos } from '../network.js' |
| 9 | + |
| 10 | +interface LotLong { |
| 11 | + lat: number |
| 12 | + lng: number |
| 13 | +} |
| 14 | + |
| 15 | +const dialogBuilder = new DialogBuilder(t('maps', 'What do you want to place')) |
| 16 | + |
| 17 | +/** |
| 18 | + * Place photos or a photo folder on a given map and location. |
| 19 | + * |
| 20 | + * @param latLong - The geo location where to place the photos |
| 21 | + * @param myMapId - The map to place the photos |
| 22 | + */ |
| 23 | +export async function placeFileOrFolder(latLong: LotLong, myMapId: number) { |
| 24 | + const { promise, resolve } = Promise.withResolvers<unknown>() |
| 25 | + const dialog = dialogBuilder |
| 26 | + .setButtons([ |
| 27 | + { |
| 28 | + label: t('maps', 'Photo folder'), |
| 29 | + callback() { |
| 30 | + resolve(placeFolder(latLong, myMapId)) |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + label: t('maps', 'Photo files'), |
| 35 | + callback() { |
| 36 | + resolve(placeFiles(latLong, myMapId)) |
| 37 | + }, |
| 38 | + variant: 'primary', |
| 39 | + }, |
| 40 | + ]) |
| 41 | + .build() |
| 42 | + |
| 43 | + await dialog.show() |
| 44 | + return promise |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Callback to select and place a folder. |
| 49 | + * |
| 50 | + * @param latLong - The location where to place |
| 51 | + * @param myMapId - The map to place photos to |
| 52 | + */ |
| 53 | +async function placeFolder(latLong: LotLong, myMapId: number) { |
| 54 | + const filePickerBuilder = new FilePickerBuilder(t('maps', 'Choose directory of photos to place')) |
| 55 | + const filePicker = filePickerBuilder.allowDirectories(true) |
| 56 | + .setMimeTypeFilter(['httpd/unix-directory']) |
| 57 | + .setButtonFactory((nodes) => [{ |
| 58 | + callback: () => {}, |
| 59 | + label: nodes.length === 1 |
| 60 | + ? t('maps', 'Select {photo}', { photo: nodes[0].displayname }, { escape: false }) |
| 61 | + : (nodes.length === 0 |
| 62 | + ? t('maps', 'Select folder') |
| 63 | + : n('maps', 'Select %n folder', 'Select %n folders', nodes.length) |
| 64 | + ), |
| 65 | + disabled: nodes.length === 0, |
| 66 | + variant: 'primary', |
| 67 | + }]) |
| 68 | + .setMultiSelect(false) |
| 69 | + .build() |
| 70 | + |
| 71 | + try { |
| 72 | + const folder = await filePicker.pick() |
| 73 | + return placePhotos([folder], [latLong.lat], [latLong.lng], true, myMapId) |
| 74 | + } catch { |
| 75 | + // cancelled picking |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Callback to select and place on or multiple photo files. |
| 81 | + * |
| 82 | + * @param latLong - The location where to place |
| 83 | + * @param myMapId - The map to place photos to |
| 84 | + */ |
| 85 | +async function placeFiles(latLong: LotLong, myMapId: number) { |
| 86 | + const filePickerBuilder = new FilePickerBuilder(t('maps', 'Choose photos to place')) |
| 87 | + const filePicker = filePickerBuilder |
| 88 | + .setMimeTypeFilter(['image/jpeg', 'image/tiff']) |
| 89 | + .setButtonFactory((nodes) => [{ |
| 90 | + callback: () => {}, |
| 91 | + label: nodes.length === 1 |
| 92 | + ? t('maps', 'Select {photo}', { photo: nodes[0].displayname }, { escape: false }) |
| 93 | + : (nodes.length === 0 |
| 94 | + ? t('maps', 'Select photo') |
| 95 | + : n('maps', 'Select %n photo', 'Select %n photos', nodes.length) |
| 96 | + ), |
| 97 | + disabled: nodes.length === 0, |
| 98 | + variant: 'primary', |
| 99 | + }]) |
| 100 | + .setMultiSelect(true) |
| 101 | + .build() |
| 102 | + |
| 103 | + try { |
| 104 | + const nodes = await filePicker.pick() |
| 105 | + return placePhotos(nodes, [latLong.lat], [latLong.lng], false, myMapId) |
| 106 | + } catch { |
| 107 | + // cancelled picking |
| 108 | + } |
| 109 | +} |
0 commit comments