mdc.autoInit.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://github.com/material-components/material-components-web/blob/master/LICENSE
  7. */
  8. (function webpackUniversalModuleDefinition(root, factory) {
  9. if(typeof exports === 'object' && typeof module === 'object')
  10. module.exports = factory();
  11. else if(typeof define === 'function' && define.amd)
  12. define("@material/auto-init", [], factory);
  13. else if(typeof exports === 'object')
  14. exports["auto-init"] = factory();
  15. else
  16. root["mdc"] = root["mdc"] || {}, root["mdc"]["auto-init"] = factory();
  17. })(this, function() {
  18. return /******/ (function(modules) { // webpackBootstrap
  19. /******/ // The module cache
  20. /******/ var installedModules = {};
  21. /******/
  22. /******/ // The require function
  23. /******/ function __webpack_require__(moduleId) {
  24. /******/
  25. /******/ // Check if module is in cache
  26. /******/ if(installedModules[moduleId]) {
  27. /******/ return installedModules[moduleId].exports;
  28. /******/ }
  29. /******/ // Create a new module (and put it into the cache)
  30. /******/ var module = installedModules[moduleId] = {
  31. /******/ i: moduleId,
  32. /******/ l: false,
  33. /******/ exports: {}
  34. /******/ };
  35. /******/
  36. /******/ // Execute the module function
  37. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  38. /******/
  39. /******/ // Flag the module as loaded
  40. /******/ module.l = true;
  41. /******/
  42. /******/ // Return the exports of the module
  43. /******/ return module.exports;
  44. /******/ }
  45. /******/
  46. /******/
  47. /******/ // expose the modules object (__webpack_modules__)
  48. /******/ __webpack_require__.m = modules;
  49. /******/
  50. /******/ // expose the module cache
  51. /******/ __webpack_require__.c = installedModules;
  52. /******/
  53. /******/ // define getter function for harmony exports
  54. /******/ __webpack_require__.d = function(exports, name, getter) {
  55. /******/ if(!__webpack_require__.o(exports, name)) {
  56. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  57. /******/ }
  58. /******/ };
  59. /******/
  60. /******/ // define __esModule on exports
  61. /******/ __webpack_require__.r = function(exports) {
  62. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  63. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  64. /******/ }
  65. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  66. /******/ };
  67. /******/
  68. /******/ // create a fake namespace object
  69. /******/ // mode & 1: value is a module id, require it
  70. /******/ // mode & 2: merge all properties of value into the ns
  71. /******/ // mode & 4: return value when already ns object
  72. /******/ // mode & 8|1: behave like require
  73. /******/ __webpack_require__.t = function(value, mode) {
  74. /******/ if(mode & 1) value = __webpack_require__(value);
  75. /******/ if(mode & 8) return value;
  76. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  77. /******/ var ns = Object.create(null);
  78. /******/ __webpack_require__.r(ns);
  79. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  80. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  81. /******/ return ns;
  82. /******/ };
  83. /******/
  84. /******/ // getDefaultExport function for compatibility with non-harmony modules
  85. /******/ __webpack_require__.n = function(module) {
  86. /******/ var getter = module && module.__esModule ?
  87. /******/ function getDefault() { return module['default']; } :
  88. /******/ function getModuleExports() { return module; };
  89. /******/ __webpack_require__.d(getter, 'a', getter);
  90. /******/ return getter;
  91. /******/ };
  92. /******/
  93. /******/ // Object.prototype.hasOwnProperty.call
  94. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  95. /******/
  96. /******/ // __webpack_public_path__
  97. /******/ __webpack_require__.p = "";
  98. /******/
  99. /******/
  100. /******/ // Load entry module and return exports
  101. /******/ return __webpack_require__(__webpack_require__.s = "./packages/mdc-auto-init/index.ts");
  102. /******/ })
  103. /************************************************************************/
  104. /******/ ({
  105. /***/ "./packages/mdc-auto-init/constants.ts":
  106. /*!*********************************************!*\
  107. !*** ./packages/mdc-auto-init/constants.ts ***!
  108. \*********************************************/
  109. /*! no static exports found */
  110. /***/ (function(module, exports, __webpack_require__) {
  111. "use strict";
  112. /**
  113. * @license
  114. * Copyright 2019 Google Inc.
  115. *
  116. * Permission is hereby granted, free of charge, to any person obtaining a copy
  117. * of this software and associated documentation files (the "Software"), to deal
  118. * in the Software without restriction, including without limitation the rights
  119. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  120. * copies of the Software, and to permit persons to whom the Software is
  121. * furnished to do so, subject to the following conditions:
  122. *
  123. * The above copyright notice and this permission notice shall be included in
  124. * all copies or substantial portions of the Software.
  125. *
  126. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  127. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  128. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  129. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  130. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  131. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  132. * THE SOFTWARE.
  133. */
  134. Object.defineProperty(exports, "__esModule", { value: true });
  135. exports.strings = void 0;
  136. exports.strings = {
  137. AUTO_INIT_ATTR: 'data-mdc-auto-init',
  138. DATASET_AUTO_INIT_STATE: 'mdcAutoInitState',
  139. INITIALIZED_STATE: 'initialized'
  140. };
  141. /***/ }),
  142. /***/ "./packages/mdc-auto-init/index.ts":
  143. /*!*****************************************!*\
  144. !*** ./packages/mdc-auto-init/index.ts ***!
  145. \*****************************************/
  146. /*! no static exports found */
  147. /***/ (function(module, exports, __webpack_require__) {
  148. "use strict";
  149. /**
  150. * @license
  151. * Copyright 2016 Google Inc.
  152. *
  153. * Permission is hereby granted, free of charge, to any person obtaining a copy
  154. * of this software and associated documentation files (the "Software"), to deal
  155. * in the Software without restriction, including without limitation the rights
  156. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  157. * copies of the Software, and to permit persons to whom the Software is
  158. * furnished to do so, subject to the following conditions:
  159. *
  160. * The above copyright notice and this permission notice shall be included in
  161. * all copies or substantial portions of the Software.
  162. *
  163. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  164. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  165. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  166. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  167. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  168. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  169. * THE SOFTWARE.
  170. */
  171. var __values = this && this.__values || function (o) {
  172. var s = typeof Symbol === "function" && Symbol.iterator,
  173. m = s && o[s],
  174. i = 0;
  175. if (m) return m.call(o);
  176. if (o && typeof o.length === "number") return {
  177. next: function next() {
  178. if (o && i >= o.length) o = void 0;
  179. return { value: o && o[i++], done: !o };
  180. }
  181. };
  182. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  183. };
  184. Object.defineProperty(exports, "__esModule", { value: true });
  185. exports.mdcAutoInit = void 0;
  186. var constants_1 = __webpack_require__(/*! ./constants */ "./packages/mdc-auto-init/constants.ts");
  187. var AUTO_INIT_ATTR = constants_1.strings.AUTO_INIT_ATTR,
  188. DATASET_AUTO_INIT_STATE = constants_1.strings.DATASET_AUTO_INIT_STATE,
  189. INITIALIZED_STATE = constants_1.strings.INITIALIZED_STATE;
  190. var registry = {};
  191. // tslint:disable-next-line:no-console
  192. var CONSOLE_WARN = console.warn.bind(console);
  193. function emit(evtType, evtData, shouldBubble) {
  194. if (shouldBubble === void 0) {
  195. shouldBubble = false;
  196. }
  197. var evt;
  198. if (typeof CustomEvent === 'function') {
  199. evt = new CustomEvent(evtType, {
  200. bubbles: shouldBubble,
  201. detail: evtData
  202. });
  203. } else {
  204. evt = document.createEvent('CustomEvent');
  205. evt.initCustomEvent(evtType, shouldBubble, false, evtData);
  206. }
  207. document.dispatchEvent(evt);
  208. }
  209. /* istanbul ignore next: optional argument is not a branch statement */
  210. /**
  211. * Auto-initializes all MDC components on a page.
  212. */
  213. function mdcAutoInit(root) {
  214. var e_1, _a;
  215. if (root === void 0) {
  216. root = document;
  217. }
  218. var components = [];
  219. var nodes = Array.from(root.querySelectorAll("[" + AUTO_INIT_ATTR + "]"));
  220. nodes = nodes.filter(function (node) {
  221. return node.dataset[DATASET_AUTO_INIT_STATE] !== INITIALIZED_STATE;
  222. });
  223. try {
  224. for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
  225. var node = nodes_1_1.value;
  226. var ctorName = node.getAttribute(AUTO_INIT_ATTR);
  227. if (!ctorName) {
  228. throw new Error('(mdc-auto-init) Constructor name must be given.');
  229. }
  230. // tslint:disable-next-line:enforce-name-casing
  231. var Constructor = registry[ctorName];
  232. if (typeof Constructor !== 'function') {
  233. throw new Error("(mdc-auto-init) Could not find constructor in registry for " + ctorName);
  234. }
  235. // TODO: Should we make an eslint rule for an attachTo() static method?
  236. // See https://github.com/Microsoft/TypeScript/issues/14600 for discussion
  237. // of static interface support in TS
  238. var component = Constructor.attachTo(node);
  239. Object.defineProperty(node, ctorName, {
  240. configurable: true,
  241. enumerable: false,
  242. value: component,
  243. writable: false
  244. });
  245. components.push(component);
  246. node.dataset[DATASET_AUTO_INIT_STATE] = INITIALIZED_STATE;
  247. }
  248. } catch (e_1_1) {
  249. e_1 = { error: e_1_1 };
  250. } finally {
  251. try {
  252. if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
  253. } finally {
  254. if (e_1) throw e_1.error;
  255. }
  256. }
  257. emit('MDCAutoInit:End', {});
  258. return components;
  259. }
  260. exports.mdcAutoInit = mdcAutoInit;
  261. // Constructor is PascalCased because it is a direct reference to a class,
  262. // rather than an instance of a class.
  263. mdcAutoInit.register = function (componentName,
  264. // tslint:disable-next-line:enforce-name-casing
  265. Constructor, warn) {
  266. if (warn === void 0) {
  267. warn = CONSOLE_WARN;
  268. }
  269. if (typeof Constructor !== 'function') {
  270. throw new Error("(mdc-auto-init) Invalid Constructor value: " + Constructor + ". Expected function.");
  271. }
  272. var registryValue = registry[componentName];
  273. if (registryValue) {
  274. warn("(mdc-auto-init) Overriding registration for " + componentName + " with " + Constructor + ". Was: " + registryValue);
  275. }
  276. registry[componentName] = Constructor;
  277. };
  278. mdcAutoInit.deregister = function (componentName) {
  279. delete registry[componentName];
  280. };
  281. /** @nocollapse */
  282. mdcAutoInit.deregisterAll = function () {
  283. var e_2, _a;
  284. try {
  285. for (var _b = __values(Object.keys(registry)), _c = _b.next(); !_c.done; _c = _b.next()) {
  286. var componentName = _c.value;
  287. mdcAutoInit.deregister(componentName);
  288. }
  289. } catch (e_2_1) {
  290. e_2 = { error: e_2_1 };
  291. } finally {
  292. try {
  293. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  294. } finally {
  295. if (e_2) throw e_2.error;
  296. }
  297. }
  298. };
  299. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  300. exports.default = mdcAutoInit;
  301. /***/ })
  302. /******/ });
  303. });
  304. //# sourceMappingURL=mdc.autoInit.js.map