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