1 /*global View render_template panels */
2 var ImageGalleryView = View.extend({
3 _className: 'ImageGalleryView',
6 template: 'image-gallery-view-template',
8 init: function(element, model, parent, template)
10 this.currentPage = -1;
12 this._super(element, model, template);
16 .addObserver(this, 'data', this.modelDataChanged.bind(this))
17 .addObserver(this, 'state', this.modelStateChanged.bind(this));
19 //$('.image-gallery-view', this.element).html(this.model.get('data'));
20 this.modelStateChanged('state', this.model.get('state'));
24 modelDataChanged: function(property, value)
26 if( property == 'data')
29 this.gotoPage(this.currentPage);
33 gotoPage: function(index)
38 var n = this.$pages.length;
39 if (index >= n) index = n-1;
41 if( (this.currentPage == index) )
44 var cpage = this.$currentPage();
47 var offset = this.pageViewOffset(cpage);
48 this.cleanPage(cpage);
51 this.currentPage = index;
53 cpage = this.$currentPage()
54 this.renderImage(cpage, cpage.attr('ui:model'));
57 cpage.css({top: offset.y, left: offset.x});
61 $('img', cpage).bind('load', function() {
63 self.setPageViewOffset(cpage, offset);
68 if(this.currentPage == n-1)
69 this.$nextButton.attr('disabled', 'disabled');
71 this.$nextButton.removeAttr('disabled');
73 if(this.currentPage == 0)
74 this.$prevButton.attr('disabled', 'disabled');
76 this.$prevButton.removeAttr('disabled');
78 this.$pageInput.val( (this.currentPage+1) );
81 modelStateChanged: function(property, value) {
82 if (value == 'synced' || value == 'dirty') {
83 this.parent.unfreeze();
84 } else if (value == 'unsynced') {
85 this.parent.freeze('Niezsynchronizowany...');
86 } else if (value == 'loading') {
87 this.parent.freeze('Ładowanie...');
88 } else if (value == 'saving') {
89 this.parent.freeze('Zapisywanie...');
93 $currentPage: function() {
94 if(this.currentPage >= 0 && this.currentPage < this.$pages.length)
95 return $(this.$pages[this.currentPage]);
100 cleanPage: function($page) {
102 $('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
159 var vp_width = this.$pageListRoot.width();
160 var vp_height = this.$pageListRoot.height();
162 var width = $page.outerWidth();
163 var height = $page.outerHeight();
165 if( offset.x+width-MARGIN < 0 ) {
166 // console.log('too much on the left', offset.x, -width)
167 offset.x = -width+MARGIN;
170 // too much on the right
171 if( offset.x > vp_width-MARGIN) {
172 offset.x = vp_width-MARGIN;
173 // console.log('too much on the right', offset.x, vp_width, width)
177 if( offset.y+height-MARGIN < 0)
178 offset.y = -height+MARGIN;
180 if( offset.y > vp_height-MARGIN)
181 offset.y = vp_height-MARGIN;
183 $page.css({left: offset.x, top: offset.y});
186 renderImage: function(target, source) {
187 target.html('<img src="'+source+'" />');
190 'user-select': 'none',
191 '-webkit-user-select': 'none',
192 '-khtml-user-select': 'none',
193 '-moz-user-select': 'none',
195 attr('unselectable', 'on').
196 mousedown(this.pageDragStart.bind(this));
200 /* first unbind all */
202 // this.pageListElement
203 if(this.$nextButton) this.$nextButton.unbind();
204 if(this.$prevButton) this.$prevButton.unbind();
205 if(this.$jumpButton) this.$jumpButton.unbind();
208 this.element.html(render_template(this.template, this));
210 /* fetch important parts */
211 this.$pageListRoot = $('.image-gallery-page-list', this.element);
212 this.$pages = $('.image-gallery-page-container', this.$pageListRoot);
214 this.$nextButton = $('.image-gallery-next-button', this.element);
215 this.$prevButton = $('.image-gallery-prev-button', this.element);
216 this.$pageInput = $('.image-gallery-current-page', this.element);
219 this.$nextButton.click( this.nextPage.bind(this) );
220 this.$prevButton.click( this.prevPage.bind(this) );
222 this.$pageInput.change( this.jumpToPage.bind(this) );
225 jumpToPage: function() {
226 this.gotoPage(parseInt(this.$pageInput.val())-1);
229 nextPage: function() {
230 this.gotoPage(this.currentPage + 1);
233 prevPage: function() {
234 this.gotoPage(this.currentPage - 1);
237 dispose: function() {
238 this.model.removeObserver(this);
244 panels['gallery'] = ImageGalleryView;