packages/mdc-auto-init/index.js
/**
 * Auto-initializes all mdc components on a page.
 */
Auto-initializes all mdc components on a page.
Auto - initializes all mdc components on a page .
function mdcAutoInit(root = document, warn = CONSOLE_WARN) {
  const nodes = root.querySelectorAll('[data-mdc-auto-init]');
  for (let i = 0, node; (node = nodes[i]); i++) {
    const ctorName = node.dataset.mdcAutoInit;
    if (!ctorName) {
      throw new Error('(mdc-auto-init) Constructor name must be given.');
    }
    const Ctor = registry[ctorName];
    if (typeof Ctor !== 'function') {
      throw new Error(
        `(mdc-auto-init) Could not find constructor in registry for ${ctorName}`);
    }
    if (node[ctorName]) {
      warn(`(mdc-auto-init) Component already initialized for ${node}. Skipping...`);
      continue;
    }
    const component = Ctor.attachTo(node);
    Object.defineProperty(node, ctorName, {
      value: component,
      writable: false,
      enumerable: false,
      configurable: true,
    });
  }
  _emit('MDCAutoInit:End', {});
}
function mdcAutoInit ( root = document , warn = CONSOLE_WARN ) { const nodes = root . querySelectorAll ( '[data-mdc-auto-init]' ) ; for ( let i = 0 , node ; ( node = nodes [ i ] ) ; i ++ ) { const ctorName = node . dataset . mdcAutoInit ; if ( ! ctorName ) { throw new Error ( '(mdc-auto-init) Constructor name must be given.' ) ; } const Ctor = registry [ ctorName ] ; if ( typeof Ctor !== 'function' ) { throw new Error ( ` ${ ctorName } ` ) ; } if ( node [ ctorName ] ) { warn ( ` ${ node } ` ) ; continue ; } const component = Ctor . attachTo ( node ) ; Object . defineProperty ( node , ctorName , { value : component , writable : false , enumerable : false , configurable : true , } ) ; } _emit ( 'MDCAutoInit:End' , { } ) ; }
Auto-initializes all mdc components on a page.
Auto - initializes all mdc components on a page .
// TODO: Should we make an eslint rule for an attachTo() static method?