Skip to content

Commit c5e9ae7

Browse files
committed
Changing the "addButton" signature to allow an object.
This way, in the future, we can add easily more parameters.
1 parent 273e7e8 commit c5e9ae7

File tree

5 files changed

+54
-36
lines changed

5 files changed

+54
-36
lines changed

docs/maps/api-ui.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -378,15 +378,20 @@ If the user is in mobile definition, the modal will open in full screen:
378378
### Add a button in the action bar
379379
380380
```ts
381-
WA.ui.actionBar.addButton(id: string, label: string, clickCallback: (buttonActionBar: AddButtonActionBar) => void): void
381+
WA.ui.actionBar.addButton(descriptor: {
382+
id: string,
383+
label: string,
384+
clickCallback: (buttonActionBar: AddButtonActionBar) => void
385+
}): void
382386
```
387+
383388
- id: the id of the button action bar defined.
384-
- label: the label to display in button action bar.
385-
- clickCallback: the callback when user click on the button. The callback have `AddButtonActionBar` class in parameter.
389+
- label: the label to display in the button.
390+
- clickCallback: function called when the user clicks on the button. The callback is passed a `AddButtonActionBar` instance in parameter.
386391
387-
The `ButtonActionBar` is defined:
392+
With `AddButtonActionBar` defined as:
388393
```ts
389-
interface AddButtonActionBar{
394+
interface AddButtonActionBar {
390395
/*
391396
* the id of the button action bar defined.
392397
*/
@@ -403,15 +408,19 @@ interface AddButtonActionBar{
403408
```ts
404409
WA.ui.actionBar.removeButton(id: string);
405410
```
406-
- id: the id of the button action bar previously defined.
411+
- id: the id of the action bar button previously defined.
407412
408-
### Example of action bar button
413+
### Example of an action bar button
409414
```ts
410415
// Add action bar button 'Register'.
411-
WA.ui.actionBar.addButton('register-btn', 'Regsiter', (event) => {
412-
console.log('Button registered triggered', event);
413-
// When user click on the action bar button 'Register', we remove it.
414-
WA.ui.actionBar.removeButton('register-btn');
416+
WA.ui.actionBar.addButton({
417+
id: 'register-btn',
418+
label: 'Register',
419+
callback: (event) => {
420+
console.log('Button clicked', event);
421+
// When a user clicks on the action bar button 'Register', we remove it.
422+
WA.ui.actionBar.removeButton('register-btn');
423+
}
415424
});
416425
```
417426
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
console.info('In ten seconds, the tuto modal will be launched')
2-
3-
function launchTuto(){
4-
console.info('Lunch tuto');
5-
WA.ui.actionBar.addButton('register-btn', 'Register', (event) => {
1+
WA.ui.actionBar.addButton({
2+
id: 'register-btn',
3+
label: 'Register',
4+
callback: (event) => {
65
console.log('Button registered triggered', event);
76
WA.ui.actionBar.removeButton('register-btn');
8-
});
9-
}
10-
11-
setTimeout(() => {
12-
launchTuto();
13-
}, 2000)
7+
}
8+
});

maps/tests/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ <h2>Iframe API</h2>
291291
<a href="#" class="testLink" data-testmap="AlterActionMenuApi/map.json" target="_blank">Test Alter ActionMenu API</a>
292292
</td>
293293
</tr>
294+
<tr>
295+
<td>
296+
<input type="radio" name="test-add-action-bar-button"> Success <input type="radio" name="test-add-action-bar-button"> Failure <input type="radio" name="test-add-action-bar-button" checked> Pending
297+
</td>
298+
<td>
299+
<a href="#" class="testLink" data-testmap="MenuActionBar/addButtonActionBar.json" target="_blank">Test Add a button in ActionBar <i>(already automated in E2E tests)</i></a>
300+
</td>
301+
</tr>
294302
<tr>
295303
<td>
296304
<input type="radio" name="test-player-movement-api"> Success <input type="radio" name="test-player-movement-api"> Failure <input type="radio" name="test-player-movement-api" checked> Pending

play/src/front/Api/Iframe/Ui/ButtonActionBar.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { apiCallback } from "../registeredCallbacks";
44

55
export type ButtonActionBarClickedCallback = (buttonActionBar: AddButtonActionBarEvent) => void;
66

7+
export type ActionBarButtonDescriptor = {
8+
id: string;
9+
label: string;
10+
callback?: ButtonActionBarClickedCallback;
11+
};
12+
713
export class WorkAdventureButtonActionBarCommands extends IframeApiContribution<WorkAdventureButtonActionBarCommands> {
814
private _callbacks: Map<string, ButtonActionBarClickedCallback> = new Map<string, ButtonActionBarClickedCallback>();
915

@@ -21,24 +27,20 @@ export class WorkAdventureButtonActionBarCommands extends IframeApiContribution<
2127
/**
2228
* Add action bar button
2329
* {@link http://workadventure.localhost/map-building/api-ui.md#add-action-bar | Website documentation}
24-
*
25-
* @param id
26-
* @param label
27-
* @param callback
2830
*/
29-
addButton(id: string, label: string, callback?: ButtonActionBarClickedCallback) {
30-
if (callback != undefined) this._callbacks.set(id, callback);
31+
addButton(descriptor: ActionBarButtonDescriptor) {
32+
if (descriptor.callback != undefined) {
33+
this._callbacks.set(descriptor.id, descriptor.callback);
34+
}
3135
sendToWorkadventure({
3236
type: "addButtonActionBar",
33-
data: { id, label },
37+
data: { id: descriptor.id, label: descriptor.label },
3438
});
3539
}
3640

3741
/**
3842
* Remove action bar button
3943
* {@link http://workadventure.localhost/map-building/api-ui.md#remove-action-bar | Website documentation}
40-
*
41-
* @param id
4244
*/
4345
removeButton(id: string) {
4446
this._callbacks.delete(id);

tests/tests/buttonactionbar_script.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from '@playwright/test';
1+
import {expect, test} from '@playwright/test';
22
import { login } from './utils/roles';
33
import {evaluateScript} from "./utils/scripting";
44

@@ -14,15 +14,19 @@ test.describe('Button action bar', () => {
1414

1515
// Use script to add new button
1616
await evaluateScript(page, async () => {
17-
return WA.ui.actionBar.addButton('register-btn', 'Register', () => {
18-
WA.ui.actionBar.removeButton('register-btn');
17+
return WA.ui.actionBar.addButton({
18+
id: 'register-btn',
19+
label: 'Register',
20+
callback: () => {
21+
WA.ui.actionBar.removeButton('register-btn');
22+
}
1923
});
2024
});
2125

2226
// Click on the register button
23-
await page.locator("#register-btn").click();
27+
await page.getByText('Register').click();
2428

2529
// Check if the register button is hidden
26-
await page.locator("#register-btn").isHidden();
30+
await expect(page.getByText('Register')).toHaveCount(0);
2731
});
2832
});

0 commit comments

Comments
 (0)