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 this.config().page = CurrentDocument.galleryStart;
59 this.$element = $("#side-gallery");
60 this.$numberInput = $('.page-number', this.$element);
66 this.$image = $('.gallery-image img', this.$element).attr('unselectable', 'on');
69 this.$numberInput.change(function(event){
70 event.preventDefault();
71 self.setPage($(this).val());
74 $('.previous-page', this.$element).click(function(){
75 self.setPage(parseInt(self.$numberInput.val(),10) - 1);
78 $('.next-page', this.$element).click(function(){
79 self.setPage(parseInt(self.$numberInput.val(),10) + 1);
82 $('.zoom-in', this.$element).click(function(){
86 $('.zoom-out', this.$element).click(function(){
87 self.alterZoom((-0.2));
90 $(window).resize(function(){
91 self.dimensions.galleryWidth = self.$image.parent().width();
92 self.dimensions.galleryHeight = self.$image.parent().height();
95 this.$image.load(function(){
96 console.log("Image loaded.")
98 }).bind('mousedown', function() {
99 self.imageMoveStart.apply(self, arguments);
104 old_callback.call(this);
107 $.wiki.Perspective.call(this, options);
110 ScanGalleryPerspective.prototype = new $.wiki.Perspective();
112 ScanGalleryPerspective.prototype._resizeImage = function(){
113 var $img = this.$image;
121 width: $img.width() * this.zoomFactor,
122 height: $img.height() * this.zoomFactor,
123 originWidth: $img.width(),
124 originHeight: $img.height(),
125 galleryWidth: $img.parent().width(),
126 galleryHeight: $img.parent().height()
129 if (!(this.dimensions.width && this.dimensions.height)) {
130 setTimeout(function(){
135 var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
140 width: $img.width() * this.zoomFactor,
141 height: $img.height() * this.zoomFactor
145 ScanGalleryPerspective.prototype.setPage = function(newPage){
146 newPage = normalizeNumber(newPage, this.doc.galleryImages.length);
147 this.$numberInput.val(newPage);
148 this.config().page = newPage;
149 $('.gallery-image img', this.$element).attr('src', this.doc.galleryImages[newPage - 1]);
152 ScanGalleryPerspective.prototype.alterZoom = function(delta){
153 var zoomFactor = this.zoomFactor + delta;
154 if (zoomFactor < 0.2)
158 this.setZoom(zoomFactor);
161 ScanGalleryPerspective.prototype.setZoom = function(factor){
162 this.zoomFactor = factor;
164 this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
165 this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
167 // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
175 ScanGalleryPerspective.prototype.imageMoved = function(event){
176 event.preventDefault();
178 // origin is where the drag started
179 // imageOrigin is where the drag started on the image
181 var newX = event.clientX - this.origin.x + this.imageOrigin.left;
182 var newY = event.clientY - this.origin.y + this.imageOrigin.top;
184 var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
194 ScanGalleryPerspective.prototype.imageMoveStart = function(event){
195 event.preventDefault();
204 this.imageOrigin = self.$image.position();
205 $(document).bind('mousemove.gallery', function(){
206 self.imageMoved.apply(self, arguments);
207 }).bind('mouseup.gallery', function() {
208 self.imageMoveStop.apply(self, arguments);
214 ScanGalleryPerspective.prototype.imageMoveStop = function(event){
215 $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
221 ScanGalleryPerspective.prototype.onEnter = function(success, failure){
224 $.wiki.Perspective.prototype.onEnter.call(this);
226 $('.vsplitbar').not('.active').trigger('click');
227 $(".vsplitbar-title").html("↓ GALERIA ↓");
229 this.doc.refreshGallery({
230 success: function(doc, data){
232 console.log("gconfig:", self.config().page );
233 self.setPage( self.config().page );
234 $('#imagesCount').html("/" + doc.galleryImages.length);
236 $('.error_message', self.$element).hide();
237 if(success) success();
239 failure: function(doc, message){
241 $('.error_message', self.$element).show().html(message);
242 if(failure) failure();
248 ScanGalleryPerspective.prototype.onExit = function(success, failure) {
252 $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;