3 function normalizeNumber(pageNumber, pageCount){
4 // Numer strony musi być pomiędzy 1 a najwyższym numerem
5 var pageNumber = parseInt(pageNumber, 10);
9 pageNumber == Infinity ||
10 pageNumber == -Infinity ||
14 if (pageNumber > pageCount)
20 function bounds(galleryWidth, galleryHeight, imageWidth, imageHeight){
24 minX: galleryWidth - imageWidth,
25 minY: galleryHeight - imageHeight
29 function normalizePosition(x, y, galleryWidth, galleryHeight, imageWidth, imageHeight){
30 var b = bounds(galleryWidth, galleryHeight, imageWidth, imageHeight);
32 x: Math.min(b.maxX, Math.max(b.minX, x)),
33 y: Math.min(b.maxY, Math.max(b.minY, y))
37 function fixImageSize(){
44 function ScanGalleryPerspective(options){
45 var old_callback = options.callback || function() { };
47 options.callback = function(){
52 this.$element = $("#side-gallery");
53 this.$numberInput = $('.page-number', this.$element);
59 this.$image = $('.gallery-image img', this.$element).attr('unselectable', 'on');
62 this.$numberInput.change(function(event){
63 event.preventDefault();
64 self.setPage($(this).val());
67 $('.previous-page', this.$element).click(function(){
68 self.setPage(parseInt(self.$numberInput.val(),10) - 1);
71 $('.next-page', this.$element).click(function(){
72 self.setPage(parseInt(self.$numberInput.val(),10) + 1);
75 $('.zoom-in', this.$element).click(function(){
79 $('.zoom-out', this.$element).click(function(){
80 self.alterZoom((-0.2));
83 $(window).resize(function(){
84 self.dimensions.galleryWidth = self.$image.parent().width();
85 self.dimensions.galleryHeight = self.$image.parent().height();
88 $('.gallery-image img', this.$element).load(function(){
89 console.log("Image loaded.")
91 }).bind('mousedown', function() {
92 self.imageMoveStart.apply(self, arguments);
95 old_callback.call(this);
98 $.wiki.Perspective.call(this, options);
101 ScanGalleryPerspective.prototype = new $.wiki.Perspective();
103 ScanGalleryPerspective.prototype._resizeImage = function(){
104 var $img = this.$image;
112 width: $img.width() * this.zoomFactor,
113 height: $img.height() * this.zoomFactor,
114 originWidth: $img.width(),
115 originHeight: $img.height(),
116 galleryWidth: $img.parent().width(),
117 galleryHeight: $img.parent().height()
120 if (!(this.dimensions.width && this.dimensions.height)) {
121 setTimeout(function(){
126 var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
131 width: $img.width() * this.zoomFactor,
132 height: $img.height() * this.zoomFactor
136 ScanGalleryPerspective.prototype.setPage = function(newPage){
137 newPage = normalizeNumber(newPage, this.doc.galleryImages.length);
138 this.$numberInput.val(newPage);
139 $('.gallery-image img', this.$element).attr('src', this.doc.galleryImages[newPage - 1]);
142 ScanGalleryPerspective.prototype.alterZoom = function(delta){
143 var zoomFactor = this.zoomFactor + delta;
144 if (zoomFactor < 0.2)
148 this.setZoom(zoomFactor);
151 ScanGalleryPerspective.prototype.setZoom = function(factor){
152 this.zoomFactor = factor;
154 this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
155 this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
157 // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
161 width: this.dimensions.width,
162 height: this.dimensions.height,
171 ScanGalleryPerspective.prototype.imageMoved = function(event){
172 event.preventDefault();
174 // origin is where the drag started
175 // imageOrigin is where the drag started on the image
177 var newX = event.clientX - this.origin.x + this.imageOrigin.left;
178 var newY = event.clientY - this.origin.y + this.imageOrigin.top;
180 var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
190 ScanGalleryPerspective.prototype.imageMoveStart = function(event){
191 event.preventDefault();
200 this.imageOrigin = self.$image.position();
201 $(document).bind('mousemove.gallery', function(){
202 self.imageMoved.apply(self, arguments);
203 }).bind('mouseup.gallery', function() {
204 self.imageMoveStop.apply(self, arguments);
210 ScanGalleryPerspective.prototype.imageMoveStop = function(event){
211 $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
217 ScanGalleryPerspective.prototype.onEnter = function(success, failure){
220 $.wiki.Perspective.prototype.onEnter.call(this);
222 this.doc.refreshGallery({
223 success: function(doc, data){
225 self.setPage( self.$numberInput.val() );
227 $('.error_message', self.$element).hide();
228 if(success) success();
230 failure: function(doc, message){
232 $('.error_message', self.$element).show().html(message);
233 if(failure) failure();
238 $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;