2020-09-25 12:44:04 +02:00
import { DropDown } from "./DropDown" ;
import * as EmailValidator from "email-validator" ;
import { parsePhoneNumberFromString } from "libphonenumber-js" ;
import { InputElement } from "./InputElement" ;
import { TextField } from "./TextField" ;
import { UIEventSource } from "../../Logic/UIEventSource" ;
2020-09-26 03:02:19 +02:00
import CombinedInputElement from "./CombinedInputElement" ;
import SimpleDatePicker from "./SimpleDatePicker" ;
2021-01-02 16:04:16 +01:00
import OpeningHoursInput from "../OpeningHours/OpeningHoursInput" ;
2020-11-15 03:10:44 +01:00
import DirectionInput from "./DirectionInput" ;
2021-05-11 02:39:51 +02:00
import ColorPicker from "./ColorPicker" ;
import { Utils } from "../../Utils" ;
2021-06-23 02:15:28 +02:00
import Loc from "../../Models/Loc" ;
2021-06-25 20:38:12 +02:00
import { Unit } from "../../Customizations/JSON/Denomination" ;
2021-06-28 00:45:49 +02:00
import BaseUIElement from "../BaseUIElement" ;
2021-07-20 01:33:58 +02:00
import LengthInput from "./LengthInput" ;
import { GeoOperations } from "../../Logic/GeoOperations" ;
2020-09-26 03:02:19 +02:00
interface TextFieldDef {
name : string ,
explanation : string ,
2021-05-11 02:39:51 +02:00
isValid : ( ( s : string , country ? : ( ) = > string ) = > boolean ) ,
2020-12-05 03:22:17 +01:00
reformat ? : ( ( s : string , country ? : ( ) = > string ) = > string ) ,
2020-11-17 02:22:48 +01:00
inputHelper ? : ( value : UIEventSource < string > , options ? : {
2021-06-23 02:15:28 +02:00
location : [ number , number ] ,
2021-07-20 01:33:58 +02:00
mapBackgroundLayer? : UIEventSource < any > ,
args : ( string | number | boolean ) [ ]
feature? : any
2020-11-17 02:22:48 +01:00
} ) = > InputElement < string > ,
2021-05-11 02:39:51 +02:00
inputmode? : string
2020-09-26 03:02:19 +02:00
}
2020-09-25 12:44:04 +02:00
export default class ValidatedTextField {
2021-07-20 01:33:58 +02:00
public static bestLayerAt : ( location : UIEventSource < Loc > , preferences : UIEventSource < string [ ] > ) = > any
2020-09-25 12:44:04 +02:00
2020-09-26 03:02:19 +02:00
public static tpList : TextFieldDef [ ] = [
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"string" ,
"A basic string" ) ,
2020-10-27 01:01:34 +01:00
ValidatedTextField . tp (
"text" ,
2021-05-11 02:39:51 +02:00
"A string, but allows input of longer strings more comfortably (a text area)" ,
undefined ,
undefined ,
undefined ,
"text" ) ,
2021-06-16 17:09:32 +02:00
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"date" ,
2020-09-26 03:02:19 +02:00
"A date" ,
( str ) = > {
const time = Date . parse ( str ) ;
return ! isNaN ( time ) ;
} ,
( str ) = > {
const d = new Date ( str ) ;
let month = '' + ( d . getMonth ( ) + 1 ) ;
let day = '' + d . getDate ( ) ;
const year = d . getFullYear ( ) ;
if ( month . length < 2 )
month = '0' + month ;
if ( day . length < 2 )
day = '0' + day ;
return [ year , month , day ] . join ( '-' ) ;
} ,
( value ) = > new SimpleDatePicker ( value ) ) ,
2021-07-20 01:33:58 +02:00
ValidatedTextField . tp (
"direction" ,
"A geographical direction, in degrees. 0° is north, 90° is east, ... Will return a value between 0 (incl) and 360 (excl)" ,
( str ) = > {
str = "" + str ;
return str !== undefined && str . indexOf ( "." ) < 0 && ! isNaN ( Number ( str ) ) && Number ( str ) >= 0 && Number ( str ) <= 360
} , str = > str ,
( value , options ) = > {
const args = options . args ? ? [ ]
let zoom = 19
if ( args [ 0 ] ) {
zoom = Number ( args [ 0 ] )
if ( isNaN ( zoom ) ) {
throw "Invalid zoom level for argument at 'length'-input"
}
}
const location = new UIEventSource < Loc > ( {
lat : options.location [ 0 ] ,
lon : options.location [ 1 ] ,
zoom : zoom
} )
if ( args [ 1 ] ) {
// We have a prefered map!
options . mapBackgroundLayer = ValidatedTextField . bestLayerAt (
location , new UIEventSource < string [ ] > ( args [ 1 ] . split ( "," ) )
)
}
const di = new DirectionInput ( options . mapBackgroundLayer , location , value )
di . SetStyle ( "height: 20rem;" ) ;
return di ;
} ,
"numeric"
) ,
ValidatedTextField . tp (
"length" ,
"A geographical length in meters (rounded at two points). Will give an extra minimap with a measurement tool. Arguments: [ zoomlevel, preferredBackgroundMapType (comma seperated) ], e.g. `[\"21\", \"map,photo\"]" ,
( str ) = > {
const t = Number ( str )
return ! isNaN ( t )
} ,
str = > str ,
( value , options ) = > {
const args = options . args ? ? [ ]
let zoom = 19
if ( args [ 0 ] ) {
zoom = Number ( args [ 0 ] )
if ( isNaN ( zoom ) ) {
throw "Invalid zoom level for argument at 'length'-input"
}
}
// Bit of a hack: we project the centerpoint to the closes point on the road - if available
2021-08-06 20:22:23 +02:00
if ( options . feature !== undefined && options . feature . geometry . type !== "Point" ) {
2021-07-20 01:59:19 +02:00
const lonlat : [ number , number ] = [ . . . options . location ]
lonlat . reverse ( )
options . location = < [ number , number ] > GeoOperations . nearestPoint ( options . feature , lonlat ) . geometry . coordinates
options . location . reverse ( )
2021-07-20 01:33:58 +02:00
}
const location = new UIEventSource < Loc > ( {
lat : options.location [ 0 ] ,
lon : options.location [ 1 ] ,
zoom : zoom
} )
if ( args [ 1 ] ) {
// We have a prefered map!
options . mapBackgroundLayer = ValidatedTextField . bestLayerAt (
location , new UIEventSource < string [ ] > ( args [ 1 ] . split ( "," ) )
)
}
const li = new LengthInput ( options . mapBackgroundLayer , location , value )
li . SetStyle ( "height: 20rem;" )
return li ;
2021-08-06 20:22:23 +02:00
} ,
"decimal"
2021-07-20 01:33:58 +02:00
) ,
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"wikidata" ,
2021-06-16 17:09:32 +02:00
"A wikidata identifier, e.g. Q42" ,
( str ) = > {
if ( str === undefined ) {
return false ;
}
return ( str . length > 1 && ( str . startsWith ( "q" ) || str . startsWith ( "Q" ) ) || str . startsWith ( "https://www.wikidata.org/wiki/Q" ) )
} ,
( str ) = > {
if ( str === undefined ) {
return undefined ;
}
const wd = "https://www.wikidata.org/wiki/" ;
if ( str . startsWith ( wd ) ) {
str = str . substr ( wd . length )
}
return str . toUpperCase ( ) ;
} ) ,
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"int" ,
"A number" ,
( str ) = > {
str = "" + str ;
return str !== undefined && str . indexOf ( "." ) < 0 && ! isNaN ( Number ( str ) )
2021-05-11 02:39:51 +02:00
} ,
undefined ,
undefined ,
"numeric" ) ,
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"nat" ,
"A positive number or zero" ,
( str ) = > {
str = "" + str ;
return str !== undefined && str . indexOf ( "." ) < 0 && ! isNaN ( Number ( str ) ) && Number ( str ) >= 0
2021-05-11 02:39:51 +02:00
} ,
undefined ,
undefined ,
"numeric" ) ,
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"pnat" ,
"A strict positive number" ,
( str ) = > {
str = "" + str ;
return str !== undefined && str . indexOf ( "." ) < 0 && ! isNaN ( Number ( str ) ) && Number ( str ) > 0
2021-05-11 02:39:51 +02:00
} ,
undefined ,
undefined ,
"numeric" ) ,
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"float" ,
"A decimal" ,
2021-05-11 02:39:51 +02:00
( str ) = > ! isNaN ( Number ( str ) ) ,
undefined ,
undefined ,
"decimal" ) ,
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"pfloat" ,
"A positive decimal (incl zero)" ,
2021-05-11 02:39:51 +02:00
( str ) = > ! isNaN ( Number ( str ) ) && Number ( str ) >= 0 ,
undefined ,
undefined ,
"decimal" ) ,
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"email" ,
"An email adress" ,
2021-05-11 02:39:51 +02:00
( str ) = > EmailValidator . validate ( str ) ,
undefined ,
undefined ,
"email" ) ,
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"url" ,
2020-09-26 03:02:19 +02:00
"A url" ,
( str ) = > {
try {
new URL ( str ) ;
return true ;
} catch ( e ) {
return false ;
}
2021-05-11 02:39:51 +02:00
} ,
( str ) = > {
2020-09-26 03:02:19 +02:00
try {
const url = new URL ( str ) ;
const blacklistedTrackingParams = [
"fbclid" , // Oh god, how I hate the fbclid. Let it burn, burn in hell!
"gclid" ,
2021-05-11 02:39:51 +02:00
"cmpid" , "agid" , "utm" , "utm_source" , "utm_medium" ]
2020-09-26 03:02:19 +02:00
for ( const dontLike of blacklistedTrackingParams ) {
url . searchParams . delete ( dontLike )
}
2021-05-10 23:49:46 +02:00
let cleaned = url . toString ( ) ;
2021-05-11 02:39:51 +02:00
if ( cleaned . endsWith ( "/" ) && ! str . endsWith ( "/" ) ) {
2021-05-10 23:49:46 +02:00
// Do not add a trailing '/' if it wasn't typed originally
cleaned = cleaned . substr ( 0 , cleaned . length - 1 )
}
return cleaned ;
2020-09-26 03:02:19 +02:00
} catch ( e ) {
console . error ( e )
return undefined ;
}
2021-05-11 02:39:51 +02:00
} ,
undefined ,
"url" ) ,
2020-09-25 12:44:04 +02:00
ValidatedTextField . tp (
"phone" ,
"A phone number" ,
2020-12-05 03:22:17 +01:00
( str , country : ( ) = > string ) = > {
2020-09-26 01:43:20 +02:00
if ( str === undefined ) {
return false ;
}
2020-12-05 03:22:17 +01:00
return parsePhoneNumberFromString ( str , ( country ( ) ) ? . toUpperCase ( ) as any ) ? . isValid ( ) ? ? false
2020-09-25 12:44:04 +02:00
} ,
2021-05-11 02:39:51 +02:00
( str , country : ( ) = > string ) = > parsePhoneNumberFromString ( str , ( country ( ) ) ? . toUpperCase ( ) as any ) . formatInternational ( ) ,
undefined ,
"tel"
2020-10-04 12:55:44 +02:00
) ,
ValidatedTextField . tp (
"opening_hours" ,
"Has extra elements to easily input when a POI is opened" ,
2021-05-11 02:39:51 +02:00
( ) = > true ,
str = > str ,
2020-10-04 12:55:44 +02:00
( value ) = > {
2020-10-08 19:03:00 +02:00
return new OpeningHoursInput ( value ) ;
2020-10-04 12:55:44 +02:00
}
2021-05-11 02:39:51 +02:00
) ,
ValidatedTextField . tp (
"color" ,
"Shows a color picker" ,
( ) = > true ,
str = > str ,
( value ) = > {
return new ColorPicker ( value . map ( color = > {
return Utils . ColourNameToHex ( color ? ? "" ) ;
} , [ ] , str = > Utils . HexToColourName ( str ) ) )
}
2020-09-25 12:44:04 +02:00
)
]
2021-05-11 02:39:51 +02:00
/ * *
* { string ( typename ) -- > TextFieldDef }
* /
public static AllTypes = ValidatedTextField . allTypesDict ( ) ;
2021-07-20 01:33:58 +02:00
2020-09-26 03:02:19 +02:00
public static InputForType ( type : string , options ? : {
2021-06-28 00:45:49 +02:00
placeholder? : string | BaseUIElement ,
2020-09-26 03:02:19 +02:00
value? : UIEventSource < string > ,
2021-05-11 02:39:51 +02:00
htmlType? : string ,
2021-06-16 17:09:32 +02:00
textArea? : boolean ,
inputMode? : string ,
2020-09-26 03:02:19 +02:00
textAreaRows? : number ,
2020-12-05 03:22:17 +01:00
isValid ? : ( ( s : string , country : ( ) = > string ) = > boolean ) ,
country ? : ( ) = > string ,
2021-06-23 02:15:28 +02:00
location ? : [ number /*lat*/ , number /*lon*/ ] ,
2021-06-25 20:38:12 +02:00
mapBackgroundLayer? : UIEventSource < any > ,
2021-07-20 01:33:58 +02:00
unit? : Unit ,
args ? : ( string | number | boolean ) [ ] // Extra arguments for the inputHelper,
feature? : any
2020-09-26 03:02:19 +02:00
} ) : InputElement < string > {
options = options ? ? { } ;
options . placeholder = options . placeholder ? ? type ;
const tp : TextFieldDef = ValidatedTextField . AllTypes [ type ]
2020-09-26 21:00:03 +02:00
const isValidTp = tp . isValid ;
let isValid ;
2020-10-27 01:01:34 +01:00
options . textArea = options . textArea ? ? type === "text" ;
2020-09-26 03:02:19 +02:00
if ( options . isValid ) {
const optValid = options . isValid ;
isValid = ( str , country ) = > {
2021-05-11 02:39:51 +02:00
if ( str === undefined ) {
2020-09-26 21:00:03 +02:00
return false ;
}
2021-07-20 01:33:58 +02:00
if ( options . unit ) {
2021-06-25 20:38:12 +02:00
str = options . unit . stripUnitParts ( str )
}
2020-09-26 21:00:03 +02:00
return isValidTp ( str , country ? ? options . country ) && optValid ( str , country ? ? options . country ) ;
2020-09-26 03:02:19 +02:00
}
2021-05-11 02:39:51 +02:00
} else {
2020-09-26 21:00:03 +02:00
isValid = isValidTp ;
2020-09-26 03:02:19 +02:00
}
options . isValid = isValid ;
2021-05-11 02:39:51 +02:00
options . inputMode = tp . inputmode ;
2020-09-26 03:02:19 +02:00
let input : InputElement < string > = new TextField ( options ) ;
2020-09-26 21:00:03 +02:00
if ( tp . reformat ) {
input . GetValue ( ) . addCallbackAndRun ( str = > {
if ( ! options . isValid ( str , options . country ) ) {
return ;
}
const formatted = tp . reformat ( str , options . country ) ;
input . GetValue ( ) . setData ( formatted ) ;
} )
}
2021-07-20 01:33:58 +02:00
if ( options . unit ) {
2021-06-25 20:38:12 +02:00
// We need to apply a unit.
// This implies:
// We have to create a dropdown with applicable denominations, and fuse those values
const unit = options . unit
const unitDropDown = new DropDown ( "" ,
unit . denominations . map ( denom = > {
return {
shown : denom.human ,
value : denom
}
} )
)
unitDropDown . GetValue ( ) . setData ( unit . defaultDenom )
2021-07-11 15:44:17 +02:00
unitDropDown . SetClass ( "w-min" )
2021-06-25 20:38:12 +02:00
input = new CombinedInputElement (
input ,
unitDropDown ,
2021-07-04 20:36:19 +02:00
// combine the value from the textfield and the dropdown into the resulting value that should go into OSM
2021-07-20 01:33:58 +02:00
( text , denom ) = > denom ? . canonicalValue ( text , true ) ? ? undefined ,
2021-06-25 20:38:12 +02:00
( valueWithDenom : string ) = > {
2021-07-04 20:36:19 +02:00
// Take the value from OSM and feed it into the textfield and the dropdown
const withDenom = unit . findDenomination ( valueWithDenom ) ;
2021-07-20 01:33:58 +02:00
if ( withDenom === undefined ) {
2021-07-04 20:36:19 +02:00
// Not a valid value at all - we give it undefined and leave the details up to the other elements
return [ undefined , undefined ]
2021-06-25 20:38:12 +02:00
}
2021-07-04 20:36:19 +02:00
const [ strippedText , denom ] = withDenom
2021-07-20 01:33:58 +02:00
if ( strippedText === undefined ) {
2021-07-04 20:36:19 +02:00
return [ undefined , undefined ]
}
return [ strippedText , denom ]
2021-06-25 20:38:12 +02:00
}
) . SetClass ( "flex" )
}
2020-09-26 03:02:19 +02:00
if ( tp . inputHelper ) {
2021-07-20 01:33:58 +02:00
const helper = tp . inputHelper ( input . GetValue ( ) , {
2021-06-24 02:33:26 +02:00
location : options.location ,
2021-07-20 01:33:58 +02:00
mapBackgroundLayer : options.mapBackgroundLayer ,
args : options.args ,
feature : options.feature
2021-06-24 02:33:26 +02:00
} )
input = new CombinedInputElement ( input , helper ,
2021-06-24 01:55:45 +02:00
( a , _ ) = > a , // We can ignore b, as they are linked earlier
2021-06-22 03:16:45 +02:00
a = > [ a , a ]
2021-07-20 01:33:58 +02:00
) ;
2020-09-26 03:02:19 +02:00
}
return input ;
2020-09-25 12:44:04 +02:00
}
2021-07-20 01:33:58 +02:00
2021-05-11 02:39:51 +02:00
public static HelpText ( ) : string {
const explanations = ValidatedTextField . tpList . map ( type = > [ "## " + type . name , "" , type . explanation ] . join ( "\n" ) ) . join ( "\n\n" )
return "# Available types for text fields\n\nThe listed types here trigger a special input element. Use them in `tagrendering.freeform.type` of your tagrendering to activate them\n\n" + explanations
}
private static tp ( name : string ,
explanation : string ,
isValid ? : ( ( s : string , country ? : ( ) = > string ) = > boolean ) ,
reformat ? : ( ( s : string , country ? : ( ) = > string ) = > string ) ,
inputHelper ? : ( value : UIEventSource < string > , options ? : {
2021-06-23 02:15:28 +02:00
location : [ number , number ] ,
2021-07-20 01:33:58 +02:00
mapBackgroundLayer : UIEventSource < any > ,
args : string [ ] ,
feature : any
2021-05-11 02:39:51 +02:00
} ) = > InputElement < string > ,
inputmode? : string ) : TextFieldDef {
if ( isValid === undefined ) {
isValid = ( ) = > true ;
}
if ( reformat === undefined ) {
reformat = ( str , _ ) = > str ;
}
return {
name : name ,
explanation : explanation ,
isValid : isValid ,
reformat : reformat ,
inputHelper : inputHelper ,
inputmode : inputmode
}
}
private static allTypesDict() {
const types = { } ;
for ( const tp of ValidatedTextField . tpList ) {
types [ tp . name ] = tp ;
}
return types ;
}
2020-09-25 12:44:04 +02:00
}