]> git.donarmstrong.com Git - roundcube.git/blob - skins/default/splitter.js
Imported Upstream version 0.1
[roundcube.git] / skins / default / splitter.js
1
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');
14   this.offset_1 = bw.ie ? 0 : -1;
15   this.offset_2 = bw.ie ? -2 : 1;
16   this.pos = 0;
17
18   this.init = function()
19     {
20     this.p1 = document.getElementById(this.p1id);
21     this.p2 = document.getElementById(this.p2id);
22     
23     // create and position the handle for this splitter
24     this.p1pos = rcube_get_object_pos(this.p1);
25     this.p2pos = rcube_get_object_pos(this.p2);
26     var top = this.p1pos.y + this.p1.offsetHeight;
27     var height = this.p2pos.y - this.p1pos.y - this.p1.offsetHeight;
28     var left = this.p1pos.x + this.p1.offsetWidth;
29     var width = this.p2pos.x - this.p1pos.x - this.p1.offsetWidth;
30     
31     if (this.horizontal)
32       this.layer = new rcube_layer(this.id, {x: this.p1pos.x, y: top, height: height, width: this.p1.offsetWidth, vis: 1});
33     else
34       this.layer = new rcube_layer(this.id, {x: left, y: this.p1pos.y, width: width, height: this.p1.offsetHeight, vis: 1});
35
36     this.elm = this.layer.elm;
37     this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
38
39     // add the mouse event listeners
40     rcube_event.add_listener({element: this.elm, event:'mousedown', object:this, method:'onDragStart'});
41     rcube_event.add_listener({element: window, event:'resize', object:this, method:'onResize'});
42
43     // read saved position form cookie
44     var cookie = bw.get_cookie(this.id);
45     if (cookie)
46       {
47       var param = cookie.split(':');
48       for (var i=0, p; i<param.length; i++)
49         {
50         p = param[i].split('=');
51         this[p[0]] = !isNaN(p[1]) ? parseFloat(p[1]) : p[1];
52         }
53
54       this.resize();
55       }
56     };
57
58   /**
59    * Set size and position of all DOM objects
60    * according to the saved splitter position
61    */
62   this.resize = function()
63     {
64      if (this.horizontal)
65       {
66       this.p1.style.height = Math.floor(this.pos - this.p1pos.y - this.layer.height / 2 + this.offset_1) + 'px';
67       this.p2.style.top = Math.ceil(this.pos + (this.layer.height / 2 + this.offset_2)) + 'px';
68       this.layer.move(this.layer.x, Math.round(this.pos - this.layer.height / 2 + 1));
69       }
70     else
71       {
72       this.p1.style.width = Math.floor(this.pos - this.p1pos.x - this.layer.width / 2 + this.offset_1) + 'px';
73       this.p2.style.left = Math.ceil(this.pos + this.layer.width / 2 + this.offset_2) + 'px';
74       this.layer.move(Math.round(this.pos - this.layer.width / 2 + 1), this.layer.y);
75       }
76     };
77
78   /**
79    * Handler for mousedown events
80    */
81   this.onDragStart = function(e)
82     {
83     this.p1pos = rcube_get_object_pos(this.p1);
84     this.p2pos = rcube_get_object_pos(this.p2);
85
86     // start listening to mousemove events
87     rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
88     rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
89
90     // need to listen in any iframe documents too, b/c otherwise the splitter stops moving when we move over an iframe
91     var iframes = document.getElementsByTagName('IFRAME');
92     this.iframe_events = Object();
93     for (var n in iframes)
94       {
95       var iframedoc = null;
96       if (iframes[n].contentDocument)
97         iframedoc = iframes[n].contentDocument;
98       else if (iframes[n].contentWindow)
99         iframedoc = iframes[n].contentWindow.document;
100       else if (iframes[n].document)
101         iframedoc = iframes[n].document;
102       if (iframedoc)
103         {
104         // I don't use the add_listener function for this one because I need to create closures to fetch
105         // the position of each iframe when the event is received
106         var s = this;
107         var id = iframes[n].id;
108         this.iframe_events[n] = function(e){ e._rc_pos_offset = rcube_get_object_pos(document.getElementById(id)); return s.onDrag(e); }
109         if (iframedoc.addEventListener)
110           iframedoc.addEventListener('mousemove', this.iframe_events[n], false);
111         else if (iframes[n].attachEvent)
112           iframedoc.attachEvent('onmousemove', this.iframe_events[n]);
113         else
114           iframedoc['onmousemove'] = this.iframe_events[n];
115
116         rcube_event.add_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
117         }
118       }
119     }
120
121   /**
122    * Handler for mousemove events
123    */
124   this.onDrag = function(e)
125     {
126     var pos = rcube_event.get_mouse_pos(e);
127     if (e._rc_pos_offset)
128       {
129       pos.x += e._rc_pos_offset.x;
130       pos.y += e._rc_pos_offset.y;
131       }
132
133     if (this.horizontal)
134       {
135       if (((pos.y - this.layer.height * 1.5) > this.p1pos.y) && ((pos.y + this.layer.height * 1.5) < (this.p2pos.y + this.p2.offsetHeight)))
136         {
137         this.pos = pos.y;
138         this.resize();
139         }
140       }
141     else
142       {
143       if (((pos.x - this.layer.width * 1.5) > this.p1pos.x) && ((pos.x + this.layer.width * 1.5) < (this.p2pos.x + this.p2.offsetWidth)))
144         {
145         this.pos = pos.x;
146         this.resize();
147         }
148       }
149
150     this.p1pos = rcube_get_object_pos(this.p1);
151     this.p2pos = rcube_get_object_pos(this.p2);
152     return false;
153     };
154
155   /**
156    * Handler for mouseup events
157    */
158   this.onDragStop = function(e)
159     {
160     // cancel the listening for drag events
161     rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
162     rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
163     var iframes = document.getElementsByTagName('IFRAME');
164
165     for (var n in iframes)
166       {
167       var iframedoc;
168       if (iframes[n].contentDocument)
169         iframedoc = iframes[n].contentDocument;
170       else if (iframes[n].contentWindow)
171         iframedoc = iframes[n].contentWindow.document;
172       else if (iframes[n].document)
173         iframedoc = iframes[n].document;
174       if (iframedoc)
175         {
176         if (this.iframe_events[n]) {
177           if (iframedoc.removeEventListener)
178             iframedoc.removeEventListener('mousemove', this.iframe_events[n], false);
179           else if (iframedoc.detachEvent)
180             iframedoc.detachEvent('onmousemove', this.iframe_events[n]);
181           else
182             iframedoc['onmousemove'] = null;
183         }
184
185         rcube_event.remove_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
186         }
187       }
188
189     // save state in cookie
190     var exp = new Date();
191     exp.setYear(exp.getFullYear() + 1);
192     bw.set_cookie(this.id, 'pos='+this.pos, exp);
193
194     return bw.safari ? true : rcube_event.cancel(e);
195     };
196
197   /**
198    * Handler for window resize events
199    */
200   this.onResize = function(e)
201     {
202     this.p1pos = rcube_get_object_pos(this.p1);
203     this.p2pos = rcube_get_object_pos(this.p2);
204     var height = this.horizontal ? this.p2pos.y - this.p1pos.y - this.p1.offsetHeight : this.p1.offsetHeight;
205     var width = this.horizontal ? this.p1.offsetWidth : this.p2pos.x - this.p1pos.x - this.p1.offsetWidth;
206     this.layer.resize(width, height);
207     };
208
209   }  // end class rcube_splitter