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);
17 .addObserver(this, 'data', this.modelDataChanged.bind(this))
18 .addObserver(this, 'state', this.modelStateChanged.bind(this));
20 //$('.image-gallery-view', this.element).html(this.model.get('data'));
21 this.modelStateChanged('state', this.model.get('state'));
25 modelDataChanged: function(property, value)
27 if( property == 'data')
30 this.gotoPage(this.currentPage);
34 gotoPage: function(index)
39 var n = this.$pages.length;
40 if (index >= n) index = n-1;
42 if( (this.currentPage == index) )
45 var cpage = this.$currentPage();
48 var offset = this.pageViewOffset(cpage);
49 this.cleanPage(cpage);
52 this.currentPage = index;
54 cpage = this.$currentPage()
55 this.renderImage(cpage);
58 cpage.css({top: offset.y, left: offset.x});
62 $('img', cpage).bind('load', function() {
64 self.setPageViewOffset(cpage, offset);
69 if(this.currentPage == n-1)
70 this.$nextButton.attr('disabled', 'disabled');
72 this.$nextButton.removeAttr('disabled');
74 if(this.currentPage == 0)
75 this.$prevButton.attr('disabled', 'disabled');
77 this.$prevButton.removeAttr('disabled');
79 this.$pageInput.val( (this.currentPage+1) );
82 modelStateChanged: function(property, value) {
83 if (value == 'loading') {
84 this.parent.freeze('Ćadowanie...');
86 this.parent.unfreeze();
90 $currentPage: function() {
91 if(this.currentPage >= 0 && this.currentPage < this.$pages.length)
92 return $(this.$pages[this.currentPage]);
97 cleanPage: function($page) {
99 $('img', $page).unbind();
103 this.setPageViewOffset($page, {x:0, y:0});
106 pageDragStart: function(event)
108 this.dragStart = {x: event.clientX, y: event.clientY};
109 $(window).bind('mousemove.imagedrag', this.pageDrag.bind(this));
110 $(window).bind('mouseup.imagedrag', this.pageDragStop.bind(this));
112 this.$currentPage().css('cursor', 'move');
117 pageDrag: function(event)
119 if(!this.dragStart) return;
122 x: this.dragStart.x - event.clientX,
123 y: this.dragStart.y - event.clientY };
125 var offset = this.pageViewOffset( $(this.$pages[this.currentPage]) );
128 this.setPageViewOffset( $(this.$pages[this.currentPage]), offset);
130 this.dragStart = {x: event.clientX, y: event.clientY };
134 pageDragStop: function(event) {
135 this.$currentPage().css('cursor', 'auto');
137 this.dragStart = undefined;
138 $(window).unbind('mousemove.imagedrag');
139 $(window).unbind('mouseup.imagedrag');
144 pageViewOffset: function($page) {
145 var left = parseInt($page.css('left'));
146 var top = parseInt($page.css('top'));
148 return {x: left, y: top};
151 setPageViewOffset: function($page, offset) {
152 // check if the image will be actually visible
157 var vp_width = this.$pageListRoot.width();
158 var vp_height = this.$pageListRoot.height();
160 var width = $page.outerWidth();
161 var height = $page.outerHeight();
163 // console.log(offset, vp_width, vp_height, width, height);
164 if( offset.x+width-MARGIN < 0 ) {
165 // console.log('too much on the left', offset.x, -width)
166 offset.x = -width+MARGIN;
169 // too much on the right
170 if( offset.x > vp_width-MARGIN) {
171 offset.x = vp_width-MARGIN;
172 // console.log('too much on the right', offset.x, vp_width, width)
175 if( offset.y+height-MARGIN < 0)
176 offset.y = -height+MARGIN;
178 if( offset.y > vp_height-MARGIN)
179 offset.y = vp_height-MARGIN;
181 $page.css({left: offset.x, top: offset.y});
184 renderImage: function(target)
186 var source = target.attr('ui:model');
187 var orig_width = parseInt(target.attr('ui:width'));
188 var orig_height = parseInt(target.attr('ui:height'));
190 target.html('<img src="' + source
191 + '" width="' + Math.floor(orig_width * this.pageZoom)
192 + '" height="' + Math.floor(orig_height * this.pageZoom)
197 'user-select': 'none',
198 '-webkit-user-select': 'none',
199 '-khtml-user-select': 'none',
200 '-moz-user-select': 'none'
202 attr('unselectable', 'on').
203 mousedown(this.pageDragStart.bind(this));
208 console.log('rendering:', this._className);
210 /* first unbind all */
211 if(this.$nextButton) this.$nextButton.unbind();
212 if(this.$prevButton) this.$prevButton.unbind();
213 if(this.$jumpButton) this.$jumpButton.unbind();
214 if(this.$pageInput) this.$pageInput.unbind();
216 if(this.$zoomInButton) this.$zoomInButton.unbind();
217 if(this.$zoomOutButton) this.$zoomOutButton.unbind();
218 if(this.$zoomResetButton) this.$zoomResetButton.unbind();
221 this.element.html(render_template(this.template, this));
223 /* fetch important parts */
224 this.$pageListRoot = $('.image-gallery-page-list', this.element);
225 this.$pages = $('.image-gallery-page-container', this.$pageListRoot);
227 this.$nextButton = $('.image-gallery-next-button', this.element);
228 this.$prevButton = $('.image-gallery-prev-button', this.element);
229 this.$pageInput = $('.image-gallery-current-page', this.element);
231 // this.$zoomSelect = $('.image-gallery-current-zoom', this.element);
232 this.$zoomInButton = $('.image-gallery-zoom-in', this.element);
233 this.$zoomOutButton = $('.image-gallery-zoom-out', this.element);
234 this.$zoomResetButton = $('.image-gallery-zoom-reset', this.element);
237 this.$nextButton.click( this.nextPage.bind(this) );
238 this.$prevButton.click( this.prevPage.bind(this) );
239 this.$pageInput.change( this.jumpToPage.bind(this) );
241 // this.$zoomSelect.change( this.zoomChanged.bind(this) );
242 this.$zoomInButton.click( this.zoomInOneStep.bind(this) );
243 this.$zoomOutButton.click( this.zoomOutOneStep.bind(this) );
244 this.$zoomResetButton.click( this.zoomReset.bind(this) );
246 this.gotoPage(this.currentPage);
247 this.changePageZoom(this.pageZoom);
250 jumpToPage: function() {
251 this.gotoPage(parseInt(this.$pageInput.val())-1);
254 nextPage: function() {
255 this.gotoPage(this.currentPage + 1);
258 prevPage: function() {
259 this.gotoPage(this.currentPage - 1);
262 zoomReset: function() {
263 this.changePageZoom(1.0);
266 zoomInOneStep: function() {
267 var zoom = this.pageZoom + 0.1;
268 if(zoom > 3.0) zoom = 3.0;
269 this.changePageZoom(zoom);
272 zoomOutOneStep: function() {
273 var zoom = this.pageZoom - 0.1;
274 if(zoom < 0.3) zoom = 0.3;
275 this.changePageZoom(zoom);
278 changePageZoom: function(value) {
279 var current = this.$currentPage();
283 var alpha = value/this.pageZoom;
284 this.pageZoom = value;
286 var nwidth = current.attr('ui:width') * this.pageZoom;
287 var nheight = current.attr('ui:height') * this.pageZoom;
288 var off_top = parseInt(current.css('top'));
289 var off_left = parseInt(current.css('left'));
291 var vpx = this.$pageListRoot.width() * 0.5;
292 var vpy = this.$pageListRoot.height() * 0.5;
294 var new_off_left = vpx - alpha*(vpx-off_left);
295 var new_off_top = vpy - alpha*(vpy-off_top);
297 $('img', current).attr('width', nwidth);
298 $('img', current).attr('height', nheight);
300 this.setPageViewOffset(current, {
301 y: new_off_top, x: new_off_left
304 // this.$zoomSelect.val(this.pageZoom);
305 // console.log('Zoom is now', this.pageZoom);
310 console.log("Disposing gallery.");
311 this.model.removeObserver(this);
317 panels['gallery'] = ImageGalleryView;