|
| 1 | +import CoordinateVM from "https://js.arcgis.com/4.29/@arcgis/core/widgets/CoordinateConversion/CoordinateConversionViewModel.js"; |
| 2 | +import Conversion from "https://js.arcgis.com/4.29/@arcgis/core/widgets/CoordinateConversion/support/Conversion.js"; |
| 3 | +import { watch } from "https://js.arcgis.com/4.29/@arcgis/core/core/reactiveUtils.js"; |
| 4 | + |
| 5 | +export function createCustomCoordinateConversion(view) { |
| 6 | + const vm = new CoordinateVM({ view, multipleConversions: false }); |
| 7 | + |
| 8 | + const customWidget = document.createElement("div"); |
| 9 | + customWidget.innerHTML = ` |
| 10 | +<div class="custom-coordinate-conversion"> |
| 11 | + |
| 12 | + <calcite-segmented-control id="custom-coordinate-mode"> |
| 13 | + <calcite-segmented-control-item icon-start="cursor" value="live" checked>Live</calcite-segmented-control-item> |
| 14 | + <calcite-segmented-control-item icon-start="pin" value="capture">Capture</calcite-segmented-control-item> |
| 15 | + </calcite-segmented-control> |
| 16 | + |
| 17 | + <calcite-select id="custom-coordinate-select"></calcite-select> |
| 18 | + |
| 19 | + <calcite-inline-editable id="custom-coordinate-editable" controls> |
| 20 | + <calcite-input id="custom-coordinate-input" placeholder="Enter coordinates"></calcite-input> |
| 21 | + </calcite-inline-editable> |
| 22 | + |
| 23 | +</div> |
| 24 | +`; |
| 25 | + |
| 26 | + view.ui.add(customWidget, "bottom-right"); |
| 27 | + |
| 28 | + const coordinateSelect = document.getElementById("custom-coordinate-select"); |
| 29 | + const coordinateInput = document.getElementById("custom-coordinate-input"); |
| 30 | + const coordinateMode = document.getElementById("custom-coordinate-mode"); |
| 31 | + const coordinateEditable = document.getElementById( |
| 32 | + "custom-coordinate-editable" |
| 33 | + ); |
| 34 | + |
| 35 | + let activeFormat = null; |
| 36 | + |
| 37 | + watch( |
| 38 | + () => vm.conversions.getItemAt(0)?.format, |
| 39 | + (format) => updateFormats(format), |
| 40 | + { initial: true } |
| 41 | + ); |
| 42 | + watch( |
| 43 | + () => vm.conversions.getItemAt(0)?.displayCoordinate, |
| 44 | + (displayCoordinate) => updateCoordinates(displayCoordinate ?? ""), |
| 45 | + { initial: true } |
| 46 | + ); |
| 47 | + |
| 48 | + coordinateSelect.addEventListener("calciteSelectChange", (event) => { |
| 49 | + const value = event.target.value; |
| 50 | + const format = vm.formats.find((format) => format.name === value); |
| 51 | + const newConversion = new Conversion({ format }); |
| 52 | + vm.conversions.removeAt(0); |
| 53 | + vm.conversions.add(newConversion, 0); |
| 54 | + }); |
| 55 | + |
| 56 | + coordinateMode.addEventListener("calciteSegmentedControlChange", (event) => { |
| 57 | + const value = event.target.value; |
| 58 | + vm.mode = value; |
| 59 | + }); |
| 60 | + |
| 61 | + async function reverseConvert() { |
| 62 | + coordinateEditable.editingEnabled = false; |
| 63 | + const value = coordinateInput.value; |
| 64 | + try { |
| 65 | + const point = await vm.reverseConvert(value, activeFormat); |
| 66 | + view.goTo(point); |
| 67 | + } catch (e) { |
| 68 | + coordinateInput.status = "invalid"; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + coordinateEditable.addEventListener( |
| 73 | + "calciteInlineEditableEditConfirm", |
| 74 | + () => { |
| 75 | + reverseConvert(); |
| 76 | + } |
| 77 | + ); |
| 78 | + |
| 79 | + coordinateInput.addEventListener("keydown", (event) => { |
| 80 | + if (event.key === "Enter") { |
| 81 | + reverseConvert(); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + function updateFormats(currentFormat) { |
| 86 | + activeFormat = currentFormat; |
| 87 | + |
| 88 | + const formatLookup = { |
| 89 | + basemap: "Basemap (XY)", |
| 90 | + xy: "Longitude, Latitude (WGS84)", |
| 91 | + mgrs: "Military Grid Reference System (MGRS)", |
| 92 | + usng: "United States National Grid (USNG)", |
| 93 | + utm: "Universal Transverse Mercator (UTM)", |
| 94 | + dd: "Decimal Degrees (DD)", |
| 95 | + ddm: "Degrees Decimal Minutes (DDS)", |
| 96 | + dms: "Degrees Minutes Seconds (DMS)", |
| 97 | + }; |
| 98 | + |
| 99 | + const options = vm.formats |
| 100 | + .toArray() |
| 101 | + .map( |
| 102 | + (format) => |
| 103 | + `<calcite-option ${ |
| 104 | + format === currentFormat ? "selected" : "" |
| 105 | + } value="${format.name}">${ |
| 106 | + formatLookup[format.name.toLowerCase()] |
| 107 | + }</calcite-option>` |
| 108 | + ) |
| 109 | + .join(""); |
| 110 | + |
| 111 | + coordinateSelect.innerHTML = options; |
| 112 | + } |
| 113 | + |
| 114 | + function updateCoordinates(activeDisplayCoordinate) { |
| 115 | + coordinateInput.value = activeDisplayCoordinate; |
| 116 | + coordinateInput.status = "valid"; |
| 117 | + } |
| 118 | +} |
0 commit comments