Repository navigation
Expand file tree
/
Copy pathfeatherlight.js
More file actions
237 lines (213 loc) · 9.89 KB
/
Copy pathfeatherlight.js
File metadata and controls
237 lines (213 loc) · 9.89 KB
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
/**
* Featherlight - ultra slim jQuery lightbox
* Version 0.3.1 - http://noelboss.github.io/featherlight/
*
* Copyright 2014, Noël Raoul Bossart (http://www.noelboss.com)
* MIT Licensed.
**/
(function($) {
"use strict";
if($ === 'undefined') {return; }
/* featherlight object */
var fl = {
id: 0, /* Used to id single featherlight instances */
defaults: { /* You can access and override all defaults using $.fl.defaults */
autostart: true, /* Initialize all links with that match "selector" on document ready */
namespace: 'featherlight', /* Name of the events and css class prefix */
selector: '[data-featherlight]', /* Elements that trigger the lightbox */
context: 'body', /* Context used to search for the lightbox content and triggers */
type: { /* Manually set type of lightbox. Otherwise, it will check for the targetAttrs value. */
image: false,
ajax: false
},
targetAttr: 'data-featherlight', /* Attribute of the triggered element that contains the selector to the lightbox content */
variant: null, /* Class that will be added to change look of the lightbox */
resetCss: false, /* Reset all css */
background: null, /* Custom DOM for the background, wrapper and the closebutton */
openTrigger: 'click', /* Event that triggers the lightbox */
closeTrigger: 'click', /* Event that triggers the closing of the lightbox */
openSpeed: 250, /* Duration of opening animation */
closeSpeed: 250, /* Duration of closing animation */
closeOnBg: true, /* Close lightbox on click on the background */
closeOnEsc: true, /* Close lightbox when pressing esc */
closeOnAny: false, /* Close lightbox when clicking anywhere */
closeIcon: '✕', /* Close icon */
beforeOpen: null, /* Called before open. can return false to prevent opening of lightbox. Gets event as parameter, this contains all data */
beforeClose: null, /* Called before close. can return false to prevent opening of lightbox. Gets event as parameter, this contains all data */
afterOpen: null, /* Called after open. Gets event as parameter, this contains all data */
afterClose: null, /* Called after close. Gets event as parameter, this contains all data */
/* opens the lightbox "this" contains $instance with the lightbox, and with the config */
open: function(event){
var open = true;
/* check if before function exists */
if(typeof $.featherlight.defaults.beforeOpen === 'function'){
open = $.proxy($.featherlight.defaults.beforeOpen, this, event)();
}
/* if no before function or before function did not stop propagation */
if(open !== false){
/* call open method */
open = $.proxy($.featherlight.methods.open, this, event)();
}
/* check if after function exists */
if(open !== false && typeof $.featherlight.defaults.afterOpen === 'function'){
$.proxy($.featherlight.defaults.afterOpen, this, event)();
}
},
/* closes the lightbox "this" contains $instance with the lightbox, and with the config */
close: function(event){
var close = true;
/* check if before Function exists */
if(typeof $.featherlight.defaults.beforeClose === 'function'){
close = $.proxy($.featherlight.defaults.beforeClose, this, event)();
}
/* if no before function or before function did not stop propagation */
if(close !== false){
/* call open method */
close = $.proxy($.featherlight.methods.close, this, event)();
}
/* check if after Function exists */
if(close !== false && typeof $.featherlight.defaults.afterClose === 'function'){
$.proxy($.featherlight.defaults.afterClose, this, event)();
}
}
},
/* you can access and override all methods using $.featherlight.methods */
methods: {
/* setup iterates over a single instance of featherlight and prepares the background and binds the events */
setup: function(config, content){
config = $.extend({}, fl.defaults, config);
var $elm = $(this) || $(),
variant = $elm.attr('data-'+config.namespace+'-variant') || config.variant,
css = !config.resetCss ? config.namespace : config.namespace+'-reset', /* by adding -reset to the classname, we reset all the default css */
$background = $(config.background || '<div class="'+css+'"><div class="'+css+'-content"><span class="'+css+'-close">'+config.closeIcon+'</span></div></div>'),
/* everything that we need later is stored in self (target) */
self = {
id: fl.id++,
config: config,
content: content,
$elm: $elm,
$instance: $background.clone().addClass(variant) /* clone DOM for the background, wrapper and the close button */
};
/* close when click on background */
self.$instance.on(config.closeTrigger+'.'+config.namespace, $.proxy(config.close, self));
/* bind close on esc */
if(self.config.closeOnEsc){
$(document).bind('keyup.'+self.config.namespace+self.id, function(e) {
if (e.keyCode === 27) { // esc keycode
self.$instance.find('.'+self.config.namespace+'-close').click();
}
});
}
/* bind or call open function */
if($elm.length > 0 && this.tagName){
$elm.on(config.openTrigger+'.'+config.namespace, $.proxy(config.open, self));
} else {
$.proxy(config.open, self)();
}
},
/* this method prepares the content and converts it into a jQuery object */
getContent: function(){
var self = this,
ok = true,
content = self.content,
$content = null,
attr = self.$elm.attr(self.config.targetAttr) || '',
url = '';
/* if we have DOM, convert to jQuery Object */
if(self.$content instanceof $ === false && typeof content === 'string'){
$content = $(content);
} else if(content instanceof $ === false){ /* if we have no jQuery Object */
/* check if we have an image and create element */
if(self.config.type.image === true || attr === 'image' || attr.match(/\.(png|jpg|jpeg|gif|tiff|bmp)$/i)){
url = attr.match(/\.(png|jpg|jpeg|gif|tiff|bmp)$/i) ? attr : self.$elm.attr('href');
$content = $('<img src="'+url+'" alt="" class="'+self.config.namespace+'-image" />');
}
/* check if we have an ajax link */
else if(self.config.type.ajax === true || attr === 'ajax' || attr.match(/(http|htm|php)/i)){
url = attr.match(/(http|htm|php)/i) ? attr : self.$elm.attr('href');
/* we are using load so one can specify a target with: url.html #targetelement */
content = url ? $('<div></div>').load(url, function(response, status){
if ( status !== "error" ) {
$.featherlight(content.html(), self.config);
}
}) : null;
ok = false;
}
/* otherwise create jquery element by using the attribute as selector */
else if(attr) {
$content = $($(attr), self.config.context);
}
/* could not find any content */
else {
ok = false;
}
}
if(ok && $content instanceof $){
/* we need a special class for the iframe */
if($content.is('iframe') || $('iframe', $content).length > 0){
self.$instance.addClass(self.config.namespace+'-iframe');
}
$content.addClass(self.config.namespace+'-inner');
self.$content = $content.clone();
/* remove existing content */
self.$instance.find('.'+self.config.namespace+'-inner').remove();
self.$instance.find('.'+self.config.namespace+'-content').append(self.$content);
}
return ok;
},
/* opens the lightbox "this" contains $instance with the lightbox, and with the config */
open: function(event){
if(event){event.preventDefault();}
var self = this;
/* If we have content, add it and show lightbox */
if($.proxy(fl.methods.getContent, self)() !== false){
self.$instance.prependTo('body').fadeIn(self.config.openSpeed);
} else {
return false;
}
},
/* closes the lightbox "this" contains $instance with the lightbox, and with the config */
close: function(event){
var self = this,
config = self.config,
$instance = $(event.target);
if(config.closeOnAny || (config.closeOnBg && $instance.is('.'+config.namespace)) || $instance.is('.'+config.namespace+'-close')){
if(event){
event.preventDefault();
}
if(self.config.closeOnEsc){
$(document).unbind('keyup.'+self.config.namespace+self.id);
}
self.$instance.fadeOut(self.config.closeSpeed,function(){
self.$instance.detach();
});
}
}
}
};
/* extend jQuery with standalone featherlight method $.featherlight(elm, config); */
$.featherlight = function($content, config) {
/* if $.featherlight() was called only with config or without anything, initialize manually */
if(typeof $content !== 'string' && $content instanceof $ === false){
config = typeof $content === 'Object' ? $.extend({}, fl.defaults, $content) : fl.defaults;
$(config.selector, config.context).featherlight();
} else {
$.proxy(fl.methods.setup, null, config, $content)();
}
};
/* extend jQuery with selector featherlight method $(elm).featherlight(config, elm); */
$.fn.featherlight = function(config, $content) {
$(this).each(function(){
$.proxy(fl.methods.setup, this, config, $content)();
});
};
/* extend featherlight with defaults and methods */
$.extend($.featherlight, fl);
/* bind featherlight on ready if config autostart is true */
$(document).ready(function(){
var config = $.featherlight.defaults;
if(config.autostart){
$(config.selector, config.context).featherlight();
}
});
}(typeof jQuery === 'function' ? jQuery : alert('Too much lightness, Featherlight needs jQuery.')));