@redvars/peacock 3.11.0 → 3.13.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/dist/cb-divider.js +7 -6
- package/dist/cb-divider.js.map +1 -1
- package/dist/cb-group-base-D9gv7SDY.js +114 -0
- package/dist/cb-group-base-D9gv7SDY.js.map +1 -0
- package/dist/cb-group-base-DsX_2aFl.js +325 -0
- package/dist/cb-group-base-DsX_2aFl.js.map +1 -0
- package/dist/cb-group.js +96 -0
- package/dist/cb-group.js.map +1 -0
- package/dist/cb-rule.js +212 -0
- package/dist/cb-rule.js.map +1 -0
- package/dist/condition-builder.js +90 -35
- package/dist/condition-builder.js.map +1 -1
- package/dist/custom-elements-jsdocs.json +176 -96
- package/dist/custom-elements.json +1348 -151
- package/dist/field.js +6 -5
- package/dist/field.js.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/loader.js +40 -22
- package/dist/loader.js.map +1 -1
- package/dist/src/LoaderUtils.d.ts +10 -2
- package/dist/src/condition-builder/cb-divider.d.ts +3 -1
- package/dist/src/condition-builder/cb-drag.d.ts +10 -0
- package/dist/src/condition-builder/cb-group-base.d.ts +59 -0
- package/dist/src/condition-builder/cb-group.d.ts +34 -0
- package/dist/src/condition-builder/cb-rule.d.ts +32 -0
- package/dist/src/condition-builder/cb-types.d.ts +13 -0
- package/dist/src/condition-builder/condition-builder.d.ts +35 -14
- package/dist/src/condition-builder/index.d.ts +3 -3
- package/dist/src/index.d.ts +2 -1
- package/dist/test/condition-builder.test.d.ts +1 -0
- package/dist/test/loader.test.d.ts +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/readme.md +2 -2
- package/src/LoaderUtils.ts +41 -13
- package/src/condition-builder/cb-divider.scss +4 -5
- package/src/condition-builder/cb-divider.ts +3 -1
- package/src/condition-builder/cb-drag.ts +35 -0
- package/src/condition-builder/cb-group-base.scss +97 -0
- package/src/condition-builder/cb-group-base.ts +335 -0
- package/src/condition-builder/cb-group.scss +21 -0
- package/src/condition-builder/cb-group.ts +72 -0
- package/src/condition-builder/cb-rule.scss +90 -0
- package/src/condition-builder/cb-rule.ts +129 -0
- package/src/condition-builder/cb-types.ts +16 -0
- package/src/condition-builder/condition-builder.scss +12 -9
- package/src/condition-builder/condition-builder.ts +77 -17
- package/src/condition-builder/index.ts +7 -3
- package/src/field/field.scss +11 -6
- package/src/index.ts +7 -3
- package/src/loader.ts +6 -13
- package/dist/cb-compound-expression.js +0 -133
- package/dist/cb-compound-expression.js.map +0 -1
- package/dist/cb-expression.js +0 -78
- package/dist/cb-expression.js.map +0 -1
- package/dist/cb-predicate.js +0 -141
- package/dist/cb-predicate.js.map +0 -1
- package/dist/src/condition-builder/cb-compound-expression.d.ts +0 -30
- package/dist/src/condition-builder/cb-expression.d.ts +0 -30
- package/dist/src/condition-builder/cb-predicate.d.ts +0 -29
- package/src/condition-builder/cb-compound-expression.scss +0 -41
- package/src/condition-builder/cb-compound-expression.ts +0 -79
- package/src/condition-builder/cb-expression.scss +0 -13
- package/src/condition-builder/cb-expression.ts +0 -48
- package/src/condition-builder/cb-predicate.scss +0 -35
- package/src/condition-builder/cb-predicate.ts +0 -101
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -44,9 +44,9 @@ Visit [https://peacock.redvars.com](https://peacock.redvars.com) to view the doc
|
|
|
44
44
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
45
45
|
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&family=Noto+Sans:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
|
|
46
46
|
|
|
47
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@redvars/peacock@3.
|
|
47
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@redvars/peacock@3.13.0/dist/assets/styles.css"></link>
|
|
48
48
|
<script type='module'
|
|
49
|
-
src='https://cdn.jsdelivr.net/npm/@redvars/peacock@3.
|
|
49
|
+
src='https://cdn.jsdelivr.net/npm/@redvars/peacock@3.13.0/dist/loader.js'></script>
|
|
50
50
|
</head>
|
|
51
51
|
|
|
52
52
|
<wc-button>Button</wc-button>
|
package/src/LoaderUtils.ts
CHANGED
|
@@ -10,10 +10,22 @@ interface LoaderConfig {
|
|
|
10
10
|
|
|
11
11
|
export { LoaderConfig };
|
|
12
12
|
|
|
13
|
+
type LoadRoot = Element | Document | DocumentFragment;
|
|
14
|
+
|
|
13
15
|
export class LoaderUtils {
|
|
14
16
|
private _loaderConfig: LoaderConfig;
|
|
15
17
|
|
|
16
|
-
private _observer
|
|
18
|
+
private _observer = new MutationObserver(mutations => {
|
|
19
|
+
for (const { addedNodes } of mutations) {
|
|
20
|
+
for (const node of addedNodes) {
|
|
21
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
22
|
+
this.load(node as Element);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
private _observed = new WeakSet<Node>();
|
|
17
29
|
|
|
18
30
|
constructor(loaderConfig: LoaderConfig) {
|
|
19
31
|
this._loaderConfig = loaderConfig;
|
|
@@ -26,10 +38,27 @@ export class LoaderUtils {
|
|
|
26
38
|
}
|
|
27
39
|
|
|
28
40
|
start() {
|
|
41
|
+
// Before the eager components are defined, so their shadow roots are watched too.
|
|
42
|
+
this.watchShadowRoots();
|
|
29
43
|
this.eagerLoadComponents();
|
|
30
44
|
this.lazyLoadComponents(document);
|
|
31
45
|
}
|
|
32
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Watches every shadow root attached from now on, as it is attached, so lazy components that a
|
|
49
|
+
* component renders in its shadow root load as they do in the document.
|
|
50
|
+
*/
|
|
51
|
+
watchShadowRoots() {
|
|
52
|
+
const watch = (shadowRoot: ShadowRoot) =>
|
|
53
|
+
this.lazyLoadComponents(shadowRoot);
|
|
54
|
+
const { attachShadow } = Element.prototype;
|
|
55
|
+
Element.prototype.attachShadow = function attachShadowAndWatch(init) {
|
|
56
|
+
const shadowRoot = attachShadow.call(this, init);
|
|
57
|
+
watch(shadowRoot);
|
|
58
|
+
return shadowRoot;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
33
62
|
eagerLoadComponents() {
|
|
34
63
|
if (!this._loaderConfig.components) return;
|
|
35
64
|
for (const [name, value] of Object.entries(this._loaderConfig.components)) {
|
|
@@ -72,7 +101,7 @@ export class LoaderUtils {
|
|
|
72
101
|
}
|
|
73
102
|
}
|
|
74
103
|
|
|
75
|
-
async load(root:
|
|
104
|
+
async load(root: LoadRoot): Promise<void> {
|
|
76
105
|
const rootTagName =
|
|
77
106
|
root instanceof Element ? root.tagName.toLowerCase() : '';
|
|
78
107
|
|
|
@@ -90,23 +119,22 @@ export class LoaderUtils {
|
|
|
90
119
|
);
|
|
91
120
|
}
|
|
92
121
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
for (const { addedNodes } of mutations) {
|
|
96
|
-
for (const node of addedNodes) {
|
|
97
|
-
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
98
|
-
this.load(node as Element);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
|
|
122
|
+
/** Loads the lazy components in `root` (a document or a shadow root) and those added later. */
|
|
123
|
+
lazyLoadComponents(root: Document | Element | ShadowRoot) {
|
|
104
124
|
const target = root instanceof Document ? root.documentElement : root;
|
|
125
|
+
if (this._observed.has(target)) return;
|
|
126
|
+
this._observed.add(target);
|
|
127
|
+
|
|
105
128
|
this.load(target);
|
|
106
129
|
|
|
107
130
|
this._observer.observe(target, {
|
|
108
131
|
subtree: true,
|
|
109
132
|
childList: true,
|
|
110
133
|
});
|
|
134
|
+
|
|
135
|
+
// Shadow roots attached before watchShadowRoots() ran; later ones are watched as attached.
|
|
136
|
+
for (const element of target.querySelectorAll('*')) {
|
|
137
|
+
if (element.shadowRoot) this.lazyLoadComponents(element.shadowRoot);
|
|
138
|
+
}
|
|
111
139
|
}
|
|
112
140
|
}
|
|
@@ -18,15 +18,14 @@
|
|
|
18
18
|
flex-direction: column;
|
|
19
19
|
align-items: center;
|
|
20
20
|
justify-content: center;
|
|
21
|
-
padding-bottom: var(--spacing-200);
|
|
22
21
|
|
|
23
22
|
.line {
|
|
24
23
|
height: 100%;
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
&.connect-start .line-start {
|
|
28
|
-
width: 1rem;
|
|
29
|
-
margin-left: 1rem;
|
|
27
|
+
width: var(--cb-divider-arm-length, 1rem);
|
|
28
|
+
margin-left: var(--cb-divider-arm-length, 1rem);
|
|
30
29
|
border-inline-start: 1px solid var(--cb-divider-line-color);
|
|
31
30
|
border-top: 1px solid var(--cb-divider-line-color);
|
|
32
31
|
border-radius: var(--shape-corner-extra-small) 0 0 0;
|
|
@@ -37,8 +36,8 @@
|
|
|
37
36
|
}
|
|
38
37
|
|
|
39
38
|
&.connect-end .line-end {
|
|
40
|
-
width: 1rem;
|
|
41
|
-
margin-left: 1rem;
|
|
39
|
+
width: var(--cb-divider-arm-length, 1rem);
|
|
40
|
+
margin-left: var(--cb-divider-arm-length, 1rem);
|
|
42
41
|
border-inline-start: 1px solid var(--cb-divider-line-color);
|
|
43
42
|
border-bottom: 1px solid var(--cb-divider-line-color);
|
|
44
43
|
border-radius: 0 0 0 var(--shape-corner-extra-small);
|
|
@@ -8,7 +8,9 @@ import styles from './cb-divider.scss';
|
|
|
8
8
|
* @label CB Divider
|
|
9
9
|
* @tag wc-cb-divider
|
|
10
10
|
* @rawTag cb-divider
|
|
11
|
-
* @summary A divider line used within a condition builder to visually connect
|
|
11
|
+
* @summary A divider line used within a condition builder to visually connect rules with an operator.
|
|
12
|
+
* @cssprop --cb-divider-line-color - Color of the divider line.
|
|
13
|
+
* @cssprop --cb-divider-arm-length - Length of the connecting arms drawn by `connect-start` and `connect-end`.
|
|
12
14
|
* @tags condition-builder
|
|
13
15
|
*
|
|
14
16
|
* @example
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { CbRule } from './cb-rule.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The rule currently being dragged. `DataTransfer` cannot carry element
|
|
5
|
+
* references, so rules publish themselves here on `dragstart` and groups read
|
|
6
|
+
* it on `dragover` / `drop`.
|
|
7
|
+
*/
|
|
8
|
+
let activeRule: CbRule | null = null;
|
|
9
|
+
|
|
10
|
+
/** Clears the drop indicator of the group the pointer is currently over. */
|
|
11
|
+
let clearDropTarget: (() => void) | null = null;
|
|
12
|
+
|
|
13
|
+
export function getActiveRule(): CbRule | null {
|
|
14
|
+
return activeRule;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function setActiveRule(rule: CbRule | null) {
|
|
18
|
+
activeRule = rule;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Makes `clear` the current drop target's cleanup, running the previous one
|
|
23
|
+
* so only one group shows a drop indicator at a time. Pass `null` to clear.
|
|
24
|
+
*/
|
|
25
|
+
export function claimDropTarget(clear: (() => void) | null) {
|
|
26
|
+
if (clearDropTarget && clearDropTarget !== clear) clearDropTarget();
|
|
27
|
+
clearDropTarget = clear;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Clears the drop target if `clear` still owns it. */
|
|
31
|
+
export function releaseDropTarget(clear: () => void) {
|
|
32
|
+
if (clearDropTarget !== clear) return;
|
|
33
|
+
clear();
|
|
34
|
+
clearDropTarget = null;
|
|
35
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
@use '../../scss/mixin';
|
|
2
|
+
|
|
3
|
+
@include mixin.base-styles;
|
|
4
|
+
|
|
5
|
+
:host {
|
|
6
|
+
display: block;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.group {
|
|
10
|
+
--_connector-width: 3.5rem;
|
|
11
|
+
|
|
12
|
+
@include mixin.for-phone-only {
|
|
13
|
+
--_connector-width: 3rem;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.body {
|
|
18
|
+
position: relative;
|
|
19
|
+
padding-inline-start: var(--_connector-width);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.connector {
|
|
23
|
+
position: absolute;
|
|
24
|
+
inset-inline-start: 0;
|
|
25
|
+
width: var(--_connector-width);
|
|
26
|
+
--cb-divider-arm-length: calc(var(--_connector-width) / 2);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.items {
|
|
30
|
+
position: relative;
|
|
31
|
+
display: flex;
|
|
32
|
+
flex-direction: column;
|
|
33
|
+
gap: var(--spacing-150);
|
|
34
|
+
|
|
35
|
+
&.empty {
|
|
36
|
+
min-height: var(--spacing-600);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.operator-pill {
|
|
41
|
+
@include mixin.reset-button-styles;
|
|
42
|
+
@include mixin.get-typography-not-important(label-medium);
|
|
43
|
+
position: relative;
|
|
44
|
+
display: inline-flex;
|
|
45
|
+
align-items: center;
|
|
46
|
+
justify-content: center;
|
|
47
|
+
gap: var(--spacing-025);
|
|
48
|
+
min-width: 3.25rem;
|
|
49
|
+
height: 1.5rem;
|
|
50
|
+
white-space: nowrap;
|
|
51
|
+
padding-inline: var(--spacing-100) var(--spacing-050);
|
|
52
|
+
border: 1px solid var(--color-primary);
|
|
53
|
+
border-radius: var(--shape-corner-full);
|
|
54
|
+
background: var(--color-primary-container);
|
|
55
|
+
color: var(--color-on-primary-container);
|
|
56
|
+
cursor: pointer;
|
|
57
|
+
|
|
58
|
+
// State layer
|
|
59
|
+
&::before {
|
|
60
|
+
content: '';
|
|
61
|
+
position: absolute;
|
|
62
|
+
inset: 0;
|
|
63
|
+
border-radius: inherit;
|
|
64
|
+
background: currentcolor;
|
|
65
|
+
opacity: 0;
|
|
66
|
+
transition: opacity var(--duration-short2, 100ms) linear;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
&:hover::before {
|
|
70
|
+
opacity: 0.08;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
&:active::before {
|
|
74
|
+
opacity: 0.12;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
wc-icon {
|
|
78
|
+
--icon-size: 1rem;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
wc-focus-ring {
|
|
82
|
+
--focus-ring-container-shape-start-start: var(--shape-corner-full);
|
|
83
|
+
--focus-ring-container-shape-start-end: var(--shape-corner-full);
|
|
84
|
+
--focus-ring-container-shape-end-start: var(--shape-corner-full);
|
|
85
|
+
--focus-ring-container-shape-end-end: var(--shape-corner-full);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.drop-indicator {
|
|
90
|
+
position: absolute;
|
|
91
|
+
inset-inline: 0;
|
|
92
|
+
height: 2px;
|
|
93
|
+
transform: translateY(-50%);
|
|
94
|
+
border-radius: var(--shape-corner-full);
|
|
95
|
+
background: var(--color-primary);
|
|
96
|
+
pointer-events: none;
|
|
97
|
+
}
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { html, LitElement, nothing, type TemplateResult } from 'lit';
|
|
2
|
+
import { property, query, state } from 'lit/decorators.js';
|
|
3
|
+
import { classMap } from 'lit/directives/class-map.js';
|
|
4
|
+
import { styleMap } from 'lit/directives/style-map.js';
|
|
5
|
+
import {
|
|
6
|
+
claimDropTarget,
|
|
7
|
+
getActiveRule,
|
|
8
|
+
releaseDropTarget,
|
|
9
|
+
} from './cb-drag.js';
|
|
10
|
+
import type { CbRule } from './cb-rule.js';
|
|
11
|
+
import type { CbOperator, CbRuleMoveDetail } from './cb-types.js';
|
|
12
|
+
|
|
13
|
+
const ITEM_SELECTOR = 'wc-cb-rule, wc-cb-group';
|
|
14
|
+
|
|
15
|
+
const OPERATOR_LABELS: Record<CbOperator, string> = { and: 'And', or: 'Or' };
|
|
16
|
+
|
|
17
|
+
type MenuElement = HTMLElement & {
|
|
18
|
+
anchorElement: HTMLElement | null;
|
|
19
|
+
show: () => void;
|
|
20
|
+
close: () => void;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Shared behaviour of `wc-condition-builder` and `wc-cb-group`: the And/Or
|
|
25
|
+
* operator pill, the bracket joining the items, and dropping dragged rules.
|
|
26
|
+
*/
|
|
27
|
+
export class CbGroupBase extends LitElement {
|
|
28
|
+
// ── Static ────────────────────────────────────────────────────────────────
|
|
29
|
+
|
|
30
|
+
/** Viewport Y of the middle of an item's first row, where the bracket meets it. */
|
|
31
|
+
private static _rowCenter(item: Element): number {
|
|
32
|
+
if (item instanceof CbGroupBase) return item.firstRowCenter();
|
|
33
|
+
const rect = item.getBoundingClientRect();
|
|
34
|
+
return rect.top + rect.height / 2;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ── Properties ────────────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
/** The logical operator joining the rules and groups in this group. */
|
|
40
|
+
@property({ type: String, reflect: true })
|
|
41
|
+
operator: CbOperator = 'and';
|
|
42
|
+
|
|
43
|
+
// ── State ─────────────────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
/** Number of rules and groups in the default slot. */
|
|
46
|
+
@state()
|
|
47
|
+
private _itemCount = 0;
|
|
48
|
+
|
|
49
|
+
/** Y of the drop indicator inside `.items`, or `null` when nothing is dragged over. */
|
|
50
|
+
@state()
|
|
51
|
+
private _dropIndicatorTop: number | null = null;
|
|
52
|
+
|
|
53
|
+
/** Whether the operator menu is open. */
|
|
54
|
+
@state()
|
|
55
|
+
private _menuOpen = false;
|
|
56
|
+
|
|
57
|
+
// ── Queries ───────────────────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
@query('.items')
|
|
60
|
+
private readonly _itemsEl?: HTMLElement;
|
|
61
|
+
|
|
62
|
+
@query('.connector')
|
|
63
|
+
private readonly _connectorEl?: HTMLElement;
|
|
64
|
+
|
|
65
|
+
@query('.operator-pill')
|
|
66
|
+
private readonly _pillEl?: HTMLElement;
|
|
67
|
+
|
|
68
|
+
@query('wc-menu')
|
|
69
|
+
private readonly _menuEl?: MenuElement;
|
|
70
|
+
|
|
71
|
+
// ── Private fields ────────────────────────────────────────────────────────
|
|
72
|
+
|
|
73
|
+
/** Id linking the operator pill to its menu. */
|
|
74
|
+
private readonly _menuId = `cb-operator-menu-${Math.random().toString(36).slice(2, 9)}`;
|
|
75
|
+
|
|
76
|
+
/** Re-measures the bracket when any item changes size. */
|
|
77
|
+
private readonly _resizeObserver = new ResizeObserver(() =>
|
|
78
|
+
this._layoutConnector(),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
/** Index the dragged rule would be inserted at, or -1. */
|
|
82
|
+
private _dropIndex = -1;
|
|
83
|
+
|
|
84
|
+
// ── Lifecycle ─────────────────────────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
override connectedCallback() {
|
|
87
|
+
super.connectedCallback();
|
|
88
|
+
this.items.forEach(item => this._resizeObserver.observe(item));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
override disconnectedCallback() {
|
|
92
|
+
this._resizeObserver.disconnect();
|
|
93
|
+
super.disconnectedCallback();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
override updated() {
|
|
97
|
+
this._layoutConnector();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ── Public methods ────────────────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
/** The rules and groups in this group, in order. */
|
|
103
|
+
get items(): HTMLElement[] {
|
|
104
|
+
return Array.from(this.children).filter(
|
|
105
|
+
(el): el is HTMLElement => el.matches(ITEM_SELECTOR) && !el.slot,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Moves `rule` into this group at `index` (counted without the rule itself).
|
|
111
|
+
* Fires a cancelable `rule-move` event first. Returns whether it moved.
|
|
112
|
+
*/
|
|
113
|
+
moveRule(rule: CbRule, index: number): boolean {
|
|
114
|
+
const others = this.items.filter(item => item !== rule);
|
|
115
|
+
const target = Math.max(0, Math.min(index, others.length));
|
|
116
|
+
if (rule.parentElement === this && this.items.indexOf(rule) === target) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const event = new CustomEvent<CbRuleMoveDetail>('rule-move', {
|
|
121
|
+
detail: { rule, from: rule.parentElement, to: this, index: target },
|
|
122
|
+
bubbles: true,
|
|
123
|
+
composed: true,
|
|
124
|
+
cancelable: true,
|
|
125
|
+
});
|
|
126
|
+
if (!this.dispatchEvent(event)) return false;
|
|
127
|
+
|
|
128
|
+
this.insertBefore(rule, others[target] ?? null);
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Viewport Y of the middle of this group's first row. A parent group aligns
|
|
134
|
+
* its bracket to it.
|
|
135
|
+
* @internal
|
|
136
|
+
*/
|
|
137
|
+
firstRowCenter(): number {
|
|
138
|
+
const [first] = this.items;
|
|
139
|
+
if (first) return CbGroupBase._rowCenter(first);
|
|
140
|
+
const rect = this.getBoundingClientRect();
|
|
141
|
+
return rect.top + rect.height / 2;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ── Private methods ───────────────────────────────────────────────────────
|
|
145
|
+
|
|
146
|
+
private _onSlotChange() {
|
|
147
|
+
const { items } = this;
|
|
148
|
+
this._itemCount = items.length;
|
|
149
|
+
this._resizeObserver.disconnect();
|
|
150
|
+
items.forEach(item => this._resizeObserver.observe(item));
|
|
151
|
+
this._layoutConnector();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Spans the bracket from the first to the last item's first row. */
|
|
155
|
+
private _layoutConnector() {
|
|
156
|
+
const { items } = this;
|
|
157
|
+
if (items.length < 2 || !this._itemsEl || !this._connectorEl) return;
|
|
158
|
+
|
|
159
|
+
const containerTop = this._itemsEl.getBoundingClientRect().top;
|
|
160
|
+
const top = CbGroupBase._rowCenter(items[0]) - containerTop;
|
|
161
|
+
const bottom =
|
|
162
|
+
CbGroupBase._rowCenter(items[items.length - 1]) - containerTop;
|
|
163
|
+
this._connectorEl.style.top = `${top}px`;
|
|
164
|
+
this._connectorEl.style.height = `${bottom - top}px`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private _onPillClick(event: MouseEvent) {
|
|
168
|
+
event.stopPropagation();
|
|
169
|
+
if (!this._menuEl || !this._pillEl) return;
|
|
170
|
+
|
|
171
|
+
if (this._menuOpen) {
|
|
172
|
+
this._menuEl.close();
|
|
173
|
+
} else {
|
|
174
|
+
this._menuEl.anchorElement = this._pillEl;
|
|
175
|
+
this._menuEl.show();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private _onMenuActivate(event: CustomEvent<{ item: { value: string } }>) {
|
|
180
|
+
const operator = event.detail.item.value as CbOperator;
|
|
181
|
+
if (operator === this.operator) return;
|
|
182
|
+
|
|
183
|
+
this.operator = operator;
|
|
184
|
+
this.dispatchEvent(
|
|
185
|
+
new CustomEvent<{ operator: CbOperator }>('operator-change', {
|
|
186
|
+
detail: { operator },
|
|
187
|
+
bubbles: true,
|
|
188
|
+
composed: true,
|
|
189
|
+
}),
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private readonly _clearDropIndicator = () => {
|
|
194
|
+
this._dropIndicatorTop = null;
|
|
195
|
+
this._dropIndex = -1;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
private _onDragOver(event: DragEvent) {
|
|
199
|
+
const rule = getActiveRule();
|
|
200
|
+
if (!rule || !this._itemsEl) return;
|
|
201
|
+
|
|
202
|
+
// Cancel both dragenter and dragover to accept the drop; the innermost
|
|
203
|
+
// group under the pointer takes it.
|
|
204
|
+
event.preventDefault();
|
|
205
|
+
event.stopPropagation();
|
|
206
|
+
const { dataTransfer } = event;
|
|
207
|
+
if (dataTransfer) dataTransfer.dropEffect = 'move';
|
|
208
|
+
claimDropTarget(this._clearDropIndicator);
|
|
209
|
+
|
|
210
|
+
const others = this.items.filter(item => item !== rule);
|
|
211
|
+
const index = others.findIndex(item => {
|
|
212
|
+
const rect = item.getBoundingClientRect();
|
|
213
|
+
return event.clientY < rect.top + rect.height / 2;
|
|
214
|
+
});
|
|
215
|
+
this._dropIndex = index === -1 ? others.length : index;
|
|
216
|
+
|
|
217
|
+
const containerTop = this._itemsEl.getBoundingClientRect().top;
|
|
218
|
+
const halfGap =
|
|
219
|
+
(parseFloat(getComputedStyle(this._itemsEl).rowGap) || 0) / 2;
|
|
220
|
+
const before = others[this._dropIndex];
|
|
221
|
+
const last = others[others.length - 1];
|
|
222
|
+
if (before) {
|
|
223
|
+
this._dropIndicatorTop =
|
|
224
|
+
before.getBoundingClientRect().top - containerTop - halfGap;
|
|
225
|
+
} else if (last) {
|
|
226
|
+
this._dropIndicatorTop =
|
|
227
|
+
last.getBoundingClientRect().bottom - containerTop + halfGap;
|
|
228
|
+
} else {
|
|
229
|
+
this._dropIndicatorTop = 0;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private _onDragLeave(event: DragEvent) {
|
|
234
|
+
const next = event.relatedTarget as Node | null;
|
|
235
|
+
if (next && (this.contains(next) || this.renderRoot.contains(next))) return;
|
|
236
|
+
releaseDropTarget(this._clearDropIndicator);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
private _onDrop(event: DragEvent) {
|
|
240
|
+
const rule = getActiveRule();
|
|
241
|
+
if (!rule || this._dropIndex < 0) return;
|
|
242
|
+
|
|
243
|
+
event.preventDefault();
|
|
244
|
+
event.stopPropagation();
|
|
245
|
+
const index = this._dropIndex;
|
|
246
|
+
claimDropTarget(null);
|
|
247
|
+
this.moveRule(rule, index);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ── Render helpers ────────────────────────────────────────────────────────
|
|
251
|
+
|
|
252
|
+
/** Content placed after the items; each group type renders its own actions. */
|
|
253
|
+
protected renderFooter(): TemplateResult | typeof nothing {
|
|
254
|
+
return nothing;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private _renderConnector() {
|
|
258
|
+
if (this._itemCount < 2) return nothing;
|
|
259
|
+
|
|
260
|
+
const label = OPERATOR_LABELS[this.operator] ?? this.operator;
|
|
261
|
+
return html`
|
|
262
|
+
<div class="connector">
|
|
263
|
+
<wc-cb-divider connect-start connect-end>
|
|
264
|
+
<button
|
|
265
|
+
class="operator-pill"
|
|
266
|
+
aria-label=${`Match conditions with ${label}`}
|
|
267
|
+
aria-haspopup="menu"
|
|
268
|
+
aria-expanded=${String(this._menuOpen)}
|
|
269
|
+
aria-controls=${this._menuId}
|
|
270
|
+
@click=${this._onPillClick}
|
|
271
|
+
>
|
|
272
|
+
<wc-focus-ring></wc-focus-ring>
|
|
273
|
+
<span class="operator-label">${label}</span>
|
|
274
|
+
<wc-icon name="arrow_drop_down" aria-hidden="true"></wc-icon>
|
|
275
|
+
</button>
|
|
276
|
+
</wc-cb-divider>
|
|
277
|
+
</div>
|
|
278
|
+
`;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private _renderOperatorMenu() {
|
|
282
|
+
return html`
|
|
283
|
+
<wc-menu
|
|
284
|
+
id=${this._menuId}
|
|
285
|
+
@opened=${() => {
|
|
286
|
+
this._menuOpen = true;
|
|
287
|
+
}}
|
|
288
|
+
@closed=${() => {
|
|
289
|
+
this._menuOpen = false;
|
|
290
|
+
}}
|
|
291
|
+
@menu-item-activate=${this._onMenuActivate}
|
|
292
|
+
>
|
|
293
|
+
${(Object.keys(OPERATOR_LABELS) as CbOperator[]).map(
|
|
294
|
+
operator => html`
|
|
295
|
+
<wc-menu-item
|
|
296
|
+
value=${operator}
|
|
297
|
+
?selected=${operator === this.operator}
|
|
298
|
+
>
|
|
299
|
+
${OPERATOR_LABELS[operator]}
|
|
300
|
+
</wc-menu-item>
|
|
301
|
+
`,
|
|
302
|
+
)}
|
|
303
|
+
</wc-menu>
|
|
304
|
+
`;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ── Render ────────────────────────────────────────────────────────────────
|
|
308
|
+
|
|
309
|
+
override render() {
|
|
310
|
+
return html`
|
|
311
|
+
<div
|
|
312
|
+
class="group"
|
|
313
|
+
@dragenter=${this._onDragOver}
|
|
314
|
+
@dragover=${this._onDragOver}
|
|
315
|
+
@dragleave=${this._onDragLeave}
|
|
316
|
+
@drop=${this._onDrop}
|
|
317
|
+
>
|
|
318
|
+
<div class="body">
|
|
319
|
+
${this._renderConnector()}
|
|
320
|
+
<div class=${classMap({ items: true, empty: this._itemCount === 0 })}>
|
|
321
|
+
<slot @slotchange=${this._onSlotChange}></slot>
|
|
322
|
+
${this._dropIndicatorTop === null
|
|
323
|
+
? nothing
|
|
324
|
+
: html`<div
|
|
325
|
+
class="drop-indicator"
|
|
326
|
+
style=${styleMap({ top: `${this._dropIndicatorTop}px` })}
|
|
327
|
+
></div>`}
|
|
328
|
+
</div>
|
|
329
|
+
</div>
|
|
330
|
+
${this.renderFooter()}
|
|
331
|
+
</div>
|
|
332
|
+
${this._renderOperatorMenu()}
|
|
333
|
+
`;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
@use '../../scss/mixin';
|
|
2
|
+
|
|
3
|
+
.group {
|
|
4
|
+
padding: var(--spacing-150);
|
|
5
|
+
border: 1px solid var(--color-outline-variant);
|
|
6
|
+
border-radius: var(--shape-corner-large);
|
|
7
|
+
background: var(--color-surface-container-lowest);
|
|
8
|
+
|
|
9
|
+
@include mixin.for-phone-only {
|
|
10
|
+
padding: var(--spacing-100);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.footer {
|
|
15
|
+
display: flex;
|
|
16
|
+
flex-wrap: wrap;
|
|
17
|
+
align-items: center;
|
|
18
|
+
gap: var(--spacing-100);
|
|
19
|
+
padding-top: var(--spacing-100);
|
|
20
|
+
padding-inline-start: var(--_connector-width);
|
|
21
|
+
}
|