thomascube
2009-05-26 b4f7c665bbe31d4ddbdca8c9f89eedb1d6187582
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         {
05b9a2 78         var new_height = (parseInt(this.p2.parentNode.offsetHeight) - parseInt(this.p2.style.top));
A 79         this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
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     {
cc97ea 97     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 98     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
e5686f 99     this.drag_active = true;
A 100     
a7d5c6 101     // start listening to mousemove events
T 102     rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
103     rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
104
105     // need to listen in any iframe documents too, b/c otherwise the splitter stops moving when we move over an iframe
106     var iframes = document.getElementsByTagName('IFRAME');
107     this.iframe_events = Object();
108     for (var n in iframes)
109       {
110       var iframedoc = null;
111       if (iframes[n].contentDocument)
112         iframedoc = iframes[n].contentDocument;
113       else if (iframes[n].contentWindow)
114         iframedoc = iframes[n].contentWindow.document;
115       else if (iframes[n].document)
116         iframedoc = iframes[n].document;
117       if (iframedoc)
118         {
119         // I don't use the add_listener function for this one because I need to create closures to fetch
120         // the position of each iframe when the event is received
121         var s = this;
cc97ea 122         var id = '#'+iframes[n].id;
T 123         this.iframe_events[n] = function(e){ e._offset = $(id).offset(); return s.onDrag(e); }
e5686f 124
a7d5c6 125         if (iframedoc.addEventListener)
T 126           iframedoc.addEventListener('mousemove', this.iframe_events[n], false);
127         else if (iframes[n].attachEvent)
128           iframedoc.attachEvent('onmousemove', this.iframe_events[n]);
129         else
130           iframedoc['onmousemove'] = this.iframe_events[n];
131
132         rcube_event.add_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
133         }
134       }
135     }
136
137   /**
138    * Handler for mousemove events
139    */
140   this.onDrag = function(e)
141     {
e5686f 142     if (!this.drag_active) return false;
A 143
05b9a2 144     var pos = rcube_event.get_mouse_pos(e);
e5686f 145
A 146     if (this.relative)
147       {
cc97ea 148       var parent = $(this.p1.parentNode).offset();
T 149       pos.x -= parent.left;
150       pos.y -= parent.top;
a7d5c6 151       }
T 152
153     if (this.horizontal)
154       {
cc97ea 155       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 156         {
T 157         this.pos = pos.y;
158         this.resize();
159         }
160       }
161     else
162       {
cc97ea 163       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 164         {
T 165         this.pos = pos.x;
166         this.resize();
167         }
168       }
169
cc97ea 170     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 171     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
a7d5c6 172     return false;
T 173     };
174
175   /**
176    * Handler for mouseup events
177    */
178   this.onDragStop = function(e)
179     {
180     // cancel the listening for drag events
181     rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
182     rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
e5686f 183     this.drag_active = false;
A 184
a7d5c6 185     var iframes = document.getElementsByTagName('IFRAME');
T 186
187     for (var n in iframes)
188       {
189       var iframedoc;
190       if (iframes[n].contentDocument)
191         iframedoc = iframes[n].contentDocument;
192       else if (iframes[n].contentWindow)
193         iframedoc = iframes[n].contentWindow.document;
194       else if (iframes[n].document)
195         iframedoc = iframes[n].document;
196       if (iframedoc)
197         {
198         if (this.iframe_events[n]) {
199           if (iframedoc.removeEventListener)
200             iframedoc.removeEventListener('mousemove', this.iframe_events[n], false);
cc97ea 201           else if (iframedoc.detachEvent)
a7d5c6 202             iframedoc.detachEvent('onmousemove', this.iframe_events[n]);
T 203           else
204             iframedoc['onmousemove'] = null;
e5686f 205           }
a7d5c6 206
T 207         rcube_event.remove_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
208         }
209       }
210
e5686f 211     this.set_cookie();
7c2d30 212
a7d5c6 213     return bw.safari ? true : rcube_event.cancel(e);
T 214     };
05b9a2 215
a7d5c6 216   /**
T 217    * Handler for window resize events
218    */
219   this.onResize = function(e)
220     {
e5686f 221     if (this.horizontal)
05b9a2 222       {
A 223       var new_height = (parseInt(this.p2.parentNode.offsetHeight) - parseInt(this.p2.style.top));
224       this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
225       }
e5686f 226     else
A 227       this.p2.style.width = (parseInt(this.p2.parentNode.offsetWidth) - parseInt(this.p2.style.left))+'px';
a7d5c6 228     };
T 229
e5686f 230   this.set_cookie = function()
A 231     {
232     // save state in cookie
233     var exp = new Date();
234     exp.setYear(exp.getFullYear() + 1);
80c678 235     bw.set_cookie(this.id, this.pos, exp);
e5686f 236     }
A 237
a7d5c6 238   }  // end class rcube_splitter