3 function normalizeNumber(pageNumber, pageCount){
4 // Page number should be >= 1, <= pageCount; 0 if pageCount = 0
5 var pageNumber = parseInt(pageNumber, 10);
12 pageNumber == Infinity ||
13 pageNumber == -Infinity ||
17 if (pageNumber > pageCount)
23 function bounds(galleryWidth, galleryHeight, imageWidth, imageHeight){
27 minX: galleryWidth - imageWidth,
28 minY: galleryHeight - imageHeight
32 function normalizePosition(x, y, galleryWidth, galleryHeight, imageWidth, imageHeight){
33 var b = bounds(galleryWidth, galleryHeight, imageWidth, imageHeight);
35 x: Math.min(b.maxX, Math.max(b.minX, x)),
36 y: Math.min(b.maxY, Math.max(b.minY, y))
40 function fixImageSize(){
47 function ScanGalleryPerspective(options){
48 var old_callback = options.callback || function() { };
50 this.noupdate_hash_onenter = true;
51 this.vsplitbar = 'GALERIA';
53 options.callback = function(){
58 if (this.config().page == undefined)
59 this.config().page = CurrentDocument.galleryStart;
60 this.$element = $("#side-gallery");
61 this.$numberInput = $('.page-number', this.$element);
67 this.$image = $('.gallery-image img', this.$element).attr('unselectable', 'on');
70 this.$numberInput.change(function(event){
71 event.preventDefault();
72 self.setPage($(this).val());
75 $('.previous-page', this.$element).click(function(){
76 self.setPage(parseInt(self.$numberInput.val(),10) - 1);
79 $('.next-page', this.$element).click(function(){
80 self.setPage(parseInt(self.$numberInput.val(),10) + 1);
83 $('.zoom-in', this.$element).click(function(){
87 $('.zoom-out', this.$element).click(function(){
88 self.alterZoom((-0.2));
91 $(window).resize(function(){
92 self.dimensions.galleryWidth = self.$image.parent().width();
93 self.dimensions.galleryHeight = self.$image.parent().height();
96 this.$image.load(function(){
97 console.log("Image loaded.")
99 }).bind('mousedown', function() {
100 self.imageMoveStart.apply(self, arguments);
105 old_callback.call(this);
108 $.wiki.Perspective.call(this, options);
111 ScanGalleryPerspective.prototype = new $.wiki.Perspective();
113 ScanGalleryPerspective.prototype._resizeImage = function(){
114 var $img = this.$image;
122 width: $img.width() * this.zoomFactor,
123 height: $img.height() * this.zoomFactor,
124 originWidth: $img.width(),
125 originHeight: $img.height(),
126 galleryWidth: $img.parent().width(),
127 galleryHeight: $img.parent().height()
130 if (!(this.dimensions.width && this.dimensions.height)) {
131 setTimeout(function(){
136 var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
141 width: $img.width() * this.zoomFactor,
142 height: $img.height() * this.zoomFactor
146 ScanGalleryPerspective.prototype.setPage = function(newPage){
147 newPage = normalizeNumber(newPage, this.doc.galleryImages.length);
148 this.$numberInput.val(newPage);
149 this.config().page = newPage;
150 $('.gallery-image img', this.$element).attr('src', this.doc.galleryImages[newPage - 1]);
153 ScanGalleryPerspective.prototype.alterZoom = function(delta){
154 var zoomFactor = this.zoomFactor + delta;
155 if (zoomFactor < 0.2)
159 this.setZoom(zoomFactor);
162 ScanGalleryPerspective.prototype.setZoom = function(factor){
163 this.zoomFactor = factor;
165 this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
166 this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
168 // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
176 ScanGalleryPerspective.prototype.imageMoved = function(event){
177 event.preventDefault();
179 // origin is where the drag started
180 // imageOrigin is where the drag started on the image
182 var newX = event.clientX - this.origin.x + this.imageOrigin.left;
183 var newY = event.clientY - this.origin.y + this.imageOrigin.top;
185 var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
195 ScanGalleryPerspective.prototype.imageMoveStart = function(event){
196 event.preventDefault();
205 this.imageOrigin = self.$image.position();
206 $(document).bind('mousemove.gallery', function(){
207 self.imageMoved.apply(self, arguments);
208 }).bind('mouseup.gallery', function() {
209 self.imageMoveStop.apply(self, arguments);
215 ScanGalleryPerspective.prototype.imageMoveStop = function(event){
216 $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
222 ScanGalleryPerspective.prototype.onEnter = function(success, failure){
225 $.wiki.Perspective.prototype.onEnter.call(this);
227 $('.vsplitbar').not('.active').trigger('click');
228 $(".vsplitbar-title").html("↓ GALERIA ↓");
230 this.doc.refreshGallery({
231 success: function(doc, data){
233 console.log("gconfig:", self.config().page );
234 self.setPage( self.config().page );
235 $('#imagesCount').html("/" + doc.galleryImages.length);
237 $('.error_message', self.$element).hide();
238 if(success) success();
240 failure: function(doc, message){
242 $('.error_message', self.$element).show().html(message);
243 if(failure) failure();
249 ScanGalleryPerspective.prototype.onExit = function(success, failure) {
253 $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;