|
| 1 | +import { |
| 2 | + CompassWidget, |
| 3 | + FullscreenWidget, |
| 4 | + _ScaleWidget as ScaleWidget, |
| 5 | + ZoomWidget, |
| 6 | +} from "@deck.gl/react"; |
| 7 | +import type { WidgetModel } from "@jupyter-widgets/base"; |
| 8 | +import React from "react"; |
| 9 | +import { |
| 10 | + FullscreenControl, |
| 11 | + NavigationControl, |
| 12 | + ScaleControl, |
| 13 | +} from "react-map-gl/maplibre"; |
| 14 | + |
| 15 | +import { isDefined } from "../util"; |
| 16 | +import { BaseModel } from "./base"; |
| 17 | + |
| 18 | +export abstract class BaseMapControlModel extends BaseModel { |
| 19 | + static controlType: string; |
| 20 | + |
| 21 | + protected position?: |
| 22 | + | "top-left" |
| 23 | + | "top-right" |
| 24 | + | "bottom-left" |
| 25 | + | "bottom-right"; |
| 26 | + |
| 27 | + constructor(model: WidgetModel, updateStateCallback: () => void) { |
| 28 | + super(model, updateStateCallback); |
| 29 | + |
| 30 | + this.initRegularAttribute("position", "position"); |
| 31 | + } |
| 32 | + |
| 33 | + baseDeckProps() { |
| 34 | + return { |
| 35 | + ...(isDefined(this.position) ? { placement: this.position } : {}), |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + baseMaplibreProps() { |
| 40 | + return { |
| 41 | + ...(isDefined(this.position) ? { position: this.position } : {}), |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + abstract renderDeck(): React.JSX.Element | null; |
| 46 | + abstract renderMaplibre(): React.JSX.Element | null; |
| 47 | +} |
| 48 | + |
| 49 | +export class FullscreenControlModel extends BaseMapControlModel { |
| 50 | + static controlType = "fullscreen"; |
| 51 | + |
| 52 | + constructor(model: WidgetModel, updateStateCallback: () => void) { |
| 53 | + super(model, updateStateCallback); |
| 54 | + } |
| 55 | + |
| 56 | + renderDeck() { |
| 57 | + const { placement, ...otherProps } = this.baseDeckProps(); |
| 58 | + const props = { placement: placement || "top-right", ...otherProps }; |
| 59 | + console.log(placement); |
| 60 | + return <div>{<FullscreenWidget {...props} />}</div>; |
| 61 | + } |
| 62 | + |
| 63 | + renderMaplibre() { |
| 64 | + return <div>{<FullscreenControl {...this.baseMaplibreProps()} />}</div>; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export class NavigationControlModel extends BaseMapControlModel { |
| 69 | + static controlType = "navigation"; |
| 70 | + |
| 71 | + protected showCompass?: boolean; |
| 72 | + protected showZoom?: boolean; |
| 73 | + protected visualizePitch?: boolean; |
| 74 | + protected visualizeRoll?: boolean; |
| 75 | + |
| 76 | + constructor(model: WidgetModel, updateStateCallback: () => void) { |
| 77 | + super(model, updateStateCallback); |
| 78 | + |
| 79 | + this.initRegularAttribute("show_compass", "showCompass"); |
| 80 | + this.initRegularAttribute("show_zoom", "showZoom"); |
| 81 | + this.initRegularAttribute("visualize_pitch", "visualizePitch"); |
| 82 | + this.initRegularAttribute("visualize_roll", "visualizeRoll"); |
| 83 | + } |
| 84 | + |
| 85 | + renderDeck() { |
| 86 | + return ( |
| 87 | + <div> |
| 88 | + {this.showZoom && <ZoomWidget {...this.baseDeckProps()} />} |
| 89 | + {this.showCompass && <CompassWidget {...this.baseDeckProps()} />} |
| 90 | + </div> |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + renderMaplibre() { |
| 95 | + const props = { |
| 96 | + ...this.baseMaplibreProps(), |
| 97 | + ...(isDefined(this.showCompass) && { showCompass: this.showCompass }), |
| 98 | + ...(isDefined(this.showZoom) && { showZoom: this.showZoom }), |
| 99 | + ...(isDefined(this.visualizePitch) && { |
| 100 | + visualizePitch: this.visualizePitch, |
| 101 | + }), |
| 102 | + ...(isDefined(this.visualizeRoll) && { |
| 103 | + visualizeRoll: this.visualizeRoll, |
| 104 | + }), |
| 105 | + }; |
| 106 | + return <NavigationControl {...props} />; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +export class ScaleControlModel extends BaseMapControlModel { |
| 111 | + static controlType = "scale"; |
| 112 | + |
| 113 | + protected maxWidth?: number; |
| 114 | + protected unit?: "imperial" | "metric" | "nautical"; |
| 115 | + |
| 116 | + constructor(model: WidgetModel, updateStateCallback: () => void) { |
| 117 | + super(model, updateStateCallback); |
| 118 | + |
| 119 | + this.initRegularAttribute("max_width", "maxWidth"); |
| 120 | + this.initRegularAttribute("unit", "unit"); |
| 121 | + } |
| 122 | + |
| 123 | + renderDeck() { |
| 124 | + return <ScaleWidget {...this.baseDeckProps()} />; |
| 125 | + } |
| 126 | + |
| 127 | + renderMaplibre() { |
| 128 | + const props = { |
| 129 | + ...this.baseMaplibreProps(), |
| 130 | + ...(isDefined(this.maxWidth) && { maxWidth: this.maxWidth }), |
| 131 | + ...(isDefined(this.unit) && { unit: this.unit }), |
| 132 | + }; |
| 133 | + return <div>{<ScaleControl {...props} />}</div>; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +export async function initializeControl( |
| 138 | + model: WidgetModel, |
| 139 | + updateStateCallback: () => void, |
| 140 | +): Promise<BaseMapControlModel> { |
| 141 | + const controlType = model.get("_control_type"); |
| 142 | + let controlModel: BaseMapControlModel; |
| 143 | + switch (controlType) { |
| 144 | + case FullscreenControlModel.controlType: |
| 145 | + controlModel = new FullscreenControlModel(model, updateStateCallback); |
| 146 | + break; |
| 147 | + |
| 148 | + case NavigationControlModel.controlType: |
| 149 | + controlModel = new NavigationControlModel(model, updateStateCallback); |
| 150 | + break; |
| 151 | + |
| 152 | + case ScaleControlModel.controlType: |
| 153 | + controlModel = new ScaleControlModel(model, updateStateCallback); |
| 154 | + break; |
| 155 | + |
| 156 | + default: |
| 157 | + throw new Error(`no control supported for ${controlType}`); |
| 158 | + } |
| 159 | + |
| 160 | + return controlModel; |
| 161 | +} |
0 commit comments