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