-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini.gallery.js
215 lines (190 loc) · 5.34 KB
/
gemini.gallery.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
/* global Templates */
/**
* @fileoverview
A Gemini plugin to quickly build galleries using a modal and carousel.
### Notes
- Default template requires the 'fit' module from gemini-css
- The gallery is built using a list of images. See example.
*
* @namespace gemini.gallery
* @copyright Carpages.ca 2014
* @author Matt Rose <matt@mattrose.ca>
*
* @requires gemini
* @requires gemini.modal
* @requires gemini.carousel
* @requires gemini.lazyload
* @requires gemini.respond
*
* @prop {integer} scrollSpeed {@link gemini.carousel#scrollSpeed}
* @prop {array} screens {@link gemini.carousel#screens}
* @prop {object} templates {@link gemini.gallery#templates}
*
* @example
<html>
<ul id="js-gallery">
<li>
<a href="http://www.placetim.com/200/200/">
<img src="http://www.placetim.com/200/200/">
</a>
</li>
<li>
<a href="http://www.placetim.com/250/250/">
<img src="http://www.placetim.com/250/250/">
</a>
</li>
</ul>
</html>
*
* @example
G('#js-gallery').gallery();
*/
( function( factory ) {
if ( typeof define === 'function' && define.amd ) {
// AMD. Register as an anonymous module.
define([
'gemini',
'gemini.gallery.templates',
'gemini.modal',
'gemini.carousel',
'gemini.lazyload',
'gemini.respond'
], factory );
} else if ( typeof exports === 'object' ) {
// Node/CommonJS
module.exports = factory(
require( 'gemini-loader' ),
require( './templates.js' ),
require( 'gemini-modal' ),
require( 'gemini-carousel' ),
require( 'gemini-lazyload' ),
require( 'gemini-respond' )
);
} else {
// Browser globals
factory( G, Templates.Default.Gallery );
}
})( function( $, T ) {
var _ = $._;
$.boiler( 'gallery', {
defaults: {
/**
* The speed that the carousel scrolls at in milliseconds.
* @name gemini.carousel#scrollSpeed
* @type Integer
* @default 500
*/
scrollSpeed: 500,
/**
* An array of screens to look for in the anchors data attributes to use
* when lazy loading the images
* @name gemini.carousel#screens
* @type Array
* @default []
*/
screens: [],
/**
* Precompiled Handlebar templates to replace default. Expecting 'gallery'
* and 'modal'
* @name gemini.gallery#templates
* @type object
* @default {}
*/
templates: {}
},
data: [ 'title', 'description' ],
init: function() {
var plugin = this;
// Extend the templates
plugin.T = $.extend( T, plugin.settings.templates );
// Grab images
plugin.imgs = _.map( plugin.$el.find( '> li > a' ), function( anchor, i ) {
var $anchor = $( anchor );
$anchor.click( function( e ) {
e.preventDefault();
plugin.open( i + 1 );
});
var screens = [];
_.each( plugin.settings.screens, function( scn ) {
if ( $anchor.data( scn )) {
screens.push({
screen: scn,
src: $anchor.data( scn )
});
}
});
return {
src: $anchor.attr( 'href' ),
screens: screens
};
});
// Create the modal
plugin.modal = new $.Modal({
templates: plugin.T,
fixed: true,
stopPropagation: 'img, .js-gallery-nav',
content: plugin.T.gallery({
title: plugin.settings.title,
description: plugin.settings.description,
imgs: plugin.imgs
})
});
// Activate Carousel
plugin.$carousel = plugin.modal.$content.find( '.js-gallery-carousel' );
plugin.$carousel.carousel({ scrollSpeed: plugin.settings.scrollSpeed, loop: true }).lazyload({
images: 'img.lazy',
effect: 'fadeIn',
threshold: window.$window.width() * 0.8
});
// Key press
window.$window.on( 'keydown', function( e ) {
if ( !plugin.modal.$modal.hasClass( 'is-active' )) {
return;
}
if ( e.keyCode === 37 ) {
// left
plugin.$carousel.carousel( 'previous' );
} else if ( e.keyCode === 39 ) {
// right
plugin.$carousel.carousel( 'next' );
} else if ( e.keyCode === 27 ) {
// esc
plugin.modal.close();
}
});
// Go to next when you click on one of the images
plugin.$carousel.on( 'click', 'img', function() {
plugin.$carousel.carousel( 'next' );
});
// Fit images according to the size of the screen
plugin.$carousel.find( '.fit' )._fit();
$.respond.bind( 'resize', function( e, scrn ) {
plugin.$carousel.find( '.fit' )._fit();
});
},
/**
* Open the gallery to a specific item
*
* @method
* @name gemini.gallery#open
* @param {integer} page The page to open up the gallery to
**/
open: function( page ) {
var plugin = this;
plugin.modal.open();
plugin.$carousel.carousel( 'gotoPage', page, false ).lazyload( 'update' );
},
/**
* Close the gallery
*
* @method
* @name gemini.gallery#close
**/
close: function() {
this.modal.close();
}
});
// Return the jquery object
// This way you don't need to require both jquery and the plugin
return $;
});