diff --git a/README.md b/README.md index d7ce49d689..b4d6bc4bd2 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ cd vuu #run the maven compile step mvn install #cd into vuu, child in repo -cd vuu +cd example/main #The server should now be started on your machine mvn exec:exec ``` diff --git a/example/main-java/pom.xml b/example/main-java/pom.xml new file mode 100644 index 0000000000..6f90144492 --- /dev/null +++ b/example/main-java/pom.xml @@ -0,0 +1,329 @@ + + + 4.0.0 + + org.finos.vuu + example + 0.9.36-SNAPSHOT + + + main-java + + + + + org.finos.vuu + vuu + 0.9.36-SNAPSHOT + + + + org.finos.vuu + basket + 0.9.36-SNAPSHOT + + + + org.finos.vuu + price + 0.9.36-SNAPSHOT + + + + org.finos.vuu + order + 0.9.36-SNAPSHOT + + + + org.finos.vuu + editable + 0.9.36-SNAPSHOT + + + + org.finos.vuu + permission + 0.9.36-SNAPSHOT + + + + org.scala-lang + scala-library + ${scala.version} + + + + org.scala-lang + scala-reflect + ${scala.version} + + + + junit + junit + 4.13.2 + test + + + + org.scalatest + scalatest_2.13 + ${scalatest.version} + test + + + org.scala-lang + scala-library + + + org.scala-lang + scala-reflect + + + + + + + + + + + sign-it + + + sign + true + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 3.0.1 + + + sign-artifacts + verify + + sign + + + + --pinentry-mode + loopback + + + + + + + + + + + legal-report + + + + org.scala-tools + maven-scala-plugin + ${maven.scala.plugin} + + + **/*.scala + + + + + + + + + + src/main/java + src/test/java + + + + + org.apache.maven.plugins + maven-source-plugin + + + + compile + + jar + + + + + + + org.apache.maven.plugins + maven-release-plugin + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + org.ow2.asm + asm + 6.2 + + + + + + + org.scala-tools + maven-scala-plugin + ${maven.scala.plugin} + + + + compile + testCompile + + + + + src/main/scala + src/test/scala + + -Xms64m + -Xmx1024m + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.2.0 + + + generate-sources + + add-source + + + + target/generated-sources/antlr4 + + + + + + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + + exec + + + + + java + + -Xmx10G + -classpath + + -Dvuu.webroot=../../vuu-ui/deployed_apps/app-vuu-example + -Dvuu.keyPath=src/main/resources/certs/key.pem + -Dvuu.certPath=src/main/resources/certs/cert.pem + -Dlogback.configurationFile=logback-netty.xml + org.finos.vuu.SimulMain + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.2 + + + + test-jar + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.7 + + + + + + + org.scalatest + scalatest-maven-plugin + 2.0.2 + + ${project.build.directory}/surefire-reports + . + test-reports.txt + + + + test + + test + + + + + + + + + + + + org.scala-tools + maven-scala-plugin + ${maven.scala.plugin} + + ${scala.version} + + + + + + \ No newline at end of file diff --git a/example/main-java/src/main/java/org/finos/vuu/VuuExampleMain.java b/example/main-java/src/main/java/org/finos/vuu/VuuExampleMain.java new file mode 100644 index 0000000000..ac4ad86ac8 --- /dev/null +++ b/example/main-java/src/main/java/org/finos/vuu/VuuExampleMain.java @@ -0,0 +1,91 @@ +package org.finos.vuu; + +import org.finos.toolbox.jmx.MetricsProvider; +import org.finos.toolbox.jmx.MetricsProviderImpl; +import org.finos.toolbox.lifecycle.LifecycleContainer; +import org.finos.toolbox.time.Clock; +import org.finos.toolbox.time.DefaultClock; +import org.finos.vuu.core.*; +import org.finos.vuu.core.module.TableDefContainer; +import org.finos.vuu.core.module.ViewServerModule; +import org.finos.vuu.core.module.authn.AuthNModule; +import org.finos.vuu.core.module.metrics.MetricsModule; +import org.finos.vuu.core.module.price.PriceModule; +import org.finos.vuu.core.module.simul.SimulationModule; +import org.finos.vuu.core.module.typeahead.TypeAheadModule; +import org.finos.vuu.core.module.vui.VuiStateModule; +import org.finos.vuu.module.MyExampleModule; +import org.finos.vuu.net.AlwaysHappyLoginValidator; +import org.finos.vuu.net.Authenticator; +import org.finos.vuu.net.LoggedInTokenValidator; +import org.finos.vuu.net.auth.AlwaysHappyAuthenticator; +import org.finos.vuu.net.http.VuuHttp2ServerOptions; +import org.finos.vuu.state.MemoryBackedVuiStateStore; +import org.finos.vuu.state.VuiStateStore; +import scala.Option; + +/** + * Example Java App using Vuu. + * + */ +public class VuuExampleMain +{ + /* + //to allow self signed certs + chrome://flags/#allow-insecure-localhost + */ + public static void main( String[] args ) + { + final MetricsProvider metrics = new MetricsProviderImpl(); + final Clock clock = new DefaultClock(); + final LifecycleContainer lifecycle = new LifecycleContainer(clock); + final TableDefContainer tableDefContainer = new TableDefContainer(); + + final VuiStateStore store = new MemoryBackedVuiStateStore(100); + + lifecycle.autoShutdownHook(); + + final Authenticator authenticator = new AlwaysHappyAuthenticator(); + final LoggedInTokenValidator loginTokenValidator = new LoggedInTokenValidator(); + + final String webRoot = "vuu-ui/deployed_apps/app-vuu-example"; + final String certPath = "example/main/src/main/resources/certs/cert.pem"; + final String keyPath = "example/main/src/main/resources/certs/key.pem"; + + final VuuServerConfig config = new VuuServerConfig( + VuuHttp2ServerOptions.apply() + .withWebRoot(webRoot) + //if we specify a web root, it means we will serve the files from a directory on the file system + //if we don't the files will be served from the vuu-ui jar directly. + //.withWebRoot(".") + .withSsl(certPath, keyPath) + .withDirectoryListings(true) + .withPort(8443), + VuuWebSocketOptions.apply() + .withUri("websocket") + .withWsPort(8090) + .withWss(certPath, keyPath, Option.apply(null)) + .withBindAddress("0.0.0.0"), + VuuSecurityOptions.apply() + .withAuthenticator(authenticator) + .withLoginValidator(new AlwaysHappyLoginValidator()), + VuuThreadingOptions.apply() + .withTreeThreads(4) + .withViewPortThreads(4), + new scala.collection.mutable.ListBuffer().toList() + ).withModule(PriceModule.apply(clock, lifecycle, tableDefContainer)) + .withModule(SimulationModule.apply(clock, lifecycle, tableDefContainer)) + .withModule(MetricsModule.apply(clock, lifecycle, metrics, tableDefContainer)) + .withModule(VuiStateModule.apply(store, clock, lifecycle, tableDefContainer)) + .withModule(TypeAheadModule.apply(clock, lifecycle, tableDefContainer)) + .withModule(AuthNModule.apply(authenticator, loginTokenValidator, clock, lifecycle, tableDefContainer)) + //the modules above are scala, the modules below are java... + .withModule(new MyExampleModule().create(tableDefContainer)) ; + + final VuuServer vuuServer = new VuuServer(config, lifecycle, clock, metrics); + + lifecycle.start(); + + vuuServer.join(); + } +} diff --git a/example/main-java/src/main/java/org/finos/vuu/module/MyExampleModule.java b/example/main-java/src/main/java/org/finos/vuu/module/MyExampleModule.java new file mode 100644 index 0000000000..188ac9ffac --- /dev/null +++ b/example/main-java/src/main/java/org/finos/vuu/module/MyExampleModule.java @@ -0,0 +1,30 @@ +package org.finos.vuu.module; + +import org.finos.vuu.api.TableDef; +import org.finos.vuu.core.module.DefaultModule; +import org.finos.vuu.core.module.ModuleFactory; +import org.finos.vuu.core.module.TableDefContainer; +import org.finos.vuu.core.module.ViewServerModule; +import org.finos.vuu.core.table.Columns; + +import java.util.ArrayList; + +import static java.util.Arrays.asList; +import static scala.jdk.javaapi.CollectionConverters.asScala; + +public class MyExampleModule extends DefaultModule { + + private final String NAME = "MY_MOD"; + + public ViewServerModule create(final TableDefContainer tableDefContainer){ + return ModuleFactory.withNamespace(NAME, tableDefContainer) + .addTable(TableDef.apply( + "myTable", + "id", + Columns.fromNames(asScala(asList("id:String", "foo:String", "myInt:Int")).toSeq()), + asScala(new ArrayList()).toSeq() + ), + (table, vs) -> new MyExampleProvider(table) + ).asModule(); + } +} diff --git a/example/main-java/src/main/java/org/finos/vuu/module/MyExampleProvider.java b/example/main-java/src/main/java/org/finos/vuu/module/MyExampleProvider.java new file mode 100644 index 0000000000..281140b477 --- /dev/null +++ b/example/main-java/src/main/java/org/finos/vuu/module/MyExampleProvider.java @@ -0,0 +1,48 @@ +package org.finos.vuu.module; + +import org.finos.vuu.core.table.DataTable; +import org.finos.vuu.provider.Provider; + +public class MyExampleProvider implements Provider { + + private final DataTable table; + + public MyExampleProvider(final DataTable table){ + this.table = table; + } + + @Override + public void doStart() { + + } + + @Override + public void doStop() { + + } + + @Override + public void doInitialize() { + + } + + @Override + public void doDestroy() { + + } + + @Override + public String lifecycleId() { + return null; + } + + @Override + public String toString() { + return Provider.super.toString(); + } + + @Override + public void subscribe(String key) { + + } +} diff --git a/example/main-java/src/main/resources/logback.xml b/example/main-java/src/main/resources/logback.xml new file mode 100644 index 0000000000..bb26d43142 --- /dev/null +++ b/example/main-java/src/main/resources/logback.xml @@ -0,0 +1,78 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/pom.xml b/example/pom.xml index ee65b256e5..fd808d4d1c 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -19,6 +19,7 @@ permission price basket + main-java diff --git a/layout-server/src/main/java/org/finos/vuu/layoutserver/dto/response/ApplicationLayoutDto.java b/layout-server/src/main/java/org/finos/vuu/layoutserver/dto/response/ApplicationLayoutDto.java new file mode 100644 index 0000000000..d04d48af50 --- /dev/null +++ b/layout-server/src/main/java/org/finos/vuu/layoutserver/dto/response/ApplicationLayoutDto.java @@ -0,0 +1,10 @@ +package org.finos.vuu.layoutserver.dto.response; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import lombok.Data; + +@Data +public class ApplicationLayoutDto { + private String username; + private ObjectNode definition; +} diff --git a/vuu-ui/packages/vuu-data-test/src/Table.ts b/vuu-ui/packages/vuu-data-test/src/Table.ts index 0c76ffdcd5..9b645b45f3 100644 --- a/vuu-ui/packages/vuu-data-test/src/Table.ts +++ b/vuu-ui/packages/vuu-data-test/src/Table.ts @@ -1,19 +1,28 @@ import { TableSchema } from "@finos/vuu-data"; import { VuuRowDataItemType } from "@finos/vuu-protocol-types"; -import { EventEmitter } from "@finos/vuu-utils"; +import { ColumnMap, EventEmitter } from "@finos/vuu-utils"; export type TableEvents = { delete: (row: VuuRowDataItemType[]) => void; insert: (row: VuuRowDataItemType[]) => void; + update: (row: VuuRowDataItemType[], columnName: string) => void; }; export class Table extends EventEmitter { #data: VuuRowDataItemType[][]; + #dataMap: ColumnMap; + #indexOfKey: number; #schema: TableSchema; - constructor(schema: TableSchema, data: VuuRowDataItemType[][]) { + constructor( + schema: TableSchema, + data: VuuRowDataItemType[][], + dataMap: ColumnMap + ) { super(); this.#data = data; + this.#dataMap = dataMap; this.#schema = schema; + this.#indexOfKey = dataMap[schema.key]; } get data() { @@ -24,4 +33,18 @@ export class Table extends EventEmitter { this.#data.push(row); this.emit("insert", row); } + + update(key: string, columnName: string, value: VuuRowDataItemType) { + const rowIndex = this.#data.findIndex( + (row) => row[this.#indexOfKey] === key + ); + const colIndex = this.#dataMap[columnName]; + if (rowIndex !== -1) { + const row = this.#data[rowIndex]; + const newRow = row.slice(); + newRow[colIndex] = value; + this.#data[rowIndex] = newRow; + this.emit("update", newRow, columnName); + } + } } diff --git a/vuu-ui/packages/vuu-data-test/src/TickingArrayDataSource.ts b/vuu-ui/packages/vuu-data-test/src/TickingArrayDataSource.ts index cd47eda97b..e939ed9ae0 100644 --- a/vuu-ui/packages/vuu-data-test/src/TickingArrayDataSource.ts +++ b/vuu-ui/packages/vuu-data-test/src/TickingArrayDataSource.ts @@ -15,6 +15,7 @@ import { VuuRange, VuuRowDataItemType, } from "@finos/vuu-protocol-types"; +import { metadataKeys } from "@finos/vuu-utils"; import { UpdateGenerator, UpdateHandler } from "./rowUpdates"; import { Table } from "./Table"; @@ -35,6 +36,7 @@ export interface TickingArrayDataSourceConstructorProps export class TickingArrayDataSource extends ArrayDataSource { #rpcServices: RpcService[] | undefined; #updateGenerator: UpdateGenerator | undefined; + #table?: Table; constructor({ data, @@ -54,11 +56,13 @@ export class TickingArrayDataSource extends ArrayDataSource { this._menu = menu; this.#rpcServices = rpcServices; this.#updateGenerator = updateGenerator; + this.#table = table; updateGenerator?.setDataSource(this); updateGenerator?.setUpdateHandler(this.processUpdates); if (table) { table.on("insert", this.insert); + table.on("update", this.update); } } @@ -139,6 +143,16 @@ export class TickingArrayDataSource extends ArrayDataSource { }, []); } + applyEdit( + row: DataSourceRow, + columnName: string, + value: VuuRowDataItemType + ): Promise { + const key = row[metadataKeys.KEY]; + this.#table?.update(key, columnName, value); + return Promise.resolve(true); + } + async menuRpcCall( rpcRequest: Omit | ClientToServerEditRpc ): Promise< diff --git a/vuu-ui/packages/vuu-data-test/src/basket/basket-module.ts b/vuu-ui/packages/vuu-data-test/src/basket/basket-module.ts index 4c06d6bb5b..d92ce20ce1 100644 --- a/vuu-ui/packages/vuu-data-test/src/basket/basket-module.ts +++ b/vuu-ui/packages/vuu-data-test/src/basket/basket-module.ts @@ -20,6 +20,18 @@ const buildDataColumnMap = (tableName: BasketsTableName) => {} ); +const tableMaps: Record = { + algoType: buildDataColumnMap("algoType"), + basket: buildDataColumnMap("basket"), + basketTrading: buildDataColumnMap("basketTrading"), + basketTradingConstituent: buildDataColumnMap("basketTradingConstituent"), + basketConstituent: buildDataColumnMap("basketConstituent"), + basketTradingConstituentJoin: buildDataColumnMap( + "basketTradingConstituentJoin" + ), + priceStrategyType: buildDataColumnMap("priceStrategyType"), +}; + //--------------- const { KEY } = metadataKeys; @@ -72,13 +84,18 @@ for (const row of sp500) { const basketConstituent = new Table( schemas.basketConstituent, - basketConstituentData + basketConstituentData, + tableMaps.basketConstituent ); /** * BasketTrading */ -const basketTrading = new Table(schemas.basketTrading, []); +const basketTrading = new Table( + schemas.basketTrading, + [], + tableMaps.basketTrading +); let basketIncrement = 1; /** @@ -86,26 +103,36 @@ let basketIncrement = 1; */ const basketTradingConstituent = new Table( schemas.basketTradingConstituent, - [] + [], + tableMaps.basketTradingConstituent ); const basketTradingConstituentJoin = new Table( schemas.basketTradingConstituentJoin, - [] + [], + tableMaps.basketTradingConstituentJoin ); +// export as convenience for showcase examples +export const createBasketTradingRow = ( + basketId: string, + basketName: string, + side = "BUY", + status = "OFF MARKET" +) => [ + basketId, + basketName, + 0, + 1.25, + `steve-${basketIncrement++}`, + side, + status, + 1_000_000, + 1_250_000, + 100, +]; + function createTradingBasket(basketId: string, basketName: string) { - const instanceId = `steve-${basketIncrement++}`; - const basketTradingRow = [ - basketId, - basketName, - 0, - 1.25, - instanceId, - "OFF MARKET", - 1_000_000, - 1_250_000, - 100, - ]; + const basketTradingRow = createBasketTradingRow(basketId, basketName); basketTrading.insert(basketTradingRow); @@ -126,13 +153,15 @@ function createTradingBasket(basketId: string, basketName: string) { const side = "BUY"; const venue = "venue"; + const { instanceId } = tableMaps.basketTrading; + const basketInstanceId = basketTradingRow[instanceId]; const basketTradingConstituentRow: VuuRowDataItemType[] = [ algo, algoParams, basketId, description, - instanceId, - `${instanceId}-${ric}`, + basketInstanceId, + `${basketInstanceId}-${ric}`, limitPrice, notionalLocal, notionalUsd, @@ -168,8 +197,8 @@ function createTradingBasket(basketId: string, basketName: string) { bidSize, close, description, - instanceId, - `${instanceId}-${ric}`, + basketInstanceId, + `${basketInstanceId}-${ric}`, last, limitPrice, notionalLocal, @@ -202,42 +231,42 @@ async function createNewBasket(rpcRequest: any) { //------------------- -const tableMaps: Record = { - algoType: buildDataColumnMap("algoType"), - basket: buildDataColumnMap("basket"), - basketTrading: buildDataColumnMap("basketTrading"), - basketTradingConstituent: buildDataColumnMap("basketTradingConstituent"), - basketConstituent: buildDataColumnMap("basketConstituent"), - basketTradingConstituentJoin: buildDataColumnMap( - "basketTradingConstituentJoin" - ), - priceStrategyType: buildDataColumnMap("priceStrategyType"), -}; - export const tables: Record = { - algoType: new Table(schemas.algoType, [ - ["Sniper", 0], - ["Dark Liquidity", 1], - ["VWAP", 2], - ["POV", 3], - ["Dynamic Close", 4], - ]), - basket: new Table(schemas.basket, [ - [".NASDAQ100", ".NASDAQ100", 0, 0], - [".HSI", ".HSI", 0, 0], - [".FTSE100", ".FTSE100", 0, 0], - [".SP500", ".SP500", 0, 0], - ]), + algoType: new Table( + schemas.algoType, + [ + ["Sniper", 0], + ["Dark Liquidity", 1], + ["VWAP", 2], + ["POV", 3], + ["Dynamic Close", 4], + ], + tableMaps.algoType + ), + basket: new Table( + schemas.basket, + [ + [".NASDAQ100", ".NASDAQ100", 0, 0], + [".HSI", ".HSI", 0, 0], + [".FTSE100", ".FTSE100", 0, 0], + [".SP500", ".SP500", 0, 0], + ], + tableMaps.basket + ), basketConstituent, basketTrading, basketTradingConstituent, basketTradingConstituentJoin, - priceStrategyType: new Table(schemas.priceStrategyType, [ - ["Peg to Near Touch", 0], - ["Far Touch", 1], - ["Limit", 2], - ["Algo", 3], - ]), + priceStrategyType: new Table( + schemas.priceStrategyType, + [ + ["Peg to Near Touch", 0], + ["Far Touch", 1], + ["Limit", 2], + ["Algo", 3], + ], + tableMaps.priceStrategyType + ), }; const menus: Record = { diff --git a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basket-generator.ts b/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basket-generator.ts deleted file mode 100644 index 34aa03b1b8..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basket-generator.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { BasketColumnMap, BasketReferenceData } from "../reference-data"; -import { getGenerators } from "../../generatorTemplate"; - -const [rowGenerator] = getGenerators( - "basket", - BasketColumnMap, - BasketReferenceData -); - -export default rowGenerator; diff --git a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basketConstituent-generator.ts b/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basketConstituent-generator.ts deleted file mode 100644 index 2793139783..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basketConstituent-generator.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { - BasketConstituentColumnMap, - BasketConstituentReferenceData, -} from "../reference-data"; -import { getGenerators } from "../../generatorTemplate"; - -const [rowGenerator] = getGenerators( - "basketConstituent", - BasketConstituentColumnMap, - BasketConstituentReferenceData -); - -export default rowGenerator; diff --git a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basketTrading-generator.ts b/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basketTrading-generator.ts deleted file mode 100644 index cbd01998e6..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basketTrading-generator.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { - BasketTradingColumnMap, - BasketTradingReferenceData, -} from "../reference-data"; -import { getGenerators } from "../../generatorTemplate"; - -const [rowGenerator] = getGenerators( - "basketTrading", - BasketTradingColumnMap, - BasketTradingReferenceData -); - -export default rowGenerator; diff --git a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basketTradingConstituent-generator.ts b/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basketTradingConstituent-generator.ts deleted file mode 100644 index 3c8391fd39..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/basketTradingConstituent-generator.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { - BasketTradingConstituentColumnMap, - BasketTradingConstituentReferenceData, -} from "../reference-data"; -import { getGenerators } from "../../generatorTemplate"; - -const [rowGenerator] = getGenerators( - "basketTradingConstituent", - BasketTradingConstituentColumnMap, - BasketTradingConstituentReferenceData -); - -export default rowGenerator; diff --git a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/index.ts b/vuu-ui/packages/vuu-data-test/src/basket/data-generators/index.ts deleted file mode 100644 index 5933c46ae5..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/basket/data-generators/index.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { RowGeneratorFactory } from "../.."; -import { BasketsTableName } from "../basket-schemas"; -import basketGenerators from "./basket-generator"; -import basketConstituentGenerators from "./basketConstituent-generator"; -import basketTradingGenerators from "./basketTrading-generator"; -import basketTradingConstituentGenerators from "./basketTradingConstituent-generator"; - -const generators: Record = { - algoType: undefined, - basket: basketGenerators, - basketConstituent: basketConstituentGenerators, - basketTrading: basketTradingGenerators, - basketTradingConstituent: basketTradingConstituentGenerators, - basketTradingConstituentJoin: basketTradingConstituentGenerators, - priceStrategyType: undefined, -}; - -export default generators; diff --git a/vuu-ui/packages/vuu-data-test/src/basket/reference-data/basketTrading.ts b/vuu-ui/packages/vuu-data-test/src/basket/reference-data/basketTrading.ts deleted file mode 100644 index a3a14b0bf8..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/basket/reference-data/basketTrading.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { VuuDataRow } from "@finos/vuu-protocol-types"; -import { ColumnMap } from "@finos/vuu-utils"; -import { getSchema } from "../../schemas"; - -import baskets, { BasketColumnMap } from "./basket"; -import basketConstituents from "./basketConstituent"; - -const schema = getSchema("basketTrading"); - -export const BasketTradingColumnMap = Object.values( - schema.columns -).reduce((map, col, index) => { - map[col.name] = index; - return map; -}, {}); - -let instance = 1; - -const data: VuuDataRow[] = []; - -const createBasket = (basketId: string, basketName: string) => { - const key = BasketColumnMap.basketId; - const basketRow = baskets.find((basket) => basket[key] === basketId); - const basketTradingRow = [ - basketId, - basketName, - 0, - 0, - `steve-${instance++}`, - "OFF-MARKET", - 0, - 0, - 0, - ]; - data.push(basketTradingRow); -}; - -// createBasket(".FTSE", "Steve FTSE 1"); -// createBasket(".FTSE", "Steve FTSE 2"); -// createBasket(".FTSE", "Steve FTSE 3"); -// createBasket(".FTSE", "Steve FTSE 4"); -// createBasket(".FTSE", "Steve FTSE 5"); -// createBasket(".FTSE", "Steve FTSE 6"); - -export default data; diff --git a/vuu-ui/packages/vuu-data-test/src/basket/reference-data/basketTradingConstituent.ts b/vuu-ui/packages/vuu-data-test/src/basket/reference-data/basketTradingConstituent.ts deleted file mode 100644 index 01e3471ba6..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/basket/reference-data/basketTradingConstituent.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { VuuDataRow } from "@finos/vuu-protocol-types"; -import { ColumnMap } from "@finos/vuu-utils"; -import { getSchema } from "../../schemas"; - -const schema = getSchema("basketTradingConstituent"); - -export const BasketTradingConstituentColumnMap = Object.values( - schema.columns -).reduce((map, col, index) => { - map[col.name] = index; - return map; -}, {}); - -const data: VuuDataRow[] = []; - -export default data; diff --git a/vuu-ui/packages/vuu-data-test/src/basket/reference-data/index.ts b/vuu-ui/packages/vuu-data-test/src/basket/reference-data/index.ts index 4ece82ad21..b430b42400 100644 --- a/vuu-ui/packages/vuu-data-test/src/basket/reference-data/index.ts +++ b/vuu-ui/packages/vuu-data-test/src/basket/reference-data/index.ts @@ -3,11 +3,3 @@ export { default as BasketConstituentReferenceData, BasketConstituentColumnMap, } from "./basketConstituent"; -export { - default as BasketTradingReferenceData, - BasketTradingColumnMap, -} from "./basketTrading"; -export { - default as BasketTradingConstituentReferenceData, - BasketTradingConstituentColumnMap, -} from "./basketTradingConstituent"; diff --git a/vuu-ui/packages/vuu-data-test/src/index.ts b/vuu-ui/packages/vuu-data-test/src/index.ts index a702e7142e..5acf00475e 100644 --- a/vuu-ui/packages/vuu-data-test/src/index.ts +++ b/vuu-ui/packages/vuu-data-test/src/index.ts @@ -4,4 +4,5 @@ export * from "./TickingArrayDataSource"; export * from "./vuu-row-generator"; export * from "./vuu-modules"; export { type BasketsTableName } from "./basket/basket-schemas"; +export { createBasketTradingRow } from "./basket/basket-module"; export { type SimulTableName } from "./simul/simul-schemas"; diff --git a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/child-order-generator.ts b/vuu-ui/packages/vuu-data-test/src/simul/data-generators/child-order-generator.ts deleted file mode 100644 index d1af767b5f..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/child-order-generator.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { ColumnDescriptor } from "@finos/vuu-datagrid-types"; -import { - ColumnGeneratorFn, - RowGeneratorFactory, -} from "../../vuu-row-generator"; -import { getSchema } from "../../index"; -import { currencies, locations, suffixes } from "./generatedData"; - -function random(min: number, max: number) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min + 1)) + min; -} - -const accounts = [ - "Account 1", - "Account 2", - "Account 3", - "Account 4", - "Account 5", -]; - -const strategies = [ - "Strategy 1", - "Strategy 2", - "Strategy 3", - "Strategy 4", - "Strategy 5", -]; -const algos = ["Algo 1", "Algo 2", "Algo 3", "Algo 4", "Algo 5"]; - -const maxIndex = 20 * 20 * 20 * 20 * 8; - -export const RowGenerator: RowGeneratorFactory = () => (index: number) => { - if (index > maxIndex) { - throw Error("generateRow index val is too high"); - } - - const suffix = suffixes[random(0, suffixes.length - 1)]; - - const account = accounts[random(0, 4)]; - const averagePrice = 0; - const ccy = currencies[random(0, 4)]; - const exchange = locations[suffix][1]; - const filledQty = 0; - const id = `${index}`; - const idAsInt = index; - const lastUpdate = Date.now(); - const openQty = 0; - const parentOrderId = 0; - const price = 0; - const quantity = 0; - const ric = "AAA.L"; - const side = "buy"; - const status = "active"; - const strategy = strategies[random(0, strategies.length - 1)]; - const volLimit = 10_000; - - return [ - account, - averagePrice, - ccy, - exchange, - filledQty, - id, - idAsInt, - lastUpdate, - openQty, - parentOrderId, - price, - quantity, - ric, - side, - status, - strategy, - volLimit, - ]; -}; - -export const ColumnGenerator: ColumnGeneratorFn = (columns = []) => { - const schema = getSchema("childOrders"); - const schemaColumns: ColumnDescriptor[] = schema.columns; - if (typeof columns === "number") { - throw Error("ChildOrderColumnGenerator must be passed columns (strings)"); - } else if (columns.length === 0) { - return schemaColumns; - } else { - // TODO return just requested columns and apply extended config - return schemaColumns; - } -}; diff --git a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/generate-data-utils.ts b/vuu-ui/packages/vuu-data-test/src/simul/data-generators/generate-data-utils.ts deleted file mode 100644 index 0cf067d084..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/generate-data-utils.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { faker } from "@faker-js/faker"; -import { VuuRowDataItemType } from "@finos/vuu-protocol-types"; - -export function createArray(numofrows: number): VuuRowDataItemType[][] { - const result = []; - - for (let i = 0; i < numofrows; i++) { - const FakerDataGenerator = [ - faker.company.name(), - faker.finance.currencyCode(), - Number(faker.finance.amount({ min: 5, max: 10, dec: 2 })), - faker.finance.amount({ min: 100, max: 2000, dec: 0 }), - faker.finance.transactionType(), - faker.finance.transactionDescription(), - faker.date.anytime().getMilliseconds(), - faker.finance.accountName(), - faker.finance.accountNumber(), - faker.commerce.department(), - faker.commerce.product(), - faker.finance.amount({ min: 5, max: 10, dec: 2 }), - faker.finance.amount({ min: 5, max: 10, dec: 2 }), - faker.finance.amount({ min: 5, max: 10, dec: 2 }), - faker.finance.amount({ min: 5, max: 10, dec: 2 }), - faker.finance.amount({ min: 5, max: 10, dec: 2 }), - faker.finance.amount({ min: 5, max: 10, dec: 2 }), - faker.finance.amount({ min: 5, max: 10, dec: 2 }), - faker.finance.amount({ min: 5, max: 10, dec: 2 }), - faker.finance.amount({ min: 5, max: 10, dec: 2 }), - ]; - result.push([ - i + 1, - FakerDataGenerator[0], - FakerDataGenerator[1], - Number(FakerDataGenerator[2]), - FakerDataGenerator[3] as number, - Number( - Math.floor( - Number(FakerDataGenerator[2]) * Number(FakerDataGenerator[3]) - ) - ), - FakerDataGenerator[4], - FakerDataGenerator[5], - FakerDataGenerator[6], - FakerDataGenerator[7], - FakerDataGenerator[8], - FakerDataGenerator[9], - FakerDataGenerator[10], - FakerDataGenerator[11], - FakerDataGenerator[12], - FakerDataGenerator[13], - FakerDataGenerator[14], - FakerDataGenerator[15], - FakerDataGenerator[16], - FakerDataGenerator[17], - Number(FakerDataGenerator[18]), - ]); - } - - return result; -} diff --git a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/generatedData.ts b/vuu-ui/packages/vuu-data-test/src/simul/data-generators/generatedData.ts deleted file mode 100644 index 78809fc233..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/generatedData.ts +++ /dev/null @@ -1,12 +0,0 @@ -export const locations: { [key: string]: string[] } = { - L: ["London PLC", "XLON/LSE-SETS"], - N: ["Corporation", "XNGS/NAS-GSM"], - AS: ["B.V.", "XAMS/ENA-MAIN"], - OQ: ["Co.", "XNYS/NYS-MAIN"], - PA: ["Paris", "PAR/EUR_FR"], - MI: ["Milan", "MIL/EUR_IT"], - FR: ["Frankfurt", "FR/EUR_DE"], - AT: ["Athens", "AT/EUR_GR"], -}; -export const suffixes = ["L", "N", "OQ", "AS", "PA", "MI", "FR", "AT"]; -export const currencies = ["CAD", "GBX", "USD", "EUR", "GBP"]; diff --git a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/index.ts b/vuu-ui/packages/vuu-data-test/src/simul/data-generators/index.ts deleted file mode 100644 index b378012d47..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -export * as childOrders from "./child-order-generator"; -export * as instruments from "./instrument-generator"; -export * as instrumentPrices from "./instrument-prices-generator"; -export * as orders from "./order-generator"; -export * as parentOrders from "./parent-order-generator"; -export * as prices from "./prices-generator"; diff --git a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/instrument-generator.ts b/vuu-ui/packages/vuu-data-test/src/simul/data-generators/instrument-generator.ts deleted file mode 100644 index 609c7eda5b..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/instrument-generator.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { ColumnDescriptor } from "@finos/vuu-datagrid-types"; -import { - ColumnGeneratorFn, - RowGeneratorFactory, -} from "../../vuu-row-generator"; -import { getSchema } from "../../index"; -import { - InstrumentReferenceData, - InstrumentColumnMap, -} from "../reference-data"; -import { getCalculatedColumnType, isCalculatedColumn } from "@finos/vuu-utils"; - -export type ExtendedColumnConfig = { [key: string]: Partial }; - -export const RowGenerator: RowGeneratorFactory = - (columnNames?: string[]) => (index: number) => { - if (index >= InstrumentReferenceData.length) { - throw Error("generateRow index val is too high"); - } - if (columnNames) { - return columnNames.map( - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - (name) => InstrumentReferenceData[index][InstrumentColumnMap[name]] - ); - } else { - return InstrumentReferenceData[index].slice(0, 7); - } - }; - -export const ColumnGenerator: ColumnGeneratorFn = ( - columns = [], - columnConfig: ExtendedColumnConfig = {} -) => { - const schema = getSchema("instruments"); - const instrumentColumns: ColumnDescriptor[] = schema.columns; - if (typeof columns === "number") { - throw Error("InstrumentColumnGenerator must be passed columns (strings)"); - } else if (columns.length === 0) { - return instrumentColumns.map((column) => ({ - ...column, - ...columnConfig[column.name], - })); - } else { - return columns.map((name) => { - const column = instrumentColumns.find((col) => col.name === name); - if (column) { - return { - ...column, - ...columnConfig[column.name], - }; - } else if (isCalculatedColumn(name)) { - return { - name, - serverDataType: getCalculatedColumnType({ name }), - } as ColumnDescriptor; - } else { - throw Error(`InstrumentColumnGenerator no column ${name}`); - } - }); - } -}; diff --git a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/instrument-prices-generator.ts b/vuu-ui/packages/vuu-data-test/src/simul/data-generators/instrument-prices-generator.ts deleted file mode 100644 index 3059b73ccc..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/instrument-prices-generator.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { ColumnDescriptor } from "@finos/vuu-datagrid-types"; -import { buildColumnMap } from "@finos/vuu-utils/src"; -import { - InstrumentPricesColumnMap, - InstrumentPricesReferenceData, -} from "../reference-data"; -import { BaseUpdateGenerator } from "../../UpdateGenerator"; -import { getSchema } from "../../index"; -import { - ColumnGeneratorFn, - RowGeneratorFactory, -} from "../../vuu-row-generator"; - -const instrumentPriceSchema = getSchema("instrumentPrices"); - -export const RowGenerator: RowGeneratorFactory = - (columnNames?: string[]) => (index: number) => { - if (index >= InstrumentPricesReferenceData.length) { - throw Error("generateRow index val is too high"); - } - if (columnNames) { - return columnNames.map( - (name) => - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - InstrumentPricesReferenceData[index][InstrumentPricesColumnMap[name]] - ); - } else { - return InstrumentPricesReferenceData[index].slice(0, 7); - } - }; - -const { bid, bidSize, ask, askSize } = buildColumnMap( - instrumentPriceSchema.columns -); - -export const createUpdateGenerator = () => - new BaseUpdateGenerator([bid, bidSize, ask, askSize]); - -export const ColumnGenerator: ColumnGeneratorFn = ( - columns = [] - //columnConfig: ExtendedColumnConfig = {} -) => { - const instrumentPriceColumns: ColumnDescriptor[] = - instrumentPriceSchema.columns; - if (typeof columns === "number") { - throw Error( - "InstrumentPricesColumnGenerator must be passed columns (strings)" - ); - } else if (columns.length === 0) { - return instrumentPriceColumns; - } else { - return columns.map((name) => { - const column = instrumentPriceColumns.find((col) => col.name === name); - if (column) { - return column; - } else { - throw Error(`InstrumentPricesColumnGenerator no column ${name}`); - } - }); - } -}; diff --git a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/order-generator.ts b/vuu-ui/packages/vuu-data-test/src/simul/data-generators/order-generator.ts deleted file mode 100644 index df7389829e..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/order-generator.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { ColumnDescriptor } from "@finos/vuu-datagrid-types"; -import { - ColumnGeneratorFn, - RowGeneratorFactory, -} from "../../vuu-row-generator"; -import { getSchema } from "../../index"; - -export type ExtendedColumnConfig = { [key: string]: Partial }; - -function random(min: number, max: number) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min + 1)) + min; -} - -const chars = Array.from("ABCDEFGHIJKLMNOPQRST"); -const suffixes = ["L", "N", "OQ", "AS", "PA", "MI", "FR", "AT"]; -const currencies = ["CAD", "GBX", "USD", "EUR", "GBP"]; -const sides = ["buy", "sell", "buy", "sell", "short"]; -const traders = ["Arkwright", "Enfield", "Bailey", "Cui", "Kohl"]; - -/* - each top level loop (20 x 'A...') has 64,000 iterations of nested loops, - so divide index by 64000 to get index of first character - - remainder is our index into next level of loops - each second level loop ( 20 x 'A...') has, 3,200 iterations, so divide remainder by - 3,200 to get index of second character - - each third level loop (20 x 'A...') has 160 iterations - -*/ - -const maxIndex = 20 * 20 * 20 * 20 * 8; - -export const RowGenerator: RowGeneratorFactory = () => (index: number) => { - if (index > maxIndex) { - throw Error("generateRow index val is too high"); - } - const index1 = Math.floor(index / 64000); - const remainder1 = index % 64000; - - const index2 = Math.floor(remainder1 / 3200); - const remainder2 = remainder1 % 3200; - - const index3 = Math.floor(remainder2 / 160); - const remainder3 = remainder2 % 160; - - const index4 = Math.floor(remainder3 / 8); - const remainder4 = remainder3 % 8; - - const suffix = suffixes[remainder4]; - - const ccy = currencies[random(0, 4)]; - const created = 0; - const filledQuantity = 0; - const lastUpdate = 0; - const orderId = "1"; - const quantity = 0; - const ric = `${chars[index1]}${chars[index2]}${chars[index3]}${chars[index4]}.${suffix}`; - const side = sides[random(0, 4)]; - const trader = traders[random(0, 4)]; - - return [ - ccy, - created, - filledQuantity, - lastUpdate, - orderId, - quantity, - ric, - side, - trader, - ]; -}; - -export const ColumnGenerator: ColumnGeneratorFn = ( - columns = [], - columnConfig: ExtendedColumnConfig = {} -) => { - console.log({ columnConfig }); - const schema = getSchema("orders"); - const instrumentColumns: ColumnDescriptor[] = schema.columns; - if (typeof columns === "number") { - throw Error("OrderColumnGenerator must be passed columns (strings)"); - } else if (columns.length === 0) { - return instrumentColumns; - } else { - // TODO return just requested columns and apply extended config - return instrumentColumns; - } -}; diff --git a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/parent-order-generator.ts b/vuu-ui/packages/vuu-data-test/src/simul/data-generators/parent-order-generator.ts deleted file mode 100644 index 78c773c1c7..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/parent-order-generator.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { ColumnDescriptor } from "@finos/vuu-datagrid-types"; -import { - ColumnGeneratorFn, - RowGeneratorFactory, -} from "../../vuu-row-generator"; -import { getSchema } from "../../index"; -import { currencies, locations, suffixes } from "./generatedData"; - -function random(min: number, max: number) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min + 1)) + min; -} - -const accounts = [ - "Account 1", - "Account 2", - "Account 3", - "Account 4", - "Account 5", -]; -const algos = ["Algo 1", "Algo 2", "Algo 3", "Algo 4", "Algo 5"]; - -const maxIndex = 20 * 20 * 20 * 20 * 8; - -export const RowGenerator: RowGeneratorFactory = () => (index: number) => { - if (index > maxIndex) { - throw Error("generateRow index val is too high"); - } - - const suffix = suffixes[random(0, suffixes.length - 1)]; - - const account = accounts[random(0, 4)]; - const algo = algos[random(0, 4)]; - const avgPrice = 0; - const ccy = currencies[random(0, 4)]; - const childCount = 0; - const exchange = locations[suffix][1]; - const filledQty = 0; - const id = `${index}`; - const idAsInt = index; - const lastUpdate = Date.now(); - const openQty = 0; - const price = 0; - const quantity = 0; - const ric = "AAA.L"; - const side = "buy"; - const status = "active"; - const volLimit = 10_000; - - return [ - account, - algo, - avgPrice, - ccy, - childCount, - exchange, - filledQty, - id, - idAsInt, - lastUpdate, - openQty, - price, - quantity, - ric, - side, - status, - volLimit, - ]; -}; - -export const ColumnGenerator: ColumnGeneratorFn = (columns = []) => { - const schema = getSchema("parentOrders"); - const schemaColumns: ColumnDescriptor[] = schema.columns; - if (typeof columns === "number") { - throw Error("ParentOrderColumnGenerator must be passed columns (strings)"); - } else if (columns.length === 0) { - return schemaColumns; - } else { - // TODO return just requested columns and apply extended config - return schemaColumns; - } -}; diff --git a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/prices-generator.ts b/vuu-ui/packages/vuu-data-test/src/simul/data-generators/prices-generator.ts deleted file mode 100644 index 223cbf01e4..0000000000 --- a/vuu-ui/packages/vuu-data-test/src/simul/data-generators/prices-generator.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { ColumnDescriptor } from "@finos/vuu-datagrid-types"; -import { buildColumnMap } from "@finos/vuu-utils"; -import { PriceReferenceData } from "../reference-data"; -import { - ColumnGeneratorFn, - RowGeneratorFactory, -} from "../../vuu-row-generator"; -import { BaseUpdateGenerator } from "../../UpdateGenerator"; -import { getAllSchemas } from "../../index"; - -export const RowGenerator: RowGeneratorFactory = () => (index: number) => { - if (index >= PriceReferenceData.length) { - throw Error("generateRow index val is too high"); - } - - return PriceReferenceData[index]; -}; - -const schemas = getAllSchemas(); -const { prices: pricesSchema } = schemas; -const { bid, bidSize, ask, askSize } = buildColumnMap(pricesSchema.columns); -const tickingColumns = [bid, bidSize, ask, askSize]; -export const createUpdateGenerator = () => - new BaseUpdateGenerator(tickingColumns); - -export const ColumnGenerator: ColumnGeneratorFn = (columns = []) => { - const schemaColumns: ColumnDescriptor[] = pricesSchema.columns; - if (typeof columns === "number") { - throw Error("PricesColumnGenerator must be passed columns (strings)"); - } else if (columns.length === 0) { - return schemaColumns; - } else { - // TODO return just requested columns and apply extended config - return schemaColumns; - } -}; diff --git a/vuu-ui/packages/vuu-data-test/src/vuu-row-generator.ts b/vuu-ui/packages/vuu-data-test/src/vuu-row-generator.ts index 3468f64c1d..91615e1d00 100644 --- a/vuu-ui/packages/vuu-data-test/src/vuu-row-generator.ts +++ b/vuu-ui/packages/vuu-data-test/src/vuu-row-generator.ts @@ -1,7 +1,5 @@ import { ColumnDescriptor } from "@finos/vuu-datagrid-types"; import { VuuRowDataItemType, VuuTable } from "@finos/vuu-protocol-types"; -import * as simulDataGenerators from "./simul/data-generators"; -import * as basketDataGenerators from "./basket/data-generators"; import { UpdateGenerator } from "./rowUpdates"; type RowAtIndexFunc = (index: number) => T | undefined; diff --git a/vuu-ui/packages/vuu-data/src/array-data-source/array-data-source.ts b/vuu-ui/packages/vuu-data/src/array-data-source/array-data-source.ts index 839ea1821a..4bf28e9930 100644 --- a/vuu-ui/packages/vuu-data/src/array-data-source/array-data-source.ts +++ b/vuu-ui/packages/vuu-data/src/array-data-source/array-data-source.ts @@ -20,6 +20,7 @@ import { getMissingItems, KeySet, logger, + metadataKeys, NULL_RANGE, rangeNewItems, resetRange, @@ -97,12 +98,9 @@ export class ArrayDataSource private dataIndices: number[] | undefined; /** Map reflecting positions of data items in raw data */ private dataMap: ColumnMap | undefined; - private disabled = false; - private groupedData: undefined | DataSourceRow[]; private groupMap: undefined | GroupMap; /** the index of key field within raw data row */ private key: number; - private suspended = false; private tableSchema: TableSchema; private lastRangeServed: VuuRange = { from: 0, to: 0 }; private rangeChangeRowset: "delta" | "full"; @@ -440,6 +438,24 @@ export class ArrayDataSource } }; + protected update = (row: VuuRowDataItemType[], columnName: string) => { + // TODO take sorting, filtering. grouping into account + const keyValue = row[this.key]; + const { KEY } = metadataKeys; + const colIndex = this.#columnMap[columnName]; + const dataColIndex = this.dataMap?.[columnName]; + const dataIndex = this.#data.findIndex((row) => row[KEY] === keyValue); + if (dataIndex !== -1 && dataColIndex !== undefined) { + const dataSourceRow = this.#data[dataIndex]; + dataSourceRow[colIndex] = row[dataColIndex]; + const { from, to } = this.#range; + const [rowIdx] = dataSourceRow; + if (rowIdx >= from && rowIdx < to) { + this.sendRowsToClient(true); + } + } + }; + private setRange(range: VuuRange, forceFullRefresh = false) { this.#range = range; this.keys.reset(range); diff --git a/vuu-ui/packages/vuu-data/src/data-source.ts b/vuu-ui/packages/vuu-data/src/data-source.ts index f04af2e0fe..d026a1f87e 100644 --- a/vuu-ui/packages/vuu-data/src/data-source.ts +++ b/vuu-ui/packages/vuu-data/src/data-source.ts @@ -8,8 +8,8 @@ import { ClientToServerMenuRPC, LinkDescriptorWithLabel, VuuAggregation, - VuuColumnDataType, VuuColumns, + VuuDataRowDto, VuuFilter, VuuGroupBy, VuuLinkDescriptor, @@ -485,6 +485,12 @@ export type DataSourceEditHandler = ( value: VuuRowDataItemType ) => Promise; +export type DataSourceDeleteHandler = (key: string) => Promise; +export type DataSourceInsertHandler = ( + key: string, + data: VuuDataRowDto +) => Promise; + export type RpcResponse = | MenuRpcResponse | VuuUIMessageInRPCEditReject @@ -525,6 +531,9 @@ export interface DataSource extends EventEmitter { */ suspend?: () => void; resume?: () => void; + + deleteRow?: DataSourceDeleteHandler; + /** * For a dataSource that has been previously disabled and is currently in disabled state , this will restore * the subscription to active status. Fresh data will be dispatched to client. The enable call optionally @@ -540,6 +549,7 @@ export interface DataSource extends EventEmitter { disable?: () => void; filter: DataSourceFilter; groupBy: VuuGroupBy; + insertRow?: DataSourceInsertHandler; links?: LinkDescriptorWithLabel[]; menu?: VuuMenu; menuRpcCall: ( diff --git a/vuu-ui/packages/vuu-data/src/inlined-worker.js b/vuu-ui/packages/vuu-data/src/inlined-worker.js index 5441fd9630..b0715dd93d 100644 --- a/vuu-ui/packages/vuu-data/src/inlined-worker.js +++ b/vuu-ui/packages/vuu-data/src/inlined-worker.js @@ -1876,13 +1876,11 @@ var ServerProxy = class { this.sendIfReady(request, requestId, viewport.status === "subscribed"); } disableViewport(viewport) { - console.log("disable viewport"); const requestId = nextRequestId(); const request = viewport.disable(requestId); this.sendIfReady(request, requestId, viewport.status === "subscribed"); } enableViewport(viewport) { - console.log("enable viewport"); if (viewport.disabled) { const requestId = nextRequestId(); const request = viewport.enable(requestId); diff --git a/vuu-ui/packages/vuu-data/src/remote-data-source.ts b/vuu-ui/packages/vuu-data/src/remote-data-source.ts index 52ee469806..8a34ca3846 100644 --- a/vuu-ui/packages/vuu-data/src/remote-data-source.ts +++ b/vuu-ui/packages/vuu-data/src/remote-data-source.ts @@ -5,6 +5,7 @@ import { ClientToServerMenuRPC, LinkDescriptorWithLabel, VuuAggregation, + VuuDataRowDto, VuuGroupBy, VuuMenu, VuuRange, @@ -457,6 +458,7 @@ export class RemoteDataSource } set columns(columns: string[]) { + console.log(`set columns ${columns.join(",")}`); this.#config = { ...this.#config, columns, @@ -662,4 +664,33 @@ export class RemoteDataSource } }); } + + insertRow(key: string, data: VuuDataRowDto) { + console.log("RemoteDataSource insertRow ${key}", { + data, + }); + return this.menuRpcCall({ + rowKey: key, + data, + type: "VP_EDIT_ADD_ROW_RPC", + }).then((response) => { + if (response?.error) { + return response.error; + } else { + return true; + } + }); + } + deleteRow(rowKey: string) { + return this.menuRpcCall({ + rowKey, + type: "VP_EDIT_DELETE_ROW_RPC", + }).then((response) => { + if (response?.error) { + return response.error; + } else { + return true; + } + }); + } } diff --git a/vuu-ui/packages/vuu-data/src/worker.ts b/vuu-ui/packages/vuu-data/src/worker.ts index 881746c681..0bab408d78 100644 --- a/vuu-ui/packages/vuu-data/src/worker.ts +++ b/vuu-ui/packages/vuu-data/src/worker.ts @@ -37,7 +37,7 @@ async function connectToServer( //TODO do we need to listen in to the connection messages here so we can lock back in, in the event of a reconnenct ? (msg) => { if (isConnectionQualityMetrics(msg)) { - console.log("post connection metrics"); + // console.log("post connection metrics"); postMessage({ type: "connection-metrics", messages: msg }); } else if (isConnectionStatusMessage(msg)) { onConnectionStatusChange(msg); diff --git a/vuu-ui/packages/vuu-datagrid-types/index.d.ts b/vuu-ui/packages/vuu-datagrid-types/index.d.ts index 630f6dc0fe..7cd0b2df24 100644 --- a/vuu-ui/packages/vuu-datagrid-types/index.d.ts +++ b/vuu-ui/packages/vuu-datagrid-types/index.d.ts @@ -1,4 +1,3 @@ -import type { ValueFormatter } from "@finos/vuu-table"; import type { Filter } from "@finos/vuu-filter-types"; import type { VuuAggType, @@ -7,9 +6,11 @@ import type { VuuSortType, VuuTable, } from "@finos/vuu-protocol-types"; -import type { FunctionComponent, MouseEvent } from "react"; +import { VuuDataRow } from "@finos/vuu-protocol-types"; +import type { ValueFormatter } from "@finos/vuu-table"; import type { ClientSideValidationChecker } from "@finos/vuu-ui-controls"; import type { ColumnMap } from "@finos/vuu-utils"; +import type { FunctionComponent, MouseEvent } from "react"; export type TableSelectionModel = "none" | "single" | "checkbox" | "extended"; @@ -42,6 +43,14 @@ export type DataItemCommitHandler< T extends VuuRowDataItemType = VuuRowDataItemType > = (value: T) => CommitResponse; +export type TableRowClickHandler = (row: VuuDataRow) => void; + +export type RowClickHandler = ( + row: DataSourceRow, + rangeSelect: boolean, + keepExistingSelection: boolean +) => void; + export interface TableCellRendererProps extends Omit { onCommit?: DataItemCommitHandler; @@ -51,7 +60,7 @@ export interface TableAttributes { columnDefaultWidth?: number; columnFormatHeader?: "capitalize" | "uppercase"; columnSeparators?: boolean; - showHighlightedRow?: boolean; + // showHighlightedRow?: boolean; rowSeparators?: boolean; zebraStripes?: boolean; } diff --git a/vuu-ui/packages/vuu-layout/src/layout-persistence/RemoteLayoutPersistenceManager.ts b/vuu-ui/packages/vuu-layout/src/layout-persistence/RemoteLayoutPersistenceManager.ts index 49afcb8e05..3a0007a333 100644 --- a/vuu-ui/packages/vuu-layout/src/layout-persistence/RemoteLayoutPersistenceManager.ts +++ b/vuu-ui/packages/vuu-layout/src/layout-persistence/RemoteLayoutPersistenceManager.ts @@ -5,9 +5,7 @@ import { import { LayoutPersistenceManager } from "./LayoutPersistenceManager"; import { LayoutJSON } from "../layout-reducer"; -const DEFAULT_SERVER_BASE_URL = "http://127.0.0.1:8081/api"; - -const baseURL = process.env.LAYOUT_BASE_URL ?? DEFAULT_SERVER_BASE_URL; +const baseURL = process.env.LAYOUT_BASE_URL; const metadataSaveLocation = "layouts/metadata"; const layoutsSaveLocation = "layouts"; const applicationLayoutsSaveLocation = "application-layouts"; diff --git a/vuu-ui/packages/vuu-popups/src/popup/Popup.tsx b/vuu-ui/packages/vuu-popups/src/popup/Popup.tsx index 88192586d9..30450c84e8 100644 --- a/vuu-ui/packages/vuu-popups/src/popup/Popup.tsx +++ b/vuu-ui/packages/vuu-popups/src/popup/Popup.tsx @@ -8,6 +8,7 @@ export type PopupPlacement = | "absolute" | "below" | "below-center" + | "below-right" | "below-full-width" | "center" | "right"; diff --git a/vuu-ui/packages/vuu-popups/src/popup/useAnchoredPosition.ts b/vuu-ui/packages/vuu-popups/src/popup/useAnchoredPosition.ts index 9ca9cc7470..09d3676647 100644 --- a/vuu-ui/packages/vuu-popups/src/popup/useAnchoredPosition.ts +++ b/vuu-ui/packages/vuu-popups/src/popup/useAnchoredPosition.ts @@ -36,6 +36,8 @@ const getPositionRelativeToAnchor = ( return { left: right + offsetLeft, top: top + offsetTop }; case "below-center": return { left: left + width / 2 + offsetLeft, top: bottom + offsetTop }; + case "below-right": + return { left: left, minWidth, top: bottom + offsetTop }; case "below-full-width": return { left: left + offsetLeft, diff --git a/vuu-ui/packages/vuu-protocol-types/index.d.ts b/vuu-ui/packages/vuu-protocol-types/index.d.ts index 7faca10cf3..278819e07d 100644 --- a/vuu-ui/packages/vuu-protocol-types/index.d.ts +++ b/vuu-ui/packages/vuu-protocol-types/index.d.ts @@ -443,12 +443,25 @@ export interface ClientToServerEditRowRpc { type: "VP_EDIT_ROW_RPC"; row: VuuDataRow; } + +export type VuuDataRowDto = { [key: string]: VuuRowDataItemType }; +export interface ClientToServerAddRowRpc { + rowKey: string; + type: "VP_EDIT_ADD_ROW_RPC"; + data: VuuDataRowDto; +} +export interface ClientToServerDeleteRowRpc { + rowKey: string; + type: "VP_EDIT_DELETE_ROW_RPC"; +} export interface ClientToServerSubmitFormRpc { type: "VP_EDIT_SUBMIT_FORM_RPC"; } export type ClientToServerEditRpc = | ClientToServerEditCellRpc + | ClientToServerAddRowRpc + | ClientToServerDeleteRowRpc | ClientToServerEditRowRpc | ClientToServerSubmitFormRpc; diff --git a/vuu-ui/packages/vuu-shell/src/layout-management/layoutTypes.ts b/vuu-ui/packages/vuu-shell/src/layout-management/layoutTypes.ts index 6b2d3c2dcf..8b2bf1dd32 100644 --- a/vuu-ui/packages/vuu-shell/src/layout-management/layoutTypes.ts +++ b/vuu-ui/packages/vuu-shell/src/layout-management/layoutTypes.ts @@ -16,4 +16,4 @@ export type LayoutMetadataDto = Omit; export interface Layout extends WithId { json: LayoutJSON; -} \ No newline at end of file +} diff --git a/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx b/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx index 0648660462..060d41a31c 100644 --- a/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx +++ b/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx @@ -15,13 +15,11 @@ import { } from "@finos/vuu-layout"; import { LayoutMetadata, LayoutMetadataDto } from "./layoutTypes"; -const local = process.env.LOCAL ?? true; - let _persistenceManager: LayoutPersistenceManager; const getPersistenceManager = () => { if (_persistenceManager === undefined) { - _persistenceManager = local + _persistenceManager = process.env.LOCAL ? new LocalLayoutPersistenceManager() : new RemoteLayoutPersistenceManager(); } @@ -68,7 +66,8 @@ export const LayoutManagementProvider = ( useEffect(() => { const persistenceManager = getPersistenceManager(); - persistenceManager.loadMetadata() + persistenceManager + .loadMetadata() .then((metadata) => { setLayoutMetadata(metadata); }) @@ -77,7 +76,8 @@ export const LayoutManagementProvider = ( console.error("Error occurred while retrieving metadata", error); }); - persistenceManager.loadApplicationLayout() + persistenceManager + .loadApplicationLayout() .then((layout: LayoutJSON) => { setApplicationLayout(layout); }) diff --git a/vuu-ui/packages/vuu-table-extras/src/cell-renderers-next/background-cell/BackgroundCell.tsx b/vuu-ui/packages/vuu-table-extras/src/cell-renderers-next/background-cell/BackgroundCell.tsx index 24b2b65a4c..0ed98b6410 100644 --- a/vuu-ui/packages/vuu-table-extras/src/cell-renderers-next/background-cell/BackgroundCell.tsx +++ b/vuu-ui/packages/vuu-table-extras/src/cell-renderers-next/background-cell/BackgroundCell.tsx @@ -38,11 +38,12 @@ const getFlashStyle = (colType?: ColumnType) => { }; // export to avoid tree shaking, component is not consumed directly -export const BackgroundCell = ({ column, row }: TableCellProps) => { +export const BackgroundCell = ({ column, columnMap, row }: TableCellProps) => { //TODO what about click handling - const { key, type, valueFormatter } = column; - const value = row[key]; + const { name, type, valueFormatter } = column; + const dataIdx = columnMap[name]; + const value = row[dataIdx]; const flashStyle = getFlashStyle(type); const direction = useDirection(row[KEY], value, column); const arrow = @@ -66,7 +67,7 @@ export const BackgroundCell = ({ column, row }: TableCellProps) => { return (
{arrow}
- {valueFormatter(row[column.key])} + {valueFormatter(row[dataIdx])}
); }; diff --git a/vuu-ui/packages/vuu-table/src/index.ts b/vuu-ui/packages/vuu-table/src/index.ts index 21dec65281..8c80575ce3 100644 --- a/vuu-ui/packages/vuu-table/src/index.ts +++ b/vuu-ui/packages/vuu-table/src/index.ts @@ -1,4 +1,8 @@ export * from "./table"; -export { GroupHeaderCellNext, TableNext } from "./table-next"; +export { + GroupHeaderCellNext, + TableNext, + useControlledTableNavigation, +} from "./table-next"; export type { RowProps } from "./table-next"; export { updateTableConfig } from "./table-next/table-config"; diff --git a/vuu-ui/packages/vuu-table/src/table-next/Row.css b/vuu-ui/packages/vuu-table/src/table-next/Row.css index 4fccc0fb9d..1307f4eb0d 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/Row.css +++ b/vuu-ui/packages/vuu-table/src/table-next/Row.css @@ -13,6 +13,11 @@ --row-background: var(--row-background-even); } + .vuuTableNextRow-highlighted { + background-color: var(--vuu-color-gray-10); + } + + .vuuTableNextRow-selected, .vuuTableNextRow-selectedEnd { /* background-color: rgb(133,133,137,.16); */ @@ -109,4 +114,10 @@ .vuuTableNextRow-expanded { --toggle-icon-transform: rotate(90deg); } - \ No newline at end of file + + .vuuDraggable .vuuTableNextRow { + --cell-borderColor: transparent; + --vuu-selection-decorator-bg: transparent; + transform: none!important; + z-index: 1; + } \ No newline at end of file diff --git a/vuu-ui/packages/vuu-table/src/table-next/Row.tsx b/vuu-ui/packages/vuu-table/src/table-next/Row.tsx index e71aa7f20f..2e713c02df 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/Row.tsx +++ b/vuu-ui/packages/vuu-table/src/table-next/Row.tsx @@ -2,8 +2,8 @@ import { DataSourceRow } from "@finos/vuu-data-types"; import { DataCellEditHandler, KeyedColumnDescriptor, + RowClickHandler, } from "@finos/vuu-datagrid-types"; -import { RowClickHandler } from "@finos/vuu-table"; import { ColumnMap, isGroupColumn, @@ -23,6 +23,7 @@ export interface RowProps { className?: string; columnMap: ColumnMap; columns: KeyedColumnDescriptor[]; + highlighted?: boolean; row: DataSourceRow; offset: number; onClick?: RowClickHandler; @@ -41,6 +42,7 @@ export const Row = memo( className: classNameProp, columnMap, columns, + highlighted, row, offset, onClick, @@ -69,6 +71,7 @@ export const Row = memo( const className = cx(classBase, classNameProp, { [`${classBase}-even`]: zebraStripes && rowIndex % 2 === 0, [`${classBase}-expanded`]: isExpanded, + [`${classBase}-highlighted`]: highlighted, [`${classBase}-selected`]: selectionStatus & True, [`${classBase}-selectedStart`]: selectionStatus & First, [`${classBase}-selectedEnd`]: selectionStatus & Last, diff --git a/vuu-ui/packages/vuu-table/src/table-next/TableNext.css b/vuu-ui/packages/vuu-table/src/table-next/TableNext.css index 9b4836589d..32b9511315 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/TableNext.css +++ b/vuu-ui/packages/vuu-table/src/table-next/TableNext.css @@ -134,4 +134,7 @@ .vuuDraggable-vuuTableNext { --header-height: 25px; --vuuTableNextHeaderCell-background: var(--vuu-color-gray-25); - } \ No newline at end of file + } +.vuuDraggable-vuuTableNext { + --row-height: 25px; +} diff --git a/vuu-ui/packages/vuu-table/src/table-next/TableNext.tsx b/vuu-ui/packages/vuu-table/src/table-next/TableNext.tsx index 5d26c7fb09..1a1106f8a6 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/TableNext.tsx +++ b/vuu-ui/packages/vuu-table/src/table-next/TableNext.tsx @@ -1,22 +1,16 @@ +import { MeasuredContainer, useId } from "@finos/vuu-layout"; import { ContextMenuProvider } from "@finos/vuu-popups"; import { TableProps } from "@finos/vuu-table"; import { isGroupColumn, metadataKeys, notHidden } from "@finos/vuu-utils"; +import { useForkRef } from "@salt-ds/core"; import cx from "classnames"; -import { - CSSProperties, - ForwardedRef, - forwardRef, - useEffect, - useRef, -} from "react"; +import { CSSProperties, ForwardedRef, forwardRef, useRef } from "react"; import { GroupHeaderCellNext as GroupHeaderCell, HeaderCell, } from "./header-cell"; import { Row as DefaultRow } from "./Row"; import { useTable } from "./useTableNext"; -import { MeasuredContainer, useId } from "@finos/vuu-layout"; -import { useForkRef } from "@salt-ds/core"; import "./TableNext.css"; @@ -27,15 +21,21 @@ const { IDX, RENDER_IDX } = metadataKeys; export const TableNext = forwardRef(function TableNext( { Row = DefaultRow, + allowDragDrop, availableColumns, className: classNameProp, config, dataSource, + disableFocus = false, + highlightedIndex: highlightedIndexProp, id: idProp, navigationStyle = "cell", onAvailableColumnsChange, onConfigChange, + onDragStart, + onDrop, onFeatureInvocation, + onHighlight, onRowClick: onRowClickProp, onSelect, onSelectionChange, @@ -51,15 +51,17 @@ export const TableNext = forwardRef(function TableNext( forwardedRef: ForwardedRef ) { const id = useId(idProp); - const containerRef = useRef(null); const { columnMap, columns, data, + draggableColumn, + draggableRow, dragDropHook, handleContextMenuAction, headerProps, + highlightedIndex, onDataEdited, onRemoveGroupColumn, onResize, @@ -71,15 +73,22 @@ export const TableNext = forwardRef(function TableNext( viewportMeasurements, ...tableProps } = useTable({ + allowDragDrop, availableColumns, config, containerRef, dataSource, + disableFocus, headerHeight, + highlightedIndex: highlightedIndexProp, + id, navigationStyle, onAvailableColumnsChange, onConfigChange, + onDragStart, + onDrop, onFeatureInvocation, + onHighlight, onRowClick: onRowClickProp, onSelect, onSelectionChange, @@ -87,6 +96,7 @@ export const TableNext = forwardRef(function TableNext( rowHeight, selectionModel, }); + const getStyle = () => { return { ...styleProp, @@ -105,7 +115,7 @@ export const TableNext = forwardRef(function TableNext( const className = cx(classBase, classNameProp, { [`${classBase}-colLines`]: tableAttributes.columnSeparators, [`${classBase}-rowLines`]: tableAttributes.rowSeparators, - [`${classBase}-highlight`]: tableAttributes.showHighlightedRow, + // [`${classBase}-highlight`]: tableAttributes.showHighlightedRow, [`${classBase}-zebra`]: tableAttributes.zebraStripes, // [`${classBase}-loading`]: isDataLoading(tableProps.columns), }); @@ -118,6 +128,7 @@ export const TableNext = forwardRef(function TableNext( -
+
{showColumnHeaders ? (
@@ -159,7 +174,7 @@ export const TableNext = forwardRef(function TableNext( /> ) )} - {dragDropHook.draggable} + {draggableColumn}
) : null} @@ -168,6 +183,7 @@ export const TableNext = forwardRef(function TableNext(
+ {draggableRow}
); diff --git a/vuu-ui/packages/vuu-table/src/table-next/cell-renderers/dropdown-cell/DropdownCell.tsx b/vuu-ui/packages/vuu-table/src/table-next/cell-renderers/dropdown-cell/DropdownCell.tsx index 52c3b975f7..0fe5b45080 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/cell-renderers/dropdown-cell/DropdownCell.tsx +++ b/vuu-ui/packages/vuu-table/src/table-next/cell-renderers/dropdown-cell/DropdownCell.tsx @@ -1,13 +1,12 @@ import { useLookupValues } from "@finos/vuu-data-react"; import { ListOption, TableCellRendererProps } from "@finos/vuu-datagrid-types"; import { - dispatchCommitEvent, Dropdown, DropdownOpenKey, SingleSelectionHandler, WarnCommit, } from "@finos/vuu-ui-controls"; -import { registerComponent } from "@finos/vuu-utils"; +import { dispatchCustomEvent, registerComponent } from "@finos/vuu-utils"; import { VuuColumnDataType } from "@finos/vuu-protocol-types"; import { memo, useCallback, useState } from "react"; import { dataAndColumnUnchanged } from "../cell-utils"; @@ -36,7 +35,7 @@ export const DropdownCell = memo(function DropdownCell({ setValue(selectedOption); onCommit(selectedOption.value as VuuColumnDataType).then((response) => { if (response === true && evt) { - dispatchCommitEvent(evt.target as HTMLElement); + dispatchCustomEvent(evt.target as HTMLElement, "vuu-commit"); } }); } diff --git a/vuu-ui/packages/vuu-table/src/table-next/cell-renderers/toggle-cell/ToggleCell.tsx b/vuu-ui/packages/vuu-table/src/table-next/cell-renderers/toggle-cell/ToggleCell.tsx index 4c5bbe9d14..dab7068574 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/cell-renderers/toggle-cell/ToggleCell.tsx +++ b/vuu-ui/packages/vuu-table/src/table-next/cell-renderers/toggle-cell/ToggleCell.tsx @@ -2,12 +2,9 @@ import { ColumnDescriptor, TableCellRendererProps, } from "@finos/vuu-datagrid-types"; +import { CycleStateButtonProps, WarnCommit } from "@finos/vuu-ui-controls"; import { - CycleStateButtonProps, - dispatchCommitEvent, - WarnCommit, -} from "@finos/vuu-ui-controls"; -import { + dispatchCustomEvent, isTypeDescriptor, isValueListRenderer, registerComponent, @@ -46,7 +43,7 @@ export const ToggleCell = memo(function ToggleCell({ (evt, value) => { return onCommit(value).then((response) => { if (response === true) { - dispatchCommitEvent(evt.target as HTMLElement); + dispatchCustomEvent(evt.target as HTMLElement, "vuu-commit"); } return response; }); diff --git a/vuu-ui/packages/vuu-table/src/table-next/index.ts b/vuu-ui/packages/vuu-table/src/table-next/index.ts index e4c1a2c0a5..280e4c9654 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/index.ts +++ b/vuu-ui/packages/vuu-table/src/table-next/index.ts @@ -3,3 +3,4 @@ export * from "./TableNext"; export * from "./table-config"; export * from "./cell-renderers"; export type { RowProps } from "./Row"; +export * from "./useControlledTableNavigation"; diff --git a/vuu-ui/packages/vuu-table/src/table-next/table-cell/TableCell.tsx b/vuu-ui/packages/vuu-table/src/table-next/table-cell/TableCell.tsx index c6c3ecf61f..1d521dfcf0 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/table-cell/TableCell.tsx +++ b/vuu-ui/packages/vuu-table/src/table-next/table-cell/TableCell.tsx @@ -2,10 +2,6 @@ import { DataItemCommitHandler, TableCellProps, } from "@finos/vuu-datagrid-types"; -import { - VuuColumnDataType, - VuuRowDataItemType, -} from "@finos/vuu-protocol-types"; import { isNumericColumn } from "@finos/vuu-utils"; import { MouseEventHandler, useCallback } from "react"; import { useCell } from "../useCell"; diff --git a/vuu-ui/packages/vuu-table/src/table-next/table-dom-utils.ts b/vuu-ui/packages/vuu-table/src/table-next/table-dom-utils.ts index b0006e1a3f..c123d20e44 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/table-dom-utils.ts +++ b/vuu-ui/packages/vuu-table/src/table-next/table-dom-utils.ts @@ -35,3 +35,18 @@ export const cellIsEditable = (cell: HTMLDivElement) => export const cellIsTextInput = (cell: HTMLElement) => cell.querySelector(".vuuTableInputCell") !== null; + +export function getRowIndex(rowEl?: HTMLElement) { + if (rowEl) { + const idx: string | null = rowEl.ariaRowIndex; + if (idx !== null) { + return parseInt(idx, 10); + } + } + return -1; +} + +const closestRow = (el: HTMLElement) => + el.closest('[role="row"]') as HTMLElement; + +export const closestRowIndex = (el: HTMLElement) => getRowIndex(closestRow(el)); diff --git a/vuu-ui/packages/vuu-table/src/table-next/useControlledTableNavigation.ts b/vuu-ui/packages/vuu-table/src/table-next/useControlledTableNavigation.ts new file mode 100644 index 0000000000..54833bc2e7 --- /dev/null +++ b/vuu-ui/packages/vuu-table/src/table-next/useControlledTableNavigation.ts @@ -0,0 +1,48 @@ +import { useStateRef } from "@finos/vuu-ui-controls"; +import { dispatchMouseEvent } from "@finos/vuu-utils"; +import { KeyboardEventHandler, useCallback, useRef } from "react"; + +export const useControlledTableNavigation = ( + initialValue: number, + rowCount: number +) => { + const tableRef = useRef(null); + + const [highlightedIndexRef, setHighlightedIndex] = useStateRef< + number | undefined + >(initialValue); + + const handleKeyDown = useCallback( + (e) => { + if (e.key === "ArrowDown") { + setHighlightedIndex((index = -1) => Math.min(rowCount - 1, index + 1)); + } else if (e.key === "ArrowUp") { + setHighlightedIndex((index = -1) => Math.max(0, index - 1)); + } else if (e.key === "Enter" || e.key === " ") { + const { current: rowIdx } = highlightedIndexRef; + // induce an onSelect event by 'clicking' the row + const rowEl = tableRef.current?.querySelector( + `[aria-rowindex="${rowIdx}"]` + ) as HTMLElement; + if (rowEl) { + dispatchMouseEvent(rowEl, "click"); + } + } + }, + [highlightedIndexRef, rowCount, setHighlightedIndex] + ); + + const handleHighlight = useCallback( + (idx: number) => { + setHighlightedIndex(idx); + }, + [setHighlightedIndex] + ); + + return { + highlightedIndexRef, + onHighlight: handleHighlight, + onKeyDown: handleKeyDown, + tableRef, + }; +}; diff --git a/vuu-ui/packages/vuu-table/src/table-next/useDataSource.ts b/vuu-ui/packages/vuu-table/src/table-next/useDataSource.ts index 0f96a7b131..d02def46b1 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/useDataSource.ts +++ b/vuu-ui/packages/vuu-table/src/table-next/useDataSource.ts @@ -44,7 +44,6 @@ export const useDataSource = ({ const setData = useCallback( (updates: DataSourceRow[]) => { - console.table(updates); for (const row of updates) { dataWindow.add(row); } @@ -135,6 +134,7 @@ export const useDataSource = ({ return { data: data.current, + dataRef: data, getSelectedRows, range: rangeRef.current, setRange, diff --git a/vuu-ui/packages/vuu-table/src/table-next/useKeyboardNavigation.ts b/vuu-ui/packages/vuu-table/src/table-next/useKeyboardNavigation.ts index 426c3c73a7..bb4e6e48e0 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/useKeyboardNavigation.ts +++ b/vuu-ui/packages/vuu-table/src/table-next/useKeyboardNavigation.ts @@ -1,3 +1,4 @@ +import { useControlled } from "@salt-ds/core"; import { VuuRange } from "@finos/vuu-protocol-types"; import { KeyboardEvent, @@ -5,12 +6,12 @@ import { RefObject, useCallback, useEffect, - useMemo, useRef, } from "react"; import { ScrollDirection, ScrollRequestHandler } from "./useTableScroll"; import { CellPos, + closestRowIndex, dataCellQuery, getTableCell, headerCellQuery, @@ -56,34 +57,58 @@ const NULL_CELL_POS: CellPos = [-1, -1]; const NO_SCROLL_NECESSARY = [undefined, undefined] as const; -const howFarIsCellOutsideViewport = ( - cellEl: HTMLElement +const howFarIsRowOutsideViewport = ( + rowEl: HTMLElement, + contentContainer = rowEl.closest(".vuuTableNext-contentContainer") ): readonly [ScrollDirection | undefined, number | undefined] => { //TODO lots of scope for optimisation here - const contentContainer = cellEl.closest(".vuuTableNext-contentContainer"); if (contentContainer) { const viewport = contentContainer?.getBoundingClientRect(); - const cell = cellEl.closest(".vuuTableNextCell")?.getBoundingClientRect(); - if (cell) { - if (cell.bottom > viewport.bottom) { - return ["down", cell.bottom - viewport.bottom]; - } else if (cell.top < viewport.top) { - return ["up", cell.top - viewport.top]; - } else if (cell.right > viewport.right) { - return ["right", cell.right + 6 - viewport.right]; - } else if (cell.left < viewport.left) { - return ["left", cell.left - viewport.left]; + const row = rowEl.getBoundingClientRect(); + if (row) { + if (row.bottom > viewport.bottom) { + return ["down", row.bottom - viewport.bottom]; + } else if (row.top < viewport.top) { + return ["up", row.top - viewport.top]; } else { return NO_SCROLL_NECESSARY; } } else { - throw Error("Whats going on, cell not found"); + throw Error("Whats going on, row not found"); } } else { throw Error("Whats going on, scrollbar container not found"); } }; +const howFarIsCellOutsideViewport = ( + cellEl: HTMLElement +): readonly [ScrollDirection | undefined, number | undefined] => { + //TODO lots of scope for optimisation here + const contentContainer = cellEl.closest(".vuuTableNext-contentContainer"); + if (contentContainer) { + const rowEl = cellEl.closest(".vuuTableNextRow") as HTMLElement; + if (rowEl) { + const result = howFarIsRowOutsideViewport(rowEl, contentContainer); + if (result !== NO_SCROLL_NECESSARY) { + return result; + } + const viewport = contentContainer?.getBoundingClientRect(); + const cell = cellEl.closest(".vuuTableNextCell")?.getBoundingClientRect(); + if (cell) { + if (cell.right > viewport.right) { + return ["right", cell.right + 6 - viewport.right]; + } else if (cell.left < viewport.left) { + return ["left", cell.left - viewport.left]; + } + } else { + throw Error("Whats going on, cell not found"); + } + } + } + return NO_SCROLL_NECESSARY; +}; + function nextCellPos( key: ArrowKey, [rowIdx, colIdx]: CellPos, @@ -124,10 +149,14 @@ function nextCellPos( export interface NavigationHookProps { containerRef: RefObject; columnCount?: number; + defaultHighlightedIndex?: number; + disableFocus?: boolean; disableHighlightOnFocus?: boolean; + highlightedIndex?: number; label?: string; navigationStyle: TableNavigationStyle; viewportRange: VuuRange; + onHighlight?: (idx: number) => void; requestScroll?: ScrollRequestHandler; restoreLastFocus?: boolean; rowCount?: number; @@ -138,9 +167,13 @@ export interface NavigationHookProps { export const useKeyboardNavigation = ({ columnCount = 0, containerRef, + disableFocus = false, + defaultHighlightedIndex, disableHighlightOnFocus, + highlightedIndex: highlightedIndexProp, navigationStyle, requestScroll, + onHighlight, rowCount = 0, viewportRowCount, }: // viewportRange, @@ -149,6 +182,28 @@ NavigationHookProps) => { const focusedCellPos = useRef([-1, -1]); const focusableCell = useRef(); const activeCellPos = useRef([-1, 0]); + // Keep this in sync with state value. This can be used by functions that need + // to reference highlightedIndex at call time but do not need to be regenerated + // every time it changes (i.e keep highlightedIndex out of their dependency + // arrays, as it can update frequently) + const highlightedIndexRef = useRef(); + + const [highlightedIndex, setHighlightedIdx] = useControlled({ + controlled: highlightedIndexProp, + default: defaultHighlightedIndex, + name: "UseKeyboardNavigation", + }); + highlightedIndexRef.current = highlightedIndex; + const setHighlightedIndex = useCallback( + (idx: number, fromKeyboard = false) => { + onHighlight?.(idx); + setHighlightedIdx(idx); + if (fromKeyboard) { + // lastFocus.current = idx; + } + }, + [onHighlight, setHighlightedIdx] + ); const getFocusedCell = (element: HTMLElement | Element | null) => element?.closest( @@ -198,12 +253,16 @@ NavigationHookProps) => { (rowIdx: number, colIdx: number, fromKeyboard = false) => { const pos: CellPos = [rowIdx, colIdx]; activeCellPos.current = pos; - focusCell(pos); + if (navigationStyle === "row") { + setHighlightedIdx(rowIdx); + } else { + focusCell(pos); + } if (fromKeyboard) { focusedCellPos.current = pos; } }, - [focusCell] + [focusCell, navigationStyle, setHighlightedIdx] ); const nextPageItemIdx = useCallback( @@ -248,17 +307,24 @@ NavigationHookProps) => { const focusedCell = getFocusedCell(document.activeElement); if (focusedCell) { focusedCellPos.current = getTableCellPos(focusedCell); + if (navigationStyle === "row") { + setHighlightedIdx(focusedCellPos.current[0]); + } } } } - }, [disableHighlightOnFocus, containerRef]); + }, [ + disableHighlightOnFocus, + containerRef, + navigationStyle, + setHighlightedIdx, + ]); const navigateChildItems = useCallback( async (key: NavigationKey) => { const [nextRowIdx, nextColIdx] = isPagingKey(key) ? await nextPageItemIdx(key, activeCellPos.current) : nextCellPos(key, activeCellPos.current, columnCount, rowCount); - const [rowIdx, colIdx] = activeCellPos.current; if (nextRowIdx !== rowIdx || nextColIdx !== colIdx) { setActiveCell(nextRowIdx, nextColIdx, true); @@ -267,15 +333,62 @@ NavigationHookProps) => { [columnCount, nextPageItemIdx, rowCount, setActiveCell] ); + const scrollRowIntoViewIfNecessary = useCallback( + (rowIndex: number) => { + const { current: container } = containerRef; + const activeRow = container?.querySelector( + `[aria-rowindex="${rowIndex}"]` + ) as HTMLElement; + if (activeRow) { + const [direction, distance] = howFarIsRowOutsideViewport(activeRow); + if (direction && distance) { + requestScroll?.({ type: "scroll-distance", distance, direction }); + } + } + }, + [containerRef, requestScroll] + ); + + const moveHighlightedRow = useCallback( + async (key: NavigationKey) => { + console.log(`moveHighlightedRow`); + const { current: highlighted } = highlightedIndexRef; + const [nextRowIdx] = isPagingKey(key) + ? await nextPageItemIdx(key, [highlighted ?? -1, 0]) + : nextCellPos(key, [highlighted ?? -1, 0], columnCount, rowCount); + if (nextRowIdx !== highlighted) { + setHighlightedIndex(nextRowIdx); + scrollRowIntoViewIfNecessary(nextRowIdx); + } + }, + [ + columnCount, + nextPageItemIdx, + rowCount, + scrollRowIntoViewIfNecessary, + setHighlightedIndex, + ] + ); + + useEffect(() => { + if (highlightedIndexProp !== undefined && highlightedIndexProp !== -1) { + scrollRowIntoViewIfNecessary(highlightedIndexProp); + } + }, [highlightedIndexProp, scrollRowIntoViewIfNecessary]); + const handleKeyDown = useCallback( (e: KeyboardEvent) => { if (rowCount > 0 && isNavigationKey(e.key, navigationStyle)) { e.preventDefault(); e.stopPropagation(); - void navigateChildItems(e.key); + if (navigationStyle === "row") { + moveHighlightedRow(e.key); + } else { + void navigateChildItems(e.key); + } } }, - [rowCount, navigationStyle, navigateChildItems] + [rowCount, navigationStyle, moveHighlightedRow, navigateChildItems] ); const handleClick = useCallback( @@ -291,25 +404,30 @@ NavigationHookProps) => { [setActiveCell] ); + const handleMouseLeave = useCallback(() => { + setHighlightedIndex(-1); + }, [setHighlightedIndex]); + + const handleMouseMove = useCallback( + (evt: MouseEvent) => { + const idx = closestRowIndex(evt.target as HTMLElement); + if (idx !== -1 && idx !== highlightedIndexRef.current) { + setHighlightedIndex(idx); + } + }, + [setHighlightedIndex] + ); + const navigate = useCallback(() => { navigateChildItems("ArrowDown"); }, [navigateChildItems]); - const containerProps = useMemo(() => { - return { - navigate, - onClick: handleClick, - onFocus: handleFocus, - onKeyDown: handleKeyDown, - }; - }, [handleClick, handleFocus, handleKeyDown, navigate]); - // First render will only render the outer container when explicit // sizing has not been provided. Outer container is measured and // only then, on second render, is content rendered. const fullyRendered = containerRef.current?.firstChild != null; useEffect(() => { - if (fullyRendered && focusableCell.current === undefined) { + if (fullyRendered && focusableCell.current === undefined && !disableFocus) { const { current: container } = containerRef; const cell = (container?.querySelector(headerCellQuery(0)) || container?.querySelector(dataCellQuery(0, 0))) as HTMLElement; @@ -320,5 +438,13 @@ NavigationHookProps) => { } }, [containerRef, fullyRendered]); - return containerProps; + return { + highlightedIndexRef, + navigate, + onClick: handleClick, + onFocus: handleFocus, + onKeyDown: handleKeyDown, + onMouseLeave: navigationStyle === "row" ? handleMouseLeave : undefined, + onMouseMove: navigationStyle === "row" ? handleMouseMove : undefined, + }; }; diff --git a/vuu-ui/packages/vuu-table/src/table-next/useSelection.ts b/vuu-ui/packages/vuu-table/src/table-next/useSelection.ts new file mode 100644 index 0000000000..7248b5d9f0 --- /dev/null +++ b/vuu-ui/packages/vuu-table/src/table-next/useSelection.ts @@ -0,0 +1,100 @@ +import { + RowClickHandler, + Selection, + SelectionChangeHandler, + TableSelectionModel, +} from "@finos/vuu-datagrid-types"; +import { + deselectItem, + dispatchMouseEvent, + isRowSelected, + metadataKeys, + selectItem, +} from "@finos/vuu-utils"; +import { DataSourceRow } from "packages/vuu-data-types"; +import { + KeyboardEvent, + KeyboardEventHandler, + MutableRefObject, + useCallback, + useRef, +} from "react"; + +const { IDX } = metadataKeys; + +const NO_SELECTION: Selection = []; + +const defaultSelectionKeys = ["Enter", " "]; + +export interface SelectionHookProps { + highlightedIndexRef: MutableRefObject; + selectionKeys?: string[]; + selectionModel: TableSelectionModel; + onSelect?: (row: DataSourceRow) => void; + onSelectionChange: SelectionChangeHandler; +} + +export const useSelection = ({ + highlightedIndexRef, + selectionKeys = defaultSelectionKeys, + selectionModel, + onSelect, + onSelectionChange, +}: SelectionHookProps) => { + selectionModel === "extended" || selectionModel === "checkbox"; + const lastActiveRef = useRef(-1); + const selectedRef = useRef(NO_SELECTION); + + const isSelectionEvent = useCallback( + (evt: KeyboardEvent) => selectionKeys.includes(evt.key), + [selectionKeys] + ); + + const handleRowClick = useCallback( + (row, rangeSelect, keepExistingSelection) => { + const { [IDX]: idx } = row; + const { current: active } = lastActiveRef; + const { current: selected } = selectedRef; + + const selectOperation = isRowSelected(row) ? deselectItem : selectItem; + + const newSelected = selectOperation( + selectionModel, + selected, + idx, + rangeSelect, + keepExistingSelection, + active + ); + + selectedRef.current = newSelected; + lastActiveRef.current = idx; + + onSelect?.(row); + onSelectionChange?.(newSelected); + }, + [onSelect, onSelectionChange, selectionModel] + ); + + const handleKeyDown = useCallback>( + (e) => { + if (isSelectionEvent(e)) { + const { current: rowIndex } = highlightedIndexRef; + if (rowIndex !== undefined && rowIndex !== -1) { + const rowEl = (e.target as HTMLElement).querySelector( + `[aria-rowindex="${rowIndex}"]` + ) as HTMLElement; + if (rowEl) { + dispatchMouseEvent(rowEl, "click"); + } + } + } + }, + [highlightedIndexRef, isSelectionEvent] + ); + + return { + onKeyDown: handleKeyDown, + onRowClick: handleRowClick, + }; +}; diff --git a/vuu-ui/packages/vuu-table/src/table-next/useTableNext.ts b/vuu-ui/packages/vuu-table/src/table-next/useTableNext.ts index ad72ae840a..d0ff0c6b52 100644 --- a/vuu-ui/packages/vuu-table/src/table-next/useTableNext.ts +++ b/vuu-ui/packages/vuu-table/src/table-next/useTableNext.ts @@ -1,5 +1,4 @@ import { - DataSource, DataSourceConfig, DataSourceSubscribedMessage, JsonDataSource, @@ -9,6 +8,7 @@ import { ColumnDescriptor, DataCellEditHandler, KeyedColumnDescriptor, + RowClickHandler, SelectionChangeHandler, TableConfig, TableSelectionModel, @@ -16,7 +16,10 @@ import { import { MeasuredSize, useLayoutEffectSkipFirst } from "@finos/vuu-layout"; import { VuuRange, VuuSortType } from "@finos/vuu-protocol-types"; import { useTableAndColumnSettings } from "@finos/vuu-table-extras"; -import { useDragDropNext as useDragDrop } from "@finos/vuu-ui-controls"; +import { + DragStartHandler, + useDragDropNext as useDragDrop, +} from "@finos/vuu-ui-controls"; import { useKeyboardNavigation } from "./useKeyboardNavigation"; import { applySort, @@ -37,7 +40,6 @@ import { useCallback, useEffect, useMemo, - useRef, useState, } from "react"; import { @@ -45,14 +47,13 @@ import { ColumnActionHide, ColumnActionPin, MeasuredProps, - RowClickHandler, TableProps, - useSelection, } from "../table"; import { TableColumnResizeHandler } from "./column-resizing"; import { updateTableConfig } from "./table-config"; import { useDataSource } from "./useDataSource"; import { useInitialValue } from "./useInitialValue"; +import { useSelection } from "./useSelection"; import { useTableContextMenu } from "./useTableContextMenu"; import { useHandleTableContextMenu } from "./context-menu"; import { useCellEditing } from "./useCellEditing"; @@ -70,13 +71,20 @@ export interface TableHookProps extends MeasuredProps, Pick< TableProps, + | "allowDragDrop" | "availableColumns" | "config" | "dataSource" + | "disableFocus" + | "highlightedIndex" + | "id" | "navigationStyle" | "onAvailableColumnsChange" | "onConfigChange" + | "onDragStart" + | "onDrop" | "onFeatureInvocation" + | "onHighlight" | "onSelect" | "onSelectionChange" | "onRowClick" @@ -99,15 +107,22 @@ const addColumn = ( }); export const useTable = ({ + allowDragDrop = false, availableColumns, config, containerRef, dataSource, + disableFocus, headerHeight = 25, + highlightedIndex: highlightedIndexProp, + id, navigationStyle = "cell", onAvailableColumnsChange, onConfigChange, + onDragStart, + onDrop, onFeatureInvocation, + onHighlight, onRowClick: onRowClickProp, onSelect, onSelectionChange, @@ -116,7 +131,6 @@ export const useTable = ({ selectionModel, }: TableHookProps) => { const [rowCount, setRowCount] = useState(dataSource.size); - const dataSourceRef = useRef(); if (dataSource === undefined) { throw Error("no data source provided to Vuu Table"); } @@ -217,7 +231,7 @@ export const useTable = ({ [dispatchColumnAction] ); - const { data, getSelectedRows, range, setRange } = useDataSource({ + const { data, dataRef, getSelectedRows, range, setRange } = useDataSource({ dataSource, onFeatureInvocation, renderBufferSize, @@ -467,6 +481,7 @@ export const useTable = ({ }); const { + highlightedIndexRef, navigate, onFocus: navigationFocus, onKeyDown: navigationKeyDown, @@ -474,9 +489,12 @@ export const useTable = ({ } = useKeyboardNavigation({ columnCount: columns.filter((c) => c.hidden !== true).length, containerRef, + disableFocus, + highlightedIndex: highlightedIndexProp, navigationStyle, requestScroll, rowCount: dataSource?.size, + onHighlight, viewportRange: range, viewportRowCount: viewportMeasurements.rowCount, }); @@ -499,16 +517,6 @@ export const useTable = ({ [editingFocus, navigationFocus] ); - const handleKeyDown = useCallback( - (e: KeyboardEvent) => { - navigationKeyDown(e); - if (!e.defaultPrevented) { - editingKeyDown(e); - } - }, - [navigationKeyDown, editingKeyDown] - ); - const onContextMenu = useTableContextMenu({ columns, data, @@ -553,12 +561,29 @@ export const useTable = ({ [dataSource, onSelectionChange] ); - const selectionHookOnRowClick = useSelection({ + const { + onKeyDown: selectionHookKeyDown, + onRowClick: selectionHookOnRowClick, + } = useSelection({ + highlightedIndexRef, onSelect, onSelectionChange: handleSelectionChange, selectionModel, }); + const handleKeyDown = useCallback( + (e: KeyboardEvent) => { + navigationKeyDown(e); + if (!e.defaultPrevented) { + editingKeyDown(e); + } + if (!e.defaultPrevented) { + selectionHookKeyDown(e); + } + }, + [navigationKeyDown, editingKeyDown, selectionHookKeyDown] + ); + const handleRowClick = useCallback( (row, rangeSelect, keepExistingSelection) => { selectionHookOnRowClick(row, rangeSelect, keepExistingSelection); @@ -585,7 +610,7 @@ export const useTable = ({ }); }, [dataSource, dispatchColumnAction]); - const handleDrop = useCallback( + const handleDropColumnHeader = useCallback( (moveFrom: number, moveTo: number) => { const column = tableConfig.columns[moveFrom]; @@ -604,40 +629,89 @@ export const useTable = ({ [dataSource.config, dispatchColumnAction, onConfigChange, tableConfig] ); + const handleDropRow = useCallback( + (dragDropState) => { + onDrop?.(dragDropState); + }, + [onDrop] + ); + const handleDataEdited = useCallback( async (row, columnName, value) => dataSource.applyEdit(row, columnName, value), [dataSource] ); - const { onMouseDown: dragDropHookHandleMouseDown, ...dragDropHook } = + // Drag Drop column headers + const { + onMouseDown: columnHeaderDragMouseDown, + draggable: draggableColumn, + ...dragDropHook + } = useDragDrop({ + allowDragDrop: true, + containerRef, + // this is for useDragDropNext + draggableClassName: `vuuTableNext`, + // extendedDropZone: overflowedItems.length > 0, + onDrop: handleDropColumnHeader, + orientation: "horizontal", + itemQuery: ".vuuTableNextHeaderCell", + }); + + const handleDragStartRow = useCallback( + (dragDropState) => { + const { initialDragElement } = dragDropState; + const rowIndex = initialDragElement.ariaRowIndex; + if (rowIndex) { + const index = parseInt(rowIndex); + const row = dataRef.current.find((row) => row[0] === index); + console.log(`handleDragStartRow setPayload`, { + row, + }); + if (row) { + dragDropState.setPayload(row); + } else { + // should we abort the operation ? + } + } + onDragStart?.(dragDropState); + }, + [dataRef, onDragStart] + ); + + // Drag Drop rowss + const { onMouseDown: rowDragMouseDown, draggable: draggableRow } = useDragDrop({ - allowDragDrop: true, + allowDragDrop, containerRef, - // this is for useDragDropNext draggableClassName: `vuuTableNext`, - // extendedDropZone: overflowedItems.length > 0, - onDrop: handleDrop, - orientation: "horizontal", - itemQuery: ".vuuTableNextHeaderCell", + id, + onDragStart: handleDragStartRow, + onDrop: handleDropRow, + orientation: "vertical", + itemQuery: ".vuuTableNextRow", }); const headerProps = { onClick: onHeaderClick, - onMouseDown: dragDropHookHandleMouseDown, + onMouseDown: columnHeaderDragMouseDown, onResize: onHeaderResize, }; return { ...containerProps, + draggableColumn, + draggableRow, onBlur: editingBlur, onFocus: handleFocus, onKeyDown: handleKeyDown, + onMouseDown: rowDragMouseDown, columnMap, columns, data, handleContextMenuAction, headerProps, + highlightedIndex: highlightedIndexRef.current, menuBuilder, onContextMenu, onDataEdited: handleDataEdited, diff --git a/vuu-ui/packages/vuu-table/src/table/TableRow.tsx b/vuu-ui/packages/vuu-table/src/table/TableRow.tsx index ffbe9d587c..9f8b4925b2 100644 --- a/vuu-ui/packages/vuu-table/src/table/TableRow.tsx +++ b/vuu-ui/packages/vuu-table/src/table/TableRow.tsx @@ -1,5 +1,8 @@ import { DataSourceRow } from "@finos/vuu-data-types"; -import { KeyedColumnDescriptor } from "@finos/vuu-datagrid-types"; +import { + KeyedColumnDescriptor, + RowClickHandler, +} from "@finos/vuu-datagrid-types"; import { ColumnMap, isGroupColumn, @@ -11,7 +14,6 @@ import { } from "@finos/vuu-utils"; import cx from "classnames"; import { HTMLAttributes, memo, MouseEvent, useCallback } from "react"; -import { RowClickHandler } from "./dataTableTypes"; import { TableCell } from "./TableCell"; import { TableGroupCell } from "./TableGroupCell"; diff --git a/vuu-ui/packages/vuu-table/src/table/dataTableTypes.ts b/vuu-ui/packages/vuu-table/src/table/dataTableTypes.ts index 4faded45f8..2468fdb3d8 100644 --- a/vuu-ui/packages/vuu-table/src/table/dataTableTypes.ts +++ b/vuu-ui/packages/vuu-table/src/table/dataTableTypes.ts @@ -2,41 +2,45 @@ import { DataSource, SchemaColumn, VuuFeatureInvocationMessage, - VuuFeatureMessage, } from "@finos/vuu-data"; import { DataSourceRow } from "@finos/vuu-data-types"; import { KeyedColumnDescriptor, + RowClickHandler, SelectionChangeHandler, TableConfig, TableHeadings, + TableRowClickHandler, TableSelectionModel, } from "@finos/vuu-datagrid-types"; -import { VuuDataRow } from "@finos/vuu-protocol-types"; import { MeasuredContainerProps } from "@finos/vuu-layout"; +import { DragStartHandler, dragStrategy } from "@finos/vuu-ui-controls"; import { FC, MouseEvent } from "react"; import { RowProps } from "../table-next/Row"; -export type TableRowClickHandler = (row: VuuDataRow) => void; // TODO implement a Model object to represent a row data for better API export type TableRowSelectHandler = (row: DataSourceRow) => void; export type TableNavigationStyle = "none" | "cell" | "row"; -export interface TableProps extends Omit { +export interface TableProps + extends Omit { Row?: FC; allowConfigEditing?: boolean; + allowDragDrop?: boolean | dragStrategy; /** * required if a fully featured column picker is to be available */ availableColumns?: SchemaColumn[]; config: TableConfig; dataSource: DataSource; + disableFocus?: boolean; headerHeight?: number; /** * Defined how focus navigation within data cells will be handled by table. * Default is cell. */ + highlightedIndex?: number; navigationStyle?: TableNavigationStyle; /** * required if a fully featured column picker is to be available. @@ -50,12 +54,15 @@ export interface TableProps extends Omit { * prop, table state can be persisted across sessions. */ onConfigChange?: (config: TableConfig) => void; + onDragStart?: DragStartHandler; + onDrop?: () => void; /** * When a Vuu feature e.g. context menu action, has been invoked, the Vuu server * response must be handled. This callback provides that response. */ onFeatureInvocation?: (message: VuuFeatureInvocationMessage) => void; + onHighlight?: (idx: number) => void; /** * callback invoked when user 'clicks' a table row. CLick triggered either * via mouse click or keyboard (default ENTER); @@ -125,9 +132,3 @@ export interface Viewport { rowCount: number; // contentWidth: number; } - -export type RowClickHandler = ( - row: DataSourceRow, - rangeSelect: boolean, - keepExistingSelection: boolean -) => void; diff --git a/vuu-ui/packages/vuu-table/src/table/useSelection.ts b/vuu-ui/packages/vuu-table/src/table/useSelection.ts index 7abab67945..c459ac9a5f 100644 --- a/vuu-ui/packages/vuu-table/src/table/useSelection.ts +++ b/vuu-ui/packages/vuu-table/src/table/useSelection.ts @@ -1,4 +1,5 @@ import { + RowClickHandler, Selection, SelectionChangeHandler, TableSelectionModel, @@ -11,7 +12,6 @@ import { } from "@finos/vuu-utils"; import { DataSourceRow } from "@finos/vuu-data-types"; import { useCallback, useRef } from "react"; -import { RowClickHandler } from "./dataTableTypes"; const { IDX } = metadataKeys; diff --git a/vuu-ui/packages/vuu-ui-controls/src/common-hooks/index.ts b/vuu-ui/packages/vuu-ui-controls/src/common-hooks/index.ts index e0d5fed6bd..4c54dfdc48 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/common-hooks/index.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/common-hooks/index.ts @@ -1,8 +1,9 @@ export * from "./collectionProvider"; export * from "./collectionTypes"; export * from "./itemToString"; +export * from "./useCollectionItems"; +export * from "./useControlled"; export * from "./use-resize-observer"; export * from "./navigationTypes"; export * from "./selectionTypes"; -export * from "./useCollectionItems"; -export * from "./useControlled"; +export * from "./useStateRef"; diff --git a/vuu-ui/packages/vuu-ui-controls/src/common-hooks/selectionTypes.ts b/vuu-ui/packages/vuu-ui-controls/src/common-hooks/selectionTypes.ts index 3196773bc0..304d18aff9 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/common-hooks/selectionTypes.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/common-hooks/selectionTypes.ts @@ -76,7 +76,7 @@ export interface ListHandlers { export interface SelectionHookProps extends SelectionProps { containerRef: RefObject; disableSelection?: boolean; - highlightedIdx: number; + highlightedIndex: number; itemQuery: string; label?: string; onClick?: MouseEventHandler; diff --git a/vuu-ui/packages/vuu-ui-controls/src/common-hooks/useSelection.ts b/vuu-ui/packages/vuu-ui-controls/src/common-hooks/useSelection.ts index 0678b89420..9064e0fb2f 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/common-hooks/useSelection.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/common-hooks/useSelection.ts @@ -11,7 +11,6 @@ import { useRef, } from "react"; import { - ListHandlers, SelectionHookProps, SelectionHookResult, selectionIsDisallowed, @@ -24,8 +23,6 @@ export const GROUP_SELECTION_NONE = "none"; export const GROUP_SELECTION_SINGLE = "single"; export const GROUP_SELECTION_CASCADE = "cascade"; -const NO_SELECTION_HANDLERS: ListHandlers = {}; - export type GroupSelectionMode = "none" | "single" | "cascade"; const defaultSelectionKeys = ["Enter", " "]; @@ -39,7 +36,7 @@ export const useSelection = ({ defaultSelected, disableSelection = false, // groupSelection = GROUP_SELECTION_NONE, - highlightedIdx, + highlightedIndex, itemQuery, onClick, // label, @@ -180,7 +177,7 @@ export const useSelection = ({ const handleKeyDown = useCallback( (evt: KeyboardEvent) => { const { current: container } = containerRef; - const element = getElementByDataIndex(container, highlightedIdx); + const element = getElementByDataIndex(container, highlightedIndex); if (isSelectableElement(element)) { if (isSelectionEvent(evt) || (tabToSelect && evt.key === "Tab")) { // We do not inhibit Tab behaviour, if we are selecting on Tab then we apply @@ -190,18 +187,18 @@ export const useSelection = ({ } selectItemAtIndex( evt, - highlightedIdx, + highlightedIndex, false, evt.ctrlKey || evt.metaKey ); if (isExtendedSelect) { - lastActive.current = highlightedIdx; + lastActive.current = highlightedIndex; } } } }, [ - highlightedIdx, + highlightedIndex, containerRef, isSelectionEvent, tabToSelect, @@ -226,25 +223,25 @@ export const useSelection = ({ const handleClick = useCallback( (evt: MouseEvent) => { const { current: container } = containerRef; - const element = getElementByDataIndex(container, highlightedIdx); + const element = getElementByDataIndex(container, highlightedIndex); if (!disableSelection && isSelectableElement(element)) { evt.preventDefault(); evt.stopPropagation(); selectItemAtIndex( evt, - highlightedIdx, + highlightedIndex, evt.shiftKey, evt.ctrlKey || evt.metaKey ); if (isExtendedSelect) { - lastActive.current = highlightedIdx; + lastActive.current = highlightedIndex; } } onClick?.(evt); }, [ containerRef, - highlightedIdx, + highlightedIndex, disableSelection, onClick, selectItemAtIndex, diff --git a/vuu-ui/packages/vuu-ui-controls/src/common-hooks/useStateRef.ts b/vuu-ui/packages/vuu-ui-controls/src/common-hooks/useStateRef.ts new file mode 100644 index 0000000000..e28c8659e4 --- /dev/null +++ b/vuu-ui/packages/vuu-ui-controls/src/common-hooks/useStateRef.ts @@ -0,0 +1,31 @@ +import { + Dispatch, + MutableRefObject, + SetStateAction, + useCallback, + useRef, + useState, +} from "react"; + +const isSimpleStateValue = (arg: SetStateAction): arg is T => + typeof arg !== "function"; + +// Keeps a ref value in sync with a state value +export const useStateRef = ( + initialValue: T +): [MutableRefObject, Dispatch>] => { + const [value, _setValue] = useState(initialValue); + const ref = useRef(value); + + const setValue = useCallback>>((arg) => { + if (isSimpleStateValue(arg)) { + ref.current = arg; + _setValue(arg); + } else { + const { current: prev } = ref; + ref.current = arg(prev); + _setValue(ref.current); + } + }, []); + return [ref, setValue]; +}; diff --git a/vuu-ui/packages/vuu-ui-controls/src/cycle-state-button/CycleStateButton.tsx b/vuu-ui/packages/vuu-ui-controls/src/cycle-state-button/CycleStateButton.tsx index 03d5b71eb2..1b8d986069 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/cycle-state-button/CycleStateButton.tsx +++ b/vuu-ui/packages/vuu-ui-controls/src/cycle-state-button/CycleStateButton.tsx @@ -37,7 +37,6 @@ export const CycleStateButton = forwardRef(function CycleStateButton( const handleClick = useCallback( (evt: SyntheticEvent) => { const nextValue = getNextValue(value, values); - console.log(`CycleStateButton handleClick ${value} => ${nextValue}`); onCommit(evt, nextValue as VuuColumnDataType).then((response) => { if (response !== true) { console.error(response); diff --git a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/DragDropProvider.tsx b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/DragDropProvider.tsx index 70f761135f..ec73cea1e8 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/DragDropProvider.tsx +++ b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/DragDropProvider.tsx @@ -6,12 +6,15 @@ import React, { useMemo, } from "react"; import { DragDropState } from "./DragDropState"; -import { MouseOffset } from "./dragDropTypesNext"; -import { ResumeDragHandler, useGlobalDragDrop } from "./useGlobalDragDrop"; +import { + GlobalDropHandler, + ResumeDragHandler, + useGlobalDragDrop, +} from "./useGlobalDragDrop"; const NO_DRAG_CONTEXT = { - isDragSource: false, - isDropTarget: false, + isDragSource: undefined, + isDropTarget: undefined, register: () => undefined, }; @@ -25,7 +28,8 @@ export type DragOutHandler = ( export type DragDropRegistrationFn = ( id: string, - resumeDrag?: ResumeDragHandler + resumeDrag: ResumeDragHandler | false, + onDrop?: GlobalDropHandler ) => void; export type EndOfDragOperationHandler = (id: string) => void; @@ -74,6 +78,7 @@ export const DragDropProvider = ({ () => new Map(), [] ); + const dropHandlers = useMemo(() => new Map(), []); const handleDragOverDropTarget = useCallback( (dropTargetId: string, dragDropState: DragDropState) => { const resumeDrag = resumeDragHandlers.get(dropTargetId); @@ -86,8 +91,19 @@ export const DragDropProvider = ({ [resumeDragHandlers] ); + const handleDrop = useCallback( + (dropTargetId: string, dragDropState: DragDropState) => { + const handleDrop = dropHandlers.get(dropTargetId); + if (handleDrop) { + handleDrop(dragDropState); + } + }, + [dropHandlers] + ); + const { measuredDropTargetsRef, resumeDrag } = useGlobalDragDrop({ onDragOverDropTarget: handleDragOverDropTarget, + onDrop: handleDrop, }); const [dragSources, dropTargets] = useMemo(() => { const sources = new Map(); @@ -123,6 +139,7 @@ export const DragDropProvider = ({ const onDragOut = useCallback( (id, dragDropState) => { + console.log("DragDropProvider onDragOut"); // we call releaseItem if and when the dragged item is dropped onto a remote dropTarget measuredDropTargetsRef.current = measureDropTargets(dragSources.get(id)); resumeDrag(dragDropState); @@ -136,12 +153,15 @@ export const DragDropProvider = ({ }, []); const registerDragDropParty = useCallback( - (id, resumeDrag) => { + (id, resumeDrag, onDrop) => { + console.log(`register drag drop agent #${id}`); if (resumeDrag) { resumeDragHandlers.set(id, resumeDrag); + } else if (onDrop) { + dropHandlers.set(id, onDrop); } }, - [resumeDragHandlers] + [dropHandlers, resumeDragHandlers] ); const contextValue: DragDropContextProps = useMemo( @@ -169,8 +189,8 @@ export const DragDropProvider = ({ }; export interface DragDropProviderResult { - isDragSource: boolean; - isDropTarget: boolean; + isDragSource?: boolean; + isDropTarget?: boolean; onDragOut?: DragOutHandler; onEndOfDragOperation?: (id: string) => void; register: DragDropRegistrationFn; @@ -184,7 +204,7 @@ export const useDragDropProvider = (id?: string): DragDropProviderResult => { onEndOfDragOperation, registerDragDropParty, } = useContext(DragDropContext); - if (id) { + if (id && (dragSources || dropTargets)) { const isDragSource = dragSources?.has(id) ?? false; const isDropTarget = dropTargets?.has(id) ?? false; diff --git a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/Draggable.tsx b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/Draggable.tsx index 87391b994e..1e8d3be34b 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/Draggable.tsx +++ b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/Draggable.tsx @@ -3,7 +3,9 @@ import cx from "classnames"; import { CSSProperties, forwardRef, + HTMLAttributes, MutableRefObject, + RefCallback, TransitionEventHandler, useCallback, useMemo, @@ -14,58 +16,66 @@ import "./Draggable.css"; const makeClassNames = (classNames: string) => classNames.split(" ").map((className) => `vuuDraggable-${className}`); -export const Draggable = forwardRef< - HTMLDivElement, - { - wrapperClassName: string; - element: HTMLElement; - onTransitionEnd?: TransitionEventHandler; - scale?: number; - style: CSSProperties; - } ->(function Draggable( - { wrapperClassName, element, onTransitionEnd, style, scale = 1 }, - forwardedRef -) { - const callbackRef = useCallback( - (el: HTMLDivElement) => { - if (el) { - el.innerHTML = ""; - el.appendChild(element); - if (scale !== 1) { - el.style.transform = `scale(${scale},${scale})`; + +export interface DraggableProps extends HTMLAttributes { + wrapperClassName: string; + element: HTMLElement; + onDropped?: () => void; + onTransitionEnd?: TransitionEventHandler; + scale?: number; + style: CSSProperties; +} + +export const Draggable = forwardRef( + function Draggable( + { wrapperClassName, element, onDropped, onTransitionEnd, style, scale = 1 }, + forwardedRef + ) { + const handleVuuDrop = useCallback(() => { + onDropped?.(); + }, [onDropped]); + + const callbackRef = useCallback>( + (el: HTMLDivElement) => { + if (el) { + el.innerHTML = ""; + el.appendChild(element); + if (scale !== 1) { + el.style.transform = `scale(${scale},${scale})`; + } + el.addEventListener("vuu-dropped", handleVuuDrop); } - } - }, - [element, scale] - ); - const forkedRef = useForkRef(forwardedRef, callbackRef); + }, + [element, handleVuuDrop, scale] + ); + const forkedRef = useForkRef(forwardedRef, callbackRef); - const position = useMemo( - () => ({ - left: 0, - top: 0, - }), - [] - ); + const position = useMemo( + () => ({ + left: 0, + top: 0, + }), + [] + ); - return ( - - -
- - - ); -}); + return ( + + +
+ + + ); + } +); // const colors = ["black", "red", "green", "yellow"]; // let color_idx = 0; diff --git a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/dragDropTypesNext.ts b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/dragDropTypesNext.ts index c8e7e1eace..6bd327b313 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/dragDropTypesNext.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/dragDropTypesNext.ts @@ -27,7 +27,11 @@ export type dimensionsType = { //----------------------------------- -export type dragStrategy = "drop-indicator" | "natural-movement"; +export type dragStrategy = + | "drop-indicator" + | "natural-movement" + | "drag-copy" + | "drop-only"; export type Direction = "fwd" | "bwd"; export const FWD: Direction = "fwd"; @@ -52,7 +56,7 @@ export interface DragHookResult { isDragging: boolean; isScrolling: RefObject; onMouseDown?: MouseEventHandler; - revealOverflowedItems: boolean; + revealOverflowedItems?: boolean; } export interface InternalDragHookResult @@ -60,8 +64,8 @@ export interface InternalDragHookResult beginDrag: (dragElement: HTMLElement) => void; drag: (dragPos: number, mouseMoveDirection: "fwd" | "bwd") => void; drop: () => void; - handleScrollStart: () => void; - handleScrollStop: ( + handleScrollStart?: () => void; + handleScrollStop?: ( scrollDirection: "fwd" | "bwd", _scrollPos: number, atEnd: boolean diff --git a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/drop-target-utils.ts b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/drop-target-utils.ts index 7abc8f959d..9585f0323b 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/drop-target-utils.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/drop-target-utils.ts @@ -9,7 +9,7 @@ const TOP_BOTTOM = ["top", "bottom"]; export const NOT_OVERFLOWED = ":not(.wrapped)"; export const NOT_HIDDEN = ':not([aria-hidden="true"])'; -// TODO figure out which of these sttributes we no longer need +// TODO figure out which of these attributes we no longer need export type MeasuredDropTarget = { /** The index position currently occupied by this item. If draggable @@ -355,3 +355,33 @@ export const dropTargetsDebugString = (dropTargets: MeasuredDropTarget[]) => )}) ${d.element?.textContent} ` ) .join(""); + +export const getScrollableContainer = ( + container: HTMLElement, + itemQuery: string +) => { + // TODO if this is too fragile a way to identify scrollable container, we + // can add a prop to pass it 'scrollableContainerQuery' + const firstItem = container.querySelector( + `${itemQuery}:not([aria-hidden="true"])` + ); + // generally, we expect the immediateParent to be a contentContainer, the + // parent of that will be the scrollable container. This may or may not be + // the outer container (likely not) + const immediateParent = firstItem?.parentElement; + if (immediateParent === container) { + return container; + } else { + return immediateParent?.parentElement as HTMLElement; + } +}; + +export const isContainerScrollable = ( + scrollableContainer: HTMLElement, + orientation: orientationType +) => { + const { SCROLL_SIZE, CLIENT_SIZE } = dimensions(orientation); + const { [SCROLL_SIZE]: scrollSize, [CLIENT_SIZE]: clientSize } = + scrollableContainer; + return scrollSize > clientSize; +}; diff --git a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropCopy.ts b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropCopy.ts new file mode 100644 index 0000000000..e4ec1744cc --- /dev/null +++ b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropCopy.ts @@ -0,0 +1,37 @@ +import { useCallback, useRef } from "react"; + +import { + InternalDragDropProps, + InternalDragHookResult, + ViewportRange, +} from "./dragDropTypesNext"; + +export const useDragDropCopy = ({ + selected, + viewportRange, +}: InternalDragDropProps): InternalDragHookResult => { + const rangeRef = useRef(); + rangeRef.current = viewportRange; + + const beginDrag = useCallback( + (dragElement: HTMLElement) => { + if ( + dragElement.ariaSelected && + Array.isArray(selected) && + selected.length > 1 + ) { + console.log("its a selected element, and we have a multi select"); + } + }, + [selected] + ); + + const drag = useCallback(() => undefined, []); + const drop = useCallback(() => undefined, []); + + return { + beginDrag, + drag, + drop, + }; +}; diff --git a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropNaturalMovementNext.tsx b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropNaturalMovementNext.tsx index 3e2b834937..93231dafb8 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropNaturalMovementNext.tsx +++ b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropNaturalMovementNext.tsx @@ -10,7 +10,7 @@ import { useDragDisplacers } from "./useDragDisplacers"; import { dispatchMouseEvent } from "@finos/vuu-utils"; import { dimensions, - dropTargetsDebugString, + // dropTargetsDebugString, getIndexOfDraggedItem, getNextDropTarget, MeasuredDropTarget, @@ -43,8 +43,6 @@ export const useDragDropNaturalMovement = ({ const draggedItemRef = useRef(); const fullItemQuery = `:is(${itemQuery}${NOT_OVERFLOWED}${NOT_HIDDEN},.vuuOverflowContainer-OverflowIndicator)`; - // const { setMeasurements: setVizData } = useListViz(); - const indexOf = (dropTarget: MeasuredDropTarget) => measuredDropTargets.current.findIndex((d) => d.id === dropTarget.id); @@ -129,7 +127,7 @@ export const useDragDropNaturalMovement = ({ )); if (internalDrag) { - console.log(dropTargetsDebugString(dropTargets)); + // console.log(dropTargetsDebugString(dropTargets)); const indexOfDraggedItem = getIndexOfDraggedItem(dropTargets); const draggedItem = dropTargets[indexOfDraggedItem]; if (draggedItem && container) { @@ -153,7 +151,7 @@ export const useDragDropNaturalMovement = ({ const index = dropTargets.indexOf(dropTarget); const { start, end, mid } = dropTarget; - console.log(`nextDropTarget ${dropTarget.element.textContent}`); + // console.log(`nextDropTarget ${dropTarget.element.textContent}`); // need to compute the correct position of this // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -178,7 +176,7 @@ export const useDragDropNaturalMovement = ({ target.start += size; } - console.log(dropTargetsDebugString(dropTargets)); + // console.log(dropTargetsDebugString(dropTargets)); const displaceFunction = dropTarget.isLast ? displaceLastItem diff --git a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropNext.tsx b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropNext.tsx index 752e4ccfb6..8f94ce7218 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropNext.tsx +++ b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useDragDropNext.tsx @@ -1,4 +1,5 @@ import { isOverflowElement } from "@finos/vuu-layout"; +import { dispatchCustomEvent } from "@finos/vuu-utils"; import { MouseEventHandler, useCallback, @@ -21,9 +22,12 @@ import { cloneElement, constrainRect, dimensions, + getScrollableContainer, + isContainerScrollable, NOT_OVERFLOWED, } from "./drop-target-utils"; import { ScrollStopHandler, useAutoScroll } from "./useAutoScroll"; +import { useDragDropCopy } from "./useDragDropCopy"; import { useDragDropIndicator } from "./useDragDropIndicator"; import { useDragDropNaturalMovement } from "./useDragDropNaturalMovementNext"; import { ResumeDragHandler } from "./useGlobalDragDrop"; @@ -114,7 +118,8 @@ export const useDragDropNext: DragDropHook = ({ const startPosRef = useRef({ x: 0, y: 0 }); /** references the dragged Item during its final 'settling' phase post drop */ const settlingItemRef = useRef(null); - + /** the container which will scroll if content overflows */ + const scrollableContainerRef = useRef(null); const dropPosRef = useRef(-1); const dropIndexRef = useRef(-1); @@ -188,6 +193,9 @@ export const useDragDropNext: DragDropHook = ({ ); const terminateDrag = useCallback(() => { + const { current: settlingItem } = settlingItemRef; + settlingItemRef.current = null; + const { current: toIndex } = dropIndexRef; const droppedItem = containerRef.current?.querySelector( `${itemQuery}[data-index="${toIndex}"]` @@ -195,18 +203,21 @@ export const useDragDropNext: DragDropHook = ({ if (droppedItem) { droppedItem.classList.remove("vuuDropTarget-settling"); } - dropIndexRef.current = -1; onDropSettle?.(toIndex); setDraggableStatus((status) => ({ ...status, draggable: undefined, })); + + if (settlingItem) { + dispatchCustomEvent(settlingItem, "vuu-dropped"); + } }, [containerRef, itemQuery, onDropSettle]); const getScrollDirection = useCallback( (mousePos: number) => { - if (containerRef.current && dragDropStateRef.current) { + if (scrollableContainerRef.current && dragDropStateRef.current) { const { mouseOffset } = dragDropStateRef.current; const { POS, SCROLL_POS, SCROLL_SIZE, CLIENT_SIZE } = @@ -215,7 +226,7 @@ export const useDragDropNext: DragDropHook = ({ [SCROLL_POS]: scrollPos, [SCROLL_SIZE]: scrollSize, [CLIENT_SIZE]: clientSize, - } = containerRef.current; + } = scrollableContainerRef.current; const maxScroll = scrollSize - clientSize; const canScrollFwd = scrollPos < maxScroll; @@ -227,7 +238,7 @@ export const useDragDropNext: DragDropHook = ({ return bwd ? "bwd" : fwd ? "fwd" : ""; } }, - [containerRef, orientation] + [scrollableContainerRef, orientation] ); const useDragDropHook: InternalHook = @@ -235,6 +246,8 @@ export const useDragDropNext: DragDropHook = ({ ? useDragDropNaturalMovement : allowDragDrop === "drop-indicator" ? useDragDropIndicator + : allowDragDrop === "drag-copy" + ? useDragDropCopy : noDragDrop; const onScrollingStopped = useCallback( @@ -245,7 +258,7 @@ export const useDragDropNext: DragDropHook = ({ ); const { isScrolling, startScrolling, stopScrolling } = useAutoScroll({ - containerRef, + containerRef: scrollableContainerRef, onScrollingStopped, orientation, }); @@ -301,7 +314,14 @@ export const useDragDropNext: DragDropHook = ({ ? Math.abs(lastClientContraPos - clientContraPos) : 0; - if (dragDropStateRef.current && dragOutDistance - dragDistance > 5) { + // If isDropTarget is false, there are configured dropTargets in context + // but this is not one, so drag will be handed straight over to DragProvider + // (global drag). If isDropTarget is undefined, we have no DragProvider + // so we are dealing with a simple local drag drop operation. + const handoverToProvider = + isDropTarget === false || dragOutDistance - dragDistance > 5; + + if (dragDropStateRef.current && handoverToProvider) { if (onDragOut?.(id as string, dragDropStateRef.current)) { // TODO create a cleanup function removeDragHandlers(); @@ -313,7 +333,15 @@ export const useDragDropNext: DragDropHook = ({ return true; } }, - [id, isDragSource, onDragOut, orientation, releaseDrag, removeDragHandlers] + [ + id, + isDragSource, + isDropTarget, + onDragOut, + orientation, + releaseDrag, + removeDragHandlers, + ] ); const dragMouseMoveHandler = useCallback( @@ -327,7 +355,6 @@ export const useDragDropNext: DragDropHook = ({ const { current: dragDropState } = dragDropStateRef; if (dragHandedOvertoProvider(dragDistance, clientContraPos)) { - console.log("drag handed over to provider"); return; } @@ -352,7 +379,7 @@ export const useDragDropNext: DragDropHook = ({ isScrollableRef.current && !isScrolling.current ) { - handleScrollStart(); + handleScrollStart?.(); startScrolling(scrollDirection, 1); } else if (!scrollDirection && isScrolling.current) { stopScrolling(); @@ -372,15 +399,12 @@ export const useDragDropNext: DragDropHook = ({ } } }, - // eslint-disable-next-line react-hooks/exhaustive-deps [ drag, + dragHandedOvertoProvider, getScrollDirection, handleScrollStart, - id, - isDragSource, isScrolling, - onDragOut, orientation, startScrolling, stopScrolling, @@ -408,12 +432,9 @@ export const useDragDropNext: DragDropHook = ({ (dragDropState: DragDropState) => { dragDropStateRef.current = dragDropState; // Note this is using the draggable element rather than the original draggedElement - const { draggableElement, mouseOffset, initialDragElement } = - dragDropState; + const { draggableElement, mouseOffset } = dragDropState; const { current: container } = containerRef; - console.log({ container, draggableElement, initialDragElement }); - if (container && draggableElement) { const containerRect = container.getBoundingClientRect(); const draggableRect = draggableElement.getBoundingClientRect(); @@ -441,11 +462,16 @@ export const useDragDropNext: DragDropHook = ({ const dragElement = getDraggableElement(target, itemQuery); const { current: container } = containerRef; if (container && dragElement) { - const { SCROLL_SIZE, CLIENT_SIZE } = dimensions(orientation); + const scrollableContainer = getScrollableContainer( + container, + itemQuery + ); - const { [SCROLL_SIZE]: scrollSize, [CLIENT_SIZE]: clientSize } = - container; - isScrollableRef.current = scrollSize > clientSize; + isScrollableRef.current = isContainerScrollable( + scrollableContainer, + orientation + ); + scrollableContainerRef.current = scrollableContainer; const containerRect = container.getBoundingClientRect(); const draggableRect = dragElement.getBoundingClientRect(); @@ -468,6 +494,7 @@ export const useDragDropNext: DragDropHook = ({ draggable: ( { + // TODO runtime check here for valid drop targets ? + const { current: container } = containerRef; // We don't want to prevent other handlers on this element from working // but we do want to stop a drag drop being initiated on a bubbled event. @@ -579,18 +608,24 @@ export const useDragDropNext: DragDropHook = ({ terminateDrag(); } }); - } else { - console.log(`dont have the dropped item (at ${dropPos})`); } - settlingItemRef.current = null; + // settlingItemRef.current = null; } }, [containerRef, itemQuery, settlingItem, terminateDrag]); useEffect(() => { if (id && (isDragSource || isDropTarget)) { - register(id, resumeDrag); + register(id, allowDragDrop === "drop-only" ? false : resumeDrag, onDrop); } - }, [id, isDragSource, isDropTarget, register, resumeDrag]); + }, [ + allowDragDrop, + id, + isDragSource, + isDropTarget, + onDrop, + register, + resumeDrag, + ]); return { ...dragResult, diff --git a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useGlobalDragDrop.ts b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useGlobalDragDrop.ts index f3d67e188f..579a452c29 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useGlobalDragDrop.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/drag-drop/useGlobalDragDrop.ts @@ -1,19 +1,23 @@ -import { boxContainsPoint } from "@finos/vuu-utils"; +import { boxContainsPoint, dispatchCustomEvent } from "@finos/vuu-utils"; import { useCallback, useRef } from "react"; import { MeasuredTarget } from "./DragDropProvider"; import { DragDropState } from "./DragDropState"; import { MouseOffset } from "./dragDropTypesNext"; export type ResumeDragHandler = (dragDropState: DragDropState) => boolean; +export type GlobalDropHandler = (dragDropState: DragDropState) => void; export const useGlobalDragDrop = ({ onDragOverDropTarget, + onDrop, }: { onDragOverDropTarget: ( dropTargetId: string, dragDropState: DragDropState ) => boolean; + onDrop: (dropTargetId: string, dragDropState: DragDropState) => void; }) => { + const dropTargetRef = useRef(); const measuredDropTargetsRef = useRef>(); const dragDropStateRef = useRef(null); @@ -48,14 +52,23 @@ export const useGlobalDragDrop = ({ draggableElement.style.top = `${dragPosY}px`; draggableElement.style.left = `${dragPosX}px`; - const dropTarget = overDropTarget(dragPosX, dragPosY); - if (dropTarget) { - if (onDragOverDropTarget(dropTarget, dragDropState)) { + const dropTargetId = overDropTarget(dragPosX, dragPosY); + if (dropTargetId) { + const dropTargetWillResumeDrag = onDragOverDropTarget( + dropTargetId, + dragDropState + ); + if (dropTargetWillResumeDrag) { // prettier-ignore document.removeEventListener("mousemove", dragMouseMoveHandler, false); document.removeEventListener("mouseup", dragMouseUpHandler, false); dragDropStateRef.current = null; + dropTargetRef.current = undefined; + } else { + dropTargetRef.current = dropTargetId; } + } else { + dropTargetRef.current = undefined; } } }, @@ -66,7 +79,17 @@ export const useGlobalDragDrop = ({ const dragMouseUpHandler = useCallback(() => { document.removeEventListener("mousemove", dragMouseMoveHandler, false); document.removeEventListener("mouseup", dragMouseUpHandler, false); - }, [dragMouseMoveHandler]); + const { current: dragDropState } = dragDropStateRef; + if (dragDropState) { + dragDropStateRef.current = null; + if (dropTargetRef.current) { + onDrop(dropTargetRef.current, dragDropState); + } + if (dragDropState.draggableElement) { + dispatchCustomEvent(dragDropState.draggableElement, "vuu-dropped"); + } + } + }, [dragMouseMoveHandler, onDrop]); const resumeDrag = useCallback( (dragDropState) => { diff --git a/vuu-ui/packages/vuu-ui-controls/src/editable/useEditableText.ts b/vuu-ui/packages/vuu-ui-controls/src/editable/useEditableText.ts index 616a555be4..8af1b4f7f9 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/editable/useEditableText.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/editable/useEditableText.ts @@ -1,6 +1,7 @@ -import { VuuRowDataItemType } from "@finos/vuu-protocol-types"; import { DataItemCommitHandler } from "@finos/vuu-datagrid-types"; import { useLayoutEffectSkipFirst } from "@finos/vuu-layout"; +import { VuuRowDataItemType } from "@finos/vuu-protocol-types"; +import { dispatchCustomEvent } from "@finos/vuu-utils"; import { FocusEventHandler, FormEventHandler, @@ -27,11 +28,6 @@ export interface EditableTextHookProps< type?: "string" | "number" | "boolean"; } -export const dispatchCommitEvent = (el: HTMLElement) => { - const commitEvent = new Event("vuu-commit"); - el.dispatchEvent(commitEvent); -}; - export const useEditableText = ({ clientSideEditValidationCheck, initialValue, @@ -61,7 +57,7 @@ export const useEditableText = ({ onCommit(value as T).then((response) => { if (response === true) { isDirtyRef.current = false; - dispatchCommitEvent(target); + dispatchCustomEvent(target, "vuu-commit"); } else { setMessage(response); } @@ -69,7 +65,7 @@ export const useEditableText = ({ } } else { // why, if not dirty ? - dispatchCommitEvent(target); + dispatchCustomEvent(target, "vuu-commit"); hasCommittedRef.current = false; } }, diff --git a/vuu-ui/packages/vuu-ui-controls/src/instrument-picker/InstrumentPicker.tsx b/vuu-ui/packages/vuu-ui-controls/src/instrument-picker/InstrumentPicker.tsx index e87ce9fa35..74058322bc 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/instrument-picker/InstrumentPicker.tsx +++ b/vuu-ui/packages/vuu-ui-controls/src/instrument-picker/InstrumentPicker.tsx @@ -54,11 +54,12 @@ export const InstrumentPicker = forwardRef(function InstrumentPicker( const id = useId(idProp); const { - controlProps, + highlightedIndex, inputProps, isOpen, onOpenChange, tableHandlers, + tableRef, value, } = useInstrumentPicker({ columnMap, @@ -76,7 +77,7 @@ export const InstrumentPicker = forwardRef(function InstrumentPicker( ...TableProps, config: { ...TableProps.config, - showHighlightedRow: true, + zebraStripes: false, }, }; @@ -95,7 +96,6 @@ export const InstrumentPicker = forwardRef(function InstrumentPicker( @@ -107,8 +107,10 @@ export const InstrumentPicker = forwardRef(function InstrumentPicker( {...tableHandlers} className={`${classBase}-list`} height={200} + highlightedIndex={highlightedIndex} dataSource={dataSource} navigationStyle="row" + ref={tableRef} showColumnHeaders={false} /> diff --git a/vuu-ui/packages/vuu-ui-controls/src/instrument-picker/useInstrumentPicker.ts b/vuu-ui/packages/vuu-ui-controls/src/instrument-picker/useInstrumentPicker.ts index 49f54efb3a..e9cbec1d63 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/instrument-picker/useInstrumentPicker.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/instrument-picker/useInstrumentPicker.ts @@ -1,7 +1,10 @@ import { DataSource } from "@finos/vuu-data"; import { DataSourceRow } from "@finos/vuu-data-types"; import { ColumnDescriptor } from "@finos/vuu-datagrid-types"; -import { TableRowSelectHandler } from "@finos/vuu-table"; +import { + TableRowSelectHandler, + useControlledTableNavigation, +} from "@finos/vuu-table"; import { ColumnMap } from "@finos/vuu-utils"; import { ChangeEvent, useCallback, useMemo, useState } from "react"; import { useControlled } from "../common-hooks"; @@ -43,6 +46,9 @@ export const useInstrumentPicker = ({ name: "useDropdownList", }); + const { highlightedIndexRef, onKeyDown, tableRef } = + useControlledTableNavigation(-1, dataSource.size); + const baseFilterPattern = useMemo( // TODO make this contains once server supports it () => searchColumns.map((col) => `${col} starts "__VALUE__"`).join(" or "), @@ -53,9 +59,6 @@ export const useInstrumentPicker = ({ (open, closeReason) => { setIsOpen(open); onOpenChange?.(open, closeReason); - // if (open === false) { - // dataSource.unsubscribe(); - // } }, [onOpenChange, setIsOpen] ); @@ -94,20 +97,21 @@ export const useInstrumentPicker = ({ const inputProps = { inputProps: { autoComplete: "off", + onKeyDown, }, onChange: handleInputChange, }; - const controlProps = {}; const tableHandlers = { onSelect: handleSelectRow, }; return { - controlProps, + highlightedIndex: highlightedIndexRef.current, inputProps, isOpen, onOpenChange: handleOpenChange, tableHandlers, + tableRef, value, }; }; diff --git a/vuu-ui/packages/vuu-ui-controls/src/instrument-search/InstrumentSearch.css b/vuu-ui/packages/vuu-ui-controls/src/instrument-search/InstrumentSearch.css index 16e0868ecd..5782c11631 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/instrument-search/InstrumentSearch.css +++ b/vuu-ui/packages/vuu-ui-controls/src/instrument-search/InstrumentSearch.css @@ -8,8 +8,10 @@ .vuuInstrumentSearch-inputField { --vuu-icon-size: 16px; flex: 0 0 40px; + padding: 0 12px; } .vuuInstrumentSearch-list { + --vuuMeasuredContainer-flex: 1 1 1px; background-color: var(--salt-container-primary-background); flex: 1 1 auto; } diff --git a/vuu-ui/packages/vuu-ui-controls/src/instrument-search/InstrumentSearch.tsx b/vuu-ui/packages/vuu-ui-controls/src/instrument-search/InstrumentSearch.tsx index 298c038ddd..89a749e0a4 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/instrument-search/InstrumentSearch.tsx +++ b/vuu-ui/packages/vuu-ui-controls/src/instrument-search/InstrumentSearch.tsx @@ -1,12 +1,17 @@ import { DataSource } from "@finos/vuu-data"; import { TableConfig } from "@finos/vuu-datagrid-types"; import { registerComponent } from "@finos/vuu-layout"; -import { TableNext, TableProps } from "@finos/vuu-table"; -import { FormField, FormFieldLabel, Input } from "@salt-ds/core"; +import { + TableNext, + TableProps, + useControlledTableNavigation, +} from "@finos/vuu-table"; +import { Input } from "@salt-ds/core"; import cx from "classnames"; import { FormEvent, HTMLAttributes, + RefCallback, useCallback, useMemo, useState, @@ -36,6 +41,7 @@ const defaultTableConfig: TableConfig = { export interface InstrumentSearchProps extends HTMLAttributes { TableProps?: Partial; + autoFocus?: boolean; dataSource: DataSource; placeHolder?: string; searchColumns?: string[]; @@ -45,6 +51,7 @@ const searchIcon = ; export const InstrumentSearch = ({ TableProps, + autoFocus = false, className, dataSource, placeHolder, @@ -57,6 +64,9 @@ export const InstrumentSearch = ({ [searchColumns] ); + const { highlightedIndexRef, onHighlight, onKeyDown, tableRef } = + useControlledTableNavigation(-1, dataSource.size); + const [searchState, setSearchState] = useState<{ searchText: string; filter: string; @@ -77,26 +87,38 @@ export const InstrumentSearch = ({ [baseFilterPattern, dataSource] ); + const searchCallbackRef = useCallback>((el) => { + setTimeout(() => { + el?.querySelector("input")?.focus(); + }, 100); + }, []); + return (
- - +
- +
diff --git a/vuu-ui/packages/vuu-ui-controls/src/list/common-hooks/useKeyboardNavigation.ts b/vuu-ui/packages/vuu-ui-controls/src/list/common-hooks/useKeyboardNavigation.ts index 17ac6213ed..32ae7942be 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/list/common-hooks/useKeyboardNavigation.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/list/common-hooks/useKeyboardNavigation.ts @@ -159,6 +159,7 @@ export const useKeyboardNavigation = ({ onKeyboardNavigation, restoreLastFocus, selected, + // TODO viewportItemCount, }: NavigationHookProps): NavigationHookResult => { const lastFocus = useRef(-1); diff --git a/vuu-ui/packages/vuu-ui-controls/src/list/useList.ts b/vuu-ui/packages/vuu-ui-controls/src/list/useList.ts index e396d668e3..5408309b54 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/list/useList.ts +++ b/vuu-ui/packages/vuu-ui-controls/src/list/useList.ts @@ -164,7 +164,7 @@ export const useList = ({ const selectionHook = useSelection({ containerRef, defaultSelected, - highlightedIdx: highlightedIndex, + highlightedIndex: highlightedIndex, itemQuery: ".vuuListItem", label: `${label}:useList`, onClick: onClickProp, diff --git a/vuu-ui/packages/vuu-ui-controls/src/vuu-input/VuuInput.tsx b/vuu-ui/packages/vuu-ui-controls/src/vuu-input/VuuInput.tsx index 32787e61ea..00c3444d67 100644 --- a/vuu-ui/packages/vuu-ui-controls/src/vuu-input/VuuInput.tsx +++ b/vuu-ui/packages/vuu-ui-controls/src/vuu-input/VuuInput.tsx @@ -7,6 +7,7 @@ import { ForwardedRef, forwardRef, KeyboardEventHandler, + ReactElement, SyntheticEvent, useCallback, } from "react"; @@ -124,4 +125,8 @@ export const VuuInput = forwardRef(function VuuInput< {tooltipProps ? : null} ); -}); +}) as ( + props: VuuInputProps & { + ref?: ForwardedRef; + } +) => ReactElement>; diff --git a/vuu-ui/packages/vuu-utils/src/html-utils.ts b/vuu-ui/packages/vuu-utils/src/html-utils.ts index 650d60bf0f..a18e5d029d 100644 --- a/vuu-ui/packages/vuu-utils/src/html-utils.ts +++ b/vuu-ui/packages/vuu-utils/src/html-utils.ts @@ -148,3 +148,13 @@ export const dispatchMouseEvent = (el: HTMLElement, type: MouseEventTypes) => { }); el.dispatchEvent(evt); }; + +export type VuuDomEventType = "vuu-commit" | "vuu-dropped"; + +export const dispatchCustomEvent = (el: HTMLElement, type: VuuDomEventType) => { + const evt = new Event(type, { + bubbles: true, + cancelable: true, + }); + el.dispatchEvent(evt); +}; diff --git a/vuu-ui/sample-apps/app-vuu-example/src/App.tsx b/vuu-ui/sample-apps/app-vuu-example/src/App.tsx index 332e584be8..a7f3eb185e 100644 --- a/vuu-ui/sample-apps/app-vuu-example/src/App.tsx +++ b/vuu-ui/sample-apps/app-vuu-example/src/App.tsx @@ -1,3 +1,7 @@ +import { + registerComponent, + useLayoutContextMenuItems, +} from "@finos/vuu-layout"; import { ContextMenuProvider, useDialog } from "@finos/vuu-popups"; import { LeftNav, @@ -6,20 +10,18 @@ import { ShellProps, VuuUser, } from "@finos/vuu-shell"; -import { getDefaultColumnConfig } from "./columnMetaData"; -import { createPlaceholder } from "./createPlaceholder"; -import { useFeatures } from "./useFeatures"; import { ColumnSettingsPanel, TableSettingsPanel, } from "@finos/vuu-table-extras"; -import { - registerComponent, - useLayoutContextMenuItems, -} from "@finos/vuu-layout"; +import { getDefaultColumnConfig } from "./columnMetaData"; +import { createPlaceholder } from "./createPlaceholder"; +import { useFeatures } from "./useFeatures"; +import { DragDropProvider } from "@finos/vuu-ui-controls"; import "./App.css"; import { useRpcResponseHandler } from "./useRpcResponseHandler"; +import { useMemo } from "react"; registerComponent("ColumnSettings", ColumnSettingsPanel, "view"); registerComponent("TableSettings", TableSettingsPanel, "view"); @@ -49,33 +51,42 @@ export const App = ({ user }: { user: VuuUser }) => { const { buildMenuOptions, handleMenuAction } = useLayoutContextMenuItems(setDialogState); + const dragSource = useMemo( + () => ({ + "basket-instruments": { dropTargets: "basket-constituents" }, + }), + [] + ); + // TODO get Context from Shell return ( - - - } - saveUrl="https://localhost:8443/api/vui" - serverUrl={serverUrl} - user={user} + + - {dialog} - - + + } + saveUrl="https://localhost:8443/api/vui" + serverUrl={serverUrl} + user={user} + > + {dialog} + + + ); }; diff --git a/vuu-ui/sample-apps/feature-basket-trading/src/VuuBasketTradingFeature.tsx b/vuu-ui/sample-apps/feature-basket-trading/src/VuuBasketTradingFeature.tsx index 56a3ccf18a..d25898d3bb 100644 --- a/vuu-ui/sample-apps/feature-basket-trading/src/VuuBasketTradingFeature.tsx +++ b/vuu-ui/sample-apps/feature-basket-trading/src/VuuBasketTradingFeature.tsx @@ -40,6 +40,7 @@ const VuuBasketTradingFeature = (props: BasketTradingFeatureProps) => { dialog, onClickAddBasket, onCommitBasketChange, + onDropInstrument, onSendToMarket, onTakeOffMarket, } = useBasketTrading({ @@ -85,6 +86,7 @@ const VuuBasketTradingFeature = (props: BasketTradingFeatureProps) => { data-tab-title="Design" contextMenuConfig={basketDesignContextMenuConfig} dataSource={dataSourceBasketTradingConstituentJoin} + onDrop={onDropInstrument} tableSchema={basketTradingConstituentJoinSchema} /> (null); const id = useId(idProp); - const { isOpen, onClickAddBasket, onOpenChange, tableProps } = + const { isOpen, onClickAddBasket, onOpenChange, tableProps, triggerRef } = useBasketSelector({ basketInstanceId, dataSourceBasketTradingSearch, @@ -49,9 +49,11 @@ export const BasketSelector = ({ return (
@@ -86,13 +88,14 @@ export const BasketSelector = ({
{basketName} - + {status === "ON MARKET" ? ( + + ) : ( + + )}
{basketId} diff --git a/vuu-ui/sample-apps/feature-basket-trading/src/basket-selector/useBasketSelector.ts b/vuu-ui/sample-apps/feature-basket-trading/src/basket-selector/useBasketSelector.ts index 805f8569d8..dcbf06ac61 100644 --- a/vuu-ui/sample-apps/feature-basket-trading/src/basket-selector/useBasketSelector.ts +++ b/vuu-ui/sample-apps/feature-basket-trading/src/basket-selector/useBasketSelector.ts @@ -1,7 +1,8 @@ -import { TableProps, TableRowClickHandler } from "@finos/vuu-table"; -import { buildColumnMap } from "@finos/vuu-utils"; +import { TableRowClickHandler } from "@finos/vuu-datagrid-types"; +import { TableProps } from "@finos/vuu-table"; import { OpenChangeHandler, useControlled } from "@finos/vuu-ui-controls"; -import { useCallback, useMemo } from "react"; +import { buildColumnMap } from "@finos/vuu-utils"; +import { useCallback, useMemo, useRef } from "react"; import { BasketSelectorProps } from "./BasketSelector"; import { BasketSelectorRow } from "./BasketSelectorRow"; @@ -25,6 +26,7 @@ export const useBasketSelector = ({ onOpenChange, onSelectBasket, }: BasketSelectorHookProps) => { + const triggerRef = useRef(null); const [isOpen, setIsOpen] = useControlled({ controlled: isOpenProp, default: defaultIsOpen ?? false, @@ -41,8 +43,12 @@ export const useBasketSelector = ({ setIsOpen(open); onOpenChange?.(open, closeReason); if (open === false) { - console.log(`%cdisable basketSearch`, "color:red;font-weight:bold;"); dataSourceBasketTradingSearch.disable?.(); + if (closeReason !== "Tab") { + setTimeout(() => { + triggerRef.current?.focus(); + }, 100); + } } }, [dataSourceBasketTradingSearch, onOpenChange, setIsOpen] @@ -68,7 +74,7 @@ export const useBasketSelector = ({ Row: BasketSelectorRow, config: { columns: [ - { name: "instanceId", width: 365 }, + { name: "instanceId", width: 380 }, { name: "basketId", width: 100, hidden: true }, { name: "name", @@ -105,5 +111,6 @@ export const useBasketSelector = ({ onClickAddBasket: handleClickAddBasket, onOpenChange: handleOpenChange, tableProps, + triggerRef, }; }; diff --git a/vuu-ui/sample-apps/feature-basket-trading/src/basket-table-edit/BasketTableEdit.tsx b/vuu-ui/sample-apps/feature-basket-trading/src/basket-table-edit/BasketTableEdit.tsx index 668974f036..3c73713ecb 100644 --- a/vuu-ui/sample-apps/feature-basket-trading/src/basket-table-edit/BasketTableEdit.tsx +++ b/vuu-ui/sample-apps/feature-basket-trading/src/basket-table-edit/BasketTableEdit.tsx @@ -31,11 +31,15 @@ export const BasketTableEdit = ({ [] ); + console.log({ dataSource }); + return ( { + if (notional === undefined) { + return ""; + } else { + return notional.toLocaleString(); + } +}; + export type BasketChangeHandler = ( columnName: string, value: VuuRowDataItemType @@ -43,7 +51,6 @@ export const BasketToolbar = ({ onTakeOffMarket, }: BasketToolbarProps) => { const handleMenuAction: MenuActionHandler = () => { - console.log("Menu Action"); return true; }; @@ -92,7 +99,7 @@ export const BasketToolbar = ({ @@ -126,7 +133,7 @@ export const BasketToolbar = ({ Total USD Not - {basket?.totalNotional ?? ""} + {formatNotional(basket?.totalNotional)} ); @@ -135,7 +142,7 @@ export const BasketToolbar = ({ Total Not - {basket?.totalNotionalUsd ?? ""} + {formatNotional(basket?.totalNotionalUsd)} ); diff --git a/vuu-ui/sample-apps/feature-basket-trading/src/new-basket-panel/NewBasketPanel.tsx b/vuu-ui/sample-apps/feature-basket-trading/src/new-basket-panel/NewBasketPanel.tsx index 6b1860b77b..a205b28bdc 100644 --- a/vuu-ui/sample-apps/feature-basket-trading/src/new-basket-panel/NewBasketPanel.tsx +++ b/vuu-ui/sample-apps/feature-basket-trading/src/new-basket-panel/NewBasketPanel.tsx @@ -12,7 +12,7 @@ import { import { Button, FormField, FormFieldLabel } from "@salt-ds/core"; import cx from "classnames"; import { DataSourceRow } from "@finos/vuu-data-types"; -import { HTMLAttributes, useMemo } from "react"; +import { HTMLAttributes, RefCallback, useCallback, useMemo } from "react"; import "./NewBasketPanel.css"; import { useNewBasketPanel } from "./useNewBasketPanel"; @@ -45,6 +45,7 @@ export const NewBasketPanel = ({ onSave, onSelectBasket, saveButtonDisabled, + saveButtonRef, } = useNewBasketPanel({ basketDataSource, basketSchema, @@ -70,6 +71,12 @@ export const NewBasketPanel = ({ const itemToString = displayName(columnMap.name); + const inputCallbackRef = useCallback>((el) => { + setTimeout(() => { + el?.querySelector("input")?.focus(); + }, 100); + }, []); + return ( @@ -82,7 +89,11 @@ export const NewBasketPanel = ({
Basket Name - + Basket Definition @@ -102,9 +113,10 @@ export const NewBasketPanel = ({ Cancel diff --git a/vuu-ui/sample-apps/feature-basket-trading/src/new-basket-panel/useNewBasketPanel.ts b/vuu-ui/sample-apps/feature-basket-trading/src/new-basket-panel/useNewBasketPanel.ts index a2092bf17d..d16e2b8ad8 100644 --- a/vuu-ui/sample-apps/feature-basket-trading/src/new-basket-panel/useNewBasketPanel.ts +++ b/vuu-ui/sample-apps/feature-basket-trading/src/new-basket-panel/useNewBasketPanel.ts @@ -6,7 +6,7 @@ import { import { TableRowSelectHandler } from "@finos/vuu-table"; import { Commithandler, OpenChangeHandler } from "@finos/vuu-ui-controls"; import { buildColumnMap, metadataKeys } from "@finos/vuu-utils"; -import { useCallback, useState } from "react"; +import { useCallback, useRef, useState } from "react"; import { NewBasketPanelProps } from "./NewBasketPanel"; const { KEY } = metadataKeys; @@ -43,6 +43,7 @@ export const useNewBasketPanel = ({ const columnMap = buildColumnMap(basketSchema.columns); const [basketName, setBasketName] = useState(""); const [basketId, setBasketId] = useState(); + const saveButtonRef = useRef(null); const saveBasket = useCallback(() => { if (basketName && basketId) { @@ -76,6 +77,9 @@ export const useNewBasketPanel = ({ const basketId = row[KEY] as string; console.log({ basketId, columnMap }); setBasketId(basketId); + setTimeout(() => { + saveButtonRef.current?.focus(); + }, 60); }, [columnMap] ); @@ -105,5 +109,6 @@ export const useNewBasketPanel = ({ onSave: saveBasket, onSelectBasket: handleSelectBasket, saveButtonDisabled: basketName === "" || basketId === undefined, + saveButtonRef, }; }; diff --git a/vuu-ui/sample-apps/feature-basket-trading/src/useBasketContextMenus.ts b/vuu-ui/sample-apps/feature-basket-trading/src/useBasketContextMenus.ts index 2a02733e10..b370a1d1bc 100644 --- a/vuu-ui/sample-apps/feature-basket-trading/src/useBasketContextMenus.ts +++ b/vuu-ui/sample-apps/feature-basket-trading/src/useBasketContextMenus.ts @@ -43,10 +43,11 @@ export const useBasketContextMenus = ({ content: { type: "InstrumentSearch", props: { + TableProps: { + allowDragDrop: "drag-copy", + id: "basket-instruments", + }, dataSource: dataSourceInstruments, - // columnName: action.column.name, - // onConfigChange, - // tableConfig, }, }, title: "Add Ticker", @@ -56,5 +57,5 @@ export const useBasketContextMenus = ({ return false; }, ]; - }, []); + }, [dataSourceInstruments, dispatchLayoutAction]); }; diff --git a/vuu-ui/sample-apps/feature-basket-trading/src/useBasketTrading.tsx b/vuu-ui/sample-apps/feature-basket-trading/src/useBasketTrading.tsx index ad24129237..b6f8f2035f 100644 --- a/vuu-ui/sample-apps/feature-basket-trading/src/useBasketTrading.tsx +++ b/vuu-ui/sample-apps/feature-basket-trading/src/useBasketTrading.tsx @@ -10,6 +10,7 @@ import { NewBasketPanel } from "./new-basket-panel"; import { useBasketContextMenus } from "./useBasketContextMenus"; import { useBasketTradingDataSources } from "./useBasketTradingDatasources"; import { BasketTradingFeatureProps } from "./VuuBasketTradingFeature"; +import { VuuDataRow, VuuDataRowDto } from "packages/vuu-protocol-types"; export class Basket { basketId: string; @@ -28,7 +29,7 @@ export class Basket { this.basketName = data[columnMap.basketName] as string; this.filledPct = data[columnMap.filledPct] as number; this.fxRateToUsd = data[columnMap.fxRateToUsd] as number; - this.side = "BUY"; + this.side = data[columnMap.side] as string; this.totalNotional = data[columnMap.totalNotional] as number; this.totalNotionalUsd = data[columnMap.totalNotionalUsd] as number; this.units = data[columnMap.units] as number; @@ -43,6 +44,13 @@ export type BasketTradingHookProps = Pick< | "instrumentsSchema" >; +const toDataDto = (dataSourceRow: VuuDataRow, columnMap: ColumnMap) => { + Object.entries(columnMap).reduce((dto, [colName, index]) => { + dto[colName] = dataSourceRow[index]; + return dto; + }, {}); +}; + type BasketState = { basketInstanceId?: string; dialog?: JSX.Element; @@ -89,10 +97,14 @@ export const useBasketTrading = ({ dialog: undefined, }); - const columnMap = useMemo( + const columnMapBasketTrading = useMemo( () => buildColumnMap(dataSourceBasketTradingControl.columns), [dataSourceBasketTradingControl.columns] ); + const columnMapInstrument = useMemo( + () => buildColumnMap(dataSourceInstruments.columns), + [dataSourceInstruments.columns] + ); useMemo(() => { dataSourceBasketTradingControl.subscribe( @@ -105,7 +117,10 @@ export const useBasketTrading = ({ setBasketCount(message.size); } if (message.rows && message.rows.length > 0) { - setBasket(new Basket(message.rows[0], columnMap)); + const basket = new Basket(message.rows[0], columnMapBasketTrading); + console.log({ basket, row: message.rows[0] }); + + setBasket(new Basket(message.rows[0], columnMapBasketTrading)); } } } @@ -115,7 +130,7 @@ export const useBasketTrading = ({ setTimeout(() => { setBasketCount((count) => (count === -1 ? 0 : count)); }, 800); - }, [columnMap, dataSourceBasketTradingControl]); + }, [columnMapBasketTrading, dataSourceBasketTradingControl]); useEffect(() => { return () => { @@ -188,6 +203,7 @@ export const useBasketTrading = ({ const handleCommitBasketChange = useCallback( (columnName, value) => { if (basket) { + console.log(`handleCommitBasketChange ${columnName} => ${value}`); const { dataSourceRow } = basket; return dataSourceBasketTradingControl.applyEdit( dataSourceRow, @@ -226,6 +242,38 @@ export const useBasketTrading = ({ menuBuilder: buildViewserverMenuOptions, }; + const handleDropInstrument = useCallback( + (dragDropState) => { + console.log(`useBasketTrading handleDropInstrument`, { + instrument: dragDropState.payload, + }); + const key = "steve-00001.AAA.L"; + const data = { + algo: -1, + algoParams: "", + basketId: ".FTSE100", + description: "Test", + instanceId: "steve-00001", + instanceIdRic: "steve-00001.AAA.L", + limitPrice: 0, + notionalLocal: 0, + notionalUsd: 0, + pctFilled: 0, + priceSpread: 0, + priceStrategyId: 2, + quantity: 0, + ric: "AAL.L", + side: "BUY", + venue: "", + weighting: 1, + }; + dataSourceBasketTradingControl.insertRow?.(key, data).then((response) => { + console.log({ response }); + }); + }, + [dataSourceBasketTradingControl] + ); + return { ...basketState, activeTabIndex, @@ -237,6 +285,7 @@ export const useBasketTrading = ({ dataSourceBasketTradingConstituentJoin, onClickAddBasket: handleAddBasket, onCommitBasketChange: handleCommitBasketChange, + onDropInstrument: handleDropInstrument, onSendToMarket, onTakeOffMarket, }; diff --git a/vuu-ui/scripts/esbuild.mjs b/vuu-ui/scripts/esbuild.mjs index 392ea30a79..254020925a 100644 --- a/vuu-ui/scripts/esbuild.mjs +++ b/vuu-ui/scripts/esbuild.mjs @@ -26,6 +26,8 @@ export async function build(config) { define: { "process.env.NODE_ENV": `"${env}"`, "process.env.NODE_DEBUG": `false`, + "process.env.LOCAL": `true`, + "process.env.LAYOUT_BASE_URL": `"http://127.0.0.1:8081/api"`, }, external, footer, diff --git a/vuu-ui/showcase/src/examples/Apps/NewTheme.examples.tsx b/vuu-ui/showcase/src/examples/Apps/NewTheme.examples.tsx index 8a317b3aca..29f89b913a 100644 --- a/vuu-ui/showcase/src/examples/Apps/NewTheme.examples.tsx +++ b/vuu-ui/showcase/src/examples/Apps/NewTheme.examples.tsx @@ -15,9 +15,10 @@ import { ColumnSettingsPanel, TableSettingsPanel, } from "@finos/vuu-table-extras"; -import { CSSProperties } from "react"; +import { CSSProperties, useMemo } from "react"; import { FilterTableFeatureProps } from "feature-vuu-filter-table"; import { getAllSchemas } from "@finos/vuu-data-test"; +import { DragDropProvider } from "@finos/vuu-ui-controls"; import "./NewTheme.examples.css"; @@ -97,34 +98,43 @@ const ShellWithNewTheme = () => { const { buildMenuOptions, handleMenuAction } = useLayoutContextMenuItems(setDialogState); + const dragSource = useMemo( + () => ({ + "basket-instruments": { dropTargets: "basket-constituents" }, + }), + [] + ); + return ( - - } - loginUrl={window.location.toString()} - user={user} - style={ - { - "--vuuShell-height": "100vh", - "--vuuShell-width": "100vw", - } as CSSProperties - } - > - {dialog} - + + + } + loginUrl={window.location.toString()} + user={user} + style={ + { + "--vuuShell-height": "100vh", + "--vuuShell-width": "100vw", + } as CSSProperties + } + > + {dialog} + + ); }; diff --git a/vuu-ui/showcase/src/examples/Shell/AppHeader.examples.tsx b/vuu-ui/showcase/src/examples/Shell/AppHeader.examples.tsx index d4b0b82bb0..fe5840d81f 100644 --- a/vuu-ui/showcase/src/examples/Shell/AppHeader.examples.tsx +++ b/vuu-ui/showcase/src/examples/Shell/AppHeader.examples.tsx @@ -3,6 +3,12 @@ import { AppHeader } from "@finos/vuu-shell"; let displaySequence = 1; export const DefaultAppHeader = () => { - return ; + return ( + console.log("onNavigate")} + /> + ); }; DefaultAppHeader.displaySequence = displaySequence++; diff --git a/vuu-ui/showcase/src/examples/Shell/LoginPanel.examples.tsx b/vuu-ui/showcase/src/examples/Shell/LoginPanel.examples.tsx index 29dda3843e..915720f71c 100644 --- a/vuu-ui/showcase/src/examples/Shell/LoginPanel.examples.tsx +++ b/vuu-ui/showcase/src/examples/Shell/LoginPanel.examples.tsx @@ -1,5 +1,5 @@ import { LoginPanel } from "@finos/vuu-shell"; export const DefaultLoginPanel = () => { - return ; + return console.log("onSubmit")} />; }; diff --git a/vuu-ui/showcase/src/examples/Table/TableNext.examples.tsx b/vuu-ui/showcase/src/examples/Table/TableNext.examples.tsx index f0fc3130bc..1a86171388 100644 --- a/vuu-ui/showcase/src/examples/Table/TableNext.examples.tsx +++ b/vuu-ui/showcase/src/examples/Table/TableNext.examples.tsx @@ -3,10 +3,11 @@ import { FlexboxLayout, LayoutProvider, registerComponent, + Toolbar, View, } from "@finos/vuu-layout"; import { ContextPanel } from "@finos/vuu-shell"; -import { TableNext } from "@finos/vuu-table"; +import { TableNext, TableProps } from "@finos/vuu-table"; import { ColumnSettingsPanel, TableSettingsPanel, @@ -15,42 +16,104 @@ import { ColumnDescriptor, TableConfig } from "@finos/vuu-datagrid-types"; import { CSSProperties, useCallback, useMemo, useState } from "react"; import { useTableConfig, useTestDataSource } from "../utils"; import { GroupHeaderCellNext } from "@finos/vuu-table"; -import { getAllSchemas } from "@finos/vuu-data-test"; +import { + getAllSchemas, + getSchema, + SimulTableName, + vuuModule, +} from "@finos/vuu-data-test"; import "./TableNext.examples.css"; +import { Button } from "@salt-ds/core"; let displaySequence = 1; export const NavigationStyle = () => { - const { - typeaheadHook: _, - config: configProp, - ...props - } = useTableConfig({ - rangeChangeRowset: "full", - table: { module: "SIMUL", table: "instruments" }, - }); - - const [config, setConfig] = useState(configProp); + const tableProps = useMemo>(() => { + const tableName: SimulTableName = "instruments"; + return { + config: { + columns: getSchema(tableName).columns, + rowSeparators: true, + zebraStripes: true, + }, + dataSource: + vuuModule("SIMUL").createDataSource(tableName), + }; + }, []); - const handleConfigChange = useCallback((config: TableConfig) => { - setConfig(config); + const onSelect = useCallback((row) => { + console.log({ row }); + }, []); + const onSelectionChange = useCallback((selected) => { + console.log({ selected }); }, []); return ( ); }; NavigationStyle.displaySequence = displaySequence++; +export const ControlledNavigation = () => { + const tableProps = useMemo>(() => { + const tableName: SimulTableName = "instruments"; + return { + config: { + columns: getSchema(tableName).columns, + rowSeparators: true, + zebraStripes: true, + }, + dataSource: + vuuModule("SIMUL").createDataSource(tableName), + }; + }, []); + const [highlightedIndex, setHighlightedIndex] = useState(-1); + + const handlePrevClick = useCallback(() => { + setHighlightedIndex((idx) => Math.max(0, idx - 1)); + }, []); + + const handleNextClick = useCallback(() => { + setHighlightedIndex((idx) => idx + 1); + }, []); + + const handleHighlight = useCallback((idx: number) => { + setHighlightedIndex(idx); + }, []); + + return ( + <> + + + + + + + ); +}; +ControlledNavigation.displaySequence = displaySequence++; + export const EditableTableNextArrayData = () => { const { config, dataSource } = useTableConfig({ columnConfig: { @@ -209,19 +272,10 @@ export const TableNextInLayoutWithContextPanel = () => { table: { module: "SIMUL", table: "instruments" }, }); - const handleConfigChange = useCallback((tableConfig: TableConfig) => { - console.log("config changed"); - }, []); - return ( - + @@ -239,11 +293,7 @@ export const AutoTableNext = () => { table: { module: "SIMUL", table: "instruments" }, }); - const [config, setConfig] = useState(configProp); - - const handleConfigChange = (config: TableConfig) => { - setConfig(config); - }; + const [config] = useState(configProp); return ( { config={{ ...config, }} - onConfigChange={handleConfigChange} renderBufferSize={0} /> ); diff --git a/vuu-ui/showcase/src/examples/UiControls/DragDrop.examples.tsx b/vuu-ui/showcase/src/examples/UiControls/DragDrop.examples.tsx index 4552428332..90acc57d00 100644 --- a/vuu-ui/showcase/src/examples/UiControls/DragDrop.examples.tsx +++ b/vuu-ui/showcase/src/examples/UiControls/DragDrop.examples.tsx @@ -18,7 +18,7 @@ export const DraggableListsOneWayDrag = () => { ); const dragSource = useMemo( () => ({ - list1: { dropTargets: "list2" }, + list1: { dropTargets: ["list1", "list2"] }, }), [] ); @@ -98,6 +98,7 @@ export const DraggableListsOneWayDrag = () => { { onDragStart={handleDragStart1} onMoveListItem={handleMoveListItem1} source={state1} - allowDragDrop + width={200} />
{ onDrop={handleDrop2} onMoveListItem={handleMoveListItem2} source={state2} - allowDragDrop + width={200} /> diff --git a/vuu-ui/showcase/src/examples/UiControls/InstrumentPicker.examples.tsx b/vuu-ui/showcase/src/examples/UiControls/InstrumentPicker.examples.tsx index 0eed499d66..d56eb2e5a8 100644 --- a/vuu-ui/showcase/src/examples/UiControls/InstrumentPicker.examples.tsx +++ b/vuu-ui/showcase/src/examples/UiControls/InstrumentPicker.examples.tsx @@ -1,48 +1,57 @@ import { InstrumentPicker } from "@finos/vuu-ui-controls"; import { - createArrayDataSource, getAllSchemas, getSchema, + SimulTableName, + vuuModule, } from "@finos/vuu-data-test"; import { buildColumnMap, ColumnMap } from "@finos/vuu-utils"; import { useCallback, useMemo } from "react"; import { TableProps, TableRowSelectHandler } from "@finos/vuu-table"; import { ColumnDescriptor } from "@finos/vuu-datagrid-types"; import { useTestDataSource } from "../utils"; +import { DataSourceRow } from "packages/vuu-data-types"; let displaySequence = 0; export const DefaultInstrumentPicker = () => { - const schema = getSchema("instruments"); - const [columnMap, searchColumns, tableProps] = useMemo< - [ColumnMap, string[], Pick] - >( - () => [ - buildColumnMap(schema.columns), - ["bbg", "description"], + const tableName: SimulTableName = "instruments"; + const schema = getSchema(tableName); + + const [tableProps, columnMap, searchColumns] = useMemo< + [Pick, ColumnMap, string[]] + >(() => { + return [ { config: { - // TODO need to inject this value - showHighlightedRow: true, - columns: [ - { name: "bbg", serverDataType: "string" }, - { name: "description", serverDataType: "string", width: 280 }, - ] as ColumnDescriptor[], + columns: schema.columns, + rowSeparators: true, + zebraStripes: true, }, - dataSource: createArrayDataSource({ table: schema.table }), + dataSource: + vuuModule("SIMUL").createDataSource(tableName), }, - ], - [schema] + buildColumnMap(schema.columns), + ["bbg", "description"], + ]; + }, [schema.columns]); + + const itemToString = useCallback( + (row: DataSourceRow) => { + return [row[columnMap.description]]; + }, + [columnMap.description] ); - const handleSelect = useCallback((row) => { - console.log(`row selected ${row.join(",")}`); + const handleSelect = useCallback((index) => { + console.log(`row selected ${index}`); }, []); return ( { - const { dataSource } = useTableConfig({ - dataSourceConfig: { - columns: ["bbg", "description"], - }, - table: { module: "SIMUL", table: "instruments" }, - }); + const dataSource = useMemo( + () => vuuModule("SIMUL").createDataSource("instruments"), + [] + ); return ( @@ -22,10 +35,8 @@ export const DefaultInstrumentSearch = () => { DefaultInstrumentSearch.displaySequence = displaySequence++; export const InstrumentSearchVuuInstruments = () => { - const schemas = getAllSchemas(); const { dataSource, error } = useTestDataSource({ - // bufferSize: 1000, - schemas, + schemas: getAllSchemas(), }); if (error) { @@ -42,3 +53,74 @@ export const InstrumentSearchVuuInstruments = () => { }; InstrumentSearchVuuInstruments.displaySequence = displaySequence++; + +type DropTargetProps = HTMLAttributes; +const DropTarget = ({ id, ...htmlAttributes }: DropTargetProps) => { + const [instrument, setInstrument] = useState(); + const { isDragSource, isDropTarget, register } = useDragDropProvider(id); + + console.log( + `DropTarget isDragSource ${isDragSource} isDropTarget ${isDropTarget}` + ); + + const acceptDrop = useCallback((dragState) => { + console.log({ payload: dragState.payload }); + setInstrument(dragState.payload as DataSourceRow); + }, []); + + useEffect(() => { + if (id && (isDragSource || isDropTarget)) { + register(id, false, acceptDrop); + } + }, [acceptDrop, id, isDragSource, isDropTarget, register]); + + return ( +
+ {instrument ? ( + <> + {instrument[8]} + - + {instrument[10]} + + ) : null} +
+ ); +}; + +export const InstrumentSearchDragDrop = () => { + const dataSource = useMemo( + () => vuuModule("SIMUL").createDataSource("instruments"), + [] + ); + + const dragSource = useMemo( + () => ({ + "source-table": { dropTargets: "drop-target" }, + }), + [] + ); + + const handleDragStart = useCallback(() => { + console.log("DragStart"); + }, []); + + return ( + + + + + + + ); +}; + +InstrumentSearchDragDrop.displaySequence = displaySequence++; diff --git a/vuu-ui/showcase/src/examples/VuuFeatures/BasketSelector.examples.tsx b/vuu-ui/showcase/src/examples/VuuFeatures/BasketSelector.examples.tsx index c83bdbe808..7d40a79079 100644 --- a/vuu-ui/showcase/src/examples/VuuFeatures/BasketSelector.examples.tsx +++ b/vuu-ui/showcase/src/examples/VuuFeatures/BasketSelector.examples.tsx @@ -1,21 +1,91 @@ import { BasketSelector } from "feature-basket-trading"; -import { useCallback, useState } from "react"; +import { useCallback, useMemo } from "react"; import { vuuModule } from "@finos/vuu-data-test"; import { Basket } from "feature-basket-trading"; +import { ArrayDataSource } from "@finos/vuu-data"; +import { createBasketTradingRow } from "@finos/vuu-data-test"; let displaySequence = 1; +const testBaskets = [ + ["Amber-0001", "Amber Basket", "OFF MARKET", "BUY"], + ["Blue-0002", "Blue Basket", "ON MARKET", "SELL"], + ["Charcoal-0003", "Charcoal Basket", "OFF MARKET", "BUY"], + ["Dandruff-0004", "Dandruff Basket", "ON MARKET", "BUY"], + ["Elephant-0005", "Elephant Basket", "OFF MARKET", "SELL"], + ["Frogger-0006", "Frogger Basket", "OFF MARKET", "BUY"], + ["Gray-0007", "Gray Basket", "ON MARKET", "SELL"], + ["Helium-0008", "Helium Basket", "OFF MARKET", "BUY"], + ["Indigo-0009", "Indigo Basket", "OFF MARKET", "BUY"], +]; + export const DefaultBasketSelector = () => { - const [basket] = useState({ - basketId: "basket-001", + const testBasket: Basket = { + dataSourceRow: [] as any, + basketId: ".FTSE", + basketName: "Test Basket", + filledPct: 0, + fxRateToUsd: 1.25, + side: "BUY", + totalNotional: 1000, + totalNotionalUsd: 1000, + units: 120, + }; + + const dataSource = useMemo(() => { + const dataSource = vuuModule("BASKET").createDataSource( + "basketTrading" + ) as ArrayDataSource; + for (const [basketId, basketName, side, status] of testBaskets) { + dataSource["insert"]( + createBasketTradingRow(basketId, basketName, status, side) + ); + } + return dataSource; + }, []); + + const handleClickAddBasket = useCallback(() => { + console.log("Add Basket"); + }, []); + + const handleSelectBasket = useCallback(() => {}, []); + + return ( + + ); +}; +DefaultBasketSelector.displaySequence = displaySequence++; + +export const OpenBasketSelector = () => { + const testBasket: Basket = { + dataSourceRow: [] as any, + basketId: ".FTSE", basketName: "Test Basket", - filledPct: 0.7, - fxRateToUsd: 1.234, - totalNotional: 1_000_123, - totalNotionalUsd: 1_234_000, - units: 100, - }); - const dataSource = vuuModule("BASKET").createDataSource("basketTrading"); + filledPct: 0, + fxRateToUsd: 1.25, + side: "BUY", + totalNotional: 1000, + totalNotionalUsd: 1000, + units: 120, + }; + + const dataSource = useMemo(() => { + const dataSource = vuuModule("BASKET").createDataSource( + "basketTrading" + ) as ArrayDataSource; + for (const [basketId, basketName, side, status] of testBaskets) { + dataSource["insert"]( + createBasketTradingRow(basketId, basketName, status, side) + ); + } + return dataSource; + }, []); const handleClickAddBasket = useCallback(() => { console.log("Add Basket"); @@ -27,12 +97,13 @@ export const DefaultBasketSelector = () => { return ( ); }; -DefaultBasketSelector.displaySequence = displaySequence++; +OpenBasketSelector.displaySequence = displaySequence++; diff --git a/vuu-ui/showcase/src/examples/html/html-table-components/Row.tsx b/vuu-ui/showcase/src/examples/html/html-table-components/Row.tsx index 135547afbb..437b770b9b 100644 --- a/vuu-ui/showcase/src/examples/html/html-table-components/Row.tsx +++ b/vuu-ui/showcase/src/examples/html/html-table-components/Row.tsx @@ -1,6 +1,8 @@ import { DataSourceRow } from "@finos/vuu-data-types"; -import { KeyedColumnDescriptor } from "@finos/vuu-datagrid-types"; -import { RowClickHandler } from "@finos/vuu-table"; +import { + KeyedColumnDescriptor, + RowClickHandler, +} from "@finos/vuu-datagrid-types"; import { ColumnMap, isGroupColumn, metadataKeys } from "@finos/vuu-utils"; import { CSSProperties, memo, MouseEvent, useCallback } from "react"; import { TableCell } from "./TableCell"; diff --git a/vuu-ui/showcase/src/examples/html/html-table-components/vuu-table/Row.tsx b/vuu-ui/showcase/src/examples/html/html-table-components/vuu-table/Row.tsx index 1c35b701b1..f2a9c6b091 100644 --- a/vuu-ui/showcase/src/examples/html/html-table-components/vuu-table/Row.tsx +++ b/vuu-ui/showcase/src/examples/html/html-table-components/vuu-table/Row.tsx @@ -1,6 +1,8 @@ import { DataSourceRow } from "@finos/vuu-data-types"; -import { KeyedColumnDescriptor } from "@finos/vuu-datagrid-types"; -import { RowClickHandler } from "@finos/vuu-table"; +import { + KeyedColumnDescriptor, + RowClickHandler, +} from "@finos/vuu-datagrid-types"; import { ColumnMap, isGroupColumn, diff --git a/vuu-ui/showcase/vite.config.js b/vuu-ui/showcase/vite.config.js index 1b58591527..a54edf443d 100644 --- a/vuu-ui/showcase/vite.config.js +++ b/vuu-ui/showcase/vite.config.js @@ -9,6 +9,7 @@ export default defineConfig({ define: { "process.env.NODE_DEBUG": false, "process.env.LOCAL": true, + "process.env.LAYOUT_BASE_URL": `"http://127.0.0.1:8081/api"`, }, esbuild: { jsx: `automatic`, diff --git a/vuu/src/main/resources/runconfigurations/SimulMain.run.xml b/vuu/src/main/resources/runconfigurations/SimulMain.run.xml index a9e6b0c2f2..ea5a58eae3 100644 --- a/vuu/src/main/resources/runconfigurations/SimulMain.run.xml +++ b/vuu/src/main/resources/runconfigurations/SimulMain.run.xml @@ -1,6 +1,6 @@ -