import LocationFinder from "./LocationFinder"; import { ReactNode } from "react"; import { Expr, Row, Image } from "@mwater/expressions"; import { LocalizeString } from "ez-localize"; /** Non-geometry fields needed to select and display an administrative region */ export interface AdminRegionRow extends Row { /** Integer admin region id */ _id: number; /** Admin region level. 0 is country. */ level: number; /** Display name without parent names */ name: string; /** Complete comma-delimited name including parent names */ full_name: string; /** Level label, such as Province or District */ type?: string; /** Country admin region id */ level0: number; /** Level 1 ancestor id */ level1?: number | null; /** Level 2 ancestor id */ level2?: number | null; /** Level 3 ancestor id */ level3?: number | null; /** Level 4 ancestor id */ level4?: number | null; /** Level 5 ancestor id */ level5?: number | null; } export interface StickyStorage { get(keyname: string): any; set(keyname: string, value: any): void; } /** Context that is passed to a form for display */ export interface FormContext { /** Used by ImageQuestion and ImagesQuestion */ imageManager: ImageManager; /** Used by ImageQuestion and ImagesQuestion */ imageAcquirer?: ImageAcquirer; /** Used for file/filelist editing. Optional until file question types are added. */ fileManager?: FileManager; /** Used by all questions. Optional. Override default locationFinder which is new LocationFinder */ locationFinder?: LocationFinder; /** Used by LocationQuestion * Function which takes location object { latitude, longitude, accuracy, altitude?, altitudeAccuracy? } * and callback with new location object. Opens a map to the location and allows setting if onSet present. */ displayMap?: (location: Location | undefined, onSet?: (location: Location | null) => void) => void; /** Stores key-value pairs for sticky questions. Object with two functions: * get(keyname) : gets object stored under keyname. null/undefined if none * set(keyname, value) : saves object as keyname */ stickyStorage?: StickyStorage; /** Scans a barcode. Calls options.success with string if successful */ scanBarcode?: (options: { success: (value: string) => void; error: (err: any) => void; }) => void; /** Select an entity */ selectEntity?: (options: { /** e.g. "water_point" */ entityType: string; /** Filter to apply to entities. MongoDB style */ entityFilter?: any; /** called with _id of entity selected */ callback: (entityId: string) => void; }) => void; /** * entityType: e.g. "water_point" * entityId: _id of entity * callback: called with an entity e.g. { _id: some id, a: "abc", b: 123 } or callback null if entity not found */ getEntityById: (entityType: string, entityId: string, callback: (entity: any) => void) => void; /** * entityType: e.g. "water_point" * entityCode: code of entity * callback: called with an entity e.g. { _id: some id, a: "abc", b: 123 } or callback null if entity not found */ getEntityByCode: (entityType: string, entityCode: string, callback: (entity: any) => void) => void; /** Edit an entity * callback: is called when update is complete */ editEntity?: (entityType: string, entityId: string, callback: () => void) => void; /** View an entity in the standard view modal * callback: is called when the modal is closed after an update */ viewEntity?: (entityType: string, entityId: string, callback?: () => void) => void; /** Renders an entity as a React element for summary (small box) */ renderEntitySummaryView: (entityType: string, entity: any) => ReactNode; /** Renders an entity as a React element for list (compact) */ renderEntityListItemView: (entityType: string, entity: any) => ReactNode; /** True if current user can edit the entity specified. Required if editEntity present */ canEditEntity?: (entityType: string, entity: any) => boolean; /** Get all rows of a custom table * @param tableId table id e.g. custom.abc.xyz * @param filterExpr filter expression to filter the rows * @param locale locale to use for the rows (for expressions that depend on locale) * @returns rows * @throws error if failed to load rows with localized error message */ getCustomTableRows: (tableId: string, filterExpr: Expr, locale?: string) => Promise; /** Get a specific row of a custom table */ getCustomTableRow: (tableId: string, rowId: string) => Promise; /** Get a specific administrative region row */ getAdminRegion: (id: number) => Promise; /** Get child administrative regions at a level under a parent region. Use parentId null and level 0 for countries. */ getAdminRegionChildren: (parentId: number | null, level: number) => Promise; /** * Get an asset * * @param assetSystemId id of the asset system * @param assetId id of the asset */ getAssetById: (assetSystemId: number, assetId: string) => Promise; /** Select an asset with optional filter * @param assetSystemId id of the asset system * @param assetId currently selected asset * @param filter MongoDB-style filter on assets * @param callback called with _id of asset selected or null. Never called if cancelled * * @deprecated Legacy assets are no longer supported. */ selectAsset?: (assetSystemId: number, assetId: string | null, filter: any, callback: (assetId: string | null) => void) => void; /** Renders an asset as a React element for summary (small box) */ renderAssetSummaryView?: (assetSystemId: number, assetId: string) => ReactNode; } /** Used by ImageQuestion and ImagesQuestion */ export interface ImageManager { /** Gets url of image */ getImageUrl: (id: string, success: (url: string) => void, error: (err: any) => void) => void; /** Gets url of thumbnail of image */ getImageThumbnailUrl: (id: string, success: (url: string) => void, error: () => void) => void; } /** Options for acquiring images */ export interface ImageAcquirerOptions { /** Maximum dimension (width or height) for the image in pixels */ maxImageDimension?: number | null; } /** Used by ImageQuestion and ImagesQuestion. If cancelled, neither success nor error is called */ export interface ImageAcquirer { /** Acquire an image */ acquire(options: ImageAcquirerOptions, success: (image: Image) => void, error: (err: any) => void): void; /** Acquire multiple images */ acquireMultiple(options: ImageAcquirerOptions, success: (images: Image[]) => void, error: (err: any) => void): void; /** Upload a base64-encoded image and get back an Image object. * @param mimeType MIME type of the image (e.g. "image/jpeg" or "image/png"). Defaults to "image/jpeg". */ addImageFromBase64(base64: string, success: (image: Image) => void, error: (err: any) => void, mimeType?: string): void; } /** Used for file/filelist property editing. Promise-based unlike the legacy callback-based ImageManager. */ export interface FileManager { /** Get URL to download a file. Async to allow checking local cache on mobile. */ getFileUrl(fileId: string): Promise; /** Upload a file. Resolves when saved (locally on mobile, or uploaded on web). */ uploadFile(file: File, fileId: string): Promise; } /** Location object */ export interface Location { latitude: number; longitude: number; /** Elevation, taking into account mastHeight and depth if present */ altitude?: number; accuracy?: number; altitudeAccuracy?: number; } export interface InnerFormContext extends FormContext { /** Locale of the form */ locale?: string; /** Localizer */ T: LocalizeString; /** True to disable the confidential fields, used during editing responses with confidential data */ disableConfidentialFields?: boolean; }