-
Notifications
You must be signed in to change notification settings - Fork 100
/
jquery.photoset-grid.js
511 lines (429 loc) · 16.9 KB
/
jquery.photoset-grid.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/**
* photoset-grid - v1.0.1
* 2014-04-08
* jQuery plugin to arrange images into a flexible grid
* http://stylehatch.github.com/photoset-grid/
*
* Copyright 2014 Jonathan Moore - Style Hatch
*/
/*jshint browser: true, curly: true, eqeqeq: true, forin: false, immed: false, newcap: true, noempty: true, strict: true, undef: true, devel: true */
;(function ( $, window, document, undefined ) {
'use strict';
// Plugin name and default settings
var pluginName = "photosetGrid",
defaults = {
// Required
// set the width of the container
width : '100%',
// the space between the rows / columns
gutter : '0px',
// Optional
// wrap the images in a vs. div and link to the data-highres images
highresLinks : false,
// threshold for the lowres image, if container is > swap the data-highres
lowresWidth : 500,
// relational attr to apply to the links for lightbox use
rel : '',
// add a border to each image
borderActive: false,
// set border width
borderWidth: '5px',
// set border color
borderColor: '#000000',
// set border radius
borderRadius: '0',
// if true it will remove "double" borders
borderRemoveDouble: false,
// Call back events
onInit : function(){},
onComplete : function(){}
};
// Plugin constructor
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
// Call the optional onInit event set when the plugin is called
this.options.onInit();
this._setupRows(this.element, this.options);
this._setupColumns(this.element, this.options);
},
_callback: function(elem){
// Call the optional onComplete event after the plugin has been completed
this.options.onComplete(elem);
},
_setupRows: function( elem, options ){
// Convert the layout string into an array to build the DOM structures
if(options.layout) {
// Check for layout defined in plugin call
this.layout = options.layout;
} else if($(elem).attr('data-layout')) {
// If not defined in the options, check for the data-layout attr
this.layout = $(elem).attr('data-layout');
} else {
// Otherwise give it a stacked layout (no grids for you)
// Generate a layout string of all ones based on the number of images
var stackedLayout = "";
var defaultColumns = 1;
for (var imgs=0; imgs<$(elem).find('img').length; imgs++ ) {
stackedLayout = stackedLayout + defaultColumns.toString();
}
this.layout = stackedLayout;
}
// Dump the layout into a rows array
// Convert the array into all numbers vs. strings
this.rows = this.layout.split('');
for (var i in this.rows ) {
this.rows[i] = parseInt(this.rows[i], 10);
}
var $images = $(elem).find('img');
var imageIndex = 0;
$.each(this.rows, function(i, val){
var rowStart = imageIndex;
var rowEnd = imageIndex + val;
// Wrap each set of images in a row into a container div
$images.slice(rowStart, rowEnd).wrapAll('<div class="photoset-row cols-' + val + '"></div>');
imageIndex = rowEnd;
});
$(elem).find('.photoset-row:not(:last-child)').css({
'margin-bottom': options.gutter
});
},
_setupColumns: function( elem, options ){
// Reference to this Plugin
var $this = this;
var setupStyles = function(waitForImagesLoaded){
var $rows = $(elem).find('.photoset-row');
var $images = $(elem).find('img');
// Wrap the images in links to the highres or regular image
// Otherwise wrap in div.photoset-cell
if(options.highresLinks){
$images.each(function(){
var title;
// If the image has a title pass it on
if($(this).attr('title')){
title = ' title="' + $(this).attr('title') + '"';
} else {
title = '';
}
var highres;
// If a highres image exists link it up!
if($(this).attr('data-highres')){
highres = $(this).attr('data-highres');
} else {
highres = $(this).attr('src');
}
$(this).wrapAll('<a href="' + highres + '"' + title + ' class="photoset-cell highres-link" />');
if(options.borderActive){
$(this).wrapAll('<span class="photoset-content-border" />');
}
});
// Apply the optional rel
if(options.rel){
$images.parent().attr('rel', options.rel);
}
} else {
$images.each(function(){
if(options.borderActive){
$(this).wrapAll('<div class="photoset-cell photoset-cell--border" />');
$(this).wrapAll('<div class="photoset-content-border" />');
} else {
$(this).wrapAll('<div class="photoset-cell" />');
}
});
}
var $cells = $(elem).find('.photoset-cell');
var $cols1 = $(elem).find('.cols-1 .photoset-cell');
var $cols2 = $(elem).find('.cols-2 .photoset-cell');
var $cols3 = $(elem).find('.cols-3 .photoset-cell');
var $cols4 = $(elem).find('.cols-4 .photoset-cell');
var $cols5 = $(elem).find('.cols-5 .photoset-cell');
var $cellBorder = $(elem).find('.photoset-content-border');
// Apply styles initial structure styles to the grid
$(elem).css({
'width': options.width
});
$rows.css({
'clear': 'left',
'display': 'block',
'overflow': 'hidden'
});
$cells.css({
'float': 'left',
'display': 'block',
'line-height': '0',
'-webkit-box-sizing': 'border-box',
'-moz-box-sizing': 'border-box',
'box-sizing': 'border-box'
});
$images.css({
'width': '100%',
'height': 'auto'
});
if(options.borderActive){
$cellBorder.css({
'display': 'block',
'border': options.borderWidth + ' solid ' + options.borderColor,
'border-radius': options.borderRadius,
'overflow': 'hidden',
'-webkit-box-sizing': 'border-box',
'-moz-box-sizing': 'border-box',
'box-sizing': 'border-box'
});
}
// if the imaged did not have height/width attr set them
if (waitForImagesLoaded) {
$images.each(function(){
$(this).attr('height', $(this).height());
$(this).attr('width', $(this).width());
});
}
// Set the width of the cells based on the number of columns in the row
$cols1.css({ 'width': '100%' });
$cols2.css({ 'width': '50%' });
$cols3.css({ 'width': '33.3%' });
$cols4.css({ 'width': '25%' });
$cols5.css({ 'width': '20%' });
var gutterVal = parseInt(options.gutter, 10);
// Apply 50% gutter to left and right
// this provides equal gutters a high values
$(elem).find('.photoset-cell:not(:last-child)').css({
'padding-right': (gutterVal / 2) + 'px'
});
$(elem).find('.photoset-cell:not(:first-child)').css({
'padding-left': (gutterVal / 2) + 'px'
});
// If 'borderRemoveDouble' is true, let us remove the extra gutter border
if(options.borderRemoveDouble){
$(elem).find('.photoset-row').not(':eq(0)').find('.photoset-content-border').css({'border-top': 'none'});
$(elem).find('.photoset-row').not('.cols-1').find('.photoset-content-border').not(":eq(0)").css({'border-left': 'none'});
}
function resizePhotosetGrid(){
// Give the values a floor to prevent misfires
var w = $(elem).width().toString();
if( w !== $(elem).attr('data-width') ) {
$rows.each(function(){
var $shortestImg = $(this).find('img:eq(0)');
$(this).find('img').each(function(){
var $img = $(this);
if( parseInt($img.attr('height'), 10) < parseInt($shortestImg.attr('height'),10) ){
$shortestImg = $(this);
}
if(parseInt($img.css('width'), 10) > options.lowresWidth && $img.attr('data-highres')){
$img.attr('src', $img.attr('data-highres'));
}
});
// Get the row height from the calculated/real height/width of the shortest image
var rowHeight = ( $shortestImg.attr('height') * parseInt($shortestImg.css('width'), 10) ) / $shortestImg.attr('width');
// Adding a buffer to shave off a few pixels in height
var bufferHeight = Math.floor(rowHeight * 0.025);
$(this).height( rowHeight - bufferHeight );
// If border is set to true, then add the parent row height to each .photoset-content-border
if(options.borderActive){
$(this).find('.photoset-content-border').each(function(){
$(this).css({'height': rowHeight - bufferHeight});
});
}
$(this).find('img').each(function(){
// Get the image height from the calculated/real height/width
var imageHeight = ( $(this).attr('height') * parseInt($(this).css('width'), 10) ) / $(this).attr('width');
var marginOffset = Math.floor( (rowHeight - imageHeight) * 0.5 ) + 'px';
$(this).css({
'margin-top' : marginOffset
});
});
});
$(elem).attr('data-width', w );
}
}
resizePhotosetGrid();
$(window).on("resize", function() {
resizePhotosetGrid();
});
};
// By default the plugin will wait until all of the images are loaded to setup the styles
var waitForImagesLoaded = true;
var hasDimensions = true;
// Loops through all of the images in the photoset
// if the height and width exists for all images set waitForImagesLoaded to false
$(elem).find('img').each(function(){
hasDimensions = hasDimensions & ( !!$(this).attr('height') & !!$(this).attr('width') );
});
waitForImagesLoaded = !hasDimensions;
// Only use imagesLoaded() if waitForImagesLoaded
if(waitForImagesLoaded) {
$(elem).imagesLoaded(function(){
setupStyles(waitForImagesLoaded);
$this._callback(elem);
});
} else {
setupStyles(waitForImagesLoaded);
$this._callback(elem);
}
}
};
// plugin wrapper around the constructor
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin( this, options ));
}
});
};
/*!
* jQuery imagesLoaded plugin v2.1.1
* http://github.com/desandro/imagesloaded
*
* MIT License. by Paul Irish et al.
*/
/*jshint curly: true, eqeqeq: true, noempty: true, strict: true, undef: true, browser: true */
/*global jQuery: false */
// blank image data-uri bypasses webkit log warning (thx doug jones)
var BLANK = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
$.fn.imagesLoaded = function( callback ) {
var $this = this,
deferred = $.isFunction($.Deferred) ? $.Deferred() : 0,
hasNotify = $.isFunction(deferred.notify),
$images = $this.find('img').add( $this.filter('img') ),
loaded = [],
proper = [],
broken = [];
// Register deferred callbacks
if ($.isPlainObject(callback)) {
$.each(callback, function (key, value) {
if (key === 'callback') {
callback = value;
} else if (deferred) {
deferred[key](value);
}
});
}
function doneLoading() {
var $proper = $(proper),
$broken = $(broken);
if ( deferred ) {
if ( broken.length ) {
deferred.reject( $images, $proper, $broken );
} else {
deferred.resolve( $images );
}
}
if ( $.isFunction( callback ) ) {
callback.call( $this, $images, $proper, $broken );
}
}
function imgLoadedHandler( event ) {
imgLoaded( event.target, event.type === 'error' );
}
function imgLoaded( img, isBroken ) {
// don't proceed if BLANK image, or image is already loaded
if ( img.src === BLANK || $.inArray( img, loaded ) !== -1 ) {
return;
}
// store element in loaded images array
loaded.push( img );
// keep track of broken and properly loaded images
if ( isBroken ) {
broken.push( img );
} else {
proper.push( img );
}
// cache image and its state for future calls
$.data( img, 'imagesLoaded', { isBroken: isBroken, src: img.src } );
// trigger deferred progress method if present
if ( hasNotify ) {
deferred.notifyWith( $(img), [ isBroken, $images, $(proper), $(broken) ] );
}
// call doneLoading and clean listeners if all images are loaded
if ( $images.length === loaded.length ) {
setTimeout( doneLoading );
$images.unbind( '.imagesLoaded', imgLoadedHandler );
}
}
// if no images, trigger immediately
if ( !$images.length ) {
doneLoading();
} else {
$images.bind( 'load.imagesLoaded error.imagesLoaded', imgLoadedHandler )
.each( function( i, el ) {
var src = el.src;
// find out if this image has been already checked for status
// if it was, and src has not changed, call imgLoaded on it
var cached = $.data( el, 'imagesLoaded' );
if ( cached && cached.src === src ) {
imgLoaded( el, cached.isBroken );
return;
}
// if complete is true and browser supports natural sizes, try
// to check for image status manually
if ( el.complete && el.naturalWidth !== undefined ) {
imgLoaded( el, el.naturalWidth === 0 || el.naturalHeight === 0 );
return;
}
// cached images don't fire load sometimes, so we reset src, but only when
// dealing with IE, or image is complete (loaded) and failed manual check
// webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
if ( el.readyState || el.complete ) {
el.src = BLANK;
el.src = src;
}
});
}
return deferred ? deferred.promise( $this ) : $this;
};
/*
* throttledresize: special jQuery event that happens at a reduced rate compared to "resize"
*
* latest version and complete README available on Github:
* https://github.com/louisremi/jquery-smartresize
*
* Copyright 2012 @louis_remi
* Licensed under the MIT license.
*
* This saved you an hour of work?
* Send me music http://www.amazon.co.uk/wishlist/HNTU0468LQON
*/
var $event = $.event,
$special,
dummy = {_:0},
frame = 0,
wasResized, animRunning;
$special = $event.special.throttledresize = {
setup: function() {
$( this ).on( "resize", $special.handler );
},
teardown: function() {
$( this ).off( "resize", $special.handler );
},
handler: function( event, execAsap ) {
// Save the context
var context = this,
args = arguments;
wasResized = true;
if ( !animRunning ) {
setInterval(function(){
frame++;
if ( frame > $special.threshold && wasResized || execAsap ) {
// set correct event type
event.type = "throttledresize";
$event.dispatch.apply( context, args );
wasResized = false;
frame = 0;
}
if ( frame > 9 ) {
$(dummy).stop();
animRunning = false;
frame = 0;
}
}, 30);
animRunning = true;
}
},
threshold: 0
};
})( jQuery, window, document );