]> git.donarmstrong.com Git - roundcube.git/blob - skins/default/splitter.js
Imported Upstream version 0.6+dfsg
[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 = 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;
18   this.callback = attrib.callback;
19
20   this.init = function()
21   {
22     this.p1 = document.getElementById(this.p1id);
23     this.p2 = document.getElementById(this.p2id);
24
25     // create and position the handle for this splitter
26     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
27     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
28
29     if (this.horizontal) {
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});
33     }
34     else {
35       var left = this.p1pos.left + this.p1.offsetWidth;
36       this.layer = new rcube_layer(this.id, {x: left, y: 0, width: 10, 
37             height: '100%', vis: 1,  parent: this.p1.parentNode});
38     }
39
40     this.elm = this.layer.elm;
41     this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
42     this.elm.unselectable = 'on';
43
44     // add the mouse event listeners
45     rcube_event.add_listener({element: this.elm, event:'mousedown', object:this, method:'onDragStart'});
46     if (bw.ie)
47       rcube_event.add_listener({element: window, event:'resize', object:this, method:'onResize'});
48
49     // read saved position from cookie
50     var cookie = bw.get_cookie(this.id);
51     if (cookie && !isNaN(cookie)) {
52       this.pos = parseFloat(cookie);
53       this.resize();
54     }
55     else if (this.pos) {
56       this.resize();
57       this.set_cookie();
58     }
59   };
60
61   /**
62    * Set size and position of all DOM objects
63    * according to the saved splitter position
64    */
65   this.resize = function()
66   {
67     if (this.horizontal) {
68       var lh = this.layer.height - this.offset * 2;
69       this.p1.style.height = Math.floor(this.pos - this.p1pos.top - lh / 2) + 'px';
70       this.p2.style.top = Math.ceil(this.pos + lh / 2) + 'px';
71       this.layer.move(this.layer.x, Math.round(this.pos - lh / 2 + 1));
72       if (bw.ie) {
73         var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
74         this.p2.style.height = (new_height > 0 ? new_height : 0) + 'px';
75       }
76     }
77     else {
78       this.p1.style.width = Math.floor(this.pos - this.p1pos.left - this.layer.width / 2) + 'px';
79       this.p2.style.left = Math.ceil(this.pos + this.layer.width / 2) + 'px';
80       this.layer.move(Math.round(this.pos - this.layer.width / 2 + 1), this.layer.y);
81       if (bw.ie) {
82         var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10) ;
83         this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
84       }
85     }
86     $(this.p2).resize();
87     $(this.p1).resize();
88   };
89
90   /**
91    * Handler for mousedown events
92    */
93   this.onDragStart = function(e)
94   {
95     // disable text selection while dragging the splitter
96     if (bw.konq || bw.chrome || bw.safari)
97       document.body.style.webkitUserSelect = 'none';
98
99     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
100     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
101     this.drag_active = true;
102
103     // start listening to mousemove events
104     rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
105     rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
106
107     // enable dragging above iframes
108     $('iframe').each(function() {
109       $('<div class="iframe-splitter-fix"></div>')
110         .css({background: '#fff',
111           width: this.offsetWidth+'px', height: this.offsetHeight+'px',
112           position: 'absolute', opacity: '0.001', zIndex: 1000
113         })
114         .css($(this).offset())
115         .appendTo('body');
116       });
117   };
118
119   /**
120    * Handler for mousemove events
121    */
122   this.onDrag = function(e)
123   {
124     if (!this.drag_active)
125       return false;
126
127     var pos = rcube_event.get_mouse_pos(e);
128
129     if (this.relative) {
130       var parent = $(this.p1.parentNode).offset();
131       pos.x -= parent.left;
132       pos.y -= parent.top;
133     }
134
135     if (this.horizontal) {
136       if (((pos.y - this.layer.height * 1.5) > this.p1pos.top) && ((pos.y + this.layer.height * 1.5) < (this.p2pos.top + this.p2.offsetHeight))) {
137         this.pos = pos.y;
138         this.resize();
139       }
140     }
141     else {
142       if (((pos.x - this.layer.width * 1.5) > this.p1pos.left) && ((pos.x + this.layer.width * 1.5) < (this.p2pos.left + this.p2.offsetWidth))) {
143         this.pos = pos.x;
144         this.resize();
145       }
146     }
147
148     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
149     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
150     return false;
151   };
152
153   /**
154    * Handler for mouseup events
155    */
156   this.onDragStop = function(e)
157   {
158     // resume the ability to highlight text
159     if (bw.konq || bw.chrome || bw.safari)
160       document.body.style.webkitUserSelect = 'auto';
161
162     // cancel the listening for drag events
163     rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
164     rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
165     this.drag_active = false;
166
167     // remove temp divs
168     $('div.iframe-splitter-fix').each(function() { this.parentNode.removeChild(this); });
169
170     this.set_cookie();
171
172     if (typeof this.callback == 'function')
173       this.callback(this);
174
175     return bw.safari ? true : rcube_event.cancel(e);
176   };
177
178   /**
179    * Handler for window resize events
180    */
181   this.onResize = function(e)
182   {
183     if (this.horizontal) {
184       var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
185       this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
186     }
187     else {
188       var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10);
189       this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
190     }
191   };
192
193   /**
194    * Saves splitter position in cookie
195    */
196   this.set_cookie = function()
197   {
198     var exp = new Date();
199     exp.setYear(exp.getFullYear() + 1);
200     bw.set_cookie(this.id, this.pos, exp);
201   };
202
203 } // end class rcube_splitter