* Installs action handler for the specified element. The action handler is * responsible for checking invocation trust. * * For AMP elements, use base-element.registerAction() instead. * * @param {!Element} target * @param {ActionHandlerDef} handler
(target, handler)
| 448 | * @param {ActionHandlerDef} handler |
| 449 | */ |
| 450 | installActionHandler(target, handler) { |
| 451 | // TODO(dvoytenko, #7063): switch back to `target.id` with form proxy. |
| 452 | const targetId = target.getAttribute('id') || ''; |
| 453 | |
| 454 | devAssert( |
| 455 | isAmpTagName(targetId) || |
| 456 | target.tagName.toLowerCase() in NON_AMP_ELEMENTS_ACTIONS_, |
| 457 | 'AMP or special element expected: %s', |
| 458 | target.tagName + '#' + targetId |
| 459 | ); |
| 460 | |
| 461 | if (target[ACTION_HANDLER_]) { |
| 462 | dev().error(TAG_, `Action handler already installed for ${target}`); |
| 463 | return; |
| 464 | } |
| 465 | target[ACTION_HANDLER_] = handler; |
| 466 | |
| 467 | /** @const {Array<!ActionInvocation>} */ |
| 468 | const queuedInvocations = target[ACTION_QUEUE_]; |
| 469 | if (isArray(queuedInvocations)) { |
| 470 | // Invoke and clear all queued invocations now handler is installed. |
| 471 | Services.timerFor(getWin(target)).delay(() => { |
| 472 | // TODO(dvoytenko, #1260): dedupe actions. |
| 473 | queuedInvocations.forEach((invocation) => { |
| 474 | try { |
| 475 | handler(invocation); |
| 476 | } catch (e) { |
| 477 | dev().error(TAG_, 'Action execution failed:', invocation, e); |
| 478 | } |
| 479 | }); |
| 480 | target[ACTION_QUEUE_].length = 0; |
| 481 | }, 1); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Checks if the given element has registered a particular action type. |
no test coverage detected