2013-08-07 08:59:08 +00:00
|
|
|
( function ( $ ) {
|
|
|
|
var LIP, lightboxHooks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
* @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
|
|
|
|
2014-01-02 22:24:42 +00:00
|
|
|
$( document ).on( 'jq-fullscreen-change', function ( e ) {
|
|
|
|
lightboxHooks.callAll( 'fullscreen', this, e.fullscreen );
|
|
|
|
|
|
|
|
if ( !lbinterface.fullscreenButtonJustPressed && !e.fullscreen ) {
|
|
|
|
// Close the interface all the way if the user pressed 'esc'
|
|
|
|
lbinterface.unattach();
|
|
|
|
} else if ( lbinterface.fullscreenButtonJustPressed ) {
|
|
|
|
lbinterface.fullscreenButtonJustPressed = false;
|
|
|
|
}
|
|
|
|
} );
|
2013-08-07 08:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LIP = LightboxInterface.prototype;
|
|
|
|
|
2013-12-10 21:04:45 +00:00
|
|
|
/**
|
|
|
|
* @type {LightboxImage}
|
|
|
|
* @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 ) {
|
|
|
|
var parent = $( parentId || document.body );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* @param {LightboxImage} image
|
|
|
|
*/
|
2013-11-21 19:50:27 +00:00
|
|
|
LIP.load = function ( image ) {
|
2013-12-10 21:04:45 +00:00
|
|
|
var iface = this,
|
|
|
|
ele = image.getImageElement( function () {
|
2013-11-21 19:50:27 +00:00
|
|
|
iface.loadCallback( image, ele );
|
2013-12-10 21:04:45 +00:00
|
|
|
} );
|
2013-08-07 08:59:08 +00:00
|
|
|
|
|
|
|
this.currentImage = image;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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 ) {
|
|
|
|
lbinterface.fullscreenButtonJustPressed = true;
|
|
|
|
lbinterface.$main.exitFullscreen();
|
|
|
|
} else {
|
|
|
|
lbinterface.$main.enterFullscreen();
|
|
|
|
}
|
2013-09-04 01:01:57 +00:00
|
|
|
} );
|
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 );
|
|
|
|
};
|
|
|
|
|
|
|
|
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] );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.LightboxInterface = LightboxInterface;
|
|
|
|
}( jQuery ) );
|