alecpl
2010-04-13 ed60fef27c3a37dfdf94db8c1a24f2e25948676f
- Code cleanup + fixed IE7,8 error when moving splitter out of the screen
- Improved iframes handling (#1486610)


1 files modified
180 ■■■■■ changed files
skins/default/splitter.js 180 ●●●●● patch | view | raw | blame | history
skins/default/splitter.js
@@ -5,7 +5,7 @@
 * @constructor
 */
function rcube_splitter(attrib)
  {
{
  this.p1id = attrib.p1;
  this.p2id = attrib.p2;
  this.id = attrib.id ? attrib.id : this.p1id + '_' + this.p2id + '_splitter';
@@ -17,7 +17,7 @@
  this.drag_active = false;
  this.init = function()
    {
  {
    this.p1 = document.getElementById(this.p1id);
    this.p2 = document.getElementById(this.p2id);
@@ -25,18 +25,16 @@
    this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
    this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
    
    if (this.horizontal)
      {
    if (this.horizontal) {
      var top = this.p1pos.top + this.p1.offsetHeight;
      this.layer = new rcube_layer(this.id, {x: 0, y: top, height: 10, 
            width: '100%', vis: 1, parent: this.p1.parentNode});
      }
    else
      {
    }
    else {
      var left = this.p1pos.left + this.p1.offsetWidth;
      this.layer = new rcube_layer(this.id, {x: left, y: 0, width: 10, 
            height: '100%', vis: 1,  parent: this.p1.parentNode});
      }
    }
    this.elm = this.layer.elm;
    this.elm.className = 'splitter '+(this.horizontal ? 'splitter-h' : 'splitter-v');
@@ -49,51 +47,48 @@
    // read saved position from cookie
    var cookie = bw.get_cookie(this.id);
    if (cookie && !isNaN(cookie))
      {
    if (cookie && !isNaN(cookie)) {
      this.pos = parseFloat(cookie);
      this.resize();
      }
    else if (this.pos)
      {
    }
    else if (this.pos) {
      this.resize();
      this.set_cookie();
      }
    };
    }
  };
  /**
   * Set size and position of all DOM objects
   * according to the saved splitter position
   */
  this.resize = function()
    {
    if (this.horizontal)
      {
  {
    if (this.horizontal) {
      var lh = this.layer.height - this.offset * 2;
      this.p1.style.height = Math.floor(this.pos - this.p1pos.top - lh / 2) + 'px';
      this.p2.style.top = Math.ceil(this.pos + lh / 2) + 'px';
      this.layer.move(this.layer.x, Math.round(this.pos - lh / 2 + 1));
      if (bw.ie)
        {
      if (bw.ie) {
        var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
        this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
        }
        this.p2.style.height = (new_height > 0 ? new_height : 0) + 'px';
      }
    else
      {
    }
    else {
      this.p1.style.width = Math.floor(this.pos - this.p1pos.left - this.layer.width / 2) + 'px';
      this.p2.style.left = Math.ceil(this.pos + this.layer.width / 2) + 'px';
      this.layer.move(Math.round(this.pos - this.layer.width / 2 + 1), this.layer.y);
      if (bw.ie)
        this.p2.style.width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10) + 'px';
      if (bw.ie) {
        var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10) ;
        this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
      }
    };
    }
  };
  /**
   * Handler for mousedown events
   */
  this.onDragStart = function(e)
    {
  {
    // disable text selection while dragging the splitter
    if (window.webkit || bw.safari)
      document.body.style.webkitUserSelect = 'none';
@@ -106,83 +101,59 @@
    rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'onDrag'});
    rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
    // need to listen in any iframe documents too, b/c otherwise the splitter stops moving when we move over an iframe
    var iframes = document.getElementsByTagName('iframe');
    this.iframe_events = Object();
    for (var n in iframes)
      {
      var iframedoc = null;
      if (iframes[n].contentDocument)
        iframedoc = iframes[n].contentDocument;
      else if (iframes[n].contentWindow)
        iframedoc = iframes[n].contentWindow.document;
      else if (iframes[n].document)
        iframedoc = iframes[n].document;
      if (iframedoc)
        {
        // I don't use the add_listener function for this one because I need to create closures to fetch
        // the position of each iframe when the event is received
        var s = this;
        var id = '#'+iframes[n].id;
        this.iframe_events[n] = function(e){ e._offset = $(id).offset(); return s.onDrag(e); };
        if (iframedoc.addEventListener)
          iframedoc.addEventListener('mousemove', this.iframe_events[n], false);
        else if (iframes[n].attachEvent)
          iframedoc.attachEvent('onmousemove', this.iframe_events[n]);
        else
          iframedoc.onmousemove = this.iframe_events[n];
        rcube_event.add_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
        }
      }
    }
    // enable dragging above iframes
    $('iframe').each(function() {
      $('<div class="iframe-splitter-fix"></div>')
        .css({background: '#fff',
          width: this.offsetWidth+'px', height: this.offsetHeight+'px',
          position: 'absolute', opacity: '0.001', zIndex: 1000
        })
        .css($(this).offset())
        .appendTo('body');
      });
  };
  /**
   * Handler for mousemove events
   */
  this.onDrag = function(e)
    {
    if (!this.drag_active) return false;
  {
    if (!this.drag_active)
      return false;
    var pos = rcube_event.get_mouse_pos(e);
    if (this.relative)
      {
    if (this.relative) {
      var parent = $(this.p1.parentNode).offset();
      pos.x -= parent.left;
      pos.y -= parent.top;
      }
    }
    if (this.horizontal)
      {
      if (((pos.y - this.layer.height * 1.5) > this.p1pos.top) && ((pos.y + this.layer.height * 1.5) < (this.p2pos.top + this.p2.offsetHeight)))
        {
    if (this.horizontal) {
      if (((pos.y - this.layer.height * 1.5) > this.p1pos.top) && ((pos.y + this.layer.height * 1.5) < (this.p2pos.top + this.p2.offsetHeight))) {
        this.pos = pos.y;
        this.resize();
        }
      }
    else
      {
      if (((pos.x - this.layer.width * 1.5) > this.p1pos.left) && ((pos.x + this.layer.width * 1.5) < (this.p2pos.left + this.p2.offsetWidth)))
        {
    }
    else {
      if (((pos.x - this.layer.width * 1.5) > this.p1pos.left) && ((pos.x + this.layer.width * 1.5) < (this.p2pos.left + this.p2.offsetWidth))) {
        this.pos = pos.x;
        this.resize();
        }
      }
    }
    this.p1pos = this.relative ? $(this.p1).position() : $(this.p1).offset();
    this.p2pos = this.relative ? $(this.p2).position() : $(this.p2).offset();
    return false;
    };
  };
  /**
   * Handler for mouseup events
   */
  this.onDragStop = function(e)
    {
  {
    // resume the ability to highlight text
    if(window.webkit || bw.safari)
    if (window.webkit || bw.safari)
      document.body.style.webkitUserSelect = 'auto';
    // cancel the listening for drag events
@@ -190,58 +161,37 @@
    rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'onDragStop'});
    this.drag_active = false;
    var iframes = document.getElementsByTagName('iframe');
    for (var n in iframes)
      {
      var iframedoc;
      if (iframes[n].contentDocument)
        iframedoc = iframes[n].contentDocument;
      else if (iframes[n].contentWindow)
        iframedoc = iframes[n].contentWindow.document;
      else if (iframes[n].document)
        iframedoc = iframes[n].document;
      if (iframedoc)
        {
        if (this.iframe_events[n]) {
          if (iframedoc.removeEventListener)
            iframedoc.removeEventListener('mousemove', this.iframe_events[n], false);
          else if (iframedoc.detachEvent)
            iframedoc.detachEvent('onmousemove', this.iframe_events[n]);
          else
            iframedoc.onmousemove = null;
          }
        rcube_event.remove_listener({element:iframedoc, event:'mouseup', object:this, method:'onDragStop'});
        }
      }
    // remove temp divs
    $('div.iframe-splitter-fix').each(function() { this.parentNode.removeChild(this); });
    this.set_cookie();
    return bw.safari ? true : rcube_event.cancel(e);
    };
  };
  /**
   * Handler for window resize events
   */
  this.onResize = function(e)
    {
    if (this.horizontal)
      {
  {
    if (this.horizontal) {
      var new_height = parseInt(this.p2.parentNode.offsetHeight, 10) - parseInt(this.p2.style.top, 10) - (bw.ie8 ? 2 : 0);
      this.p2.style.height = (new_height > 0 ? new_height : 0) +'px';
      }
    else
      this.p2.style.width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10) + 'px';
    };
    }
    else {
      var new_width = parseInt(this.p2.parentNode.offsetWidth, 10) - parseInt(this.p2.style.left, 10);
      this.p2.style.width = (new_width > 0 ? new_width : 0) + 'px';
    }
  };
  /**
   * Saves splitter position in cookie
   */
  this.set_cookie = function()
    {
    // save state in cookie
  {
    var exp = new Date();
    exp.setYear(exp.getFullYear() + 1);
    bw.set_cookie(this.id, this.pos, exp);
    };
  };
  }  // end class rcube_splitter
} // end class rcube_splitter