2013-08-07 08:59:08 +00:00
|
|
|
( function ( $ ) {
|
|
|
|
var LIP, lightboxHooks;
|
|
|
|
|
|
|
|
/**
|
2014-02-03 23:20:54 +00:00
|
|
|
* @class mlb.LightboxInterface
|
2013-08-07 08:59:08 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function LightboxInterface() {
|
|
|
|
lightboxHooks = window.lightboxHooks;
|
|
|
|
|
|
|
|
var result,
|
|
|
|
addToPre = [],
|
2013-09-04 01:01:57 +00:00
|
|
|
addToPost = [],
|
|
|
|
lbinterface = this;
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
// Staging area for image resizes
|
|
|
|
this.$staging = $( '<div>' )
|
|
|
|
.addClass( 'mlb-staging-area' );
|
|
|
|
$( document.body ).append( this.$staging );
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
this.$overlay = $( '<div>' )
|
|
|
|
.addClass( 'mlb-overlay' );
|
|
|
|
|
|
|
|
this.$wrapper = $( '<div>' )
|
|
|
|
.addClass( 'mlb-wrapper' );
|
|
|
|
|
|
|
|
this.$main = $( '<div>' )
|
|
|
|
.addClass( 'mlb-main' );
|
|
|
|
|
|
|
|
this.$imageDiv = $( '<div>' )
|
|
|
|
.addClass( 'mlb-image' );
|
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
// I blame CSS for this
|
|
|
|
this.$innerWrapper = $( '<div>' )
|
|
|
|
.addClass( 'mlb-image-inner-wrapper' )
|
|
|
|
.append( this.$imageDiv );
|
|
|
|
|
|
|
|
this.$imageWrapper = $( '<div>' )
|
|
|
|
.addClass( 'mlb-image-wrapper' )
|
|
|
|
.append( this.$innerWrapper );
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
this.$preDiv = $( '<div>' )
|
|
|
|
.addClass( 'mlb-pre-image' );
|
|
|
|
result = lightboxHooks.callAll( 'addToPreDiv', this, addToPre );
|
|
|
|
this.setupPreDiv( result, addToPre );
|
|
|
|
|
|
|
|
this.$postDiv = $( '<div>' )
|
|
|
|
.addClass( 'mlb-post-image' );
|
|
|
|
result = lightboxHooks.callAll( 'addToPostDiv', this, addToPost );
|
|
|
|
this.setupPostDiv( result, addToPost );
|
|
|
|
|
|
|
|
this.$main.append(
|
|
|
|
this.$preDiv,
|
2013-11-13 00:43:46 +00:00
|
|
|
this.$imageWrapper,
|
2013-08-07 08:59:08 +00:00
|
|
|
this.$postDiv
|
|
|
|
);
|
|
|
|
|
|
|
|
this.$wrapper.append(
|
|
|
|
this.$main
|
|
|
|
);
|
|
|
|
|
2013-10-22 16:34:08 +00:00
|
|
|
lightboxHooks.callAll( 'modifyInterface', this );
|
2013-08-07 08:59:08 +00:00
|
|
|
|
|
|
|
window.addEventListener( 'keyup', function ( e ) {
|
|
|
|
if ( e.keyCode === 27 ) {
|
|
|
|
// Escape button pressed
|
2013-09-04 01:01:57 +00:00
|
|
|
lbinterface.unattach();
|
2013-08-07 08:59:08 +00:00
|
|
|
}
|
2013-09-04 01:01:57 +00:00
|
|
|
} );
|
2013-08-07 08:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LIP = LightboxInterface.prototype;
|
|
|
|
|
2013-12-10 21:04:45 +00:00
|
|
|
/**
|
2014-02-03 23:20:54 +00:00
|
|
|
* The currently selected LightboxImage.
|
|
|
|
* @type {mlb.LightboxImage}
|
2013-12-10 21:04:45 +00:00
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
LIP.currentImage = null;
|
|
|
|
|
2013-10-23 23:51:34 +00:00
|
|
|
LIP.empty = function () {
|
|
|
|
this.$imageDiv.empty();
|
|
|
|
|
|
|
|
lightboxHooks.callAll( 'clearInterface', this );
|
2013-11-21 19:50:27 +00:00
|
|
|
|
|
|
|
if ( this.resizeListener ) {
|
|
|
|
window.removeEventListener( 'resize', this.resizeListener );
|
|
|
|
}
|
2013-10-23 23:51:34 +00:00
|
|
|
};
|
|
|
|
|
2013-12-11 02:08:07 +00:00
|
|
|
/**
|
|
|
|
* Attaches interface to document or given parent id.
|
|
|
|
*
|
|
|
|
* @param {string} [parentId] parent id where we want to attach the UI. Mainly for testing.
|
|
|
|
*/
|
|
|
|
LIP.attach = function ( parentId ) {
|
2014-02-03 11:23:31 +00:00
|
|
|
var lbinterface = this,
|
|
|
|
$parent;
|
|
|
|
|
2014-01-27 17:12:09 +00:00
|
|
|
// Re-appending the same content can have nasty side-effects
|
|
|
|
// Such as the browser leaving fullscreen mode if the fullscreened element is part of it
|
|
|
|
if ( this.currentlyAttached ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
$( document ).on( 'jq-fullscreen-change.lip', function( e ) {
|
|
|
|
lbinterface.fullscreenChange( e );
|
|
|
|
} );
|
|
|
|
|
|
|
|
$parent = $( parentId || document.body );
|
|
|
|
|
|
|
|
// Clean up fullscreen data because hard-existing fullscreen might have left
|
|
|
|
// jquery.fullscreen unable to remove the class and attribute, since $main wasn't
|
|
|
|
// attached to the DOM anymore at the time the jq-fullscreen-change event triggered
|
|
|
|
this.$main.data( 'isFullscreened', false ).removeClass( 'jq-fullscreened' );
|
|
|
|
this.isFullscreen = false;
|
2013-12-11 02:08:07 +00:00
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
$parent
|
2013-08-07 08:59:08 +00:00
|
|
|
.append(
|
|
|
|
this.$wrapper,
|
|
|
|
this.$overlay
|
|
|
|
);
|
2014-01-11 14:07:15 +00:00
|
|
|
this.currentlyAttached = true;
|
2013-08-07 08:59:08 +00:00
|
|
|
};
|
|
|
|
|
2013-12-11 02:08:07 +00:00
|
|
|
/**
|
|
|
|
* Unattaches interface from parent element. Calls global lightboxHooks.
|
|
|
|
*/
|
2013-08-07 08:59:08 +00:00
|
|
|
LIP.unattach = function () {
|
2013-12-11 02:08:07 +00:00
|
|
|
// TODO(aarcos): This is global and it breaks tests.
|
2013-11-13 00:43:46 +00:00
|
|
|
lightboxHooks.callAll( 'closeInterface', this );
|
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
$( document ).off( 'jq-fullscreen-change.lip' );
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
this.$wrapper.detach();
|
|
|
|
this.$overlay.detach();
|
2014-01-11 14:07:15 +00:00
|
|
|
|
|
|
|
this.currentlyAttached = false;
|
2013-08-07 08:59:08 +00:00
|
|
|
};
|
|
|
|
|
2013-11-21 19:50:27 +00:00
|
|
|
/**
|
2014-02-03 23:20:54 +00:00
|
|
|
* Resize callback
|
2013-11-21 19:50:27 +00:00
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
LIP.resizeCallback = function() {
|
2014-01-11 14:07:15 +00:00
|
|
|
if ( this.currentlyAttached ) {
|
|
|
|
var result = lightboxHooks.callAll( 'imageResize', this );
|
2013-11-13 00:43:46 +00:00
|
|
|
|
2014-01-11 14:07:15 +00:00
|
|
|
if ( result !== false ) {
|
|
|
|
this.autoResizeImage();
|
|
|
|
}
|
2013-11-21 19:50:27 +00:00
|
|
|
}
|
|
|
|
};
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2013-11-21 19:50:27 +00:00
|
|
|
/**
|
2014-02-03 23:20:54 +00:00
|
|
|
* Load callback
|
2013-11-21 19:50:27 +00:00
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
LIP.loadCallback = function ( image, ele ) {
|
|
|
|
var iface = this;
|
|
|
|
|
|
|
|
image.globalMaxWidth = ele.width;
|
|
|
|
image.globalMaxHeight = ele.height;
|
|
|
|
this.$image = $( ele );
|
|
|
|
|
|
|
|
this.autoResizeImage();
|
|
|
|
|
|
|
|
// Capture listener so we can remove it later, otherwise
|
|
|
|
// we are going to leak listeners !
|
|
|
|
this.resizeListener = function () { iface.resizeCallback(); };
|
2013-10-28 22:32:56 +00:00
|
|
|
|
2013-11-21 19:50:27 +00:00
|
|
|
window.addEventListener( 'resize', this.resizeListener );
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2013-11-21 19:50:27 +00:00
|
|
|
lightboxHooks.callAll( 'imageLoaded', this );
|
|
|
|
};
|
|
|
|
|
2013-12-10 21:04:45 +00:00
|
|
|
/**
|
2014-02-03 23:20:54 +00:00
|
|
|
* Loads the image, then calls the load callback of the interface.
|
|
|
|
* @param {mlb.LightboxImage} image
|
2013-12-10 21:04:45 +00:00
|
|
|
*/
|
2013-11-21 19:50:27 +00:00
|
|
|
LIP.load = function ( image ) {
|
2014-01-15 23:33:16 +00:00
|
|
|
var iface = this;
|
2013-08-07 08:59:08 +00:00
|
|
|
|
|
|
|
this.currentImage = image;
|
2014-01-15 23:33:16 +00:00
|
|
|
|
|
|
|
image.getImageElement().done( function( image, ele ) {
|
|
|
|
iface.loadCallback.call( iface, image, ele );
|
|
|
|
} );
|
2013-08-07 08:59:08 +00:00
|
|
|
};
|
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
LIP.autoResizeImage = function () {
|
|
|
|
this.$staging.append( this.$image );
|
|
|
|
this.currentImage.autoResize( this.$image.get( 0 ), this.$imageDiv );
|
|
|
|
this.$imageDiv.append( this.$image );
|
|
|
|
};
|
|
|
|
|
2013-12-10 21:04:45 +00:00
|
|
|
/**
|
|
|
|
* Changes what image is being displayed.
|
|
|
|
* @param {HTMLImageElement} imageEle
|
|
|
|
*/
|
2013-10-28 22:32:56 +00:00
|
|
|
LIP.replaceImageWith = function ( imageEle ) {
|
|
|
|
var $image = $( imageEle );
|
|
|
|
|
|
|
|
this.currentImage.src = imageEle.src;
|
|
|
|
|
|
|
|
this.$image.replaceWith( $image );
|
|
|
|
this.$image = $image;
|
|
|
|
|
|
|
|
this.currentImage.globalMaxWidth = this.$image.width();
|
|
|
|
this.currentImage.globalMaxHeight = this.$image.height();
|
2013-11-13 00:43:46 +00:00
|
|
|
this.currentImage.autoResize( imageEle );
|
2013-10-28 22:32:56 +00:00
|
|
|
};
|
|
|
|
|
2014-01-06 21:53:42 +00:00
|
|
|
LIP.exitFullscreen = function () {
|
|
|
|
this.fullscreenButtonJustPressed = true;
|
|
|
|
this.$main.exitFullscreen();
|
|
|
|
};
|
|
|
|
|
|
|
|
LIP.enterFullscreen = function () {
|
|
|
|
this.$main.enterFullscreen();
|
|
|
|
};
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
LIP.setupPreDiv = function ( buildDefaults, toAdd ) {
|
2013-09-04 01:01:57 +00:00
|
|
|
var lbinterface = this;
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
if ( buildDefaults ) {
|
|
|
|
this.$controlBar = $( '<div>' )
|
|
|
|
.addClass( 'mlb-controls' );
|
|
|
|
|
|
|
|
this.$closeButton = $( '<div>' )
|
|
|
|
.text( ' ' )
|
|
|
|
.addClass( 'mlb-close' )
|
2013-09-04 01:01:57 +00:00
|
|
|
.click( function () {
|
|
|
|
lbinterface.unattach();
|
|
|
|
} );
|
2013-08-07 08:59:08 +00:00
|
|
|
|
|
|
|
this.$fullscreenButton = $( '<div>' )
|
|
|
|
.text( ' ' )
|
|
|
|
.addClass( 'mlb-fullscreen' )
|
|
|
|
.click( function () {
|
2014-01-02 22:24:42 +00:00
|
|
|
if ( lbinterface.isFullscreen ) {
|
2014-01-06 21:53:42 +00:00
|
|
|
lbinterface.exitFullscreen();
|
2014-01-02 22:24:42 +00:00
|
|
|
} else {
|
2014-01-06 21:53:42 +00:00
|
|
|
lbinterface.enterFullscreen();
|
2014-01-02 22:24:42 +00:00
|
|
|
}
|
2013-09-04 01:01:57 +00:00
|
|
|
} );
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2014-01-31 09:58:28 +00:00
|
|
|
this.setupFullscreenButton();
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
this.$controlBar.append(
|
|
|
|
this.$closeButton,
|
|
|
|
this.$fullscreenButton
|
|
|
|
);
|
|
|
|
|
|
|
|
this.$preDiv.append( this.$controlBar );
|
|
|
|
|
|
|
|
lightboxHooks.callAll( 'modifyDefaultPreDiv', this );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addElementsToDiv( this.$preDiv, toAdd );
|
|
|
|
};
|
|
|
|
|
2014-01-31 09:58:28 +00:00
|
|
|
LIP.setupFullscreenButton = function () {
|
|
|
|
// If the browser doesn't support fullscreen mode, hide the fullscreen button
|
|
|
|
if ( $.support.fullscreen ) {
|
|
|
|
this.$fullscreenButton.show();
|
|
|
|
} else {
|
|
|
|
this.$fullscreenButton.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
LIP.setupPostDiv = function ( buildDefaults, toAdd ) {
|
|
|
|
if ( buildDefaults ) {
|
|
|
|
lightboxHooks.callAll( 'modifyDefaultPostDiv', this );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addElementsToDiv( this.$postDiv, toAdd );
|
|
|
|
};
|
|
|
|
|
|
|
|
LIP.addElementsToDiv = function ( $div, toAdd ) {
|
|
|
|
var i;
|
|
|
|
|
|
|
|
for ( i = 0; i < toAdd.length; i++ ) {
|
|
|
|
$div.append( toAdd[i] );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
LIP.fullscreenChange = function ( e ) {
|
|
|
|
this.isFullscreen = e.fullscreen;
|
|
|
|
|
|
|
|
lightboxHooks.callAll( 'fullscreen', e.element, e.fullscreen );
|
|
|
|
|
|
|
|
if ( !this.fullscreenButtonJustPressed && !e.fullscreen ) {
|
|
|
|
// Close the interface all the way if the user pressed 'esc'
|
|
|
|
this.unattach();
|
|
|
|
} else if ( this.fullscreenButtonJustPressed ) {
|
|
|
|
this.fullscreenButtonJustPressed = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
window.LightboxInterface = LightboxInterface;
|
|
|
|
}( jQuery ) );
|