]> git.donarmstrong.com Git - roundcube.git/blob - skins/default/splitter.js
e11f9fea5855c2698fe0ff93b0863533abfe24aa
[roundcube.git] / skins / default / splitter.js
1
2 /**
3  * Roundcube splitter GUI class
4  *
5  * @constructor
6  */
7 function rcube_splitter(attrib)
8 {
9   this.p1id = attrib.p1;
10   this.p2id = attrib.p2;
11   this.id = attrib.id ? attrib.id : this.p1id + '_' + this.p2id + '_splitter';
12   this.orientation = attrib.orientation;
13   this.horizontal = (this.orientation == 'horizontal' || this.orientation == 'h');
14   this.offset = bw.ie6 ? 2 : 0;
15   this.pos = attrib.start ? attrib.start * 1 : 0;
16   this.relative = attrib.relative ? true : false;
17   this.drag_active = false;
18
19   this.init = function()
20   {
21     this.p1 = document.getElementById(this.p1id);
22     this.p2 = document.getElementById(this.p2id);
23
24     // create and position the handle for this splitter
25     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
26     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
27
28     if (this.horizontal) {
29       var top = this.p1pos.top + this.p1.offsetHeight;
30       this.layer = new rcube_layer(this.id, {x: 0, y: top, height: 10, 
31             width: '100%', vis: 1, parent: this.p1.parentNode});
32     }
33     else {
34       var left = this.p1pos.left + this.p1.offsetWidth;
35       this.layer = new rcube_layer(this.id, {x: left, y: 0, width: 10, 
36             height: '100%', vis: 1,  parent: this.p1.parentNode});
37     }
38
39     this.elm = this.layer.elm;
40     this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
41     this.elm.unselectable = 'on';
42
43     // add the mouse event listeners
44     rcube_event.add_listener({element: this.elm, event:'mousedown', object:this, method:'onDragStart'});
45     if (bw.ie)
46       rcube_event.add_listener({element: window, event:'resize', object:this, method:'onResize'});
47
48     // read saved position from cookie
49     var cookie = bw.get_cookie(this.id);
50     if (cookie && !isNaN(cookie)) {
51       this.pos = parseFloat(cookie);
52       this.resize();
53     }
54     else if (this.pos) {
55       this.resize();
56       this.set_cookie();
57     }
58   };
59
60   /**
61    * Set size and position of all DOM objects
62    * according to the saved splitter position
63    */
64   this.resize = function()
65   {
66     if (this.horizontal) {
67       var lh = this.layer.height - this.offset * 2;
68       this.p1.style.height = Math.floor(this.pos - this.p1pos.top - lh / 2) + 'px';
69       this.p2.style.top = Math.ceil(this.pos + lh / 2) + 'px';
70       this.layer.move(this.layer.x, Math.round(this.pos - lh / 2 + 1));
71       if (bw.ie) {
72         var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
73         this.p2.style.height = (new_height > 0 ? new_height : 0) + 'px';
74       }
75     }
76     else {
77       this.p1.style.width = Math.floor(this.pos - this.p1pos.left - this.layer.width / 2) + 'px';
78       this.p2.style.left = Math.ceil(this.pos + this.layer.width / 2) + 'px';
79       this.layer.move(Math.round(this.pos - this.layer.width / 2 + 1), this.layer.y);
80       if (bw.ie) {
81         var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10) ;
82         this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
83       }
84     }
85     $(this.p2).resize();
86     $(this.p1).resize();
87   };
88
89   /**
90    * Handler for mousedown events
91    */
92   this.onDragStart = function(e)
93   {
94     // disable text selection while dragging the splitter
95     if (bw.konq || bw.chrome || bw.safari)
96       document.body.style.webkitUserSelect = 'none';
97
98     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
99     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
100     this.drag_active = true;
101
102     // start listening to mousemove events
103     rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
104     rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
105
106     // enable dragging above iframes
107     $('iframe').each(function() {
108       $('<div class="iframe-splitter-fix"></div>')
109         .css({background: '#fff',
110           width: this.offsetWidth+'px', height: this.offsetHeight+'px',
111           position: 'absolute', opacity: '0.001', zIndex: 1000
112         })
113         .css($(this).offset())
114         .appendTo('body');
115       });
116   };
117
118   /**
119    * Handler for mousemove events
120    */
121   this.onDrag = function(e)
122   {
123     if (!this.drag_active)
124       return false;
125
126     var pos = rcube_event.get_mouse_pos(e);
127
128     if (this.relative) {
129       var parent = $(this.p1.parentNode).offset();
130       pos.x -= parent.left;
131       pos.y -= parent.top;
132     }
133
134     if (this.horizontal) {
135       if (((pos.y - this.layer.height * 1.5) > this.p1pos.top) && ((pos.y + this.layer.height * 1.5) < (this.p2pos.top + this.p2.offsetHeight))) {
136         this.pos = pos.y;
137         this.resize();
138       }
139     }
140     else {
141       if (((pos.x - this.layer.width * 1.5) > this.p1pos.left) && ((pos.x + this.layer.width * 1.5) < (this.p2pos.left + this.p2.offsetWidth))) {
142         this.pos = pos.x;
143         this.resize();
144       }
145     }
146
147     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
148     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
149     return false;
150   };
151
152   /**
153    * Handler for mouseup events
154    */
155   this.onDragStop = function(e)
156   {
157     // resume the ability to highlight text
158     if (bw.konq || bw.chrome || bw.safari)
159       document.body.style.webkitUserSelect = 'auto';
160
161     // cancel the listening for drag events
162     rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
163     rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
164     this.drag_active = false;
165
166     // remove temp divs
167     $('div.iframe-splitter-fix').each(function() { this.parentNode.removeChild(this); });
168
169     this.set_cookie();
170
171     return bw.safari ? true : rcube_event.cancel(e);
172   };
173
174   /**
175    * Handler for window resize events
176    */
177   this.onResize = function(e)
178   {
179     if (this.horizontal) {
180       var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
181       this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
182     }
183     else {
184       var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10);
185       this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
186     }
187   };
188
189   /**
190    * Saves splitter position in cookie
191    */
192   this.set_cookie = function()
193   {
194     var exp = new Date();
195     exp.setYear(exp.getFullYear() + 1);
196     bw.set_cookie(this.id, this.pos, exp);
197   };
198
199 } // end class rcube_splitter