/* globals $, App, d3 */ (function() { // ---------- window.App = { // ---------- init: function() { var self = this; this.maxImages = 500; this.mode = 'none'; this.pageBuffer = 0.05; this.bigBuffer = 0.2; this.pageIndex = 0; this.modeNames = [ 'thumbs', 'scroll', 'book', 'page' ]; this.pages = this.createPages(); var tileSources = $.map(this.pages, function(v, i) { return v.tileSource; }); this.viewer = OpenSeadragon({ id: "contentDiv", prefixUrl: "../../../build/openseadragon/images/", autoResize: false, showHomeControl: false, tileSources: tileSources }); this.viewer.addHandler('open', function() { self.$el = $(self.viewer.element); $.each(self.pages, function(i, v) { v.tiledImage = self.viewer.world.getItemAt(i); }); self.setMode({ mode: 'thumbs', immediately: true }); }); this.viewer.addHandler('canvas-drag', function() { if (self.mode === 'scroll') { var result = self.hitTest(self.viewer.viewport.getCenter()); if (result) { self.pageIndex = result.index; self.update(); } } }); this.viewer.addHandler('viewport-change', function(event) { self.applyConstraints(); }); $.each(this.modeNames, function(i, v) { $('.' + v).click(function() { self.setMode({ mode: v }); }); }); $('.next').click(function() { self.next(); }); $('.previous').click(function() { self.previous(); }); $(window).keyup(function(event) { if (self.mode === 'thumbs') { return; } if (event.which === 39) { // Right arrow self.next(); } else if (event.which === 37) { // Left arrow self.previous(); } }); 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); pixel.y -= self.$scrollCover.position().top; 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); pixel.y -= self.$scrollCover.position().top; var result = self.hitTest(self.viewer.viewport.pointFromPixel(pixel)); if (result) { self.setMode({ mode: 'page', pageIndex: 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() { var newSize = new OpenSeadragon.Point(self.$el.width(), self.$el.height()); self.viewer.viewport.resize(newSize, false); self.setMode({ mode: self.mode, immediately: true }); self.viewer.forceRedraw(); self.viewer.svgOverlay('resize'); }); this.update(); }, // ---------- next: function() { var pageIndex = this.pageIndex + (this.mode === 'book' ? 2 : 1); if (this.mode === 'book' && pageIndex % 2 === 0 && pageIndex !== 0) { pageIndex --; } this.goToPage({ pageIndex: pageIndex }); }, // ---------- previous: function() { var pageIndex = this.pageIndex - (this.mode === 'book' ? 2 : 1); if (this.mode === 'book' && pageIndex % 2 === 0 && pageIndex !== 0) { pageIndex --; } this.goToPage({ pageIndex: pageIndex }); }, // ---------- hitTest: function(pos) { var count = this.pages.length; var item, box; for (var i = 0; i < count; i++) { item = this.pages[i].tiledImage; 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; }, // ---------- 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; }, // ---------- update: function() { var self = this; $('.nav').toggle(this.mode === 'scroll' || this.mode === 'book' || this.mode === 'page'); $('.previous').toggleClass('hidden', this.pageIndex <= 0); $('.next').toggleClass('hidden', this.pageIndex >= this.pages.length - 1); $.each(this.modeNames, function(i, v) { $('.' + v).toggleClass('active', v === self.mode); }); // alternates menu if (this.$alternates) { this.$alternates.remove(); this.$alternates = null; } var page = this.pages[this.pageIndex]; if (page && page.alternates && page.alternates.length) { this.$alternates = $('