(notification: Notification, action?: string)
| 424 | } |
| 425 | |
| 426 | private async handleClick(notification: Notification, action?: string): Promise<void> { |
| 427 | notification.close(); |
| 428 | |
| 429 | const options: {-readonly [K in keyof Notification]?: Notification[K]} = {}; |
| 430 | // The filter uses `name in notification` because the properties are on the prototype so |
| 431 | // hasOwnProperty does not work here |
| 432 | NOTIFICATION_OPTION_NAMES.filter((name) => name in notification).forEach( |
| 433 | (name) => (options[name] = notification[name]), |
| 434 | ); |
| 435 | |
| 436 | const notificationAction = action === '' || action === undefined ? 'default' : action; |
| 437 | |
| 438 | const onActionClick = notification?.data?.onActionClick?.[notificationAction]; |
| 439 | |
| 440 | const urlToOpen = new URL(onActionClick?.url ?? '', this.scope.registration.scope).href; |
| 441 | |
| 442 | switch (onActionClick?.operation) { |
| 443 | case 'openWindow': |
| 444 | await this.scope.clients.openWindow(urlToOpen); |
| 445 | break; |
| 446 | case 'focusLastFocusedOrOpen': { |
| 447 | let matchingClient = await this.getLastFocusedMatchingClient(this.scope); |
| 448 | if (matchingClient) { |
| 449 | await matchingClient?.focus(); |
| 450 | } else { |
| 451 | await this.scope.clients.openWindow(urlToOpen); |
| 452 | } |
| 453 | break; |
| 454 | } |
| 455 | case 'navigateLastFocusedOrOpen': { |
| 456 | let matchingClient = await this.getLastFocusedMatchingClient(this.scope); |
| 457 | if (matchingClient) { |
| 458 | matchingClient = await matchingClient.navigate(urlToOpen); |
| 459 | await matchingClient?.focus(); |
| 460 | } else { |
| 461 | await this.scope.clients.openWindow(urlToOpen); |
| 462 | } |
| 463 | break; |
| 464 | } |
| 465 | case 'sendRequest': { |
| 466 | await this.scope.fetch(urlToOpen); |
| 467 | break; |
| 468 | } |
| 469 | default: |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | await this.broadcast({ |
| 474 | type: 'NOTIFICATION_CLICK', |
| 475 | data: {action, notification: options}, |
| 476 | }); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Handles the closing of a notification by extracting its options and |
no test coverage detected