-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathpopover-inline.ts
More file actions
177 lines (158 loc) · 4.89 KB
/
popover-inline.ts
File metadata and controls
177 lines (158 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { isMobileScreen } from '../../utils';
import type { PopoverItem } from './components/popover-item';
import { PopoverItemDefault, PopoverItemType } from './components/popover-item';
import { PopoverItemHtml } from './components/popover-item/popover-item-html/popover-item-html';
import { PopoverDesktop } from './popover-desktop';
import { CSSVariables, css } from './popover.const';
import type { PopoverParams } from '@/types/utils/popover/popover';
/**
* Horizontal popover that is displayed inline with the content
*/
export class PopoverInline extends PopoverDesktop {
/**
* Constructs the instance
*
* @param params - instance parameters
*/
constructor(params: PopoverParams) {
const isHintEnabled = !isMobileScreen();
super(
{
...params,
class: css.popoverInline,
},
{
[PopoverItemType.Default]: {
/**
* We use button instead of div here to fix bug associated with focus loss (which leads to selection change) on click in safari
*
* @todo figure out better way to solve the issue
*/
wrapperTag: 'button',
hint: {
position: 'top',
alignment: 'center',
enabled: isHintEnabled,
},
},
[PopoverItemType.Html]: {
hint: {
position: 'top',
alignment: 'center',
enabled: isHintEnabled,
},
},
}
);
/**
* If active popover item has children, show them.
* This is needed to display link url text (which is displayed as a nested popover content)
* once you select <a> tag content in text
*/
this.items
.forEach((item) => {
if (!(item instanceof PopoverItemDefault) && !(item instanceof PopoverItemHtml)) {
return;
}
if (item.hasChildren && item.isChildrenOpen) {
this.showNestedItems(item);
}
});
}
/**
* Returns visible element offset top
*/
public get offsetLeft(): number {
if (this.nodes.popoverContainer === null) {
return 0;
}
return this.nodes.popoverContainer.offsetLeft;
}
/**
* Open popover
*/
public override show(): void {
/**
* If this is not a nested popover, set CSS variable with width of the popover
*/
if (this.nestingLevel === 0) {
this.nodes.popover.style.setProperty(
CSSVariables.InlinePopoverWidth,
this.size.width + 'px'
);
}
super.show();
}
/**
* Disable hover event handling.
* Overrides parent's class behavior
*/
protected override handleHover(): void {
return;
}
/**
* Sets CSS variable with position of item near which nested popover should be displayed.
* Is used to position nested popover right below clicked item
*
* @param nestedPopoverEl - nested popover element
* @param item – item near which nested popover should be displayed
*/
protected override setTriggerItemPosition(
nestedPopoverEl: HTMLElement,
item: PopoverItemDefault
): void {
const itemEl = item.getElement();
const itemOffsetLeft = itemEl ? itemEl.offsetLeft : 0;
const totalLeftOffset = this.offsetLeft + itemOffsetLeft;
nestedPopoverEl.style.setProperty(
CSSVariables.TriggerItemLeft,
totalLeftOffset + 'px'
);
}
/**
* Handles displaying nested items for the item.
* Overriding in order to add toggling behaviour
*
* @param item – item to toggle nested popover for
*/
protected override showNestedItems(item: PopoverItemDefault | PopoverItemHtml): void {
if (this.nestedPopoverTriggerItem === item) {
this.destroyNestedPopoverIfExists();
this.nestedPopoverTriggerItem = null;
return;
}
super.showNestedItems(item);
}
/**
* Creates and displays nested popover for specified item.
* Is used only on desktop
*
* @param item - item to display nested popover by
*/
protected showNestedPopoverForItem(item: PopoverItem): PopoverDesktop {
const nestedPopover = super.showNestedPopoverForItem(item);
const nestedPopoverEl = nestedPopover.getElement();
/**
* We need to add class with nesting level, shich will help position nested popover.
* Currently only '.ce-popover--nested-level-1' class is used
*/
nestedPopoverEl.classList.add(css.getPopoverNestedClass(nestedPopover.nestingLevel));
return nestedPopover;
}
/**
* Overrides default item click handling.
* Helps to close nested popover once other item is clicked.
*
* @param item - clicked item
*/
protected override handleItemClick(item: PopoverItem): void {
if (item !== this.nestedPopoverTriggerItem) {
/**
* Close the nested popover without triggering the tool's action.
* The onChildrenClose callback will handle any necessary UI cleanup.
*/
super.destroyNestedPopoverIfExists();
}
super.handleItemClick(item);
}
}