LCOV - code coverage report
Current view: top level - pkg/lib/cockpit/_internal - event-mixin.js Coverage Total Hit
Test: cockpit Lines: 100.0 % 51 51
Test Date: 2026-07-13 10:00:01

            Line data    Source code
       1              : // SPDX-License-Identifier: LGPL-2.1-or-later
       2              : import { is_function, invoke_functions } from './common';
       3              : 
       4              : /*
       5              :  * Extends an object to have the standard DOM style addEventListener
       6              :  * removeEventListener and dispatchEvent methods. The dispatchEvent
       7              :  * method has the additional capability to create a new event from a type
       8              :  * string and arguments.
       9              :  */
      10          368 : export function event_mixin(obj, handlers) {
      11          368 :     Object.defineProperties(obj, {
      12          368 :         addEventListener: {
      13          368 :             enumerable: false,
      14          368 :             value: function addEventListener(type, handler) {
      15          368 :                 if (handlers[type] === undefined)
      16          368 :                     handlers[type] = [];
      17          368 :                 handlers[type].push(handler);
      18          368 :             }
      19          368 :         },
      20          368 :         removeEventListener: {
      21          368 :             enumerable: false,
      22          385 :             value: function removeEventListener(type, handler) {
      23          352 :                 const length = handlers[type] ? handlers[type].length : 0;
      24          385 :                 for (let i = 0; i < length; i++) {
      25          385 :                     if (handlers[type][i] === handler) {
      26          385 :                         handlers[type][i] = null;
      27          385 :                         break;
      28          385 :                     }
      29          385 :                 }
      30          385 :             }
      31          368 :         },
      32          368 :         dispatchEvent: {
      33          368 :             enumerable: false,
      34          399 :             value: function dispatchEvent(event) {
      35          399 :                 let type, args;
      36          399 :                 if (typeof event === "string") {
      37          399 :                     type = event;
      38          399 :                     args = Array.prototype.slice.call(arguments, 1);
      39              : 
      40          399 :                     let detail = null;
      41          399 :                     if (arguments.length == 2)
      42          399 :                         detail = arguments[1];
      43          399 :                     else if (arguments.length > 2)
      44          392 :                         detail = args;
      45              : 
      46          399 :                     event = new CustomEvent(type, {
      47          399 :                         bubbles: false,
      48          399 :                         cancelable: false,
      49          399 :                         detail
      50          399 :                     });
      51              : 
      52          399 :                     args.unshift(event);
      53           98 :                 } else {
      54           98 :                     type = event.type;
      55           98 :                     args = arguments;
      56           98 :                 }
      57          399 :                 if (is_function(obj['on' + type]))
      58           98 :                     obj['on' + type].apply(obj, args);
      59          399 :                 invoke_functions(handlers[type], obj, args);
      60          399 :             }
      61          368 :         }
      62          368 :     });
      63          368 : }
        

Generated by: LCOV version 2.0-1