2013-05-01 08:46:16 +04:00
|
|
|
/*
|
2013-05-14 08:00:24 +04:00
|
|
|
* OpenSeadragon - Overlay
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 CodePlex Foundation
|
2013-05-14 07:32:09 +04:00
|
|
|
* Copyright (C) 2010-2013 OpenSeadragon contributors
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* - Neither the name of CodePlex Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
(function( $ ){
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
2014-02-05 23:57:47 +04:00
|
|
|
* An enumeration of positions that an overlay may be assigned relative to
|
|
|
|
* the viewport.
|
2016-03-22 22:50:48 +03:00
|
|
|
* It is identical to OpenSeadragon.Placement but is kept for backward
|
|
|
|
* compatibility.
|
2013-11-16 10:19:53 +04:00
|
|
|
* @member OverlayPlacement
|
|
|
|
* @memberof OpenSeadragon
|
2012-01-25 23:14:02 +04:00
|
|
|
* @static
|
2013-11-22 00:19:07 +04:00
|
|
|
* @type {Object}
|
|
|
|
* @property {Number} CENTER
|
|
|
|
* @property {Number} TOP_LEFT
|
|
|
|
* @property {Number} TOP
|
|
|
|
* @property {Number} TOP_RIGHT
|
|
|
|
* @property {Number} RIGHT
|
|
|
|
* @property {Number} BOTTOM_RIGHT
|
|
|
|
* @property {Number} BOTTOM
|
|
|
|
* @property {Number} BOTTOM_LEFT
|
|
|
|
* @property {Number} LEFT
|
2012-01-25 23:14:02 +04:00
|
|
|
*/
|
2016-03-22 22:50:48 +03:00
|
|
|
$.OverlayPlacement = $.Placement;
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
2013-11-16 10:19:53 +04:00
|
|
|
* @class Overlay
|
2013-11-25 20:48:44 +04:00
|
|
|
* @classdesc Provides a way to float an HTML element on top of the viewer element.
|
|
|
|
*
|
2013-11-16 10:19:53 +04:00
|
|
|
* @memberof OpenSeadragon
|
2013-11-25 20:48:44 +04:00
|
|
|
* @param {Object} options
|
|
|
|
* @param {Element} options.element
|
2014-02-05 23:57:47 +04:00
|
|
|
* @param {OpenSeadragon.Point|OpenSeadragon.Rect} options.location - The
|
|
|
|
* location of the overlay on the image. If a {@link OpenSeadragon.Point}
|
|
|
|
* is specified, the overlay will keep a constant size independently of the
|
2014-03-15 19:20:33 +04:00
|
|
|
* zoom. If a {@link OpenSeadragon.Rect} is specified, the overlay size will
|
2014-02-05 23:57:47 +04:00
|
|
|
* be adjusted when the zoom changes.
|
2016-03-22 22:50:48 +03:00
|
|
|
* @param {OpenSeadragon.Placement} [options.placement=OpenSeadragon.Placement.TOP_LEFT]
|
2014-02-05 23:57:47 +04:00
|
|
|
* Relative position to the viewport.
|
|
|
|
* Only used if location is a {@link OpenSeadragon.Point}.
|
2014-03-20 23:39:31 +04:00
|
|
|
* @param {OpenSeadragon.Overlay.OnDrawCallback} [options.onDraw]
|
|
|
|
* @param {Boolean} [options.checkResize=true] Set to false to avoid to
|
|
|
|
* check the size of the overlay everytime it is drawn when using a
|
|
|
|
* {@link OpenSeadragon.Point} as options.location. It will improve
|
|
|
|
* performances but will cause a misalignment if the overlay size changes.
|
2012-01-25 23:14:02 +04:00
|
|
|
*/
|
2012-02-02 01:56:04 +04:00
|
|
|
$.Overlay = function( element, location, placement ) {
|
2013-07-16 06:04:37 +04:00
|
|
|
|
2013-11-25 20:48:44 +04:00
|
|
|
/**
|
|
|
|
* onDraw callback signature used by {@link OpenSeadragon.Overlay}.
|
|
|
|
*
|
|
|
|
* @callback OnDrawCallback
|
|
|
|
* @memberof OpenSeadragon.Overlay
|
|
|
|
* @param {OpenSeadragon.Point} position
|
|
|
|
* @param {OpenSeadragon.Point} size
|
|
|
|
* @param {Element} element
|
|
|
|
*/
|
|
|
|
|
2013-07-16 06:04:37 +04:00
|
|
|
var options;
|
2014-02-05 23:57:47 +04:00
|
|
|
if ( $.isPlainObject( element ) ) {
|
2013-07-16 06:04:37 +04:00
|
|
|
options = element;
|
2014-02-05 23:57:47 +04:00
|
|
|
} else {
|
2013-07-16 06:04:37 +04:00
|
|
|
options = {
|
|
|
|
element: element,
|
|
|
|
location: location,
|
|
|
|
placement: placement
|
|
|
|
};
|
|
|
|
}
|
2014-11-04 04:14:17 +03:00
|
|
|
|
2013-07-16 06:04:37 +04:00
|
|
|
this.element = options.element;
|
|
|
|
this.scales = options.location instanceof $.Rect;
|
2011-12-30 02:14:42 +04:00
|
|
|
this.bounds = new $.Rect(
|
2013-07-16 06:04:37 +04:00
|
|
|
options.location.x,
|
|
|
|
options.location.y,
|
|
|
|
options.location.width,
|
|
|
|
options.location.height
|
2012-01-24 07:48:45 +04:00
|
|
|
);
|
2011-12-30 02:14:42 +04:00
|
|
|
this.position = new $.Point(
|
2013-07-16 06:04:37 +04:00
|
|
|
options.location.x,
|
|
|
|
options.location.y
|
2011-12-30 02:14:42 +04:00
|
|
|
);
|
|
|
|
this.size = new $.Point(
|
2013-07-16 06:04:37 +04:00
|
|
|
options.location.width,
|
|
|
|
options.location.height
|
2011-12-30 02:14:42 +04:00
|
|
|
);
|
2013-07-16 06:04:37 +04:00
|
|
|
this.style = options.element.style;
|
2011-12-14 02:38:36 +04:00
|
|
|
// rects are always top-left
|
2013-07-16 06:04:37 +04:00
|
|
|
this.placement = options.location instanceof $.Point ?
|
2016-03-22 22:50:48 +03:00
|
|
|
options.placement : $.Placement.TOP_LEFT;
|
2013-07-16 06:04:37 +04:00
|
|
|
this.onDraw = options.onDraw;
|
2014-03-20 23:39:31 +04:00
|
|
|
this.checkResize = options.checkResize === undefined ?
|
|
|
|
true : options.checkResize;
|
2011-12-14 02:38:36 +04:00
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2016-01-25 00:09:18 +03:00
|
|
|
/** @lends OpenSeadragon.Overlay.prototype */
|
|
|
|
$.Overlay.prototype = {
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
2016-03-22 22:50:48 +03:00
|
|
|
* @param {OpenSeadragon.Point} position
|
2012-02-15 23:50:27 +04:00
|
|
|
* @param {OpenSeadragon.Point} size
|
|
|
|
*/
|
2016-03-22 22:50:48 +03:00
|
|
|
adjust: function(position, size) {
|
|
|
|
var properties = $.Placement.properties[this.placement];
|
|
|
|
if (!properties) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (properties.isHorizontallyCentered) {
|
|
|
|
position.x -= size.x / 2;
|
|
|
|
} else if (properties.isRight) {
|
|
|
|
position.x -= size.x;
|
|
|
|
}
|
|
|
|
if (properties.isVerticallyCentered) {
|
|
|
|
position.y -= size.y / 2;
|
|
|
|
} else if (properties.isBottom) {
|
|
|
|
position.y -= size.y;
|
2011-12-14 02:38:36 +04:00
|
|
|
}
|
|
|
|
},
|
2012-01-24 07:48:45 +04:00
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
*/
|
2011-12-14 02:38:36 +04:00
|
|
|
destroy: function() {
|
2012-02-02 01:56:04 +04:00
|
|
|
var element = this.element,
|
2012-01-24 07:48:45 +04:00
|
|
|
style = this.style;
|
2011-12-14 02:38:36 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( element.parentNode ) {
|
|
|
|
element.parentNode.removeChild( element );
|
2013-02-11 07:53:51 +04:00
|
|
|
//this should allow us to preserve overlays when required between
|
|
|
|
//pages
|
2014-01-31 00:38:37 +04:00
|
|
|
if ( element.prevElementParent ) {
|
2013-02-11 07:53:51 +04:00
|
|
|
style.display = 'none';
|
2013-06-19 21:33:25 +04:00
|
|
|
//element.prevElementParent.insertBefore(
|
2013-02-11 07:53:51 +04:00
|
|
|
// element,
|
|
|
|
// element.prevNextSibling
|
|
|
|
//);
|
|
|
|
document.body.appendChild( element );
|
|
|
|
}
|
2011-12-14 02:38:36 +04:00
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2013-08-07 04:54:20 +04:00
|
|
|
// clear the onDraw callback
|
|
|
|
this.onDraw = null;
|
|
|
|
|
2011-12-14 02:38:36 +04:00
|
|
|
style.top = "";
|
|
|
|
style.left = "";
|
|
|
|
style.position = "";
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( this.scales ) {
|
2011-12-14 02:38:36 +04:00
|
|
|
style.width = "";
|
|
|
|
style.height = "";
|
|
|
|
}
|
|
|
|
},
|
2012-01-24 07:48:45 +04:00
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {Element} container
|
|
|
|
*/
|
2013-08-14 23:43:49 +04:00
|
|
|
drawHTML: function( container, viewport ) {
|
2012-02-02 01:56:04 +04:00
|
|
|
var element = this.element,
|
2011-12-30 02:14:42 +04:00
|
|
|
style = this.style,
|
|
|
|
scales = this.scales,
|
2014-01-31 00:38:37 +04:00
|
|
|
degrees = viewport.degrees,
|
|
|
|
position = viewport.pixelFromPoint(
|
|
|
|
this.bounds.getTopLeft(),
|
|
|
|
true
|
2014-03-15 19:51:36 +04:00
|
|
|
),
|
2014-03-20 23:39:31 +04:00
|
|
|
size,
|
2013-08-14 23:43:49 +04:00
|
|
|
overlayCenter;
|
2011-12-30 02:14:42 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( element.parentNode != container ) {
|
2013-02-11 07:53:51 +04:00
|
|
|
//save the source parent for later if we need it
|
|
|
|
element.prevElementParent = element.parentNode;
|
|
|
|
element.prevNextSibling = element.nextSibling;
|
2012-01-24 07:48:45 +04:00
|
|
|
container.appendChild( element );
|
2014-03-20 23:39:31 +04:00
|
|
|
this.size = $.getElementSize( element );
|
2011-12-14 02:38:36 +04:00
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2014-03-20 23:39:31 +04:00
|
|
|
if ( scales ) {
|
|
|
|
size = viewport.deltaPixelsFromPoints(
|
|
|
|
this.bounds.getSize(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
} else if ( this.checkResize ) {
|
|
|
|
size = $.getElementSize( element );
|
|
|
|
} else {
|
|
|
|
size = this.size;
|
2011-12-14 02:38:36 +04:00
|
|
|
}
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2014-01-31 00:38:37 +04:00
|
|
|
this.position = position;
|
|
|
|
this.size = size;
|
2011-12-14 02:38:36 +04:00
|
|
|
|
2011-12-30 02:14:42 +04:00
|
|
|
this.adjust( position, size );
|
2011-12-14 02:38:36 +04:00
|
|
|
|
2015-10-09 21:15:54 +03:00
|
|
|
position = position.apply( Math.round );
|
|
|
|
size = size.apply( Math.round );
|
2013-07-16 06:04:37 +04:00
|
|
|
|
2013-08-14 23:43:49 +04:00
|
|
|
// rotate the position of the overlay
|
2013-08-16 02:15:20 +04:00
|
|
|
// TODO only rotate overlays if in canvas mode
|
|
|
|
// TODO replace the size rotation with CSS3 transforms
|
|
|
|
// TODO add an option to overlays to not rotate with the image
|
|
|
|
// Currently only rotates position and size
|
|
|
|
if( degrees !== 0 && this.scales ) {
|
2013-08-14 23:43:49 +04:00
|
|
|
overlayCenter = new $.Point( size.x / 2, size.y / 2 );
|
|
|
|
|
2014-03-20 23:39:31 +04:00
|
|
|
var drawerCenter = new $.Point(
|
|
|
|
viewport.viewer.drawer.canvas.width / 2,
|
|
|
|
viewport.viewer.drawer.canvas.height / 2
|
|
|
|
);
|
2013-08-14 23:43:49 +04:00
|
|
|
position = position.plus( overlayCenter ).rotate(
|
|
|
|
degrees,
|
|
|
|
drawerCenter
|
|
|
|
).minus( overlayCenter );
|
|
|
|
|
|
|
|
size = size.rotate( degrees, new $.Point( 0, 0 ) );
|
|
|
|
size = new $.Point( Math.abs( size.x ), Math.abs( size.y ) );
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:39:31 +04:00
|
|
|
// call the onDraw callback if it exists to allow one to overwrite
|
2013-08-01 07:36:00 +04:00
|
|
|
// the drawing/positioning/sizing of the overlay
|
2014-03-20 23:39:31 +04:00
|
|
|
if ( this.onDraw ) {
|
|
|
|
this.onDraw( position, size, element );
|
2013-07-31 11:01:48 +04:00
|
|
|
} else {
|
2013-07-16 06:04:37 +04:00
|
|
|
style.left = position.x + "px";
|
|
|
|
style.top = position.y + "px";
|
|
|
|
style.position = "absolute";
|
2015-06-24 01:04:40 +03:00
|
|
|
|
2015-06-24 01:08:18 +03:00
|
|
|
if (style.display != 'none') {
|
2015-06-24 01:04:40 +03:00
|
|
|
style.display = 'block';
|
2015-06-24 01:08:18 +03:00
|
|
|
}
|
2013-07-16 06:04:37 +04:00
|
|
|
|
|
|
|
if ( scales ) {
|
|
|
|
style.width = size.x + "px";
|
|
|
|
style.height = size.y + "px";
|
|
|
|
}
|
|
|
|
}
|
2011-12-14 02:38:36 +04:00
|
|
|
},
|
2012-01-24 07:48:45 +04:00
|
|
|
|
2012-02-15 23:50:27 +04:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @param {OpenSeadragon.Point|OpenSeadragon.Rect} location
|
2016-03-22 22:50:48 +03:00
|
|
|
* @param {OpenSeadragon.Placement} position
|
2012-02-15 23:50:27 +04:00
|
|
|
*/
|
2012-01-24 07:48:45 +04:00
|
|
|
update: function( location, placement ) {
|
|
|
|
this.scales = location instanceof $.Rect;
|
2013-06-19 21:33:25 +04:00
|
|
|
this.bounds = new $.Rect(
|
|
|
|
location.x,
|
|
|
|
location.y,
|
|
|
|
location.width,
|
2012-01-24 07:48:45 +04:00
|
|
|
location.height
|
|
|
|
);
|
2011-12-30 02:14:42 +04:00
|
|
|
// rects are always top-left
|
2012-01-24 07:48:45 +04:00
|
|
|
this.placement = location instanceof $.Point ?
|
2016-03-22 22:50:48 +03:00
|
|
|
placement : $.Placement.TOP_LEFT;
|
2016-02-18 01:43:53 +03:00
|
|
|
},
|
2016-02-23 20:22:53 +03:00
|
|
|
|
2016-02-18 01:43:53 +03:00
|
|
|
/**
|
|
|
|
* @function
|
|
|
|
* @returns {OpenSeadragon.Rect} overlay bounds
|
|
|
|
*/
|
|
|
|
getBounds: function() {
|
2016-02-20 00:44:33 +03:00
|
|
|
return this.bounds.clone();
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
2011-12-14 02:38:36 +04:00
|
|
|
};
|
2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
}( OpenSeadragon ));
|