2021-07-13 11:26:50 +02:00
import Toggle from "../Input/Toggle"
import Svg from "../../Svg"
import { UIEventSource } from "../../Logic/UIEventSource"
import { SubtleButton } from "../Base/SubtleButton"
2021-07-13 16:11:57 +02:00
import Combine from "../Base/Combine"
import { Button } from "../Base/Button"
import Translations from "../i18n/Translations"
2021-07-15 20:47:28 +02:00
import SplitAction from "../../Logic/Osm/Actions/SplitAction"
import Title from "../Base/Title"
2022-12-24 01:58:52 +01:00
import BaseUIElement from "../BaseUIElement"
import { VariableUiElement } from "../Base/VariableUIElement"
2023-01-06 03:37:22 +01:00
import { LoginToggle } from "./LoginButton"
2023-04-20 01:52:23 +02:00
import SvelteUIElement from "../Base/SvelteUIElement"
import WaySplitMap from "../BigComponents/WaySplitMap.svelte"
import { Feature , Point } from "geojson"
import { WayId } from "../../Models/OsmFeature"
import { OsmConnection } from "../../Logic/Osm/OsmConnection"
import { Changes } from "../../Logic/Osm/Changes"
import { IndexedFeatureSource } from "../../Logic/FeatureSource/FeatureSource"
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
2023-04-20 03:58:31 +02:00
import OsmObjectDownloader from "../../Logic/Osm/OsmObjectDownloader"
2022-01-18 18:52:42 +01:00
2022-12-24 01:58:52 +01:00
export default class SplitRoadWizard extends Combine {
2021-09-22 05:02:09 +02:00
public dialogIsOpened : UIEventSource < boolean >
2021-07-13 11:26:50 +02:00
/ * *
* A UI Element used for splitting roads
*
* @param id : The id of the road to remove
2022-01-19 20:34:04 +01:00
* @param state : the state of the application
2021-07-13 11:26:50 +02:00
* /
2023-04-20 01:52:23 +02:00
constructor (
id : WayId ,
state : {
layout? : LayoutConfig
osmConnection? : OsmConnection
2023-04-20 03:58:31 +02:00
osmObjectDownloader? : OsmObjectDownloader
2023-04-20 01:52:23 +02:00
changes? : Changes
indexedFeatures? : IndexedFeatureSource
selectedElement? : UIEventSource < Feature >
}
) {
2021-07-13 16:11:57 +02:00
const t = Translations . t . split
2021-07-13 11:26:50 +02:00
2021-07-15 00:26:25 +02:00
// Contains the points on the road that are selected to split on - contains geojson points with extra properties such as 'location' with the distance along the linestring
2023-04-20 01:52:23 +02:00
const splitPoints = new UIEventSource < Feature < Point > [ ] > ( [ ] )
2021-07-15 20:47:28 +02:00
const hasBeenSplit = new UIEventSource ( false )
2021-07-13 16:11:57 +02:00
// Toggle variable between show split button and map
2021-07-15 00:26:25 +02:00
const splitClicked = new UIEventSource < boolean > ( false )
2022-12-24 01:58:52 +01:00
2023-04-20 01:52:23 +02:00
const leafletMap = new UIEventSource < BaseUIElement > ( undefined )
function initMap() {
2023-04-20 03:58:31 +02:00
; ( async function (
id : WayId ,
splitPoints : UIEventSource < Feature [ ] >
) : Promise < BaseUIElement > {
return new SvelteUIElement ( WaySplitMap , {
osmWay : await state . osmObjectDownloader . DownloadObjectAsync ( id ) ,
splitPoints ,
} )
} ) ( id , splitPoints ) . then ( ( mapComponent ) = >
2023-04-20 01:52:23 +02:00
leafletMap . setData ( mapComponent . SetClass ( "w-full h-80" ) )
)
}
2022-12-24 01:58:52 +01:00
// Toggle between splitmap
const splitButton = new SubtleButton (
2023-05-08 01:55:21 +02:00
Svg . scissors_svg ( ) . SetStyle ( "height: 1.5rem; width: auto" ) ,
2022-12-24 01:58:52 +01:00
new Toggle (
t . splitAgain . Clone ( ) . SetClass ( "text-lg font-bold" ) ,
t . inviteToSplit . Clone ( ) . SetClass ( "text-lg font-bold" ) ,
hasBeenSplit
)
)
2023-01-06 03:37:22 +01:00
const splitToggle = new LoginToggle ( splitButton , t . loginToSplit . Clone ( ) , state )
2022-12-24 01:58:52 +01:00
// Save button
const saveButton = new Button ( t . split . Clone ( ) , async ( ) = > {
hasBeenSplit . setData ( true )
splitClicked . setData ( false )
const splitAction = new SplitAction (
id ,
2023-04-20 01:52:23 +02:00
splitPoints . data . map ( ( ff ) = > < [ number , number ] > ( < Point > ff . geometry ) . coordinates ) ,
2022-12-24 01:58:52 +01:00
{
2023-04-20 01:52:23 +02:00
theme : state?.layout?.id ,
2022-12-24 01:58:52 +01:00
} ,
2023-04-20 01:52:23 +02:00
5
2022-12-24 01:58:52 +01:00
)
2023-04-20 01:52:23 +02:00
await state . changes ? . applyAction ( splitAction )
2022-12-24 01:58:52 +01:00
// We throw away the old map and splitpoints, and create a new map from scratch
splitPoints . setData ( [ ] )
2023-01-04 18:52:49 +01:00
// Close the popup. The contributor has to select a segment again to make sure they continue editing the correct segment; see #1219
2023-04-20 01:52:23 +02:00
state . selectedElement ? . setData ( undefined )
2022-12-24 01:58:52 +01:00
} )
saveButton . SetClass ( "btn btn-primary mr-3" )
2023-01-03 01:01:59 +01:00
const disabledSaveButton = new Button ( t . split . Clone ( ) , undefined )
2022-12-24 01:58:52 +01:00
disabledSaveButton . SetClass ( "btn btn-disabled mr-3" )
// Only show the save button if there are split points defined
const saveToggle = new Toggle (
disabledSaveButton ,
saveButton ,
splitPoints . map ( ( data ) = > data . length === 0 )
)
const cancelButton = Translations . t . general . cancel
. Clone ( ) // Not using Button() element to prevent full width button
. SetClass ( "btn btn-secondary mr-3" )
. onClick ( ( ) = > {
splitPoints . setData ( [ ] )
splitClicked . setData ( false )
} )
cancelButton . SetClass ( "btn btn-secondary block" )
const splitTitle = new Title ( t . splitTitle )
const mapView = new Combine ( [
splitTitle ,
new VariableUiElement ( leafletMap ) ,
new Combine ( [ cancelButton , saveToggle ] ) . SetClass ( "flex flex-row" ) ,
] )
mapView . SetClass ( "question" )
super ( [
Toggle . If ( hasBeenSplit , ( ) = >
t . hasBeenSplit . Clone ( ) . SetClass ( "font-bold thanks block w-full" )
) ,
new Toggle ( mapView , splitToggle , splitClicked ) ,
] )
2023-04-20 17:42:07 +02:00
splitClicked . addCallback ( ( view ) = > {
if ( view ) {
initMap ( )
}
} )
2022-12-24 01:58:52 +01:00
this . dialogIsOpened = splitClicked
2023-01-04 18:52:49 +01:00
const self = this
splitButton . onClick ( ( ) = > {
splitClicked . setData ( true )
self . ScrollIntoView ( )
} )
2022-12-24 01:58:52 +01:00
}
2021-07-13 11:26:50 +02:00
}