Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit b806be7

Browse files
authored
[FEAT] Add new API to add an handler on the grid (#15)
1 parent 110d961 commit b806be7

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ If it's necessary (because the exercise is too long), no need to implement all t
6161
<p align="center">
6262
<img title="Token" src="images/token.png">
6363
</p>
64-
65-
- Add a new api method that adds a handler on click on square. \
66-
It should be called with: `<library object>.addHandler(squareLabel, (event) => {....});`
6764

6865
- Add one or several new api methods that let know if a line or a column is completed. The number of the squares that are
6966
in the row or in the column should be returned for a latter use. \

library/ts-rollup/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ The result should be something like this:
3131
<img title="Grid" src="../../images/background-color.png">
3232
</p>
3333

34+
### Register a handler on the grid
35+
```javascript
36+
bingo.registerHandler(cell => {
37+
alert(`You click on square ${cell.value}`);
38+
});
39+
```
40+
41+
This API could be useful for the implementation of the demonstrators. \
42+
The current handler parameter may not be enough or the wrong one. You are free to adapt it.
43+
3444
## Technical part
3545

3646
The project is configured with Jest for the tests, Playwright & Jest-image-snapshot for the visual tests, ESLint for the code formatting.

library/ts-rollup/dev/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ updateBackground.onchange = () => {
2424
bingo.updateBackgroundColor(updateBackground.value, 'Pink');
2525
updateBackground.value = ''; // reset
2626
};
27+
28+
// register handler on grid
29+
bingo.registerHandler(cell => {
30+
alert(`You click on square ${cell.value}`);
31+
});

library/ts-rollup/src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BingoGraph } from './graph';
22
import { mxgraph } from './initializer';
3+
import { mxCell } from 'mxgraph';
34

45
const titleHeight = 70;
56

@@ -87,6 +88,20 @@ export class Bingo {
8788
const newStyle = `${style};${mxgraph.mxConstants.STYLE_FILLCOLOR}=${fillColor}`;
8889
this.graph.getModel().setStyle(cell, newStyle);
8990
}
91+
92+
registerHandler(handler: (cell: mxCell) => void): void {
93+
this.graph.addListener(mxgraph.mxEvent.CLICK, (sender, event) => {
94+
const cell = event.getProperty('cell'); // cell may be null
95+
if (cell == null) {
96+
return;
97+
}
98+
99+
if (handler) {
100+
handler(cell);
101+
event.consume();
102+
}
103+
});
104+
}
90105
}
91106

92107
// naive implementation, doesn't prevent duplicates in grid

0 commit comments

Comments
 (0)