See More

/** * Featherlight - ultra slim jQuery lightbox * Version 0.4.9 - http://noelboss.github.io/featherlight/ * * Copyright 2014, Noël Raoul Bossart (http://www.noelboss.com) * MIT Licensed. **/ (function($) { "use strict"; if('undefined' === typeof $) { if('console' in window){ window.console.info('Too much lightness, Featherlight needs jQuery.'); } return; } /* extend jQuery with standalone featherlight method $.featherlight(elm, config); */ function Featherlight($content, config) { if(this.constructor === Featherlight) { /* called with new */ this.id = Featherlight.id++; this.setup($content, config); } else { var fl = new Featherlight($content, config); fl.open(); return fl; } } /* document wide esc handler, attached in setup method */ var escapeHelper = function(event) { if (27 === event.keyCode && !event.isDefaultPrevented()) { // esc keycode var self = Featherlight.current(); if(self && self.closeOnEsc) { self.$instance.find('.'+self.namespace+'-close:first').click(); event.preventDefault(); } } }; Featherlight.prototype = { constructor: Featherlight, /*** defaults ***/ /* extend featherlight with defaults and methods */ namespace: 'featherlight', /* Name of the events and css class prefix */ 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 */ filter: null, /* Selector to filter events. Think $(...).on('click', filter, eventHandler) */ root: 'body', /* Where to append featherlights */ openSpeed: 250, /* Duration of opening animation */ closeSpeed: 250, /* Duration of closing animation */ closeOnClick: 'background', /* Close lightbox on click ('background', 'anywhere' or false) */ closeOnEsc: true, /* Close lightbox when pressing esc */ closeIcon: '✕', /* Close icon */ otherClose: null, /* Selector for alternate close buttons (e.g. "a.close") */ beforeOpen: $.noop, /* Called before open. can return false to prevent opening of lightbox. Gets event as parameter, this contains all data */ beforeClose: $.noop, /* Called before close. can return false to prevent opening of lightbox. Gets event as parameter, this contains all data */ afterOpen: $.noop, /* Called after open. Gets event as parameter, this contains all data */ afterClose: $.noop, /* Called after close. Gets event as parameter, this contains all data */ type: null, /* Specify type of lightbox. If unset, it will check for the targetAttrs value. */ contentFilters: ['jquery', 'image', 'html', 'ajax', 'text'], /* List of content filters to use to determine the content */ /*** methods ***/ /* setup iterates over a single instance of featherlight and prepares the background and binds the events */ setup: function(target, config){ /* all arguments are optional */ if (typeof target === 'object' && target instanceof $ === false && !config) { config = target; target = undefined; } var self = $.extend(this, config, {target: target}), css = !self.resetCss ? self.namespace : self.namespace+'-reset', /* by adding -reset to the classname, we reset all the default css */ $background = $(self.background || '

'+self.closeIcon+'
'), closeButtonSelector = '.'+self.namespace+'-close' + (self.otherClose ? ',' + self.otherClose : ''); self.$instance = $background.clone().addClass(self.variant); /* clone DOM for the background, wrapper and the close button */ /* attach esc handler to document if configured */ if(self.closeOnEsc && escapeHelper) { $(document).bind('keyup.'+this.constructor.prototype.namespace, escapeHelper); escapeHelper = null; } /* close when click on background/anywhere/null or closebox */ self.$instance.on(self.closeTrigger+'.'+self.namespace, function(event) { var $target = $(event.target); if( ('background' === self.closeOnClick && $target.is('.'+self.namespace)) || 'anywhere' === self.closeOnClick || $target.is(closeButtonSelector) ){ event.preventDefault(); self.close(); } }); return this; }, /* this method prepares the content and converts it into a jQuery object or a promise */ getContent: function(){ var self = this, filters = this.constructor.contentFilters, readTargetAttr = function(name){ return self.$currentTarget && self.$currentTarget.attr(name); }, targetValue = readTargetAttr(self.targetAttr), data = self.target || targetValue || ''; /* Find which filter applies */ var filter = filters[self.type]; /* check explicit type like {type: 'image'} */ /* check explicit type like data-featherlight="image" */ if(!filter && data in filters) { filter = filters[data]; data = self.target && targetValue; } data = data || readTargetAttr('href') || ''; /* check explicity type & content like {image: 'photo.jpg'} */ if(!filter) { for(var filterName in filters) { if(self[filterName]) { filter = filters[filterName]; data = self[filterName]; } } } /* otherwise it's implicit, run checks */ if(!filter) { var target = data; data = null; $.each(self.contentFilters, function() { filter = filters[this]; if(filter.test) { data = filter.test(target); } if(!data && filter.regex && target.match && target.match(filter.regex)) { data = target; } return !data; }); if(!data) { if('console' in window){ window.console.error('Featherlight: no content filter found ' + (target ? ' for "' + target + '"' : ' (no target specified)')); } return false; } } /* Process it */ return filter.process.call(self, data); }, /* sets the content of $instance to $content */ setContent: function($content){ var self = this; /* we need a special class for the iframe */ if($content.is('iframe') || $('iframe', $content).length > 0){ self.$instance.addClass(self.namespace+'-iframe'); } self.$content = $content.addClass(self.namespace+'-inner'); /* remove existing content */ self.$instance.find('.'+self.namespace+'-inner').remove(); self.$instance.find('.'+self.namespace+'-content').append(self.$content); return self; }, /* opens the lightbox. "this" contains $instance with the lightbox, and with the config */ open: function(event){ var self = this; if((!event || !event.isDefaultPrevented()) && self.beforeOpen(event) !== false) { if(event){ event.preventDefault(); } var $content = self.getContent(); if($content){ /* Add to opened registry */ self.constructor._opened.add(self._openedCallback = function(response){ if(self.$instance.closest('body').length > 0) { response.currentFeatherlight = self; } }); /* Set content and show */ $.when($content).done(function($content){ self.setContent($content) .$instance.appendTo(self.root).fadeIn(self.openSpeed); self.afterOpen(event); }); return self; } } return false; }, /* closes the lightbox. "this" contains $instance with the lightbox, and with the config */ close: function(event){ var self = this; if(self.beforeClose(event) === false) { return false; } self.constructor._opened.remove(self._openedCallback); self.$instance.fadeOut(self.closeSpeed,function(){ self.$instance.detach(); self.afterClose(event); }); } }; $.extend(Featherlight, { id: 0, /* Used to id single featherlight instances */ autoBind: '[data-featherlight]', /* Will automatically bind elements matching this selector. Clear or set before onReady */ defaults: Featherlight.prototype, /* You can access and override all defaults using $.featherlight.defaults, which is just a synonym for $.featherlight.prototype */ /* Contains the logic to determine content */ contentFilters: { jquery: { regex: /^[#.]\w/, /* Anything that starts with a class name or identifiers */ test: function(elem) { return elem instanceof $ && elem; }, process: function(elem) { return $(elem).clone(true); } }, image: { regex: /\.(png|jpg|jpeg|gif|tiff|bmp)(\?\S*)?$/i, process: function(url) { var self = this, deferred = $.Deferred(), img = new Image(); img.onload = function() { deferred.resolve( $('') ); }; img.onerror = function() { deferred.reject(); }; img.src = url; return deferred.promise(); } }, html: { regex: /^\s*<[\w!][^<]*>/, /* Anything that starts with some kind of valid tag */ process: function(html) { return $(html); } }, ajax: { regex: /./, /* At this point, any content is assumed to be an URL */ process: function(url) { var self = this, deferred = $.Deferred(); /* we are using load so one can specify a target with: url.html #targetelement */ var $container = $('
').load(url, function(response, status){ if ( status !== "error" ) { deferred.resolve($container.contents()); } deferred.fail(); }); return deferred.promise(); } }, text: { process: function(text) { return $('
', {text: text}); } } }, functionAttributes: ['beforeOpen', 'afterOpen', 'beforeClose', 'afterClose'], /*** class methods ***/ /* read element's attributes starting with data-featherlight- */ readElementConfig: function(element) { var Klass = this, config = {}; if (element && element.attributes) { $.each(element.attributes, function(){ var match = this.name.match(/^data-featherlight-(.*)/); if (match) { var val = this.value, name = $.camelCase(match[1]); if ($.inArray(name, Klass.functionAttributes) >= 0) { /* jshint -W054 */ val = new Function(val); /* jshint +W054 */ } else { try { val = $.parseJSON(val); } catch(e) {} } config[name] = val; } }); } return config; }, attach: function($source, $content, config) { var Klass = this; if (typeof $content === 'object' && $content instanceof $ === false && !config) { config = $content; $content = undefined; } /* make a copy */ config = $.extend({}, config); /* Only for openTrigger and namespace... */ var tempConfig = $.extend({}, Klass.defaults, Klass.readElementConfig($source[0]), config); $source.on(tempConfig.openTrigger+'.'+tempConfig.namespace, tempConfig.filter, function(event) { /* ... since we might as well compute the config on the actual target */ var elemConfig = $.extend({$currentTarget: $(this)}, Klass.readElementConfig($source[0]), Klass.readElementConfig(this), config); new $.featherlight($content, elemConfig).open(event); }); }, current: function() { var response = {}; this._opened.fire(response); return response.currentFeatherlight; }, close: function() { var cur = this.current(); if(cur) { cur.close(); } }, _opened: $.Callbacks() }); $.featherlight = Featherlight; /* bind jQuery elements to trigger featherlight */ $.fn.featherlight = function($content, config) { Featherlight.attach(this, $content, config); return this; }; /* bind featherlight on ready if config autoBind is set */ $(document).ready(function(){ if(Featherlight.autoBind){ /* First, bind click on document, so it will work for items added dynamically */ $(document).featherlight({filter: Featherlight.autoBind}); /* Auto bound elements with attr-featherlight-filter won't work (since we already used it to bind on document), so bind these directly. We can't easily support dynamically added element with filters */ $(Featherlight.autoBind).filter('[data-featherlight-filter]').each(function(){ $(this).featherlight(); }); } }); }(jQuery));