|
| 1 | +/* |
| 2 | + * Facebox (for jQuery) |
| 3 | + * version: 1.2 (05/05/2008) |
| 4 | + * @requires jQuery v1.2 or later |
| 5 | + * |
| 6 | + * Examples at http://famspam.com/facebox/ |
| 7 | + * |
| 8 | + * Licensed under the MIT: |
| 9 | + * http://www.opensource.org/licenses/mit-license.php |
| 10 | + * |
| 11 | + * Copyright 2007, 2008 Chris Wanstrath [ [email protected] ] |
| 12 | + * |
| 13 | + * Usage: |
| 14 | + * |
| 15 | + * jQuery(document).ready(function() { |
| 16 | + * jQuery('a[rel*=facebox]').facebox() |
| 17 | + * }) |
| 18 | + * |
| 19 | + * <a href="#terms" rel="facebox">Terms</a> |
| 20 | + * Loads the #terms div in the box |
| 21 | + * |
| 22 | + * <a href="terms.html" rel="facebox">Terms</a> |
| 23 | + * Loads the terms.html page in the box |
| 24 | + * |
| 25 | + * <a href="terms.png" rel="facebox">Terms</a> |
| 26 | + * Loads the terms.png image in the box |
| 27 | + * |
| 28 | + * |
| 29 | + * You can also use it programmatically: |
| 30 | + * |
| 31 | + * jQuery.facebox('some html') |
| 32 | + * jQuery.facebox('some html', 'my-groovy-style') |
| 33 | + * |
| 34 | + * The above will open a facebox with "some html" as the content. |
| 35 | + * |
| 36 | + * jQuery.facebox(function($) { |
| 37 | + * $.get('blah.html', function(data) { $.facebox(data) }) |
| 38 | + * }) |
| 39 | + * |
| 40 | + * The above will show a loading screen before the passed function is called, |
| 41 | + * allowing for a better ajaxy experience. |
| 42 | + * |
| 43 | + * The facebox function can also display an ajax page, an image, or the contents of a div: |
| 44 | + * |
| 45 | + * jQuery.facebox({ ajax: 'remote.html' }) |
| 46 | + * jQuery.facebox({ ajax: 'remote.html' }, 'my-groovy-style') |
| 47 | + * jQuery.facebox({ image: 'stairs.jpg' }) |
| 48 | + * jQuery.facebox({ image: 'stairs.jpg' }, 'my-groovy-style') |
| 49 | + * jQuery.facebox({ div: '#box' }) |
| 50 | + * jQuery.facebox({ div: '#box' }, 'my-groovy-style') |
| 51 | + * |
| 52 | + * Want to close the facebox? Trigger the 'close.facebox' document event: |
| 53 | + * |
| 54 | + * jQuery(document).trigger('close.facebox') |
| 55 | + * |
| 56 | + * Facebox also has a bunch of other hooks: |
| 57 | + * |
| 58 | + * loading.facebox |
| 59 | + * beforeReveal.facebox |
| 60 | + * reveal.facebox (aliased as 'afterReveal.facebox') |
| 61 | + * init.facebox |
| 62 | + * afterClose.facebox |
| 63 | + * |
| 64 | + * Simply bind a function to any of these hooks: |
| 65 | + * |
| 66 | + * $(document).bind('reveal.facebox', function() { ...stuff to do after the facebox and contents are revealed... }) |
| 67 | + * |
| 68 | + */ |
| 69 | +(function($) { |
| 70 | + $.facebox = function(data, klass) { |
| 71 | + $.facebox.loading() |
| 72 | + |
| 73 | + if (data.ajax) fillFaceboxFromAjax(data.ajax, klass) |
| 74 | + else if (data.image) fillFaceboxFromImage(data.image, klass) |
| 75 | + else if (data.div) fillFaceboxFromHref(data.div, klass) |
| 76 | + else if ($.isFunction(data)) data.call($) |
| 77 | + else $.facebox.reveal(data, klass) |
| 78 | + } |
| 79 | + |
| 80 | + /* |
| 81 | + * Public, $.facebox methods |
| 82 | + */ |
| 83 | + |
| 84 | + $.extend($.facebox, { |
| 85 | + settings: { |
| 86 | + opacity : 0.2, |
| 87 | + overlay : true, |
| 88 | + /* I don't know why absolute paths don't work. If you try to use facebox |
| 89 | + * outside of the examples folder these images won't show up. |
| 90 | + */ |
| 91 | + loadingImage : '../../_static/loading.gif', |
| 92 | + closeImage : '../../_static/closelabel.png', |
| 93 | + imageTypes : [ 'png', 'jpg', 'jpeg', 'gif' ], |
| 94 | + faceboxHtml : '\ |
| 95 | + <div id="facebox" style="display:none;"> \ |
| 96 | + <div class="popup"> \ |
| 97 | + <div class="content"> \ |
| 98 | + </div> \ |
| 99 | + <a href="#" class="close"><img src="../../_static/closelabel.png" title="close" class="close_image" /></a> \ |
| 100 | + </div> \ |
| 101 | + </div>' |
| 102 | + }, |
| 103 | + |
| 104 | + loading: function() { |
| 105 | + init() |
| 106 | + if ($('#facebox .loading').length == 1) return true |
| 107 | + showOverlay() |
| 108 | + |
| 109 | + $('#facebox .content').empty() |
| 110 | + $('#facebox .body').children().hide().end(). |
| 111 | + append('<div class="loading"><img src="'+$.facebox.settings.loadingImage+'"/></div>') |
| 112 | + |
| 113 | + $('#facebox').css({ |
| 114 | + top: getPageScroll()[1] + (getPageHeight() / 10), |
| 115 | + left: $(window).width() / 2 - 205 |
| 116 | + }).show() |
| 117 | + |
| 118 | + $(document).bind('keydown.facebox', function(e) { |
| 119 | + if (e.keyCode == 27) $.facebox.close() |
| 120 | + return true |
| 121 | + }) |
| 122 | + $(document).trigger('loading.facebox') |
| 123 | + }, |
| 124 | + |
| 125 | + reveal: function(data, klass) { |
| 126 | + $(document).trigger('beforeReveal.facebox') |
| 127 | + if (klass) $('#facebox .content').addClass(klass) |
| 128 | + $('#facebox .content').append(data) |
| 129 | + $('#facebox .loading').remove() |
| 130 | + $('#facebox .body').children().fadeIn('normal') |
| 131 | + $('#facebox').css('left', $(window).width() / 2 - ($('#facebox .popup').width() / 2)) |
| 132 | + $(document).trigger('reveal.facebox').trigger('afterReveal.facebox') |
| 133 | + }, |
| 134 | + |
| 135 | + close: function() { |
| 136 | + $(document).trigger('close.facebox') |
| 137 | + return false |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + /* |
| 142 | + * Public, $.fn methods |
| 143 | + */ |
| 144 | + |
| 145 | + $.fn.facebox = function(settings) { |
| 146 | + if ($(this).length == 0) return |
| 147 | + |
| 148 | + init(settings) |
| 149 | + |
| 150 | + function clickHandler() { |
| 151 | + $.facebox.loading(true) |
| 152 | + |
| 153 | + // support for rel="facebox.inline_popup" syntax, to add a class |
| 154 | + // also supports deprecated "facebox[.inline_popup]" syntax |
| 155 | + var klass = this.rel.match(/facebox\[?\.(\w+)\]?/) |
| 156 | + if (klass) klass = klass[1] |
| 157 | + |
| 158 | + fillFaceboxFromHref(this.href, klass) |
| 159 | + return false |
| 160 | + } |
| 161 | + |
| 162 | + return this.bind('click.facebox', clickHandler) |
| 163 | + } |
| 164 | + |
| 165 | + /* |
| 166 | + * Private methods |
| 167 | + */ |
| 168 | + |
| 169 | + // called one time to setup facebox on this page |
| 170 | + function init(settings) { |
| 171 | + if ($.facebox.settings.inited) return true |
| 172 | + else $.facebox.settings.inited = true |
| 173 | + |
| 174 | + $(document).trigger('init.facebox') |
| 175 | + makeCompatible() |
| 176 | + |
| 177 | + var imageTypes = $.facebox.settings.imageTypes.join('|') |
| 178 | + $.facebox.settings.imageTypesRegexp = new RegExp('\.(' + imageTypes + ')$', 'i') |
| 179 | + |
| 180 | + if (settings) $.extend($.facebox.settings, settings) |
| 181 | + $('body').append($.facebox.settings.faceboxHtml) |
| 182 | + |
| 183 | + var preload = [ new Image(), new Image() ] |
| 184 | + preload[0].src = $.facebox.settings.closeImage |
| 185 | + preload[1].src = $.facebox.settings.loadingImage |
| 186 | + |
| 187 | + $('#facebox').find('.b:first, .bl').each(function() { |
| 188 | + preload.push(new Image()) |
| 189 | + preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1') |
| 190 | + }) |
| 191 | + |
| 192 | + $('#facebox .close').click($.facebox.close) |
| 193 | + $('#facebox .close_image').attr('src', $.facebox.settings.closeImage) |
| 194 | + } |
| 195 | + |
| 196 | + // getPageScroll() by quirksmode.com |
| 197 | + function getPageScroll() { |
| 198 | + var xScroll, yScroll; |
| 199 | + if (self.pageYOffset) { |
| 200 | + yScroll = self.pageYOffset; |
| 201 | + xScroll = self.pageXOffset; |
| 202 | + } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict |
| 203 | + yScroll = document.documentElement.scrollTop; |
| 204 | + xScroll = document.documentElement.scrollLeft; |
| 205 | + } else if (document.body) {// all other Explorers |
| 206 | + yScroll = document.body.scrollTop; |
| 207 | + xScroll = document.body.scrollLeft; |
| 208 | + } |
| 209 | + return new Array(xScroll,yScroll) |
| 210 | + } |
| 211 | + |
| 212 | + // Adapted from getPageSize() by quirksmode.com |
| 213 | + function getPageHeight() { |
| 214 | + var windowHeight |
| 215 | + if (self.innerHeight) { // all except Explorer |
| 216 | + windowHeight = self.innerHeight; |
| 217 | + } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode |
| 218 | + windowHeight = document.documentElement.clientHeight; |
| 219 | + } else if (document.body) { // other Explorers |
| 220 | + windowHeight = document.body.clientHeight; |
| 221 | + } |
| 222 | + return windowHeight |
| 223 | + } |
| 224 | + |
| 225 | + // Backwards compatibility |
| 226 | + function makeCompatible() { |
| 227 | + var $s = $.facebox.settings |
| 228 | + |
| 229 | + $s.loadingImage = $s.loading_image || $s.loadingImage |
| 230 | + $s.closeImage = $s.close_image || $s.closeImage |
| 231 | + $s.imageTypes = $s.image_types || $s.imageTypes |
| 232 | + $s.faceboxHtml = $s.facebox_html || $s.faceboxHtml |
| 233 | + } |
| 234 | + |
| 235 | + // Figures out what you want to display and displays it |
| 236 | + // formats are: |
| 237 | + // div: #id |
| 238 | + // image: blah.extension |
| 239 | + // ajax: anything else |
| 240 | + function fillFaceboxFromHref(href, klass) { |
| 241 | + // div |
| 242 | + if (href.match(/#/)) { |
| 243 | + var url = window.location.href.split('#')[0] |
| 244 | + var target = href.replace(url,'') |
| 245 | + if (target == '#') return |
| 246 | + $.facebox.reveal($(target).html(), klass) |
| 247 | + |
| 248 | + // image |
| 249 | + } else if (href.match($.facebox.settings.imageTypesRegexp)) { |
| 250 | + fillFaceboxFromImage(href, klass) |
| 251 | + // ajax |
| 252 | + } else { |
| 253 | + fillFaceboxFromAjax(href, klass) |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + function fillFaceboxFromImage(href, klass) { |
| 258 | + var image = new Image() |
| 259 | + image.onload = function() { |
| 260 | + $.facebox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass) |
| 261 | + } |
| 262 | + image.src = href |
| 263 | + } |
| 264 | + |
| 265 | + function fillFaceboxFromAjax(href, klass) { |
| 266 | + $.get(href, function(data) { $.facebox.reveal(data, klass) }) |
| 267 | + } |
| 268 | + |
| 269 | + function skipOverlay() { |
| 270 | + return $.facebox.settings.overlay == false || $.facebox.settings.opacity === null |
| 271 | + } |
| 272 | + |
| 273 | + function showOverlay() { |
| 274 | + if (skipOverlay()) return |
| 275 | + |
| 276 | + if ($('#facebox_overlay').length == 0) |
| 277 | + $("body").append('<div id="facebox_overlay" class="facebox_hide"></div>') |
| 278 | + |
| 279 | + $('#facebox_overlay').hide().addClass("facebox_overlayBG") |
| 280 | + .css('opacity', $.facebox.settings.opacity) |
| 281 | + .click(function() { $(document).trigger('close.facebox') }) |
| 282 | + .fadeIn(200) |
| 283 | + return false |
| 284 | + } |
| 285 | + |
| 286 | + function hideOverlay() { |
| 287 | + if (skipOverlay()) return |
| 288 | + |
| 289 | + $('#facebox_overlay').fadeOut(200, function(){ |
| 290 | + $("#facebox_overlay").removeClass("facebox_overlayBG") |
| 291 | + $("#facebox_overlay").addClass("facebox_hide") |
| 292 | + $("#facebox_overlay").remove() |
| 293 | + }) |
| 294 | + |
| 295 | + return false |
| 296 | + } |
| 297 | + |
| 298 | + /* |
| 299 | + * Bindings |
| 300 | + */ |
| 301 | + |
| 302 | + $(document).bind('close.facebox', function() { |
| 303 | + $(document).unbind('keydown.facebox') |
| 304 | + $('#facebox').fadeOut(function() { |
| 305 | + $('#facebox .content').removeClass().addClass('content') |
| 306 | + $('#facebox .loading').remove() |
| 307 | + $(document).trigger('afterClose.facebox') |
| 308 | + }) |
| 309 | + hideOverlay() |
| 310 | + }) |
| 311 | + |
| 312 | +})(jQuery); |
0 commit comments