Thomas Bruederli
2012-10-15 27be4e7644637255d7adbf5386ec883d9c2c64cd
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;
9e1daa 18   this.callback = attrib.callback;
a7d5c6 19
T 20   this.init = function()
ed60fe 21   {
a7d5c6 22     this.p1 = document.getElementById(this.p1id);
T 23     this.p2 = document.getElementById(this.p2id);
e5686f 24
a7d5c6 25     // create and position the handle for this splitter
cc97ea 26     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 27     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
935d1a 28
ed60fe 29     if (this.horizontal) {
cc97ea 30       var top = this.p1pos.top + this.p1.offsetHeight;
e5686f 31       this.layer = new rcube_layer(this.id, {x: 0, y: top, height: 10, 
A 32             width: '100%', vis: 1, parent: this.p1.parentNode});
ed60fe 33     }
A 34     else {
cc97ea 35       var left = this.p1pos.left + this.p1.offsetWidth;
e5686f 36       this.layer = new rcube_layer(this.id, {x: left, y: 0, width: 10, 
A 37             height: '100%', vis: 1,  parent: this.p1.parentNode});
ed60fe 38     }
a7d5c6 39
T 40     this.elm = this.layer.elm;
41     this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
e5686f 42     this.elm.unselectable = 'on';
a7d5c6 43
T 44     // add the mouse event listeners
45     rcube_event.add_listener({element: this.elm, event:'mousedown', object:this, method:'onDragStart'});
e5686f 46     if (bw.ie)
A 47       rcube_event.add_listener({element: window, event:'resize', object:this, method:'onResize'});
a7d5c6 48
7c2d30 49     // read saved position from cookie
ae7027 50     var cookie = rcmail.get_cookie(this.id);
ed60fe 51     if (cookie && !isNaN(cookie)) {
80c678 52       this.pos = parseFloat(cookie);
a7d5c6 53       this.resize();
ed60fe 54     }
A 55     else if (this.pos) {
e5686f 56       this.resize();
A 57       this.set_cookie();
ed60fe 58     }
A 59   };
a7d5c6 60
T 61   /**
62    * Set size and position of all DOM objects
63    * according to the saved splitter position
64    */
65   this.resize = function()
ed60fe 66   {
A 67     if (this.horizontal) {
e5686f 68       var lh = this.layer.height - this.offset * 2;
cc97ea 69       this.p1.style.height = Math.floor(this.pos - this.p1pos.top - lh / 2) + 'px';
e5686f 70       this.p2.style.top = Math.ceil(this.pos + lh / 2) + 'px';
cc97ea 71       this.layer.move(this.layer.x, Math.round(this.pos - lh / 2 + 1));
ed60fe 72       if (bw.ie) {
a4865a 73         var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
ed60fe 74         this.p2.style.height = (new_height > 0 ? new_height : 0) + 'px';
a7d5c6 75       }
ed60fe 76     }
A 77     else {
cc97ea 78       this.p1.style.width = Math.floor(this.pos - this.p1pos.left - this.layer.width / 2) + 'px';
e5686f 79       this.p2.style.left = Math.ceil(this.pos + this.layer.width / 2) + 'px';
a7d5c6 80       this.layer.move(Math.round(this.pos - this.layer.width / 2 + 1), this.layer.y);
ed60fe 81       if (bw.ie) {
A 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';
a7d5c6 84       }
ed60fe 85     }
087c7d 86     $(this.p2).resize();
A 87     $(this.p1).resize();
ed60fe 88   };
a7d5c6 89
T 90   /**
91    * Handler for mousedown events
92    */
93   this.onDragStart = function(e)
ed60fe 94   {
f9aeec 95     // disable text selection while dragging the splitter
935d1a 96     if (bw.konq || bw.chrome || bw.safari)
f9aeec 97       document.body.style.webkitUserSelect = 'none';
A 98
cc97ea 99     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 100     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
e5686f 101     this.drag_active = true;
f9aeec 102
a7d5c6 103     // start listening to mousemove events
T 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
ed60fe 107     // enable dragging above iframes
A 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   };
a7d5c6 118
T 119   /**
120    * Handler for mousemove events
121    */
122   this.onDrag = function(e)
ed60fe 123   {
A 124     if (!this.drag_active)
125       return false;
e5686f 126
05b9a2 127     var pos = rcube_event.get_mouse_pos(e);
e5686f 128
ed60fe 129     if (this.relative) {
cc97ea 130       var parent = $(this.p1.parentNode).offset();
T 131       pos.x -= parent.left;
132       pos.y -= parent.top;
ed60fe 133     }
a7d5c6 134
ed60fe 135     if (this.horizontal) {
A 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))) {
a7d5c6 137         this.pos = pos.y;
T 138         this.resize();
139       }
ed60fe 140     }
A 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))) {
a7d5c6 143         this.pos = pos.x;
T 144         this.resize();
145       }
ed60fe 146     }
a7d5c6 147
cc97ea 148     this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
T 149     this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
a7d5c6 150     return false;
ed60fe 151   };
a7d5c6 152
T 153   /**
154    * Handler for mouseup events
155    */
156   this.onDragStop = function(e)
ed60fe 157   {
f9aeec 158     // resume the ability to highlight text
935d1a 159     if (bw.konq || bw.chrome || bw.safari)
f9aeec 160       document.body.style.webkitUserSelect = 'auto';
A 161
a7d5c6 162     // cancel the listening for drag events
T 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'});
e5686f 165     this.drag_active = false;
A 166
ed60fe 167     // remove temp divs
A 168     $('div.iframe-splitter-fix').each(function() { this.parentNode.removeChild(this); });
a7d5c6 169
e5686f 170     this.set_cookie();
7c2d30 171
9e1daa 172     if (typeof this.callback == 'function')
T 173       this.callback(this);
174
a7d5c6 175     return bw.safari ? true : rcube_event.cancel(e);
ed60fe 176   };
05b9a2 177
a7d5c6 178   /**
T 179    * Handler for window resize events
180    */
181   this.onResize = function(e)
ed60fe 182   {
A 183     if (this.horizontal) {
a4865a 184       var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
05b9a2 185       this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
ed60fe 186     }
A 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   };
a7d5c6 192
ed60fe 193   /**
A 194    * Saves splitter position in cookie
195    */
e5686f 196   this.set_cookie = function()
ed60fe 197   {
e5686f 198     var exp = new Date();
A 199     exp.setYear(exp.getFullYear() + 1);
ae7027 200     rcmail.set_cookie(this.id, this.pos, exp);
ed60fe 201   };
e5686f 202
ed60fe 203 } // end class rcube_splitter