1 /*global View render_template panels */
2 var ImageGalleryView = View.extend({
3 _className: 'ImageGalleryView',
8 template: 'image-gallery-view-template',
10 init: function(element, model, parent, template)
12 console.log("init for gallery");
13 this._super(element, model, template);
16 console.log("gallery model", this.model);
19 .addObserver(this, 'data', this.modelDataChanged.bind(this))
20 .addObserver(this, 'state', this.modelStateChanged.bind(this));
22 //$('.image-gallery-view', this.element).html(this.model.get('data'));
23 this.modelStateChanged('state', this.model.get('state'));
27 modelDataChanged: function(property, value)
29 if( property == 'data')
32 this.gotoPage(this.currentPage);
36 gotoPage: function(index)
41 var n = this.$pages.length;
42 if (index >= n) index = n-1;
44 if( (this.currentPage == index) )
47 var cpage = this.$currentPage();
50 var offset = this.pageViewOffset(cpage);
51 this.cleanPage(cpage);
54 this.currentPage = index;
56 cpage = this.$currentPage()
57 this.renderImage(cpage);
60 cpage.css({top: offset.y, left: offset.x});
64 $('img', cpage).bind('load', function() {
66 self.setPageViewOffset(cpage, offset);
71 if(this.currentPage == n-1)
72 this.$nextButton.attr('disabled', 'disabled');
74 this.$nextButton.removeAttr('disabled');
76 if(this.currentPage == 0)
77 this.$prevButton.attr('disabled', 'disabled');
79 this.$prevButton.removeAttr('disabled');
81 this.$pageInput.val( (this.currentPage+1) );
84 reload: function() {},
86 modelStateChanged: function(property, value) {
87 if (value == 'loading') {
88 // this.freeze('Ładowanie...');
94 $currentPage: function() {
95 if(this.currentPage >= 0 && this.currentPage < this.$pages.length)
96 return $(this.$pages[this.currentPage]);
101 cleanPage: function($page) {
103 $('img', $page).unbind();
106 this.setPageViewOffset($page, {x:0, y:0});
109 pageDragStart: function(event)
111 this.dragStart = {x: event.clientX, y: event.clientY};
112 $(window).bind('mousemove.imagedrag', this.pageDrag.bind(this));
113 $(window).bind('mouseup.imagedrag', this.pageDragStop.bind(this));
115 this.$currentPage().css('cursor', 'move');
120 pageDrag: function(event)
122 if(!this.dragStart) return;
125 x: this.dragStart.x - event.clientX,
126 y: this.dragStart.y - event.clientY };
128 var offset = this.pageViewOffset( $(this.$pages[this.currentPage]) );
131 this.setPageViewOffset( $(this.$pages[this.currentPage]), offset);
133 this.dragStart = {x: event.clientX, y: event.clientY };
137 pageDragStop: function(event) {
138 this.$currentPage().css('cursor', 'auto');
140 this.dragStart = undefined;
141 $(window).unbind('mousemove.imagedrag');
142 $(window).unbind('mouseup.imagedrag');
147 pageViewOffset: function($page) {
148 var left = parseInt($page.css('left'));
149 var top = parseInt($page.css('top'));
151 return {x: left, y: top};
154 setPageViewOffset: function($page, offset) {
155 // check if the image will be actually visible
160 var vp_width = this.$pageListRoot.width();
161 var vp_height = this.$pageListRoot.height();
163 var width = $page.outerWidth();
164 var height = $page.outerHeight();
166 // console.log(offset, vp_width, vp_height, width, height);
167 if( offset.x+width-MARGIN < 0 ) {
168 // console.log('too much on the left', offset.x, -width)
169 offset.x = -width+MARGIN;
172 // too much on the right
173 if( offset.x > vp_width-MARGIN) {
174 offset.x = vp_width-MARGIN;
175 // console.log('too much on the right', offset.x, vp_width, width)
178 if( offset.y+height-MARGIN < 0)
179 offset.y = -height+MARGIN;
181 if( offset.y > vp_height-MARGIN)
182 offset.y = vp_height-MARGIN;
184 $page.css({left: offset.x, top: offset.y});
188 this.model.load(true);
191 renderImage: function(target)
193 var source = target.attr('ui:model');
194 var orig_width = parseInt(target.attr('ui:width'));
195 var orig_height = parseInt(target.attr('ui:height'));
197 target.html('<img src="' + source
198 + '" width="' + Math.floor(orig_width * this.pageZoom)
199 + '" height="' + Math.floor(orig_height * this.pageZoom)
204 'user-select': 'none',
205 '-webkit-user-select': 'none',
206 '-khtml-user-select': 'none',
207 '-moz-user-select': 'none'
209 attr('unselectable', 'on').
210 mousedown(this.pageDragStart.bind(this));
215 if(!this.model) return;
217 /* first unbind all */
218 if(this.$nextButton) this.$nextButton.unbind();
219 if(this.$prevButton) this.$prevButton.unbind();
220 if(this.$jumpButton) this.$jumpButton.unbind();
221 if(this.$pageInput) this.$pageInput.unbind();
223 if(this.$zoomInButton) this.$zoomInButton.unbind();
224 if(this.$zoomOutButton) this.$zoomOutButton.unbind();
225 if(this.$zoomResetButton) this.$zoomResetButton.unbind();
230 /* fetch important parts */
231 this.$pageListRoot = $('.image-gallery-page-list', this.element);
232 this.$pages = $('.image-gallery-page-container', this.$pageListRoot);
234 this.$nextButton = $('.image-gallery-next-button', this.element);
235 this.$prevButton = $('.image-gallery-prev-button', this.element);
236 this.$pageInput = $('.image-gallery-current-page', this.element);
238 // this.$zoomSelect = $('.image-gallery-current-zoom', this.element);
239 this.$zoomInButton = $('.image-gallery-zoom-in', this.element);
240 this.$zoomOutButton = $('.image-gallery-zoom-out', this.element);
241 this.$zoomResetButton = $('.image-gallery-zoom-reset', this.element);
244 this.$nextButton.click( this.nextPage.bind(this) );
245 this.$prevButton.click( this.prevPage.bind(this) );
246 this.$pageInput.change( this.jumpToPage.bind(this) );
248 // this.$zoomSelect.change( this.zoomChanged.bind(this) );
249 this.$zoomInButton.click( this.zoomInOneStep.bind(this) );
250 this.$zoomOutButton.click( this.zoomOutOneStep.bind(this) );
251 this.$zoomResetButton.click( this.zoomReset.bind(this) );
253 this.gotoPage(this.currentPage);
254 this.changePageZoom(this.pageZoom);
257 jumpToPage: function() {
258 this.gotoPage(parseInt(this.$pageInput.val())-1);
261 nextPage: function() {
262 this.gotoPage(this.currentPage + 1);
265 prevPage: function() {
266 this.gotoPage(this.currentPage - 1);
269 zoomReset: function() {
270 this.changePageZoom(1.0);
273 zoomInOneStep: function() {
274 var zoom = this.pageZoom + 0.1;
275 if(zoom > 3.0) zoom = 3.0;
276 this.changePageZoom(zoom);
279 zoomOutOneStep: function() {
280 var zoom = this.pageZoom - 0.1;
281 if(zoom < 0.3) zoom = 0.3;
282 this.changePageZoom(zoom);
285 changePageZoom: function(value) {
286 var current = this.$currentPage();
290 var alpha = value/this.pageZoom;
291 this.pageZoom = value;
293 var nwidth = current.attr('ui:width') * this.pageZoom;
294 var nheight = current.attr('ui:height') * this.pageZoom;
295 var off_top = parseInt(current.css('top'));
296 var off_left = parseInt(current.css('left'));
298 var vpx = this.$pageListRoot.width() * 0.5;
299 var vpy = this.$pageListRoot.height() * 0.5;
301 var new_off_left = vpx - alpha*(vpx-off_left);
302 var new_off_top = vpy - alpha*(vpy-off_top);
304 $('img', current).attr('width', nwidth);
305 $('img', current).attr('height', nheight);
307 this.setPageViewOffset(current, {
308 y: new_off_top, x: new_off_left
311 // this.$zoomSelect.val(this.pageZoom);
312 // console.log('Zoom is now', this.pageZoom);
317 console.log("Disposing gallery.");
318 this.model.removeObserver(this);
324 panels['gallery'] = ImageGalleryView;