index-1f9b54dc.js 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. const NAMESPACE = 'calcite';
  7. /**
  8. * Virtual DOM patching algorithm based on Snabbdom by
  9. * Simon Friis Vindum (@paldepind)
  10. * Licensed under the MIT License
  11. * https://github.com/snabbdom/snabbdom/blob/master/LICENSE
  12. *
  13. * Modified for Stencil's renderer and slot projection
  14. */
  15. let scopeId;
  16. let hostTagName;
  17. let isSvgMode = false;
  18. let queuePending = false;
  19. const getAssetPath = (path) => {
  20. const assetUrl = new URL(path, plt.$resourcesUrl$);
  21. return assetUrl.origin !== win.location.origin ? assetUrl.href : assetUrl.pathname;
  22. };
  23. const createTime = (fnName, tagName = '') => {
  24. {
  25. return () => {
  26. return;
  27. };
  28. }
  29. };
  30. const uniqueTime = (key, measureText) => {
  31. {
  32. return () => {
  33. return;
  34. };
  35. }
  36. };
  37. const CONTENT_REF_ID = 'r';
  38. const ORG_LOCATION_ID = 'o';
  39. const SLOT_NODE_ID = 's';
  40. const TEXT_NODE_ID = 't';
  41. const HYDRATE_ID = 's-id';
  42. const HYDRATED_STYLE_ID = 'sty-id';
  43. const HYDRATE_CHILD_ID = 'c-id';
  44. const HYDRATED_CSS = '{visibility:hidden}[calcite-hydrated]{visibility:inherit}';
  45. const XLINK_NS = 'http://www.w3.org/1999/xlink';
  46. /**
  47. * Default style mode id
  48. */
  49. /**
  50. * Reusable empty obj/array
  51. * Don't add values to these!!
  52. */
  53. const EMPTY_OBJ = {};
  54. /**
  55. * Namespaces
  56. */
  57. const SVG_NS = 'http://www.w3.org/2000/svg';
  58. const HTML_NS = 'http://www.w3.org/1999/xhtml';
  59. const isDef = (v) => v != null;
  60. const isComplexType = (o) => {
  61. // https://jsperf.com/typeof-fn-object/5
  62. o = typeof o;
  63. return o === 'object' || o === 'function';
  64. };
  65. /**
  66. * Production h() function based on Preact by
  67. * Jason Miller (@developit)
  68. * Licensed under the MIT License
  69. * https://github.com/developit/preact/blob/master/LICENSE
  70. *
  71. * Modified for Stencil's compiler and vdom
  72. */
  73. // const stack: any[] = [];
  74. // export function h(nodeName: string | d.FunctionalComponent, vnodeData: d.PropsType, child?: d.ChildType): d.VNode;
  75. // export function h(nodeName: string | d.FunctionalComponent, vnodeData: d.PropsType, ...children: d.ChildType[]): d.VNode;
  76. const h = (nodeName, vnodeData, ...children) => {
  77. let child = null;
  78. let key = null;
  79. let simple = false;
  80. let lastSimple = false;
  81. const vNodeChildren = [];
  82. const walk = (c) => {
  83. for (let i = 0; i < c.length; i++) {
  84. child = c[i];
  85. if (Array.isArray(child)) {
  86. walk(child);
  87. }
  88. else if (child != null && typeof child !== 'boolean') {
  89. if ((simple = typeof nodeName !== 'function' && !isComplexType(child))) {
  90. child = String(child);
  91. }
  92. if (simple && lastSimple) {
  93. // If the previous child was simple (string), we merge both
  94. vNodeChildren[vNodeChildren.length - 1].$text$ += child;
  95. }
  96. else {
  97. // Append a new vNode, if it's text, we create a text vNode
  98. vNodeChildren.push(simple ? newVNode(null, child) : child);
  99. }
  100. lastSimple = simple;
  101. }
  102. }
  103. };
  104. walk(children);
  105. if (vnodeData) {
  106. // normalize class / classname attributes
  107. if (vnodeData.key) {
  108. key = vnodeData.key;
  109. }
  110. {
  111. const classData = vnodeData.className || vnodeData.class;
  112. if (classData) {
  113. vnodeData.class =
  114. typeof classData !== 'object'
  115. ? classData
  116. : Object.keys(classData)
  117. .filter((k) => classData[k])
  118. .join(' ');
  119. }
  120. }
  121. }
  122. if (typeof nodeName === 'function') {
  123. // nodeName is a functional component
  124. return nodeName(vnodeData === null ? {} : vnodeData, vNodeChildren, vdomFnUtils);
  125. }
  126. const vnode = newVNode(nodeName, null);
  127. vnode.$attrs$ = vnodeData;
  128. if (vNodeChildren.length > 0) {
  129. vnode.$children$ = vNodeChildren;
  130. }
  131. {
  132. vnode.$key$ = key;
  133. }
  134. return vnode;
  135. };
  136. const newVNode = (tag, text) => {
  137. const vnode = {
  138. $flags$: 0,
  139. $tag$: tag,
  140. $text$: text,
  141. $elm$: null,
  142. $children$: null,
  143. };
  144. {
  145. vnode.$attrs$ = null;
  146. }
  147. {
  148. vnode.$key$ = null;
  149. }
  150. return vnode;
  151. };
  152. const Host = {};
  153. const isHost = (node) => node && node.$tag$ === Host;
  154. const vdomFnUtils = {
  155. forEach: (children, cb) => children.map(convertToPublic).forEach(cb),
  156. map: (children, cb) => children.map(convertToPublic).map(cb).map(convertToPrivate),
  157. };
  158. const convertToPublic = (node) => ({
  159. vattrs: node.$attrs$,
  160. vchildren: node.$children$,
  161. vkey: node.$key$,
  162. vname: node.$name$,
  163. vtag: node.$tag$,
  164. vtext: node.$text$,
  165. });
  166. const convertToPrivate = (node) => {
  167. if (typeof node.vtag === 'function') {
  168. const vnodeData = Object.assign({}, node.vattrs);
  169. if (node.vkey) {
  170. vnodeData.key = node.vkey;
  171. }
  172. if (node.vname) {
  173. vnodeData.name = node.vname;
  174. }
  175. return h(node.vtag, vnodeData, ...(node.vchildren || []));
  176. }
  177. const vnode = newVNode(node.vtag, node.vtext);
  178. vnode.$attrs$ = node.vattrs;
  179. vnode.$children$ = node.vchildren;
  180. vnode.$key$ = node.vkey;
  181. vnode.$name$ = node.vname;
  182. return vnode;
  183. };
  184. const initializeClientHydrate = (hostElm, tagName, hostId, hostRef) => {
  185. const endHydrate = createTime('hydrateClient', tagName);
  186. const shadowRoot = hostElm.shadowRoot;
  187. const childRenderNodes = [];
  188. const slotNodes = [];
  189. const shadowRootNodes = shadowRoot ? [] : null;
  190. const vnode = (hostRef.$vnode$ = newVNode(tagName, null));
  191. if (!plt.$orgLocNodes$) {
  192. initializeDocumentHydrate(doc.body, (plt.$orgLocNodes$ = new Map()));
  193. }
  194. hostElm[HYDRATE_ID] = hostId;
  195. hostElm.removeAttribute(HYDRATE_ID);
  196. clientHydrate(vnode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, hostElm, hostId);
  197. childRenderNodes.map((c) => {
  198. const orgLocationId = c.$hostId$ + '.' + c.$nodeId$;
  199. const orgLocationNode = plt.$orgLocNodes$.get(orgLocationId);
  200. const node = c.$elm$;
  201. if (orgLocationNode && supportsShadow && orgLocationNode['s-en'] === '') {
  202. orgLocationNode.parentNode.insertBefore(node, orgLocationNode.nextSibling);
  203. }
  204. if (!shadowRoot) {
  205. node['s-hn'] = tagName;
  206. if (orgLocationNode) {
  207. node['s-ol'] = orgLocationNode;
  208. node['s-ol']['s-nr'] = node;
  209. }
  210. }
  211. plt.$orgLocNodes$.delete(orgLocationId);
  212. });
  213. if (shadowRoot) {
  214. shadowRootNodes.map((shadowRootNode) => {
  215. if (shadowRootNode) {
  216. shadowRoot.appendChild(shadowRootNode);
  217. }
  218. });
  219. }
  220. endHydrate();
  221. };
  222. const clientHydrate = (parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node, hostId) => {
  223. let childNodeType;
  224. let childIdSplt;
  225. let childVNode;
  226. let i;
  227. if (node.nodeType === 1 /* NODE_TYPE.ElementNode */) {
  228. childNodeType = node.getAttribute(HYDRATE_CHILD_ID);
  229. if (childNodeType) {
  230. // got the node data from the element's attribute
  231. // `${hostId}.${nodeId}.${depth}.${index}`
  232. childIdSplt = childNodeType.split('.');
  233. if (childIdSplt[0] === hostId || childIdSplt[0] === '0') {
  234. childVNode = {
  235. $flags$: 0,
  236. $hostId$: childIdSplt[0],
  237. $nodeId$: childIdSplt[1],
  238. $depth$: childIdSplt[2],
  239. $index$: childIdSplt[3],
  240. $tag$: node.tagName.toLowerCase(),
  241. $elm$: node,
  242. $attrs$: null,
  243. $children$: null,
  244. $key$: null,
  245. $name$: null,
  246. $text$: null,
  247. };
  248. childRenderNodes.push(childVNode);
  249. node.removeAttribute(HYDRATE_CHILD_ID);
  250. // this is a new child vnode
  251. // so ensure its parent vnode has the vchildren array
  252. if (!parentVNode.$children$) {
  253. parentVNode.$children$ = [];
  254. }
  255. // add our child vnode to a specific index of the vnode's children
  256. parentVNode.$children$[childVNode.$index$] = childVNode;
  257. // this is now the new parent vnode for all the next child checks
  258. parentVNode = childVNode;
  259. if (shadowRootNodes && childVNode.$depth$ === '0') {
  260. shadowRootNodes[childVNode.$index$] = childVNode.$elm$;
  261. }
  262. }
  263. }
  264. // recursively drill down, end to start so we can remove nodes
  265. for (i = node.childNodes.length - 1; i >= 0; i--) {
  266. clientHydrate(parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node.childNodes[i], hostId);
  267. }
  268. if (node.shadowRoot) {
  269. // keep drilling down through the shadow root nodes
  270. for (i = node.shadowRoot.childNodes.length - 1; i >= 0; i--) {
  271. clientHydrate(parentVNode, childRenderNodes, slotNodes, shadowRootNodes, hostElm, node.shadowRoot.childNodes[i], hostId);
  272. }
  273. }
  274. }
  275. else if (node.nodeType === 8 /* NODE_TYPE.CommentNode */) {
  276. // `${COMMENT_TYPE}.${hostId}.${nodeId}.${depth}.${index}`
  277. childIdSplt = node.nodeValue.split('.');
  278. if (childIdSplt[1] === hostId || childIdSplt[1] === '0') {
  279. // comment node for either the host id or a 0 host id
  280. childNodeType = childIdSplt[0];
  281. childVNode = {
  282. $flags$: 0,
  283. $hostId$: childIdSplt[1],
  284. $nodeId$: childIdSplt[2],
  285. $depth$: childIdSplt[3],
  286. $index$: childIdSplt[4],
  287. $elm$: node,
  288. $attrs$: null,
  289. $children$: null,
  290. $key$: null,
  291. $name$: null,
  292. $tag$: null,
  293. $text$: null,
  294. };
  295. if (childNodeType === TEXT_NODE_ID) {
  296. childVNode.$elm$ = node.nextSibling;
  297. if (childVNode.$elm$ && childVNode.$elm$.nodeType === 3 /* NODE_TYPE.TextNode */) {
  298. childVNode.$text$ = childVNode.$elm$.textContent;
  299. childRenderNodes.push(childVNode);
  300. // remove the text comment since it's no longer needed
  301. node.remove();
  302. if (!parentVNode.$children$) {
  303. parentVNode.$children$ = [];
  304. }
  305. parentVNode.$children$[childVNode.$index$] = childVNode;
  306. if (shadowRootNodes && childVNode.$depth$ === '0') {
  307. shadowRootNodes[childVNode.$index$] = childVNode.$elm$;
  308. }
  309. }
  310. }
  311. else if (childVNode.$hostId$ === hostId) {
  312. // this comment node is specifcally for this host id
  313. if (childNodeType === SLOT_NODE_ID) {
  314. // `${SLOT_NODE_ID}.${hostId}.${nodeId}.${depth}.${index}.${slotName}`;
  315. childVNode.$tag$ = 'slot';
  316. if (childIdSplt[5]) {
  317. node['s-sn'] = childVNode.$name$ = childIdSplt[5];
  318. }
  319. else {
  320. node['s-sn'] = '';
  321. }
  322. node['s-sr'] = true;
  323. if (shadowRootNodes) {
  324. // browser support shadowRoot and this is a shadow dom component
  325. // create an actual slot element
  326. childVNode.$elm$ = doc.createElement(childVNode.$tag$);
  327. if (childVNode.$name$) {
  328. // add the slot name attribute
  329. childVNode.$elm$.setAttribute('name', childVNode.$name$);
  330. }
  331. // insert the new slot element before the slot comment
  332. node.parentNode.insertBefore(childVNode.$elm$, node);
  333. // remove the slot comment since it's not needed for shadow
  334. node.remove();
  335. if (childVNode.$depth$ === '0') {
  336. shadowRootNodes[childVNode.$index$] = childVNode.$elm$;
  337. }
  338. }
  339. slotNodes.push(childVNode);
  340. if (!parentVNode.$children$) {
  341. parentVNode.$children$ = [];
  342. }
  343. parentVNode.$children$[childVNode.$index$] = childVNode;
  344. }
  345. else if (childNodeType === CONTENT_REF_ID) {
  346. // `${CONTENT_REF_ID}.${hostId}`;
  347. if (shadowRootNodes) {
  348. // remove the content ref comment since it's not needed for shadow
  349. node.remove();
  350. }
  351. }
  352. }
  353. }
  354. }
  355. else if (parentVNode && parentVNode.$tag$ === 'style') {
  356. const vnode = newVNode(null, node.textContent);
  357. vnode.$elm$ = node;
  358. vnode.$index$ = '0';
  359. parentVNode.$children$ = [vnode];
  360. }
  361. };
  362. const initializeDocumentHydrate = (node, orgLocNodes) => {
  363. if (node.nodeType === 1 /* NODE_TYPE.ElementNode */) {
  364. let i = 0;
  365. for (; i < node.childNodes.length; i++) {
  366. initializeDocumentHydrate(node.childNodes[i], orgLocNodes);
  367. }
  368. if (node.shadowRoot) {
  369. for (i = 0; i < node.shadowRoot.childNodes.length; i++) {
  370. initializeDocumentHydrate(node.shadowRoot.childNodes[i], orgLocNodes);
  371. }
  372. }
  373. }
  374. else if (node.nodeType === 8 /* NODE_TYPE.CommentNode */) {
  375. const childIdSplt = node.nodeValue.split('.');
  376. if (childIdSplt[0] === ORG_LOCATION_ID) {
  377. orgLocNodes.set(childIdSplt[1] + '.' + childIdSplt[2], node);
  378. node.nodeValue = '';
  379. // useful to know if the original location is
  380. // the root light-dom of a shadow dom component
  381. node['s-en'] = childIdSplt[3];
  382. }
  383. }
  384. };
  385. /**
  386. * Parse a new property value for a given property type.
  387. *
  388. * While the prop value can reasonably be expected to be of `any` type as far as TypeScript's type checker is concerned,
  389. * it is not safe to assume that the string returned by evaluating `typeof propValue` matches:
  390. * 1. `any`, the type given to `propValue` in the function signature
  391. * 2. the type stored from `propType`.
  392. *
  393. * This function provides the capability to parse/coerce a property's value to potentially any other JavaScript type.
  394. *
  395. * Property values represented in TSX preserve their type information. In the example below, the number 0 is passed to
  396. * a component. This `propValue` will preserve its type information (`typeof propValue === 'number'`). Note that is
  397. * based on the type of the value being passed in, not the type declared of the class member decorated with `@Prop`.
  398. * ```tsx
  399. * <my-cmp prop-val={0}></my-cmp>
  400. * ```
  401. *
  402. * HTML prop values on the other hand, will always a string
  403. *
  404. * @param propValue the new value to coerce to some type
  405. * @param propType the type of the prop, expressed as a binary number
  406. * @returns the parsed/coerced value
  407. */
  408. const parsePropertyValue = (propValue, propType) => {
  409. // ensure this value is of the correct prop type
  410. if (propValue != null && !isComplexType(propValue)) {
  411. if (propType & 4 /* MEMBER_FLAGS.Boolean */) {
  412. // per the HTML spec, any string value means it is a boolean true value
  413. // but we'll cheat here and say that the string "false" is the boolean false
  414. return propValue === 'false' ? false : propValue === '' || !!propValue;
  415. }
  416. if (propType & 2 /* MEMBER_FLAGS.Number */) {
  417. // force it to be a number
  418. return parseFloat(propValue);
  419. }
  420. if (propType & 1 /* MEMBER_FLAGS.String */) {
  421. // could have been passed as a number or boolean
  422. // but we still want it as a string
  423. return String(propValue);
  424. }
  425. // redundant return here for better minification
  426. return propValue;
  427. }
  428. // not sure exactly what type we want
  429. // so no need to change to a different type
  430. return propValue;
  431. };
  432. const getElement = (ref) => (getHostRef(ref).$hostElement$ );
  433. const createEvent = (ref, name, flags) => {
  434. const elm = getElement(ref);
  435. return {
  436. emit: (detail) => {
  437. return emitEvent(elm, name, {
  438. bubbles: !!(flags & 4 /* EVENT_FLAGS.Bubbles */),
  439. composed: !!(flags & 2 /* EVENT_FLAGS.Composed */),
  440. cancelable: !!(flags & 1 /* EVENT_FLAGS.Cancellable */),
  441. detail,
  442. });
  443. },
  444. };
  445. };
  446. /**
  447. * Helper function to create & dispatch a custom Event on a provided target
  448. * @param elm the target of the Event
  449. * @param name the name to give the custom Event
  450. * @param opts options for configuring a custom Event
  451. * @returns the custom Event
  452. */
  453. const emitEvent = (elm, name, opts) => {
  454. const ev = plt.ce(name, opts);
  455. elm.dispatchEvent(ev);
  456. return ev;
  457. };
  458. const rootAppliedStyles = /*@__PURE__*/ new WeakMap();
  459. const registerStyle = (scopeId, cssText, allowCS) => {
  460. let style = styles.get(scopeId);
  461. if (supportsConstructableStylesheets && allowCS) {
  462. style = (style || new CSSStyleSheet());
  463. if (typeof style === 'string') {
  464. style = cssText;
  465. }
  466. else {
  467. style.replaceSync(cssText);
  468. }
  469. }
  470. else {
  471. style = cssText;
  472. }
  473. styles.set(scopeId, style);
  474. };
  475. const addStyle = (styleContainerNode, cmpMeta, mode, hostElm) => {
  476. let scopeId = getScopeId(cmpMeta);
  477. const style = styles.get(scopeId);
  478. // if an element is NOT connected then getRootNode() will return the wrong root node
  479. // so the fallback is to always use the document for the root node in those cases
  480. styleContainerNode = styleContainerNode.nodeType === 11 /* NODE_TYPE.DocumentFragment */ ? styleContainerNode : doc;
  481. if (style) {
  482. if (typeof style === 'string') {
  483. styleContainerNode = styleContainerNode.head || styleContainerNode;
  484. let appliedStyles = rootAppliedStyles.get(styleContainerNode);
  485. let styleElm;
  486. if (!appliedStyles) {
  487. rootAppliedStyles.set(styleContainerNode, (appliedStyles = new Set()));
  488. }
  489. if (!appliedStyles.has(scopeId)) {
  490. if (styleContainerNode.host &&
  491. (styleElm = styleContainerNode.querySelector(`[${HYDRATED_STYLE_ID}="${scopeId}"]`))) {
  492. // This is only happening on native shadow-dom, do not needs CSS var shim
  493. styleElm.innerHTML = style;
  494. }
  495. else {
  496. {
  497. styleElm = doc.createElement('style');
  498. styleElm.innerHTML = style;
  499. }
  500. styleContainerNode.insertBefore(styleElm, styleContainerNode.querySelector('link'));
  501. }
  502. if (appliedStyles) {
  503. appliedStyles.add(scopeId);
  504. }
  505. }
  506. }
  507. else if (!styleContainerNode.adoptedStyleSheets.includes(style)) {
  508. styleContainerNode.adoptedStyleSheets = [...styleContainerNode.adoptedStyleSheets, style];
  509. }
  510. }
  511. return scopeId;
  512. };
  513. const attachStyles = (hostRef) => {
  514. const cmpMeta = hostRef.$cmpMeta$;
  515. const elm = hostRef.$hostElement$;
  516. const flags = cmpMeta.$flags$;
  517. const endAttachStyles = createTime('attachStyles', cmpMeta.$tagName$);
  518. const scopeId = addStyle(elm.shadowRoot ? elm.shadowRoot : elm.getRootNode(), cmpMeta);
  519. if (flags & 10 /* CMP_FLAGS.needsScopedEncapsulation */) {
  520. // only required when we're NOT using native shadow dom (slot)
  521. // or this browser doesn't support native shadow dom
  522. // and this host element was NOT created with SSR
  523. // let's pick out the inner content for slot projection
  524. // create a node to represent where the original
  525. // content was first placed, which is useful later on
  526. // DOM WRITE!!
  527. elm['s-sc'] = scopeId;
  528. elm.classList.add(scopeId + '-h');
  529. }
  530. endAttachStyles();
  531. };
  532. const getScopeId = (cmp, mode) => 'sc-' + (cmp.$tagName$);
  533. const convertScopedToShadow = (css) => css.replace(/\/\*!@([^\/]+)\*\/[^\{]+\{/g, '$1{');
  534. /**
  535. * Production setAccessor() function based on Preact by
  536. * Jason Miller (@developit)
  537. * Licensed under the MIT License
  538. * https://github.com/developit/preact/blob/master/LICENSE
  539. *
  540. * Modified for Stencil's compiler and vdom
  541. */
  542. const setAccessor = (elm, memberName, oldValue, newValue, isSvg, flags) => {
  543. if (oldValue !== newValue) {
  544. let isProp = isMemberInElement(elm, memberName);
  545. let ln = memberName.toLowerCase();
  546. if (memberName === 'class') {
  547. const classList = elm.classList;
  548. const oldClasses = parseClassList(oldValue);
  549. const newClasses = parseClassList(newValue);
  550. classList.remove(...oldClasses.filter((c) => c && !newClasses.includes(c)));
  551. classList.add(...newClasses.filter((c) => c && !oldClasses.includes(c)));
  552. }
  553. else if (memberName === 'style') {
  554. // update style attribute, css properties and values
  555. {
  556. for (const prop in oldValue) {
  557. if (!newValue || newValue[prop] == null) {
  558. if (prop.includes('-')) {
  559. elm.style.removeProperty(prop);
  560. }
  561. else {
  562. elm.style[prop] = '';
  563. }
  564. }
  565. }
  566. }
  567. for (const prop in newValue) {
  568. if (!oldValue || newValue[prop] !== oldValue[prop]) {
  569. if (prop.includes('-')) {
  570. elm.style.setProperty(prop, newValue[prop]);
  571. }
  572. else {
  573. elm.style[prop] = newValue[prop];
  574. }
  575. }
  576. }
  577. }
  578. else if (memberName === 'key')
  579. ;
  580. else if (memberName === 'ref') {
  581. // minifier will clean this up
  582. if (newValue) {
  583. newValue(elm);
  584. }
  585. }
  586. else if ((!isProp ) &&
  587. memberName[0] === 'o' &&
  588. memberName[1] === 'n') {
  589. // Event Handlers
  590. // so if the member name starts with "on" and the 3rd characters is
  591. // a capital letter, and it's not already a member on the element,
  592. // then we're assuming it's an event listener
  593. if (memberName[2] === '-') {
  594. // on- prefixed events
  595. // allows to be explicit about the dom event to listen without any magic
  596. // under the hood:
  597. // <my-cmp on-click> // listens for "click"
  598. // <my-cmp on-Click> // listens for "Click"
  599. // <my-cmp on-ionChange> // listens for "ionChange"
  600. // <my-cmp on-EVENTS> // listens for "EVENTS"
  601. memberName = memberName.slice(3);
  602. }
  603. else if (isMemberInElement(win, ln)) {
  604. // standard event
  605. // the JSX attribute could have been "onMouseOver" and the
  606. // member name "onmouseover" is on the window's prototype
  607. // so let's add the listener "mouseover", which is all lowercased
  608. memberName = ln.slice(2);
  609. }
  610. else {
  611. // custom event
  612. // the JSX attribute could have been "onMyCustomEvent"
  613. // so let's trim off the "on" prefix and lowercase the first character
  614. // and add the listener "myCustomEvent"
  615. // except for the first character, we keep the event name case
  616. memberName = ln[2] + memberName.slice(3);
  617. }
  618. if (oldValue) {
  619. plt.rel(elm, memberName, oldValue, false);
  620. }
  621. if (newValue) {
  622. plt.ael(elm, memberName, newValue, false);
  623. }
  624. }
  625. else {
  626. // Set property if it exists and it's not a SVG
  627. const isComplex = isComplexType(newValue);
  628. if ((isProp || (isComplex && newValue !== null)) && !isSvg) {
  629. try {
  630. if (!elm.tagName.includes('-')) {
  631. const n = newValue == null ? '' : newValue;
  632. // Workaround for Safari, moving the <input> caret when re-assigning the same valued
  633. if (memberName === 'list') {
  634. isProp = false;
  635. }
  636. else if (oldValue == null || elm[memberName] != n) {
  637. elm[memberName] = n;
  638. }
  639. }
  640. else {
  641. elm[memberName] = newValue;
  642. }
  643. }
  644. catch (e) { }
  645. }
  646. /**
  647. * Need to manually update attribute if:
  648. * - memberName is not an attribute
  649. * - if we are rendering the host element in order to reflect attribute
  650. * - if it's a SVG, since properties might not work in <svg>
  651. * - if the newValue is null/undefined or 'false'.
  652. */
  653. let xlink = false;
  654. {
  655. if (ln !== (ln = ln.replace(/^xlink\:?/, ''))) {
  656. memberName = ln;
  657. xlink = true;
  658. }
  659. }
  660. if (newValue == null || newValue === false) {
  661. if (newValue !== false || elm.getAttribute(memberName) === '') {
  662. if (xlink) {
  663. elm.removeAttributeNS(XLINK_NS, memberName);
  664. }
  665. else {
  666. elm.removeAttribute(memberName);
  667. }
  668. }
  669. }
  670. else if ((!isProp || flags & 4 /* VNODE_FLAGS.isHost */ || isSvg) && !isComplex) {
  671. newValue = newValue === true ? '' : newValue;
  672. if (xlink) {
  673. elm.setAttributeNS(XLINK_NS, memberName, newValue);
  674. }
  675. else {
  676. elm.setAttribute(memberName, newValue);
  677. }
  678. }
  679. }
  680. }
  681. };
  682. const parseClassListRegex = /\s/;
  683. const parseClassList = (value) => (!value ? [] : value.split(parseClassListRegex));
  684. const updateElement = (oldVnode, newVnode, isSvgMode, memberName) => {
  685. // if the element passed in is a shadow root, which is a document fragment
  686. // then we want to be adding attrs/props to the shadow root's "host" element
  687. // if it's not a shadow root, then we add attrs/props to the same element
  688. const elm = newVnode.$elm$.nodeType === 11 /* NODE_TYPE.DocumentFragment */ && newVnode.$elm$.host
  689. ? newVnode.$elm$.host
  690. : newVnode.$elm$;
  691. const oldVnodeAttrs = (oldVnode && oldVnode.$attrs$) || EMPTY_OBJ;
  692. const newVnodeAttrs = newVnode.$attrs$ || EMPTY_OBJ;
  693. {
  694. // remove attributes no longer present on the vnode by setting them to undefined
  695. for (memberName in oldVnodeAttrs) {
  696. if (!(memberName in newVnodeAttrs)) {
  697. setAccessor(elm, memberName, oldVnodeAttrs[memberName], undefined, isSvgMode, newVnode.$flags$);
  698. }
  699. }
  700. }
  701. // add new & update changed attributes
  702. for (memberName in newVnodeAttrs) {
  703. setAccessor(elm, memberName, oldVnodeAttrs[memberName], newVnodeAttrs[memberName], isSvgMode, newVnode.$flags$);
  704. }
  705. };
  706. /**
  707. * Create a DOM Node corresponding to one of the children of a given VNode.
  708. *
  709. * @param oldParentVNode the parent VNode from the previous render
  710. * @param newParentVNode the parent VNode from the current render
  711. * @param childIndex the index of the VNode, in the _new_ parent node's
  712. * children, for which we will create a new DOM node
  713. * @param parentElm the parent DOM node which our new node will be a child of
  714. * @returns the newly created node
  715. */
  716. const createElm = (oldParentVNode, newParentVNode, childIndex, parentElm) => {
  717. // tslint:disable-next-line: prefer-const
  718. const newVNode = newParentVNode.$children$[childIndex];
  719. let i = 0;
  720. let elm;
  721. let childNode;
  722. if (newVNode.$text$ !== null) {
  723. // create text node
  724. elm = newVNode.$elm$ = doc.createTextNode(newVNode.$text$);
  725. }
  726. else {
  727. if (!isSvgMode) {
  728. isSvgMode = newVNode.$tag$ === 'svg';
  729. }
  730. // create element
  731. elm = newVNode.$elm$ = (doc.createElementNS(isSvgMode ? SVG_NS : HTML_NS, newVNode.$tag$)
  732. );
  733. if (isSvgMode && newVNode.$tag$ === 'foreignObject') {
  734. isSvgMode = false;
  735. }
  736. // add css classes, attrs, props, listeners, etc.
  737. {
  738. updateElement(null, newVNode, isSvgMode);
  739. }
  740. if (isDef(scopeId) && elm['s-si'] !== scopeId) {
  741. // if there is a scopeId and this is the initial render
  742. // then let's add the scopeId as a css class
  743. elm.classList.add((elm['s-si'] = scopeId));
  744. }
  745. if (newVNode.$children$) {
  746. for (i = 0; i < newVNode.$children$.length; ++i) {
  747. // create the node
  748. childNode = createElm(oldParentVNode, newVNode, i);
  749. // return node could have been null
  750. if (childNode) {
  751. // append our new node
  752. elm.appendChild(childNode);
  753. }
  754. }
  755. }
  756. {
  757. if (newVNode.$tag$ === 'svg') {
  758. // Only reset the SVG context when we're exiting <svg> element
  759. isSvgMode = false;
  760. }
  761. else if (elm.tagName === 'foreignObject') {
  762. // Reenter SVG context when we're exiting <foreignObject> element
  763. isSvgMode = true;
  764. }
  765. }
  766. }
  767. return elm;
  768. };
  769. const addVnodes = (parentElm, before, parentVNode, vnodes, startIdx, endIdx) => {
  770. let containerElm = (parentElm);
  771. let childNode;
  772. if (containerElm.shadowRoot && containerElm.tagName === hostTagName) {
  773. containerElm = containerElm.shadowRoot;
  774. }
  775. for (; startIdx <= endIdx; ++startIdx) {
  776. if (vnodes[startIdx]) {
  777. childNode = createElm(null, parentVNode, startIdx);
  778. if (childNode) {
  779. vnodes[startIdx].$elm$ = childNode;
  780. containerElm.insertBefore(childNode, before);
  781. }
  782. }
  783. }
  784. };
  785. const removeVnodes = (vnodes, startIdx, endIdx, vnode, elm) => {
  786. for (; startIdx <= endIdx; ++startIdx) {
  787. if ((vnode = vnodes[startIdx])) {
  788. elm = vnode.$elm$;
  789. callNodeRefs(vnode);
  790. // remove the vnode's element from the dom
  791. elm.remove();
  792. }
  793. }
  794. };
  795. /**
  796. * Reconcile the children of a new VNode with the children of an old VNode by
  797. * traversing the two collections of children, identifying nodes that are
  798. * conserved or changed, calling out to `patch` to make any necessary
  799. * updates to the DOM, and rearranging DOM nodes as needed.
  800. *
  801. * The algorithm for reconciling children works by analyzing two 'windows' onto
  802. * the two arrays of children (`oldCh` and `newCh`). We keep track of the
  803. * 'windows' by storing start and end indices and references to the
  804. * corresponding array entries. Initially the two 'windows' are basically equal
  805. * to the entire array, but we progressively narrow the windows until there are
  806. * no children left to update by doing the following:
  807. *
  808. * 1. Skip any `null` entries at the beginning or end of the two arrays, so
  809. * that if we have an initial array like the following we'll end up dealing
  810. * only with a window bounded by the highlighted elements:
  811. *
  812. * [null, null, VNode1 , ... , VNode2, null, null]
  813. * ^^^^^^ ^^^^^^
  814. *
  815. * 2. Check to see if the elements at the head and tail positions are equal
  816. * across the windows. This will basically detect elements which haven't
  817. * been added, removed, or changed position, i.e. if you had the following
  818. * VNode elements (represented as HTML):
  819. *
  820. * oldVNode: `<div><p><span>HEY</span></p></div>`
  821. * newVNode: `<div><p><span>THERE</span></p></div>`
  822. *
  823. * Then when comparing the children of the `<div>` tag we check the equality
  824. * of the VNodes corresponding to the `<p>` tags and, since they are the
  825. * same tag in the same position, we'd be able to avoid completely
  826. * re-rendering the subtree under them with a new DOM element and would just
  827. * call out to `patch` to handle reconciling their children and so on.
  828. *
  829. * 3. Check, for both windows, to see if the element at the beginning of the
  830. * window corresponds to the element at the end of the other window. This is
  831. * a heuristic which will let us identify _some_ situations in which
  832. * elements have changed position, for instance it _should_ detect that the
  833. * children nodes themselves have not changed but merely moved in the
  834. * following example:
  835. *
  836. * oldVNode: `<div><element-one /><element-two /></div>`
  837. * newVNode: `<div><element-two /><element-one /></div>`
  838. *
  839. * If we find cases like this then we also need to move the concrete DOM
  840. * elements corresponding to the moved children to write the re-order to the
  841. * DOM.
  842. *
  843. * 4. Finally, if VNodes have the `key` attribute set on them we check for any
  844. * nodes in the old children which have the same key as the first element in
  845. * our window on the new children. If we find such a node we handle calling
  846. * out to `patch`, moving relevant DOM nodes, and so on, in accordance with
  847. * what we find.
  848. *
  849. * Finally, once we've narrowed our 'windows' to the point that either of them
  850. * collapse (i.e. they have length 0) we then handle any remaining VNode
  851. * insertion or deletion that needs to happen to get a DOM state that correctly
  852. * reflects the new child VNodes. If, for instance, after our window on the old
  853. * children has collapsed we still have more nodes on the new children that
  854. * we haven't dealt with yet then we need to add them, or if the new children
  855. * collapse but we still have unhandled _old_ children then we need to make
  856. * sure the corresponding DOM nodes are removed.
  857. *
  858. * @param parentElm the node into which the parent VNode is rendered
  859. * @param oldCh the old children of the parent node
  860. * @param newVNode the new VNode which will replace the parent
  861. * @param newCh the new children of the parent node
  862. */
  863. const updateChildren = (parentElm, oldCh, newVNode, newCh) => {
  864. let oldStartIdx = 0;
  865. let newStartIdx = 0;
  866. let idxInOld = 0;
  867. let i = 0;
  868. let oldEndIdx = oldCh.length - 1;
  869. let oldStartVnode = oldCh[0];
  870. let oldEndVnode = oldCh[oldEndIdx];
  871. let newEndIdx = newCh.length - 1;
  872. let newStartVnode = newCh[0];
  873. let newEndVnode = newCh[newEndIdx];
  874. let node;
  875. let elmToMove;
  876. while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
  877. if (oldStartVnode == null) {
  878. // VNode might have been moved left
  879. oldStartVnode = oldCh[++oldStartIdx];
  880. }
  881. else if (oldEndVnode == null) {
  882. oldEndVnode = oldCh[--oldEndIdx];
  883. }
  884. else if (newStartVnode == null) {
  885. newStartVnode = newCh[++newStartIdx];
  886. }
  887. else if (newEndVnode == null) {
  888. newEndVnode = newCh[--newEndIdx];
  889. }
  890. else if (isSameVnode(oldStartVnode, newStartVnode)) {
  891. // if the start nodes are the same then we should patch the new VNode
  892. // onto the old one, and increment our `newStartIdx` and `oldStartIdx`
  893. // indices to reflect that. We don't need to move any DOM Nodes around
  894. // since things are matched up in order.
  895. patch(oldStartVnode, newStartVnode);
  896. oldStartVnode = oldCh[++oldStartIdx];
  897. newStartVnode = newCh[++newStartIdx];
  898. }
  899. else if (isSameVnode(oldEndVnode, newEndVnode)) {
  900. // likewise, if the end nodes are the same we patch new onto old and
  901. // decrement our end indices, and also likewise in this case we don't
  902. // need to move any DOM Nodes.
  903. patch(oldEndVnode, newEndVnode);
  904. oldEndVnode = oldCh[--oldEndIdx];
  905. newEndVnode = newCh[--newEndIdx];
  906. }
  907. else if (isSameVnode(oldStartVnode, newEndVnode)) {
  908. patch(oldStartVnode, newEndVnode);
  909. // We need to move the element for `oldStartVnode` into a position which
  910. // will be appropriate for `newEndVnode`. For this we can use
  911. // `.insertBefore` and `oldEndVnode.$elm$.nextSibling`. If there is a
  912. // sibling for `oldEndVnode.$elm$` then we want to move the DOM node for
  913. // `oldStartVnode` between `oldEndVnode` and it's sibling, like so:
  914. //
  915. // <old-start-node />
  916. // <some-intervening-node />
  917. // <old-end-node />
  918. // <!-- -> <-- `oldStartVnode.$elm$` should be inserted here
  919. // <next-sibling />
  920. //
  921. // If instead `oldEndVnode.$elm$` has no sibling then we just want to put
  922. // the node for `oldStartVnode` at the end of the children of
  923. // `parentElm`. Luckily, `Node.nextSibling` will return `null` if there
  924. // aren't any siblings, and passing `null` to `Node.insertBefore` will
  925. // append it to the children of the parent element.
  926. parentElm.insertBefore(oldStartVnode.$elm$, oldEndVnode.$elm$.nextSibling);
  927. oldStartVnode = oldCh[++oldStartIdx];
  928. newEndVnode = newCh[--newEndIdx];
  929. }
  930. else if (isSameVnode(oldEndVnode, newStartVnode)) {
  931. patch(oldEndVnode, newStartVnode);
  932. // We've already checked above if `oldStartVnode` and `newStartVnode` are
  933. // the same node, so since we're here we know that they are not. Thus we
  934. // can move the element for `oldEndVnode` _before_ the element for
  935. // `oldStartVnode`, leaving `oldStartVnode` to be reconciled in the
  936. // future.
  937. parentElm.insertBefore(oldEndVnode.$elm$, oldStartVnode.$elm$);
  938. oldEndVnode = oldCh[--oldEndIdx];
  939. newStartVnode = newCh[++newStartIdx];
  940. }
  941. else {
  942. // Here we do some checks to match up old and new nodes based on the
  943. // `$key$` attribute, which is set by putting a `key="my-key"` attribute
  944. // in the JSX for a DOM element in the implementation of a Stencil
  945. // component.
  946. //
  947. // First we check to see if there are any nodes in the array of old
  948. // children which have the same key as the first node in the new
  949. // children.
  950. idxInOld = -1;
  951. {
  952. for (i = oldStartIdx; i <= oldEndIdx; ++i) {
  953. if (oldCh[i] && oldCh[i].$key$ !== null && oldCh[i].$key$ === newStartVnode.$key$) {
  954. idxInOld = i;
  955. break;
  956. }
  957. }
  958. }
  959. if (idxInOld >= 0) {
  960. // We found a node in the old children which matches up with the first
  961. // node in the new children! So let's deal with that
  962. elmToMove = oldCh[idxInOld];
  963. if (elmToMove.$tag$ !== newStartVnode.$tag$) {
  964. // the tag doesn't match so we'll need a new DOM element
  965. node = createElm(oldCh && oldCh[newStartIdx], newVNode, idxInOld);
  966. }
  967. else {
  968. patch(elmToMove, newStartVnode);
  969. // invalidate the matching old node so that we won't try to update it
  970. // again later on
  971. oldCh[idxInOld] = undefined;
  972. node = elmToMove.$elm$;
  973. }
  974. newStartVnode = newCh[++newStartIdx];
  975. }
  976. else {
  977. // We either didn't find an element in the old children that matches
  978. // the key of the first new child OR the build is not using `key`
  979. // attributes at all. In either case we need to create a new element
  980. // for the new node.
  981. node = createElm(oldCh && oldCh[newStartIdx], newVNode, newStartIdx);
  982. newStartVnode = newCh[++newStartIdx];
  983. }
  984. if (node) {
  985. // if we created a new node then handle inserting it to the DOM
  986. {
  987. oldStartVnode.$elm$.parentNode.insertBefore(node, oldStartVnode.$elm$);
  988. }
  989. }
  990. }
  991. }
  992. if (oldStartIdx > oldEndIdx) {
  993. // we have some more new nodes to add which don't match up with old nodes
  994. addVnodes(parentElm, newCh[newEndIdx + 1] == null ? null : newCh[newEndIdx + 1].$elm$, newVNode, newCh, newStartIdx, newEndIdx);
  995. }
  996. else if (newStartIdx > newEndIdx) {
  997. // there are nodes in the `oldCh` array which no longer correspond to nodes
  998. // in the new array, so lets remove them (which entails cleaning up the
  999. // relevant DOM nodes)
  1000. removeVnodes(oldCh, oldStartIdx, oldEndIdx);
  1001. }
  1002. };
  1003. /**
  1004. * Compare two VNodes to determine if they are the same
  1005. *
  1006. * **NB**: This function is an equality _heuristic_ based on the available
  1007. * information set on the two VNodes and can be misleading under certain
  1008. * circumstances. In particular, if the two nodes do not have `key` attrs
  1009. * (available under `$key$` on VNodes) then the function falls back on merely
  1010. * checking that they have the same tag.
  1011. *
  1012. * So, in other words, if `key` attrs are not set on VNodes which may be
  1013. * changing order within a `children` array or something along those lines then
  1014. * we could obtain a false positive and then have to do needless re-rendering.
  1015. *
  1016. * @param leftVNode the first VNode to check
  1017. * @param rightVNode the second VNode to check
  1018. * @returns whether they're equal or not
  1019. */
  1020. const isSameVnode = (leftVNode, rightVNode) => {
  1021. // compare if two vnode to see if they're "technically" the same
  1022. // need to have the same element tag, and same key to be the same
  1023. if (leftVNode.$tag$ === rightVNode.$tag$) {
  1024. // this will be set if components in the build have `key` attrs set on them
  1025. {
  1026. return leftVNode.$key$ === rightVNode.$key$;
  1027. }
  1028. }
  1029. return false;
  1030. };
  1031. /**
  1032. * Handle reconciling an outdated VNode with a new one which corresponds to
  1033. * it. This function handles flushing updates to the DOM and reconciling the
  1034. * children of the two nodes (if any).
  1035. *
  1036. * @param oldVNode an old VNode whose DOM element and children we want to update
  1037. * @param newVNode a new VNode representing an updated version of the old one
  1038. */
  1039. const patch = (oldVNode, newVNode) => {
  1040. const elm = (newVNode.$elm$ = oldVNode.$elm$);
  1041. const oldChildren = oldVNode.$children$;
  1042. const newChildren = newVNode.$children$;
  1043. const tag = newVNode.$tag$;
  1044. const text = newVNode.$text$;
  1045. if (text === null) {
  1046. {
  1047. // test if we're rendering an svg element, or still rendering nodes inside of one
  1048. // only add this to the when the compiler sees we're using an svg somewhere
  1049. isSvgMode = tag === 'svg' ? true : tag === 'foreignObject' ? false : isSvgMode;
  1050. }
  1051. {
  1052. if (tag === 'slot')
  1053. ;
  1054. else {
  1055. // either this is the first render of an element OR it's an update
  1056. // AND we already know it's possible it could have changed
  1057. // this updates the element's css classes, attrs, props, listeners, etc.
  1058. updateElement(oldVNode, newVNode, isSvgMode);
  1059. }
  1060. }
  1061. if (oldChildren !== null && newChildren !== null) {
  1062. // looks like there's child vnodes for both the old and new vnodes
  1063. // so we need to call `updateChildren` to reconcile them
  1064. updateChildren(elm, oldChildren, newVNode, newChildren);
  1065. }
  1066. else if (newChildren !== null) {
  1067. // no old child vnodes, but there are new child vnodes to add
  1068. if (oldVNode.$text$ !== null) {
  1069. // the old vnode was text, so be sure to clear it out
  1070. elm.textContent = '';
  1071. }
  1072. // add the new vnode children
  1073. addVnodes(elm, null, newVNode, newChildren, 0, newChildren.length - 1);
  1074. }
  1075. else if (oldChildren !== null) {
  1076. // no new child vnodes, but there are old child vnodes to remove
  1077. removeVnodes(oldChildren, 0, oldChildren.length - 1);
  1078. }
  1079. if (isSvgMode && tag === 'svg') {
  1080. isSvgMode = false;
  1081. }
  1082. }
  1083. else if (oldVNode.$text$ !== text) {
  1084. // update the text content for the text only vnode
  1085. // and also only if the text is different than before
  1086. elm.data = text;
  1087. }
  1088. };
  1089. const callNodeRefs = (vNode) => {
  1090. {
  1091. vNode.$attrs$ && vNode.$attrs$.ref && vNode.$attrs$.ref(null);
  1092. vNode.$children$ && vNode.$children$.map(callNodeRefs);
  1093. }
  1094. };
  1095. const renderVdom = (hostRef, renderFnResults) => {
  1096. const hostElm = hostRef.$hostElement$;
  1097. const cmpMeta = hostRef.$cmpMeta$;
  1098. const oldVNode = hostRef.$vnode$ || newVNode(null, null);
  1099. const rootVnode = isHost(renderFnResults) ? renderFnResults : h(null, null, renderFnResults);
  1100. hostTagName = hostElm.tagName;
  1101. if (cmpMeta.$attrsToReflect$) {
  1102. rootVnode.$attrs$ = rootVnode.$attrs$ || {};
  1103. cmpMeta.$attrsToReflect$.map(([propName, attribute]) => (rootVnode.$attrs$[attribute] = hostElm[propName]));
  1104. }
  1105. rootVnode.$tag$ = null;
  1106. rootVnode.$flags$ |= 4 /* VNODE_FLAGS.isHost */;
  1107. hostRef.$vnode$ = rootVnode;
  1108. rootVnode.$elm$ = oldVNode.$elm$ = (hostElm.shadowRoot || hostElm );
  1109. {
  1110. scopeId = hostElm['s-sc'];
  1111. }
  1112. // synchronous patch
  1113. patch(oldVNode, rootVnode);
  1114. };
  1115. const attachToAncestor = (hostRef, ancestorComponent) => {
  1116. if (ancestorComponent && !hostRef.$onRenderResolve$ && ancestorComponent['s-p']) {
  1117. ancestorComponent['s-p'].push(new Promise((r) => (hostRef.$onRenderResolve$ = r)));
  1118. }
  1119. };
  1120. const scheduleUpdate = (hostRef, isInitialLoad) => {
  1121. {
  1122. hostRef.$flags$ |= 16 /* HOST_FLAGS.isQueuedForUpdate */;
  1123. }
  1124. if (hostRef.$flags$ & 4 /* HOST_FLAGS.isWaitingForChildren */) {
  1125. hostRef.$flags$ |= 512 /* HOST_FLAGS.needsRerender */;
  1126. return;
  1127. }
  1128. attachToAncestor(hostRef, hostRef.$ancestorComponent$);
  1129. // there is no ancestor component or the ancestor component
  1130. // has already fired off its lifecycle update then
  1131. // fire off the initial update
  1132. const dispatch = () => dispatchHooks(hostRef, isInitialLoad);
  1133. return writeTask(dispatch) ;
  1134. };
  1135. const dispatchHooks = (hostRef, isInitialLoad) => {
  1136. const endSchedule = createTime('scheduleUpdate', hostRef.$cmpMeta$.$tagName$);
  1137. const instance = hostRef.$lazyInstance$ ;
  1138. let promise;
  1139. if (isInitialLoad) {
  1140. {
  1141. hostRef.$flags$ |= 256 /* HOST_FLAGS.isListenReady */;
  1142. if (hostRef.$queuedListeners$) {
  1143. hostRef.$queuedListeners$.map(([methodName, event]) => safeCall(instance, methodName, event));
  1144. hostRef.$queuedListeners$ = null;
  1145. }
  1146. }
  1147. {
  1148. promise = safeCall(instance, 'componentWillLoad');
  1149. }
  1150. }
  1151. {
  1152. promise = then(promise, () => safeCall(instance, 'componentWillRender'));
  1153. }
  1154. endSchedule();
  1155. return then(promise, () => updateComponent(hostRef, instance, isInitialLoad));
  1156. };
  1157. const updateComponent = async (hostRef, instance, isInitialLoad) => {
  1158. // updateComponent
  1159. const elm = hostRef.$hostElement$;
  1160. const endUpdate = createTime('update', hostRef.$cmpMeta$.$tagName$);
  1161. const rc = elm['s-rc'];
  1162. if (isInitialLoad) {
  1163. // DOM WRITE!
  1164. attachStyles(hostRef);
  1165. }
  1166. const endRender = createTime('render', hostRef.$cmpMeta$.$tagName$);
  1167. {
  1168. callRender(hostRef, instance);
  1169. }
  1170. if (rc) {
  1171. // ok, so turns out there are some child host elements
  1172. // waiting on this parent element to load
  1173. // let's fire off all update callbacks waiting
  1174. rc.map((cb) => cb());
  1175. elm['s-rc'] = undefined;
  1176. }
  1177. endRender();
  1178. endUpdate();
  1179. {
  1180. const childrenPromises = elm['s-p'];
  1181. const postUpdate = () => postUpdateComponent(hostRef);
  1182. if (childrenPromises.length === 0) {
  1183. postUpdate();
  1184. }
  1185. else {
  1186. Promise.all(childrenPromises).then(postUpdate);
  1187. hostRef.$flags$ |= 4 /* HOST_FLAGS.isWaitingForChildren */;
  1188. childrenPromises.length = 0;
  1189. }
  1190. }
  1191. };
  1192. const callRender = (hostRef, instance, elm) => {
  1193. try {
  1194. instance = instance.render() ;
  1195. {
  1196. hostRef.$flags$ &= ~16 /* HOST_FLAGS.isQueuedForUpdate */;
  1197. }
  1198. {
  1199. hostRef.$flags$ |= 2 /* HOST_FLAGS.hasRendered */;
  1200. }
  1201. {
  1202. {
  1203. // looks like we've got child nodes to render into this host element
  1204. // or we need to update the css class/attrs on the host element
  1205. // DOM WRITE!
  1206. {
  1207. renderVdom(hostRef, instance);
  1208. }
  1209. }
  1210. }
  1211. }
  1212. catch (e) {
  1213. consoleError(e, hostRef.$hostElement$);
  1214. }
  1215. return null;
  1216. };
  1217. const postUpdateComponent = (hostRef) => {
  1218. const tagName = hostRef.$cmpMeta$.$tagName$;
  1219. const elm = hostRef.$hostElement$;
  1220. const endPostUpdate = createTime('postUpdate', tagName);
  1221. const instance = hostRef.$lazyInstance$ ;
  1222. const ancestorComponent = hostRef.$ancestorComponent$;
  1223. {
  1224. safeCall(instance, 'componentDidRender');
  1225. }
  1226. if (!(hostRef.$flags$ & 64 /* HOST_FLAGS.hasLoadedComponent */)) {
  1227. hostRef.$flags$ |= 64 /* HOST_FLAGS.hasLoadedComponent */;
  1228. {
  1229. // DOM WRITE!
  1230. addHydratedFlag(elm);
  1231. }
  1232. {
  1233. safeCall(instance, 'componentDidLoad');
  1234. }
  1235. endPostUpdate();
  1236. {
  1237. hostRef.$onReadyResolve$(elm);
  1238. if (!ancestorComponent) {
  1239. appDidLoad();
  1240. }
  1241. }
  1242. }
  1243. else {
  1244. endPostUpdate();
  1245. }
  1246. {
  1247. hostRef.$onInstanceResolve$(elm);
  1248. }
  1249. // load events fire from bottom to top
  1250. // the deepest elements load first then bubbles up
  1251. {
  1252. if (hostRef.$onRenderResolve$) {
  1253. hostRef.$onRenderResolve$();
  1254. hostRef.$onRenderResolve$ = undefined;
  1255. }
  1256. if (hostRef.$flags$ & 512 /* HOST_FLAGS.needsRerender */) {
  1257. nextTick(() => scheduleUpdate(hostRef, false));
  1258. }
  1259. hostRef.$flags$ &= ~(4 /* HOST_FLAGS.isWaitingForChildren */ | 512 /* HOST_FLAGS.needsRerender */);
  1260. }
  1261. // ( •_•)
  1262. // ( •_•)>⌐■-■
  1263. // (⌐■_■)
  1264. };
  1265. const forceUpdate = (ref) => {
  1266. {
  1267. const hostRef = getHostRef(ref);
  1268. const isConnected = hostRef.$hostElement$.isConnected;
  1269. if (isConnected &&
  1270. (hostRef.$flags$ & (2 /* HOST_FLAGS.hasRendered */ | 16 /* HOST_FLAGS.isQueuedForUpdate */)) === 2 /* HOST_FLAGS.hasRendered */) {
  1271. scheduleUpdate(hostRef, false);
  1272. }
  1273. // Returns "true" when the forced update was successfully scheduled
  1274. return isConnected;
  1275. }
  1276. };
  1277. const appDidLoad = (who) => {
  1278. // on appload
  1279. // we have finish the first big initial render
  1280. {
  1281. addHydratedFlag(doc.documentElement);
  1282. }
  1283. nextTick(() => emitEvent(win, 'appload', { detail: { namespace: NAMESPACE } }));
  1284. };
  1285. const safeCall = (instance, method, arg) => {
  1286. if (instance && instance[method]) {
  1287. try {
  1288. return instance[method](arg);
  1289. }
  1290. catch (e) {
  1291. consoleError(e);
  1292. }
  1293. }
  1294. return undefined;
  1295. };
  1296. const then = (promise, thenFn) => {
  1297. return promise && promise.then ? promise.then(thenFn) : thenFn();
  1298. };
  1299. const addHydratedFlag = (elm) => elm.setAttribute('calcite-hydrated', '')
  1300. ;
  1301. const getValue = (ref, propName) => getHostRef(ref).$instanceValues$.get(propName);
  1302. const setValue = (ref, propName, newVal, cmpMeta) => {
  1303. // check our new property value against our internal value
  1304. const hostRef = getHostRef(ref);
  1305. const elm = hostRef.$hostElement$ ;
  1306. const oldVal = hostRef.$instanceValues$.get(propName);
  1307. const flags = hostRef.$flags$;
  1308. const instance = hostRef.$lazyInstance$ ;
  1309. newVal = parsePropertyValue(newVal, cmpMeta.$members$[propName][0]);
  1310. // explicitly check for NaN on both sides, as `NaN === NaN` is always false
  1311. const areBothNaN = Number.isNaN(oldVal) && Number.isNaN(newVal);
  1312. const didValueChange = newVal !== oldVal && !areBothNaN;
  1313. if ((!(flags & 8 /* HOST_FLAGS.isConstructingInstance */) || oldVal === undefined) && didValueChange) {
  1314. // gadzooks! the property's value has changed!!
  1315. // set our new value!
  1316. hostRef.$instanceValues$.set(propName, newVal);
  1317. if (instance) {
  1318. // get an array of method names of watch functions to call
  1319. if (cmpMeta.$watchers$ && flags & 128 /* HOST_FLAGS.isWatchReady */) {
  1320. const watchMethods = cmpMeta.$watchers$[propName];
  1321. if (watchMethods) {
  1322. // this instance is watching for when this property changed
  1323. watchMethods.map((watchMethodName) => {
  1324. try {
  1325. // fire off each of the watch methods that are watching this property
  1326. instance[watchMethodName](newVal, oldVal, propName);
  1327. }
  1328. catch (e) {
  1329. consoleError(e, elm);
  1330. }
  1331. });
  1332. }
  1333. }
  1334. if ((flags & (2 /* HOST_FLAGS.hasRendered */ | 16 /* HOST_FLAGS.isQueuedForUpdate */)) === 2 /* HOST_FLAGS.hasRendered */) {
  1335. if (instance.componentShouldUpdate) {
  1336. if (instance.componentShouldUpdate(newVal, oldVal, propName) === false) {
  1337. return;
  1338. }
  1339. }
  1340. // looks like this value actually changed, so we've got work to do!
  1341. // but only if we've already rendered, otherwise just chill out
  1342. // queue that we need to do an update, but don't worry about queuing
  1343. // up millions cuz this function ensures it only runs once
  1344. scheduleUpdate(hostRef, false);
  1345. }
  1346. }
  1347. }
  1348. };
  1349. /**
  1350. * Attach a series of runtime constructs to a compiled Stencil component
  1351. * constructor, including getters and setters for the `@Prop` and `@State`
  1352. * decorators, callbacks for when attributes change, and so on.
  1353. *
  1354. * @param Cstr the constructor for a component that we need to process
  1355. * @param cmpMeta metadata collected previously about the component
  1356. * @param flags a number used to store a series of bit flags
  1357. * @returns a reference to the same constructor passed in (but now mutated)
  1358. */
  1359. const proxyComponent = (Cstr, cmpMeta, flags) => {
  1360. if (cmpMeta.$members$) {
  1361. if (Cstr.watchers) {
  1362. cmpMeta.$watchers$ = Cstr.watchers;
  1363. }
  1364. // It's better to have a const than two Object.entries()
  1365. const members = Object.entries(cmpMeta.$members$);
  1366. const prototype = Cstr.prototype;
  1367. members.map(([memberName, [memberFlags]]) => {
  1368. if ((memberFlags & 31 /* MEMBER_FLAGS.Prop */ ||
  1369. ((flags & 2 /* PROXY_FLAGS.proxyState */) && memberFlags & 32 /* MEMBER_FLAGS.State */))) {
  1370. // proxyComponent - prop
  1371. Object.defineProperty(prototype, memberName, {
  1372. get() {
  1373. // proxyComponent, get value
  1374. return getValue(this, memberName);
  1375. },
  1376. set(newValue) {
  1377. // proxyComponent, set value
  1378. setValue(this, memberName, newValue, cmpMeta);
  1379. },
  1380. configurable: true,
  1381. enumerable: true,
  1382. });
  1383. }
  1384. else if (flags & 1 /* PROXY_FLAGS.isElementConstructor */ &&
  1385. memberFlags & 64 /* MEMBER_FLAGS.Method */) {
  1386. // proxyComponent - method
  1387. Object.defineProperty(prototype, memberName, {
  1388. value(...args) {
  1389. const ref = getHostRef(this);
  1390. return ref.$onInstancePromise$.then(() => ref.$lazyInstance$[memberName](...args));
  1391. },
  1392. });
  1393. }
  1394. });
  1395. if ((flags & 1 /* PROXY_FLAGS.isElementConstructor */)) {
  1396. const attrNameToPropName = new Map();
  1397. prototype.attributeChangedCallback = function (attrName, _oldValue, newValue) {
  1398. plt.jmp(() => {
  1399. const propName = attrNameToPropName.get(attrName);
  1400. // In a web component lifecycle the attributeChangedCallback runs prior to connectedCallback
  1401. // in the case where an attribute was set inline.
  1402. // ```html
  1403. // <my-component some-attribute="some-value"></my-component>
  1404. // ```
  1405. //
  1406. // There is an edge case where a developer sets the attribute inline on a custom element and then
  1407. // programmatically changes it before it has been upgraded as shown below:
  1408. //
  1409. // ```html
  1410. // <!-- this component has _not_ been upgraded yet -->
  1411. // <my-component id="test" some-attribute="some-value"></my-component>
  1412. // <script>
  1413. // // grab non-upgraded component
  1414. // el = document.querySelector("#test");
  1415. // el.someAttribute = "another-value";
  1416. // // upgrade component
  1417. // customElements.define('my-component', MyComponent);
  1418. // </script>
  1419. // ```
  1420. // In this case if we do not unshadow here and use the value of the shadowing property, attributeChangedCallback
  1421. // will be called with `newValue = "some-value"` and will set the shadowed property (this.someAttribute = "another-value")
  1422. // to the value that was set inline i.e. "some-value" from above example. When
  1423. // the connectedCallback attempts to unshadow it will use "some-value" as the initial value rather than "another-value"
  1424. //
  1425. // The case where the attribute was NOT set inline but was not set programmatically shall be handled/unshadowed
  1426. // by connectedCallback as this attributeChangedCallback will not fire.
  1427. //
  1428. // https://developers.google.com/web/fundamentals/web-components/best-practices#lazy-properties
  1429. //
  1430. // TODO(STENCIL-16) we should think about whether or not we actually want to be reflecting the attributes to
  1431. // properties here given that this goes against best practices outlined here
  1432. // https://developers.google.com/web/fundamentals/web-components/best-practices#avoid-reentrancy
  1433. if (this.hasOwnProperty(propName)) {
  1434. newValue = this[propName];
  1435. delete this[propName];
  1436. }
  1437. else if (prototype.hasOwnProperty(propName) &&
  1438. typeof this[propName] === 'number' &&
  1439. this[propName] == newValue) {
  1440. // if the propName exists on the prototype of `Cstr`, this update may be a result of Stencil using native
  1441. // APIs to reflect props as attributes. Calls to `setAttribute(someElement, propName)` will result in
  1442. // `propName` to be converted to a `DOMString`, which may not be what we want for other primitive props.
  1443. return;
  1444. }
  1445. this[propName] = newValue === null && typeof this[propName] === 'boolean' ? false : newValue;
  1446. });
  1447. };
  1448. // create an array of attributes to observe
  1449. // and also create a map of html attribute name to js property name
  1450. Cstr.observedAttributes = members
  1451. .filter(([_, m]) => m[0] & 15 /* MEMBER_FLAGS.HasAttribute */) // filter to only keep props that should match attributes
  1452. .map(([propName, m]) => {
  1453. const attrName = m[1] || propName;
  1454. attrNameToPropName.set(attrName, propName);
  1455. if (m[0] & 512 /* MEMBER_FLAGS.ReflectAttr */) {
  1456. cmpMeta.$attrsToReflect$.push([propName, attrName]);
  1457. }
  1458. return attrName;
  1459. });
  1460. }
  1461. }
  1462. return Cstr;
  1463. };
  1464. const initializeComponent = async (elm, hostRef, cmpMeta, hmrVersionId, Cstr) => {
  1465. // initializeComponent
  1466. if ((hostRef.$flags$ & 32 /* HOST_FLAGS.hasInitializedComponent */) === 0) {
  1467. {
  1468. // we haven't initialized this element yet
  1469. hostRef.$flags$ |= 32 /* HOST_FLAGS.hasInitializedComponent */;
  1470. // lazy loaded components
  1471. // request the component's implementation to be
  1472. // wired up with the host element
  1473. Cstr = loadModule(cmpMeta);
  1474. if (Cstr.then) {
  1475. // Await creates a micro-task avoid if possible
  1476. const endLoad = uniqueTime();
  1477. Cstr = await Cstr;
  1478. endLoad();
  1479. }
  1480. if (!Cstr.isProxied) {
  1481. // we've never proxied this Constructor before
  1482. // let's add the getters/setters to its prototype before
  1483. // the first time we create an instance of the implementation
  1484. {
  1485. cmpMeta.$watchers$ = Cstr.watchers;
  1486. }
  1487. proxyComponent(Cstr, cmpMeta, 2 /* PROXY_FLAGS.proxyState */);
  1488. Cstr.isProxied = true;
  1489. }
  1490. const endNewInstance = createTime('createInstance', cmpMeta.$tagName$);
  1491. // ok, time to construct the instance
  1492. // but let's keep track of when we start and stop
  1493. // so that the getters/setters don't incorrectly step on data
  1494. {
  1495. hostRef.$flags$ |= 8 /* HOST_FLAGS.isConstructingInstance */;
  1496. }
  1497. // construct the lazy-loaded component implementation
  1498. // passing the hostRef is very important during
  1499. // construction in order to directly wire together the
  1500. // host element and the lazy-loaded instance
  1501. try {
  1502. new Cstr(hostRef);
  1503. }
  1504. catch (e) {
  1505. consoleError(e);
  1506. }
  1507. {
  1508. hostRef.$flags$ &= ~8 /* HOST_FLAGS.isConstructingInstance */;
  1509. }
  1510. {
  1511. hostRef.$flags$ |= 128 /* HOST_FLAGS.isWatchReady */;
  1512. }
  1513. endNewInstance();
  1514. fireConnectedCallback(hostRef.$lazyInstance$);
  1515. }
  1516. if (Cstr.style) {
  1517. // this component has styles but we haven't registered them yet
  1518. let style = Cstr.style;
  1519. const scopeId = getScopeId(cmpMeta);
  1520. if (!styles.has(scopeId)) {
  1521. const endRegisterStyles = createTime('registerStyles', cmpMeta.$tagName$);
  1522. registerStyle(scopeId, style, !!(cmpMeta.$flags$ & 1 /* CMP_FLAGS.shadowDomEncapsulation */));
  1523. endRegisterStyles();
  1524. }
  1525. }
  1526. }
  1527. // we've successfully created a lazy instance
  1528. const ancestorComponent = hostRef.$ancestorComponent$;
  1529. const schedule = () => scheduleUpdate(hostRef, true);
  1530. if (ancestorComponent && ancestorComponent['s-rc']) {
  1531. // this is the initial load and this component it has an ancestor component
  1532. // but the ancestor component has NOT fired its will update lifecycle yet
  1533. // so let's just cool our jets and wait for the ancestor to continue first
  1534. // this will get fired off when the ancestor component
  1535. // finally gets around to rendering its lazy self
  1536. // fire off the initial update
  1537. ancestorComponent['s-rc'].push(schedule);
  1538. }
  1539. else {
  1540. schedule();
  1541. }
  1542. };
  1543. const fireConnectedCallback = (instance) => {
  1544. {
  1545. safeCall(instance, 'connectedCallback');
  1546. }
  1547. };
  1548. const connectedCallback = (elm) => {
  1549. if ((plt.$flags$ & 1 /* PLATFORM_FLAGS.isTmpDisconnected */) === 0) {
  1550. const hostRef = getHostRef(elm);
  1551. const cmpMeta = hostRef.$cmpMeta$;
  1552. const endConnected = createTime('connectedCallback', cmpMeta.$tagName$);
  1553. if (!(hostRef.$flags$ & 1 /* HOST_FLAGS.hasConnected */)) {
  1554. // first time this component has connected
  1555. hostRef.$flags$ |= 1 /* HOST_FLAGS.hasConnected */;
  1556. let hostId;
  1557. {
  1558. hostId = elm.getAttribute(HYDRATE_ID);
  1559. if (hostId) {
  1560. if (cmpMeta.$flags$ & 1 /* CMP_FLAGS.shadowDomEncapsulation */) {
  1561. const scopeId = addStyle(elm.shadowRoot, cmpMeta);
  1562. elm.classList.remove(scopeId + '-h', scopeId + '-s');
  1563. }
  1564. initializeClientHydrate(elm, cmpMeta.$tagName$, hostId, hostRef);
  1565. }
  1566. }
  1567. {
  1568. // find the first ancestor component (if there is one) and register
  1569. // this component as one of the actively loading child components for its ancestor
  1570. let ancestorComponent = elm;
  1571. while ((ancestorComponent = ancestorComponent.parentNode || ancestorComponent.host)) {
  1572. // climb up the ancestors looking for the first
  1573. // component that hasn't finished its lifecycle update yet
  1574. if ((ancestorComponent.nodeType === 1 /* NODE_TYPE.ElementNode */ &&
  1575. ancestorComponent.hasAttribute('s-id') &&
  1576. ancestorComponent['s-p']) ||
  1577. ancestorComponent['s-p']) {
  1578. // we found this components first ancestor component
  1579. // keep a reference to this component's ancestor component
  1580. attachToAncestor(hostRef, (hostRef.$ancestorComponent$ = ancestorComponent));
  1581. break;
  1582. }
  1583. }
  1584. }
  1585. // Lazy properties
  1586. // https://developers.google.com/web/fundamentals/web-components/best-practices#lazy-properties
  1587. if (cmpMeta.$members$) {
  1588. Object.entries(cmpMeta.$members$).map(([memberName, [memberFlags]]) => {
  1589. if (memberFlags & 31 /* MEMBER_FLAGS.Prop */ && elm.hasOwnProperty(memberName)) {
  1590. const value = elm[memberName];
  1591. delete elm[memberName];
  1592. elm[memberName] = value;
  1593. }
  1594. });
  1595. }
  1596. {
  1597. initializeComponent(elm, hostRef, cmpMeta);
  1598. }
  1599. }
  1600. else {
  1601. // not the first time this has connected
  1602. // reattach any event listeners to the host
  1603. // since they would have been removed when disconnected
  1604. addHostEventListeners(elm, hostRef, cmpMeta.$listeners$);
  1605. // fire off connectedCallback() on component instance
  1606. fireConnectedCallback(hostRef.$lazyInstance$);
  1607. }
  1608. endConnected();
  1609. }
  1610. };
  1611. const disconnectedCallback = (elm) => {
  1612. if ((plt.$flags$ & 1 /* PLATFORM_FLAGS.isTmpDisconnected */) === 0) {
  1613. const hostRef = getHostRef(elm);
  1614. const instance = hostRef.$lazyInstance$ ;
  1615. {
  1616. if (hostRef.$rmListeners$) {
  1617. hostRef.$rmListeners$.map((rmListener) => rmListener());
  1618. hostRef.$rmListeners$ = undefined;
  1619. }
  1620. }
  1621. {
  1622. safeCall(instance, 'disconnectedCallback');
  1623. }
  1624. }
  1625. };
  1626. const bootstrapLazy = (lazyBundles, options = {}) => {
  1627. const endBootstrap = createTime();
  1628. const cmpTags = [];
  1629. const exclude = options.exclude || [];
  1630. const customElements = win.customElements;
  1631. const head = doc.head;
  1632. const metaCharset = /*@__PURE__*/ head.querySelector('meta[charset]');
  1633. const visibilityStyle = /*@__PURE__*/ doc.createElement('style');
  1634. const deferredConnectedCallbacks = [];
  1635. const styles = /*@__PURE__*/ doc.querySelectorAll(`[${HYDRATED_STYLE_ID}]`);
  1636. let appLoadFallback;
  1637. let isBootstrapping = true;
  1638. let i = 0;
  1639. Object.assign(plt, options);
  1640. plt.$resourcesUrl$ = new URL(options.resourcesUrl || './', doc.baseURI).href;
  1641. {
  1642. // If the app is already hydrated there is not point to disable the
  1643. // async queue. This will improve the first input delay
  1644. plt.$flags$ |= 2 /* PLATFORM_FLAGS.appLoaded */;
  1645. }
  1646. {
  1647. for (; i < styles.length; i++) {
  1648. registerStyle(styles[i].getAttribute(HYDRATED_STYLE_ID), convertScopedToShadow(styles[i].innerHTML), true);
  1649. }
  1650. }
  1651. lazyBundles.map((lazyBundle) => {
  1652. lazyBundle[1].map((compactMeta) => {
  1653. const cmpMeta = {
  1654. $flags$: compactMeta[0],
  1655. $tagName$: compactMeta[1],
  1656. $members$: compactMeta[2],
  1657. $listeners$: compactMeta[3],
  1658. };
  1659. {
  1660. cmpMeta.$members$ = compactMeta[2];
  1661. }
  1662. {
  1663. cmpMeta.$listeners$ = compactMeta[3];
  1664. }
  1665. {
  1666. cmpMeta.$attrsToReflect$ = [];
  1667. }
  1668. {
  1669. cmpMeta.$watchers$ = {};
  1670. }
  1671. const tagName = cmpMeta.$tagName$;
  1672. const HostElement = class extends HTMLElement {
  1673. // StencilLazyHost
  1674. constructor(self) {
  1675. // @ts-ignore
  1676. super(self);
  1677. self = this;
  1678. registerHost(self, cmpMeta);
  1679. if (cmpMeta.$flags$ & 1 /* CMP_FLAGS.shadowDomEncapsulation */) {
  1680. // this component is using shadow dom
  1681. // and this browser supports shadow dom
  1682. // add the read-only property "shadowRoot" to the host element
  1683. // adding the shadow root build conditionals to minimize runtime
  1684. {
  1685. {
  1686. self.attachShadow({ mode: 'open' });
  1687. }
  1688. }
  1689. }
  1690. }
  1691. connectedCallback() {
  1692. if (appLoadFallback) {
  1693. clearTimeout(appLoadFallback);
  1694. appLoadFallback = null;
  1695. }
  1696. if (isBootstrapping) {
  1697. // connectedCallback will be processed once all components have been registered
  1698. deferredConnectedCallbacks.push(this);
  1699. }
  1700. else {
  1701. plt.jmp(() => connectedCallback(this));
  1702. }
  1703. }
  1704. disconnectedCallback() {
  1705. plt.jmp(() => disconnectedCallback(this));
  1706. }
  1707. componentOnReady() {
  1708. return getHostRef(this).$onReadyPromise$;
  1709. }
  1710. };
  1711. cmpMeta.$lazyBundleId$ = lazyBundle[0];
  1712. if (!exclude.includes(tagName) && !customElements.get(tagName)) {
  1713. cmpTags.push(tagName);
  1714. customElements.define(tagName, proxyComponent(HostElement, cmpMeta, 1 /* PROXY_FLAGS.isElementConstructor */));
  1715. }
  1716. });
  1717. });
  1718. {
  1719. visibilityStyle.innerHTML = cmpTags + HYDRATED_CSS;
  1720. visibilityStyle.setAttribute('data-styles', '');
  1721. head.insertBefore(visibilityStyle, metaCharset ? metaCharset.nextSibling : head.firstChild);
  1722. }
  1723. // Process deferred connectedCallbacks now all components have been registered
  1724. isBootstrapping = false;
  1725. if (deferredConnectedCallbacks.length) {
  1726. deferredConnectedCallbacks.map((host) => host.connectedCallback());
  1727. }
  1728. else {
  1729. {
  1730. plt.jmp(() => (appLoadFallback = setTimeout(appDidLoad, 30)));
  1731. }
  1732. }
  1733. // Fallback appLoad event
  1734. endBootstrap();
  1735. };
  1736. const Fragment = (_, children) => children;
  1737. const addHostEventListeners = (elm, hostRef, listeners, attachParentListeners) => {
  1738. if (listeners) {
  1739. listeners.map(([flags, name, method]) => {
  1740. const target = getHostListenerTarget(elm, flags) ;
  1741. const handler = hostListenerProxy(hostRef, method);
  1742. const opts = hostListenerOpts(flags);
  1743. plt.ael(target, name, handler, opts);
  1744. (hostRef.$rmListeners$ = hostRef.$rmListeners$ || []).push(() => plt.rel(target, name, handler, opts));
  1745. });
  1746. }
  1747. };
  1748. const hostListenerProxy = (hostRef, methodName) => (ev) => {
  1749. try {
  1750. {
  1751. if (hostRef.$flags$ & 256 /* HOST_FLAGS.isListenReady */) {
  1752. // instance is ready, let's call it's member method for this event
  1753. hostRef.$lazyInstance$[methodName](ev);
  1754. }
  1755. else {
  1756. (hostRef.$queuedListeners$ = hostRef.$queuedListeners$ || []).push([methodName, ev]);
  1757. }
  1758. }
  1759. }
  1760. catch (e) {
  1761. consoleError(e);
  1762. }
  1763. };
  1764. const getHostListenerTarget = (elm, flags) => {
  1765. if (flags & 4 /* LISTENER_FLAGS.TargetDocument */)
  1766. return doc;
  1767. if (flags & 8 /* LISTENER_FLAGS.TargetWindow */)
  1768. return win;
  1769. if (flags & 16 /* LISTENER_FLAGS.TargetBody */)
  1770. return doc.body;
  1771. return elm;
  1772. };
  1773. // prettier-ignore
  1774. const hostListenerOpts = (flags) => (flags & 2 /* LISTENER_FLAGS.Capture */) !== 0;
  1775. const hostRefs = /*@__PURE__*/ new WeakMap();
  1776. const getHostRef = (ref) => hostRefs.get(ref);
  1777. const registerInstance = (lazyInstance, hostRef) => hostRefs.set((hostRef.$lazyInstance$ = lazyInstance), hostRef);
  1778. const registerHost = (elm, cmpMeta) => {
  1779. const hostRef = {
  1780. $flags$: 0,
  1781. $hostElement$: elm,
  1782. $cmpMeta$: cmpMeta,
  1783. $instanceValues$: new Map(),
  1784. };
  1785. {
  1786. hostRef.$onInstancePromise$ = new Promise((r) => (hostRef.$onInstanceResolve$ = r));
  1787. }
  1788. {
  1789. hostRef.$onReadyPromise$ = new Promise((r) => (hostRef.$onReadyResolve$ = r));
  1790. elm['s-p'] = [];
  1791. elm['s-rc'] = [];
  1792. }
  1793. addHostEventListeners(elm, hostRef, cmpMeta.$listeners$);
  1794. return hostRefs.set(elm, hostRef);
  1795. };
  1796. const isMemberInElement = (elm, memberName) => memberName in elm;
  1797. const consoleError = (e, el) => (0, console.error)(e, el);
  1798. const cmpModules = /*@__PURE__*/ new Map();
  1799. const loadModule = (cmpMeta, hostRef, hmrVersionId) => {
  1800. // loadModuleImport
  1801. const exportName = cmpMeta.$tagName$.replace(/-/g, '_');
  1802. const bundleId = cmpMeta.$lazyBundleId$;
  1803. const module = cmpModules.get(bundleId) ;
  1804. if (module) {
  1805. return module[exportName];
  1806. }
  1807. /*!__STENCIL_STATIC_IMPORT_SWITCH__*/
  1808. return import(
  1809. /* @vite-ignore */
  1810. /* webpackInclude: /\.entry\.js$/ */
  1811. /* webpackExclude: /\.system\.entry\.js$/ */
  1812. /* webpackMode: "lazy" */
  1813. `./${bundleId}.entry.js${''}`).then((importedModule) => {
  1814. {
  1815. cmpModules.set(bundleId, importedModule);
  1816. }
  1817. return importedModule[exportName];
  1818. }, consoleError);
  1819. };
  1820. const styles = /*@__PURE__*/ new Map();
  1821. const win = typeof window !== 'undefined' ? window : {};
  1822. const doc = win.document || { head: {} };
  1823. const plt = {
  1824. $flags$: 0,
  1825. $resourcesUrl$: '',
  1826. jmp: (h) => h(),
  1827. raf: (h) => requestAnimationFrame(h),
  1828. ael: (el, eventName, listener, opts) => el.addEventListener(eventName, listener, opts),
  1829. rel: (el, eventName, listener, opts) => el.removeEventListener(eventName, listener, opts),
  1830. ce: (eventName, opts) => new CustomEvent(eventName, opts),
  1831. };
  1832. const supportsShadow = true;
  1833. const promiseResolve = (v) => Promise.resolve(v);
  1834. const supportsConstructableStylesheets = /*@__PURE__*/ (() => {
  1835. try {
  1836. new CSSStyleSheet();
  1837. return typeof new CSSStyleSheet().replaceSync === 'function';
  1838. }
  1839. catch (e) { }
  1840. return false;
  1841. })()
  1842. ;
  1843. const queueDomReads = [];
  1844. const queueDomWrites = [];
  1845. const queueTask = (queue, write) => (cb) => {
  1846. queue.push(cb);
  1847. if (!queuePending) {
  1848. queuePending = true;
  1849. if (write && plt.$flags$ & 4 /* PLATFORM_FLAGS.queueSync */) {
  1850. nextTick(flush);
  1851. }
  1852. else {
  1853. plt.raf(flush);
  1854. }
  1855. }
  1856. };
  1857. const consume = (queue) => {
  1858. for (let i = 0; i < queue.length; i++) {
  1859. try {
  1860. queue[i](performance.now());
  1861. }
  1862. catch (e) {
  1863. consoleError(e);
  1864. }
  1865. }
  1866. queue.length = 0;
  1867. };
  1868. const flush = () => {
  1869. // always force a bunch of medium callbacks to run, but still have
  1870. // a throttle on how many can run in a certain time
  1871. // DOM READS!!!
  1872. consume(queueDomReads);
  1873. // DOM WRITES!!!
  1874. {
  1875. consume(queueDomWrites);
  1876. if ((queuePending = queueDomReads.length > 0)) {
  1877. // still more to do yet, but we've run out of time
  1878. // let's let this thing cool off and try again in the next tick
  1879. plt.raf(flush);
  1880. }
  1881. }
  1882. };
  1883. const nextTick = /*@__PURE__*/ (cb) => promiseResolve().then(cb);
  1884. const writeTask = /*@__PURE__*/ queueTask(queueDomWrites, true);
  1885. export { Fragment as F, Host as H, NAMESPACE as N, getAssetPath as a, bootstrapLazy as b, createEvent as c, doc as d, forceUpdate as f, getElement as g, h, promiseResolve as p, registerInstance as r };