315a421b65181464fd4607cab3b90a85a8397a57
[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             if (this.config().page == undefined)
59                 this.config().page = CurrentDocument.galleryStart;
60             this.$element = $("#side-gallery");
61             this.$numberInput = $('.page-number', this.$element);
62
63             // ...
64             var origin = {};
65             var imageOrigin = {};
66
67             this.$image = $('.gallery-image img', this.$element).attr('unselectable', 'on');
68
69             // button handlers
70             this.$numberInput.change(function(event){
71                 event.preventDefault();
72                 self.setPage($(this).val());
73             });
74                     
75             $('.previous-page', this.$element).click(function(){
76                 self.setPage(parseInt(self.$numberInput.val(),10) - 1);
77             });
78
79             $('.next-page', this.$element).click(function(){
80                 self.setPage(parseInt(self.$numberInput.val(),10) + 1);
81             });
82
83             $('.zoom-in', this.$element).click(function(){
84                 self.alterZoom(0.2);
85             });
86
87             $('.zoom-out', this.$element).click(function(){
88                 self.alterZoom((-0.2));
89             });
90
91             $(window).resize(function(){
92                 self.dimensions.galleryWidth = self.$image.parent().width();
93                 self.dimensions.galleryHeight = self.$image.parent().height();
94             });
95
96             this.$image.load(function(){
97                 console.log("Image loaded.")
98                 self._resizeImage();
99             }).bind('mousedown', function() {
100                                 self.imageMoveStart.apply(self, arguments);
101                         });
102
103
104
105                         old_callback.call(this);
106         };
107
108         $.wiki.Perspective.call(this, options);
109     };
110
111     ScanGalleryPerspective.prototype = new $.wiki.Perspective();
112
113     ScanGalleryPerspective.prototype._resizeImage = function(){
114         var $img = this.$image;
115
116         $img.css({
117             width: '',
118             height: ''
119         });
120
121         this.dimensions = {
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()
128         };
129
130         if (!(this.dimensions.width && this.dimensions.height)) {
131             setTimeout(function(){
132                 $img.load();
133             }, 100);
134         }
135
136         var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
137
138         $img.css({
139             left: position.x,
140             top: position.y,
141             width: $img.width() * this.zoomFactor,
142             height: $img.height() * this.zoomFactor
143         });
144     };
145
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]);
151     };
152
153     ScanGalleryPerspective.prototype.alterZoom = function(delta){
154         var zoomFactor = this.zoomFactor + delta;
155         if (zoomFactor < 0.2)
156             zoomFactor = 0.2;
157         if (zoomFactor > 2)
158             zoomFactor = 2;
159         this.setZoom(zoomFactor);
160     };
161
162     ScanGalleryPerspective.prototype.setZoom = function(factor){
163         this.zoomFactor = factor;
164
165         this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
166         this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
167
168         // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
169
170                 this._resizeImage();
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         $('.vsplitbar').not('.active').trigger('click');
228         $(".vsplitbar-title").html("&darr;&nbsp;GALERIA&nbsp;&darr;");        
229         
230         this.doc.refreshGallery({
231             success: function(doc, data){
232                 self.$image.show();
233                                 console.log("gconfig:", self.config().page );
234                                 self.setPage( self.config().page );
235                 $('#imagesCount').html("/" + doc.galleryImages.length);
236
237                 $('.error_message', self.$element).hide();
238                 if(success) success();
239             },
240             failure: function(doc, message){
241                 self.$image.hide();
242                 $('.error_message', self.$element).show().html(message);
243                 if(failure) failure();
244             }
245         });
246
247     };
248
249         ScanGalleryPerspective.prototype.onExit = function(success, failure) {
250
251         };
252
253     $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;
254
255 })(jQuery);