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;
18 this.callback = attrib.callback;
20 this.init = function()
22 this.p1 = document.getElementById(this.p1id);
23 this.p2 = document.getElementById(this.p2id);
25 // create and position the handle for this splitter
26 this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
27 this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
29 if (this.horizontal) {
30 var top = this.p1pos.top + this.p1.offsetHeight;
31 this.layer = new rcube_layer(this.id, {x: 0, y: top, height: 10,
32 width: '100%', vis: 1, parent: this.p1.parentNode});
35 var left = this.p1pos.left + this.p1.offsetWidth;
36 this.layer = new rcube_layer(this.id, {x: left, y: 0, width: 10,
37 height: '100%', vis: 1, parent: this.p1.parentNode});
40 this.elm = this.layer.elm;
41 this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
42 this.elm.unselectable = 'on';
44 // add the mouse event listeners
45 rcube_event.add_listener({element: this.elm, event:'mousedown', object:this, method:'onDragStart'});
47 rcube_event.add_listener({element: window, event:'resize', object:this, method:'onResize'});
49 // read saved position from cookie
50 var cookie = bw.get_cookie(this.id);
51 if (cookie && !isNaN(cookie)) {
52 this.pos = parseFloat(cookie);
62 * Set size and position of all DOM objects
63 * according to the saved splitter position
65 this.resize = function()
67 if (this.horizontal) {
68 var lh = this.layer.height - this.offset * 2;
69 this.p1.style.height = Math.floor(this.pos - this.p1pos.top - lh / 2) + 'px';
70 this.p2.style.top = Math.ceil(this.pos + lh / 2) + 'px';
71 this.layer.move(this.layer.x, Math.round(this.pos - lh / 2 + 1));
73 var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
74 this.p2.style.height = (new_height > 0 ? new_height : 0) + 'px';
78 this.p1.style.width = Math.floor(this.pos - this.p1pos.left - this.layer.width / 2) + 'px';
79 this.p2.style.left = Math.ceil(this.pos + this.layer.width / 2) + 'px';
80 this.layer.move(Math.round(this.pos - this.layer.width / 2 + 1), this.layer.y);
82 var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10) ;
83 this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
91 * Handler for mousedown events
93 this.onDragStart = function(e)
95 // disable text selection while dragging the splitter
96 if (bw.konq || bw.chrome || bw.safari)
97 document.body.style.webkitUserSelect = 'none';
99 this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
100 this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
101 this.drag_active = true;
103 // start listening to mousemove events
104 rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
105 rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
107 // enable dragging above iframes
108 $('iframe').each(function() {
109 $('<div class="iframe-splitter-fix"></div>')
110 .css({background: '#fff',
111 width: this.offsetWidth+'px', height: this.offsetHeight+'px',
112 position: 'absolute', opacity: '0.001', zIndex: 1000
114 .css($(this).offset())
120 * Handler for mousemove events
122 this.onDrag = function(e)
124 if (!this.drag_active)
127 var pos = rcube_event.get_mouse_pos(e);
130 var parent = $(this.p1.parentNode).offset();
131 pos.x -= parent.left;
135 if (this.horizontal) {
136 if (((pos.y - this.layer.height * 1.5) > this.p1pos.top) && ((pos.y + this.layer.height * 1.5) < (this.p2pos.top + this.p2.offsetHeight))) {
142 if (((pos.x - this.layer.width * 1.5) > this.p1pos.left) && ((pos.x + this.layer.width * 1.5) < (this.p2pos.left + this.p2.offsetWidth))) {
148 this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
149 this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
154 * Handler for mouseup events
156 this.onDragStop = function(e)
158 // resume the ability to highlight text
159 if (bw.konq || bw.chrome || bw.safari)
160 document.body.style.webkitUserSelect = 'auto';
162 // cancel the listening for drag events
163 rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
164 rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
165 this.drag_active = false;
168 $('div.iframe-splitter-fix').each(function() { this.parentNode.removeChild(this); });
172 if (typeof this.callback == 'function')
175 return bw.safari ? true : rcube_event.cancel(e);
179 * Handler for window resize events
181 this.onResize = function(e)
183 if (this.horizontal) {
184 var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
185 this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
188 var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10);
189 this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
194 * Saves splitter position in cookie
196 this.set_cookie = function()
198 var exp = new Date();
199 exp.setYear(exp.getFullYear() + 1);
200 bw.set_cookie(this.id, this.pos, exp);
203 } // end class rcube_splitter