Basic gallery.
[redakcja.git] / project / static / js / views / gallery.js
1 /*global View render_template panels */
2 var ImageGalleryView = View.extend({
3   _className: 'ImageGalleryView',
4   element: null,
5   model: null,
6   template: 'image-gallery-view-template',
7   
8   init: function(element, model, parent, template) 
9   {
10     this.currentPage = 0;
11     
12     this._super(element, model, template);
13     this.parent = parent;
14        
15     this.model
16       .addObserver(this, 'data', this.modelDataChanged.bind(this))
17       .addObserver(this, 'state', this.modelStateChanged.bind(this));
18       
19     //$('.image-gallery-view', this.element).html(this.model.get('data'));
20     this.modelStateChanged('state', this.model.get('state'));
21     this.model.load();
22   },
23   
24   modelDataChanged: function(property, value) 
25   {
26     $.log('updating pages', property, value);
27     if( property == 'data')
28     {        
29         this.gotoPage(this.currentPage);        
30         this.element.html(render_template(this.template, this));        
31     }   
32   },
33
34   gotoPage: function(index) {
35      if (index < 0) 
36          index = 0;
37
38      var n = this.model.get('pages').length;
39      if (index >= n) index = n-1;
40
41      this.currentPage = index;
42   },
43   
44   modelStateChanged: function(property, value) {
45     if (value == 'synced' || value == 'dirty') {
46       this.parent.unfreeze();
47     } else if (value == 'unsynced') {
48       this.parent.freeze('Niezsynchronizowany...');
49     } else if (value == 'loading') {
50       this.parent.freeze('Ɓadowanie...');
51     } else if (value == 'saving') {
52       this.parent.freeze('Zapisywanie...');
53     }
54   },
55   
56   dispose: function() {
57     this.model.removeObserver(this);
58     this._super();
59   }
60 });
61
62 // Register view
63 panels['gallery'] = ImageGalleryView;