alecpl
2010-05-18 0e7b66357de62abad98dbd8b1c06ac5de3d4575c
program/js/list.js
@@ -33,6 +33,7 @@
  this.rows = [];
  this.selection = [];
  this.rowcount = 0;
  this.colcount = 0;
  this.subject_col = -1;
  this.shiftkey = false;
@@ -40,11 +41,14 @@
  this.multiexpand = false;
  this.multi_selecting = false;
  this.draggable = false;
  this.column_movable = false;
  this.keyboard = false;
  this.toggleselect = false;
  this.dont_select = false;
  this.drag_active = false;
  this.col_drag_active = false;
  this.column_fixed = null;
  this.last_selected = 0;
  this.shift_start = 0;
  this.in_selection_before = false;
@@ -72,18 +76,14 @@
    this.rows = [];
    this.rowcount = 0;
    var row;
    for(var r=0; r<this.list.tBodies[0].childNodes.length; r++) {
      row = this.list.tBodies[0].childNodes[r];
      while (row && row.nodeType != 1) {
        row = row.nextSibling;
        r++;
      }
    var r, len, rows = this.list.tBodies[0].rows;
      this.init_row(row);
    for (r=0, len=rows.length; r<len; r++) {
      this.init_row(rows[r]);
      this.rowcount++;
    }
    this.init_header();
    this.frame = this.list.parentNode;
    // set body events
@@ -120,6 +120,29 @@
/**
 * Init list column headers and set mouse events on them
 */
init_header: function()
{
  if (this.list && this.list.tHead) {
    this.colcount = 0;
    var col, r, p = this;
    // add events for list columns moving
    if (this.column_movable && this.list.tHead && this.list.tHead.rows) {
      for (r=0; r<this.list.tHead.rows[0].cells.length; r++) {
        if (this.column_fixed == r)
          continue;
        col = this.list.tHead.rows[0].cells[r];
        col.onmousedown = function(e){ return p.drag_column(e, this); };
        this.colcount++;
      }
    }
  }
},
/**
 * Remove all list rows
 */
clear: function(sel)
@@ -146,7 +169,7 @@
  if (sel_next)
    this.select_next();
  this.rows[uid] = null;
  delete this.rows[uid];
  this.rowcount--;
},
@@ -156,10 +179,7 @@
 */
insert_row: function(row, attop)
{
  if (this.background)
    var tbody = this.background;
  else
    var tbody = this.list.tBodies[0];
  var tbody = this.list.tBodies[0];
  if (attop && tbody.rows.length)
    tbody.insertBefore(row, tbody.firstChild);
@@ -177,8 +197,9 @@
 */
focus: function(e)
{
  var id;
  this.focused = true;
  for (var n=0; n<this.selection.length; n++) {
  for (var n in this.selection) {
    id = this.selection[n];
    if (this.rows[id] && this.rows[id].obj) {
      $(this.rows[id].obj).addClass('selected').removeClass('unfocused');
@@ -197,12 +218,40 @@
{
  var id;
  this.focused = false;
  for (var n=0; n<this.selection.length; n++) {
  for (var n in this.selection) {
    id = this.selection[n];
    if (this.rows[id] && this.rows[id].obj) {
      $(this.rows[id].obj).removeClass('selected').addClass('unfocused');
    }
  }
},
/**
 * onmousedown-handler of message list column
 */
drag_column: function(e, col)
{
  if (this.colcount > 1) {
    this.drag_start = true;
    this.drag_mouse_start = rcube_event.get_mouse_pos(e);
    rcube_event.add_listener({event:'mousemove', object:this, method:'column_drag_mouse_move'});
    rcube_event.add_listener({event:'mouseup', object:this, method:'column_drag_mouse_up'});
    // enable dragging over iframes
    this.add_dragfix();
    // find selected column number
    for (var i=0; i<this.list.tHead.rows[0].cells.length; i++) {
      if (col == this.list.tHead.rows[0].cells[i]) {
        this.selected_column = i;
        break;
      }
    }
  }
  return false;
},
@@ -236,15 +285,7 @@
    rcube_event.add_listener({event:'mouseup', object:this, method:'drag_mouse_up'});
    // enable dragging over iframes
    $('iframe').each(function() {
      $('<div class="iframe-dragdrop-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');
    });
    this.add_dragfix();
  }
  return false;
@@ -287,7 +328,7 @@
  if (!this.drag_active) {
    // remove temp divs
    $('div.iframe-dragdrop-fix').each(function() { this.parentNode.removeChild(this); });
    this.del_dragfix();
    rcube_event.cancel(e);
  }
@@ -442,7 +483,8 @@
          $(new_row).hide();
        if (r.has_children) {
          r.expanded = false;
          this.update_expando(r.uid);
          if (!r.depth || !this.multiexpand)
            this.update_expando(r.uid, false);
          this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded });
        }
      }
@@ -481,7 +523,12 @@
        $(new_row).show();
        if (r.has_children) {
          r.expanded = true;
          this.update_expando(r.uid, true);
          // in multiexpand mode only root has expando icon
          // so we don't need to set it for children, this
          // improves performance because getElementById()
          // is relatively slow on IE
          if (!r.depth || !this.multiexpand)
            this.update_expando(r.uid, true);
          this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded });
        }
      }
@@ -531,12 +578,11 @@
get_first_row: function()
{
  if (this.rowcount) {
    var rows = this.list.tBodies[0].rows;
    var i, len, rows = this.list.tBodies[0].rows;
    for(var i=0; i<rows.length-1; i++)
      if(rows[i].id && String(rows[i].id).match(/rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
   return RegExp.$1;
    for (i=0, len=rows.length-1; i<len; i++)
      if (rows[i].id && String(rows[i].id).match(/rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
       return RegExp.$1;
  }
  return null;
@@ -545,12 +591,11 @@
get_last_row: function()
{
  if (this.rowcount) {
    var rows = this.list.tBodies[0].rows;
    var i, rows = this.list.tBodies[0].rows;
    for(var i=rows.length-1; i>=0; i--)
      if(rows[i].id && String(rows[i].id).match(/rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
   return RegExp.$1;
    for (i=rows.length-1; i>=0; i--)
      if (rows[i].id && String(rows[i].id).match(/rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
        return RegExp.$1;
  }
  return null;
@@ -706,15 +751,14 @@
  if (!this.rows[this.shift_start] || !this.selection.length)
    this.shift_start = id;
  var from_rowIndex = this.rows[this.shift_start].obj.rowIndex;
  var to_rowIndex = this.rows[id].obj.rowIndex;
  var i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex);
  var j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
  var from_rowIndex = this.rows[this.shift_start].obj.rowIndex,
    to_rowIndex = this.rows[id].obj.rowIndex,
    i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex),
    j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
  // iterate through the entire message list
  for (var n in this.rows) {
    if ((this.rows[n].obj.rowIndex >= i) && (this.rows[n].obj.rowIndex <= j)) {
    if (this.rows[n].obj.rowIndex >= i && this.rows[n].obj.rowIndex <= j) {
      if (!this.in_selection(n)) {
        this.highlight_row(n, true);
      }
@@ -754,11 +798,11 @@
  this.selection = [];
  for (var n in this.rows) {
    if (!filter || (this.rows[n] && this.rows[n][filter] == true)) {
    if (!filter || this.rows[n][filter] == true) {
      this.last_selected = n;
      this.highlight_row(n, true);
    }
    else if (this.rows[n]) {
    else {
      $(this.rows[n].obj).removeClass('selected').removeClass('unfocused');
    }
  }
@@ -806,7 +850,7 @@
  // one row
  if (id) {
    for (var n=0; n<this.selection.length; n++)
    for (var n=0 in this.selection)
      if (this.selection[n] == id) {
        this.selection.splice(n,1);
        break;
@@ -814,7 +858,7 @@
  }
  // all rows
  else {
    for (var n=0; n<this.selection.length; n++)
    for (var n in this.selection)
      if (this.rows[this.selection[n]]) {
        $(this.rows[this.selection[n]].obj).removeClass('selected').removeClass('unfocused');
      }
@@ -931,7 +975,11 @@
  switch (rcube_event.get_keycode(e)) {
    case 27:
      if (this.drag_active)
   return this.drag_mouse_up(e);
       return this.drag_mouse_up(e);
      if (this.col_drag_active) {
        this.selected_column = null;
       return this.column_drag_mouse_up(e);
      }
    case 40:
    case 38: 
@@ -995,9 +1043,7 @@
    else
      this.collapse(selected_row);
  var expando = document.getElementById('rcmexpando' + selected_row.uid);
  if (expando)
    expando.className = selected_row.expanded?'expanded':'collapsed';
  this.update_expando(selected_row.uid, selected_row.expanded);
  return false;
},
@@ -1040,12 +1086,13 @@
      return false;
    if (!this.draglayer)
      this.draglayer = $('<div>').attr('id', 'rcmdraglayer').css({ position:'absolute', display:'none', 'z-index':2000 }).appendTo(document.body);
      this.draglayer = $('<div>').attr('id', 'rcmdraglayer')
        .css({ position:'absolute', display:'none', 'z-index':2000 })
        .appendTo(document.body);
    // also select childs of (collapsed) threads for dragging
    var selection = $.merge([], this.selection);
    var depth, row, uid, r;
    for (var n=0; n < selection.length; n++) {
    var n, uid, selection = $.merge([], this.selection);
    for (n in selection) {
      uid = selection[n];
      if (this.rows[uid].has_children && !this.rows[uid].expanded)
        this.select_childs(uid);
@@ -1054,7 +1101,7 @@
    // get subjects of selected messages
    var names = '';
    var c, i, subject, obj;
    for(var n=0; n<this.selection.length; n++) {
    for (var n=0; n<this.selection.length; n++) {
      // only show 12 lines
      if (n>12) {
        names += '...';
@@ -1134,7 +1181,7 @@
  rcube_event.remove_listener({event:'mouseup', object:this, method:'drag_mouse_up'});
  // remove temp divs
  $('div.iframe-dragdrop-fix').each(function() { this.parentNode.removeChild(this); });
  this.del_dragfix();
  this.triggerEvent('dragend');
@@ -1143,16 +1190,181 @@
/**
 * Creating the list in background
 * Handler for mouse move events for dragging list column
 */
set_background_mode: function(flag)
column_drag_mouse_move: function(e)
{
  if (flag) {
    this.background = document.createElement('tbody');
  } else if (this.background) {
    this.list.replaceChild(this.background, this.list.tBodies[0]);
    this.background = null;
  if (this.drag_start) {
    // check mouse movement, of less than 3 pixels, don't start dragging
    var i, m = rcube_event.get_mouse_pos(e);
    if (!this.drag_mouse_start || (Math.abs(m.x - this.drag_mouse_start.x) < 3 && Math.abs(m.y - this.drag_mouse_start.y) < 3))
      return false;
    if (!this.col_draglayer) {
      var lpos = $(this.list).offset(),
        cells = this.list.tHead.rows[0].cells;
      // create dragging layer
      this.col_draglayer = $('<div>').attr('id', 'rcmcoldraglayer')
        .css(lpos).css({ position:'absolute', 'z-index':2001,
           'background-color':'white', opacity:0.75,
           height: (this.frame.offsetHeight-2)+'px', width: (this.frame.offsetWidth-2)+'px' })
        .appendTo(document.body)
        // ... and column position indicator
       .append($('<div>').attr('id', 'rcmcolumnindicator')
          .css({ position:'absolute', 'border-right':'2px dotted #555',
          'z-index':2002, height: (this.frame.offsetHeight-2)+'px' }));
      this.cols = [];
      this.list_pos = this.list_min_pos = lpos.left;
      // save columns positions
      for (i=0; i<cells.length; i++) {
        this.cols[i] = cells[i].offsetWidth;
        if (this.column_fixed !== null && i <= this.column_fixed) {
          this.list_min_pos += this.cols[i];
        }
      }
    }
    this.col_draglayer.show();
    this.col_drag_active = true;
    this.triggerEvent('column_dragstart');
  }
  // set column indicator position
  if (this.col_drag_active && this.col_draglayer) {
    var i, cpos = 0, pos = rcube_event.get_mouse_pos(e);
    for (i=0; i<this.cols.length; i++) {
      if (pos.x >= this.cols[i]/2 + this.list_pos + cpos)
        cpos += this.cols[i];
      else
        break;
    }
    // handle fixed columns on left
    if (i == 0 && this.list_min_pos > pos.x)
      cpos = this.list_min_pos - this.list_pos;
    // empty list needs some assignment
    else if (!this.list.rowcount && i == this.cols.length)
      cpos -= 2;
    $('#rcmcolumnindicator').css({ width: cpos+'px'});
    this.triggerEvent('column_dragmove', e?e:window.event);
  }
  this.drag_start = false;
  return false;
},
/**
 * Handler for mouse up events for dragging list columns
 */
column_drag_mouse_up: function(e)
{
  document.onmousemove = null;
  if (this.col_draglayer) {
    (this.col_draglayer).remove();
    this.col_draglayer = null;
  }
  if (this.col_drag_active)
    this.focus();
  this.col_drag_active = false;
  rcube_event.remove_listener({event:'mousemove', object:this, method:'column_drag_mouse_move'});
  rcube_event.remove_listener({event:'mouseup', object:this, method:'column_drag_mouse_up'});
  // remove temp divs
  this.del_dragfix();
  if (this.selected_column !== null && this.cols && this.cols.length) {
    var i, cpos = 0, pos = rcube_event.get_mouse_pos(e);
    // find destination position
    for (i=0; i<this.cols.length; i++) {
      if (pos.x >= this.cols[i]/2 + this.list_pos + cpos)
        cpos += this.cols[i];
      else
        break;
    }
    if (i != this.selected_column && i != this.selected_column+1) {
      this.column_replace(this.selected_column, i);
    }
  }
  this.triggerEvent('column_dragend');
  return rcube_event.cancel(e);
},
/**
 * Creates a layer for drag&drop over iframes
 */
add_dragfix: function()
{
  $('iframe').each(function() {
    $('<div class="iframe-dragdrop-fix"></div>')
      .css({background: '#fff',
        width: this.offsetWidth+'px', height: this.offsetHeight+'px',
        position: 'absolute', opacity: '0.001', zIndex: 1000
      })
      .css($(this).offset())
      .appendTo(document.body);
  });
},
/**
 * Removes the layer for drag&drop over iframes
 */
del_dragfix: function()
{
  $('div.iframe-dragdrop-fix').each(function() { this.parentNode.removeChild(this); });
},
/**
 * Replaces two columns
 */
column_replace: function(from, to)
{
  var cells = this.list.tHead.rows[0].cells,
    elem = cells[from],
    before = cells[to],
    td = document.createElement('td');
  // replace header cells
  if (before)
    cells[0].parentNode.insertBefore(td, before);
  else
    cells[0].parentNode.appendChild(td);
  cells[0].parentNode.replaceChild(elem, td);
  // replace list cells
  for (r=0; r<this.list.tBodies[0].rows.length; r++) {
    row = this.list.tBodies[0].rows[r];
    elem = row.cells[from];
    before = row.cells[to];
    td = document.createElement('td');
    if (before)
      row.insertBefore(td, before);
    else
      row.appendChild(td);
    row.replaceChild(elem, td);
  }
  // update subject column position
  if (this.subject_col == from)
    this.subject_col = to > from ? to - 1 : to;
  this.triggerEvent('column_replace');
}
};