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();
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});
36 var left = this.p1pos.left + this.p1.offsetWidth;
37 this.layer = new rcube_layer(this.id, {x: left, y: 0, width: 10,
38 height: '100%', vis: 1, parent: this.p1.parentNode});
41 this.elm = this.layer.elm;
42 this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
43 this.elm.unselectable = 'on';
45 // add the mouse event listeners
46 rcube_event.add_listener({element: this.elm, event:'mousedown', object:this, method:'onDragStart'});
48 rcube_event.add_listener({element: window, event:'resize', object:this, method:'onResize'});
50 // read saved position from cookie
51 var cookie = bw.get_cookie(this.id);
52 if (cookie && !isNaN(cookie))
54 this.pos = parseFloat(cookie);
65 * Set size and position of all DOM objects
66 * according to the saved splitter position
68 this.resize = function()
72 var lh = this.layer.height - this.offset * 2;
73 this.p1.style.height = Math.floor(this.pos - this.p1pos.top - lh / 2) + 'px';
74 this.p2.style.top = Math.ceil(this.pos + lh / 2) + 'px';
75 this.layer.move(this.layer.x, Math.round(this.pos - lh / 2 + 1));
78 var new_height = (parseInt(this.p2.parentNode.offsetHeight) - parseInt(this.p2.style.top) - (bw.ie8 ? 2 : 0));
79 this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
84 this.p1.style.width = Math.floor(this.pos - this.p1pos.left - this.layer.width / 2) + 'px';
85 this.p2.style.left = Math.ceil(this.pos + this.layer.width / 2) + 'px';
86 this.layer.move(Math.round(this.pos - this.layer.width / 2 + 1), this.layer.y);
88 this.p2.style.width = (parseInt(this.p2.parentNode.offsetWidth) - parseInt(this.p2.style.left))+'px';
93 * Handler for mousedown events
95 this.onDragStart = function(e)
97 // disable text selection while dragging the splitter
98 if (window.webkit || bw.safari)
99 document.body.style.webkitUserSelect = 'none';
101 this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
102 this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
103 this.drag_active = true;
105 // start listening to mousemove events
106 rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
107 rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
109 // need to listen in any iframe documents too, b/c otherwise the splitter stops moving when we move over an iframe
110 var iframes = document.getElementsByTagName('iframe');
111 this.iframe_events = Object();
112 for (var n in iframes)
114 var iframedoc = null;
115 if (iframes[n].contentDocument)
116 iframedoc = iframes[n].contentDocument;
117 else if (iframes[n].contentWindow)
118 iframedoc = iframes[n].contentWindow.document;
119 else if (iframes[n].document)
120 iframedoc = iframes[n].document;
123 // I don't use the add_listener function for this one because I need to create closures to fetch
124 // the position of each iframe when the event is received
126 var id = '#'+iframes[n].id;
127 this.iframe_events[n] = function(e){ e._offset = $(id).offset(); return s.onDrag(e); }
129 if (iframedoc.addEventListener)
130 iframedoc.addEventListener('mousemove', this.iframe_events[n], false);
131 else if (iframes[n].attachEvent)
132 iframedoc.attachEvent('onmousemove', this.iframe_events[n]);
134 iframedoc['onmousemove'] = this.iframe_events[n];
136 rcube_event.add_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
142 * Handler for mousemove events
144 this.onDrag = function(e)
146 if (!this.drag_active) return false;
148 var pos = rcube_event.get_mouse_pos(e);
152 var parent = $(this.p1.parentNode).offset();
153 pos.x -= parent.left;
159 if (((pos.y - this.layer.height * 1.5) > this.p1pos.top) && ((pos.y + this.layer.height * 1.5) < (this.p2pos.top + this.p2.offsetHeight)))
167 if (((pos.x - this.layer.width * 1.5) > this.p1pos.left) && ((pos.x + this.layer.width * 1.5) < (this.p2pos.left + this.p2.offsetWidth)))
174 this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
175 this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
180 * Handler for mouseup events
182 this.onDragStop = function(e)
184 // resume the ability to highlight text
185 if(window.webkit || bw.safari)
186 document.body.style.webkitUserSelect = 'auto';
188 // cancel the listening for drag events
189 rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
190 rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
191 this.drag_active = false;
193 var iframes = document.getElementsByTagName('iframe');
195 for (var n in iframes)
198 if (iframes[n].contentDocument)
199 iframedoc = iframes[n].contentDocument;
200 else if (iframes[n].contentWindow)
201 iframedoc = iframes[n].contentWindow.document;
202 else if (iframes[n].document)
203 iframedoc = iframes[n].document;
206 if (this.iframe_events[n]) {
207 if (iframedoc.removeEventListener)
208 iframedoc.removeEventListener('mousemove', this.iframe_events[n], false);
209 else if (iframedoc.detachEvent)
210 iframedoc.detachEvent('onmousemove', this.iframe_events[n]);
212 iframedoc['onmousemove'] = null;
215 rcube_event.remove_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
221 return bw.safari ? true : rcube_event.cancel(e);
225 * Handler for window resize events
227 this.onResize = function(e)
231 var new_height = (parseInt(this.p2.parentNode.offsetHeight) - parseInt(this.p2.style.top) - (bw.ie8 ? 2 : 0));
232 this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
235 this.p2.style.width = (parseInt(this.p2.parentNode.offsetWidth) - parseInt(this.p2.style.left))+'px';
238 this.set_cookie = function()
240 // save state in cookie
241 var exp = new Date();
242 exp.setYear(exp.getFullYear() + 1);
243 bw.set_cookie(this.id, this.pos, exp);
246 } // end class rcube_splitter