Oops #2
[redakcja.git] / project / static / js / views / split.js
1 /*globals View*/
2
3 // Split view inspired by jQuery Splitter Plugin http://methvin.com/splitter/
4 var SplitView = View.extend({
5   _className: 'SplitView',
6   splitbarClass: 'splitview-splitbar',
7   activeClass: 'splitview-active',
8   overlayClass: 'splitview-overlay',
9   element: null,
10   model: null,
11   zombie: null,
12   leftViewOffset: 0,
13   
14   // Cache
15   _splitbarWidth: 0,
16   
17   init: function(element, model) {
18     this._super(element, model, null);
19     this.element.css('position', 'relative');
20     this._resizingSubviews = false;
21     
22     this.views = $(">*", this.element[0]).css({
23         position: 'absolute',                     // positioned inside splitter container
24         'z-index': 1,                                           // splitbar is positioned above
25         '-moz-outline-style': 'none',   // don't show dotted outline
26       overflow: 'auto'
27     });
28     
29     this.leftView = $(this.views[0]);
30     this.rightView = $(this.views[1]);
31     this.splitbar = $(this.views[2] || '<div></div>')
32       .insertAfter(this.leftView)
33       .css({
34         position: 'absolute',
35         'user-select': 'none',
36         '-webkit-user-select': 'none',
37         '-khtml-user-select': 'none',
38         '-moz-user-select': 'none',
39         'z-index': 100
40       })
41       .attr('unselectable', 'on')
42       .addClass(this.splitbarClass)
43       .bind('mousedown.splitview', this.beginResize.bind(this));
44     
45     this._splitbarWidth = this.splitbar.outerWidth();
46     
47     // Solomon's algorithm ;-)
48     this.resplit(this.element.width() / 2);
49   },
50     
51   beginResize: function(event) {
52     this.zombie = this.zombie || this.splitbar.clone(false).insertAfter(this.leftView);
53     this.overlay = this.overlay || $('<div></div>').addClass(this.overlayClass).css({
54         position: 'absolute',
55         width: this.element.width(),
56         height: this.element.height(),
57         top: this.element.position().top,
58         left: this.element.position().left
59       }).appendTo(this.element);
60     this.views.css("-webkit-user-select", "none"); // Safari selects A/B text on a move
61     this.splitbar.addClass(this.activeClass);
62     this.leftViewOffset = this.leftView[0].offsetWidth - event.pageX;
63     
64     $(document)
65       .bind('mousemove.splitview', this.resizeChanged.bind(this))
66       .bind('mouseup.splitview', this.endResize.bind(this));
67   },
68   
69   resizeChanged: function(event) {
70     var newPosition = event.pageX + this.leftViewOffset;
71     newPosition = Math.max(0, Math.min(newPosition, this.element.width() - this._splitbarWidth));
72     this.splitbar.css('left', newPosition);
73   },
74
75   endResize: function(event) {
76     var newPosition = event.pageX + this.leftViewOffset;
77     this.zombie.remove();
78     this.zombie = null;
79     this.overlay.remove();
80     this.overlay = null;
81     this.resplit(newPosition);
82
83     $(document)
84       .unbind('mousemove.splitview')
85       .unbind('mouseup.splitview');
86   },
87
88   resized: function(event) {
89     if (!this._resizingSubviews) {
90       this.resplit(Math.min(this.leftView.width(), this.element.width() - this._splitbarWidth));
91     }
92   },
93   
94   resplit: function(newPosition) {
95     newPosition = Math.max(0, Math.min(newPosition, this.element.width() - this._splitbarWidth));
96     this.splitbar.css('left', newPosition);
97     this.leftView.css({
98       left: 0,
99       width: newPosition
100     });
101     this.rightView.css({
102       left: newPosition + this._splitbarWidth,
103       width: this.element.width() - newPosition - this._splitbarWidth
104     });
105     if (!$.browser.msie) {
106       this._resizingSubviews = true;
107                   $(window).trigger('resize');
108                   this._resizingSubviews = false;
109                 }
110   },
111   
112   dispose: function() {
113     this.splitter.unbind('mousedown.splitview');
114     this._super();
115   }
116 });
117