thomascube
2010-12-17 db1a87cd6c506f2afbd1a37c64cb56ae11120b49
commit | author | age
a7d5c6 1
T 2 /**
e019f2 3  * Roundcube splitter GUI class
a7d5c6 4  *
T 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();
db1a87 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     }
087c7d 85     $(this.p2).resize();
A 86     $(this.p1).resize();
ed60fe 87   };
a7d5c6 88
T 89   /**
90    * Handler for mousedown events
91    */
92   this.onDragStart = function(e)
ed60fe 93   {
f9aeec 94     // disable text selection while dragging the splitter
db1a87 95     if (bw.konq || bw.chrome || bw.safari)
f9aeec 96       document.body.style.webkitUserSelect = 'none';
A 97
cc97ea 98     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 99     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
e5686f 100     this.drag_active = true;
f9aeec 101
a7d5c6 102     // start listening to mousemove events
T 103     rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
104     rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
105
ed60fe 106     // enable dragging above iframes
A 107     $('iframe').each(function() {
108       $('<div class="iframe-splitter-fix"></div>')
109         .css({background: '#fff',
110           width: this.offsetWidth+'px', height: this.offsetHeight+'px',
111           position: 'absolute', opacity: '0.001', zIndex: 1000
112         })
113         .css($(this).offset())
114         .appendTo('body');
115       });
116   };
a7d5c6 117
T 118   /**
119    * Handler for mousemove events
120    */
121   this.onDrag = function(e)
ed60fe 122   {
A 123     if (!this.drag_active)
124       return false;
e5686f 125
05b9a2 126     var pos = rcube_event.get_mouse_pos(e);
e5686f 127
ed60fe 128     if (this.relative) {
cc97ea 129       var parent = $(this.p1.parentNode).offset();
T 130       pos.x -= parent.left;
131       pos.y -= parent.top;
ed60fe 132     }
a7d5c6 133
ed60fe 134     if (this.horizontal) {
A 135       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 136         this.pos = pos.y;
T 137         this.resize();
138       }
ed60fe 139     }
A 140     else {
141       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 142         this.pos = pos.x;
T 143         this.resize();
144       }
ed60fe 145     }
a7d5c6 146
cc97ea 147     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 148     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
a7d5c6 149     return false;
ed60fe 150   };
a7d5c6 151
T 152   /**
153    * Handler for mouseup events
154    */
155   this.onDragStop = function(e)
ed60fe 156   {
f9aeec 157     // resume the ability to highlight text
db1a87 158     if (bw.konq || bw.chrome || bw.safari)
f9aeec 159       document.body.style.webkitUserSelect = 'auto';
A 160
a7d5c6 161     // cancel the listening for drag events
T 162     rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
163     rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
e5686f 164     this.drag_active = false;
A 165
ed60fe 166     // remove temp divs
A 167     $('div.iframe-splitter-fix').each(function() { this.parentNode.removeChild(this); });
a7d5c6 168
e5686f 169     this.set_cookie();
7c2d30 170
a7d5c6 171     return bw.safari ? true : rcube_event.cancel(e);
ed60fe 172   };
05b9a2 173
a7d5c6 174   /**
T 175    * Handler for window resize events
176    */
177   this.onResize = function(e)
ed60fe 178   {
A 179     if (this.horizontal) {
a4865a 180       var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
05b9a2 181       this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
ed60fe 182     }
A 183     else {
184       var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10);
185       this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
186     }
187   };
a7d5c6 188
ed60fe 189   /**
A 190    * Saves splitter position in cookie
191    */
e5686f 192   this.set_cookie = function()
ed60fe 193   {
e5686f 194     var exp = new Date();
A 195     exp.setYear(exp.getFullYear() + 1);
80c678 196     bw.set_cookie(this.id, this.pos, exp);
ed60fe 197   };
e5686f 198
ed60fe 199 } // end class rcube_splitter