3 * Roundcube splitter GUI class
7 function rcube_splitter(attrib)
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;
19 this.init = function()
21 this.p1 = document.getElementById(this.p1id);
22 this.p2 = document.getElementById(this.p2id);
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();
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});
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});
39 this.elm = this.layer.elm;
40 this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
41 this.elm.unselectable = 'on';
43 // add the mouse event listeners
44 rcube_event.add_listener({element: this.elm, event:'mousedown', object:this, method:'onDragStart'});
46 rcube_event.add_listener({element: window, event:'resize', object:this, method:'onResize'});
48 // read saved position from cookie
49 var cookie = bw.get_cookie(this.id);
50 if (cookie && !isNaN(cookie)) {
51 this.pos = parseFloat(cookie);
61 * Set size and position of all DOM objects
62 * according to the saved splitter position
64 this.resize = function()
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));
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';
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);
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';
90 * Handler for mousedown events
92 this.onDragStart = function(e)
94 // disable text selection while dragging the splitter
95 if (bw.konq || bw.chrome || bw.safari)
96 document.body.style.webkitUserSelect = 'none';
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;
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'});
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
113 .css($(this).offset())
119 * Handler for mousemove events
121 this.onDrag = function(e)
123 if (!this.drag_active)
126 var pos = rcube_event.get_mouse_pos(e);
129 var parent = $(this.p1.parentNode).offset();
130 pos.x -= parent.left;
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))) {
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))) {
147 this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
148 this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
153 * Handler for mouseup events
155 this.onDragStop = function(e)
157 // resume the ability to highlight text
158 if (bw.konq || bw.chrome || bw.safari)
159 document.body.style.webkitUserSelect = 'auto';
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;
167 $('div.iframe-splitter-fix').each(function() { this.parentNode.removeChild(this); });
171 return bw.safari ? true : rcube_event.cancel(e);
175 * Handler for window resize events
177 this.onResize = function(e)
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';
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';
190 * Saves splitter position in cookie
192 this.set_cookie = function()
194 var exp = new Date();
195 exp.setYear(exp.getFullYear() + 1);
196 bw.set_cookie(this.id, this.pos, exp);
199 } // end class rcube_splitter