|
1 | | -import { getAllItems, IResult } from "@concord-consortium/codap-plugin-api"; |
2 | | -import { makeAutoObservable } from "mobx"; |
| 1 | +import { addDataContextChangeListener, getAllItems, getCaseByID, getCaseBySearch, getSelectionList, initializePlugin, |
| 2 | + IResult, sendMessage } from "@concord-consortium/codap-plugin-api"; |
| 3 | +import { makeAutoObservable, reaction } from "mobx"; |
3 | 4 | import { |
4 | | - kPinColorAttributeName, kPinDataContextName, kPinLatAttributeName, kPinLongAttributeName |
| 5 | + kDataContextName, |
| 6 | + kInitialDimensions, |
| 7 | + kMapPinsCollectionName, |
| 8 | + kPinColorAttributeName, kPinDataContextName, kPinLatAttributeName, kPinLongAttributeName, |
| 9 | + kPluginName, |
| 10 | + kVersion |
5 | 11 | } from "../data/constants"; |
| 12 | +import { createOrUpdateMap, createSelectionList, deleteSelectionList, updateSelectionList } from "../utils/codap-utils"; |
6 | 13 | import { NeoDataset } from "./neo-types"; |
7 | 14 | import { getMapComponentInfo } from "../utils/codap-utils"; |
8 | 15 | import { geoLocSearch, MapComponentInfo } from "../utils/location-utils"; |
9 | 16 |
|
10 | | -interface IMapPin { |
| 17 | +export interface IMapPin { |
11 | 18 | color: string; |
12 | 19 | id: string; |
13 | 20 | lat: number; |
14 | 21 | long: number; |
15 | 22 | label: string; |
16 | 23 | } |
17 | 24 |
|
| 25 | +export interface ILocationCase { |
| 26 | + label: string; |
| 27 | + pinColor: string; |
| 28 | +} |
| 29 | + |
18 | 30 | export function pinLabel(pin: IMapPin) { |
19 | 31 | return `${pin.lat.toFixed(2)}, ${pin.long.toFixed(2)}`; |
20 | 32 | } |
21 | 33 |
|
| 34 | + |
22 | 35 | class PluginState { |
23 | 36 | neoDataset: NeoDataset | undefined; |
24 | 37 | neoDatasetName = ""; |
25 | 38 | pins: IMapPin[] = []; |
| 39 | + selectedPins: IMapPin[] = []; |
| 40 | + selectedCases: any[] =[]; |
26 | 41 |
|
27 | 42 | constructor() { |
28 | 43 | makeAutoObservable(this); |
| 44 | + // Reaction to changes in selectedPins from MapPinDataContext |
| 45 | + reaction( |
| 46 | + () => this.selectedPins, |
| 47 | + (selectedPins) => { |
| 48 | + this.handleSelectionChange( |
| 49 | + selectedPins, |
| 50 | + kDataContextName, |
| 51 | + kMapPinsCollectionName, |
| 52 | + (pin) => `label == ${pinLabel(pin)}` |
| 53 | + ); |
| 54 | + } |
| 55 | + ); |
| 56 | + // Reaction to changes in selectedCases NEOPluginDataContext |
| 57 | + reaction( |
| 58 | + () => this.selectedCases, |
| 59 | + (selectedCases) => { |
| 60 | + this.handleSelectionChange( |
| 61 | + selectedCases, |
| 62 | + kPinDataContextName, |
| 63 | + kMapPinsCollectionName, |
| 64 | + (sCase) => `pinColor == ${sCase.pinColor}` |
| 65 | + ); |
| 66 | + } |
| 67 | + ); |
29 | 68 | } |
30 | 69 |
|
31 | 70 | setNeoDataset(neoDataset: NeoDataset | undefined) { |
@@ -61,6 +100,124 @@ class PluginState { |
61 | 100 | }); |
62 | 101 | } |
63 | 102 | } |
| 103 | + |
| 104 | + setSelectedPins(selectedPins: IMapPin[]) { |
| 105 | + this.selectedPins = selectedPins; |
| 106 | + } |
| 107 | + |
| 108 | + setSelectedCases(selectedCases: any[]) { |
| 109 | + this.selectedCases = selectedCases; |
| 110 | + } |
| 111 | + |
| 112 | + async handleSelectionChange<T>( |
| 113 | + selectedItems: T[], dataContextName: string, collectionName: string, searchQueryFn: (item: T) => string |
| 114 | + ): Promise<void> { |
| 115 | + if (selectedItems.length === 0) { |
| 116 | + deleteSelectionList(dataContextName); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + for (const item of selectedItems) { |
| 121 | + const searchQuery = searchQueryFn(item); |
| 122 | + const result = await getCaseBySearch(dataContextName, collectionName, searchQuery); |
| 123 | + |
| 124 | + if (result.success) { |
| 125 | + const selectedItemIds = result.values.map((val: any) => val.id); |
| 126 | + if (selectedItems.length === 1) { |
| 127 | + createSelectionList(dataContextName, selectedItemIds); |
| 128 | + return; |
| 129 | + } else { |
| 130 | + const updateSelection = await updateSelectionList(dataContextName, selectedItemIds); |
| 131 | + if (!updateSelection.success) { |
| 132 | + createSelectionList(dataContextName, selectedItemIds); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +export async function initializeNeoPlugin() { |
| 141 | + initializePlugin({ pluginName: kPluginName, version: kVersion, dimensions: kInitialDimensions }); |
| 142 | + |
| 143 | + // Create the pin dataset |
| 144 | + await sendMessage("create", `dataContext`, { |
| 145 | + name: kPinDataContextName, |
| 146 | + collections: [ |
| 147 | + { |
| 148 | + name: "Map Pins", |
| 149 | + attrs: [ |
| 150 | + { name: kPinLatAttributeName, type: "numeric" }, |
| 151 | + { name: kPinLongAttributeName, type: "numeric" }, |
| 152 | + { name: kPinColorAttributeName, type: "color" } |
| 153 | + ] |
| 154 | + } |
| 155 | + ] |
| 156 | + }); |
| 157 | + |
| 158 | + // Create map if it doesn't exist |
| 159 | + await createOrUpdateMap("Map"); |
| 160 | + |
| 161 | + // See if there are any existing pins |
| 162 | + pluginState.updatePins(); |
| 163 | + |
| 164 | + // Set up a listener for changes to the pin dataset |
| 165 | + addDataContextChangeListener(kPinDataContextName, notification => { |
| 166 | + const { operation } = notification.values; |
| 167 | + |
| 168 | + if (["createCases", "deleteCases", "updateCases"].includes(operation)) { |
| 169 | + pluginState.updatePins(); |
| 170 | + } |
| 171 | + }); |
| 172 | + // Set up a listener for pin selection |
| 173 | + addDataContextChangeListener(kPinDataContextName, async notification => { |
| 174 | + const { operation, result } = notification.values; |
| 175 | + if (operation === "selectCases" && result.success) { |
| 176 | + const selectedPins = await getSelectionList(kPinDataContextName); |
| 177 | + const selectedPinValues: IMapPin[] = await Promise.all( |
| 178 | + selectedPins.values.map(async (pin: any) => { |
| 179 | + const pinItem = await getCaseByID(kPinDataContextName, pin.caseID); |
| 180 | + if (pinItem.success) { |
| 181 | + const pinValues = pinItem.values; |
| 182 | + const pinCase = (pinValues as any).case; |
| 183 | + return { |
| 184 | + id: pinCase.id, |
| 185 | + lat: pinCase.values.pinLat, |
| 186 | + long: pinCase.values.pinLong, |
| 187 | + color: pinCase.values.pinColor, |
| 188 | + }; |
| 189 | + } |
| 190 | + return null; |
| 191 | + }) |
| 192 | + ); |
| 193 | + pluginState.setSelectedPins(selectedPinValues); |
| 194 | + } |
| 195 | + }); |
| 196 | + |
| 197 | + // Set up a listener for case selection |
| 198 | + addDataContextChangeListener(kDataContextName, async notification => { |
| 199 | + const { operation, result } = notification.values; |
| 200 | + if (operation === "selectCases" && result.success) { |
| 201 | + const selectedCases = await getSelectionList(kDataContextName); |
| 202 | + const selectedPinCases = selectedCases.values |
| 203 | + .filter((sCase: any) => sCase.collectionName === kMapPinsCollectionName); |
| 204 | + const selectedCaseValues: any[] = await Promise.all( |
| 205 | + selectedPinCases.map(async (sCase: any) => { |
| 206 | + const caseItem = await getCaseByID(kDataContextName, sCase.caseID); |
| 207 | + if (caseItem.success) { |
| 208 | + const caseValues = caseItem.values; |
| 209 | + return { |
| 210 | + id: caseValues.id, |
| 211 | + label: caseValues.case.values.label, |
| 212 | + pinColor: caseValues.case.values.pinColor, |
| 213 | + }; |
| 214 | + } |
| 215 | + return null; |
| 216 | + }) |
| 217 | + ); |
| 218 | + pluginState.setSelectedCases(selectedCaseValues); |
| 219 | + } |
| 220 | + }); |
64 | 221 | } |
65 | 222 |
|
66 | 223 | export const pluginState = new PluginState(); |
0 commit comments