openseadragon/test/demo/m2/js/main.js

610 lines
20 KiB
JavaScript
Raw Normal View History

/* globals $, App, d3 */
2015-01-09 00:22:23 +03:00
(function() {
// ----------
window.App = {
// ----------
init: function() {
var self = this;
2015-01-10 02:46:57 +03:00
var count = 70;
2015-01-09 00:22:23 +03:00
this.mode = 'none';
this.pageBuffer = 0.05;
2015-01-10 00:53:48 +03:00
this.bigBuffer = 0.2;
2015-01-09 00:22:23 +03:00
this.page = 0;
this.modeNames = [
'thumbs',
'scroll',
'book',
'page'
];
2015-01-21 03:10:24 +03:00
var highsmith = {
Image: {
xmlns: "http://schemas.microsoft.com/deepzoom/2008",
Url: "http://openseadragon.github.io/example-images/highsmith/highsmith_files/",
Format: "jpg",
Overlap: "2",
TileSize: "256",
Size: {
Height: "9221",
Width: "7026"
}
}
};
// this.tileSources = [];
// for (var i = 0; i < count; i++) {
// this.tileSources.push(highsmith);
// }
2015-01-09 00:22:23 +03:00
this.viewer = OpenSeadragon({
id: "contentDiv",
prefixUrl: "../../../build/openseadragon/images/",
2015-01-09 22:45:53 +03:00
autoResize: false,
2015-01-14 02:33:03 +03:00
showHomeControl: false,
2015-01-09 22:45:53 +03:00
// animationTime: 10,
// springStiffness: 2,
2015-01-10 02:46:57 +03:00
tileSources: this.tileSources.slice(0, count)
2015-01-09 00:22:23 +03:00
});
this.viewer.addHandler('open', function() {
2015-01-09 22:45:53 +03:00
self.$el = $(self.viewer.element);
2015-01-16 01:32:42 +03:00
self.setMode({
mode: 'thumbs'
});
2015-01-09 00:22:23 +03:00
});
2015-01-10 02:46:57 +03:00
this.viewer.addHandler('canvas-drag', function() {
2015-01-20 22:23:56 +03:00
if (this.mode === 'scroll') {
var result = this.hitTest(this.viewer.viewport.getCenter());
2015-01-14 02:33:03 +03:00
if (result) {
2015-01-20 22:23:56 +03:00
this.page = result.index;
2015-01-14 02:33:03 +03:00
}
2015-01-09 01:04:31 +03:00
}
});
2015-01-20 22:23:56 +03:00
this.viewer.addHandler('viewport-change', function(event) {
self.applyConstraints();
});
2015-01-09 00:22:23 +03:00
$.each(this.modeNames, function(i, v) {
$('.' + v).click(function() {
2015-01-16 01:32:42 +03:00
self.setMode({
mode: v
});
2015-01-09 00:22:23 +03:00
});
});
$('.next').click(function() {
2015-01-10 02:11:11 +03:00
self.next();
2015-01-09 00:22:23 +03:00
});
$('.previous').click(function() {
2015-01-10 02:11:11 +03:00
self.previous();
});
$(window).keyup(function(event) {
if (self.mode === 'thumbs') {
return;
2015-01-09 00:22:23 +03:00
}
2015-01-10 02:11:11 +03:00
if (event.which === 39) { // Right arrow
self.next();
} else if (event.which === 37) { // Left arrow
self.previous();
}
2015-01-09 00:22:23 +03:00
});
2015-01-21 03:10:24 +03:00
this.$scrollInner = $('.scroll-inner');
this.$scrollCover = $('.scroll-cover')
.scroll(function(event) {
var info = self.getScrollInfo();
if (!info || self.ignoreScroll) {
return;
}
var pos = new OpenSeadragon.Point(info.thumbBounds.getCenter().x,
info.thumbBounds.y + (info.viewportHeight / 2) +
(info.viewportMax * info.scrollFactor));
self.viewer.viewport.panTo(pos, true);
})
.mousemove(function(event) {
var pixel = new OpenSeadragon.Point(event.clientX, event.clientY);
var result = self.hitTest(self.viewer.viewport.pointFromPixel(pixel));
self.updateHover(result ? result.index : -1);
})
.click(function(event) {
var pixel = new OpenSeadragon.Point(event.clientX, event.clientY);
var result = self.hitTest(self.viewer.viewport.pointFromPixel(pixel));
if (result) {
self.setMode({
mode: 'page',
page: result.index
});
}
});
var svgNode = this.viewer.svgOverlay();
this.highlight = d3.select(svgNode).append("rect")
.style('fill', 'none')
.style('stroke', '#08f')
.style('opacity', 0)
.style('stroke-width', 0.05)
.attr("pointer-events", "none");
this.hover = d3.select(svgNode).append("rect")
.style('fill', 'none')
.style('stroke', '#08f')
.style('opacity', 0)
.style('stroke-width', 0.05)
.attr("pointer-events", "none");
$(window).resize(function() {
2015-01-16 01:32:42 +03:00
var newSize = new OpenSeadragon.Point(self.$el.width(), self.$el.height());
self.viewer.viewport.resize(newSize, false);
self.setMode({
mode: self.mode,
immediately: true
});
2015-01-21 04:19:20 +03:00
self.viewer.forceRedraw();
self.viewer.svgOverlay('resize');
});
2015-01-09 00:22:23 +03:00
this.update();
},
2015-01-10 02:11:11 +03:00
// ----------
next: function() {
var page = this.page + (this.mode === 'book' ? 2 : 1);
if (this.mode === 'book' && page % 2 === 0 && page !== 0) {
page --;
}
2015-01-16 01:32:42 +03:00
this.goToPage({
page: page
});
2015-01-10 02:11:11 +03:00
},
// ----------
previous: function() {
var page = this.page - (this.mode === 'book' ? 2 : 1);
if (this.mode === 'book' && page % 2 === 0 && page !== 0) {
page --;
}
2015-01-16 01:32:42 +03:00
this.goToPage({
page: page
});
2015-01-10 02:11:11 +03:00
},
// ----------
hitTest: function(pos) {
var count = this.viewer.world.getItemCount();
var item, box;
for (var i = 0; i < count; i++) {
item = this.viewer.world.getItemAt(i);
box = item.getBounds();
if (pos.x > box.x && pos.y > box.y && pos.x < box.x + box.width &&
pos.y < box.y + box.height) {
return {
item: item,
index: i
};
}
}
return null;
},
2015-01-21 03:10:24 +03:00
// ----------
getScrollInfo: function() {
if (!this.thumbBounds) {
return null;
}
var output = {};
var viewerWidth = this.$el.width();
var viewerHeight = this.$el.height();
var scrollTop = this.$scrollCover.scrollTop();
output.scrollMax = this.$scrollInner.height() - this.$scrollCover.height();
output.scrollFactor = (output.scrollMax > 0 ? scrollTop / output.scrollMax : 0);
output.thumbBounds = this.thumbBounds;
output.viewportHeight = output.thumbBounds.width * (viewerHeight / viewerWidth);
output.viewportMax = Math.max(0, output.thumbBounds.height - output.viewportHeight);
return output;
},
2015-01-09 00:22:23 +03:00
// ----------
update: function() {
var self = this;
2015-01-10 02:11:11 +03:00
$('.nav').toggle(this.mode === 'scroll' || this.mode === 'book' || this.mode === 'page');
2015-01-09 00:22:23 +03:00
$('.previous').toggleClass('hidden', this.page <= 0);
$('.next').toggleClass('hidden', this.page >= this.viewer.world.getItemCount() - 1);
$.each(this.modeNames, function(i, v) {
$('.' + v).toggleClass('active', v === self.mode);
});
},
2015-01-20 22:23:56 +03:00
// ----------
applyConstraints: function() {
if (this.mode === 'thumbs') {
return;
}
if (this.panBounds) {
var center = this.viewer.viewport.getCenter(true);
var viewBounds = this.viewer.viewport.getBounds(true);
var bounds = this.panBounds.clone();
var left = bounds.x + (viewBounds.width / 2);
var top = bounds.y + (viewBounds.height / 2);
var right = (bounds.x + bounds.width) - (viewBounds.width / 2);
var bottom = (bounds.y + bounds.height) - (viewBounds.height / 2);
var x;
if (left <= right) {
x = Math.max(left, Math.min(right, center.x));
} else {
x = bounds.x + (bounds.width / 2);
}
var y;
if (top <= bottom) {
y = Math.max(top, Math.min(bottom, center.y));
} else {
y = bounds.y + (bounds.height / 2);
}
if (x !== center.x || y !== center.y) {
this.viewer.viewport.panTo(new OpenSeadragon.Point(x, y), true);
}
}
},
2015-01-09 00:22:23 +03:00
// ----------
2015-01-16 01:32:42 +03:00
setMode: function(config) {
2015-01-09 22:45:53 +03:00
var self = this;
2015-01-16 01:32:42 +03:00
this.mode = config.mode;
2015-01-09 00:22:23 +03:00
2015-01-16 01:32:42 +03:00
if (config.page !== undefined) {
this.page = config.page; // Need to do this before layout
2015-01-09 01:04:31 +03:00
}
2015-01-21 03:10:24 +03:00
this.ignoreScroll = true;
this.thumbBounds = null;
2015-01-10 00:53:48 +03:00
2015-01-21 03:10:24 +03:00
var layout = this.createLayout();
2015-01-09 22:45:53 +03:00
if (this.mode === 'thumbs') {
this.viewer.gestureSettingsMouse.scrollToZoom = false;
this.viewer.zoomPerClick = 1;
2015-01-14 02:33:03 +03:00
this.viewer.panHorizontal = false;
this.viewer.panVertical = false;
2015-01-10 00:53:48 +03:00
var viewerWidth = this.$el.width();
var width = layout.bounds.width + (this.bigBuffer * 2);
var height = layout.bounds.height + (this.bigBuffer * 2);
var newHeight = viewerWidth * (height / width);
2015-01-21 03:10:24 +03:00
this.$scrollCover.show();
this.$scrollInner
2015-01-09 22:45:53 +03:00
.css({
2015-01-10 00:53:48 +03:00
height: newHeight
2015-01-09 22:45:53 +03:00
});
} else {
this.viewer.gestureSettingsMouse.scrollToZoom = true;
this.viewer.zoomPerClick = 2;
2015-01-14 02:33:03 +03:00
this.viewer.panHorizontal = true;
this.viewer.panVertical = true;
2015-01-21 03:10:24 +03:00
this.$scrollCover.hide();
2015-01-09 22:45:53 +03:00
}
2015-01-16 01:32:42 +03:00
this.setLayout({
layout: layout,
immediately: config.immediately
});
2015-01-09 00:22:23 +03:00
2015-01-21 03:10:24 +03:00
if (this.mode === 'thumbs') {
// Set up thumbBounds
this.thumbBounds = this.viewer.world.getHomeBounds();
this.thumbBounds.x -= this.bigBuffer;
this.thumbBounds.y -= this.bigBuffer;
this.thumbBounds.width += (this.bigBuffer * 2);
this.thumbBounds.height += (this.bigBuffer * 2);
// Scroll to the appropriate location
var info = this.getScrollInfo();
var viewportBounds = this.thumbBounds.clone();
viewportBounds.y += info.viewportMax * info.scrollFactor;
viewportBounds.height = info.viewportHeight;
var itemBounds = this.viewer.world.getItemAt(this.page).getBounds();
var top = itemBounds.y - this.bigBuffer;
var bottom = top + itemBounds.height + (this.bigBuffer * 2);
var normalY;
if (top < viewportBounds.y) {
normalY = top - this.thumbBounds.y;
} else if (bottom > viewportBounds.y + viewportBounds.height) {
normalY = (bottom - info.viewportHeight) - this.thumbBounds.y;
}
if (normalY !== undefined) {
var viewportFactor = normalY / info.viewportMax;
this.$scrollCover.scrollTop(info.scrollMax * viewportFactor);
}
}
2015-01-16 01:32:42 +03:00
this.goHome({
immediately: config.immediately
});
2015-01-09 01:04:31 +03:00
2015-01-14 02:33:03 +03:00
this.viewer.viewport.minZoomLevel = this.viewer.viewport.getZoom();
2015-01-09 00:22:23 +03:00
this.update();
this.updateHighlight();
this.updateHover(-1);
2015-01-21 03:10:24 +03:00
2015-01-21 04:19:20 +03:00
clearTimeout(this.scrollTimeout);
this.scrollTimeout = setTimeout(function() {
2015-01-21 03:10:24 +03:00
self.ignoreScroll = false;
}, this.viewer.animationTime * 1000);
},
// ----------
updateHighlight: function() {
if (this.mode !== 'thumbs') {
this.highlight.style('opacity', 0);
return;
}
var item = this.viewer.world.getItemAt(this.page);
var box = item.getBounds();
this.highlight
.style('opacity', 1)
.attr("x", box.x)
.attr("width", box.width)
.attr("y", box.y)
.attr("height", box.height);
},
// ----------
updateHover: function(page) {
if (page === -1 || this.mode !== 'thumbs') {
this.hover.style('opacity', 0);
2015-01-21 03:10:24 +03:00
this.$scrollCover.css({
'cursor': 'default'
});
return;
}
2015-01-21 03:10:24 +03:00
this.$scrollCover.css({
'cursor': 'pointer'
});
var item = this.viewer.world.getItemAt(page);
var box = item.getBounds();
this.hover
.style('opacity', 0.3)
.attr("x", box.x)
.attr("width", box.width)
.attr("y", box.y)
.attr("height", box.height);
2015-01-09 00:22:23 +03:00
},
// ----------
2015-01-16 01:32:42 +03:00
goToPage: function(config) {
2015-01-20 22:23:56 +03:00
var self = this;
2015-01-14 02:33:03 +03:00
var itemCount = this.viewer.world.getItemCount();
2015-01-16 01:32:42 +03:00
this.page = Math.max(0, Math.min(itemCount - 1, config.page));
2015-01-09 00:22:23 +03:00
var viewerWidth = this.$el.width();
var viewerHeight = this.$el.height();
2015-01-09 00:22:23 +03:00
var bounds = this.viewer.world.getItemAt(this.page).getBounds();
var x = bounds.x;
var y = bounds.y;
var width = bounds.width;
var height = bounds.height;
2015-01-14 02:33:03 +03:00
var box;
2015-01-09 00:22:23 +03:00
if (this.mode === 'book') {
var item;
if (this.page % 2) { // First in a pair
item = this.viewer.world.getItemAt(this.page + 1);
if (item) {
width += item.getBounds().width;
}
} else {
item = this.viewer.world.getItemAt(this.page - 1);
if (item) {
2015-01-14 02:33:03 +03:00
box = item.getBounds();
2015-01-09 00:22:23 +03:00
x -= width;
width += box.width;
}
}
}
x -= this.pageBuffer;
y -= this.pageBuffer;
width += (this.pageBuffer * 2);
height += (this.pageBuffer * 2);
if (this.mode === 'scroll') {
if (this.page === 0) {
2015-01-10 02:11:11 +03:00
x = bounds.x - this.pageBuffer;
width = height * (viewerWidth / viewerHeight);
} else if (this.page === this.viewer.world.getItemCount() - 1) {
width = height * (viewerWidth / viewerHeight);
x = (bounds.x + bounds.width + this.pageBuffer) - width;
}
}
2015-01-14 02:33:03 +03:00
box = new OpenSeadragon.Rect(x, y, width, height);
2015-01-16 01:32:42 +03:00
this.viewer.viewport.fitBounds(box, config.immediately);
2015-01-14 02:33:03 +03:00
2015-01-20 22:23:56 +03:00
this.panBounds = null;
2015-01-14 02:33:03 +03:00
2015-01-20 22:23:56 +03:00
var setPanBounds = function() {
if (self.mode === 'page' || self.mode === 'book') {
self.panBounds = box;
} else if (self.mode === 'scroll') {
self.panBounds = self.viewer.world.getItemAt(0).getBounds()
.union(self.viewer.world.getItemAt(itemCount - 1).getBounds());
self.panBounds.x -= self.pageBuffer;
self.panBounds.y -= self.pageBuffer;
self.panBounds.width += (self.pageBuffer * 2);
self.panBounds.height += (self.pageBuffer * 2);
}
};
if (config.immediately) {
setPanBounds();
} else {
setTimeout(setPanBounds, this.viewer.animationTime * 1000);
2015-01-14 02:33:03 +03:00
}
this.viewer.viewport.minZoomLevel = this.viewer.viewport.getZoom();
2015-01-09 00:22:23 +03:00
this.update();
},
// ----------
2015-01-10 00:53:48 +03:00
createLayout: function() {
var viewerWidth = this.$el.width();
var viewerHeight = this.$el.height();
var layoutConfig = {};
if (this.mode === 'thumbs') {
layoutConfig.columns = Math.floor(viewerWidth / 150);
layoutConfig.buffer = this.bigBuffer;
2015-01-14 03:15:40 +03:00
layoutConfig.sameWidth = true;
2015-01-10 00:53:48 +03:00
} else if (this.mode === 'scroll') {
layoutConfig.buffer = this.pageBuffer;
} else if (this.mode === 'book' || this.mode === 'page') {
layoutConfig.book = (this.mode === 'book');
var height = 1 + (this.pageBuffer * 2);
// Note that using window here is approximate, but that's close enough.
// We can't use viewer, because it may be stretched for the thumbs view.
layoutConfig.buffer = (height * ($(window).width() / $(window).height())) / 2;
2015-01-10 00:53:48 +03:00
}
var layout = {
bounds: null,
specs: []
};
2015-01-09 00:22:23 +03:00
var count = this.viewer.world.getItemCount();
var x = 0;
var y = 0;
2015-01-14 03:15:40 +03:00
var offset = new OpenSeadragon.Point();
var rowHeight = 0;
var item, box;
2015-01-09 00:22:23 +03:00
for (var i = 0; i < count; i++) {
item = this.viewer.world.getItemAt(i);
2015-01-14 03:15:40 +03:00
box = item.getBounds();
if (i === this.page) {
offset = box.getTopLeft().minus(new OpenSeadragon.Point(x, y));
}
box.x = x;
box.y = y;
if (layoutConfig.sameWidth) {
box.height = box.height / box.width;
box.width = 1;
} else {
box.width = box.width / box.height;
box.height = 1;
}
rowHeight = Math.max(rowHeight, box.height);
layout.specs.push({
item: item,
bounds: box
});
2015-01-10 00:53:48 +03:00
if (layoutConfig.columns && i % layoutConfig.columns === layoutConfig.columns - 1) {
2015-01-09 00:22:23 +03:00
x = 0;
2015-01-14 03:15:40 +03:00
y += rowHeight + layoutConfig.buffer;
rowHeight = 0;
2015-01-09 00:22:23 +03:00
} else {
2015-01-10 00:53:48 +03:00
if (!layoutConfig.book || i % 2 === 0) {
x += layoutConfig.buffer;
2015-01-09 00:22:23 +03:00
}
2015-01-14 03:15:40 +03:00
x += box.width;
2015-01-09 00:22:23 +03:00
}
}
2015-01-09 01:04:31 +03:00
2015-01-14 03:15:40 +03:00
var pos, spec;
2015-01-09 01:04:31 +03:00
for (i = 0; i < count; i++) {
2015-01-14 03:15:40 +03:00
spec = layout.specs[i];
pos = spec.bounds.getTopLeft().plus(offset);
spec.bounds.x = pos.x;
spec.bounds.y = pos.y;
2015-01-10 00:53:48 +03:00
if (layout.bounds) {
2015-01-14 03:15:40 +03:00
layout.bounds = layout.bounds.union(spec.bounds);
2015-01-10 00:53:48 +03:00
} else {
2015-01-14 03:15:40 +03:00
layout.bounds = spec.bounds.clone();
2015-01-10 00:53:48 +03:00
}
2015-01-09 01:04:31 +03:00
}
2015-01-10 00:53:48 +03:00
return layout;
2015-01-09 00:22:23 +03:00
},
// ----------
2015-01-16 01:32:42 +03:00
setLayout: function(config) {
2015-01-10 00:53:48 +03:00
var spec;
2015-01-09 00:22:23 +03:00
2015-01-16 01:32:42 +03:00
for (var i = 0; i < config.layout.specs.length; i++) {
spec = config.layout.specs[i];
spec.item.setPosition(spec.bounds.getTopLeft(), config.immediately);
spec.item.setWidth(spec.bounds.width, config.immediately);
2015-01-10 00:53:48 +03:00
}
2015-01-09 00:22:23 +03:00
},
// ----------
2015-01-16 01:32:42 +03:00
goHome: function(config) {
2015-01-10 00:53:48 +03:00
var viewerWidth = this.$el.width();
var viewerHeight = this.$el.height();
var layoutConfig = {};
2015-01-09 00:22:23 +03:00
2015-01-10 00:53:48 +03:00
if (this.mode === 'thumbs') {
2015-01-21 03:10:24 +03:00
var info = this.getScrollInfo();
var box = this.thumbBounds.clone();
2015-01-10 00:53:48 +03:00
box.height = box.width * (viewerHeight / viewerWidth);
2015-01-21 03:10:24 +03:00
box.y += info.viewportMax * info.scrollFactor;
2015-01-16 01:32:42 +03:00
this.viewer.viewport.fitBounds(box, config.immediately);
2015-01-14 02:33:03 +03:00
} else {
2015-01-16 01:32:42 +03:00
this.goToPage({
page: this.page,
immediately: config.immediately
});
2015-01-10 00:53:48 +03:00
}
2015-01-09 00:22:23 +03:00
}
};
// ----------
$(document).ready(function() {
App.init();
});
})();