This repository was archived by the owner on Jun 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathwpFeatherlight.js
More file actions
147 lines (124 loc) · 3.32 KB
/
Copy pathwpFeatherlight.js
File metadata and controls
147 lines (124 loc) · 3.32 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
/**
* WP Featherlight - Loader and helpers for the Featherlight WordPress plugin
*
* @copyright Copyright (c) 2018, Cipher Development, LLC
* @license MIT
*/
(function( window, $, undefined ) {
'use strict';
var $body = $( document.body );
/**
* Checks href targets to see if a given anchor is linking to an image.
*
* @since 0.1.0
* @return mixed
*/
function testImages( index, element ) {
return /(.png|.jpg|.jpeg|.webp|.gif|.tiff|.bmp)$/.test(
$( element ).attr( 'href' ).toLowerCase().split( '?' )[0].split( '#' )[0]
);
}
/**
* Filters all href elements on a page to add Featherlight's data attribute.
* When a match is found, the data attribute is added so Featherlight will
* open it normally.
*
* @since 0.1.0
* @return void
*/
function findImages() {
$body.find( 'a[href]' ).filter( testImages ).attr( 'data-featherlight', 'image' );
}
/**
* Callback function to initialize Featherlight galleries when they contain
* items that are able to be opened in a light box.
*
* @since 0.1.0
* @return void
*/
function buildGalleries( index, element ) {
var $galleryObj = $( element ),
$galleryItems = $galleryObj.find( 'a[data-featherlight]' );
if ( $galleryItems.attr( 'data-featherlight' ) ) {
$galleryItems.featherlightGallery({
previousIcon: '',
nextIcon: ''
});
}
}
/**
* Finds and creates Featherlight galleries for WordPress image galleries.
*
* @since 0.1.0
* @return void
*/
function findGalleries() {
var $gallery = $body.find( '[class*="gallery"]' );
if ( 0 !== $gallery.length ) {
$.each( $gallery, buildGalleries );
}
}
/**
* Attempt to Find image captions using common WordPress caption markup.
*
* @since 1.3.0
* @return void
*/
function findCaption( target ) {
var caption = target.parent().find( '.wp-caption-text' );
if ( 0 !== caption.length ) {
return caption;
}
var gutCaption = target.parent().find( 'figcaption' );
if ( 0 !== gutCaption.length ) {
return gutCaption;
}
var galParent = target.parents( '.gallery-item' );
if ( 0 !== galParent.length ) {
return galParent.find( '.wp-caption-text' );
}
var gutParent = target.parents( '.blocks-gallery-item' );
if ( 0 !== gutParent.length ) {
return gutParent.find( 'figcaption' );
}
var jetParent = target.parents( '.tiled-gallery-item' );
if ( 0 !== jetParent.length ) {
return jetParent.find( '.tiled-gallery-caption' );
}
return '';
}
/**
* Append image captions to the Featherlight content <div>.
*
* @since 0.3.0
* @return void
*/
function addCaptions() {
$.featherlight.prototype.afterContent = function() {
var object = this.$instance,
caption = findCaption( this.$currentTarget );
object.find( '.caption' ).remove();
if ( 0 !== caption.length ) {
var $captionElm = $( '<div class="caption">' ).appendTo( object.find( '.featherlight-content' ) );
$captionElm[0].innerHTML = caption.html();
}
};
}
/**
* Fires all of our helper methods to load featherlight.
*
* @since 0.1.0
* @return void
*/
function wpFeatherlightInit() {
$.featherlight.defaults.closeIcon = '';
findImages();
findGalleries();
if ( $body.hasClass( 'wp-featherlight-captions' ) ) {
addCaptions();
}
}
$( document ).ready(function() {
wpFeatherlightInit();
});
})( this, jQuery );