Uproszczenie implementacji wzorca observer.
[redakcja.git] / project / static / js / views / view.js
1 /*globals Class render_template*/
2 var View = Class.extend({
3   element: null,
4   model: null,
5   template: null,
6   overlayClass: 'view-overlay',
7   overlay: null,
8
9   init: function(element, model, template) {
10     this.element = $(element);
11     this.model = model;
12     this.template = template || this.template;
13     
14     if (this.template) {
15       this.element.html(render_template(this.template, {}));
16     }
17   },
18
19   frozen: function() {
20     return !!this.overlay;
21   },
22   
23   freeze: function(message) {
24     this.overlay = this.overlay 
25       || $('<div><div>' + message + '</div></div>')
26             .addClass(this.overlayClass)
27             .css({
28               position: 'absolute',
29               width: this.element.width(),
30               height: this.element.height(),
31               top: this.element.position().top,
32               left: this.element.position().left
33             })
34             .appendTo(this.element.parent());
35             
36     this.overlay.children('div').css({
37       position: 'relative',
38       top: this.overlay.height() / 2 - 20
39     });
40   },
41   
42   unfreeze: function() {
43     if (this.frozen()) {
44       this.overlay.remove();
45       this.overlay = null;      
46     }
47   },
48
49   dispose: function() {
50     this.unfreeze();
51     this.element.contents().remove();
52   }
53 });