Conditions | 1 |
Paths | 512 |
Total Lines | 172 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | ;(function( window, document, undefined ) { |
||
|
|||
2 | "use strict"; |
||
3 | |||
4 | var Plugin = function( selector, options, photoswipeOptions ) |
||
5 | { |
||
6 | this.galleries = castor._isString( selector ) ? document.querySelectorAll( selector ) : selector; |
||
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 ) { |
||
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; |
||
54 | } |
||
55 | imageSrcWillChange = false; |
||
56 | }, |
||
57 | |||
58 | getTextForShare: function( shareButtonData ) { |
||
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({ |
||
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; |
||
98 | var clickedGallery = clickedListItem.parentNode; |
||
99 | [].some.call( clickedGallery.children, function( el, index ) { |
||
100 | if( clickedListItem === el ) { |
||
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; |
||
114 | |||
115 | // Initialize PhotoSwipe |
||
116 | this.gallery = new PhotoSwipe( |
||
117 | document.querySelectorAll('.pswp')[0], |
||
118 | PhotoSwipeUI_Default, |
||
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; |
||
135 | hash.split( '&' ).forEach( function( item, index ) { |
||
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 ) { |
||
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; |
||
171 | |||
172 | })( window, document ); |
||
173 |
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.