-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (129 loc) · 4.23 KB
/
index.js
File metadata and controls
141 lines (129 loc) · 4.23 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
/* globals newspack_ras_config */
/**
* Internal dependencies
*/
import { SIGN_IN_MODAL_HASHES, getModalContainer, openAuthModal } from './auth-modal.js';
import { domReady } from '../utils';
import './auth-form.js';
window.newspackRAS = window.newspackRAS || [];
window.newspackRAS.push( readerActivation => {
domReady( function () {
/** Expose the openAuthModal function to the RAS scope */
readerActivation._openAuthModal = openAuthModal;
/**
* Handle hash change.
*
* @param {Event} ev Hash change event.
*/
function handleHashChange( ev ) {
const container = getModalContainer();
if ( ! container ) {
return;
}
const currentHash = window.location.hash.replace( '#', '' );
if ( SIGN_IN_MODAL_HASHES.includes( currentHash ) ) {
if ( ev ) {
ev.preventDefault();
}
container.setFormAction( currentHash === 'register_modal' ? 'register' : 'signin' );
openAuthModal( { closeOnSuccess: true } );
}
}
window.addEventListener( 'hashchange', handleHashChange );
handleHashChange();
/**
* Handle account link click.
*
* @param {Event} ev Click event.
*/
function handleAccountLinkClick( ev ) {
ev.preventDefault();
const modalTrigger = ev.target;
let callback, redirect;
if ( ev.target.getAttribute( 'data-redirect' ) ) {
redirect = ev.target.getAttribute( 'data-redirect' );
} else {
redirect = ev.target.getAttribute( 'href' );
}
if ( ! redirect ) {
const closestEl = ev.target.closest( 'a' );
if ( closestEl ) {
if ( closestEl.getAttribute( 'data-redirect' ) ) {
redirect = closestEl.getAttribute( 'data-redirect' );
} else {
redirect = closestEl.getAttribute( 'href' );
}
}
}
if ( redirect && redirect !== '#' ) {
callback = () => {
window.location.href = redirect;
};
}
openAuthModal( {
onSuccess: callback,
onError: callback,
trigger: modalTrigger,
closeOnSuccess: true,
} );
}
/**
* Initialize trigger links.
*/
function initializeTriggerLinks() {
const triggerLinks = document.querySelectorAll(
// The href selector excludes the My Account Button block.
`[data-newspack-reader-account-link],[href="${ newspack_ras_config.account_url }"]:not(.wp-block-newspack-my-account-button)`
);
triggerLinks.forEach( link => {
link.addEventListener( 'click', handleAccountLinkClick );
} );
}
initializeTriggerLinks();
/** Re-initialize links in case the navigation DOM was modified by a third-party. */
setTimeout( initializeTriggerLinks, 1000 );
/**
* Handle reader changes.
*/
function handleReaderChanges() {
const reader = window.newspackReaderActivation.getReader();
const accountLinks = document.querySelectorAll( '.newspack-reader__account-link' );
if ( accountLinks?.length ) {
accountLinks.forEach( link => {
const labels = JSON.parse( link.getAttribute( 'data-labels' ) );
const labelEl = link.querySelector( '.newspack-reader__account-link__label' );
if ( labelEl ) {
const isLoggedIn = link.getAttribute( 'data-wp-logged-in' ) === '1';
if ( isLoggedIn ) {
labelEl.textContent = labels.signedin;
return;
}
labelEl.textContent = reader?.authenticated ? labels.signedin : labels.signedout;
// Set my account link href if the reader is authenticated.
if ( reader?.authenticated ) {
link.setAttribute( 'href', newspack_ras_config.account_url );
}
}
} );
}
}
window.newspackReaderActivation.on( 'reader', handleReaderChanges );
handleReaderChanges();
} );
/**
* Detect a reader login via magic link token.
*/
const queryString = window.location.search;
const params = new URLSearchParams( queryString );
const reader = readerActivation.getReader();
if ( params.get( newspack_ras_config?.auth_action_result ) && reader?.email && reader?.authenticated ) {
// Remove the auth action result from the URL.
params.delete( newspack_ras_config?.auth_action_result );
const newQueryString = params.toString() ? '?' + params.toString() : '';
window.history.replaceState( {}, '', window.location.pathname + newQueryString );
readerActivation.dispatchActivity( 'reader_logged_in', {
email: reader.email,
login_method: 'auth-token',
} );
}
} );