packages/js-toolkit/abstracts/Base/events.js
/**
 * Bind event handler methods to refs HTML element.
 *
 * @param  {Base}  instance     A Base instance.
 * @param  {Array} eventMethods A list of methods to bind.
 * @return {Array}              A list of unbind functions.
 */
Bind event handler methods to refs HTML element.
@param  {Base}  instance     A Base instance.
@param  {Array} eventMethods A list of methods to bind.
@return {Array}              A list of unbind functions.
Bind event handler methods to refs HTML element . @param { Base } instance A Base instance . @param { Array } eventMethods A list of methods to bind . @return { Array } A list of unbind functions .
function bindRefsEvents(instance, eventMethods) {
  const unbindMethods = [];
  Object.entries(instance.$refs).forEach(([refName, $refOrRefs]) => {
    const $refs = Array.isArray($refOrRefs) ? $refOrRefs : [$refOrRefs];
    const refEventMethod = `on${refName.replace(/^\w/, (c) => c.toUpperCase())}`;
    eventMethods
      .filter((eventMethod) => eventMethod.startsWith(refEventMethod))
      .forEach((eventMethod) => {
        $refs.forEach(($ref, index) => {
          const eventName = eventMethod.replace(refEventMethod, '').toLowerCase();
          const handler = (...args) => {
            if (__DEV__) {
              debug(instance, eventMethod, $ref, ...args, index);
            }
            instance[eventMethod](...args, index);
          };
          if (__DEV__) {
            debug(instance, 'binding ref event', refName, eventName);
          }
          if ($ref instanceof instance.constructor) {
            $ref = $ref.$el; 
          }
           ($ref).addEventListener(eventName, handler);
          const unbindMethod = () => {
            if (__DEV__) {
              debug(instance, 'unbinding ref event', refName, eventMethod);
            }
             ($ref).removeEventListener(eventName, handler);
          };
          unbindMethods.push(unbindMethod);
        });
      });
  });
  return unbindMethods;
}
function bindRefsEvents ( instance , eventMethods ) { const unbindMethods = [ ] ; Object . entries ( instance . $refs ) . forEach ( ( [ refName , $refOrRefs ] ) => { const $refs = Array . isArray ( $refOrRefs ) ? $refOrRefs : [ $refOrRefs ] ; const refEventMethod = ` ${ refName . replace ( / ^\w / , ( c ) => c . toUpperCase ( ) ) } ` ; eventMethods . filter ( ( eventMethod ) => eventMethod . startsWith ( refEventMethod ) ) . forEach ( ( eventMethod ) => { $refs . forEach ( ( $ref , index ) => { const eventName = eventMethod . replace ( refEventMethod , '' ) . toLowerCase ( ) ; const handler = ( ... args ) => { if ( __DEV__ ) { debug ( instance , eventMethod , $ref , ... args , index ) ; } instance [ eventMethod ] ( ... args , index ) ; } ; if ( __DEV__ ) { debug ( instance , 'binding ref event' , refName , eventName ) ; } if ( $ref instanceof instance . constructor ) { $ref = $ref . $el ; } ( $ref ) . addEventListener ( eventName , handler ) ; const unbindMethod = ( ) => { if ( __DEV__ ) { debug ( instance , 'unbinding ref event' , refName , eventMethod ) ; } ( $ref ) . removeEventListener ( eventName , handler ) ; } ; unbindMethods . push ( unbindMethod ) ; } ) ; } ) ; } ) ; return unbindMethods ; }
Bind event handler methods to refs HTML element.
Bind event handler methods to refs HTML element .
// @ts-ignore // eslint-disable-line no-param-reassign /** @type {HTMLElement} */ /** @type {HTMLElement} */