-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprpl-interactive-task.js
More file actions
104 lines (89 loc) · 2.36 KB
/
Copy pathprpl-interactive-task.js
File metadata and controls
104 lines (89 loc) · 2.36 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
/* global HTMLElement, prplSuggestedTask */
/**
* Register the custom web component.
*/
// eslint-disable-next-line no-unused-vars
class PrplInteractiveTask extends HTMLElement {
// eslint-disable-next-line no-useless-constructor
constructor() {
// Get parent class properties
super();
}
/**
* Runs when the component is added to the DOM.
*/
connectedCallback() {
const popoverId = this.getAttribute( 'popover-id' );
// Add default event listeners.
this.attachDefaultEventListeners();
// Allow child components to add event listeners when the popover is added to the DOM.
this.popoverAddedToDOM();
// Add popover close event listener.
const popover = document.getElementById( popoverId );
popover.addEventListener( 'beforetoggle', ( event ) => {
if ( event.newState === 'open' ) {
this.popoverOpening();
}
if ( event.newState === 'closed' ) {
this.popoverClosing();
}
} );
}
/**
* Attach button event listeners.
* Every button with a data-action attribute will be handled by the component.
*/
attachDefaultEventListeners() {
// Add event listeners.
this.querySelectorAll( 'button' ).forEach( ( buttonElement ) => {
buttonElement.addEventListener( 'click', ( e ) => {
const button = e.target.closest( 'button' );
const action = button?.dataset.action;
if ( action && typeof this[ action ] === 'function' ) {
this[ action ]();
}
} );
} );
}
/**
* Runs when the popover is added to the DOM.
*/
popoverAddedToDOM() {}
/**
* Runs when the popover is opening.
*/
popoverOpening() {}
/**
* Runs when the popover is closing.
*/
popoverClosing() {}
/**
* Complete the task.
*/
completeTask() {
const providerId = this.getAttribute( 'provider-id' );
const tasks = document.querySelectorAll(
'#prpl-suggested-tasks-list .prpl-suggested-task'
);
tasks.forEach( ( taskElement ) => {
if ( taskElement.dataset.taskId === providerId ) {
// Close popover.
document
.getElementById( 'prpl-popover-' + providerId )
.hidePopover();
const postId = parseInt( taskElement.dataset.postId );
if ( postId ) {
prplSuggestedTask.maybeComplete( postId );
}
}
} );
}
/**
* Close the popover.
*/
closePopover() {
const popoverId = this.getAttribute( 'popover-id' );
const popover = document.getElementById( popoverId );
popover.hidePopover();
}
}