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 console.log(this.model.get('state'), value, value.length);
30 if ((this.model.get('state') == 'synced') && (value.length == 0)) {
32 this.render('image-gallery-empty-template');
35 this.gotoPage(this.currentPage);
39 gotoPage: function(index)
44 var n = this.$pages.length;
45 if (index >= n) index = n-1;
47 if( (this.currentPage == index) )
50 var cpage = this.$currentPage();
53 var offset = this.pageViewOffset(cpage);
54 this.cleanPage(cpage);
57 this.currentPage = index;
59 cpage = this.$currentPage()
60 this.renderImage(cpage);
63 cpage.css({top: offset.y, left: offset.x});
67 $('img', cpage).bind('load', function() {
69 self.setPageViewOffset(cpage, offset);
74 if(this.currentPage == n-1)
75 this.$nextButton.attr('disabled', 'disabled');
77 this.$nextButton.removeAttr('disabled');
79 if(this.currentPage == 0)
80 this.$prevButton.attr('disabled', 'disabled');
82 this.$prevButton.removeAttr('disabled');
84 this.$pageInput.val( (this.currentPage+1) );
87 reload: function() {},
89 modelStateChanged: function(property, value) {
90 if (value == 'loading') {
91 this.freeze('Ćadowanie...');
93 if ((value == 'synced') && (this.model.get('data').length == 0)) {
94 this.render('image-gallery-empty-template');
100 $currentPage: function() {
101 if(this.currentPage >= 0 && this.currentPage < this.$pages.length)
102 return $(this.$pages[this.currentPage]);
107 cleanPage: function($page) {
109 $('img', $page).unbind();
112 this.setPageViewOffset($page, {x:0, y:0});
115 pageDragStart: function(event)
117 this.dragStart = {x: event.clientX, y: event.clientY};
118 $(window).bind('mousemove.imagedrag', this.pageDrag.bind(this));
119 $(window).bind('mouseup.imagedrag', this.pageDragStop.bind(this));
121 this.$currentPage().css('cursor', 'move');
126 pageDrag: function(event)
128 if(!this.dragStart) return;
131 x: this.dragStart.x - event.clientX,
132 y: this.dragStart.y - event.clientY };
134 var offset = this.pageViewOffset( $(this.$pages[this.currentPage]) );
137 this.setPageViewOffset( $(this.$pages[this.currentPage]), offset);
139 this.dragStart = {x: event.clientX, y: event.clientY };
143 pageDragStop: function(event) {
144 this.$currentPage().css('cursor', 'auto');
146 this.dragStart = undefined;
147 $(window).unbind('mousemove.imagedrag');
148 $(window).unbind('mouseup.imagedrag');
153 pageViewOffset: function($page) {
154 var left = parseInt($page.css('left'));
155 var top = parseInt($page.css('top'));
157 return {x: left, y: top};
160 setPageViewOffset: function($page, offset) {
161 // check if the image will be actually visible
166 var vp_width = this.$pageListRoot.width();
167 var vp_height = this.$pageListRoot.height();
169 var width = $page.outerWidth();
170 var height = $page.outerHeight();
172 // console.log(offset, vp_width, vp_height, width, height);
173 if( offset.x+width-MARGIN < 0 ) {
174 // console.log('too much on the left', offset.x, -width)
175 offset.x = -width+MARGIN;
178 // too much on the right
179 if( offset.x > vp_width-MARGIN) {
180 offset.x = vp_width-MARGIN;
181 // console.log('too much on the right', offset.x, vp_width, width)
184 if( offset.y+height-MARGIN < 0)
185 offset.y = -height+MARGIN;
187 if( offset.y > vp_height-MARGIN)
188 offset.y = vp_height-MARGIN;
190 $page.css({left: offset.x, top: offset.y});
194 this.model.load(true);
197 renderImage: function(target)
199 var source = target.attr('ui:model');
200 var orig_width = parseInt(target.attr('ui:width'));
201 var orig_height = parseInt(target.attr('ui:height'));
203 target.html('<img src="' + source
204 + '" width="' + Math.floor(orig_width * this.pageZoom)
205 + '" height="' + Math.floor(orig_height * this.pageZoom)
210 'user-select': 'none',
211 '-webkit-user-select': 'none',
212 '-khtml-user-select': 'none',
213 '-moz-user-select': 'none'
215 attr('unselectable', 'on').
216 mousedown(this.pageDragStart.bind(this));
219 render: function(template)
221 if(!this.model) return;
223 /* first unbind all */
224 if(this.$nextButton) this.$nextButton.unbind();
225 if(this.$prevButton) this.$prevButton.unbind();
226 if(this.$jumpButton) this.$jumpButton.unbind();
227 if(this.$pageInput) this.$pageInput.unbind();
229 if(this.$zoomInButton) this.$zoomInButton.unbind();
230 if(this.$zoomOutButton) this.$zoomOutButton.unbind();
231 if(this.$zoomResetButton) this.$zoomResetButton.unbind();
237 /* fetch important parts */
238 this.$pageListRoot = $('.image-gallery-page-list', this.element);
239 this.$pages = $('.image-gallery-page-container', this.$pageListRoot);
241 this.$nextButton = $('.image-gallery-next-button', this.element);
242 this.$prevButton = $('.image-gallery-prev-button', this.element);
243 this.$pageInput = $('.image-gallery-current-page', this.element);
245 // this.$zoomSelect = $('.image-gallery-current-zoom', this.element);
246 this.$zoomInButton = $('.image-gallery-zoom-in', this.element);
247 this.$zoomOutButton = $('.image-gallery-zoom-out', this.element);
248 this.$zoomResetButton = $('.image-gallery-zoom-reset', this.element);
251 this.$nextButton.click( this.nextPage.bind(this) );
252 this.$prevButton.click( this.prevPage.bind(this) );
253 this.$pageInput.change( this.jumpToPage.bind(this) );
255 // this.$zoomSelect.change( this.zoomChanged.bind(this) );
256 this.$zoomInButton.click( this.zoomInOneStep.bind(this) );
257 this.$zoomOutButton.click( this.zoomOutOneStep.bind(this) );
258 this.$zoomResetButton.click( this.zoomReset.bind(this) );
260 this.gotoPage(this.currentPage);
261 this.changePageZoom(this.pageZoom);
263 this._super(template);
267 jumpToPage: function() {
268 this.gotoPage(parseInt(this.$pageInput.val())-1);
271 nextPage: function() {
272 this.gotoPage(this.currentPage + 1);
275 prevPage: function() {
276 this.gotoPage(this.currentPage - 1);
279 zoomReset: function() {
280 this.changePageZoom(1.0);
283 zoomInOneStep: function() {
284 var zoom = this.pageZoom + 0.1;
285 if(zoom > 3.0) zoom = 3.0;
286 this.changePageZoom(zoom);
289 zoomOutOneStep: function() {
290 var zoom = this.pageZoom - 0.1;
291 if(zoom < 0.3) zoom = 0.3;
292 this.changePageZoom(zoom);
295 changePageZoom: function(value) {
296 var current = this.$currentPage();
300 var alpha = value/this.pageZoom;
301 this.pageZoom = value;
303 var nwidth = current.attr('ui:width') * this.pageZoom;
304 var nheight = current.attr('ui:height') * this.pageZoom;
305 var off_top = parseInt(current.css('top'));
306 var off_left = parseInt(current.css('left'));
308 var vpx = this.$pageListRoot.width() * 0.5;
309 var vpy = this.$pageListRoot.height() * 0.5;
311 var new_off_left = vpx - alpha*(vpx-off_left);
312 var new_off_top = vpy - alpha*(vpy-off_top);
314 $('img', current).attr('width', nwidth);
315 $('img', current).attr('height', nheight);
317 this.setPageViewOffset(current, {
318 y: new_off_top, x: new_off_left
321 // this.$zoomSelect.val(this.pageZoom);
322 // console.log('Zoom is now', this.pageZoom);
327 console.log("Disposing gallery.");
328 this.model.removeObserver(this);
334 panels['gallery'] = ImageGalleryView;