var selectionRectangle = $("<div id='text-select-rect'></div>").css('display', 'none');
var $scrollView = $('#DocumentViewer');
var oldMouseDown = Tools.TextSelectTool.prototype.mouseLeftDown;
Tools.TextSelectTool.prototype.mouseLeftDown = function(e) {
$('#viewer').append(selectionRectangle);
oldMouseDown.apply(this, arguments);
this.mousePt1 = e.data.mouseCoordinate;
};
var oldMouseMove = Tools.TextSelectTool.prototype.mouseMove;
Tools.TextSelectTool.prototype.mouseMove = function(e) {
oldMouseMove.apply(this, arguments);
if (!this.mousePt1 || !e.data || !e.data.pageCoordinate) {
return;
}
var mousePt2 = e.data.mouseCoordinate;
var pageIndex = e.data.pageCoordinate.pageIndex;
var scrollLeft = $scrollView.scrollLeft();
var scrollTop = $scrollView.scrollTop();
var screenW = mousePt2.x - this.mousePt1.x;
var screenH = mousePt2.y - this.mousePt1.y;
var pt1 = {
x: this.mousePt1.x - scrollLeft,
y: this.mousePt1.y - scrollTop
};
var pt2 = {
x: mousePt2.x - scrollLeft,
y: mousePt2.y - scrollTop
};
var pageWidth = readerControl.docViewer.getPageWidth(pageIndex);
var pageHeight = readerControl.docViewer.getPageHeight(pageIndex);
var rectTop = Math.min(pt2.y, pt1.y);
var rectLeft = Math.min(pt2.x, pt1.x);
var displayMode = readerControl.docViewer.getDisplayModeManager().getDisplayMode();
var topLeftPagePoint = displayMode.pageToWindow({
x: 0,
y: 0
}, pageIndex);
var bottomRightPagePoint = displayMode.pageToWindow({
x: pageWidth,
y: pageHeight
}, pageIndex);
var minWinPoint = {
x: Math.min(topLeftPagePoint.x, bottomRightPagePoint.x),
y: Math.min(topLeftPagePoint.y, bottomRightPagePoint.y)
};
var maxWinPoint = {
x: Math.max(topLeftPagePoint.x, bottomRightPagePoint.x),
y: Math.max(topLeftPagePoint.y, bottomRightPagePoint.y)
};
minWinPoint.x -= scrollLeft;
minWinPoint.y -= scrollTop;
if (minWinPoint.y > rectTop) {
screenH += (minWinPoint.y - rectTop);
rectTop = minWinPoint.y;
}
if (minWinPoint.x > rectLeft) {
screenW += (minWinPoint.x - rectLeft);
rectLeft = minWinPoint.x;
}
maxWinPoint.x -= scrollLeft;
maxWinPoint.y -= scrollTop;
var rectWidth = clamp(Math.abs(screenW), 0, maxWinPoint.x - rectLeft);
var rectHeight = clamp(Math.abs(screenH), 0, maxWinPoint.y - rectTop);
selectionRectangle.css({
"width": rectWidth,
"height": rectHeight,
"top": rectTop,
"left": rectLeft,
"z-index": "44",
"position": "absolute",
"border": "1px dotted black",
"display": "inline"
});
};
var oldMouseUp = Tools.TextSelectTool.prototype.mouseLeftUp;
Tools.TextSelectTool.prototype.mouseLeftUp = function(e) {
oldMouseUp.apply(this, arguments);
selectionRectangle.css('display', 'none').detach();
this.mousePt1 = null;
};
function clamp(value, bottom, top) {
top = (top !== undefined) ? (top) : (Infinity);
bottom = (bottom !== undefined) ? (bottom) : (-Infinity);
return Math.max(Math.min(value, top), bottom);
}