snice 2.5.4 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +501 -882
- package/bin/templates/base/src/components/counter-button.ts +13 -26
- package/bin/templates/base/src/controllers/counter-controller.ts +3 -3
- package/dist/components/accordion/snice-accordion-item.d.ts +4 -5
- package/dist/components/accordion/snice-accordion-item.js +37 -39
- package/dist/components/accordion/snice-accordion-item.js.map +1 -1
- package/dist/components/accordion/snice-accordion.d.ts +5 -11
- package/dist/components/accordion/snice-accordion.js +51 -52
- package/dist/components/accordion/snice-accordion.js.map +1 -1
- package/dist/components/alert/snice-alert.d.ts +2 -6
- package/dist/components/alert/snice-alert.js +41 -56
- package/dist/components/alert/snice-alert.js.map +1 -1
- package/dist/components/avatar/snice-avatar.d.ts +2 -6
- package/dist/components/avatar/snice-avatar.js +64 -71
- package/dist/components/avatar/snice-avatar.js.map +1 -1
- package/dist/components/badge/snice-badge.d.ts +2 -3
- package/dist/components/badge/snice-badge.js +22 -23
- package/dist/components/badge/snice-badge.js.map +1 -1
- package/dist/components/breadcrumbs/snice-breadcrumbs.d.ts +5 -12
- package/dist/components/breadcrumbs/snice-breadcrumbs.js +88 -89
- package/dist/components/breadcrumbs/snice-breadcrumbs.js.map +1 -1
- package/dist/components/button/snice-button.d.ts +3 -7
- package/dist/components/button/snice-button.js +37 -58
- package/dist/components/button/snice-button.js.map +1 -1
- package/dist/components/card/snice-card.d.ts +5 -8
- package/dist/components/card/snice-card.js +71 -56
- package/dist/components/card/snice-card.js.map +1 -1
- package/dist/components/checkbox/snice-checkbox.d.ts +4 -13
- package/dist/components/checkbox/snice-checkbox.js +66 -137
- package/dist/components/checkbox/snice-checkbox.js.map +1 -1
- package/dist/components/chip/snice-chip.d.ts +5 -11
- package/dist/components/chip/snice-chip.js +44 -47
- package/dist/components/chip/snice-chip.js.map +1 -1
- package/dist/components/date-picker/snice-date-picker.d.ts +11 -11
- package/dist/components/date-picker/snice-date-picker.js +134 -133
- package/dist/components/date-picker/snice-date-picker.js.map +1 -1
- package/dist/components/divider/snice-divider.d.ts +2 -4
- package/dist/components/divider/snice-divider.js +14 -22
- package/dist/components/divider/snice-divider.js.map +1 -1
- package/dist/components/drawer/snice-drawer.d.ts +4 -4
- package/dist/components/drawer/snice-drawer.js +25 -19
- package/dist/components/drawer/snice-drawer.js.map +1 -1
- package/dist/components/input/snice-input.d.ts +8 -6
- package/dist/components/input/snice-input.js +122 -105
- package/dist/components/input/snice-input.js.map +1 -1
- package/dist/components/layout/snice-layout-blog.d.ts +4 -4
- package/dist/components/layout/snice-layout-blog.js +21 -19
- package/dist/components/layout/snice-layout-blog.js.map +1 -1
- package/dist/components/layout/snice-layout-card.d.ts +2 -2
- package/dist/components/layout/snice-layout-card.js +16 -9
- package/dist/components/layout/snice-layout-card.js.map +1 -1
- package/dist/components/layout/snice-layout-centered.d.ts +2 -2
- package/dist/components/layout/snice-layout-centered.js +14 -7
- package/dist/components/layout/snice-layout-centered.js.map +1 -1
- package/dist/components/layout/snice-layout-dashboard.d.ts +5 -5
- package/dist/components/layout/snice-layout-dashboard.js +38 -30
- package/dist/components/layout/snice-layout-dashboard.js.map +1 -1
- package/dist/components/layout/snice-layout-fullscreen.d.ts +2 -2
- package/dist/components/layout/snice-layout-fullscreen.js +17 -10
- package/dist/components/layout/snice-layout-fullscreen.js.map +1 -1
- package/dist/components/layout/snice-layout-landing.d.ts +4 -4
- package/dist/components/layout/snice-layout-landing.js +21 -19
- package/dist/components/layout/snice-layout-landing.js.map +1 -1
- package/dist/components/layout/snice-layout-minimal.d.ts +2 -2
- package/dist/components/layout/snice-layout-minimal.js +17 -6
- package/dist/components/layout/snice-layout-minimal.js.map +1 -1
- package/dist/components/layout/snice-layout-sidebar.d.ts +5 -4
- package/dist/components/layout/snice-layout-sidebar.js +42 -20
- package/dist/components/layout/snice-layout-sidebar.js.map +1 -1
- package/dist/components/layout/snice-layout-split.d.ts +2 -2
- package/dist/components/layout/snice-layout-split.js +14 -7
- package/dist/components/layout/snice-layout-split.js.map +1 -1
- package/dist/components/layout/snice-layout.d.ts +4 -4
- package/dist/components/layout/snice-layout.js +16 -10
- package/dist/components/layout/snice-layout.js.map +1 -1
- package/dist/components/login/snice-login.d.ts +6 -11
- package/dist/components/login/snice-login.js +97 -71
- package/dist/components/login/snice-login.js.map +1 -1
- package/dist/components/modal/snice-modal.d.ts +5 -9
- package/dist/components/modal/snice-modal.js +47 -78
- package/dist/components/modal/snice-modal.js.map +1 -1
- package/dist/components/nav/snice-nav.d.ts +13 -7
- package/dist/components/nav/snice-nav.js +191 -100
- package/dist/components/nav/snice-nav.js.map +1 -1
- package/dist/components/nav/snice-nav.types.d.ts +3 -3
- package/dist/components/pagination/snice-pagination.d.ts +6 -7
- package/dist/components/pagination/snice-pagination.js +94 -81
- package/dist/components/pagination/snice-pagination.js.map +1 -1
- package/dist/components/progress/snice-progress.d.ts +2 -7
- package/dist/components/progress/snice-progress.js +41 -98
- package/dist/components/progress/snice-progress.js.map +1 -1
- package/dist/components/radio/snice-radio.d.ts +4 -4
- package/dist/components/radio/snice-radio.js +52 -44
- package/dist/components/radio/snice-radio.js.map +1 -1
- package/dist/components/select/snice-option.d.ts +2 -1
- package/dist/components/select/snice-option.js +12 -5
- package/dist/components/select/snice-option.js.map +1 -1
- package/dist/components/select/snice-select.d.ts +9 -21
- package/dist/components/select/snice-select.js +98 -170
- package/dist/components/select/snice-select.js.map +1 -1
- package/dist/components/skeleton/snice-skeleton.d.ts +2 -6
- package/dist/components/skeleton/snice-skeleton.js +18 -49
- package/dist/components/skeleton/snice-skeleton.js.map +1 -1
- package/dist/components/snice-cell-BLFVdxPp.js +4 -0
- package/dist/components/snice-cell-BLFVdxPp.js.map +1 -0
- package/dist/components/switch/snice-switch.d.ts +2 -2
- package/dist/components/switch/snice-switch.js +38 -26
- package/dist/components/switch/snice-switch.js.map +1 -1
- package/dist/components/table/snice-cell-actions.d.ts +24 -0
- package/dist/components/table/snice-cell-actions.js +149 -0
- package/dist/components/table/snice-cell-actions.js.map +1 -0
- package/dist/components/table/snice-cell-boolean.d.ts +2 -2
- package/dist/components/table/snice-cell-boolean.js +13 -7
- package/dist/components/table/snice-cell-boolean.js.map +1 -1
- package/dist/components/table/snice-cell-color.d.ts +18 -0
- package/dist/components/table/snice-cell-color.js +149 -0
- package/dist/components/table/snice-cell-color.js.map +1 -0
- package/dist/components/table/snice-cell-currency.d.ts +24 -0
- package/dist/components/table/snice-cell-currency.js +235 -0
- package/dist/components/table/snice-cell-currency.js.map +1 -0
- package/dist/components/table/snice-cell-date.d.ts +2 -2
- package/dist/components/table/snice-cell-date.js +14 -8
- package/dist/components/table/snice-cell-date.js.map +1 -1
- package/dist/components/table/snice-cell-duration.d.ts +2 -2
- package/dist/components/table/snice-cell-duration.js +12 -6
- package/dist/components/table/snice-cell-duration.js.map +1 -1
- package/dist/components/table/snice-cell-email.d.ts +15 -0
- package/dist/components/table/snice-cell-email.js +125 -0
- package/dist/components/table/snice-cell-email.js.map +1 -0
- package/dist/components/table/snice-cell-filesize.d.ts +2 -2
- package/dist/components/table/snice-cell-filesize.js +12 -6
- package/dist/components/table/snice-cell-filesize.js.map +1 -1
- package/dist/components/table/snice-cell-image.d.ts +20 -0
- package/dist/components/table/snice-cell-image.js +162 -0
- package/dist/components/table/snice-cell-image.js.map +1 -0
- package/dist/components/table/snice-cell-json.d.ts +20 -0
- package/dist/components/table/snice-cell-json.js +186 -0
- package/dist/components/table/snice-cell-json.js.map +1 -0
- package/dist/components/table/snice-cell-link.d.ts +17 -0
- package/dist/components/table/snice-cell-link.js +142 -0
- package/dist/components/table/snice-cell-link.js.map +1 -0
- package/dist/components/table/snice-cell-location.d.ts +19 -0
- package/dist/components/table/snice-cell-location.js +185 -0
- package/dist/components/table/snice-cell-location.js.map +1 -0
- package/dist/components/table/snice-cell-number.d.ts +2 -2
- package/dist/components/table/snice-cell-number.js +12 -6
- package/dist/components/table/snice-cell-number.js.map +1 -1
- package/dist/components/table/snice-cell-percentage.d.ts +22 -0
- package/dist/components/table/snice-cell-percentage.js +208 -0
- package/dist/components/table/snice-cell-percentage.js.map +1 -0
- package/dist/components/table/snice-cell-phone.d.ts +18 -0
- package/dist/components/table/snice-cell-phone.js +153 -0
- package/dist/components/table/snice-cell-phone.js.map +1 -0
- package/dist/components/table/snice-cell-progress.d.ts +2 -2
- package/dist/components/table/snice-cell-progress.js +12 -6
- package/dist/components/table/snice-cell-progress.js.map +1 -1
- package/dist/components/table/snice-cell-rating.d.ts +2 -2
- package/dist/components/table/snice-cell-rating.js +12 -6
- package/dist/components/table/snice-cell-rating.js.map +1 -1
- package/dist/components/table/snice-cell-sparkline.d.ts +2 -2
- package/dist/components/table/snice-cell-sparkline.js +13 -7
- package/dist/components/table/snice-cell-sparkline.js.map +1 -1
- package/dist/components/table/snice-cell-status.d.ts +17 -0
- package/dist/components/table/snice-cell-status.js +144 -0
- package/dist/components/table/snice-cell-status.js.map +1 -0
- package/dist/components/table/snice-cell-tag.d.ts +16 -0
- package/dist/components/table/snice-cell-tag.js +131 -0
- package/dist/components/table/snice-cell-tag.js.map +1 -0
- package/dist/components/table/snice-cell-text.d.ts +2 -2
- package/dist/components/table/snice-cell-text.js +14 -8
- package/dist/components/table/snice-cell-text.js.map +1 -1
- package/dist/components/table/snice-cell.d.ts +2 -2
- package/dist/components/table/snice-cell.js +12 -6
- package/dist/components/table/snice-cell.js.map +1 -1
- package/dist/components/table/snice-column.d.ts +1 -1
- package/dist/components/table/snice-column.js +6 -3
- package/dist/components/table/snice-column.js.map +1 -1
- package/dist/components/table/snice-header.d.ts +5 -5
- package/dist/components/table/snice-header.js +60 -50
- package/dist/components/table/snice-header.js.map +1 -1
- package/dist/components/table/snice-progress.d.ts +2 -2
- package/dist/components/table/snice-progress.js +18 -11
- package/dist/components/table/snice-progress.js.map +1 -1
- package/dist/components/table/snice-rating.d.ts +2 -2
- package/dist/components/table/snice-rating.js +15 -8
- package/dist/components/table/snice-rating.js.map +1 -1
- package/dist/components/table/snice-row.d.ts +17 -6
- package/dist/components/table/snice-row.js +95 -44
- package/dist/components/table/snice-row.js.map +1 -1
- package/dist/components/table/snice-table.d.ts +18 -10
- package/dist/components/table/snice-table.js +355 -173
- package/dist/components/table/snice-table.js.map +1 -1
- package/dist/components/table/snice-table.types.d.ts +101 -2
- package/dist/components/tabs/snice-tab-panel.d.ts +2 -2
- package/dist/components/tabs/snice-tab-panel.js +12 -6
- package/dist/components/tabs/snice-tab-panel.js.map +1 -1
- package/dist/components/tabs/snice-tab.d.ts +6 -5
- package/dist/components/tabs/snice-tab.js +36 -19
- package/dist/components/tabs/snice-tab.js.map +1 -1
- package/dist/components/tabs/snice-tabs.d.ts +5 -5
- package/dist/components/tabs/snice-tabs.js +38 -28
- package/dist/components/tabs/snice-tabs.js.map +1 -1
- package/dist/components/toast/snice-toast-container.d.ts +7 -7
- package/dist/components/toast/snice-toast-container.js +19 -12
- package/dist/components/toast/snice-toast-container.js.map +1 -1
- package/dist/components/toast/snice-toast.d.ts +3 -15
- package/dist/components/toast/snice-toast.js +49 -108
- package/dist/components/toast/snice-toast.js.map +1 -1
- package/dist/components/tooltip/snice-tooltip.d.ts +2 -2
- package/dist/components/tooltip/snice-tooltip.js +14 -7
- package/dist/components/tooltip/snice-tooltip.js.map +1 -1
- package/dist/context.d.ts +44 -0
- package/dist/element-ready.d.ts +40 -0
- package/dist/{types/element.d.ts → element.d.ts} +2 -8
- package/dist/{types/events.d.ts → events.d.ts} +0 -4
- package/dist/index.cjs +2589 -605
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +21 -0
- package/dist/index.esm.js +2568 -604
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +2589 -605
- package/dist/index.iife.js.map +1 -1
- package/dist/method-decorators.d.ts +121 -0
- package/dist/on.d.ts +59 -0
- package/dist/parts.d.ts +159 -0
- package/dist/render-debug.d.ts +27 -0
- package/dist/render-tracker.d.ts +14 -0
- package/dist/render.d.ts +96 -0
- package/dist/symbols.cjs +163 -0
- package/dist/symbols.cjs.map +1 -1
- package/dist/{types/symbols.d.ts → symbols.d.ts} +22 -0
- package/dist/symbols.esm.js +27 -3
- package/dist/symbols.esm.js.map +1 -1
- package/dist/template.d.ts +100 -0
- package/dist/transitions.cjs +219 -0
- package/dist/transitions.esm.js +2 -2
- package/dist/types/context.d.ts +48 -0
- package/dist/types/element-options.d.ts +26 -0
- package/dist/types/index.d.ts +25 -9
- package/dist/types/nav-context.d.ts +19 -0
- package/dist/types/{types/on-options.d.ts → on-options.d.ts} +2 -0
- package/dist/types/{types/placard.d.ts → placard.d.ts} +0 -1
- package/docs/ai/README.md +17 -0
- package/docs/ai/api.md +175 -0
- package/docs/ai/architecture.md +160 -0
- package/docs/ai/components/accordion.md +174 -0
- package/docs/ai/components/alert.md +77 -0
- package/docs/ai/components/avatar.md +61 -0
- package/docs/ai/components/badge.md +69 -0
- package/docs/ai/components/breadcrumbs.md +74 -0
- package/docs/ai/components/button.md +75 -0
- package/docs/ai/components/card.md +61 -0
- package/docs/ai/components/checkbox.md +74 -0
- package/docs/ai/components/chip.md +73 -0
- package/docs/ai/components/date-picker.md +75 -0
- package/docs/ai/components/divider.md +66 -0
- package/docs/ai/components/drawer.md +80 -0
- package/docs/ai/components/input.md +111 -0
- package/docs/ai/components/login.md +109 -0
- package/docs/ai/components/modal.md +67 -0
- package/docs/ai/components/nav.md +76 -0
- package/docs/ai/components/pagination.md +55 -0
- package/docs/ai/components/progress.md +72 -0
- package/docs/ai/components/radio.md +79 -0
- package/docs/ai/components/select.md +92 -0
- package/docs/ai/components/skeleton.md +57 -0
- package/docs/ai/components/switch.md +53 -0
- package/docs/ai/components/table.md +227 -0
- package/docs/ai/components/tabs.md +83 -0
- package/docs/ai/components/toast.md +140 -0
- package/docs/ai/components/tooltip.md +146 -0
- package/docs/ai/patterns.md +244 -0
- package/docs/components/accordion.md +558 -0
- package/docs/components/drawer.md +602 -0
- package/docs/components/modal.md +558 -0
- package/docs/components/nav.md +239 -0
- package/docs/components/pagination.md +289 -0
- package/docs/components/select.md +599 -0
- package/docs/components/switch.md +354 -0
- package/docs/components/tabs.md +546 -0
- package/docs/components/toast.md +506 -0
- package/docs/components/tooltip.md +523 -0
- package/docs/controllers.md +744 -0
- package/docs/elements.md +855 -0
- package/docs/events.md +807 -0
- package/docs/migration-v2-to-v3.md +569 -0
- package/docs/observe.md +588 -0
- package/docs/placards.md +401 -0
- package/docs/request-response.md +852 -0
- package/docs/routing.md +1186 -0
- package/package.json +10 -11
- package/dist/components/snice-cell-C9N6yGxQ.js +0 -4
- package/dist/components/snice-cell-C9N6yGxQ.js.map +0 -1
- package/dist/types/types/index.d.ts +0 -23
- /package/dist/{types/controller.d.ts → controller.d.ts} +0 -0
- /package/dist/{types/global.d.ts → global.d.ts} +0 -0
- /package/dist/{types/observe.d.ts → observe.d.ts} +0 -0
- /package/dist/{types/request-response.d.ts → request-response.d.ts} +0 -0
- /package/dist/{types/router.d.ts → router.d.ts} +0 -0
- /package/dist/{types/testing.d.ts → testing.d.ts} +0 -0
- /package/dist/{types/transitions.d.ts → transitions.d.ts} +0 -0
- /package/dist/types/{types/adopted-options.d.ts → adopted-options.d.ts} +0 -0
- /package/dist/types/{types/app-context.d.ts → app-context.d.ts} +0 -0
- /package/dist/types/{types/dispatch-options.d.ts → dispatch-options.d.ts} +0 -0
- /package/dist/types/{types/guard.d.ts → guard.d.ts} +0 -0
- /package/dist/types/{types/i-controller.d.ts → i-controller.d.ts} +0 -0
- /package/dist/types/{types/moved-options.d.ts → moved-options.d.ts} +0 -0
- /package/dist/types/{types/observe-options.d.ts → observe-options.d.ts} +0 -0
- /package/dist/types/{types/page-options.d.ts → page-options.d.ts} +0 -0
- /package/dist/types/{types/part-options.d.ts → part-options.d.ts} +0 -0
- /package/dist/types/{types/property-converter.d.ts → property-converter.d.ts} +0 -0
- /package/dist/types/{types/property-options.d.ts → property-options.d.ts} +0 -0
- /package/dist/types/{types/query-options.d.ts → query-options.d.ts} +0 -0
- /package/dist/types/{types/request-options.d.ts → request-options.d.ts} +0 -0
- /package/dist/types/{types/respond-options.d.ts → respond-options.d.ts} +0 -0
- /package/dist/types/{types/route-params.d.ts → route-params.d.ts} +0 -0
- /package/dist/types/{types/router-instance.d.ts → router-instance.d.ts} +0 -0
- /package/dist/types/{types/router-options.d.ts → router-options.d.ts} +0 -0
- /package/dist/types/{types/simple-array.d.ts → simple-array.d.ts} +0 -0
- /package/dist/types/{types/snice-element.d.ts → snice-element.d.ts} +0 -0
- /package/dist/types/{types/snice-global.d.ts → snice-global.d.ts} +0 -0
- /package/dist/types/{types/transition.d.ts → transition.d.ts} +0 -0
- /package/dist/{types/utils.d.ts → utils.d.ts} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* snice
|
|
3
|
-
* Imperative TypeScript framework for building vanilla web components with decorators, routing, and controllers. No virtual DOM, no build complexity.
|
|
2
|
+
* snice v3.0.0
|
|
3
|
+
* Imperative TypeScript framework for building vanilla web components with decorators, differential rendering, routing, and controllers. No virtual DOM, no build complexity.
|
|
4
4
|
* (c) 2024
|
|
5
5
|
* Released under the MIT License.
|
|
6
6
|
*/
|
|
@@ -38,10 +38,12 @@ const CHANNEL_HANDLERS = getSymbol('channel-handlers');
|
|
|
38
38
|
// Internal element state symbols
|
|
39
39
|
const READY_PROMISE = getSymbol('ready-promise');
|
|
40
40
|
const READY_RESOLVE = getSymbol('ready-resolve');
|
|
41
|
+
getSymbol('rendered-promise');
|
|
42
|
+
getSymbol('rendered-resolve');
|
|
41
43
|
const CONTROLLER = getSymbol('controller');
|
|
42
44
|
const INITIALIZED = getSymbol('initialized');
|
|
43
45
|
// Event handler symbols
|
|
44
|
-
|
|
46
|
+
getSymbol('on-handlers');
|
|
45
47
|
// Controller symbols
|
|
46
48
|
const CONTROLLER_KEY = getSymbol('controller-key');
|
|
47
49
|
const CONTROLLER_NAME_KEY = getSymbol('controller-name');
|
|
@@ -53,13 +55,14 @@ const CLEANUP = getSymbol('cleanup');
|
|
|
53
55
|
// Property symbols
|
|
54
56
|
const PROPERTIES = getSymbol('properties');
|
|
55
57
|
const PROPERTY_VALUES = getSymbol('property-values');
|
|
58
|
+
const PRE_INIT_PROPERTY_VALUES = getSymbol('pre-init-property-values');
|
|
56
59
|
const PROPERTIES_INITIALIZED = getSymbol('properties-initialized');
|
|
57
60
|
const PROPERTY_WATCHERS = getSymbol('property-watchers');
|
|
58
61
|
const EXPLICITLY_SET_PROPERTIES = getSymbol('explicitly-set-properties');
|
|
59
62
|
// Router context symbol
|
|
60
63
|
const ROUTER_CONTEXT = getSymbol('router-context');
|
|
61
64
|
getSymbol('current-page-marker');
|
|
62
|
-
|
|
65
|
+
getSymbol('context-request-handler');
|
|
63
66
|
const PAGE_TRANSITION = getSymbol('page-transition');
|
|
64
67
|
const CREATED_AT = getSymbol('created-at');
|
|
65
68
|
// Lifecycle symbols
|
|
@@ -70,313 +73,34 @@ const ADOPTED_HANDLERS = getSymbol('adopted-handlers');
|
|
|
70
73
|
// Observer symbols
|
|
71
74
|
const OBSERVERS = getSymbol('observers');
|
|
72
75
|
// Part symbols
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
getSymbol('parts');
|
|
77
|
+
getSymbol('part-timers');
|
|
75
78
|
// Lifecycle callback timers
|
|
76
79
|
const MOVED_TIMERS = getSymbol('moved-timers');
|
|
77
80
|
const ADOPTED_TIMERS = getSymbol('adopted-timers');
|
|
78
81
|
// Dispatch timing symbols
|
|
79
82
|
const DISPATCH_TIMERS = getSymbol('dispatch-timers');
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
// Normalize to array and expand at decoration time
|
|
102
|
-
const eventNames = Array.isArray(eventName) ? eventName : [eventName];
|
|
103
|
-
// Create a handler entry for each event
|
|
104
|
-
for (const event of eventNames) {
|
|
105
|
-
constructor.prototype[ON_HANDLERS].push({
|
|
106
|
-
eventName: event,
|
|
107
|
-
selector,
|
|
108
|
-
methodName: propertyKey,
|
|
109
|
-
method: target,
|
|
110
|
-
options: opts
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
// Helper to setup event handlers for elements
|
|
117
|
-
function setupEventHandlers(instance, element) {
|
|
118
|
-
const handlers = instance.constructor.prototype[ON_HANDLERS];
|
|
119
|
-
if (!handlers)
|
|
120
|
-
return;
|
|
121
|
-
// Initialize cleanup object if needed
|
|
122
|
-
if (!instance[CLEANUP]) {
|
|
123
|
-
instance[CLEANUP] = { events: [], channels: [] };
|
|
124
|
-
}
|
|
125
|
-
for (const handler of handlers) {
|
|
126
|
-
// Get the current method from the instance (preserves decorator stacking)
|
|
127
|
-
const currentMethod = instance[handler.method.name];
|
|
128
|
-
const originalMethod = currentMethod ? currentMethod.bind(instance) : handler.method.bind(instance);
|
|
129
|
-
const handlerOptions = handler.options || {};
|
|
130
|
-
// Parse event name for key modifiers
|
|
131
|
-
const [baseEventName, keyModifier] = handler.eventName.split(':');
|
|
132
|
-
// Create debounced/throttled wrapper if needed
|
|
133
|
-
const createTimedWrapper = (method) => {
|
|
134
|
-
if (handlerOptions.debounce) {
|
|
135
|
-
let timeoutId;
|
|
136
|
-
return function (...args) {
|
|
137
|
-
clearTimeout(timeoutId);
|
|
138
|
-
timeoutId = setTimeout(() => method.apply(this, args), handlerOptions.debounce);
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
if (handlerOptions.throttle) {
|
|
142
|
-
let lastCall = 0;
|
|
143
|
-
let timeoutId;
|
|
144
|
-
return function (...args) {
|
|
145
|
-
const now = Date.now();
|
|
146
|
-
const remaining = handlerOptions.throttle - (now - lastCall);
|
|
147
|
-
if (remaining <= 0) {
|
|
148
|
-
clearTimeout(timeoutId);
|
|
149
|
-
lastCall = now;
|
|
150
|
-
method.apply(this, args);
|
|
151
|
-
}
|
|
152
|
-
else if (!timeoutId) {
|
|
153
|
-
timeoutId = setTimeout(() => {
|
|
154
|
-
lastCall = Date.now();
|
|
155
|
-
timeoutId = null;
|
|
156
|
-
method.apply(this, args);
|
|
157
|
-
}, remaining);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
return method;
|
|
162
|
-
};
|
|
163
|
-
// Create the event handler with key modifier support
|
|
164
|
-
const createEventHandler = (method) => {
|
|
165
|
-
if (keyModifier && (baseEventName === 'keydown' || baseEventName === 'keyup' || baseEventName === 'keypress')) {
|
|
166
|
-
return (event) => {
|
|
167
|
-
const keyEvent = event;
|
|
168
|
-
// Helper to normalize key names (e.g., "Space" -> " ")
|
|
169
|
-
const normalizeKey = (key) => {
|
|
170
|
-
if (key === 'Space')
|
|
171
|
-
return ' ';
|
|
172
|
-
return key;
|
|
173
|
-
};
|
|
174
|
-
// Check for "any modifiers" match with ~ prefix
|
|
175
|
-
if (keyModifier.startsWith('~')) {
|
|
176
|
-
const key = normalizeKey(keyModifier.slice(1)); // Remove the ~ and normalize
|
|
177
|
-
// Match if key matches, regardless of modifiers
|
|
178
|
-
if (keyEvent.key === key) {
|
|
179
|
-
method(event);
|
|
180
|
-
}
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
// Check for modifier combinations using +
|
|
184
|
-
if (keyModifier.includes('+')) {
|
|
185
|
-
const parts = keyModifier.split('+');
|
|
186
|
-
const key = normalizeKey(parts[parts.length - 1]); // Last part is the actual key
|
|
187
|
-
const modifiers = parts.slice(0, -1); // Everything else is modifiers
|
|
188
|
-
// Check the actual key
|
|
189
|
-
if (keyEvent.key !== key)
|
|
190
|
-
return;
|
|
191
|
-
// Create a set of expected modifiers
|
|
192
|
-
const expectedModifiers = new Set(modifiers.map((m) => m.toLowerCase()));
|
|
193
|
-
const hasCtrl = expectedModifiers.has('ctrl');
|
|
194
|
-
const hasShift = expectedModifiers.has('shift');
|
|
195
|
-
const hasAlt = expectedModifiers.has('alt');
|
|
196
|
-
const hasMeta = expectedModifiers.has('meta') || expectedModifiers.has('cmd');
|
|
197
|
-
// Check that expected modifiers are pressed and unexpected ones are not
|
|
198
|
-
const modifiersMatch = keyEvent.ctrlKey === hasCtrl &&
|
|
199
|
-
keyEvent.shiftKey === hasShift &&
|
|
200
|
-
keyEvent.altKey === hasAlt &&
|
|
201
|
-
keyEvent.metaKey === hasMeta;
|
|
202
|
-
if (modifiersMatch) {
|
|
203
|
-
method(event);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
// Default: exact match (no modifiers allowed)
|
|
208
|
-
const key = normalizeKey(keyModifier);
|
|
209
|
-
// Only match if key matches AND no modifiers are pressed
|
|
210
|
-
if (keyEvent.key === key &&
|
|
211
|
-
!keyEvent.ctrlKey &&
|
|
212
|
-
!keyEvent.shiftKey &&
|
|
213
|
-
!keyEvent.altKey &&
|
|
214
|
-
!keyEvent.metaKey) {
|
|
215
|
-
method(event);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
return method;
|
|
221
|
-
};
|
|
222
|
-
// Apply timing wrapper (debounce/throttle)
|
|
223
|
-
const timedMethod = createTimedWrapper(originalMethod);
|
|
224
|
-
// Wrap boundMethod in try-catch for error isolation
|
|
225
|
-
const wrappedMethod = createEventHandler((event) => {
|
|
226
|
-
try {
|
|
227
|
-
// Apply automatic preventDefault/stopPropagation if configured
|
|
228
|
-
if (handlerOptions.preventDefault) {
|
|
229
|
-
event.preventDefault();
|
|
230
|
-
}
|
|
231
|
-
if (handlerOptions.stopPropagation) {
|
|
232
|
-
event.stopPropagation();
|
|
233
|
-
}
|
|
234
|
-
return timedMethod(event);
|
|
235
|
-
}
|
|
236
|
-
catch (error) {
|
|
237
|
-
console.error(`Error in event handler ${handler.methodName}:`, error);
|
|
238
|
-
// Don't rethrow - allow other handlers to continue
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
if (handler.selector) {
|
|
242
|
-
// Delegated event handling - use shadow root if available
|
|
243
|
-
const eventRoot = element.shadowRoot || element;
|
|
244
|
-
const delegatedHandler = (event) => {
|
|
245
|
-
const target = event.target;
|
|
246
|
-
let shouldHandle = false;
|
|
247
|
-
if (target.matches && target.matches(handler.selector)) {
|
|
248
|
-
shouldHandle = true;
|
|
249
|
-
}
|
|
250
|
-
else if (target.closest) {
|
|
251
|
-
const closest = target.closest(handler.selector);
|
|
252
|
-
if (closest) {
|
|
253
|
-
shouldHandle = true;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
if (shouldHandle) {
|
|
257
|
-
// Apply automatic preventDefault/stopPropagation only if we're handling this event
|
|
258
|
-
if (handlerOptions.preventDefault) {
|
|
259
|
-
event.preventDefault();
|
|
260
|
-
}
|
|
261
|
-
if (handlerOptions.stopPropagation) {
|
|
262
|
-
event.stopPropagation();
|
|
263
|
-
event.stopImmediatePropagation(); // Also stop other handlers on same element
|
|
264
|
-
}
|
|
265
|
-
wrappedMethod(event);
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
const listenerOptions = {
|
|
269
|
-
capture: handlerOptions.capture || false,
|
|
270
|
-
once: handlerOptions.once || false,
|
|
271
|
-
passive: handlerOptions.passive || false
|
|
272
|
-
};
|
|
273
|
-
eventRoot.addEventListener(baseEventName, delegatedHandler, listenerOptions);
|
|
274
|
-
instance[CLEANUP].events.push(() => {
|
|
275
|
-
eventRoot.removeEventListener(baseEventName, delegatedHandler, listenerOptions);
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
// Direct event handling - always on the element itself
|
|
280
|
-
const listenerOptions = {
|
|
281
|
-
capture: handlerOptions.capture || false,
|
|
282
|
-
once: handlerOptions.once || false,
|
|
283
|
-
passive: handlerOptions.passive || false
|
|
284
|
-
};
|
|
285
|
-
element.addEventListener(baseEventName, wrappedMethod, listenerOptions);
|
|
286
|
-
instance[CLEANUP].events.push(() => {
|
|
287
|
-
element.removeEventListener(baseEventName, wrappedMethod, listenerOptions);
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
// Helper to cleanup event handlers
|
|
293
|
-
function cleanupEventHandlers(instance) {
|
|
294
|
-
if (instance[CLEANUP]?.events) {
|
|
295
|
-
for (const cleanup of instance[CLEANUP].events) {
|
|
296
|
-
cleanup();
|
|
297
|
-
}
|
|
298
|
-
instance[CLEANUP].events = [];
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
* Decorator that automatically dispatches a custom event after a method is called.
|
|
303
|
-
* The return value of the method becomes the event detail.
|
|
304
|
-
*
|
|
305
|
-
* @param eventName The name of the event to dispatch
|
|
306
|
-
* @param options Optional configuration extending EventInit
|
|
307
|
-
*/
|
|
308
|
-
function dispatch(eventName, options) {
|
|
309
|
-
return function (originalMethod, _context) {
|
|
310
|
-
return function (...args) {
|
|
311
|
-
// Create timing wrappers for dispatch (per-instance)
|
|
312
|
-
if (!this[DISPATCH_TIMERS]) {
|
|
313
|
-
this[DISPATCH_TIMERS] = new Map();
|
|
314
|
-
}
|
|
315
|
-
const timerKey = `${eventName}_${_context.name}`;
|
|
316
|
-
if (!this[DISPATCH_TIMERS].has(timerKey)) {
|
|
317
|
-
this[DISPATCH_TIMERS].set(timerKey, {
|
|
318
|
-
debounceTimeout: null,
|
|
319
|
-
throttleLastCall: 0,
|
|
320
|
-
throttleTimeout: null
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
const timers = this[DISPATCH_TIMERS].get(timerKey);
|
|
324
|
-
// Call the original method with preserved this context
|
|
325
|
-
const result = originalMethod.apply(this, args);
|
|
326
|
-
// Helper to dispatch the event
|
|
327
|
-
const doDispatch = (detail) => {
|
|
328
|
-
// Skip dispatch if result is undefined and dispatchOnUndefined is false
|
|
329
|
-
if (detail === undefined && options?.dispatchOnUndefined === false) {
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
// Create event with spread operator for options
|
|
333
|
-
const event = new CustomEvent(eventName, {
|
|
334
|
-
bubbles: true, // Default to true for component events
|
|
335
|
-
composed: true, // Allow crossing shadow DOM boundaries
|
|
336
|
-
...options, // Spread all EventInit options
|
|
337
|
-
detail
|
|
338
|
-
});
|
|
339
|
-
this.dispatchEvent(event);
|
|
340
|
-
};
|
|
341
|
-
// Helper to handle timed dispatch
|
|
342
|
-
const timedDispatch = (detail) => {
|
|
343
|
-
if (options?.debounce) {
|
|
344
|
-
clearTimeout(timers.debounceTimeout);
|
|
345
|
-
timers.debounceTimeout = setTimeout(() => doDispatch(detail), options.debounce);
|
|
346
|
-
}
|
|
347
|
-
else if (options?.throttle) {
|
|
348
|
-
const now = Date.now();
|
|
349
|
-
const remaining = options.throttle - (now - timers.throttleLastCall);
|
|
350
|
-
if (remaining <= 0) {
|
|
351
|
-
clearTimeout(timers.throttleTimeout);
|
|
352
|
-
timers.throttleLastCall = now;
|
|
353
|
-
doDispatch(detail);
|
|
354
|
-
}
|
|
355
|
-
else if (!timers.throttleTimeout) {
|
|
356
|
-
timers.throttleTimeout = setTimeout(() => {
|
|
357
|
-
timers.throttleLastCall = Date.now();
|
|
358
|
-
timers.throttleTimeout = null;
|
|
359
|
-
doDispatch(detail);
|
|
360
|
-
}, remaining);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
doDispatch(detail);
|
|
365
|
-
}
|
|
366
|
-
};
|
|
367
|
-
// Handle async methods
|
|
368
|
-
if (result instanceof Promise) {
|
|
369
|
-
return result.then((resolvedResult) => {
|
|
370
|
-
timedDispatch(resolvedResult);
|
|
371
|
-
return resolvedResult;
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
// Sync method
|
|
375
|
-
timedDispatch(result);
|
|
376
|
-
return result;
|
|
377
|
-
};
|
|
378
|
-
};
|
|
379
|
-
}
|
|
83
|
+
// Render symbols (v3.0.0)
|
|
84
|
+
const RENDER_METHOD = getSymbol('render-method');
|
|
85
|
+
const RENDER_OPTIONS = getSymbol('render-options');
|
|
86
|
+
const RENDER_INSTANCE = getSymbol('render-instance');
|
|
87
|
+
const RENDER_SCHEDULED = getSymbol('render-scheduled');
|
|
88
|
+
const RENDER_TIMERS = getSymbol('render-timers');
|
|
89
|
+
const RENDER_CALLBACKS = getSymbol('render-callbacks');
|
|
90
|
+
const STYLES_METHOD = getSymbol('styles-method');
|
|
91
|
+
const STYLES_APPLIED = getSymbol('styles-applied');
|
|
92
|
+
// Navigation context symbols
|
|
93
|
+
const CONTEXT_HANDLER = getSymbol('context-handler');
|
|
94
|
+
getSymbol('context-method-name');
|
|
95
|
+
const NAVIGATION_CONTEXT_INSTANCE = getSymbol('navigation-context-instance');
|
|
96
|
+
const REGISTERED_ELEMENTS = getSymbol('registered-elements');
|
|
97
|
+
const IS_UPDATING = getSymbol('is-updating');
|
|
98
|
+
const CONTEXT_REGISTER = getSymbol('context-register');
|
|
99
|
+
const CONTEXT_UNREGISTER = getSymbol('context-unregister');
|
|
100
|
+
const CONTEXT_NOTIFY_ELEMENT = getSymbol('context-notify-element');
|
|
101
|
+
getSymbol('context-options');
|
|
102
|
+
const CONTEXT_TIMER = getSymbol('context-timer');
|
|
103
|
+
const CONTEXT_CALLED = getSymbol('context-called');
|
|
380
104
|
|
|
381
105
|
// Global cache for MediaQueryList objects
|
|
382
106
|
const mediaQueryCache = new Map();
|
|
@@ -403,21 +127,22 @@ function observe(observeTarget, selectorOrOptions, options) {
|
|
|
403
127
|
}
|
|
404
128
|
return function (target, context) {
|
|
405
129
|
const propertyKey = context.name;
|
|
130
|
+
const initKey = `__observe_init_${propertyKey}`;
|
|
406
131
|
context.addInitializer(function () {
|
|
407
132
|
const constructor = this.constructor;
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
133
|
+
if (constructor[initKey])
|
|
134
|
+
return;
|
|
135
|
+
constructor[initKey] = true;
|
|
136
|
+
if (!constructor[OBSERVERS]) {
|
|
137
|
+
constructor[OBSERVERS] = [];
|
|
411
138
|
}
|
|
412
|
-
// Normalize to array
|
|
413
139
|
const observeTargets = Array.isArray(observeTarget) ? observeTarget : [observeTarget];
|
|
414
|
-
// Create an observer entry for each target
|
|
415
140
|
for (const targetString of observeTargets) {
|
|
416
|
-
// Parse the observation type from the observeTarget string
|
|
417
141
|
const [type, ...modifiers] = targetString.split(':');
|
|
418
|
-
|
|
142
|
+
const targetStr = modifiers.join(':');
|
|
143
|
+
constructor[OBSERVERS].push({
|
|
419
144
|
type,
|
|
420
|
-
target:
|
|
145
|
+
target: targetStr,
|
|
421
146
|
selector,
|
|
422
147
|
methodName: propertyKey,
|
|
423
148
|
method: target,
|
|
@@ -429,8 +154,7 @@ function observe(observeTarget, selectorOrOptions, options) {
|
|
|
429
154
|
}
|
|
430
155
|
// Helper to setup observers for elements
|
|
431
156
|
function setupObservers(instance, element) {
|
|
432
|
-
|
|
433
|
-
const observers = instance.constructor.prototype[OBSERVERS];
|
|
157
|
+
const observers = instance.constructor[OBSERVERS];
|
|
434
158
|
if (!observers || !Array.isArray(observers) || observers.length === 0) {
|
|
435
159
|
return;
|
|
436
160
|
}
|
|
@@ -858,14 +582,16 @@ function request(requestName, options) {
|
|
|
858
582
|
function respond(requestName, options) {
|
|
859
583
|
return function (target, context) {
|
|
860
584
|
const propertyKey = context.name;
|
|
585
|
+
const initKey = `__respond_init_${requestName}_${propertyKey}`;
|
|
861
586
|
context.addInitializer(function () {
|
|
862
587
|
const constructor = this.constructor;
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
588
|
+
if (constructor[initKey])
|
|
589
|
+
return;
|
|
590
|
+
constructor[initKey] = true;
|
|
591
|
+
if (!constructor[CHANNEL_HANDLERS]) {
|
|
592
|
+
constructor[CHANNEL_HANDLERS] = [];
|
|
867
593
|
}
|
|
868
|
-
constructor
|
|
594
|
+
constructor[CHANNEL_HANDLERS].push({
|
|
869
595
|
channelName: requestName,
|
|
870
596
|
methodName: propertyKey,
|
|
871
597
|
method: target,
|
|
@@ -876,7 +602,7 @@ function respond(requestName, options) {
|
|
|
876
602
|
}
|
|
877
603
|
// Helper to setup response handlers for elements and controllers
|
|
878
604
|
function setupResponseHandlers(instance, element) {
|
|
879
|
-
const handlers = instance.constructor
|
|
605
|
+
const handlers = instance.constructor[CHANNEL_HANDLERS];
|
|
880
606
|
if (!handlers)
|
|
881
607
|
return;
|
|
882
608
|
// Store cleanup functions
|
|
@@ -976,71 +702,1296 @@ function cleanupResponseHandlers(instance) {
|
|
|
976
702
|
}
|
|
977
703
|
}
|
|
978
704
|
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
705
|
+
/**
|
|
706
|
+
* Template system for Snice v3.0.0
|
|
707
|
+
* Inspired by lit-html but custom implementation
|
|
708
|
+
* Provides html`` and css`` tagged template processors with differential rendering
|
|
709
|
+
*/
|
|
710
|
+
// Unique symbols for type checking
|
|
711
|
+
const HTML_RESULT = Symbol('html-result');
|
|
712
|
+
const CSS_RESULT = Symbol('css-result');
|
|
713
|
+
/**
|
|
714
|
+
* Tagged template function for creating HTML templates
|
|
715
|
+
*
|
|
716
|
+
* @example
|
|
717
|
+
* ```typescript
|
|
718
|
+
* html`<div class="card">
|
|
719
|
+
* <h1>${this.title}</h1>
|
|
720
|
+
* <button @click=${this.handleClick}>Click me</button>
|
|
721
|
+
* </div>`
|
|
722
|
+
* ```
|
|
723
|
+
*/
|
|
724
|
+
function html(strings, ...values) {
|
|
725
|
+
return {
|
|
726
|
+
_$litType$: HTML_RESULT,
|
|
727
|
+
strings,
|
|
728
|
+
values
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Tagged template function for creating CSS
|
|
733
|
+
*
|
|
734
|
+
* @example
|
|
735
|
+
* ```typescript
|
|
736
|
+
* css`:host {
|
|
737
|
+
* display: block;
|
|
738
|
+
* padding: 1rem;
|
|
739
|
+
* }`
|
|
740
|
+
* ```
|
|
741
|
+
*/
|
|
742
|
+
function css(strings, ...values) {
|
|
743
|
+
// Combine strings and values into final CSS text
|
|
744
|
+
let cssText = strings[0];
|
|
745
|
+
for (let i = 0; i < values.length; i++) {
|
|
746
|
+
cssText += String(values[i]) + strings[i + 1];
|
|
1004
747
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
748
|
+
const result = {
|
|
749
|
+
_$litType$: CSS_RESULT,
|
|
750
|
+
cssText
|
|
751
|
+
};
|
|
752
|
+
// Try to create constructable stylesheet for better performance
|
|
753
|
+
// This will be cached and reused across instances
|
|
754
|
+
if (typeof CSSStyleSheet !== 'undefined' && 'adoptedStyleSheets' in Document.prototype) {
|
|
1009
755
|
try {
|
|
1010
|
-
const
|
|
1011
|
-
|
|
1012
|
-
|
|
756
|
+
const sheet = new CSSStyleSheet();
|
|
757
|
+
sheet.replaceSync(cssText);
|
|
758
|
+
result.styleSheet = sheet;
|
|
1013
759
|
}
|
|
1014
|
-
catch (
|
|
1015
|
-
|
|
1016
|
-
throw error;
|
|
760
|
+
catch (e) {
|
|
761
|
+
// Fall back to regular <style> tag if constructable stylesheets fail
|
|
1017
762
|
}
|
|
1018
763
|
}
|
|
764
|
+
return result;
|
|
1019
765
|
}
|
|
1020
766
|
/**
|
|
1021
|
-
*
|
|
1022
|
-
* @param name The name to register the controller under
|
|
767
|
+
* Check if a value is a TemplateResult
|
|
1023
768
|
*/
|
|
1024
|
-
function
|
|
1025
|
-
return
|
|
1026
|
-
snice.controllerRegistry.set(name, constructor);
|
|
1027
|
-
// Mark as controller class for channel decorator detection
|
|
1028
|
-
constructor.prototype[IS_CONTROLLER_CLASS] = true;
|
|
1029
|
-
return constructor;
|
|
1030
|
-
};
|
|
769
|
+
function isTemplateResult(value) {
|
|
770
|
+
return value && value._$litType$ === HTML_RESULT;
|
|
1031
771
|
}
|
|
1032
772
|
/**
|
|
1033
|
-
*
|
|
1034
|
-
* @param element The element to attach the controller to
|
|
1035
|
-
* @param controllerName The name of the controller to attach
|
|
773
|
+
* Check if a value is a CSSResult
|
|
1036
774
|
*/
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
775
|
+
function isCSSResult(value) {
|
|
776
|
+
return value && value._$litType$ === CSS_RESULT;
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Nothing - represents no value (different from null/undefined)
|
|
780
|
+
* Used to remove content from templates
|
|
781
|
+
*/
|
|
782
|
+
const nothing = Symbol('nothing');
|
|
783
|
+
// Unique symbol for unsafe HTML
|
|
784
|
+
const UNSAFE_HTML = Symbol('unsafe-html');
|
|
785
|
+
/**
|
|
786
|
+
* Mark a string as raw HTML that should not be escaped
|
|
787
|
+
* WARNING: Only use with sanitized content - using user input can lead to XSS!
|
|
788
|
+
*
|
|
789
|
+
* @example
|
|
790
|
+
* ```typescript
|
|
791
|
+
* const htmlString = '<span class="bold">Hello</span>';
|
|
792
|
+
* html`<div>${unsafeHTML(htmlString)}</div>`
|
|
793
|
+
* ```
|
|
794
|
+
*/
|
|
795
|
+
function unsafeHTML(html) {
|
|
796
|
+
return {
|
|
797
|
+
_$litType$: UNSAFE_HTML,
|
|
798
|
+
html
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Check if a value is an UnsafeHTML wrapper
|
|
803
|
+
*/
|
|
804
|
+
function isUnsafeHTML(value) {
|
|
805
|
+
return value && value._$litType$ === UNSAFE_HTML;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* Simplified lit-html-style template system
|
|
810
|
+
* Based on lit-html's approach but simplified
|
|
811
|
+
*/
|
|
812
|
+
// Unique marker for dynamic parts
|
|
813
|
+
// Using processing instruction syntax like lit-html for better compatibility
|
|
814
|
+
// This parses as a comment node but doesn't get escaped in attributes
|
|
815
|
+
const marker = `snice$${Math.random().toFixed(9).slice(2)}$`;
|
|
816
|
+
const markerMatch = '?' + marker;
|
|
817
|
+
const nodeMarker = `<${markerMatch}>`;
|
|
818
|
+
const markerRegex = new RegExp(marker, 'g');
|
|
819
|
+
// Template cache removed - negligible performance benefit (~0.00004ms per template)
|
|
820
|
+
/**
|
|
821
|
+
* A prepared template ready for rendering
|
|
822
|
+
*/
|
|
823
|
+
class Template {
|
|
824
|
+
constructor(result, element, attrNamesForParts) {
|
|
825
|
+
this.parts = [];
|
|
826
|
+
this.element = element;
|
|
827
|
+
const walker = document.createTreeWalker(element.content, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_TEXT);
|
|
828
|
+
let partIndex = 0;
|
|
829
|
+
const nodesToRemove = [];
|
|
830
|
+
let node;
|
|
831
|
+
while ((node = walker.nextNode()) !== null) {
|
|
832
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
833
|
+
const element = node;
|
|
834
|
+
const tagName = element.tagName.toLowerCase();
|
|
835
|
+
// Handle virtual elements: <if>, <case>
|
|
836
|
+
// These elements stay in the DOM but are styled with display:contents
|
|
837
|
+
// to be transparent wrappers that don't affect layout
|
|
838
|
+
if (tagName === 'if') {
|
|
839
|
+
// <if value="${condition}">children</if>
|
|
840
|
+
const valueAttr = element.getAttribute('value');
|
|
841
|
+
if (valueAttr && valueAttr.includes(marker)) {
|
|
842
|
+
// Remove the value attribute
|
|
843
|
+
element.removeAttribute('value');
|
|
844
|
+
this.parts.push({
|
|
845
|
+
type: 'if',
|
|
846
|
+
index: partIndex++,
|
|
847
|
+
element // Keep the <if> element, will show/hide via display property
|
|
848
|
+
});
|
|
849
|
+
// Continue processing children normally
|
|
850
|
+
}
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
// Handle <case> element
|
|
854
|
+
// Like <if>, <case> stays in DOM styled with display:contents
|
|
855
|
+
if (tagName === 'case') {
|
|
856
|
+
// <case value="${value}">children</case>
|
|
857
|
+
const valueAttr = element.getAttribute('value');
|
|
858
|
+
if (valueAttr && valueAttr.includes(marker)) {
|
|
859
|
+
// Remove the value attribute
|
|
860
|
+
element.removeAttribute('value');
|
|
861
|
+
this.parts.push({
|
|
862
|
+
type: 'case',
|
|
863
|
+
index: partIndex++,
|
|
864
|
+
caseElement: element // Keep the <case> element for when/default processing
|
|
865
|
+
});
|
|
866
|
+
// Continue processing children normally
|
|
867
|
+
}
|
|
868
|
+
continue;
|
|
869
|
+
}
|
|
870
|
+
if (element.hasAttributes()) {
|
|
871
|
+
const attributes = element.attributes;
|
|
872
|
+
const attrsToRemove = [];
|
|
873
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
874
|
+
const attr = attributes[i];
|
|
875
|
+
const value = attr.value;
|
|
876
|
+
// Check for attribute bindings
|
|
877
|
+
if (value.includes(marker)) {
|
|
878
|
+
attrsToRemove.push(attr);
|
|
879
|
+
// Get original attribute name with preserved case
|
|
880
|
+
const originalName = attrNamesForParts[partIndex] || attr.name;
|
|
881
|
+
// Extract static string segments by splitting on marker
|
|
882
|
+
const attrStrings = value.split(marker);
|
|
883
|
+
if (originalName.startsWith('@')) {
|
|
884
|
+
// Event binding
|
|
885
|
+
this.parts.push({
|
|
886
|
+
type: 'event',
|
|
887
|
+
index: partIndex++,
|
|
888
|
+
name: originalName.slice(1),
|
|
889
|
+
element
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
else if (originalName.startsWith('.')) {
|
|
893
|
+
// Property binding - preserve original case for JavaScript properties
|
|
894
|
+
this.parts.push({
|
|
895
|
+
type: 'property',
|
|
896
|
+
index: partIndex++,
|
|
897
|
+
name: originalName.slice(1),
|
|
898
|
+
element
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
else if (originalName.startsWith('?')) {
|
|
902
|
+
// Boolean attribute
|
|
903
|
+
this.parts.push({
|
|
904
|
+
type: 'boolean-attribute',
|
|
905
|
+
index: partIndex++,
|
|
906
|
+
name: originalName.slice(1),
|
|
907
|
+
element
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
else {
|
|
911
|
+
// Regular attribute - use lowercased name from DOM
|
|
912
|
+
// Store static string segments for interpolation
|
|
913
|
+
this.parts.push({
|
|
914
|
+
type: 'attribute',
|
|
915
|
+
index: partIndex++,
|
|
916
|
+
name: attr.name,
|
|
917
|
+
element,
|
|
918
|
+
attrStrings
|
|
919
|
+
});
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
// Remove marker attributes
|
|
924
|
+
for (const attr of attrsToRemove) {
|
|
925
|
+
element.removeAttribute(attr.name);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
else if (node.nodeType === Node.COMMENT_NODE) {
|
|
930
|
+
const comment = node;
|
|
931
|
+
// Check for marker match (processing instruction becomes comment)
|
|
932
|
+
if (comment.data === markerMatch) {
|
|
933
|
+
// Node part
|
|
934
|
+
const parent = comment.parentNode;
|
|
935
|
+
const endNode = document.createComment('');
|
|
936
|
+
parent.insertBefore(endNode, comment.nextSibling);
|
|
937
|
+
this.parts.push({
|
|
938
|
+
type: 'node',
|
|
939
|
+
index: partIndex++,
|
|
940
|
+
startNode: comment,
|
|
941
|
+
endNode
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
else if (node.nodeType === Node.TEXT_NODE) {
|
|
946
|
+
const text = node;
|
|
947
|
+
const data = text.data;
|
|
948
|
+
if (data.includes(marker)) {
|
|
949
|
+
// Split text node at markers
|
|
950
|
+
const parent = text.parentNode;
|
|
951
|
+
const parts = data.split(markerRegex);
|
|
952
|
+
const lastIndex = parts.length - 1;
|
|
953
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
954
|
+
parent.insertBefore(document.createTextNode(parts[i]), text);
|
|
955
|
+
const comment = document.createComment('');
|
|
956
|
+
const endNode = document.createComment('');
|
|
957
|
+
parent.insertBefore(comment, text);
|
|
958
|
+
parent.insertBefore(endNode, text);
|
|
959
|
+
this.parts.push({
|
|
960
|
+
type: 'node',
|
|
961
|
+
index: partIndex++,
|
|
962
|
+
startNode: comment,
|
|
963
|
+
endNode
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
// Last part
|
|
967
|
+
if (parts[lastIndex] !== '') {
|
|
968
|
+
text.data = parts[lastIndex];
|
|
969
|
+
}
|
|
970
|
+
else {
|
|
971
|
+
nodesToRemove.push(text);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
// Remove marker nodes
|
|
977
|
+
for (const node of nodesToRemove) {
|
|
978
|
+
node.parentNode?.removeChild(node);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Prepare a template for rendering
|
|
984
|
+
*/
|
|
985
|
+
function prepareTemplate(result) {
|
|
986
|
+
// Build HTML with markers and extract original attribute names
|
|
987
|
+
const { strings } = result;
|
|
988
|
+
let html = '';
|
|
989
|
+
const attrNamesForParts = [];
|
|
990
|
+
for (let i = 0; i < strings.length; i++) {
|
|
991
|
+
const str = strings[i];
|
|
992
|
+
html += str;
|
|
993
|
+
if (i < strings.length - 1) {
|
|
994
|
+
// Check if we're in an attribute context
|
|
995
|
+
// Look backwards for = sign
|
|
996
|
+
const lastEquals = str.lastIndexOf('=');
|
|
997
|
+
const lastCloseTag = str.lastIndexOf('>');
|
|
998
|
+
if (lastEquals > lastCloseTag) {
|
|
999
|
+
// We're in an attribute value - extract and preserve the original attribute name
|
|
1000
|
+
let attrStart = lastEquals - 1;
|
|
1001
|
+
while (attrStart >= 0 && /\S/.test(str[attrStart])) {
|
|
1002
|
+
attrStart--;
|
|
1003
|
+
}
|
|
1004
|
+
const attrName = str.substring(attrStart + 1, lastEquals).trim();
|
|
1005
|
+
attrNamesForParts.push(attrName);
|
|
1006
|
+
html += marker;
|
|
1007
|
+
}
|
|
1008
|
+
else {
|
|
1009
|
+
// Check if this is a meta element (<if> or <case>) by looking backwards
|
|
1010
|
+
// Match pattern: <if or <case followed by whitespace or >
|
|
1011
|
+
const metaElementMatch = str.match(/<(if|case)\s*$/);
|
|
1012
|
+
if (metaElementMatch) {
|
|
1013
|
+
// This is a meta element - add value attribute
|
|
1014
|
+
attrNamesForParts.push('value');
|
|
1015
|
+
html += `value="${marker}"`;
|
|
1016
|
+
}
|
|
1017
|
+
else {
|
|
1018
|
+
// We're in node content
|
|
1019
|
+
attrNamesForParts.push(''); // Empty string for node parts
|
|
1020
|
+
html += nodeMarker;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
const template = document.createElement('template');
|
|
1026
|
+
template.innerHTML = html;
|
|
1027
|
+
return new Template(result, template, attrNamesForParts);
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* Instance of a rendered template
|
|
1031
|
+
*/
|
|
1032
|
+
class TemplateInstance {
|
|
1033
|
+
constructor(result) {
|
|
1034
|
+
this.parts = [];
|
|
1035
|
+
this.fragment = null;
|
|
1036
|
+
this.template = prepareTemplate(result);
|
|
1037
|
+
}
|
|
1038
|
+
renderFragment() {
|
|
1039
|
+
if (!this.fragment) {
|
|
1040
|
+
// First render - clone template and create parts
|
|
1041
|
+
this.fragment = this.template.element.content.cloneNode(true);
|
|
1042
|
+
// Build a map of nodes from template to cloned fragment
|
|
1043
|
+
const walker = document.createTreeWalker(this.template.element.content, NodeFilter.SHOW_ALL);
|
|
1044
|
+
const clonedWalker = document.createTreeWalker(this.fragment, NodeFilter.SHOW_ALL);
|
|
1045
|
+
const nodeMap = new Map();
|
|
1046
|
+
let templateNode = walker.currentNode;
|
|
1047
|
+
let clonedNode = clonedWalker.currentNode;
|
|
1048
|
+
while (templateNode && clonedNode) {
|
|
1049
|
+
nodeMap.set(templateNode, clonedNode);
|
|
1050
|
+
templateNode = walker.nextNode();
|
|
1051
|
+
clonedNode = clonedWalker.nextNode();
|
|
1052
|
+
}
|
|
1053
|
+
for (const partDef of this.template.parts) {
|
|
1054
|
+
let part;
|
|
1055
|
+
switch (partDef.type) {
|
|
1056
|
+
case 'node':
|
|
1057
|
+
const startNode = nodeMap.get(partDef.startNode);
|
|
1058
|
+
const endNode = nodeMap.get(partDef.endNode);
|
|
1059
|
+
part = new NodePart(startNode, endNode);
|
|
1060
|
+
break;
|
|
1061
|
+
case 'attribute':
|
|
1062
|
+
const attrElement = nodeMap.get(partDef.element);
|
|
1063
|
+
part = new AttributePart(attrElement, partDef.name, partDef.attrStrings);
|
|
1064
|
+
break;
|
|
1065
|
+
case 'property':
|
|
1066
|
+
const propElement = nodeMap.get(partDef.element);
|
|
1067
|
+
part = new PropertyPart(propElement, partDef.name);
|
|
1068
|
+
break;
|
|
1069
|
+
case 'boolean-attribute':
|
|
1070
|
+
const boolElement = nodeMap.get(partDef.element);
|
|
1071
|
+
part = new BooleanAttributePart(boolElement, partDef.name);
|
|
1072
|
+
break;
|
|
1073
|
+
case 'event':
|
|
1074
|
+
const eventElement = nodeMap.get(partDef.element);
|
|
1075
|
+
part = new EventPart(eventElement, partDef.name);
|
|
1076
|
+
break;
|
|
1077
|
+
case 'if':
|
|
1078
|
+
const ifElement = nodeMap.get(partDef.element);
|
|
1079
|
+
part = new IfPart(ifElement);
|
|
1080
|
+
break;
|
|
1081
|
+
case 'case':
|
|
1082
|
+
const caseElement = nodeMap.get(partDef.caseElement);
|
|
1083
|
+
part = new CasePart(caseElement);
|
|
1084
|
+
break;
|
|
1085
|
+
default:
|
|
1086
|
+
throw new Error(`Unknown part type: ${partDef.type}`);
|
|
1087
|
+
}
|
|
1088
|
+
this.parts.push(part);
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
return this.fragment;
|
|
1092
|
+
}
|
|
1093
|
+
render(values) {
|
|
1094
|
+
const fragment = this.renderFragment();
|
|
1095
|
+
// Commit values to parts
|
|
1096
|
+
this.update(values);
|
|
1097
|
+
return fragment;
|
|
1098
|
+
}
|
|
1099
|
+
update(values) {
|
|
1100
|
+
// Process virtual elements (if/case) first
|
|
1101
|
+
for (let i = 0; i < this.parts.length; i++) {
|
|
1102
|
+
const part = this.parts[i];
|
|
1103
|
+
if (part instanceof IfPart || part instanceof CasePart) {
|
|
1104
|
+
part.commit(values[i]);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
// Then process all other parts
|
|
1108
|
+
for (let i = 0; i < this.parts.length; i++) {
|
|
1109
|
+
const part = this.parts[i];
|
|
1110
|
+
if (!(part instanceof IfPart || part instanceof CasePart)) {
|
|
1111
|
+
part.commit(values[i]);
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
clear() {
|
|
1116
|
+
for (const part of this.parts) {
|
|
1117
|
+
part.clear();
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* Base class for all parts
|
|
1123
|
+
*/
|
|
1124
|
+
class Part {
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* NodePart handles text content and nested templates
|
|
1128
|
+
*/
|
|
1129
|
+
class NodePart extends Part {
|
|
1130
|
+
constructor(startNode, endNode) {
|
|
1131
|
+
super();
|
|
1132
|
+
this.value = undefined;
|
|
1133
|
+
this.startNode = startNode;
|
|
1134
|
+
this.endNode = endNode;
|
|
1135
|
+
}
|
|
1136
|
+
commit(value) {
|
|
1137
|
+
if (value === this.value)
|
|
1138
|
+
return;
|
|
1139
|
+
this.value = value;
|
|
1140
|
+
// Handle arrays
|
|
1141
|
+
if (Array.isArray(value)) {
|
|
1142
|
+
this.commitArray(value);
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
// Handle nested templates
|
|
1146
|
+
if (isTemplateResult(value)) {
|
|
1147
|
+
this.commitTemplate(value);
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
// Handle unsafe HTML
|
|
1151
|
+
if (isUnsafeHTML(value)) {
|
|
1152
|
+
this.commitUnsafeHTML(value);
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
// Handle primitives
|
|
1156
|
+
this.commitPrimitive(value);
|
|
1157
|
+
}
|
|
1158
|
+
commitPrimitive(value) {
|
|
1159
|
+
this.clear();
|
|
1160
|
+
const text = value === null || value === undefined ? '' : String(value);
|
|
1161
|
+
const textNode = document.createTextNode(text);
|
|
1162
|
+
this.insertBefore(textNode);
|
|
1163
|
+
}
|
|
1164
|
+
commitTemplate(template) {
|
|
1165
|
+
this.clear();
|
|
1166
|
+
const instance = new TemplateInstance(template);
|
|
1167
|
+
const fragment = instance.render(template.values);
|
|
1168
|
+
this.insertBefore(fragment);
|
|
1169
|
+
}
|
|
1170
|
+
commitArray(values) {
|
|
1171
|
+
this.clear();
|
|
1172
|
+
for (const value of values) {
|
|
1173
|
+
if (isTemplateResult(value)) {
|
|
1174
|
+
const instance = new TemplateInstance(value);
|
|
1175
|
+
const fragment = instance.render(value.values);
|
|
1176
|
+
this.insertBefore(fragment);
|
|
1177
|
+
}
|
|
1178
|
+
else {
|
|
1179
|
+
const text = value === null || value === undefined ? '' : String(value);
|
|
1180
|
+
const textNode = document.createTextNode(text);
|
|
1181
|
+
this.insertBefore(textNode);
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
commitUnsafeHTML(value) {
|
|
1186
|
+
this.clear();
|
|
1187
|
+
// Create a temporary container to parse the HTML
|
|
1188
|
+
const temp = document.createElement('template');
|
|
1189
|
+
temp.innerHTML = value.html;
|
|
1190
|
+
// Insert all parsed nodes
|
|
1191
|
+
const fragment = temp.content;
|
|
1192
|
+
this.insertBefore(fragment);
|
|
1193
|
+
}
|
|
1194
|
+
insertBefore(node) {
|
|
1195
|
+
this.endNode.parentNode?.insertBefore(node, this.endNode);
|
|
1196
|
+
}
|
|
1197
|
+
clear() {
|
|
1198
|
+
const parent = this.startNode.parentNode;
|
|
1199
|
+
if (!parent)
|
|
1200
|
+
return;
|
|
1201
|
+
let node = this.startNode.nextSibling;
|
|
1202
|
+
while (node && node !== this.endNode) {
|
|
1203
|
+
const next = node.nextSibling;
|
|
1204
|
+
parent.removeChild(node);
|
|
1205
|
+
node = next;
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* AttributePart handles regular attribute updates
|
|
1211
|
+
*/
|
|
1212
|
+
class AttributePart extends Part {
|
|
1213
|
+
constructor(element, name, attrStrings) {
|
|
1214
|
+
super();
|
|
1215
|
+
this.value = undefined;
|
|
1216
|
+
this.element = element;
|
|
1217
|
+
this.name = name;
|
|
1218
|
+
this.attrStrings = attrStrings;
|
|
1219
|
+
}
|
|
1220
|
+
commit(value) {
|
|
1221
|
+
if (value === this.value)
|
|
1222
|
+
return;
|
|
1223
|
+
this.value = value;
|
|
1224
|
+
if (value === null || value === undefined) {
|
|
1225
|
+
this.element.removeAttribute(this.name);
|
|
1226
|
+
}
|
|
1227
|
+
else {
|
|
1228
|
+
// Data-oriented: compute final value from strings + value
|
|
1229
|
+
const finalValue = computeAttributeValue(this.attrStrings, value);
|
|
1230
|
+
this.element.setAttribute(this.name, finalValue);
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
clear() {
|
|
1234
|
+
this.element.removeAttribute(this.name);
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
/**
|
|
1238
|
+
* Data-oriented function to compute attribute value from static strings and dynamic value
|
|
1239
|
+
*/
|
|
1240
|
+
function computeAttributeValue(attrStrings, value) {
|
|
1241
|
+
if (!attrStrings || attrStrings.length === 0) {
|
|
1242
|
+
// No template strings, just use the value
|
|
1243
|
+
return String(value);
|
|
1244
|
+
}
|
|
1245
|
+
if (attrStrings.length === 1) {
|
|
1246
|
+
// Single string segment, no interpolation (shouldn't happen but handle it)
|
|
1247
|
+
return attrStrings[0];
|
|
1248
|
+
}
|
|
1249
|
+
// Multiple segments: "prefix" + value + "suffix"
|
|
1250
|
+
// For aria-label="Remove ${label}", attrStrings = ["Remove ", ""]
|
|
1251
|
+
return attrStrings[0] + String(value) + attrStrings[1];
|
|
1252
|
+
}
|
|
1253
|
+
/**
|
|
1254
|
+
* PropertyPart handles property bindings
|
|
1255
|
+
*/
|
|
1256
|
+
class PropertyPart extends Part {
|
|
1257
|
+
constructor(element, name) {
|
|
1258
|
+
super();
|
|
1259
|
+
this.value = undefined;
|
|
1260
|
+
this.element = element;
|
|
1261
|
+
this.name = name;
|
|
1262
|
+
}
|
|
1263
|
+
commit(value) {
|
|
1264
|
+
if (value === this.value)
|
|
1265
|
+
return;
|
|
1266
|
+
this.value = value;
|
|
1267
|
+
this.element[this.name] = value;
|
|
1268
|
+
}
|
|
1269
|
+
clear() {
|
|
1270
|
+
this.element[this.name] = undefined;
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
/**
|
|
1274
|
+
* BooleanAttributePart handles boolean attributes
|
|
1275
|
+
*/
|
|
1276
|
+
class BooleanAttributePart extends Part {
|
|
1277
|
+
constructor(element, name) {
|
|
1278
|
+
super();
|
|
1279
|
+
this.value = undefined;
|
|
1280
|
+
this.element = element;
|
|
1281
|
+
this.name = name;
|
|
1282
|
+
}
|
|
1283
|
+
commit(value) {
|
|
1284
|
+
if (value === this.value)
|
|
1285
|
+
return;
|
|
1286
|
+
this.value = value;
|
|
1287
|
+
if (value) {
|
|
1288
|
+
this.element.setAttribute(this.name, '');
|
|
1289
|
+
}
|
|
1290
|
+
else {
|
|
1291
|
+
this.element.removeAttribute(this.name);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
clear() {
|
|
1295
|
+
this.element.removeAttribute(this.name);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
/**
|
|
1299
|
+
* EventPart handles event listener bindings with keyboard shortcut support
|
|
1300
|
+
*/
|
|
1301
|
+
class EventPart extends Part {
|
|
1302
|
+
constructor(element, eventName) {
|
|
1303
|
+
super();
|
|
1304
|
+
this.listener = null;
|
|
1305
|
+
this.value = undefined;
|
|
1306
|
+
this.keyFilter = null;
|
|
1307
|
+
this.element = element;
|
|
1308
|
+
// Parse keyboard shortcuts:
|
|
1309
|
+
// Supports both dot notation (@keydown.enter) and colon notation (@keydown:Enter) to match @on decorator
|
|
1310
|
+
const dotIndex = eventName.indexOf('.');
|
|
1311
|
+
const colonIndex = eventName.indexOf(':');
|
|
1312
|
+
// Use whichever delimiter comes first (dot or colon)
|
|
1313
|
+
const delimiterIndex = dotIndex > 0 && colonIndex > 0
|
|
1314
|
+
? Math.min(dotIndex, colonIndex)
|
|
1315
|
+
: Math.max(dotIndex, colonIndex);
|
|
1316
|
+
if (delimiterIndex > 0) {
|
|
1317
|
+
const baseEvent = eventName.substring(0, delimiterIndex);
|
|
1318
|
+
const keySpec = eventName.substring(delimiterIndex + 1);
|
|
1319
|
+
this.eventName = baseEvent;
|
|
1320
|
+
this.keyFilter = parseKeyboardFilter(keySpec);
|
|
1321
|
+
}
|
|
1322
|
+
else {
|
|
1323
|
+
this.eventName = eventName;
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
commit(value) {
|
|
1327
|
+
// Skip if same value (but null/undefined always triggers update)
|
|
1328
|
+
if (value === this.value && value !== null && value !== undefined)
|
|
1329
|
+
return;
|
|
1330
|
+
// Remove old listener
|
|
1331
|
+
if (this.listener) {
|
|
1332
|
+
this.element.removeEventListener(this.eventName, this.listener);
|
|
1333
|
+
this.listener = null;
|
|
1334
|
+
}
|
|
1335
|
+
this.value = value;
|
|
1336
|
+
// Add new listener
|
|
1337
|
+
if (value === null || value === undefined) {
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
if (typeof value === 'function') {
|
|
1341
|
+
// Auto-bind to host element (the custom element with shadow root)
|
|
1342
|
+
// This allows @click=${this.handleClick} to work without manual binding
|
|
1343
|
+
const rootNode = this.element.getRootNode();
|
|
1344
|
+
const host = rootNode.host;
|
|
1345
|
+
// Create a wrapper that calls the handler with the host as context
|
|
1346
|
+
// This ensures consistent function reference for proper cleanup
|
|
1347
|
+
if (host) {
|
|
1348
|
+
this.listener = ((event) => {
|
|
1349
|
+
// If keyboard filter is specified, check if event matches
|
|
1350
|
+
if (this.keyFilter && !matchesKeyboardFilter(event, this.keyFilter)) {
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
value.call(host, event);
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
else {
|
|
1357
|
+
this.listener = ((event) => {
|
|
1358
|
+
if (this.keyFilter && !matchesKeyboardFilter(event, this.keyFilter)) {
|
|
1359
|
+
return;
|
|
1360
|
+
}
|
|
1361
|
+
value.call(null, event);
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
this.element.addEventListener(this.eventName, this.listener);
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
clear() {
|
|
1368
|
+
if (this.listener) {
|
|
1369
|
+
this.element.removeEventListener(this.eventName, this.listener);
|
|
1370
|
+
this.listener = null;
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
/**
|
|
1375
|
+
* Parse keyboard shortcut specification
|
|
1376
|
+
* Examples:
|
|
1377
|
+
* "enter" -> { key: "Enter" }
|
|
1378
|
+
* "ctrl+s" -> { key: "s", ctrl: true }
|
|
1379
|
+
* "ctrl+shift+s" -> { key: "s", ctrl: true, shift: true }
|
|
1380
|
+
* "~enter" -> { key: "Enter", anyModifiers: true }
|
|
1381
|
+
*/
|
|
1382
|
+
function parseKeyboardFilter(spec) {
|
|
1383
|
+
// Handle ~ prefix for matching regardless of modifiers
|
|
1384
|
+
const anyModifiers = spec.startsWith('~');
|
|
1385
|
+
if (anyModifiers) {
|
|
1386
|
+
spec = spec.substring(1);
|
|
1387
|
+
}
|
|
1388
|
+
const parts = spec.split('+');
|
|
1389
|
+
const filter = {
|
|
1390
|
+
key: '',
|
|
1391
|
+
anyModifiers
|
|
1392
|
+
};
|
|
1393
|
+
for (const part of parts) {
|
|
1394
|
+
const lower = part.toLowerCase();
|
|
1395
|
+
if (lower === 'ctrl' || lower === 'control') {
|
|
1396
|
+
filter.ctrl = true;
|
|
1397
|
+
}
|
|
1398
|
+
else if (lower === 'alt') {
|
|
1399
|
+
filter.alt = true;
|
|
1400
|
+
}
|
|
1401
|
+
else if (lower === 'shift') {
|
|
1402
|
+
filter.shift = true;
|
|
1403
|
+
}
|
|
1404
|
+
else if (lower === 'meta' || lower === 'cmd' || lower === 'command') {
|
|
1405
|
+
filter.meta = true;
|
|
1406
|
+
}
|
|
1407
|
+
else {
|
|
1408
|
+
// This is the key itself - normalize common keys
|
|
1409
|
+
filter.key = normalizeKey(part);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
return filter;
|
|
1413
|
+
}
|
|
1414
|
+
/**
|
|
1415
|
+
* Normalize key names to match KeyboardEvent.key
|
|
1416
|
+
*/
|
|
1417
|
+
function normalizeKey(key) {
|
|
1418
|
+
const keyMap = {
|
|
1419
|
+
'esc': 'Escape',
|
|
1420
|
+
'escape': 'Escape',
|
|
1421
|
+
'enter': 'Enter',
|
|
1422
|
+
'return': 'Enter',
|
|
1423
|
+
'space': ' ',
|
|
1424
|
+
'spacebar': ' ',
|
|
1425
|
+
'up': 'ArrowUp',
|
|
1426
|
+
'down': 'ArrowDown',
|
|
1427
|
+
'left': 'ArrowLeft',
|
|
1428
|
+
'right': 'ArrowRight',
|
|
1429
|
+
'arrowup': 'ArrowUp',
|
|
1430
|
+
'arrowdown': 'ArrowDown',
|
|
1431
|
+
'arrowleft': 'ArrowLeft',
|
|
1432
|
+
'arrowright': 'ArrowRight',
|
|
1433
|
+
'delete': 'Delete',
|
|
1434
|
+
'del': 'Delete',
|
|
1435
|
+
'backspace': 'Backspace',
|
|
1436
|
+
'tab': 'Tab',
|
|
1437
|
+
'home': 'Home',
|
|
1438
|
+
'end': 'End',
|
|
1439
|
+
'pageup': 'PageUp',
|
|
1440
|
+
'pagedown': 'PageDown'
|
|
1441
|
+
};
|
|
1442
|
+
const lower = key.toLowerCase();
|
|
1443
|
+
return keyMap[lower] || key;
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Check if keyboard event matches the filter
|
|
1447
|
+
*/
|
|
1448
|
+
function matchesKeyboardFilter(event, filter) {
|
|
1449
|
+
// Check key match
|
|
1450
|
+
if (event.key !== filter.key) {
|
|
1451
|
+
return false;
|
|
1452
|
+
}
|
|
1453
|
+
// If anyModifiers is true, we don't care about modifiers
|
|
1454
|
+
if (filter.anyModifiers) {
|
|
1455
|
+
return true;
|
|
1456
|
+
}
|
|
1457
|
+
// Check modifiers - by default, exact match
|
|
1458
|
+
// If filter specifies ctrl: true, event must have ctrlKey
|
|
1459
|
+
// If filter doesn't specify ctrl, event must NOT have ctrlKey
|
|
1460
|
+
const ctrlMatch = filter.ctrl ? event.ctrlKey : !event.ctrlKey;
|
|
1461
|
+
const altMatch = filter.alt ? event.altKey : !event.altKey;
|
|
1462
|
+
const shiftMatch = filter.shift ? event.shiftKey : !event.shiftKey;
|
|
1463
|
+
const metaMatch = filter.meta ? event.metaKey : !event.metaKey;
|
|
1464
|
+
return ctrlMatch && altMatch && shiftMatch && metaMatch;
|
|
1465
|
+
}
|
|
1466
|
+
/**
|
|
1467
|
+
* IfPart handles <if> conditional rendering
|
|
1468
|
+
* Uses document fragments to actually remove/insert nodes from the DOM
|
|
1469
|
+
* instead of just hiding them with CSS
|
|
1470
|
+
*/
|
|
1471
|
+
class IfPart extends Part {
|
|
1472
|
+
constructor(ifElement) {
|
|
1473
|
+
super();
|
|
1474
|
+
this.value = undefined;
|
|
1475
|
+
this.fragment = null;
|
|
1476
|
+
this.childNodes = [];
|
|
1477
|
+
this.ifElement = ifElement;
|
|
1478
|
+
// Set display to contents so the wrapper is transparent
|
|
1479
|
+
// display:contents makes the element invisible but renders its children
|
|
1480
|
+
this.ifElement.style.display = 'contents';
|
|
1481
|
+
// Store initial child nodes
|
|
1482
|
+
this.childNodes = Array.from(this.ifElement.childNodes);
|
|
1483
|
+
}
|
|
1484
|
+
commit(value) {
|
|
1485
|
+
const condition = Boolean(value);
|
|
1486
|
+
// If condition hasn't changed, do nothing
|
|
1487
|
+
if (this.value !== undefined && Boolean(this.value) === condition) {
|
|
1488
|
+
return;
|
|
1489
|
+
}
|
|
1490
|
+
this.value = value;
|
|
1491
|
+
if (condition) {
|
|
1492
|
+
// Show: restore children from fragment if they were removed
|
|
1493
|
+
if (this.fragment && this.fragment.hasChildNodes()) {
|
|
1494
|
+
this.ifElement.appendChild(this.fragment);
|
|
1495
|
+
this.fragment = null;
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
else {
|
|
1499
|
+
// Hide: move children to document fragment
|
|
1500
|
+
this.fragment = document.createDocumentFragment();
|
|
1501
|
+
while (this.ifElement.firstChild) {
|
|
1502
|
+
this.fragment.appendChild(this.ifElement.firstChild);
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
clear() {
|
|
1507
|
+
// Move all children to fragment
|
|
1508
|
+
this.fragment = document.createDocumentFragment();
|
|
1509
|
+
while (this.ifElement.firstChild) {
|
|
1510
|
+
this.fragment.appendChild(this.ifElement.firstChild);
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* CasePart handles <case>/<when>/<default> conditional rendering
|
|
1516
|
+
* Uses document fragments to remove/insert <when> and <default> children
|
|
1517
|
+
*/
|
|
1518
|
+
class CasePart extends Part {
|
|
1519
|
+
constructor(caseElement) {
|
|
1520
|
+
super();
|
|
1521
|
+
this.value = undefined;
|
|
1522
|
+
this.fragments = new Map(); // Store removed children in fragments
|
|
1523
|
+
this.attachedChildren = new Map(); // Track which children are in DOM
|
|
1524
|
+
this.caseElement = caseElement;
|
|
1525
|
+
// Store children in original order
|
|
1526
|
+
this.childrenList = Array.from(this.caseElement.children);
|
|
1527
|
+
// Initialize all children as attached
|
|
1528
|
+
this.childrenList.forEach(child => {
|
|
1529
|
+
this.attachedChildren.set(child, true);
|
|
1530
|
+
// Create a fragment for each child to hold it when detached
|
|
1531
|
+
this.fragments.set(child, document.createDocumentFragment());
|
|
1532
|
+
});
|
|
1533
|
+
}
|
|
1534
|
+
commit(value) {
|
|
1535
|
+
// If value hasn't changed, do nothing
|
|
1536
|
+
if (this.value === value)
|
|
1537
|
+
return;
|
|
1538
|
+
this.value = value;
|
|
1539
|
+
// Find matching <when> or <default> and remove/insert appropriately
|
|
1540
|
+
let hasMatch = false;
|
|
1541
|
+
// First pass: check if any <when> matches
|
|
1542
|
+
for (const child of this.childrenList) {
|
|
1543
|
+
if (child.tagName.toLowerCase() === 'when') {
|
|
1544
|
+
const whenValue = child.getAttribute('value');
|
|
1545
|
+
if (whenValue === String(value)) {
|
|
1546
|
+
hasMatch = true;
|
|
1547
|
+
break;
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
// Second pass: remove/insert children based on match
|
|
1552
|
+
for (const child of this.childrenList) {
|
|
1553
|
+
const tagName = child.tagName.toLowerCase();
|
|
1554
|
+
const isAttached = this.attachedChildren.get(child) ?? true;
|
|
1555
|
+
let shouldBeAttached = false;
|
|
1556
|
+
if (tagName === 'when') {
|
|
1557
|
+
const whenValue = child.getAttribute('value');
|
|
1558
|
+
shouldBeAttached = (whenValue === String(value));
|
|
1559
|
+
}
|
|
1560
|
+
else if (tagName === 'default') {
|
|
1561
|
+
shouldBeAttached = !hasMatch;
|
|
1562
|
+
}
|
|
1563
|
+
// Update DOM if state changed
|
|
1564
|
+
if (shouldBeAttached && !isAttached) {
|
|
1565
|
+
// Re-insert from fragment in correct position
|
|
1566
|
+
// Find next sibling that is still attached
|
|
1567
|
+
const childIndex = this.childrenList.indexOf(child);
|
|
1568
|
+
let insertBefore = null;
|
|
1569
|
+
for (let i = childIndex + 1; i < this.childrenList.length; i++) {
|
|
1570
|
+
if (this.attachedChildren.get(this.childrenList[i])) {
|
|
1571
|
+
insertBefore = this.childrenList[i];
|
|
1572
|
+
break;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
// Get child from fragment
|
|
1576
|
+
const fragment = this.fragments.get(child);
|
|
1577
|
+
if (fragment && fragment.firstChild) {
|
|
1578
|
+
if (insertBefore) {
|
|
1579
|
+
this.caseElement.insertBefore(fragment.firstChild, insertBefore);
|
|
1580
|
+
}
|
|
1581
|
+
else {
|
|
1582
|
+
this.caseElement.appendChild(fragment.firstChild);
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
this.attachedChildren.set(child, true);
|
|
1586
|
+
}
|
|
1587
|
+
else if (!shouldBeAttached && isAttached) {
|
|
1588
|
+
// Move to fragment
|
|
1589
|
+
const fragment = this.fragments.get(child);
|
|
1590
|
+
if (fragment) {
|
|
1591
|
+
fragment.appendChild(child);
|
|
1592
|
+
}
|
|
1593
|
+
this.attachedChildren.set(child, false);
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
clear() {
|
|
1598
|
+
// Move all children to fragments
|
|
1599
|
+
for (const child of this.childrenList) {
|
|
1600
|
+
if (this.attachedChildren.get(child)) {
|
|
1601
|
+
const fragment = this.fragments.get(child);
|
|
1602
|
+
if (fragment) {
|
|
1603
|
+
fragment.appendChild(child);
|
|
1604
|
+
}
|
|
1605
|
+
this.attachedChildren.set(child, false);
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
/**
|
|
1612
|
+
* @on decorator for listening to events
|
|
1613
|
+
* Use in elements or controllers to listen to DOM events or custom events
|
|
1614
|
+
*/
|
|
1615
|
+
const ON_HANDLERS = getSymbol('on-handlers');
|
|
1616
|
+
/**
|
|
1617
|
+
* @on decorator for listening to events
|
|
1618
|
+
*
|
|
1619
|
+
* Works in both elements and controllers with full event delegation support.
|
|
1620
|
+
*
|
|
1621
|
+
* @param eventName - Event name(s) to listen for
|
|
1622
|
+
* @param selector - Optional CSS selector for event delegation
|
|
1623
|
+
* @param options - Event listener options including debounce/throttle
|
|
1624
|
+
*
|
|
1625
|
+
* @example
|
|
1626
|
+
* ```typescript
|
|
1627
|
+
* // In elements
|
|
1628
|
+
* @element('my-button')
|
|
1629
|
+
* class MyButton extends HTMLElement {
|
|
1630
|
+
* @on('click', 'button')
|
|
1631
|
+
* handleClick(e: MouseEvent) {
|
|
1632
|
+
* console.log('Button clicked!', e);
|
|
1633
|
+
* }
|
|
1634
|
+
*
|
|
1635
|
+
* @on('input', 'input', { debounce: 300 })
|
|
1636
|
+
* handleInput(e: Event) {
|
|
1637
|
+
* console.log('Input changed:', (e.target as HTMLInputElement).value);
|
|
1638
|
+
* }
|
|
1639
|
+
* }
|
|
1640
|
+
*
|
|
1641
|
+
* // In controllers
|
|
1642
|
+
* @controller('my-controller')
|
|
1643
|
+
* class MyController {
|
|
1644
|
+
* element!: HTMLElement;
|
|
1645
|
+
*
|
|
1646
|
+
* @on('count-changed')
|
|
1647
|
+
* handleCountChanged(e: CustomEvent) {
|
|
1648
|
+
* console.log('Count changed to:', e.detail.count);
|
|
1649
|
+
* }
|
|
1650
|
+
*
|
|
1651
|
+
* @on('click', '.item', { throttle: 100 })
|
|
1652
|
+
* handleItemClick(e: MouseEvent) {
|
|
1653
|
+
* console.log('Item clicked');
|
|
1654
|
+
* }
|
|
1655
|
+
* }
|
|
1656
|
+
* ```
|
|
1657
|
+
*/
|
|
1658
|
+
function on(eventName, selectorOrOptions, options) {
|
|
1659
|
+
// Parse arguments to support multiple call signatures
|
|
1660
|
+
let selector = null;
|
|
1661
|
+
let opts = {};
|
|
1662
|
+
if (typeof selectorOrOptions === 'string') {
|
|
1663
|
+
// With selector: (eventName, selector, options)
|
|
1664
|
+
selector = selectorOrOptions;
|
|
1665
|
+
opts = options || {};
|
|
1666
|
+
}
|
|
1667
|
+
else if (selectorOrOptions === null && options) {
|
|
1668
|
+
// With null selector: (eventName, null, options)
|
|
1669
|
+
opts = options;
|
|
1670
|
+
}
|
|
1671
|
+
else if (selectorOrOptions && typeof selectorOrOptions === 'object') {
|
|
1672
|
+
// Without selector: (eventName, options)
|
|
1673
|
+
opts = selectorOrOptions;
|
|
1674
|
+
}
|
|
1675
|
+
return function (originalMethod, context) {
|
|
1676
|
+
const methodName = context.name;
|
|
1677
|
+
const initKey = `__on_init_${methodName}_${selector || ''}_${JSON.stringify(eventName)}`;
|
|
1678
|
+
context.addInitializer(function () {
|
|
1679
|
+
const constructor = this.constructor;
|
|
1680
|
+
// Only initialize once per class, not per instance
|
|
1681
|
+
if (constructor[initKey])
|
|
1682
|
+
return;
|
|
1683
|
+
constructor[initKey] = true;
|
|
1684
|
+
if (!constructor[ON_HANDLERS]) {
|
|
1685
|
+
constructor[ON_HANDLERS] = [];
|
|
1686
|
+
}
|
|
1687
|
+
const eventNames = Array.isArray(eventName) ? eventName : [eventName];
|
|
1688
|
+
for (const event of eventNames) {
|
|
1689
|
+
constructor[ON_HANDLERS].push({
|
|
1690
|
+
eventName: event,
|
|
1691
|
+
selector,
|
|
1692
|
+
methodName,
|
|
1693
|
+
method: originalMethod,
|
|
1694
|
+
options: opts
|
|
1695
|
+
});
|
|
1696
|
+
}
|
|
1697
|
+
});
|
|
1698
|
+
return originalMethod;
|
|
1699
|
+
};
|
|
1700
|
+
}
|
|
1701
|
+
/**
|
|
1702
|
+
* Create a debounced version of a function
|
|
1703
|
+
*/
|
|
1704
|
+
function debounce$1(fn, delay) {
|
|
1705
|
+
let timeoutId = null;
|
|
1706
|
+
return function (...args) {
|
|
1707
|
+
if (timeoutId !== null) {
|
|
1708
|
+
clearTimeout(timeoutId);
|
|
1709
|
+
}
|
|
1710
|
+
timeoutId = setTimeout(() => {
|
|
1711
|
+
fn.apply(this, args);
|
|
1712
|
+
timeoutId = null;
|
|
1713
|
+
}, delay);
|
|
1714
|
+
};
|
|
1715
|
+
}
|
|
1716
|
+
/**
|
|
1717
|
+
* Create a throttled version of a function (leading edge only)
|
|
1718
|
+
*/
|
|
1719
|
+
function throttle$1(fn, delay) {
|
|
1720
|
+
let lastCall = 0;
|
|
1721
|
+
return function (...args) {
|
|
1722
|
+
const now = Date.now();
|
|
1723
|
+
const timeSinceLastCall = now - lastCall;
|
|
1724
|
+
if (timeSinceLastCall >= delay) {
|
|
1725
|
+
lastCall = now;
|
|
1726
|
+
fn.apply(this, args);
|
|
1727
|
+
}
|
|
1728
|
+
// Events within the throttle window are simply ignored (leading edge only)
|
|
1729
|
+
};
|
|
1730
|
+
}
|
|
1731
|
+
/**
|
|
1732
|
+
* Events that don't bubble - these require capture phase for delegation
|
|
1733
|
+
*/
|
|
1734
|
+
const NON_BUBBLING_EVENTS = new Set([
|
|
1735
|
+
'scroll',
|
|
1736
|
+
'focus',
|
|
1737
|
+
'blur',
|
|
1738
|
+
'load',
|
|
1739
|
+
'unload',
|
|
1740
|
+
'error',
|
|
1741
|
+
'resize',
|
|
1742
|
+
'abort',
|
|
1743
|
+
'mouseenter',
|
|
1744
|
+
'mouseleave',
|
|
1745
|
+
'pointerenter',
|
|
1746
|
+
'pointerleave',
|
|
1747
|
+
]);
|
|
1748
|
+
/**
|
|
1749
|
+
* Setup event listeners for an element or controller instance
|
|
1750
|
+
* Called automatically during element connection or controller attachment
|
|
1751
|
+
*/
|
|
1752
|
+
function setupEventHandlers(instance, targetElement) {
|
|
1753
|
+
const handlers = instance.constructor[ON_HANDLERS];
|
|
1754
|
+
if (!handlers || !Array.isArray(handlers) || handlers.length === 0) {
|
|
1755
|
+
return;
|
|
1756
|
+
}
|
|
1757
|
+
// Initialize cleanup object if needed
|
|
1758
|
+
if (!instance[CLEANUP]) {
|
|
1759
|
+
instance[CLEANUP] = { events: [], channels: [], observers: [] };
|
|
1760
|
+
}
|
|
1761
|
+
else if (!instance[CLEANUP].events) {
|
|
1762
|
+
instance[CLEANUP].events = [];
|
|
1763
|
+
}
|
|
1764
|
+
else if (instance[CLEANUP].events.length > 0) {
|
|
1765
|
+
// Events already set up - clean them up first to avoid duplicates
|
|
1766
|
+
cleanupEventHandlers(instance);
|
|
1767
|
+
}
|
|
1768
|
+
for (const handler of handlers) {
|
|
1769
|
+
// Get current method from instance (preserves decorator stacking)
|
|
1770
|
+
const currentMethod = instance[handler.methodName];
|
|
1771
|
+
let boundMethod = currentMethod ? currentMethod.bind(instance) : handler.method.bind(instance);
|
|
1772
|
+
const handlerOptions = handler.options || {};
|
|
1773
|
+
// Parse event name for key modifiers
|
|
1774
|
+
// Supports both dot notation (@keydown.enter) and colon notation (@keydown:Enter)
|
|
1775
|
+
const dotIndex = handler.eventName.indexOf('.');
|
|
1776
|
+
const colonIndex = handler.eventName.indexOf(':');
|
|
1777
|
+
const delimiterIndex = dotIndex > 0 && colonIndex > 0
|
|
1778
|
+
? Math.min(dotIndex, colonIndex)
|
|
1779
|
+
: Math.max(dotIndex, colonIndex);
|
|
1780
|
+
const baseEventName = delimiterIndex > 0
|
|
1781
|
+
? handler.eventName.substring(0, delimiterIndex)
|
|
1782
|
+
: handler.eventName;
|
|
1783
|
+
const keyModifier = delimiterIndex > 0
|
|
1784
|
+
? handler.eventName.substring(delimiterIndex + 1)
|
|
1785
|
+
: null;
|
|
1786
|
+
// Apply debounce if specified
|
|
1787
|
+
if (handlerOptions.debounce && handlerOptions.debounce > 0) {
|
|
1788
|
+
boundMethod = debounce$1(boundMethod, handlerOptions.debounce);
|
|
1789
|
+
}
|
|
1790
|
+
// Apply throttle if specified (debounce takes precedence)
|
|
1791
|
+
else if (handlerOptions.throttle && handlerOptions.throttle > 0) {
|
|
1792
|
+
boundMethod = throttle$1(boundMethod, handlerOptions.throttle);
|
|
1793
|
+
}
|
|
1794
|
+
// Create event handler with key modifier support
|
|
1795
|
+
// Uses shared keyboard filter implementation from parts.ts
|
|
1796
|
+
let keyFilter = null;
|
|
1797
|
+
if (keyModifier && ['keydown', 'keyup', 'keypress'].includes(baseEventName)) {
|
|
1798
|
+
keyFilter = parseKeyboardFilter(keyModifier);
|
|
1799
|
+
}
|
|
1800
|
+
const createKeyModifierHandler = (method) => {
|
|
1801
|
+
if (!keyFilter) {
|
|
1802
|
+
return method;
|
|
1803
|
+
}
|
|
1804
|
+
return (event) => {
|
|
1805
|
+
const keyEvent = event;
|
|
1806
|
+
if (matchesKeyboardFilter(keyEvent, keyFilter)) {
|
|
1807
|
+
method(event);
|
|
1808
|
+
}
|
|
1809
|
+
};
|
|
1810
|
+
};
|
|
1811
|
+
// Apply key modifier wrapper
|
|
1812
|
+
const keyModifierMethod = createKeyModifierHandler(boundMethod);
|
|
1813
|
+
// Main event handler with error handling and event delegation
|
|
1814
|
+
if (handler.selector) {
|
|
1815
|
+
// Delegated event handling - use shadow root if available
|
|
1816
|
+
const eventRoot = targetElement.shadowRoot || targetElement;
|
|
1817
|
+
const delegatedHandler = (event) => {
|
|
1818
|
+
const target = event.target;
|
|
1819
|
+
let matchingElement = null;
|
|
1820
|
+
// Check if target itself matches the selector
|
|
1821
|
+
if (target.matches && target.matches(handler.selector)) {
|
|
1822
|
+
matchingElement = target;
|
|
1823
|
+
}
|
|
1824
|
+
// Check if any parent matches the selector (event delegation)
|
|
1825
|
+
else if (target.closest) {
|
|
1826
|
+
matchingElement = target.closest(handler.selector);
|
|
1827
|
+
}
|
|
1828
|
+
// Only handle if we found a match
|
|
1829
|
+
// Note: No need to check contains() since the event bubbled to eventRoot
|
|
1830
|
+
if (matchingElement) {
|
|
1831
|
+
// Apply automatic preventDefault/stopPropagation
|
|
1832
|
+
if (handlerOptions.preventDefault) {
|
|
1833
|
+
event.preventDefault();
|
|
1834
|
+
}
|
|
1835
|
+
if (handlerOptions.stopPropagation) {
|
|
1836
|
+
event.stopPropagation();
|
|
1837
|
+
event.stopImmediatePropagation();
|
|
1838
|
+
}
|
|
1839
|
+
try {
|
|
1840
|
+
keyModifierMethod(event);
|
|
1841
|
+
}
|
|
1842
|
+
catch (error) {
|
|
1843
|
+
console.error(`Error in event handler ${handler.methodName}:`, error);
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
};
|
|
1847
|
+
// Auto-enable capture for non-bubbling events when using delegation
|
|
1848
|
+
const needsCapture = NON_BUBBLING_EVENTS.has(baseEventName);
|
|
1849
|
+
const useCapture = handlerOptions.capture !== undefined
|
|
1850
|
+
? handlerOptions.capture
|
|
1851
|
+
: needsCapture;
|
|
1852
|
+
const listenerOptions = {
|
|
1853
|
+
capture: useCapture,
|
|
1854
|
+
once: handlerOptions.once || false,
|
|
1855
|
+
passive: handlerOptions.passive || false,
|
|
1856
|
+
};
|
|
1857
|
+
eventRoot.addEventListener(baseEventName, delegatedHandler, listenerOptions);
|
|
1858
|
+
instance[CLEANUP].events.push({
|
|
1859
|
+
target: eventRoot,
|
|
1860
|
+
eventName: baseEventName,
|
|
1861
|
+
handler: delegatedHandler,
|
|
1862
|
+
options: listenerOptions,
|
|
1863
|
+
});
|
|
1864
|
+
}
|
|
1865
|
+
else {
|
|
1866
|
+
// Direct event handling - on the element itself
|
|
1867
|
+
// If element has shadow root, listen on both shadow root AND host element
|
|
1868
|
+
// to catch events from inside shadow DOM (with correct target) and on host itself
|
|
1869
|
+
const shadowRoot = targetElement.shadowRoot;
|
|
1870
|
+
const handledSymbol = Symbol('snice-event-handled');
|
|
1871
|
+
const wrappedMethod = (event) => {
|
|
1872
|
+
// Prevent double-triggering when listening on both shadow root and host
|
|
1873
|
+
if (event[handledSymbol]) {
|
|
1874
|
+
return;
|
|
1875
|
+
}
|
|
1876
|
+
event[handledSymbol] = true;
|
|
1877
|
+
try {
|
|
1878
|
+
// Apply automatic preventDefault/stopPropagation
|
|
1879
|
+
if (handlerOptions.preventDefault) {
|
|
1880
|
+
event.preventDefault();
|
|
1881
|
+
}
|
|
1882
|
+
if (handlerOptions.stopPropagation) {
|
|
1883
|
+
event.stopPropagation();
|
|
1884
|
+
}
|
|
1885
|
+
keyModifierMethod(event);
|
|
1886
|
+
}
|
|
1887
|
+
catch (error) {
|
|
1888
|
+
console.error(`Error in event handler ${handler.methodName}:`, error);
|
|
1889
|
+
}
|
|
1890
|
+
};
|
|
1891
|
+
const listenerOptions = {
|
|
1892
|
+
capture: handlerOptions.capture || false,
|
|
1893
|
+
once: handlerOptions.once || false,
|
|
1894
|
+
passive: handlerOptions.passive || false,
|
|
1895
|
+
};
|
|
1896
|
+
if (shadowRoot) {
|
|
1897
|
+
// Listen on shadow root for events inside shadow DOM
|
|
1898
|
+
shadowRoot.addEventListener(baseEventName, wrappedMethod, listenerOptions);
|
|
1899
|
+
instance[CLEANUP].events.push({
|
|
1900
|
+
target: shadowRoot,
|
|
1901
|
+
eventName: baseEventName,
|
|
1902
|
+
handler: wrappedMethod,
|
|
1903
|
+
options: listenerOptions,
|
|
1904
|
+
});
|
|
1905
|
+
}
|
|
1906
|
+
// Also listen on host element (for clicks on host itself or when no shadow root)
|
|
1907
|
+
targetElement.addEventListener(baseEventName, wrappedMethod, listenerOptions);
|
|
1908
|
+
instance[CLEANUP].events.push({
|
|
1909
|
+
target: targetElement,
|
|
1910
|
+
eventName: baseEventName,
|
|
1911
|
+
handler: wrappedMethod,
|
|
1912
|
+
options: listenerOptions,
|
|
1913
|
+
});
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
/**
|
|
1918
|
+
* Cleanup event listeners for a controller instance
|
|
1919
|
+
* Called automatically by the controller system during detach
|
|
1920
|
+
*/
|
|
1921
|
+
function cleanupEventHandlers(instance) {
|
|
1922
|
+
if (!instance[CLEANUP]?.events)
|
|
1923
|
+
return;
|
|
1924
|
+
for (const { target, eventName, handler, options } of instance[CLEANUP].events) {
|
|
1925
|
+
target.removeEventListener(eventName, handler, options);
|
|
1926
|
+
}
|
|
1927
|
+
instance[CLEANUP].events = [];
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
// Controller-scoped cleanup registry
|
|
1931
|
+
class ControllerScope {
|
|
1932
|
+
constructor() {
|
|
1933
|
+
this.cleanupFns = new Map();
|
|
1934
|
+
this.pendingOperations = new Set();
|
|
1935
|
+
}
|
|
1936
|
+
register(key, cleanup) {
|
|
1937
|
+
this.cleanupFns.set(key, cleanup);
|
|
1938
|
+
}
|
|
1939
|
+
unregister(key) {
|
|
1940
|
+
this.cleanupFns.delete(key);
|
|
1941
|
+
}
|
|
1942
|
+
async cleanup() {
|
|
1943
|
+
// Wait for all pending operations
|
|
1944
|
+
await Promise.all(this.pendingOperations);
|
|
1945
|
+
// Run all cleanup functions
|
|
1946
|
+
for (const cleanup of this.cleanupFns.values()) {
|
|
1947
|
+
try {
|
|
1948
|
+
await cleanup();
|
|
1949
|
+
}
|
|
1950
|
+
catch (error) {
|
|
1951
|
+
console.error('Error during cleanup:', error);
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
this.cleanupFns.clear();
|
|
1955
|
+
}
|
|
1956
|
+
async runOperation(operation) {
|
|
1957
|
+
const promise = operation();
|
|
1958
|
+
const voidPromise = promise.then(() => { }, () => { });
|
|
1959
|
+
this.pendingOperations.add(voidPromise);
|
|
1960
|
+
try {
|
|
1961
|
+
const result = await promise;
|
|
1962
|
+
this.pendingOperations.delete(voidPromise);
|
|
1963
|
+
return result;
|
|
1964
|
+
}
|
|
1965
|
+
catch (error) {
|
|
1966
|
+
this.pendingOperations.delete(voidPromise);
|
|
1967
|
+
throw error;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
/**
|
|
1972
|
+
* Decorator to register a controller class with a name
|
|
1973
|
+
* @param name The name to register the controller under
|
|
1974
|
+
*/
|
|
1975
|
+
function controller(name) {
|
|
1976
|
+
return function (constructor, _context) {
|
|
1977
|
+
snice.controllerRegistry.set(name, constructor);
|
|
1978
|
+
// Mark as controller class for channel decorator detection
|
|
1979
|
+
constructor.prototype[IS_CONTROLLER_CLASS] = true;
|
|
1980
|
+
return constructor;
|
|
1981
|
+
};
|
|
1982
|
+
}
|
|
1983
|
+
/**
|
|
1984
|
+
* Attaches a controller to an element
|
|
1985
|
+
* @param element The element to attach the controller to
|
|
1986
|
+
* @param controllerName The name of the controller to attach
|
|
1987
|
+
*/
|
|
1988
|
+
async function attachController(element, controllerName) {
|
|
1989
|
+
const existingController = element[CONTROLLER_KEY];
|
|
1990
|
+
const existingName = element[CONTROLLER_NAME_KEY];
|
|
1991
|
+
// For native elements, check if this is actually the desired controller
|
|
1992
|
+
const nativeController = element[NATIVE_CONTROLLER];
|
|
1993
|
+
if (nativeController !== undefined && nativeController !== controllerName) {
|
|
1994
|
+
// This attachment is outdated, skip it
|
|
1044
1995
|
return;
|
|
1045
1996
|
}
|
|
1046
1997
|
if (existingName === controllerName && existingController) {
|
|
@@ -1079,12 +2030,12 @@ async function attachController(element, controllerName) {
|
|
|
1079
2030
|
await scope.runOperation(async () => {
|
|
1080
2031
|
await controllerInstance.attach(element);
|
|
1081
2032
|
});
|
|
1082
|
-
// Setup @on event handlers for controller
|
|
1083
|
-
setupEventHandlers(controllerInstance, element);
|
|
1084
2033
|
// Setup @observe observers for controller
|
|
1085
2034
|
setupObservers(controllerInstance, element);
|
|
1086
2035
|
// Setup @channel handlers for controller
|
|
1087
2036
|
setupResponseHandlers(controllerInstance, element);
|
|
2037
|
+
// Setup @on event handlers for controller
|
|
2038
|
+
setupEventHandlers(controllerInstance, element);
|
|
1088
2039
|
element.dispatchEvent(new CustomEvent('@snice/controller-attached', {
|
|
1089
2040
|
detail: { name: controllerName, controller: controllerInstance }
|
|
1090
2041
|
}));
|
|
@@ -1110,12 +2061,12 @@ async function detachController(element) {
|
|
|
1110
2061
|
await controllerInstance.detach(element);
|
|
1111
2062
|
}
|
|
1112
2063
|
controllerInstance.element = null;
|
|
1113
|
-
// Cleanup @on event handlers for controller
|
|
1114
|
-
cleanupEventHandlers(controllerInstance);
|
|
1115
2064
|
// Cleanup @observe observers for controller
|
|
1116
2065
|
cleanupObservers(controllerInstance);
|
|
1117
2066
|
// Cleanup @channel handlers for controller
|
|
1118
2067
|
cleanupResponseHandlers(controllerInstance);
|
|
2068
|
+
// Cleanup @on event handlers for controller
|
|
2069
|
+
cleanupEventHandlers(controllerInstance);
|
|
1119
2070
|
// Cleanup the controller scope
|
|
1120
2071
|
if (scope) {
|
|
1121
2072
|
await scope.cleanup();
|
|
@@ -1198,33 +2149,168 @@ function useNativeElementControllers() {
|
|
|
1198
2149
|
});
|
|
1199
2150
|
}
|
|
1200
2151
|
}
|
|
1201
|
-
});
|
|
1202
|
-
// Start observing when DOM is ready
|
|
1203
|
-
if (document.readyState === 'loading') {
|
|
1204
|
-
document.addEventListener('DOMContentLoaded', () => {
|
|
1205
|
-
// Process existing elements (excluding custom elements)
|
|
1206
|
-
document.querySelectorAll('[controller]:not([class*="-"])').forEach(processElement);
|
|
1207
|
-
// Start observing
|
|
1208
|
-
observer.observe(document.body, {
|
|
1209
|
-
attributes: true,
|
|
1210
|
-
attributeFilter: ['controller'],
|
|
1211
|
-
childList: true,
|
|
1212
|
-
subtree: true
|
|
1213
|
-
});
|
|
1214
|
-
});
|
|
2152
|
+
});
|
|
2153
|
+
// Start observing when DOM is ready
|
|
2154
|
+
if (document.readyState === 'loading') {
|
|
2155
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
2156
|
+
// Process existing elements (excluding custom elements)
|
|
2157
|
+
document.querySelectorAll('[controller]:not([class*="-"])').forEach(processElement);
|
|
2158
|
+
// Start observing
|
|
2159
|
+
observer.observe(document.body, {
|
|
2160
|
+
attributes: true,
|
|
2161
|
+
attributeFilter: ['controller'],
|
|
2162
|
+
childList: true,
|
|
2163
|
+
subtree: true
|
|
2164
|
+
});
|
|
2165
|
+
});
|
|
2166
|
+
}
|
|
2167
|
+
else {
|
|
2168
|
+
// DOM already loaded
|
|
2169
|
+
document.querySelectorAll('[controller]:not([class*="-"])').forEach(processElement);
|
|
2170
|
+
observer.observe(document.body, {
|
|
2171
|
+
attributes: true,
|
|
2172
|
+
attributeFilter: ['controller'],
|
|
2173
|
+
childList: true,
|
|
2174
|
+
subtree: true
|
|
2175
|
+
});
|
|
2176
|
+
}
|
|
2177
|
+
// Store observer reference for cleanup if needed
|
|
2178
|
+
globalThis.sniceNativeControllerObserver = observer;
|
|
2179
|
+
}
|
|
2180
|
+
|
|
2181
|
+
/**
|
|
2182
|
+
* @context decorator for receiving router context updates
|
|
2183
|
+
*/
|
|
2184
|
+
const CONTEXT_HANDLERS = getSymbol('context-handlers');
|
|
2185
|
+
/**
|
|
2186
|
+
* @context decorator for receiving router context updates
|
|
2187
|
+
*
|
|
2188
|
+
* @example
|
|
2189
|
+
* ```typescript
|
|
2190
|
+
* @element('my-layout')
|
|
2191
|
+
* class MyLayout extends HTMLElement {
|
|
2192
|
+
* @context
|
|
2193
|
+
* handleContext(ctx: Context) {
|
|
2194
|
+
* this.renderNav(ctx.placards, ctx.currentRoute);
|
|
2195
|
+
* }
|
|
2196
|
+
*
|
|
2197
|
+
* @context({ debounce: 300 })
|
|
2198
|
+
* handleContextDebounced(ctx: Context) {
|
|
2199
|
+
* // Called after 300ms of no updates
|
|
2200
|
+
* }
|
|
2201
|
+
* }
|
|
2202
|
+
* ```
|
|
2203
|
+
*/
|
|
2204
|
+
function context$1(options = {}) {
|
|
2205
|
+
return function (originalMethod, context) {
|
|
2206
|
+
const methodName = context.name;
|
|
2207
|
+
const initKey = `__context_init_${methodName}`;
|
|
2208
|
+
context.addInitializer(function () {
|
|
2209
|
+
const constructor = this.constructor;
|
|
2210
|
+
if (constructor[initKey])
|
|
2211
|
+
return;
|
|
2212
|
+
constructor[initKey] = true;
|
|
2213
|
+
if (!constructor[CONTEXT_HANDLERS]) {
|
|
2214
|
+
constructor[CONTEXT_HANDLERS] = [];
|
|
2215
|
+
}
|
|
2216
|
+
constructor[CONTEXT_HANDLERS].push({
|
|
2217
|
+
methodName,
|
|
2218
|
+
method: originalMethod,
|
|
2219
|
+
options,
|
|
2220
|
+
});
|
|
2221
|
+
});
|
|
2222
|
+
return originalMethod;
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
/**
|
|
2226
|
+
* Setup context handler for an element instance
|
|
2227
|
+
* Called automatically during element connection
|
|
2228
|
+
*/
|
|
2229
|
+
function setupContextHandler(element) {
|
|
2230
|
+
const handlers = element.constructor[CONTEXT_HANDLERS];
|
|
2231
|
+
if (!handlers || !Array.isArray(handlers) || handlers.length === 0) {
|
|
2232
|
+
return;
|
|
2233
|
+
}
|
|
2234
|
+
// Get the Context instance from the router
|
|
2235
|
+
const ctx = element[CONTEXT_HANDLER];
|
|
2236
|
+
if (!ctx) {
|
|
2237
|
+
return;
|
|
2238
|
+
}
|
|
2239
|
+
// Store the Context instance for cleanup
|
|
2240
|
+
element[NAVIGATION_CONTEXT_INSTANCE] = ctx;
|
|
2241
|
+
// Register each handler with the Context
|
|
2242
|
+
for (const handler of handlers) {
|
|
2243
|
+
const { methodName, method, options } = handler;
|
|
2244
|
+
const wrappedMethodName = `__wrapped_${methodName}`;
|
|
2245
|
+
// Create wrapped method with timing controls
|
|
2246
|
+
element[wrappedMethodName] = function (context) {
|
|
2247
|
+
// Skip if already called once
|
|
2248
|
+
if (options.once && element[CONTEXT_CALLED]) {
|
|
2249
|
+
return;
|
|
2250
|
+
}
|
|
2251
|
+
const callMethod = () => {
|
|
2252
|
+
method.call(element, context);
|
|
2253
|
+
// Handle once option
|
|
2254
|
+
if (options.once) {
|
|
2255
|
+
element[CONTEXT_CALLED] = true;
|
|
2256
|
+
// Unregister after first call
|
|
2257
|
+
const ctx = element[NAVIGATION_CONTEXT_INSTANCE];
|
|
2258
|
+
if (ctx && typeof ctx[CONTEXT_UNREGISTER] === 'function') {
|
|
2259
|
+
ctx[CONTEXT_UNREGISTER](element);
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
};
|
|
2263
|
+
// Handle debounce
|
|
2264
|
+
if (options.debounce) {
|
|
2265
|
+
clearTimeout(element[CONTEXT_TIMER]);
|
|
2266
|
+
element[CONTEXT_TIMER] = setTimeout(callMethod, options.debounce);
|
|
2267
|
+
}
|
|
2268
|
+
// Handle throttle
|
|
2269
|
+
else if (options.throttle) {
|
|
2270
|
+
const now = Date.now();
|
|
2271
|
+
const lastCall = element[CONTEXT_TIMER] || 0;
|
|
2272
|
+
if (now - lastCall >= options.throttle) {
|
|
2273
|
+
element[CONTEXT_TIMER] = now;
|
|
2274
|
+
callMethod();
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
// No timing options - call immediately
|
|
2278
|
+
else {
|
|
2279
|
+
callMethod();
|
|
2280
|
+
}
|
|
2281
|
+
};
|
|
2282
|
+
// Register with the Context using the wrapped method name
|
|
2283
|
+
if (typeof ctx[CONTEXT_REGISTER] === 'function') {
|
|
2284
|
+
ctx[CONTEXT_REGISTER](element, wrappedMethodName);
|
|
2285
|
+
}
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
/**
|
|
2289
|
+
* Cleanup context handler for an element instance
|
|
2290
|
+
* Called automatically during element disconnection
|
|
2291
|
+
*/
|
|
2292
|
+
function cleanupContextHandler(element) {
|
|
2293
|
+
const handlers = element.constructor[CONTEXT_HANDLERS];
|
|
2294
|
+
if (!handlers || !Array.isArray(handlers) || handlers.length === 0) {
|
|
2295
|
+
return;
|
|
2296
|
+
}
|
|
2297
|
+
// Clear any pending debounce timer
|
|
2298
|
+
for (const handler of handlers) {
|
|
2299
|
+
if (handler.options.debounce && element[CONTEXT_TIMER]) {
|
|
2300
|
+
clearTimeout(element[CONTEXT_TIMER]);
|
|
2301
|
+
delete element[CONTEXT_TIMER];
|
|
2302
|
+
}
|
|
2303
|
+
// Clean up wrapped method
|
|
2304
|
+
const wrappedMethodName = `__wrapped_${handler.methodName}`;
|
|
2305
|
+
delete element[wrappedMethodName];
|
|
1215
2306
|
}
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
attributes: true,
|
|
1221
|
-
attributeFilter: ['controller'],
|
|
1222
|
-
childList: true,
|
|
1223
|
-
subtree: true
|
|
1224
|
-
});
|
|
2307
|
+
// Unregister from Context if available
|
|
2308
|
+
const ctx = element[NAVIGATION_CONTEXT_INSTANCE];
|
|
2309
|
+
if (ctx && typeof ctx[CONTEXT_UNREGISTER] === 'function') {
|
|
2310
|
+
ctx[CONTEXT_UNREGISTER](element);
|
|
1225
2311
|
}
|
|
1226
|
-
|
|
1227
|
-
|
|
2312
|
+
delete element[NAVIGATION_CONTEXT_INSTANCE];
|
|
2313
|
+
delete element[CONTEXT_CALLED];
|
|
1228
2314
|
}
|
|
1229
2315
|
|
|
1230
2316
|
/**
|
|
@@ -1285,6 +2371,87 @@ class SimpleArray {
|
|
|
1285
2371
|
}
|
|
1286
2372
|
}
|
|
1287
2373
|
|
|
2374
|
+
var _a, _b, _c;
|
|
2375
|
+
// Symbol for storing the Set of elements
|
|
2376
|
+
const REGISTERED_ELEMENTS_SET = Symbol('registered-elements-set');
|
|
2377
|
+
// Counter for generating unique context IDs
|
|
2378
|
+
let contextIdCounter = 0;
|
|
2379
|
+
/**
|
|
2380
|
+
* Represents the bundled router state that can notify registered elements of changes
|
|
2381
|
+
*/
|
|
2382
|
+
class Context {
|
|
2383
|
+
constructor(context = {}, placards = [], currentRoute = '', routeParams = {}) {
|
|
2384
|
+
this[_a] = new WeakMap();
|
|
2385
|
+
this[_b] = new Set();
|
|
2386
|
+
this[_c] = false;
|
|
2387
|
+
this.id = contextIdCounter++;
|
|
2388
|
+
this.application = context;
|
|
2389
|
+
this.navigation = {
|
|
2390
|
+
placards,
|
|
2391
|
+
route: currentRoute,
|
|
2392
|
+
params: routeParams
|
|
2393
|
+
};
|
|
2394
|
+
}
|
|
2395
|
+
/**
|
|
2396
|
+
* Register an element to receive context updates
|
|
2397
|
+
* @internal Used by @context decorator
|
|
2398
|
+
*/
|
|
2399
|
+
[(_a = REGISTERED_ELEMENTS, _b = REGISTERED_ELEMENTS_SET, _c = IS_UPDATING, CONTEXT_REGISTER)](element, methodName) {
|
|
2400
|
+
this[REGISTERED_ELEMENTS].set(element, methodName);
|
|
2401
|
+
this[REGISTERED_ELEMENTS_SET].add(element);
|
|
2402
|
+
}
|
|
2403
|
+
/**
|
|
2404
|
+
* Unregister an element from receiving context updates
|
|
2405
|
+
* @internal Used by @context decorator cleanup
|
|
2406
|
+
*/
|
|
2407
|
+
[CONTEXT_UNREGISTER](element) {
|
|
2408
|
+
this[REGISTERED_ELEMENTS].delete(element);
|
|
2409
|
+
this[REGISTERED_ELEMENTS_SET].delete(element);
|
|
2410
|
+
}
|
|
2411
|
+
/**
|
|
2412
|
+
* Update the context and notify all registered elements
|
|
2413
|
+
* Prevents infinite loops by tracking update state
|
|
2414
|
+
*/
|
|
2415
|
+
update(context, placards, currentRoute, routeParams) {
|
|
2416
|
+
// Prevent infinite loops
|
|
2417
|
+
if (this[IS_UPDATING]) {
|
|
2418
|
+
return;
|
|
2419
|
+
}
|
|
2420
|
+
this[IS_UPDATING] = true;
|
|
2421
|
+
// Update properties (id remains immutable)
|
|
2422
|
+
this.application = context;
|
|
2423
|
+
this.navigation.placards = placards;
|
|
2424
|
+
this.navigation.route = currentRoute;
|
|
2425
|
+
this.navigation.params = routeParams;
|
|
2426
|
+
// Notify all registered elements by calling their methods directly
|
|
2427
|
+
const elementsSet = this[REGISTERED_ELEMENTS_SET];
|
|
2428
|
+
const elementsMap = this[REGISTERED_ELEMENTS];
|
|
2429
|
+
for (const element of elementsSet) {
|
|
2430
|
+
const methodName = elementsMap.get(element);
|
|
2431
|
+
if (methodName && typeof element[methodName] === 'function') {
|
|
2432
|
+
try {
|
|
2433
|
+
element[methodName](this);
|
|
2434
|
+
}
|
|
2435
|
+
catch (error) {
|
|
2436
|
+
// Log error but continue notifying other elements
|
|
2437
|
+
console.error(`Error calling @context method ${methodName}:`, error);
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
}
|
|
2441
|
+
this[IS_UPDATING] = false;
|
|
2442
|
+
}
|
|
2443
|
+
/**
|
|
2444
|
+
* Notify a specific element of the current context state
|
|
2445
|
+
* @internal Used by @context decorator
|
|
2446
|
+
*/
|
|
2447
|
+
[CONTEXT_NOTIFY_ELEMENT](element) {
|
|
2448
|
+
const methodName = this[REGISTERED_ELEMENTS].get(element);
|
|
2449
|
+
if (methodName && typeof element[methodName] === 'function') {
|
|
2450
|
+
element[methodName](this);
|
|
2451
|
+
}
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
|
|
1288
2455
|
/**
|
|
1289
2456
|
* Detects the type constructor from an initial value
|
|
1290
2457
|
* @param initialValue - The default value assigned to a property field (e.g., `name = "default"` -> "default")
|
|
@@ -1413,6 +2580,297 @@ function valueToAttribute(value, propertyOptions, initialValue) {
|
|
|
1413
2580
|
}
|
|
1414
2581
|
}
|
|
1415
2582
|
|
|
2583
|
+
/**
|
|
2584
|
+
* @render and @styles decorators for Snice v3.0.0
|
|
2585
|
+
* Provides automatic differential rendering on property changes
|
|
2586
|
+
*/
|
|
2587
|
+
/**
|
|
2588
|
+
* Global render scheduler for microtask batching
|
|
2589
|
+
* Batches multiple property changes into a single render
|
|
2590
|
+
*/
|
|
2591
|
+
class RenderScheduler {
|
|
2592
|
+
constructor() {
|
|
2593
|
+
this.pending = new Set();
|
|
2594
|
+
this.scheduled = false;
|
|
2595
|
+
}
|
|
2596
|
+
/**
|
|
2597
|
+
* Schedule an element for rendering
|
|
2598
|
+
* Batches renders in a microtask unless sync option is enabled
|
|
2599
|
+
*/
|
|
2600
|
+
schedule(element, options) {
|
|
2601
|
+
// Sync rendering - execute immediately
|
|
2602
|
+
if (options.sync) {
|
|
2603
|
+
performRender(element, options);
|
|
2604
|
+
return;
|
|
2605
|
+
}
|
|
2606
|
+
// Async rendering - batch in microtask
|
|
2607
|
+
this.pending.add(element);
|
|
2608
|
+
if (!this.scheduled) {
|
|
2609
|
+
this.scheduled = true;
|
|
2610
|
+
queueMicrotask(() => this.flush());
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
/**
|
|
2614
|
+
* Flush all pending renders
|
|
2615
|
+
*/
|
|
2616
|
+
flush() {
|
|
2617
|
+
const elements = Array.from(this.pending);
|
|
2618
|
+
this.pending.clear();
|
|
2619
|
+
this.scheduled = false;
|
|
2620
|
+
for (const element of elements) {
|
|
2621
|
+
const options = element[RENDER_OPTIONS] || {};
|
|
2622
|
+
performRender(element, options);
|
|
2623
|
+
}
|
|
2624
|
+
}
|
|
2625
|
+
}
|
|
2626
|
+
const renderScheduler = new RenderScheduler();
|
|
2627
|
+
/**
|
|
2628
|
+
* Perform the actual render of an element
|
|
2629
|
+
*/
|
|
2630
|
+
function performRender(element, options, precomputedResult) {
|
|
2631
|
+
const renderMethod = element[RENDER_METHOD];
|
|
2632
|
+
if (!renderMethod)
|
|
2633
|
+
return;
|
|
2634
|
+
// If once is true and we've already rendered, skip
|
|
2635
|
+
if (options.once && element[RENDER_INSTANCE]) {
|
|
2636
|
+
return;
|
|
2637
|
+
}
|
|
2638
|
+
try {
|
|
2639
|
+
// Use precomputed result if provided, otherwise call the render method
|
|
2640
|
+
const result = precomputedResult !== undefined ? precomputedResult : renderMethod.call(element);
|
|
2641
|
+
// Check if differential rendering is disabled (expects string)
|
|
2642
|
+
const differential = options.differential !== false;
|
|
2643
|
+
if (!differential) {
|
|
2644
|
+
// Non-differential expects string
|
|
2645
|
+
if (typeof result !== 'string') {
|
|
2646
|
+
console.warn('Render method with differential: false must return a string');
|
|
2647
|
+
return;
|
|
2648
|
+
}
|
|
2649
|
+
// Simple string rendering
|
|
2650
|
+
if (!element.shadowRoot) {
|
|
2651
|
+
element.attachShadow({ mode: 'open' });
|
|
2652
|
+
}
|
|
2653
|
+
element.shadowRoot.innerHTML = result;
|
|
2654
|
+
// Mark scheduled flag as false
|
|
2655
|
+
element[RENDER_SCHEDULED] = false;
|
|
2656
|
+
// Call all registered render callbacks
|
|
2657
|
+
const callbacks = element[RENDER_CALLBACKS];
|
|
2658
|
+
if (callbacks && callbacks.length > 0) {
|
|
2659
|
+
const cbs = [...callbacks];
|
|
2660
|
+
element[RENDER_CALLBACKS] = [];
|
|
2661
|
+
cbs.forEach(cb => cb());
|
|
2662
|
+
}
|
|
2663
|
+
return;
|
|
2664
|
+
}
|
|
2665
|
+
if (!isTemplateResult(result)) {
|
|
2666
|
+
console.warn('Render method must return html`` template result');
|
|
2667
|
+
return;
|
|
2668
|
+
}
|
|
2669
|
+
// Get or create template instance (differential rendering)
|
|
2670
|
+
let instance = element[RENDER_INSTANCE];
|
|
2671
|
+
if (!instance) {
|
|
2672
|
+
// First render - create shadow root if needed and initial instance
|
|
2673
|
+
if (!element.shadowRoot) {
|
|
2674
|
+
element.attachShadow({ mode: 'open' });
|
|
2675
|
+
}
|
|
2676
|
+
instance = new TemplateInstance(result);
|
|
2677
|
+
element[RENDER_INSTANCE] = instance;
|
|
2678
|
+
// Create the fragment but don't commit values yet
|
|
2679
|
+
const fragment = instance.renderFragment();
|
|
2680
|
+
// Append to shadow root first so getRootNode() works in event handlers
|
|
2681
|
+
element.shadowRoot.appendChild(fragment);
|
|
2682
|
+
// Now commit values (this binds event handlers with correct host)
|
|
2683
|
+
instance.update(result.values);
|
|
2684
|
+
}
|
|
2685
|
+
else {
|
|
2686
|
+
// Subsequent render - just update the parts (differential rendering!)
|
|
2687
|
+
instance.update(result.values);
|
|
2688
|
+
}
|
|
2689
|
+
// Mark scheduled flag as false
|
|
2690
|
+
element[RENDER_SCHEDULED] = false;
|
|
2691
|
+
// Call all registered render callbacks (for testing/debugging)
|
|
2692
|
+
const callbacks = element[RENDER_CALLBACKS];
|
|
2693
|
+
if (callbacks && callbacks.length > 0) {
|
|
2694
|
+
const cbs = [...callbacks];
|
|
2695
|
+
element[RENDER_CALLBACKS] = [];
|
|
2696
|
+
cbs.forEach(cb => cb());
|
|
2697
|
+
}
|
|
2698
|
+
}
|
|
2699
|
+
catch (error) {
|
|
2700
|
+
console.error('Error rendering element:', error);
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
/**
|
|
2704
|
+
* Request a render for an element
|
|
2705
|
+
* Respects debounce/throttle/once/sync options
|
|
2706
|
+
* @param immediate - Force immediate render (used for initial render)
|
|
2707
|
+
*/
|
|
2708
|
+
function requestRender(element, immediate = false) {
|
|
2709
|
+
const options = element[RENDER_OPTIONS] || {};
|
|
2710
|
+
// Handle once option
|
|
2711
|
+
if (options.once && element[RENDER_INSTANCE]) {
|
|
2712
|
+
return;
|
|
2713
|
+
}
|
|
2714
|
+
// Force immediate render (for initial render)
|
|
2715
|
+
if (immediate) {
|
|
2716
|
+
performRender(element, options);
|
|
2717
|
+
return;
|
|
2718
|
+
}
|
|
2719
|
+
// Handle debounce
|
|
2720
|
+
if (options.debounce !== undefined && options.debounce > 0) {
|
|
2721
|
+
if (!element[RENDER_TIMERS]) {
|
|
2722
|
+
element[RENDER_TIMERS] = {};
|
|
2723
|
+
}
|
|
2724
|
+
clearTimeout(element[RENDER_TIMERS].debounce);
|
|
2725
|
+
element[RENDER_TIMERS].debounce = setTimeout(() => {
|
|
2726
|
+
renderScheduler.schedule(element, options);
|
|
2727
|
+
}, options.debounce);
|
|
2728
|
+
return;
|
|
2729
|
+
}
|
|
2730
|
+
// Handle throttle
|
|
2731
|
+
if (options.throttle !== undefined && options.throttle > 0) {
|
|
2732
|
+
if (!element[RENDER_TIMERS]) {
|
|
2733
|
+
element[RENDER_TIMERS] = { lastThrottle: 0 };
|
|
2734
|
+
}
|
|
2735
|
+
const timers = element[RENDER_TIMERS];
|
|
2736
|
+
const now = Date.now();
|
|
2737
|
+
if (timers.lastThrottle === 0 || now - timers.lastThrottle >= options.throttle) {
|
|
2738
|
+
timers.lastThrottle = now;
|
|
2739
|
+
renderScheduler.schedule(element, options);
|
|
2740
|
+
}
|
|
2741
|
+
else {
|
|
2742
|
+
// Schedule for later if not already scheduled
|
|
2743
|
+
if (!timers.throttleTimer) {
|
|
2744
|
+
const remaining = options.throttle - (now - timers.lastThrottle);
|
|
2745
|
+
timers.throttleTimer = setTimeout(() => {
|
|
2746
|
+
timers.throttleTimer = null;
|
|
2747
|
+
timers.lastThrottle = Date.now();
|
|
2748
|
+
renderScheduler.schedule(element, options);
|
|
2749
|
+
}, remaining);
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
return;
|
|
2753
|
+
}
|
|
2754
|
+
// Normal rendering (with microtask batching unless sync)
|
|
2755
|
+
renderScheduler.schedule(element, options);
|
|
2756
|
+
}
|
|
2757
|
+
/**
|
|
2758
|
+
* @render decorator for component rendering
|
|
2759
|
+
*
|
|
2760
|
+
* Marks a method as the render method for the component.
|
|
2761
|
+
* The method should return html`...` template.
|
|
2762
|
+
* Automatically re-renders when properties change (unless once: true).
|
|
2763
|
+
*
|
|
2764
|
+
* @example
|
|
2765
|
+
* ```typescript
|
|
2766
|
+
* @render()
|
|
2767
|
+
* renderContent() {
|
|
2768
|
+
* return html`<div>${this.count}</div>`;
|
|
2769
|
+
* }
|
|
2770
|
+
* ```
|
|
2771
|
+
*
|
|
2772
|
+
* @example
|
|
2773
|
+
* ```typescript
|
|
2774
|
+
* // Debounced rendering
|
|
2775
|
+
* @render({ debounce: 100 })
|
|
2776
|
+
* renderContent() {
|
|
2777
|
+
* return html`<div>${this.searchTerm}</div>`;
|
|
2778
|
+
* }
|
|
2779
|
+
* ```
|
|
2780
|
+
*
|
|
2781
|
+
* @example
|
|
2782
|
+
* ```typescript
|
|
2783
|
+
* // Render only once (manual re-renders only)
|
|
2784
|
+
* @render({ once: true })
|
|
2785
|
+
* renderContent() {
|
|
2786
|
+
* return html`<div>Static content</div>`;
|
|
2787
|
+
* }
|
|
2788
|
+
* ```
|
|
2789
|
+
*/
|
|
2790
|
+
function render(options = {}) {
|
|
2791
|
+
return function (originalMethod, context) {
|
|
2792
|
+
context.name;
|
|
2793
|
+
context.addInitializer(function () {
|
|
2794
|
+
// Store the render method and options
|
|
2795
|
+
this[RENDER_METHOD] = originalMethod;
|
|
2796
|
+
this[RENDER_OPTIONS] = options;
|
|
2797
|
+
});
|
|
2798
|
+
// Return wrapped method that triggers re-render when called manually
|
|
2799
|
+
return function (...args) {
|
|
2800
|
+
// Call original method to get the template
|
|
2801
|
+
const result = originalMethod.apply(this, args);
|
|
2802
|
+
// Always render when method is called manually (even if once: true)
|
|
2803
|
+
// Force immediate render to bypass all options, pass precomputed result to avoid calling method twice
|
|
2804
|
+
performRender(this, {}, result);
|
|
2805
|
+
return result;
|
|
2806
|
+
};
|
|
2807
|
+
};
|
|
2808
|
+
}
|
|
2809
|
+
/**
|
|
2810
|
+
* @styles decorator for component styles
|
|
2811
|
+
*
|
|
2812
|
+
* Marks a method as the styles method for the component.
|
|
2813
|
+
* The method should return css`...` template.
|
|
2814
|
+
* Styles are applied once when the component is connected.
|
|
2815
|
+
*
|
|
2816
|
+
* @example
|
|
2817
|
+
* ```typescript
|
|
2818
|
+
* @styles()
|
|
2819
|
+
* styles() {
|
|
2820
|
+
* return css`:host { display: block; }`;
|
|
2821
|
+
* }
|
|
2822
|
+
* ```
|
|
2823
|
+
*/
|
|
2824
|
+
function styles() {
|
|
2825
|
+
return function (originalMethod, context) {
|
|
2826
|
+
context.addInitializer(function () {
|
|
2827
|
+
// Store the styles method
|
|
2828
|
+
this[STYLES_METHOD] = originalMethod;
|
|
2829
|
+
});
|
|
2830
|
+
return originalMethod;
|
|
2831
|
+
};
|
|
2832
|
+
}
|
|
2833
|
+
/**
|
|
2834
|
+
* Apply styles to an element
|
|
2835
|
+
* Called during element initialization
|
|
2836
|
+
*/
|
|
2837
|
+
function applyStyles(element) {
|
|
2838
|
+
const stylesMethod = element[STYLES_METHOD];
|
|
2839
|
+
if (!stylesMethod)
|
|
2840
|
+
return;
|
|
2841
|
+
// Only apply once
|
|
2842
|
+
if (element[STYLES_APPLIED])
|
|
2843
|
+
return;
|
|
2844
|
+
element[STYLES_APPLIED] = true;
|
|
2845
|
+
try {
|
|
2846
|
+
const result = stylesMethod.call(element);
|
|
2847
|
+
if (!isCSSResult(result)) {
|
|
2848
|
+
console.warn('Styles method must return css`` template result');
|
|
2849
|
+
return;
|
|
2850
|
+
}
|
|
2851
|
+
// Ensure shadow root exists
|
|
2852
|
+
if (!element.shadowRoot) {
|
|
2853
|
+
element.attachShadow({ mode: 'open' });
|
|
2854
|
+
}
|
|
2855
|
+
// Create base styles for meta elements (if, case)
|
|
2856
|
+
const baseStyleSheet = new CSSStyleSheet();
|
|
2857
|
+
baseStyleSheet.replaceSync('if, case { display: contents; }');
|
|
2858
|
+
// Try to use constructable stylesheets for better performance
|
|
2859
|
+
if (element.shadowRoot && result.styleSheet && 'adoptedStyleSheets' in element.shadowRoot) {
|
|
2860
|
+
element.shadowRoot.adoptedStyleSheets = [baseStyleSheet, result.styleSheet];
|
|
2861
|
+
}
|
|
2862
|
+
else if (element.shadowRoot) {
|
|
2863
|
+
// Fallback to <style> tag
|
|
2864
|
+
const style = document.createElement('style');
|
|
2865
|
+
style.textContent = 'if, case { display: contents; }\n' + result.cssText;
|
|
2866
|
+
element.shadowRoot.appendChild(style);
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
catch (error) {
|
|
2870
|
+
console.error('Error applying styles:', error);
|
|
2871
|
+
}
|
|
2872
|
+
}
|
|
2873
|
+
|
|
1416
2874
|
/**
|
|
1417
2875
|
* Applies core element functionality to a constructor
|
|
1418
2876
|
* This is shared between @element and @page decorators
|
|
@@ -1457,6 +2915,9 @@ function applyElementFunctionality(constructor) {
|
|
|
1457
2915
|
enumerable: true,
|
|
1458
2916
|
configurable: true
|
|
1459
2917
|
});
|
|
2918
|
+
// Note: rendered promise is stored via symbols RENDERED_PROMISE and RENDERED_RESOLVE
|
|
2919
|
+
// It's not exposed as a public property - only accessible via test utilities
|
|
2920
|
+
// This prevents accidental misuse in production code
|
|
1460
2921
|
// Add controller property
|
|
1461
2922
|
Object.defineProperty(constructor.prototype, 'controller', {
|
|
1462
2923
|
get() {
|
|
@@ -1494,6 +2955,7 @@ function applyElementFunctionality(constructor) {
|
|
|
1494
2955
|
// Re-establish handlers that get cleaned up on disconnect
|
|
1495
2956
|
setupEventHandlers(this, this);
|
|
1496
2957
|
setupResponseHandlers(this, this);
|
|
2958
|
+
setupContextHandler(this);
|
|
1497
2959
|
// Re-establish observers that get cleaned up on disconnect
|
|
1498
2960
|
try {
|
|
1499
2961
|
setupObservers(this, this);
|
|
@@ -1508,6 +2970,9 @@ function applyElementFunctionality(constructor) {
|
|
|
1508
2970
|
return;
|
|
1509
2971
|
}
|
|
1510
2972
|
try {
|
|
2973
|
+
// Mark that properties are being initialized from attributes
|
|
2974
|
+
// This allows property setters to work during initialization
|
|
2975
|
+
this[PROPERTIES_INITIALIZED] = true;
|
|
1511
2976
|
// Initialize properties from attributes before rendering
|
|
1512
2977
|
const properties = constructor[PROPERTIES];
|
|
1513
2978
|
if (properties) {
|
|
@@ -1526,82 +2991,69 @@ function applyElementFunctionality(constructor) {
|
|
|
1526
2991
|
}
|
|
1527
2992
|
}
|
|
1528
2993
|
}
|
|
1529
|
-
//
|
|
1530
|
-
|
|
2994
|
+
// Apply any properties that were set before element was connected
|
|
2995
|
+
// BUT only if they don't have an HTML attribute (don't override HTML attributes)
|
|
2996
|
+
if (this[PRE_INIT_PROPERTY_VALUES]) {
|
|
2997
|
+
for (const [propName, propValue] of this[PRE_INIT_PROPERTY_VALUES]) {
|
|
2998
|
+
// Remove from map first so getter doesn't return it during setter call
|
|
2999
|
+
this[PRE_INIT_PROPERTY_VALUES].delete(propName);
|
|
3000
|
+
// Only apply pre-init value if NO HTML attribute exists
|
|
3001
|
+
// This prevents field initializers from overwriting HTML attributes
|
|
3002
|
+
const propOptions = properties?.get(propName);
|
|
3003
|
+
const attributeName = typeof propOptions?.attribute === 'string' ? propOptions.attribute : propName.toLowerCase();
|
|
3004
|
+
if (!this.hasAttribute(attributeName)) {
|
|
3005
|
+
this[propName] = propValue;
|
|
3006
|
+
}
|
|
3007
|
+
}
|
|
3008
|
+
// Clear the pre-init values map
|
|
3009
|
+
delete this[PRE_INIT_PROPERTY_VALUES];
|
|
3010
|
+
}
|
|
1531
3011
|
// Properties are now stateless and read from DOM attributes only
|
|
1532
3012
|
// Initial values are not automatically reflected
|
|
1533
|
-
//
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
//
|
|
1538
|
-
|
|
1539
|
-
//
|
|
1540
|
-
if (this
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
//
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
shadowContent += htmlContent;
|
|
3013
|
+
// v3.0.0: Apply @styles decorator if present
|
|
3014
|
+
// This creates the shadow root and applies styles
|
|
3015
|
+
applyStyles(this);
|
|
3016
|
+
// v3.0.0: Perform initial @render if present
|
|
3017
|
+
// This uses differential rendering with template system
|
|
3018
|
+
// Defer initial render to next microtask to allow property bindings
|
|
3019
|
+
// from parent to be set first (avoids infinite loops in nested elements)
|
|
3020
|
+
if (this[RENDER_METHOD]) {
|
|
3021
|
+
queueMicrotask(() => {
|
|
3022
|
+
requestRender(this, true);
|
|
3023
|
+
// Setup observers after first render completes so shadow DOM content exists
|
|
3024
|
+
try {
|
|
3025
|
+
setupObservers(this, this);
|
|
1547
3026
|
}
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
3027
|
+
catch (error) {
|
|
3028
|
+
console.error(`Error setting up observers for ${this.tagName}:`, error);
|
|
3029
|
+
}
|
|
3030
|
+
// Mark element as ready after initial render completes
|
|
3031
|
+
if (this[READY_RESOLVE]) {
|
|
3032
|
+
this[READY_RESOLVE]();
|
|
3033
|
+
this[READY_RESOLVE] = null;
|
|
3034
|
+
}
|
|
3035
|
+
});
|
|
1552
3036
|
}
|
|
1553
|
-
|
|
1554
|
-
|
|
3037
|
+
else {
|
|
3038
|
+
// No render method, setup observers immediately
|
|
1555
3039
|
try {
|
|
1556
|
-
|
|
1557
|
-
// Handle both async and sync css
|
|
1558
|
-
const cssResolved = cssResult instanceof Promise ? await cssResult : cssResult;
|
|
1559
|
-
if (cssResolved) {
|
|
1560
|
-
// Handle both string and array of strings
|
|
1561
|
-
const cssContent = Array.isArray(cssResolved) ? cssResolved.join('\n') : cssResolved;
|
|
1562
|
-
// No need for scoping with Shadow DOM, but add data attribute for compatibility
|
|
1563
|
-
shadowContent += `<style data-component-css>${cssContent}</style>`;
|
|
1564
|
-
}
|
|
3040
|
+
setupObservers(this, this);
|
|
1565
3041
|
}
|
|
1566
3042
|
catch (error) {
|
|
1567
|
-
console.error(`Error
|
|
3043
|
+
console.error(`Error setting up observers for ${this.tagName}:`, error);
|
|
1568
3044
|
}
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
}
|
|
1574
|
-
// Render all @part methods into their corresponding elements
|
|
1575
|
-
const parts = constructor[PARTS];
|
|
1576
|
-
if (parts && this.shadowRoot) {
|
|
1577
|
-
for (const [partName, partHandler] of parts) {
|
|
1578
|
-
try {
|
|
1579
|
-
const partElement = this.shadowRoot.querySelector(`[part="${partName}"]`);
|
|
1580
|
-
if (partElement) {
|
|
1581
|
-
// For initial render, call original method directly to avoid timing restrictions
|
|
1582
|
-
const partResult = partHandler.method.call(this);
|
|
1583
|
-
const partContent = partResult instanceof Promise ? await partResult : partResult;
|
|
1584
|
-
if (partContent !== undefined) {
|
|
1585
|
-
partElement.innerHTML = partContent;
|
|
1586
|
-
}
|
|
1587
|
-
}
|
|
1588
|
-
}
|
|
1589
|
-
catch (error) {
|
|
1590
|
-
console.error(`Error rendering @part('${partName}') in ${this.tagName}:`, error);
|
|
1591
|
-
}
|
|
3045
|
+
// Mark element as ready immediately if no render method
|
|
3046
|
+
if (this[READY_RESOLVE]) {
|
|
3047
|
+
this[READY_RESOLVE]();
|
|
3048
|
+
this[READY_RESOLVE] = null;
|
|
1592
3049
|
}
|
|
1593
3050
|
}
|
|
1594
|
-
// Setup @on event handlers
|
|
3051
|
+
// Setup @on event handlers (v2.5.4 compatibility restored!)
|
|
1595
3052
|
setupEventHandlers(this, this);
|
|
1596
3053
|
// Setup @respond handlers for elements
|
|
1597
3054
|
setupResponseHandlers(this, this);
|
|
1598
|
-
// Setup @
|
|
1599
|
-
|
|
1600
|
-
setupObservers(this, this);
|
|
1601
|
-
}
|
|
1602
|
-
catch (error) {
|
|
1603
|
-
console.error(`Error setting up observers for ${this.tagName}:`, error);
|
|
1604
|
-
}
|
|
3055
|
+
// Setup @context handler for elements
|
|
3056
|
+
setupContextHandler(this);
|
|
1605
3057
|
// Mark as initialized
|
|
1606
3058
|
this[INITIALIZED] = true;
|
|
1607
3059
|
// NOW call the original user-defined connectedCallback after shadow DOM is set up
|
|
@@ -1610,11 +3062,8 @@ function applyElementFunctionality(constructor) {
|
|
|
1610
3062
|
}
|
|
1611
3063
|
}
|
|
1612
3064
|
finally {
|
|
1613
|
-
//
|
|
1614
|
-
|
|
1615
|
-
this[READY_RESOLVE]();
|
|
1616
|
-
this[READY_RESOLVE] = null; // Clear the resolver
|
|
1617
|
-
}
|
|
3065
|
+
// Ready is now resolved inside the render microtask (or immediately if no render)
|
|
3066
|
+
// This ensures ready waits for initial render to complete
|
|
1618
3067
|
}
|
|
1619
3068
|
// Call @ready handlers after everything is set up and ready promise is resolved
|
|
1620
3069
|
const readyHandlers = constructor[READY_HANDLERS];
|
|
@@ -1651,10 +3100,12 @@ function applyElementFunctionality(constructor) {
|
|
|
1651
3100
|
console.error(`Failed to detach controller:`, error);
|
|
1652
3101
|
});
|
|
1653
3102
|
}
|
|
1654
|
-
// Cleanup @on event handlers
|
|
3103
|
+
// Cleanup @on event handlers (v2.5.4 compatibility restored!)
|
|
1655
3104
|
cleanupEventHandlers(this);
|
|
1656
3105
|
// Cleanup @respond handlers
|
|
1657
3106
|
cleanupResponseHandlers(this);
|
|
3107
|
+
// Cleanup @context handler
|
|
3108
|
+
cleanupContextHandler(this);
|
|
1658
3109
|
// Cleanup @observe observers
|
|
1659
3110
|
cleanupObservers(this);
|
|
1660
3111
|
};
|
|
@@ -1717,6 +3168,10 @@ function applyElementFunctionality(constructor) {
|
|
|
1717
3168
|
}
|
|
1718
3169
|
}
|
|
1719
3170
|
}
|
|
3171
|
+
// Trigger auto-render on attribute change (same as property setter)
|
|
3172
|
+
if (this[RENDER_METHOD] && this[INITIALIZED]) {
|
|
3173
|
+
requestRender(this);
|
|
3174
|
+
}
|
|
1720
3175
|
}
|
|
1721
3176
|
}
|
|
1722
3177
|
break;
|
|
@@ -1756,7 +3211,7 @@ function applyElementFunctionality(constructor) {
|
|
|
1756
3211
|
}
|
|
1757
3212
|
};
|
|
1758
3213
|
}
|
|
1759
|
-
function element(tagName) {
|
|
3214
|
+
function element(tagName, options) {
|
|
1760
3215
|
return function (constructor, context) {
|
|
1761
3216
|
// Transfer metadata from context to constructor
|
|
1762
3217
|
if (context.metadata && context.metadata[PROPERTIES]) {
|
|
@@ -1767,6 +3222,11 @@ function element(tagName) {
|
|
|
1767
3222
|
constructor[PROPERTIES].set(key, value);
|
|
1768
3223
|
}
|
|
1769
3224
|
}
|
|
3225
|
+
// Set up form association if requested via options
|
|
3226
|
+
// MUST be done BEFORE applyElementFunctionality and customElements.define
|
|
3227
|
+
if (options?.formAssociated === true) {
|
|
3228
|
+
constructor.formAssociated = true;
|
|
3229
|
+
}
|
|
1770
3230
|
applyElementFunctionality(constructor);
|
|
1771
3231
|
customElements.define(tagName, constructor);
|
|
1772
3232
|
return constructor;
|
|
@@ -1822,29 +3282,43 @@ function property(options) {
|
|
|
1822
3282
|
if (attrValue !== null) {
|
|
1823
3283
|
return parseAttributeValue(attrValue, finalOptions || {}, undefined, initialValue);
|
|
1824
3284
|
}
|
|
1825
|
-
// For Boolean properties that have been explicitly set via attribute,
|
|
3285
|
+
// For Boolean properties that have been explicitly set via attribute (and then removed),
|
|
1826
3286
|
// follow HTML boolean attribute semantics (absence = false)
|
|
1827
3287
|
const inferredType = finalOptions?.type || detectType(initialValue);
|
|
1828
3288
|
if (inferredType === Boolean && this[EXPLICITLY_SET_PROPERTIES]?.has(propertyKey)) {
|
|
1829
3289
|
return false;
|
|
1830
3290
|
}
|
|
1831
|
-
//
|
|
3291
|
+
// Check for pre-init property values (set before element was connected)
|
|
3292
|
+
if (this[PRE_INIT_PROPERTY_VALUES]?.has(propertyKey)) {
|
|
3293
|
+
return this[PRE_INIT_PROPERTY_VALUES].get(propertyKey);
|
|
3294
|
+
}
|
|
3295
|
+
// Otherwise return initial value (respects default values like showRememberMe = true)
|
|
1832
3296
|
return initialValue;
|
|
1833
3297
|
},
|
|
1834
3298
|
set(newValue) {
|
|
1835
|
-
// Get old value
|
|
1836
|
-
|
|
1837
|
-
this[PROPERTY_VALUES] = {};
|
|
1838
|
-
}
|
|
1839
|
-
const oldValue = this[PROPERTY_VALUES][propertyKey];
|
|
3299
|
+
// Get old value by calling the getter (which reads from attribute)
|
|
3300
|
+
const oldValue = this[propertyKey];
|
|
1840
3301
|
// Check if value actually changed
|
|
1841
3302
|
if (oldValue === newValue)
|
|
1842
3303
|
return;
|
|
1843
|
-
//
|
|
1844
|
-
|
|
1845
|
-
|
|
3304
|
+
// Don't reflect to DOM until connectedCallback has started
|
|
3305
|
+
// This prevents field initializers from overwriting HTML attributes
|
|
3306
|
+
if (!this[PROPERTIES_INITIALIZED]) {
|
|
3307
|
+
// Store value for later application when element is connected
|
|
3308
|
+
if (!this[PRE_INIT_PROPERTY_VALUES]) {
|
|
3309
|
+
this[PRE_INIT_PROPERTY_VALUES] = new Map();
|
|
3310
|
+
}
|
|
3311
|
+
this[PRE_INIT_PROPERTY_VALUES].set(propertyKey, newValue);
|
|
3312
|
+
return;
|
|
3313
|
+
}
|
|
3314
|
+
// Reflect to DOM - properties are backed by attributes
|
|
1846
3315
|
const attributeName = typeof finalOptions.attribute === 'string' ? finalOptions.attribute : propertyKey.toLowerCase();
|
|
1847
3316
|
const attributeValue = valueToAttribute(newValue, finalOptions, initialValue);
|
|
3317
|
+
// Mark as explicitly set for boolean handling
|
|
3318
|
+
if (!this[EXPLICITLY_SET_PROPERTIES]) {
|
|
3319
|
+
this[EXPLICITLY_SET_PROPERTIES] = new Set();
|
|
3320
|
+
}
|
|
3321
|
+
this[EXPLICITLY_SET_PROPERTIES].add(propertyKey);
|
|
1848
3322
|
// Flag to prevent attributeChangedCallback from triggering watchers for this change
|
|
1849
3323
|
if (!this._settingFromProperty)
|
|
1850
3324
|
this._settingFromProperty = new Set();
|
|
@@ -1886,8 +3360,12 @@ function property(options) {
|
|
|
1886
3360
|
}
|
|
1887
3361
|
}
|
|
1888
3362
|
}
|
|
1889
|
-
|
|
1890
|
-
|
|
3363
|
+
// v3.0.0: Trigger auto-render on property change
|
|
3364
|
+
// This respects @render options (debounce, throttle, once, sync)
|
|
3365
|
+
// Only trigger renders after element is fully initialized to avoid
|
|
3366
|
+
// infinite loops during initial setup
|
|
3367
|
+
if (this[RENDER_METHOD] && this[INITIALIZED]) {
|
|
3368
|
+
requestRender(this);
|
|
1891
3369
|
}
|
|
1892
3370
|
},
|
|
1893
3371
|
configurable: true,
|
|
@@ -1982,12 +3460,15 @@ function queryAll(selector, options = {}) {
|
|
|
1982
3460
|
function watch(...propertyNames) {
|
|
1983
3461
|
return function (target, context) {
|
|
1984
3462
|
const methodName = context.name;
|
|
3463
|
+
const initKey = `__watch_init_${methodName}`;
|
|
1985
3464
|
context.addInitializer(function () {
|
|
1986
3465
|
const constructor = this.constructor;
|
|
3466
|
+
if (constructor[initKey])
|
|
3467
|
+
return;
|
|
3468
|
+
constructor[initKey] = true;
|
|
1987
3469
|
if (!constructor[PROPERTY_WATCHERS]) {
|
|
1988
3470
|
constructor[PROPERTY_WATCHERS] = new Map();
|
|
1989
3471
|
}
|
|
1990
|
-
// Store the watcher method for each property
|
|
1991
3472
|
for (const propertyName of propertyNames) {
|
|
1992
3473
|
if (!constructor[PROPERTY_WATCHERS].has(propertyName)) {
|
|
1993
3474
|
constructor[PROPERTY_WATCHERS].set(propertyName, []);
|
|
@@ -2066,8 +3547,12 @@ function context() {
|
|
|
2066
3547
|
function ready() {
|
|
2067
3548
|
return function (target, context) {
|
|
2068
3549
|
const methodName = context.name;
|
|
3550
|
+
const initKey = `__ready_init_${methodName}`;
|
|
2069
3551
|
context.addInitializer(function () {
|
|
2070
3552
|
const constructor = this.constructor;
|
|
3553
|
+
if (constructor[initKey])
|
|
3554
|
+
return;
|
|
3555
|
+
constructor[initKey] = true;
|
|
2071
3556
|
if (!constructor[READY_HANDLERS]) {
|
|
2072
3557
|
constructor[READY_HANDLERS] = [];
|
|
2073
3558
|
}
|
|
@@ -2085,8 +3570,12 @@ function ready() {
|
|
|
2085
3570
|
function dispose() {
|
|
2086
3571
|
return function (target, context) {
|
|
2087
3572
|
const methodName = context.name;
|
|
3573
|
+
const initKey = `__dispose_init_${methodName}`;
|
|
2088
3574
|
context.addInitializer(function () {
|
|
2089
3575
|
const constructor = this.constructor;
|
|
3576
|
+
if (constructor[initKey])
|
|
3577
|
+
return;
|
|
3578
|
+
constructor[initKey] = true;
|
|
2090
3579
|
if (!constructor[DISPOSE_HANDLERS]) {
|
|
2091
3580
|
constructor[DISPOSE_HANDLERS] = [];
|
|
2092
3581
|
}
|
|
@@ -2104,8 +3593,12 @@ function dispose() {
|
|
|
2104
3593
|
function moved(options = {}) {
|
|
2105
3594
|
return function (originalMethod, context) {
|
|
2106
3595
|
const methodName = context.name;
|
|
3596
|
+
const initKey = `__moved_init_${methodName}`;
|
|
2107
3597
|
context.addInitializer(function () {
|
|
2108
3598
|
const constructor = this.constructor;
|
|
3599
|
+
if (constructor[initKey])
|
|
3600
|
+
return;
|
|
3601
|
+
constructor[initKey] = true;
|
|
2109
3602
|
if (!constructor[MOVED_HANDLERS]) {
|
|
2110
3603
|
constructor[MOVED_HANDLERS] = [];
|
|
2111
3604
|
}
|
|
@@ -2190,102 +3683,20 @@ function adopted(options = {}) {
|
|
|
2190
3683
|
return function (...args) {
|
|
2191
3684
|
// Initialize timers storage if not present
|
|
2192
3685
|
if (!this[ADOPTED_TIMERS]) {
|
|
2193
|
-
this[ADOPTED_TIMERS] = new Map();
|
|
2194
|
-
}
|
|
2195
|
-
// Get or create timers for this specific method
|
|
2196
|
-
if (!this[ADOPTED_TIMERS].has(methodName)) {
|
|
2197
|
-
this[ADOPTED_TIMERS].set(methodName, {
|
|
2198
|
-
throttleTimer: null,
|
|
2199
|
-
debounceTimer: null,
|
|
2200
|
-
lastThrottleCall: 0
|
|
2201
|
-
});
|
|
2202
|
-
}
|
|
2203
|
-
const timers = this[ADOPTED_TIMERS].get(methodName);
|
|
2204
|
-
// Helper function to execute method
|
|
2205
|
-
const executeMethod = (...methodArgs) => {
|
|
2206
|
-
return originalMethod.apply(this, methodArgs);
|
|
2207
|
-
};
|
|
2208
|
-
const hasDebounce = options.debounce !== undefined && options.debounce > 0;
|
|
2209
|
-
const hasThrottle = options.throttle !== undefined && options.throttle > 0;
|
|
2210
|
-
// Handle timing based on priority: debounce > throttle > immediate
|
|
2211
|
-
switch (true) {
|
|
2212
|
-
case hasDebounce: {
|
|
2213
|
-
clearTimeout(timers.debounceTimer);
|
|
2214
|
-
timers.debounceTimer = setTimeout(() => executeMethod(...args), options.debounce);
|
|
2215
|
-
return undefined;
|
|
2216
|
-
}
|
|
2217
|
-
case hasThrottle: {
|
|
2218
|
-
const throttleMs = options.throttle;
|
|
2219
|
-
const now = Date.now();
|
|
2220
|
-
const canExecuteImmediately = timers.lastThrottleCall === 0 || now - timers.lastThrottleCall >= throttleMs;
|
|
2221
|
-
if (canExecuteImmediately) {
|
|
2222
|
-
timers.lastThrottleCall = now;
|
|
2223
|
-
return executeMethod(...args);
|
|
2224
|
-
}
|
|
2225
|
-
const hasScheduledTimer = !!timers.throttleTimer;
|
|
2226
|
-
if (!hasScheduledTimer) {
|
|
2227
|
-
const remainingTime = throttleMs - (now - timers.lastThrottleCall);
|
|
2228
|
-
timers.throttleTimer = setTimeout(() => {
|
|
2229
|
-
timers.throttleTimer = null;
|
|
2230
|
-
timers.lastThrottleCall = Date.now();
|
|
2231
|
-
executeMethod(...args);
|
|
2232
|
-
}, remainingTime);
|
|
2233
|
-
}
|
|
2234
|
-
return undefined;
|
|
2235
|
-
}
|
|
2236
|
-
default:
|
|
2237
|
-
return executeMethod(...args);
|
|
2238
|
-
}
|
|
2239
|
-
};
|
|
2240
|
-
};
|
|
2241
|
-
}
|
|
2242
|
-
/**
|
|
2243
|
-
* Decorator for methods that render specific parts of the template
|
|
2244
|
-
* Parts are identified by the 'part' attribute in the HTML template
|
|
2245
|
-
* When the decorated method is called, it automatically re-renders its part
|
|
2246
|
-
*/
|
|
2247
|
-
function part(partName, options = {}) {
|
|
2248
|
-
return function (originalMethod, context) {
|
|
2249
|
-
const methodName = context.name;
|
|
2250
|
-
context.addInitializer(function () {
|
|
2251
|
-
const constructor = this.constructor;
|
|
2252
|
-
if (!constructor[PARTS]) {
|
|
2253
|
-
constructor[PARTS] = new Map();
|
|
2254
|
-
}
|
|
2255
|
-
constructor[PARTS].set(partName, {
|
|
2256
|
-
methodName,
|
|
2257
|
-
method: originalMethod
|
|
2258
|
-
});
|
|
2259
|
-
});
|
|
2260
|
-
// Return wrapped method that automatically re-renders the part when called
|
|
2261
|
-
return function (...args) {
|
|
2262
|
-
// Initialize timers storage if not present
|
|
2263
|
-
if (!this[PART_TIMERS]) {
|
|
2264
|
-
this[PART_TIMERS] = new Map();
|
|
3686
|
+
this[ADOPTED_TIMERS] = new Map();
|
|
2265
3687
|
}
|
|
2266
|
-
// Get or create timers for this specific
|
|
2267
|
-
if (!this[
|
|
2268
|
-
this[
|
|
3688
|
+
// Get or create timers for this specific method
|
|
3689
|
+
if (!this[ADOPTED_TIMERS].has(methodName)) {
|
|
3690
|
+
this[ADOPTED_TIMERS].set(methodName, {
|
|
2269
3691
|
throttleTimer: null,
|
|
2270
3692
|
debounceTimer: null,
|
|
2271
3693
|
lastThrottleCall: 0
|
|
2272
3694
|
});
|
|
2273
3695
|
}
|
|
2274
|
-
const timers = this[
|
|
2275
|
-
// Helper function to execute method
|
|
2276
|
-
const
|
|
2277
|
-
|
|
2278
|
-
const updateDOM = (content) => {
|
|
2279
|
-
const hasContent = content !== undefined;
|
|
2280
|
-
const hasElement = this.shadowRoot?.querySelector(`[part="${partName}"]`);
|
|
2281
|
-
if (hasContent && hasElement) {
|
|
2282
|
-
hasElement.innerHTML = content;
|
|
2283
|
-
}
|
|
2284
|
-
};
|
|
2285
|
-
const isPromise = result instanceof Promise;
|
|
2286
|
-
return isPromise
|
|
2287
|
-
? result.then(content => { updateDOM(content); return content; })
|
|
2288
|
-
: (updateDOM(result), result);
|
|
3696
|
+
const timers = this[ADOPTED_TIMERS].get(methodName);
|
|
3697
|
+
// Helper function to execute method
|
|
3698
|
+
const executeMethod = (...methodArgs) => {
|
|
3699
|
+
return originalMethod.apply(this, methodArgs);
|
|
2289
3700
|
};
|
|
2290
3701
|
const hasDebounce = options.debounce !== undefined && options.debounce > 0;
|
|
2291
3702
|
const hasThrottle = options.throttle !== undefined && options.throttle > 0;
|
|
@@ -2293,7 +3704,7 @@ function part(partName, options = {}) {
|
|
|
2293
3704
|
switch (true) {
|
|
2294
3705
|
case hasDebounce: {
|
|
2295
3706
|
clearTimeout(timers.debounceTimer);
|
|
2296
|
-
timers.debounceTimer = setTimeout(() =>
|
|
3707
|
+
timers.debounceTimer = setTimeout(() => executeMethod(...args), options.debounce);
|
|
2297
3708
|
return undefined;
|
|
2298
3709
|
}
|
|
2299
3710
|
case hasThrottle: {
|
|
@@ -2302,7 +3713,7 @@ function part(partName, options = {}) {
|
|
|
2302
3713
|
const canExecuteImmediately = timers.lastThrottleCall === 0 || now - timers.lastThrottleCall >= throttleMs;
|
|
2303
3714
|
if (canExecuteImmediately) {
|
|
2304
3715
|
timers.lastThrottleCall = now;
|
|
2305
|
-
return
|
|
3716
|
+
return executeMethod(...args);
|
|
2306
3717
|
}
|
|
2307
3718
|
const hasScheduledTimer = !!timers.throttleTimer;
|
|
2308
3719
|
if (!hasScheduledTimer) {
|
|
@@ -2310,17 +3721,19 @@ function part(partName, options = {}) {
|
|
|
2310
3721
|
timers.throttleTimer = setTimeout(() => {
|
|
2311
3722
|
timers.throttleTimer = null;
|
|
2312
3723
|
timers.lastThrottleCall = Date.now();
|
|
2313
|
-
|
|
3724
|
+
executeMethod(...args);
|
|
2314
3725
|
}, remainingTime);
|
|
2315
3726
|
}
|
|
2316
3727
|
return undefined;
|
|
2317
3728
|
}
|
|
2318
3729
|
default:
|
|
2319
|
-
return
|
|
3730
|
+
return executeMethod(...args);
|
|
2320
3731
|
}
|
|
2321
3732
|
};
|
|
2322
3733
|
};
|
|
2323
3734
|
}
|
|
3735
|
+
// @part decorator removed in v3.0.0
|
|
3736
|
+
// Use @render with differential rendering instead
|
|
2324
3737
|
|
|
2325
3738
|
/*!
|
|
2326
3739
|
* pica-route v1.1.2
|
|
@@ -3071,6 +4484,8 @@ function Router(options) {
|
|
|
3071
4484
|
let currentLayoutName = null; // Track current layout name
|
|
3072
4485
|
let currentLayoutTimestamp = null; // Track current layout timestamp
|
|
3073
4486
|
const context = options.context || {}; // Store context for guards
|
|
4487
|
+
// Create Context instance for managing router state
|
|
4488
|
+
const navigationContext = new Context(context, [], '', {});
|
|
3074
4489
|
function getCurrentLayoutElement(target) {
|
|
3075
4490
|
const noCurrentLayout = !currentLayoutName || !currentLayoutTimestamp;
|
|
3076
4491
|
if (noCurrentLayout) {
|
|
@@ -3108,33 +4523,19 @@ function Router(options) {
|
|
|
3108
4523
|
// Extend the connectedCallback to add router-specific functionality
|
|
3109
4524
|
const elementConnectedCallback = constructor.prototype.connectedCallback;
|
|
3110
4525
|
constructor.prototype.connectedCallback = function () {
|
|
4526
|
+
// Store the Context instance for @context decorated methods to access
|
|
4527
|
+
this[CONTEXT_HANDLER] = navigationContext;
|
|
3111
4528
|
// Call the element's connectedCallback first
|
|
3112
4529
|
elementConnectedCallback?.call(this);
|
|
3113
|
-
// Setup context request handler for nested elements
|
|
3114
|
-
const contextRequestHandler = (event) => {
|
|
3115
|
-
// Only respond if this element has context
|
|
3116
|
-
if (this[ROUTER_CONTEXT] !== undefined) {
|
|
3117
|
-
event.detail.context = this[ROUTER_CONTEXT];
|
|
3118
|
-
event.stopPropagation(); // Stop bubbling once context is provided
|
|
3119
|
-
}
|
|
3120
|
-
};
|
|
3121
|
-
this.addEventListener('@context/request', contextRequestHandler);
|
|
3122
|
-
// Store handler for cleanup
|
|
3123
|
-
this[CONTEXT_REQUEST_HANDLER] = contextRequestHandler;
|
|
3124
4530
|
};
|
|
3125
4531
|
// Extend the disconnectedCallback to clean up router-specific stuff
|
|
3126
4532
|
const elementDisconnectedCallback = constructor.prototype.disconnectedCallback;
|
|
3127
4533
|
constructor.prototype.disconnectedCallback = function () {
|
|
3128
4534
|
// Call element's disconnectedCallback first
|
|
3129
4535
|
elementDisconnectedCallback?.call(this);
|
|
3130
|
-
// Clean up context
|
|
3131
|
-
const handler = this[CONTEXT_REQUEST_HANDLER];
|
|
3132
|
-
if (handler) {
|
|
3133
|
-
this.removeEventListener('@context/request', handler);
|
|
3134
|
-
delete this[CONTEXT_REQUEST_HANDLER];
|
|
3135
|
-
}
|
|
3136
|
-
// Clean up context reference
|
|
4536
|
+
// Clean up context references
|
|
3137
4537
|
delete this[ROUTER_CONTEXT];
|
|
4538
|
+
delete this[CONTEXT_HANDLER];
|
|
3138
4539
|
};
|
|
3139
4540
|
// Define the custom element
|
|
3140
4541
|
customElements.define(pageOptions.tag, constructor);
|
|
@@ -3193,8 +4594,8 @@ function Router(options) {
|
|
|
3193
4594
|
* initialize();
|
|
3194
4595
|
*/
|
|
3195
4596
|
function initialize() {
|
|
3196
|
-
const
|
|
3197
|
-
if (!
|
|
4597
|
+
const target = document.querySelector(options.target);
|
|
4598
|
+
if (!target) {
|
|
3198
4599
|
throw new Error(`Target element not found: ${options.target}`);
|
|
3199
4600
|
}
|
|
3200
4601
|
const needsSorting = !is_sorted;
|
|
@@ -3218,6 +4619,10 @@ function Router(options) {
|
|
|
3218
4619
|
: placard;
|
|
3219
4620
|
});
|
|
3220
4621
|
}
|
|
4622
|
+
function emitContextUpdate(target, currentPath, routeParams) {
|
|
4623
|
+
// Update the navigation context and notify all registered elements
|
|
4624
|
+
navigationContext.update(context, placards, currentPath, routeParams);
|
|
4625
|
+
}
|
|
3221
4626
|
function updateLayout(layoutElement, currentPath, routeParams) {
|
|
3222
4627
|
// Check if layout implements the update method
|
|
3223
4628
|
if (typeof layoutElement.update === 'function') {
|
|
@@ -3300,6 +4705,7 @@ function Router(options) {
|
|
|
3300
4705
|
}
|
|
3301
4706
|
const newPageElement = document.createElement(route.tag);
|
|
3302
4707
|
newPageElement[ROUTER_CONTEXT] = context;
|
|
4708
|
+
newPageElement[CONTEXT_HANDLER] = navigationContext;
|
|
3303
4709
|
const routeParams = params;
|
|
3304
4710
|
Object.keys(routeParams).forEach(key => newPageElement.setAttribute(key, routeParams[key]));
|
|
3305
4711
|
return { result: RouteResult.SUCCESS, element: newPageElement, transition: route.transition, layout: route.layout, routeParams };
|
|
@@ -3390,7 +4796,7 @@ function Router(options) {
|
|
|
3390
4796
|
// Collect fresh placards before navigation
|
|
3391
4797
|
collectPlacards();
|
|
3392
4798
|
window.scrollTo(0, 0);
|
|
3393
|
-
const isHomePath = (path
|
|
4799
|
+
const isHomePath = (path?.trim() === '' || path === '/') && !!home;
|
|
3394
4800
|
if (isHomePath) {
|
|
3395
4801
|
const homeRoute = routes.find(r => r.route.match('/'));
|
|
3396
4802
|
const guardsAllowed = checkGuards(homeRoute?.guards, {}, target);
|
|
@@ -3404,11 +4810,15 @@ function Router(options) {
|
|
|
3404
4810
|
const hasLayout = layoutElement !== null || getCurrentLayoutElement(target) !== null;
|
|
3405
4811
|
if (hasLayout) {
|
|
3406
4812
|
await renderWithLayout(target, element, finalTransition, layoutElement, needsNewLayout, path, {});
|
|
4813
|
+
emitContextUpdate(target, path, {});
|
|
3407
4814
|
return;
|
|
3408
4815
|
}
|
|
3409
4816
|
await renderDirect(target, element, finalTransition);
|
|
4817
|
+
emitContextUpdate(target, path, {});
|
|
3410
4818
|
return;
|
|
3411
4819
|
}
|
|
4820
|
+
if (!path)
|
|
4821
|
+
return;
|
|
3412
4822
|
const routeResult = resolveRoute(path, target);
|
|
3413
4823
|
const isGuardsFailed = routeResult.result === RouteResult.GUARDS_FAILED;
|
|
3414
4824
|
if (isGuardsFailed) {
|
|
@@ -3423,9 +4833,11 @@ function Router(options) {
|
|
|
3423
4833
|
const hasLayout = layoutElement !== null || getCurrentLayoutElement(target) !== null;
|
|
3424
4834
|
if (hasLayout) {
|
|
3425
4835
|
await renderWithLayout(target, element, finalTransition, layoutElement, needsNewLayout, path, routeParams);
|
|
4836
|
+
emitContextUpdate(target, path, routeParams);
|
|
3426
4837
|
return;
|
|
3427
4838
|
}
|
|
3428
4839
|
await renderDirect(target, element, finalTransition);
|
|
4840
|
+
emitContextUpdate(target, path, routeParams);
|
|
3429
4841
|
return;
|
|
3430
4842
|
}
|
|
3431
4843
|
const { element, transition, layout } = create404Element();
|
|
@@ -3435,9 +4847,11 @@ function Router(options) {
|
|
|
3435
4847
|
const hasLayout = layoutElement !== null || getCurrentLayoutElement(target) !== null;
|
|
3436
4848
|
if (hasLayout) {
|
|
3437
4849
|
await renderWithLayout(target, element, finalTransition, layoutElement, needsNewLayout, path, {});
|
|
4850
|
+
emitContextUpdate(target, path, {});
|
|
3438
4851
|
return;
|
|
3439
4852
|
}
|
|
3440
4853
|
await renderDirect(target, element, finalTransition);
|
|
4854
|
+
emitContextUpdate(target, path, {});
|
|
3441
4855
|
}
|
|
3442
4856
|
async function performTransition$1(container, oldElement, newElement, transition) {
|
|
3443
4857
|
return performTransition(container, oldElement, newElement, transition);
|
|
@@ -3450,28 +4864,598 @@ function Router(options) {
|
|
|
3450
4864
|
};
|
|
3451
4865
|
}
|
|
3452
4866
|
|
|
4867
|
+
// @on decorator removed in v3.0.0 - use template event syntax instead: @click=${handler}
|
|
4868
|
+
/**
|
|
4869
|
+
* Decorator that automatically dispatches a custom event after a method is called.
|
|
4870
|
+
* The return value of the method becomes the event detail.
|
|
4871
|
+
*
|
|
4872
|
+
* @param eventName The name of the event to dispatch
|
|
4873
|
+
* @param options Optional configuration extending EventInit
|
|
4874
|
+
*/
|
|
4875
|
+
function dispatch(eventName, options) {
|
|
4876
|
+
return function (originalMethod, _context) {
|
|
4877
|
+
return function (...args) {
|
|
4878
|
+
// Create timing wrappers for dispatch (per-instance)
|
|
4879
|
+
if (!this[DISPATCH_TIMERS]) {
|
|
4880
|
+
this[DISPATCH_TIMERS] = new Map();
|
|
4881
|
+
}
|
|
4882
|
+
const timerKey = `${eventName}_${_context.name}`;
|
|
4883
|
+
if (!this[DISPATCH_TIMERS].has(timerKey)) {
|
|
4884
|
+
this[DISPATCH_TIMERS].set(timerKey, {
|
|
4885
|
+
debounceTimeout: null,
|
|
4886
|
+
throttleLastCall: 0,
|
|
4887
|
+
throttleTimeout: null
|
|
4888
|
+
});
|
|
4889
|
+
}
|
|
4890
|
+
const timers = this[DISPATCH_TIMERS].get(timerKey);
|
|
4891
|
+
// Call the original method with preserved this context
|
|
4892
|
+
const result = originalMethod.apply(this, args);
|
|
4893
|
+
// Helper to dispatch the event
|
|
4894
|
+
const doDispatch = (detail) => {
|
|
4895
|
+
// Skip dispatch if result is undefined and dispatchOnUndefined is false
|
|
4896
|
+
if (detail === undefined && options?.dispatchOnUndefined === false) {
|
|
4897
|
+
return;
|
|
4898
|
+
}
|
|
4899
|
+
// Create event with spread operator for options
|
|
4900
|
+
const event = new CustomEvent(eventName, {
|
|
4901
|
+
bubbles: true, // Default to true for component events
|
|
4902
|
+
composed: true, // Allow crossing shadow DOM boundaries
|
|
4903
|
+
...options, // Spread all EventInit options
|
|
4904
|
+
detail
|
|
4905
|
+
});
|
|
4906
|
+
this.dispatchEvent(event);
|
|
4907
|
+
};
|
|
4908
|
+
// Helper to handle timed dispatch
|
|
4909
|
+
const timedDispatch = (detail) => {
|
|
4910
|
+
if (options?.debounce) {
|
|
4911
|
+
clearTimeout(timers.debounceTimeout);
|
|
4912
|
+
timers.debounceTimeout = setTimeout(() => doDispatch(detail), options.debounce);
|
|
4913
|
+
}
|
|
4914
|
+
else if (options?.throttle) {
|
|
4915
|
+
const now = Date.now();
|
|
4916
|
+
const remaining = options.throttle - (now - timers.throttleLastCall);
|
|
4917
|
+
if (remaining <= 0) {
|
|
4918
|
+
clearTimeout(timers.throttleTimeout);
|
|
4919
|
+
timers.throttleLastCall = now;
|
|
4920
|
+
doDispatch(detail);
|
|
4921
|
+
}
|
|
4922
|
+
else if (!timers.throttleTimeout) {
|
|
4923
|
+
timers.throttleTimeout = setTimeout(() => {
|
|
4924
|
+
timers.throttleLastCall = Date.now();
|
|
4925
|
+
timers.throttleTimeout = null;
|
|
4926
|
+
doDispatch(detail);
|
|
4927
|
+
}, remaining);
|
|
4928
|
+
}
|
|
4929
|
+
}
|
|
4930
|
+
else {
|
|
4931
|
+
doDispatch(detail);
|
|
4932
|
+
}
|
|
4933
|
+
};
|
|
4934
|
+
// Handle async methods
|
|
4935
|
+
if (result instanceof Promise) {
|
|
4936
|
+
return result.then((resolvedResult) => {
|
|
4937
|
+
timedDispatch(resolvedResult);
|
|
4938
|
+
return resolvedResult;
|
|
4939
|
+
});
|
|
4940
|
+
}
|
|
4941
|
+
// Sync method
|
|
4942
|
+
timedDispatch(result);
|
|
4943
|
+
return result;
|
|
4944
|
+
};
|
|
4945
|
+
};
|
|
4946
|
+
}
|
|
4947
|
+
|
|
4948
|
+
/**
|
|
4949
|
+
* Custom element readiness utilities
|
|
4950
|
+
* Handles waiting for custom elements to be defined with timeout warnings
|
|
4951
|
+
*/
|
|
4952
|
+
/**
|
|
4953
|
+
* Global flag to disable custom element readiness timeout warnings
|
|
4954
|
+
* Set this to true in environments where slow element registration is expected
|
|
4955
|
+
*/
|
|
4956
|
+
let DISABLE_ELEMENT_READY_WARNINGS = false;
|
|
4957
|
+
/**
|
|
4958
|
+
* Set whether to disable custom element readiness timeout warnings
|
|
4959
|
+
*/
|
|
4960
|
+
function setDisableElementReadyWarnings(value) {
|
|
4961
|
+
DISABLE_ELEMENT_READY_WARNINGS = value;
|
|
4962
|
+
}
|
|
4963
|
+
/**
|
|
4964
|
+
* Default timeout for custom element registration warning (500ms)
|
|
4965
|
+
*/
|
|
4966
|
+
const DEFAULT_WARNING_TIMEOUT = 500;
|
|
4967
|
+
/**
|
|
4968
|
+
* Wait for a custom element to be defined
|
|
4969
|
+
* Logs a warning if it takes longer than the warning timeout
|
|
4970
|
+
*
|
|
4971
|
+
* @param tagName - The custom element tag name
|
|
4972
|
+
* @param warningTimeout - Time in ms before warning (default 500ms)
|
|
4973
|
+
* @returns Promise that resolves when element is defined
|
|
4974
|
+
*/
|
|
4975
|
+
async function waitForElementDefined(tagName, warningTimeout = DEFAULT_WARNING_TIMEOUT) {
|
|
4976
|
+
// If already defined, return immediately
|
|
4977
|
+
if (customElements.get(tagName)) {
|
|
4978
|
+
return;
|
|
4979
|
+
}
|
|
4980
|
+
// Set up warning timer if not disabled
|
|
4981
|
+
let warningTimer = null;
|
|
4982
|
+
if (!DISABLE_ELEMENT_READY_WARNINGS) {
|
|
4983
|
+
warningTimer = setTimeout(() => {
|
|
4984
|
+
console.warn(`Custom element <${tagName}> is taking longer than ${warningTimeout}ms to register. ` +
|
|
4985
|
+
`This may indicate a missing import or circular dependency. ` +
|
|
4986
|
+
`Set DISABLE_ELEMENT_READY_WARNINGS=true to disable this warning.`);
|
|
4987
|
+
}, warningTimeout);
|
|
4988
|
+
}
|
|
4989
|
+
try {
|
|
4990
|
+
// Wait for element to be defined
|
|
4991
|
+
await customElements.whenDefined(tagName);
|
|
4992
|
+
}
|
|
4993
|
+
finally {
|
|
4994
|
+
// Clear warning timer
|
|
4995
|
+
if (warningTimer) {
|
|
4996
|
+
clearTimeout(warningTimer);
|
|
4997
|
+
}
|
|
4998
|
+
}
|
|
4999
|
+
}
|
|
5000
|
+
/**
|
|
5001
|
+
* Wait for a custom element to be defined and ready
|
|
5002
|
+
* First waits for the element to be registered, then waits for its ready promise
|
|
5003
|
+
*
|
|
5004
|
+
* @param element - The custom element instance
|
|
5005
|
+
* @param warningTimeout - Time in ms before warning about registration (default 500ms)
|
|
5006
|
+
* @returns Promise that resolves when element is defined and ready
|
|
5007
|
+
*/
|
|
5008
|
+
async function waitForElementReady(element, warningTimeout = DEFAULT_WARNING_TIMEOUT) {
|
|
5009
|
+
const tagName = element.tagName.toLowerCase();
|
|
5010
|
+
// Wait for element to be defined
|
|
5011
|
+
await waitForElementDefined(tagName, warningTimeout);
|
|
5012
|
+
// Wait for element's ready promise if it exists
|
|
5013
|
+
if ('ready' in element && typeof element.ready?.then === 'function') {
|
|
5014
|
+
await element.ready;
|
|
5015
|
+
}
|
|
5016
|
+
}
|
|
5017
|
+
/**
|
|
5018
|
+
* Process all custom elements in a node tree and wait for them to be ready
|
|
5019
|
+
* This is useful after inserting a template with custom elements
|
|
5020
|
+
*
|
|
5021
|
+
* @param node - The root node to scan for custom elements
|
|
5022
|
+
* @param warningTimeout - Time in ms before warning about registration (default 500ms)
|
|
5023
|
+
* @returns Promise that resolves when all custom elements are ready
|
|
5024
|
+
*/
|
|
5025
|
+
async function waitForAllCustomElements(node, warningTimeout = DEFAULT_WARNING_TIMEOUT) {
|
|
5026
|
+
const customElements = [];
|
|
5027
|
+
// Find all custom elements (tag names with hyphens)
|
|
5028
|
+
if (node instanceof Element) {
|
|
5029
|
+
if (node.tagName.includes('-')) {
|
|
5030
|
+
customElements.push(node);
|
|
5031
|
+
}
|
|
5032
|
+
// Also check in shadow DOM
|
|
5033
|
+
if (node.shadowRoot) {
|
|
5034
|
+
const shadowCustomElements = node.shadowRoot.querySelectorAll('*');
|
|
5035
|
+
shadowCustomElements.forEach(el => {
|
|
5036
|
+
if (el.tagName.includes('-')) {
|
|
5037
|
+
customElements.push(el);
|
|
5038
|
+
}
|
|
5039
|
+
});
|
|
5040
|
+
}
|
|
5041
|
+
}
|
|
5042
|
+
// If it's a DocumentFragment, check all children
|
|
5043
|
+
if (node instanceof DocumentFragment) {
|
|
5044
|
+
const elements = node.querySelectorAll('*');
|
|
5045
|
+
elements.forEach(el => {
|
|
5046
|
+
if (el.tagName.includes('-')) {
|
|
5047
|
+
customElements.push(el);
|
|
5048
|
+
}
|
|
5049
|
+
});
|
|
5050
|
+
}
|
|
5051
|
+
// Wait for all custom elements to be ready
|
|
5052
|
+
await Promise.all(customElements.map(el => waitForElementReady(el, warningTimeout)));
|
|
5053
|
+
}
|
|
5054
|
+
|
|
5055
|
+
/**
|
|
5056
|
+
* Render debugging utilities for Snice v3.0.0
|
|
5057
|
+
* For testing and debugging only - not recommended for production use
|
|
5058
|
+
*/
|
|
5059
|
+
/**
|
|
5060
|
+
* Track renders of an element using an async generator
|
|
5061
|
+
* Each call to tracker.next() waits for the next render to complete
|
|
5062
|
+
*
|
|
5063
|
+
* @example
|
|
5064
|
+
* ```typescript
|
|
5065
|
+
* const tracker = trackRenders(element);
|
|
5066
|
+
*
|
|
5067
|
+
* element.someProp = 'new value';
|
|
5068
|
+
* await tracker.next(); // Waits for render
|
|
5069
|
+
* // DOM is now updated
|
|
5070
|
+
*
|
|
5071
|
+
* element.someProp = 'another value';
|
|
5072
|
+
* await tracker.next(); // Waits for next render
|
|
5073
|
+
* // DOM is updated again
|
|
5074
|
+
* ```
|
|
5075
|
+
*
|
|
5076
|
+
* WARNING: For testing/debugging only!
|
|
5077
|
+
* - Do not use in production code
|
|
5078
|
+
* - The generator yields indefinitely - use it only in controlled test environments
|
|
5079
|
+
* - Each yield waits for the next render event
|
|
5080
|
+
*/
|
|
5081
|
+
async function* trackRenders(element) {
|
|
5082
|
+
while (true) {
|
|
5083
|
+
await new Promise(resolve => {
|
|
5084
|
+
if (!element[RENDER_CALLBACKS]) {
|
|
5085
|
+
element[RENDER_CALLBACKS] = [];
|
|
5086
|
+
}
|
|
5087
|
+
element[RENDER_CALLBACKS].push(resolve);
|
|
5088
|
+
});
|
|
5089
|
+
yield;
|
|
5090
|
+
}
|
|
5091
|
+
}
|
|
5092
|
+
|
|
5093
|
+
/**
|
|
5094
|
+
* Method decorators for common patterns
|
|
5095
|
+
* @debounce, @throttle, @once, @memoize
|
|
5096
|
+
*/
|
|
5097
|
+
const DEBOUNCE_TIMERS = getSymbol('debounce-timers');
|
|
5098
|
+
const THROTTLE_TIMERS = getSymbol('throttle-timers');
|
|
5099
|
+
const ONCE_CALLED = getSymbol('once-called');
|
|
5100
|
+
const MEMOIZE_CACHE = getSymbol('memoize-cache');
|
|
5101
|
+
/**
|
|
5102
|
+
* @debounce decorator - delays function execution until after wait time has elapsed
|
|
5103
|
+
* since the last invocation
|
|
5104
|
+
*
|
|
5105
|
+
* @param wait - Time to wait in milliseconds (default: 300)
|
|
5106
|
+
* @param options - Debounce options
|
|
5107
|
+
* @param options.leading - Invoke on the leading edge (default: false)
|
|
5108
|
+
* @param options.trailing - Invoke on the trailing edge (default: true)
|
|
5109
|
+
* @param options.maxWait - Maximum time to wait before invoking (default: undefined)
|
|
5110
|
+
*
|
|
5111
|
+
* @example
|
|
5112
|
+
* ```typescript
|
|
5113
|
+
* @element('search-input')
|
|
5114
|
+
* class SearchInput extends HTMLElement {
|
|
5115
|
+
* @debounce(500)
|
|
5116
|
+
* handleSearch(query: string) {
|
|
5117
|
+
* // Only called 500ms after last keystroke
|
|
5118
|
+
* fetch(`/api/search?q=${query}`);
|
|
5119
|
+
* }
|
|
5120
|
+
* }
|
|
5121
|
+
* ```
|
|
5122
|
+
*/
|
|
5123
|
+
function debounce(wait = 300, options = {}) {
|
|
5124
|
+
const { leading = false, trailing = true, maxWait } = options;
|
|
5125
|
+
return function (originalMethod, context) {
|
|
5126
|
+
const methodName = context.name;
|
|
5127
|
+
return function (...args) {
|
|
5128
|
+
if (!this[DEBOUNCE_TIMERS]) {
|
|
5129
|
+
this[DEBOUNCE_TIMERS] = {};
|
|
5130
|
+
}
|
|
5131
|
+
const timers = this[DEBOUNCE_TIMERS];
|
|
5132
|
+
const timerKey = methodName;
|
|
5133
|
+
// Clear existing timer
|
|
5134
|
+
if (timers[timerKey]) {
|
|
5135
|
+
clearTimeout(timers[timerKey].timeout);
|
|
5136
|
+
}
|
|
5137
|
+
// Track when debounce started for maxWait
|
|
5138
|
+
const now = Date.now();
|
|
5139
|
+
const isFirstCall = !timers[timerKey];
|
|
5140
|
+
const startTime = isFirstCall ? now : timers[timerKey].startTime;
|
|
5141
|
+
// Check if maxWait exceeded
|
|
5142
|
+
const shouldInvokeFromMaxWait = maxWait !== undefined && now - startTime >= maxWait;
|
|
5143
|
+
// Leading edge invocation
|
|
5144
|
+
if (leading && isFirstCall) {
|
|
5145
|
+
const result = originalMethod.apply(this, args);
|
|
5146
|
+
timers[timerKey] = {
|
|
5147
|
+
timeout: null,
|
|
5148
|
+
startTime,
|
|
5149
|
+
lastArgs: args,
|
|
5150
|
+
};
|
|
5151
|
+
return result;
|
|
5152
|
+
}
|
|
5153
|
+
// Set new timer for trailing edge
|
|
5154
|
+
const timeout = setTimeout(() => {
|
|
5155
|
+
if (trailing || shouldInvokeFromMaxWait) {
|
|
5156
|
+
originalMethod.apply(this, timers[timerKey].lastArgs);
|
|
5157
|
+
}
|
|
5158
|
+
delete timers[timerKey];
|
|
5159
|
+
}, shouldInvokeFromMaxWait ? 0 : wait);
|
|
5160
|
+
timers[timerKey] = {
|
|
5161
|
+
timeout,
|
|
5162
|
+
startTime,
|
|
5163
|
+
lastArgs: args,
|
|
5164
|
+
};
|
|
5165
|
+
};
|
|
5166
|
+
};
|
|
5167
|
+
}
|
|
5168
|
+
/**
|
|
5169
|
+
* @throttle decorator - ensures function is called at most once per specified time period
|
|
5170
|
+
*
|
|
5171
|
+
* @param wait - Time to wait in milliseconds (default: 300)
|
|
5172
|
+
* @param options - Throttle options
|
|
5173
|
+
* @param options.leading - Invoke on the leading edge (default: true)
|
|
5174
|
+
* @param options.trailing - Invoke on the trailing edge (default: true)
|
|
5175
|
+
*
|
|
5176
|
+
* @example
|
|
5177
|
+
* ```typescript
|
|
5178
|
+
* @element('scroll-tracker')
|
|
5179
|
+
* class ScrollTracker extends HTMLElement {
|
|
5180
|
+
* @throttle(100)
|
|
5181
|
+
* handleScroll(e: Event) {
|
|
5182
|
+
* // Called at most once every 100ms
|
|
5183
|
+
* this.updateScrollPosition();
|
|
5184
|
+
* }
|
|
5185
|
+
* }
|
|
5186
|
+
* ```
|
|
5187
|
+
*/
|
|
5188
|
+
function throttle(wait = 300, options = {}) {
|
|
5189
|
+
const { leading = true, trailing = true } = options;
|
|
5190
|
+
return function (originalMethod, context) {
|
|
5191
|
+
const methodName = context.name;
|
|
5192
|
+
return function (...args) {
|
|
5193
|
+
if (!this[THROTTLE_TIMERS]) {
|
|
5194
|
+
this[THROTTLE_TIMERS] = {};
|
|
5195
|
+
}
|
|
5196
|
+
const timers = this[THROTTLE_TIMERS];
|
|
5197
|
+
const timerKey = methodName;
|
|
5198
|
+
const now = Date.now();
|
|
5199
|
+
if (!timers[timerKey]) {
|
|
5200
|
+
// First call
|
|
5201
|
+
if (leading) {
|
|
5202
|
+
originalMethod.apply(this, args);
|
|
5203
|
+
}
|
|
5204
|
+
timers[timerKey] = {
|
|
5205
|
+
lastInvoke: now,
|
|
5206
|
+
timeout: null,
|
|
5207
|
+
lastArgs: args,
|
|
5208
|
+
};
|
|
5209
|
+
if (trailing && !leading) {
|
|
5210
|
+
// If no leading edge, set up trailing
|
|
5211
|
+
timers[timerKey].timeout = setTimeout(() => {
|
|
5212
|
+
originalMethod.apply(this, timers[timerKey].lastArgs);
|
|
5213
|
+
delete timers[timerKey];
|
|
5214
|
+
}, wait);
|
|
5215
|
+
}
|
|
5216
|
+
}
|
|
5217
|
+
else {
|
|
5218
|
+
// Subsequent calls
|
|
5219
|
+
const timeSinceLastInvoke = now - timers[timerKey].lastInvoke;
|
|
5220
|
+
// Update last args
|
|
5221
|
+
timers[timerKey].lastArgs = args;
|
|
5222
|
+
// Clear any pending trailing call
|
|
5223
|
+
if (timers[timerKey].timeout) {
|
|
5224
|
+
clearTimeout(timers[timerKey].timeout);
|
|
5225
|
+
}
|
|
5226
|
+
if (timeSinceLastInvoke >= wait) {
|
|
5227
|
+
// Enough time has passed, invoke immediately
|
|
5228
|
+
originalMethod.apply(this, args);
|
|
5229
|
+
timers[timerKey].lastInvoke = now;
|
|
5230
|
+
}
|
|
5231
|
+
else if (trailing) {
|
|
5232
|
+
// Set up trailing call
|
|
5233
|
+
const remaining = wait - timeSinceLastInvoke;
|
|
5234
|
+
timers[timerKey].timeout = setTimeout(() => {
|
|
5235
|
+
originalMethod.apply(this, timers[timerKey].lastArgs);
|
|
5236
|
+
timers[timerKey].lastInvoke = Date.now();
|
|
5237
|
+
timers[timerKey].timeout = null;
|
|
5238
|
+
}, remaining);
|
|
5239
|
+
}
|
|
5240
|
+
}
|
|
5241
|
+
};
|
|
5242
|
+
};
|
|
5243
|
+
}
|
|
5244
|
+
/**
|
|
5245
|
+
* @once decorator - ensures function is only called once
|
|
5246
|
+
* Subsequent calls return the result of the first call
|
|
5247
|
+
*
|
|
5248
|
+
* @param perInstance - If true, function can be called once per instance (default: true)
|
|
5249
|
+
* If false, function can only be called once globally across all instances
|
|
5250
|
+
*
|
|
5251
|
+
* @example
|
|
5252
|
+
* ```typescript
|
|
5253
|
+
* @element('data-loader')
|
|
5254
|
+
* class DataLoader extends HTMLElement {
|
|
5255
|
+
* @once()
|
|
5256
|
+
* async loadData() {
|
|
5257
|
+
* // Only loads data once, even if called multiple times
|
|
5258
|
+
* const data = await fetch('/api/data');
|
|
5259
|
+
* return data.json();
|
|
5260
|
+
* }
|
|
5261
|
+
* }
|
|
5262
|
+
* ```
|
|
5263
|
+
*/
|
|
5264
|
+
function once(perInstance = true) {
|
|
5265
|
+
let globalCalled = false;
|
|
5266
|
+
let globalResult;
|
|
5267
|
+
return function (originalMethod, context) {
|
|
5268
|
+
const methodName = context.name;
|
|
5269
|
+
return function (...args) {
|
|
5270
|
+
if (perInstance) {
|
|
5271
|
+
// Per-instance tracking
|
|
5272
|
+
if (!this[ONCE_CALLED]) {
|
|
5273
|
+
this[ONCE_CALLED] = {};
|
|
5274
|
+
}
|
|
5275
|
+
if (!this[ONCE_CALLED][methodName]) {
|
|
5276
|
+
this[ONCE_CALLED][methodName] = {
|
|
5277
|
+
called: true,
|
|
5278
|
+
result: originalMethod.apply(this, args),
|
|
5279
|
+
};
|
|
5280
|
+
}
|
|
5281
|
+
return this[ONCE_CALLED][methodName].result;
|
|
5282
|
+
}
|
|
5283
|
+
else {
|
|
5284
|
+
// Global tracking
|
|
5285
|
+
if (!globalCalled) {
|
|
5286
|
+
globalCalled = true;
|
|
5287
|
+
globalResult = originalMethod.apply(this, args);
|
|
5288
|
+
}
|
|
5289
|
+
return globalResult;
|
|
5290
|
+
}
|
|
5291
|
+
};
|
|
5292
|
+
};
|
|
5293
|
+
}
|
|
5294
|
+
/**
|
|
5295
|
+
* @memoize decorator - caches function results based on arguments
|
|
5296
|
+
* Uses JSON.stringify for argument comparison by default
|
|
5297
|
+
*
|
|
5298
|
+
* @param options - Memoization options
|
|
5299
|
+
* @param options.keyGenerator - Custom function to generate cache key from arguments
|
|
5300
|
+
* @param options.maxSize - Maximum cache size (default: 100)
|
|
5301
|
+
* @param options.ttl - Time to live in milliseconds (default: undefined - no expiration)
|
|
5302
|
+
*
|
|
5303
|
+
* @example
|
|
5304
|
+
* ```typescript
|
|
5305
|
+
* @element('calculator')
|
|
5306
|
+
* class Calculator extends HTMLElement {
|
|
5307
|
+
* @memoize({ maxSize: 50 })
|
|
5308
|
+
* fibonacci(n: number): number {
|
|
5309
|
+
* // Results are cached, subsequent calls with same n are instant
|
|
5310
|
+
* if (n <= 1) return n;
|
|
5311
|
+
* return this.fibonacci(n - 1) + this.fibonacci(n - 2);
|
|
5312
|
+
* }
|
|
5313
|
+
* }
|
|
5314
|
+
* ```
|
|
5315
|
+
*/
|
|
5316
|
+
function memoize(options = {}) {
|
|
5317
|
+
const { keyGenerator = (...args) => JSON.stringify(args), maxSize = 100, ttl } = options;
|
|
5318
|
+
return function (originalMethod, context) {
|
|
5319
|
+
const methodName = context.name;
|
|
5320
|
+
return function (...args) {
|
|
5321
|
+
if (!this[MEMOIZE_CACHE]) {
|
|
5322
|
+
this[MEMOIZE_CACHE] = {};
|
|
5323
|
+
}
|
|
5324
|
+
if (!this[MEMOIZE_CACHE][methodName]) {
|
|
5325
|
+
this[MEMOIZE_CACHE][methodName] = new Map();
|
|
5326
|
+
}
|
|
5327
|
+
const cache = this[MEMOIZE_CACHE][methodName];
|
|
5328
|
+
const key = keyGenerator(...args);
|
|
5329
|
+
// Check if cached
|
|
5330
|
+
if (cache.has(key)) {
|
|
5331
|
+
const cached = cache.get(key);
|
|
5332
|
+
// Check TTL
|
|
5333
|
+
if (ttl !== undefined) {
|
|
5334
|
+
const age = Date.now() - cached.timestamp;
|
|
5335
|
+
if (age > ttl) {
|
|
5336
|
+
cache.delete(key);
|
|
5337
|
+
}
|
|
5338
|
+
else {
|
|
5339
|
+
return cached.value;
|
|
5340
|
+
}
|
|
5341
|
+
}
|
|
5342
|
+
else {
|
|
5343
|
+
return cached.value;
|
|
5344
|
+
}
|
|
5345
|
+
}
|
|
5346
|
+
// Compute result
|
|
5347
|
+
const result = originalMethod.apply(this, args);
|
|
5348
|
+
// Store in cache
|
|
5349
|
+
cache.set(key, {
|
|
5350
|
+
value: result,
|
|
5351
|
+
timestamp: Date.now(),
|
|
5352
|
+
});
|
|
5353
|
+
// Enforce max size (LRU - delete oldest)
|
|
5354
|
+
if (cache.size > maxSize) {
|
|
5355
|
+
const firstKey = cache.keys().next().value;
|
|
5356
|
+
cache.delete(firstKey);
|
|
5357
|
+
}
|
|
5358
|
+
return result;
|
|
5359
|
+
};
|
|
5360
|
+
};
|
|
5361
|
+
}
|
|
5362
|
+
/**
|
|
5363
|
+
* Clear all debounce timers for an instance
|
|
5364
|
+
* Useful in cleanup/disconnectedCallback
|
|
5365
|
+
*/
|
|
5366
|
+
function clearDebounceTimers(instance) {
|
|
5367
|
+
if (instance[DEBOUNCE_TIMERS]) {
|
|
5368
|
+
for (const timerKey in instance[DEBOUNCE_TIMERS]) {
|
|
5369
|
+
if (instance[DEBOUNCE_TIMERS][timerKey]?.timeout) {
|
|
5370
|
+
clearTimeout(instance[DEBOUNCE_TIMERS][timerKey].timeout);
|
|
5371
|
+
}
|
|
5372
|
+
}
|
|
5373
|
+
instance[DEBOUNCE_TIMERS] = {};
|
|
5374
|
+
}
|
|
5375
|
+
}
|
|
5376
|
+
/**
|
|
5377
|
+
* Clear all throttle timers for an instance
|
|
5378
|
+
* Useful in cleanup/disconnectedCallback
|
|
5379
|
+
*/
|
|
5380
|
+
function clearThrottleTimers(instance) {
|
|
5381
|
+
if (instance[THROTTLE_TIMERS]) {
|
|
5382
|
+
for (const timerKey in instance[THROTTLE_TIMERS]) {
|
|
5383
|
+
if (instance[THROTTLE_TIMERS][timerKey]?.timeout) {
|
|
5384
|
+
clearTimeout(instance[THROTTLE_TIMERS][timerKey].timeout);
|
|
5385
|
+
}
|
|
5386
|
+
}
|
|
5387
|
+
instance[THROTTLE_TIMERS] = {};
|
|
5388
|
+
}
|
|
5389
|
+
}
|
|
5390
|
+
/**
|
|
5391
|
+
* Clear memoize cache for an instance
|
|
5392
|
+
*/
|
|
5393
|
+
function clearMemoizeCache(instance, methodName) {
|
|
5394
|
+
if (instance[MEMOIZE_CACHE]) {
|
|
5395
|
+
if (methodName) {
|
|
5396
|
+
delete instance[MEMOIZE_CACHE][methodName];
|
|
5397
|
+
}
|
|
5398
|
+
else {
|
|
5399
|
+
instance[MEMOIZE_CACHE] = {};
|
|
5400
|
+
}
|
|
5401
|
+
}
|
|
5402
|
+
}
|
|
5403
|
+
/**
|
|
5404
|
+
* Reset once-called state for an instance
|
|
5405
|
+
*/
|
|
5406
|
+
function resetOnce(instance, methodName) {
|
|
5407
|
+
if (instance[ONCE_CALLED]) {
|
|
5408
|
+
if (methodName) {
|
|
5409
|
+
delete instance[ONCE_CALLED][methodName];
|
|
5410
|
+
}
|
|
5411
|
+
else {
|
|
5412
|
+
instance[ONCE_CALLED] = {};
|
|
5413
|
+
}
|
|
5414
|
+
}
|
|
5415
|
+
}
|
|
5416
|
+
|
|
5417
|
+
exports.Context = Context;
|
|
3453
5418
|
exports.IS_CONTROLLER_INSTANCE = IS_CONTROLLER_INSTANCE;
|
|
3454
5419
|
exports.Router = Router;
|
|
3455
5420
|
exports.SimpleArray = SimpleArray;
|
|
3456
5421
|
exports.adopted = adopted;
|
|
3457
5422
|
exports.applyElementFunctionality = applyElementFunctionality;
|
|
3458
|
-
exports.
|
|
5423
|
+
exports.clearDebounceTimers = clearDebounceTimers;
|
|
5424
|
+
exports.clearMemoizeCache = clearMemoizeCache;
|
|
5425
|
+
exports.clearThrottleTimers = clearThrottleTimers;
|
|
5426
|
+
exports.context = context$1;
|
|
5427
|
+
exports.contextProperty = context;
|
|
3459
5428
|
exports.controller = controller;
|
|
5429
|
+
exports.css = css;
|
|
5430
|
+
exports.debounce = debounce;
|
|
3460
5431
|
exports.dispatch = dispatch;
|
|
3461
5432
|
exports.dispose = dispose;
|
|
3462
5433
|
exports.element = element;
|
|
3463
5434
|
exports.getSymbol = getSymbol;
|
|
5435
|
+
exports.html = html;
|
|
3464
5436
|
exports.layout = layout;
|
|
5437
|
+
exports.memoize = memoize;
|
|
3465
5438
|
exports.moved = moved;
|
|
5439
|
+
exports.nothing = nothing;
|
|
3466
5440
|
exports.observe = observe;
|
|
3467
5441
|
exports.on = on;
|
|
3468
|
-
exports.
|
|
5442
|
+
exports.once = once;
|
|
3469
5443
|
exports.property = property;
|
|
3470
5444
|
exports.query = query;
|
|
3471
5445
|
exports.queryAll = queryAll;
|
|
3472
5446
|
exports.ready = ready;
|
|
5447
|
+
exports.render = render;
|
|
3473
5448
|
exports.request = request;
|
|
5449
|
+
exports.resetOnce = resetOnce;
|
|
3474
5450
|
exports.respond = respond;
|
|
5451
|
+
exports.setDisableElementReadyWarnings = setDisableElementReadyWarnings;
|
|
5452
|
+
exports.styles = styles;
|
|
5453
|
+
exports.throttle = throttle;
|
|
5454
|
+
exports.trackRenders = trackRenders;
|
|
5455
|
+
exports.unsafeHTML = unsafeHTML;
|
|
3475
5456
|
exports.useNativeElementControllers = useNativeElementControllers;
|
|
5457
|
+
exports.waitForAllCustomElements = waitForAllCustomElements;
|
|
5458
|
+
exports.waitForElementDefined = waitForElementDefined;
|
|
5459
|
+
exports.waitForElementReady = waitForElementReady;
|
|
3476
5460
|
exports.watch = watch;
|
|
3477
5461
|
//# sourceMappingURL=index.cjs.map
|