#1744: go back after chunk edit,
[redakcja.git] / redakcja / static / js / wiki / view_gallery.js
1 (function($){
2
3     function normalizeNumber(pageNumber, pageCount){
4         // Page number should be >= 1, <= pageCount; 0 if pageCount = 0
5         var pageNumber = parseInt(pageNumber, 10);
6
7         if (!pageCount)
8             return 0;
9
10         if (!pageNumber ||
11                 isNaN(pageNumber) ||
12                 pageNumber == Infinity ||
13                 pageNumber == -Infinity ||
14                 pageNumber < 1)
15             return 1;
16
17         if (pageNumber > pageCount)
18             return pageCount;
19
20         return pageNumber;
21     }
22
23     function bounds(galleryWidth, galleryHeight, imageWidth, imageHeight){
24         return {
25             maxX: 0,
26             maxY: 0,
27             minX: galleryWidth - imageWidth,
28             minY: galleryHeight - imageHeight
29         }
30     };
31
32     function normalizePosition(x, y, galleryWidth, galleryHeight, imageWidth, imageHeight){
33         var b = bounds(galleryWidth, galleryHeight, imageWidth, imageHeight);
34         return {
35             x: Math.min(b.maxX, Math.max(b.minX, x)),
36             y: Math.min(b.maxY, Math.max(b.minY, y))
37         }
38     };
39
40     function fixImageSize(){
41
42     }
43
44     /*
45      * Perspective
46      */
47     function ScanGalleryPerspective(options){
48         var old_callback = options.callback || function() { };
49
50                 this.noupdate_hash_onenter = true;
51         this.vsplitbar = 'GALERIA';
52
53         options.callback = function(){
54             var self = this;
55
56             this.dimensions = {};
57             this.zoomFactor = 1;
58             this.config().page = CurrentDocument.galleryStart;
59             this.$element = $("#side-gallery");
60             this.$numberInput = $('.page-number', this.$element);
61
62             // ...
63             var origin = {};
64             var imageOrigin = {};
65
66             this.$image = $('.gallery-image img', this.$element).attr('unselectable', 'on');
67
68             // button handlers
69             this.$numberInput.change(function(event){
70                 event.preventDefault();
71                 self.setPage($(this).val());
72             });
73                     
74             $('.previous-page', this.$element).click(function(){
75                 self.setPage(parseInt(self.$numberInput.val(),10) - 1);
76             });
77
78             $('.next-page', this.$element).click(function(){
79                 self.setPage(parseInt(self.$numberInput.val(),10) + 1);
80             });
81
82             $('.zoom-in', this.$element).click(function(){
83                 self.alterZoom(0.2);
84             });
85
86             $('.zoom-out', this.$element).click(function(){
87                 self.alterZoom((-0.2));
88             });
89
90             $(window).resize(function(){
91                 self.dimensions.galleryWidth = self.$image.parent().width();
92                 self.dimensions.galleryHeight = self.$image.parent().height();
93             });
94
95             this.$image.load(function(){
96                 console.log("Image loaded.")
97                 self._resizeImage();
98             }).bind('mousedown', function() {
99                                 self.imageMoveStart.apply(self, arguments);
100                         });
101
102
103
104                         old_callback.call(this);
105         };
106
107         $.wiki.Perspective.call(this, options);
108     };
109
110     ScanGalleryPerspective.prototype = new $.wiki.Perspective();
111
112     ScanGalleryPerspective.prototype._resizeImage = function(){
113         var $img = this.$image;
114
115         $img.css({
116             width: '',
117             height: ''
118         });
119
120         this.dimensions = {
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()
127         };
128
129         if (!(this.dimensions.width && this.dimensions.height)) {
130             setTimeout(function(){
131                 $img.load();
132             }, 100);
133         }
134
135         var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
136
137         $img.css({
138             left: position.x,
139             top: position.y,
140             width: $img.width() * this.zoomFactor,
141             height: $img.height() * this.zoomFactor
142         });
143     };
144
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]);
150     };
151
152     ScanGalleryPerspective.prototype.alterZoom = function(delta){
153         var zoomFactor = this.zoomFactor + delta;
154         if (zoomFactor < 0.2)
155             zoomFactor = 0.2;
156         if (zoomFactor > 2)
157             zoomFactor = 2;
158         this.setZoom(zoomFactor);
159     };
160
161     ScanGalleryPerspective.prototype.setZoom = function(factor){
162         this.zoomFactor = factor;
163
164         this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
165         this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
166
167         // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
168
169                 this._resizeImage();
170     };
171
172         /*
173          * Movement
174          */
175         ScanGalleryPerspective.prototype.imageMoved = function(event){
176                 event.preventDefault();
177
178                 // origin is where the drag started
179                 // imageOrigin is where the drag started on the image
180
181                 var newX = event.clientX - this.origin.x + this.imageOrigin.left;
182                 var newY = event.clientY - this.origin.y + this.imageOrigin.top;
183
184                 var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
185
186                 this.$image.css({
187                         left: position.x,
188                         top: position.y,
189                 });
190
191                 return false;
192         };
193
194         ScanGalleryPerspective.prototype.imageMoveStart = function(event){
195                 event.preventDefault();
196
197                 var self = this;
198
199                 this.origin = {
200                         x: event.clientX,
201                         y: event.clientY
202                 };
203
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);
209                 });
210
211                 return false;
212         };
213
214         ScanGalleryPerspective.prototype.imageMoveStop = function(event){
215                 $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
216         };
217
218     /*
219      * Loading gallery
220      */
221     ScanGalleryPerspective.prototype.onEnter = function(success, failure){
222         var self = this;
223
224         $.wiki.Perspective.prototype.onEnter.call(this);
225
226         $('.vsplitbar').not('.active').trigger('click');
227         $(".vsplitbar-title").html("&darr;&nbsp;GALERIA&nbsp;&darr;");        
228         
229         this.doc.refreshGallery({
230             success: function(doc, data){
231                 self.$image.show();
232                                 console.log("gconfig:", self.config().page );
233                                 self.setPage( self.config().page );
234                 $('#imagesCount').html("/" + doc.galleryImages.length);
235
236                 $('.error_message', self.$element).hide();
237                 if(success) success();
238             },
239             failure: function(doc, message){
240                 self.$image.hide();
241                 $('.error_message', self.$element).show().html(message);
242                 if(failure) failure();
243             }
244         });
245
246     };
247
248         ScanGalleryPerspective.prototype.onExit = function(success, failure) {
249
250         };
251
252     $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;
253
254 })(jQuery);