PageSourceSearch

https://towndock.net/js/jquery.fancybox-thumbs.js

js towndock.net collected 2026-10-02 11:27:18 UTC 7,586 bytes, 335 lines download raw bytes

1/*!
2 * Thumbnail helper for fancyBox
3 * version: 2.0.0 (Tue, 29 Jan 2013)
4 * @requires fancyBox v3.0 or later
5 *
6 * Usage:
7 *     $(".fancybox").fancybox({
8 *         helpers : {
9 *             thumbs: {
10 *                 width  : 50,
11 *                 height : 50
12 *             }
13 *         }
14 *     });
15 *
16 */
17(function ($) {
18	//Shortcut for fancyBox object
19	var F = $.fancybox;
20
21	//Add helper object
22	F.helpers.thumbs = {
23		defaults : {
24			width    : 75,            // thumbnail width
25			height   : 50,            // thumbnail height
26			position : 'bottom',      // 'top' or 'bottom'
27			source : function() {}    // callback for setting cutom thumb url
28		},
29
30		list  : null,
31		items : null,
32		count : 0,
33
34		_create : function( obj ) {
35			var opts = this.opts,
36				str,
37				list;
38
39			str = '';
40
41			$.each(obj.group, function (i) {
42				str += '<li><a data-index="' + i + '" href="javascript:jQuery.fancybox.jumpto(' + i + ');"></a></li>';
43			});
44
45			this.list  = list = $('<ul>' + str + '</ul>');
46			this.items = list.children();
47			this.count = this.items.length;
48
49			this.wrap = $('<div id="fancybox-thumbs" class="' + opts.position + '"></div>')
50				.append(list)
51				.wrapInner('<div class="inner" />')
52				.wrapInner('<div class="outer" />')
53				.appendTo('body');
54
55			$('<a class="fancybox-thumb-prev" href="javascript:;"><span></span></a>')
56				.click( $.proxy(this.prev, this) )
57				.prependTo( this.wrap );
58
59			$('<a class="fancybox-thumb-next" href="javascript:;"><span></span></a>')
60				.click( $.proxy(this.next, this) )
61				.appendTo( this.wrap );
62
63			//Set dimensions and get initial width
64			list.find('a').width( opts.width ).height( opts.height );
65
66			this.width  = this.items.outerWidth(true);
67			this.height = this.items.outerHeight(true);
68
69			list.width( this.width * this.count ).height( this.height );
70		},
71
72		_loadPage : function() {
73			var that = this,
74				link,
75				item,
76				src;
77
78			var callback = function( href ) {
79				that._setThumb( link, href );
80			};
81
82			if (!this.list) {
83				return;
84			}
85
86			// Find next one that is not already loaded
87			link = this.list.find('a').slice( this.start, this.end + 1).not('.ready').first();
88
89			if (link && link.length) {
90				link.addClass('ready');
91
92				item = F.group[ link.data('index') ];
93				href = this._getThumb( item, callback );
94
95				if ($.type(href) === 'string') {
96					callback( href );
97
98				} else if (!href) {
99					this._loadPage();
100				}
101			}
102		},
103
104		_getThumb : function( item, callback ) {
105			var that = this,
106				href,
107				rez;
108
109			// First, call callback
110			href = this.opts.source( item, callback );
111
112			// Try to find thumbnail image from the link
113			if (!href && item.element) {
114				href = $(item.element).find('img').attr('src');
115			}
116
117			// Try to match youtube or vimeo
118			if (!href && (rez = item.href.match(/(youtube\.com|youtu\.be)\/(watch\?v=|v\/|u\/|embed\/?)?(videoseries\?list=(.*)|[\w-]{11}|\?listType=(.*)&list=(.*)).*/i))) {
119				href = 'http://img.youtube.com/vi/' + rez[ 3 ] + '/mqdefault.jpg';
120			}
121
122			if (!href && (rez = item.href.match(/(?:vimeo(?:pro)?.com)\/(?:[^\d]+)?(\d+)(?:.*)/))) {
123				$.getJSON('http://www.vimeo.com/api/v2/video/' + rez[ 1 ] + '.json?callback=?', {format: "json"}, function(data) {
124					callback( data[0].thumbnail_small );
125				});
126
127				return true;
128			}
129
130			// If not found and item type is image, then use link url instead
131			if (!href && item.type === 'image' && item.href) {
132				href = item.href;
133			}
134
135			return href;
136		},
137
138		_setThumb : function( link, thumbUrl ) {
139			var that = this;
140			var go   = function() {
141				// Start loading next thumb
142				that._loadPage();
143			}
144
145			if (!this.list) {
146				return;
147			}
148
149			$("<img />")
150				.load(function() {
151					var width       = this.width,
152						height      = this.height,
153						thumbWidth  = link.width(),
154						thumbHeight = link.height(),
155						widthRatio,
156						heightRatio;
157
158					if (!that.wrap || !width || !height) {
159						go();
160						return;
161					}
162
163					//Calculate thumbnail width/height and center it
164					widthRatio  = width  / thumbWidth;
165					heightRatio = height / thumbHeight;
166
167					if (widthRatio >= 1 && heightRatio >= 1) {
168						if (widthRatio > heightRatio) {
169							width  = width / heightRatio;
170							height = thumbHeight;
171
172						} else {
173							width  = thumbWidth;
174							height = height / widthRatio;
175						}
176					}
177
178					$(this).css({
179						width  : Math.floor(width),
180						height : Math.floor(height),
181						'margin-top'    : Math.floor(thumbHeight * 0.3 - height * 0.3 ),
182						'margin-left'   : Math.floor(thumbWidth  * 0.5 - width  * 0.5 )
183					})
184					.appendTo( link );
185
186					go();
187				})
188				.error( go )
189				.attr( 'src', thumbUrl );
190		},
191
192		_move : function( page ) {
193			var left  = 0,
194				speed = 400,
195				pages,
196				start,
197				end;
198
199			if (!this.wrap) {
200				return;
201			}
202
203			pages = Math.ceil( this.count / this.itemsMin );
204
205			if (page === undefined) {
206				page = Math.floor( F.current.index / this.itemsMin ) + 1;
207			}
208
209			$(".fancybox-thumb-prev, .fancybox-thumb-next").hide();
210
211			if ( pages < 2 ) {
212				$.extend(this, {
213					pages : pages,
214					page  : 1,
215					start : 0,
216					end   : this.count
217				});
218
219				this.list.stop(true).css({
220					'margin-left'  : 'auto',
221					'margin-right' : 'auto',
222					'left'         : 0
223				});
224
225				this._loadPage();
226
227				return;
228			}
229
230			if (page <= 1) {
231				page = 1;
232
233			} else {
234				$(".fancybox-thumb-prev").show();
235			}
236
237			if (page >= pages) {
238				page = pages;
239
240			} else {
241				$(".fancybox-thumb-next").show();
242			}
243
244			start = (page - 1) * this.itemsMin;
245			end   = (start + this.itemsMax) - 1;
246
247			left  = (this.width * this.itemsMin * (page - 1) * -1);
248
249			if (this.left === left) {
250				return;
251			}
252
253			$.extend(this, {
254				pages : pages,
255				page  : page,
256				start : start,
257				end   : end,
258				left  : left
259			});
260
261			this._loadPage();
262
263			this.list.stop(true).animate({'margin-left' : left + 'px'}, speed);
264		},
265
266		prev : function() {
267			this._move( this.page - 1 );
268		},
269
270		next : function() {
271			this._move( this.page + 1 );
272		},
273
274		afterLoad : function(opts, obj) {
275			var pos  = opts.position === 'bottom' ? 2 : 0;
276
277			//Remove self if gallery does not have at least two items
278			if (obj.group.length < 2) {
279				obj.helpers.thumbs = false;
280
281				return;
282			}
283
284			if (!this.wrap) {
285				this._create( obj );
286			}
287
288			if (opts.margin !== false) {
289				obj.margin[ pos ] = Math.max(((this.height) + 40), obj.margin[ pos ] );
290			}
291		},
292
293		beforeShow : function(opts, obj) {
294			if (this.items) {
295				this.items.removeClass('fancybox-thumb-active');
296
297				this.current = this.list.find("a[data-index='" + obj.index + "']").parent().addClass('fancybox-thumb-active');
298			}
299		},
300
301		onUpdate: function() {
302			if (!this.wrap) {
303				return;
304			}
305
306			this.wrap.width( F.getViewport().w );
307
308			this.view = this.list.parent().innerWidth();
309
310			this.itemsMin = Math.floor( this.view / this.width );
311			this.itemsMax = Math.ceil( this.view / this.width );
312
313			this._move();
314		},
315
316		beforeClose : function() {
317			// Remove thumbnails
318			if (this.wrap) {
319				this.wrap.stop(true).remove();
320			}
321
322			$.extend(this, {
323				pages : 0,
324				page  : 0,
325				start : 0,
326				end   : 0,
327				count : 0,
328				items : null,
329				left  : null,
330				wrap  : null,
331				list  : null
332			});
333		}
334	}
335}(jQuery));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.