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