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 $('.start-page', this.$element).click(function(){
76 self.setPage(CurrentDocument.galleryStart);
79 $('.previous-page', this.$element).click(function(){
80 self.setPage(parseInt(self.$numberInput.val(),10) - 1);
83 $('.next-page', this.$element).click(function(){
84 self.setPage(parseInt(self.$numberInput.val(),10) + 1);
87 $('.zoom-in', this.$element).click(function(){
91 $('.zoom-out', this.$element).click(function(){
92 self.alterZoom((-0.2));
95 $(window).resize(function(){
96 self.dimensions.galleryWidth = self.$image.parent().width();
97 self.dimensions.galleryHeight = self.$image.parent().height();
100 this.$image.load(function(){
101 console.log("Image loaded.")
103 }).bind('mousedown', function() {
104 self.imageMoveStart.apply(self, arguments);
109 old_callback.call(this);
112 $.wiki.Perspective.call(this, options);
115 ScanGalleryPerspective.prototype = new $.wiki.Perspective();
117 ScanGalleryPerspective.prototype._resizeImage = function(){
118 var $img = this.$image;
126 width: $img.width() * this.zoomFactor,
127 height: $img.height() * this.zoomFactor,
128 originWidth: $img.width(),
129 originHeight: $img.height(),
130 galleryWidth: $img.parent().width(),
131 galleryHeight: $img.parent().height()
134 if (!(this.dimensions.width && this.dimensions.height)) {
135 setTimeout(function(){
140 var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
145 width: $img.width() * this.zoomFactor,
146 height: $img.height() * this.zoomFactor
150 ScanGalleryPerspective.prototype.setPage = function(newPage){
151 newPage = normalizeNumber(newPage, this.doc.galleryImages.length);
152 this.$numberInput.val(newPage);
153 this.config().page = newPage;
154 $('.gallery-image img', this.$element).attr('src', this.doc.galleryImages[newPage - 1]);
157 ScanGalleryPerspective.prototype.alterZoom = function(delta){
158 var zoomFactor = this.zoomFactor + delta;
159 if (zoomFactor < 0.2)
163 this.setZoom(zoomFactor);
166 ScanGalleryPerspective.prototype.setZoom = function(factor){
167 this.zoomFactor = factor;
169 this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
170 this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
172 // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
180 ScanGalleryPerspective.prototype.imageMoved = function(event){
181 event.preventDefault();
183 // origin is where the drag started
184 // imageOrigin is where the drag started on the image
186 var newX = event.clientX - this.origin.x + this.imageOrigin.left;
187 var newY = event.clientY - this.origin.y + this.imageOrigin.top;
189 var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
199 ScanGalleryPerspective.prototype.imageMoveStart = function(event){
200 event.preventDefault();
209 this.imageOrigin = self.$image.position();
210 $(document).bind('mousemove.gallery', function(){
211 self.imageMoved.apply(self, arguments);
212 }).bind('mouseup.gallery', function() {
213 self.imageMoveStop.apply(self, arguments);
219 ScanGalleryPerspective.prototype.imageMoveStop = function(event){
220 $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
226 ScanGalleryPerspective.prototype.onEnter = function(success, failure){
229 $.wiki.Perspective.prototype.onEnter.call(this);
231 $('.vsplitbar').not('.active').trigger('click');
232 $(".vsplitbar-title").html("↓ GALERIA ↓");
234 this.doc.refreshGallery({
235 success: function(doc, data){
237 console.log("gconfig:", self.config().page );
238 self.setPage( self.config().page );
239 $('#imagesCount').html("/" + doc.galleryImages.length);
241 $('.error_message', self.$element).hide();
242 if(success) success();
244 failure: function(doc, message){
246 $('.error_message', self.$element).show().html(message);
247 if(failure) failure();
253 ScanGalleryPerspective.prototype.onExit = function(success, failure) {
257 $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;