alecpl
2010-03-04 4e4445f55b2ac36381db5a30126510b13488b09a
commit | author | age
a7d5c6 1
T 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');
e5686f 14   this.offset = bw.ie6 ? 2 : 0;
A 15   this.pos = attrib.start ? attrib.start * 1 : 0;
16   this.relative = attrib.relative ? true : false;
17   this.drag_active = false;
a7d5c6 18
T 19   this.init = function()
20     {
21     this.p1 = document.getElementById(this.p1id);
22     this.p2 = document.getElementById(this.p2id);
e5686f 23
a7d5c6 24     // create and position the handle for this splitter
cc97ea 25     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 26     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
a7d5c6 27     
T 28     if (this.horizontal)
e5686f 29       {
cc97ea 30       var top = this.p1pos.top + this.p1.offsetHeight;
e5686f 31       this.layer = new rcube_layer(this.id, {x: 0, y: top, height: 10, 
A 32             width: '100%', vis: 1, parent: this.p1.parentNode});
33       }
a7d5c6 34     else
e5686f 35       {
cc97ea 36       var left = this.p1pos.left + this.p1.offsetWidth;
e5686f 37       this.layer = new rcube_layer(this.id, {x: left, y: 0, width: 10, 
A 38             height: '100%', vis: 1,  parent: this.p1.parentNode});
39       }
a7d5c6 40
T 41     this.elm = this.layer.elm;
42     this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
e5686f 43     this.elm.unselectable = 'on';
a7d5c6 44
T 45     // add the mouse event listeners
46     rcube_event.add_listener({element: this.elm, event:'mousedown', object:this, method:'onDragStart'});
e5686f 47     if (bw.ie)
A 48       rcube_event.add_listener({element: window, event:'resize', object:this, method:'onResize'});
a7d5c6 49
7c2d30 50     // read saved position from cookie
a7d5c6 51     var cookie = bw.get_cookie(this.id);
80c678 52     if (cookie && !isNaN(cookie))
a7d5c6 53       {
80c678 54       this.pos = parseFloat(cookie);
a7d5c6 55       this.resize();
e5686f 56       }
A 57     else if (this.pos)
58       {
59       this.resize();
60       this.set_cookie();
a7d5c6 61       }
T 62     };
63
64   /**
65    * Set size and position of all DOM objects
66    * according to the saved splitter position
67    */
68   this.resize = function()
69     {
e5686f 70     if (this.horizontal)
a7d5c6 71       {
e5686f 72       var lh = this.layer.height - this.offset * 2;
cc97ea 73       this.p1.style.height = Math.floor(this.pos - this.p1pos.top - lh / 2) + 'px';
e5686f 74       this.p2.style.top = Math.ceil(this.pos + lh / 2) + 'px';
cc97ea 75       this.layer.move(this.layer.x, Math.round(this.pos - lh / 2 + 1));
e5686f 76       if (bw.ie)
cc97ea 77         {
52bd7b 78         var new_height = (parseInt(this.p2.parentNode.offsetHeight) - parseInt(this.p2.style.top) - (bw.ie8 ? 2 : 0));
05b9a2 79         this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
A 80         }
a7d5c6 81       }
T 82     else
83       {
cc97ea 84       this.p1.style.width = Math.floor(this.pos - this.p1pos.left - this.layer.width / 2) + 'px';
e5686f 85       this.p2.style.left = Math.ceil(this.pos + this.layer.width / 2) + 'px';
a7d5c6 86       this.layer.move(Math.round(this.pos - this.layer.width / 2 + 1), this.layer.y);
e5686f 87       if (bw.ie)
A 88         this.p2.style.width = (parseInt(this.p2.parentNode.offsetWidth) - parseInt(this.p2.style.left))+'px';
a7d5c6 89       }
T 90     };
91
92   /**
93    * Handler for mousedown events
94    */
95   this.onDragStart = function(e)
96     {
f9aeec 97     // disable text selection while dragging the splitter
A 98     if (window.webkit || bw.safari)
99       document.body.style.webkitUserSelect = 'none';
100
cc97ea 101     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 102     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
e5686f 103     this.drag_active = true;
f9aeec 104
a7d5c6 105     // start listening to mousemove events
T 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'});
108
109     // need to listen in any iframe documents too, b/c otherwise the splitter stops moving when we move over an iframe
3541d0 110     var iframes = document.getElementsByTagName('iframe');
a7d5c6 111     this.iframe_events = Object();
T 112     for (var n in iframes)
113       {
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;
121       if (iframedoc)
122         {
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
125         var s = this;
cc97ea 126         var id = '#'+iframes[n].id;
T 127         this.iframe_events[n] = function(e){ e._offset = $(id).offset(); return s.onDrag(e); }
e5686f 128
a7d5c6 129         if (iframedoc.addEventListener)
T 130           iframedoc.addEventListener('mousemove', this.iframe_events[n], false);
131         else if (iframes[n].attachEvent)
132           iframedoc.attachEvent('onmousemove', this.iframe_events[n]);
133         else
134           iframedoc['onmousemove'] = this.iframe_events[n];
135
136         rcube_event.add_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
137         }
138       }
139     }
140
141   /**
142    * Handler for mousemove events
143    */
144   this.onDrag = function(e)
145     {
e5686f 146     if (!this.drag_active) return false;
A 147
05b9a2 148     var pos = rcube_event.get_mouse_pos(e);
e5686f 149
A 150     if (this.relative)
151       {
cc97ea 152       var parent = $(this.p1.parentNode).offset();
T 153       pos.x -= parent.left;
154       pos.y -= parent.top;
a7d5c6 155       }
T 156
157     if (this.horizontal)
158       {
cc97ea 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)))
a7d5c6 160         {
T 161         this.pos = pos.y;
162         this.resize();
163         }
164       }
165     else
166       {
cc97ea 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)))
a7d5c6 168         {
T 169         this.pos = pos.x;
170         this.resize();
171         }
172       }
173
cc97ea 174     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 175     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
a7d5c6 176     return false;
T 177     };
178
179   /**
180    * Handler for mouseup events
181    */
182   this.onDragStop = function(e)
183     {
f9aeec 184     // resume the ability to highlight text
A 185     if(window.webkit || bw.safari)
186       document.body.style.webkitUserSelect = 'auto';
187
a7d5c6 188     // cancel the listening for drag events
T 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'});
e5686f 191     this.drag_active = false;
A 192
3541d0 193     var iframes = document.getElementsByTagName('iframe');
a7d5c6 194
T 195     for (var n in iframes)
196       {
197       var iframedoc;
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;
204       if (iframedoc)
205         {
206         if (this.iframe_events[n]) {
207           if (iframedoc.removeEventListener)
208             iframedoc.removeEventListener('mousemove', this.iframe_events[n], false);
cc97ea 209           else if (iframedoc.detachEvent)
a7d5c6 210             iframedoc.detachEvent('onmousemove', this.iframe_events[n]);
T 211           else
212             iframedoc['onmousemove'] = null;
e5686f 213           }
a7d5c6 214
T 215         rcube_event.remove_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
216         }
217       }
218
e5686f 219     this.set_cookie();
7c2d30 220
a7d5c6 221     return bw.safari ? true : rcube_event.cancel(e);
T 222     };
05b9a2 223
a7d5c6 224   /**
T 225    * Handler for window resize events
226    */
227   this.onResize = function(e)
228     {
e5686f 229     if (this.horizontal)
05b9a2 230       {
52bd7b 231       var new_height = (parseInt(this.p2.parentNode.offsetHeight) - parseInt(this.p2.style.top) - (bw.ie8 ? 2 : 0));
05b9a2 232       this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
A 233       }
e5686f 234     else
A 235       this.p2.style.width = (parseInt(this.p2.parentNode.offsetWidth) - parseInt(this.p2.style.left))+'px';
a7d5c6 236     };
T 237
e5686f 238   this.set_cookie = function()
A 239     {
240     // save state in cookie
241     var exp = new Date();
242     exp.setYear(exp.getFullYear() + 1);
80c678 243     bw.set_cookie(this.id, this.pos, exp);
e5686f 244     }
A 245
a7d5c6 246   }  // end class rcube_splitter