Skip to content

Commit 73450a8

Browse files
committed
Move JS code to parent class
1 parent 2566545 commit 73450a8

2 files changed

Lines changed: 43 additions & 47 deletions

File tree

assets/js/web-components/prpl-interactive-task.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class PrplInteractiveTask extends HTMLElement {
99
constructor() {
1010
// Get parent class properties
1111
super();
12+
13+
this.repositionPopover = this.repositionPopover.bind( this ); // So this is available in the event listener.
1214
}
1315

1416
/**
@@ -56,12 +58,16 @@ class PrplInteractiveTask extends HTMLElement {
5658
/**
5759
* Runs when the popover is added to the DOM.
5860
*/
59-
popoverAddedToDOM() {}
61+
popoverAddedToDOM() {
62+
window.addEventListener( 'resize', this.repositionPopover );
63+
}
6064

6165
/**
6266
* Runs when the popover is opening.
6367
*/
64-
popoverOpening() {}
68+
popoverOpening() {
69+
this.repositionPopover();
70+
}
6571

6672
/**
6773
* Runs when the popover is closing.
@@ -101,4 +107,38 @@ class PrplInteractiveTask extends HTMLElement {
101107
const popover = document.getElementById( popoverId );
102108
popover.hidePopover();
103109
}
110+
111+
/**
112+
* Repositions the popover relative to the target element.
113+
* @private
114+
*/
115+
repositionPopover() {
116+
const horizontalTarget = document.querySelector( '.prpl-wrap' );
117+
const verticalTarget = document.querySelector(
118+
'.prpl-widget-wrapper.prpl-suggested-tasks'
119+
);
120+
121+
// Just in case.
122+
if ( ! horizontalTarget || ! verticalTarget ) {
123+
return;
124+
}
125+
126+
const horizontalRect = horizontalTarget.getBoundingClientRect();
127+
const verticalRect = verticalTarget.getBoundingClientRect();
128+
const popoverId = this.getAttribute( 'popover-id' );
129+
const popover = document.getElementById( popoverId );
130+
131+
// Reset default popover styles.
132+
popover.style.margin = '0';
133+
134+
// Calculate target's center
135+
const horizontalTargetCenter =
136+
horizontalRect.left + horizontalRect.width / 2;
137+
138+
// Apply the position.
139+
popover.style.position = 'fixed';
140+
popover.style.left = `${ horizontalTargetCenter }px`;
141+
popover.style.top = `${ Math.round( Math.abs( verticalRect.top ) ) }px`;
142+
popover.style.transform = 'translateX(-50%)';
143+
}
104144
}

assets/js/web-components/prpl-task-sending-email.js

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,18 @@ customElements.define(
1616
constructor() {
1717
// Get parent class properties
1818
super();
19-
this.repositionPopover = this.repositionPopover.bind( this ); // So this is available in the event listener.
2019

2120
// First step.
2221
this.formStep = this.querySelector(
2322
'#prpl-sending-email-form-step'
2423
);
2524
}
2625

27-
/**
28-
* Repositions the popover relative to the target element.
29-
* @private
30-
*/
31-
repositionPopover() {
32-
const horizontalTarget = document.querySelector( '.prpl-wrap' );
33-
const verticalTarget = document.querySelector(
34-
'.prpl-widget-wrapper.prpl-suggested-tasks'
35-
);
36-
37-
// Just in case.
38-
if ( ! horizontalTarget || ! verticalTarget ) {
39-
return;
40-
}
41-
42-
const horizontalRect = horizontalTarget.getBoundingClientRect();
43-
const verticalRect = verticalTarget.getBoundingClientRect();
44-
const popoverId = this.getAttribute( 'popover-id' );
45-
const popover = document.getElementById( popoverId );
46-
47-
// Reset default popover styles.
48-
popover.style.margin = '0';
49-
50-
// Calculate target's center
51-
const horizontalTargetCenter =
52-
horizontalRect.left + horizontalRect.width / 2;
53-
54-
// Apply the position.
55-
popover.style.position = 'fixed';
56-
popover.style.left = `${ horizontalTargetCenter }px`;
57-
popover.style.top = `${ Math.round(
58-
Math.abs( verticalRect.top )
59-
) }px`;
60-
popover.style.transform = 'translateX(-50%)';
61-
}
62-
6326
/**
6427
* Runs when the popover is added to the DOM.
6528
*/
6629
popoverAddedToDOM() {
67-
window.addEventListener( 'resize', this.repositionPopover );
30+
super.popoverAddedToDOM();
6831

6932
// For the results step, add event listener to radio buttons.
7033
const nextButton = this.querySelector(
@@ -85,13 +48,6 @@ customElements.define(
8548
}
8649
}
8750

88-
/**
89-
* Runs when the popover is opening.
90-
*/
91-
popoverOpening() {
92-
this.repositionPopover();
93-
}
94-
9551
/**
9652
* Hide all steps.
9753
*/

0 commit comments

Comments
 (0)