Passed
Push — develop ( c6dd2a...1f8334 )
by Paul
03:38
created

Plugin.getTextForShare   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
;(function( window, document, undefined ) {
0 ignored issues
show
Unused Code introduced by
The parameter undefined is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
2
	"use strict";
3
4
	var Plugin = function( selector, options, photoswipeOptions )
5
	{
6
		this.galleries = castor._isString( selector ) ? document.querySelectorAll( selector ) : selector;
0 ignored issues
show
Bug introduced by
The variable castor seems to be never declared. If this is a global, consider adding a /** global: castor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
7
		if( this.galleries ) {
8
			this.imageSize = 'l';
9
			this.options = castor._extend( this.defaults, options );
10
			this.photoswipeOptions = photoswipeOptions;
11
			this.init();
12
		}
13
	}
14
15
	Plugin.prototype =
16
	{
17
		defaults: {
18
			imageSelector: '.gallery-image',
19
			captionSelector: 'figcaption',
20
			thumbnailSrcAttribute: 'data-src',
21
		},
22
23
		init: function()
24
		{
25
			this.parseHash( this.galleries );
26
			[].forEach.call( this.galleries, function( gallery, index ) {
27
				gallery.setAttribute( 'data-pswp-uid', index + 1 );
28
				gallery.addEventListener( 'click', this.onClick.bind( this ));
29
			}.bind( this ));
30
		},
31
32
		beforeResize: function()
33
		{
34
			var firstResize = true;
35
			var imageSrcWillChange;
36
			var realViewportWidth;
37
			var dpiRatio = window.devicePixelRatio ? window.devicePixelRatio : 1;
38
			realViewportWidth = this.gallery.viewportSize.x * Math.min( dpiRatio, 2.5 );
39
			if(( !this.gallery.likelyTouchDevice && realViewportWidth > 800 ) || realViewportWidth >= 1200 || screen.width > 1200 ) {
0 ignored issues
show
Bug introduced by
The variable screen seems to be never declared. If this is a global, consider adding a /** global: screen */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
				if( this.imageSize === 'm' ) {
41
					this.imageSize = 'l';
42
					imageSrcWillChange = true;
43
				}
44
			}
45
			else if( this.imageSize === 'l' ) {
46
				this.imageSize = 'm';
47
				imageSrcWillChange = true;
48
			}
49
			if( imageSrcWillChange && !firstResize ) {
50
				this.gallery.invalidateCurrItems();
51
			}
52
			if( firstResize ) {
53
				firstResize = false;
0 ignored issues
show
Unused Code introduced by
The assignment to variable firstResize seems to be never used. Consider removing it.
Loading history...
54
			}
55
			imageSrcWillChange = false;
0 ignored issues
show
Unused Code introduced by
The assignment to variable imageSrcWillChange seems to be never used. Consider removing it.
Loading history...
56
		},
57
58
		getTextForShare: function( shareButtonData ) {
0 ignored issues
show
Unused Code introduced by
The parameter shareButtonData is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
59
			var text = document.createElement( 'DIV' );
60
			text.innerHTML = this.gallery.currItem.title || '';
61
			return text.innerText;
62
		},
63
64
		gettingData: function( index, item )
65
		{
66
			item.h = item[this.imageSize].h;
67
			item.src = item[this.imageSize].src;
68
			item.w = item[this.imageSize].w;
69
		},
70
71
		getPhotoswipeOptions: function( index, galleryElement, disableAnimation, fromURL )
72
		{
73
			return castor._extend({
0 ignored issues
show
Bug introduced by
The variable castor seems to be never declared. If this is a global, consider adding a /** global: castor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
74
				galleryUID: galleryElement.getAttribute( 'data-pswp-uid' ),
75
				getThumbBoundsFn: function( index ) {
76
					var rect = galleryElement.children[index].children[0].getBoundingClientRect();
77
					return {
78
						x: rect.left,
79
						y: rect.top + window.pageYOffset,
80
						w: rect.width,
81
					};
82
				},
83
				clickToCloseNonZoomable: false,
84
				shareButtons: [
85
					{id:'facebook', label:'Share on Facebook', url:'https://www.facebook.com/sharer/sharer.php?u={{url}}&picture={{image_url}}&description={{text}}'},
86
					{id:'twitter', label:'Tweet', url:'https://twitter.com/intent/tweet?text={{text}}&url={{url}}'},
87
					{id:'pinterest', label:'Pin it', url:'http://www.pinterest.com/pin/create/button/?url={{url}}&media={{image_url}}&description={{text}}'},
88
				],
89
				index: fromURL ? parseInt( index, 10 )-1 : parseInt( index, 10 ),
90
				showAnimationDuration: disableAnimation ? 0 : 1,
91
			}, this.photoswipeOptions );
92
		},
93
94
		onClick: function( ev )
95
		{
96
			var clickedListItem = ev.target.closest( this.options.imageSelector );
97
			if( !clickedListItem )return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
98
			var clickedGallery = clickedListItem.parentNode;
99
			[].some.call( clickedGallery.children, function( el, index ) {
100
				if( clickedListItem === el ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if clickedListItem === el is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
101
					this.open( index, clickedGallery );
102
					return true;
103
				}
104
			}.bind( this ));
105
			ev.preventDefault();
106
		},
107
108
		open: function( index, galleryElement, disableAnimation, fromURL )
109
		{
110
			var options = this.getPhotoswipeOptions( index, galleryElement, disableAnimation, fromURL );
111
112
			// exit if index not found
113
			if( isNaN( options.index ))return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
114
115
			// Initialize PhotoSwipe
116
			this.gallery = new PhotoSwipe(
0 ignored issues
show
Bug introduced by
The variable PhotoSwipe seems to be never declared. If this is a global, consider adding a /** global: PhotoSwipe */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
117
				document.querySelectorAll('.pswp')[0],
118
				PhotoSwipeUI_Default,
0 ignored issues
show
Bug introduced by
The variable PhotoSwipeUI_Default seems to be never declared. If this is a global, consider adding a /** global: PhotoSwipeUI_Default */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
119
				this.parseThumbnails( galleryElement ),
120
				options
121
			);
122
123
			this.gallery.options.getTextForShare = this.getTextForShare.bind( this );
124
125
			this.gallery.listen( 'beforeResize', this.beforeResize.bind( this ));
126
			this.gallery.listen( 'gettingData', this.gettingData.bind( this ));
127
			this.gallery.init();
128
		},
129
130
		parseHash: function( galleryElements )
131
		{
132
			var hash = window.location.hash.substring(1);
133
			var params = {};
134
			if( hash.length < 5 )return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
135
			hash.split( '&' ).forEach( function( item, index ) {
0 ignored issues
show
Unused Code introduced by
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
136
				item = item.split( '=' );
137
				if( item.length === 2 ) {
138
					params[item[0]] = (item[0] === 'gid') ? parseInt( item[1], 10 ) : item[1];
139
				}
140
			});
141
			if( params.pid && params.gid ) {
142
				this.open( params.pid,  galleryElements[params.gid - 1], true, true );
143
			}
144
		},
145
146
		parseThumbnails: function( galleryElement )
147
		{
148
			var items = [];
149
			[].forEach.call( galleryElement.children, function( el, index ) {
0 ignored issues
show
Unused Code introduced by
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
150
				var img = el.querySelector( 'img' );
151
				var caption = el.querySelector( this.options.captionSelector );
152
				var data = JSON.parse( el.getAttribute( 'data-ps' ));
153
				var item = data.l;
154
				item.l = data.l;
155
				item.m = data.m;
156
				if( img ) {
157
					item.msrc = img.getAttribute( this.options.thumbnailSrcAttribute ); // thumbnail url
158
				}
159
				if( caption ) {
160
					item.title = caption.innerHTML;
161
				}
162
				items.push( item );
163
			}.bind( this ));
164
			return items;
165
		},
166
	};
167
168
	Plugin.defaults = Plugin.prototype.defaults;
169
170
	castor.PhotoSwipe = Plugin;
0 ignored issues
show
Bug introduced by
The variable castor seems to be never declared. If this is a global, consider adding a /** global: castor */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
171
172
})( window, document );
173