File size: 1,384 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const openLink = ( href, tracksEvent ) => ( { type: 'OPEN_LINK', href, tracksEvent } );
const openSite = ( { siteId, href } ) => ( { type: 'OPEN_SITE', siteId, href } );
const openPost = ( siteId, postId, href ) => ( { type: 'OPEN_POST', siteId, postId, href } );
const openComment = ( { siteId, postId, href, commentId } ) => ( {
	type: 'OPEN_COMMENT',
	siteId,
	postId,
	href,
	commentId,
} );

export const interceptLinks = ( event ) => ( dispatch ) => {
	const { target } = event;

	if ( 'A' !== target.tagName && 'A' !== target.parentNode.tagName ) {
		return;
	}

	const node = 'A' === target.tagName ? target : target.parentNode;
	const { dataset = {}, href } = node;
	const { linkType, postId, siteId, commentId, tracksEvent } = dataset;

	if ( ! linkType ) {
		return;
	}

	// we don't want to interfere with the click
	// if the user has specifically overwritten the
	// normal behavior already by holding down
	// one of the modifier keys.
	if ( event.ctrlKey || event.metaKey ) {
		return;
	}

	event.stopPropagation();
	event.preventDefault();

	if ( 'post' === linkType ) {
		dispatch( openPost( siteId, postId, href ) );
	} else if ( 'comment' === linkType ) {
		dispatch( openComment( { siteId, postId, href, commentId } ) );
	} else if ( 'site' === linkType ) {
		dispatch( openSite( { siteId, href } ) );
	} else {
		dispatch( openLink( href, tracksEvent ) );
	}
};