till
2009-01-16 cbbef379a5ff8c48a65b6b80297a404e1aaf0613
program/js/app.js
@@ -3,7 +3,7 @@
 | RoundCube Webmail Client Script                                       |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2005-2008, RoundCube Dev, - Switzerland                 |
 | Copyright (C) 2005-2009, RoundCube Dev, - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 +-----------------------------------------------------------------------+
@@ -133,7 +133,7 @@
          this.message_list.addEventListener('dblclick', function(o){ p.msglist_dbl_click(o); });
          this.message_list.addEventListener('keypress', function(o){ p.msglist_keypress(o); });
          this.message_list.addEventListener('select', function(o){ p.msglist_select(o); });
          this.message_list.addEventListener('dragstart', function(o){ p.drag_active = true; if (p.preview_timer) clearTimeout(p.preview_timer); });
          this.message_list.addEventListener('dragstart', function(o){ p.drag_start(o); });
          this.message_list.addEventListener('dragmove', function(o, e){ p.drag_move(e); });
          this.message_list.addEventListener('dragend', function(o){ p.drag_active = false; });
@@ -202,6 +202,8 @@
            }
          if (this.env.drafts_mailbox)
            this.enable_command('savedraft', true);
          document.onmouseup = function(e){ return p.doc_mouse_up(e); };
          }
        if (this.env.messagecount)
@@ -247,7 +249,7 @@
          this.contact_list = new rcube_list_widget(this.gui_objects.contactslist, {multiselect:true, draggable:true, keyboard:true});
          this.contact_list.addEventListener('keypress', function(o){ p.contactlist_keypress(o); });
          this.contact_list.addEventListener('select', function(o){ p.contactlist_select(o); });
          this.contact_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
          this.contact_list.addEventListener('dragstart', function(o){ p.drag_start(o); });
          this.contact_list.addEventListener('dragmove', function(o, e){ p.drag_move(e); });
          this.contact_list.addEventListener('dragend', function(o){ p.drag_active = false; });
          this.contact_list.init();
@@ -266,7 +268,7 @@
        this.set_page_buttons();
        
        if (this.env.address_sources && !this.env.address_sources[this.env.source].readonly)
        if (this.env.address_sources && this.env.address_sources[this.env.source] && !this.env.address_sources[this.env.source].readonly)
          this.enable_command('add', true);
        
        if (this.env.cid)
@@ -364,7 +366,7 @@
  this.start_keepalive = function()
    {
    if (this.env.keep_alive && !this.env.framed && this.task=='mail' && this.gui_objects.mailboxlist)
      this._int = setInterval(function(){ ref.check_for_recent(); }, this.env.keep_alive * 1000);
      this._int = setInterval(function(){ ref.check_for_recent(false); }, this.env.keep_alive * 1000);
    else if (this.env.keep_alive && !this.env.framed && this.task!='login')
      this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);
    }
@@ -458,20 +460,13 @@
  this.init_address_input_events = function(obj)
    {
    var handler = function(e){ return ref.ksearch_keypress(e,this); };
    var handler2 = function(e){ return ref.ksearch_blur(e,this); };
    
    if (obj.addEventListener)
    {
      obj.addEventListener(bw.safari ? 'keydown' : 'keypress', handler, false);
      obj.addEventListener('blur', handler2, false);
    }
    else
    {
      obj.onkeydown = handler;
      obj.onblur = handler2;
    }
    obj.setAttribute('autocomplete', 'off');
    obj.setAttribute('autocomplete', 'off');
    };
@@ -565,7 +560,7 @@
        var sort_col = a_sort[0];
        var sort_order = a_sort[1] ? a_sort[1].toUpperCase() : null;
        var header;
        // no sort order specified: toggle
        if (sort_order==null)
          {
@@ -579,9 +574,9 @@
          break;
        // set table header class
        if (header = document.getElementById('rcmHead'+this.env.sort_col))
        if (header = document.getElementById('rcm'+this.env.sort_col))
          this.set_classname(header, 'sorted'+(this.env.sort_order.toUpperCase()), false);
        if (header = document.getElementById('rcmHead'+sort_col))
        if (header = document.getElementById('rcm'+sort_col))
          this.set_classname(header, 'sorted'+sort_order, true);
        // save new sort properties
@@ -820,7 +815,7 @@
        break;
      
      case 'checkmail':
        this.check_for_recent();
        this.check_for_recent(true);
        break;
      
      case 'compose':
@@ -1173,31 +1168,81 @@
      this.contact_list.blur();
      model = this.env.address_sources;
    }
    else if (this.ksearch_value) {
      this.ksearch_blur();
    }
    
    // handle mouse release when dragging
    if (this.drag_active && model) {
      for (var k in model) {
        if ((li = this.get_folder_li(k)) && rcube_mouse_is_over(e, li.firstChild) && this.check_droptarget(k)) {
          this.set_classname(li, 'droptarget', false);
          this.command('moveto', model[k].id);
          break;
        }
      }
    if (this.drag_active && model && this.env.last_folder_target) {
      this.set_classname(this.get_folder_li(this.env.last_folder_target), 'droptarget', false);
      this.command('moveto', model[this.env.last_folder_target].id);
      this.env.last_folder_target = null;
    }
  };
  this.drag_move = function(e)
  this.drag_start = function(list)
  {
    var li;
    var model = this.task == 'mail' ? this.env.mailboxes : this.env.address_sources;
    this.drag_active = true;
    if (this.preview_timer)
      clearTimeout(this.preview_timer);
    
    if (this.gui_objects.folderlist && model) {
    // save folderlist and folders location/sizes for droptarget calculation in drag_move()
    if (this.gui_objects.folderlist && model)
      {
      var li, pos, list, height;
      list = rcube_find_object(this.task == 'mail' ? 'mailboxlist' : 'directorylist');
      pos = rcube_get_object_pos(list);
      this.env.folderlist_coords = {x1:pos.x, y1:pos.y, x2:pos.x + list.offsetWidth, y2:pos.y + list.offsetHeight};
      this.env.folder_coords = new Array();
      for (var k in model) {
        if (li = this.get_folder_li(k))
          this.set_classname(li, 'droptarget', (rcube_mouse_is_over(e, li.firstChild) && this.check_droptarget(k)));
     {
     pos = rcube_get_object_pos(li.firstChild);
     // only visible folders
     if (height = li.firstChild.offsetHeight)
       this.env.folder_coords[k] = {x1:pos.x, y1:pos.y, x2:pos.x + li.firstChild.offsetWidth, y2:pos.y + height};
          }
        }
      }
    }
  };
  this.drag_move = function(e)
    {
    if (this.gui_objects.folderlist && this.env.folder_coords)
      {
      var li, pos, mouse;
      mouse = rcube_event.get_mouse_pos(e);
      pos = this.env.folderlist_coords;
      // if mouse pointer is outside of folderlist
      if (mouse.x < pos.x1 || mouse.x >= pos.x2
       || mouse.y < pos.y1 || mouse.y >= pos.y2)
   {
   if (this.env.last_folder_target) {
     this.set_classname(this.get_folder_li(this.env.last_folder_target), 'droptarget', false);
          this.env.last_folder_target = null;
     }
   return;
        }
      // over the folders
      for (var k in this.env.folder_coords)
        {
   pos = this.env.folder_coords[k];
   if (this.check_droptarget(k) && ((mouse.x >= pos.x1) && (mouse.x < pos.x2)
       && (mouse.y >= pos.y1) && (mouse.y < pos.y2)))
     {
          this.set_classname(this.get_folder_li(k), 'droptarget', true);
     this.env.last_folder_target = k;
     }
   else
     this.set_classname(this.get_folder_li(k), 'droptarget', false);
        }
      }
    };
  
  this.collapse_folder = function(id)
    {
@@ -1212,7 +1257,7 @@
        ul.style.display = '';
        this.set_classname(div, 'collapsed', false);
        this.set_classname(div, 'expanded', true);
        var reg = new RegExp('&'+escape(id)+'&');
        var reg = new RegExp('&'+urlencode(id)+'&');
        this.set_env('collapsed_folders', this.env.collapsed_folders.replace(reg, ''));
        }
      else
@@ -1220,8 +1265,8 @@
        ul.style.display = 'none';
        this.set_classname(div, 'expanded', false);
        this.set_classname(div, 'collapsed', true);
        this.set_env('collapsed_folders', this.env.collapsed_folders+'&'+escape(id)+'&');
        this.set_env('collapsed_folders', this.env.collapsed_folders+'&'+urlencode(id)+'&');
        // select parent folder if one of its childs is currently selected
        if (this.env.mailbox.indexOf(id + this.env.delimiter) == 0)
          this.command('list', id);
@@ -1238,7 +1283,7 @@
          li.nextSibling.getElementsByTagName("ul")[0].style.display = '';
        }
      this.http_post('save-pref', '_name=collapsed_folders&_value='+escape(this.env.collapsed_folders));
      this.http_post('save-pref', '_name=collapsed_folders&_value='+urlencode(this.env.collapsed_folders));
      this.set_unread_count_display(id, false);
      }
    }
@@ -1414,6 +1459,24 @@
      }
    };
  // list messages of a specific mailbox using filter
  this.filter_mailbox = function(filter)
    {
      var search;
      if (this.gui_objects.qsearchbox)
        search = this.gui_objects.qsearchbox.value;
      this.message_list.clear();
      // reset vars
      this.env.current_page = 1;
      this.set_busy(true, 'searching');
      this.http_request('search', '_filter='+filter
           + (search ? '&_q='+urlencode(search) : '')
           + (this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : ''), true);
    }
  // list messages of a specific mailbox
  this.list_mailbox = function(mbox, page, sort)
    {
@@ -1561,7 +1624,7 @@
    
    if (rows[uid].flagged && this.env.flaggedicon)
      icn_src = this.env.flaggedicon;
    else if (this.env.unflaggedicon)
    else if (!rows[uid].flagged && this.env.unflaggedicon)
      icn_src = this.env.unflaggedicon;
    if (rows[uid].flagged_icon && icn_src)
@@ -2128,14 +2191,18 @@
      if (this.env.signatures)
        {
        // Append the signature as a div within the body
        var sigElem = editor.dom.get("_rc_sig");
        var sigElem = editor.dom.get('_rc_sig');
   var newsig = '';
   var htmlsig = true;
        if (!sigElem)
          {
          sigElem = editor.getDoc().createElement("div");
          sigElem.setAttribute("id", "_rc_sig");
     // add empty line before signature on IE
     if (bw.ie)
            editor.getBody().appendChild(editor.getDoc().createElement('br'));
     sigElem = editor.getDoc().createElement('div');
          sigElem.setAttribute('id', '_rc_sig');
          editor.getBody().appendChild(sigElem);
          }
@@ -2283,7 +2350,7 @@
    };
  // send remote request to search mail or contacts
  this.qsearch = function(value)
  this.qsearch = function(value, addurl)
    {
    if (value != '')
      {
@@ -2292,12 +2359,18 @@
      else if (this.contact_list) {
        this.contact_list.clear(true);
        this.show_contentframe(false);
      }
        }
      if (this.gui_objects.search_filter)
      addurl = '&_filter=' + this.gui_objects.search_filter.value;
      // reset vars
      this.env.current_page = 1;
      this.set_busy(true, 'searching');
      this.http_request('search', '_q='+urlencode(value)+(this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : '')+(this.env.source ? '&_source='+urlencode(this.env.source) : ''), true);
      this.http_request('search', '_q='+urlencode(value)
        + (this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : '')
        + (this.env.source ? '&_source='+urlencode(this.env.source) : '')
        + (addurl ? addurl : ''), true);
      }
    return true;
    };
@@ -2325,10 +2398,7 @@
  // handler for keyboard events on address-fields
  this.ksearch_keypress = function(e, obj)
    {
    if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
      return true;
  {
    if (this.ksearch_timer)
      clearTimeout(this.ksearch_timer);
@@ -2344,24 +2414,13 @@
          break;
          
        var dir = key==38 ? 1 : 0;
        var next;
        
        highlight = document.getElementById('rcmksearchSelected');
        if (!highlight)
          highlight = this.ksearch_pane.ul.firstChild;
        
        if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
          {
          highlight.removeAttribute('id');
          this.set_classname(highlight, 'selected', false);
          }
        if (next)
          {
          next.setAttribute('id', 'rcmksearchSelected');
          this.set_classname(next, 'selected', true);
          this.ksearch_selected = next._rcm_id;
          }
        if (highlight)
          this.ksearch_select(dir ? highlight.previousSibling : highlight.nextSibling);
        return rcube_event.cancel(e);
@@ -2369,7 +2428,7 @@
        if(mod == SHIFT_KEY)
          break;
      case 13:  // enter
      case 13:  // enter
        if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
          break;
@@ -2390,7 +2449,22 @@
    this.ksearch_input = obj;
    
    return true;
    };
  };
  this.ksearch_select = function(node)
  {
    var current = document.getElementById('rcmksearchSelected');
    if (current && node) {
      current.removeAttribute('id');
      this.set_classname(current, 'selected', false);
    }
    if (node) {
      node.setAttribute('id', 'rcmksearchSelected');
      this.set_classname(node, 'selected', true);
      this.ksearch_selected = node._rcm_id;
    }
  };
  this.insert_recipient = function(id)
  {
@@ -2416,10 +2490,13 @@
  // address search processor
  this.ksearch_get_results = function()
    {
  {
    var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
    if (inp_value===null)
    if (inp_value === null)
      return;
    if (this.ksearch_pane && this.ksearch_pane.visible)
      this.ksearch_pane.show(0);
    // get string from current cursor pos to last comma
    var cpos = this.get_caret_pos(this.ksearch_input);
@@ -2429,45 +2506,45 @@
    // trim query string
    q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
    if (!q.length || q==this.ksearch_value)
      {
      if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
        this.ksearch_pane.show(0);
      return;
      }
    // Don't (re-)search if string is empty or if the last results are still active
    if (!q.length || q == this.ksearch_value)
        return;
    this.ksearch_value = q;
    
    // start searching the contact list
    var a_results = new Array();
    var a_result_ids = new Array();
    var c=0;
    for (var i=0; i<this.env.contacts.length; i++)
      {
      if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
        {
        a_results[c] = this.env.contacts[i];
        a_result_ids[c++] = i;
        if (c==15)  // limit search results
          break;
        }
      }
    this.display_message(this.get_label('searching'), 'loading', true);
    this.http_post('autocomplete', '_search='+q);
  };
  this.ksearch_query_results = function(results)
  {
    this.hide_message();
    this.env.contacts = results ? results : [];
    var result_ids = new Array();
    var c=0;
    for (var i=0; i < this.env.contacts.length; i++) {
      result_ids[c++] = i;
      if (c == 15)  // limit search results
        break;
    }
    this.ksearch_display_results(this.env.contacts, result_ids, c);
  };
  this.ksearch_display_results = function (a_results, a_result_ids, c)
  {
    // display search results
    if (c && a_results.length)
      {
    if (c && a_results.length && this.ksearch_input) {
      var p, ul, li;
      
      // create results pane if not present
      if (!this.ksearch_pane)
        {
      if (!this.ksearch_pane) {
        ul = document.createElement('UL');
        this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
        this.ksearch_pane.elm.appendChild(ul);
        this.ksearch_pane.ul = ul;
        }
      }
      else
        ul = this.ksearch_pane.ul;
@@ -2475,51 +2552,58 @@
      ul.innerHTML = '';
            
      // add each result line to list
      for (i=0; i<a_results.length; i++)
        {
      for (i=0; i<a_results.length; i++) {
        li = document.createElement('LI');
        li.innerHTML = a_results[i].replace(/</, '&lt;').replace(/>/, '&gt;');
        li.innerHTML = a_results[i].replace(new RegExp('('+this.ksearch_value+')', 'ig'), '##$1%%').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/##([^%]+)%%/g, '<b>$1</b>');
        li.onmouseover = function(){ ref.ksearch_select(this); };
        li.onmouseup = function(){ ref.ksearch_click(this) };
        li._rcm_id = a_result_ids[i];
        ul.appendChild(li);
        }
      }
      // check if last selected item is still in result list
      if (this.ksearch_selected!==null)
        {
      if (this.ksearch_selected !== null) {
        p = find_in_array(this.ksearch_selected, a_result_ids);
        if (p>=0 && ul.childNodes)
          {
        if (p >= 0 && ul.childNodes) {
          ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
          this.set_classname(ul.childNodes[p], 'selected', true);
          }
        }
        else
          this.ksearch_selected = null;
        }
      }
      
      // if no item selected, select the first one
      if (this.ksearch_selected===null)
        {
      if (this.ksearch_selected === null) {
        ul.firstChild.setAttribute('id', 'rcmksearchSelected');
        this.set_classname(ul.firstChild, 'selected', true);
        this.ksearch_selected = a_result_ids[0];
        }
      }
      // move the results pane right under the input box and make it visible
      var pos = rcube_get_object_pos(this.ksearch_input);
      this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
      this.ksearch_pane.show(1);
      }
      this.ksearch_pane.show(1);
    }
    // hide results pane
    else
      this.ksearch_hide();
    };
  };
  this.ksearch_click = function(node)
  {
    this.insert_recipient(node._rcm_id);
    this.ksearch_hide();
    if (ref.ksearch_input)
      this.ksearch_input.focus();
  };
  this.ksearch_blur = function(e, obj)
  this.ksearch_blur = function()
    {
    if (this.ksearch_timer)
      clearTimeout(this.ksearch_timer);
    this.ksearch_value = '';
    this.ksearch_value = '';
    this.ksearch_input = null;
    
    this.ksearch_hide();
@@ -2531,7 +2615,7 @@
    this.ksearch_selected = null;
    
    if (this.ksearch_pane)
      this.ksearch_pane.show(0);
      this.ksearch_pane.show(0);
    };
@@ -3403,14 +3487,10 @@
        else
          cell.innerHTML = this.get_label(this.coltypes[n]);
        cell.id = 'rcmHead'+col;
        cell.id = 'rcm'+col;
        }
      else if (col == 'subject' && this.message_list)
        this.message_list.subject_col = n+1;
      else if (col == 'flag' && this.env.unflaggedicon)
        {
     cell.innerHTML = '<img src="'+this.env.unflaggedicon+'" alt="" />';
   }
      }
  };
@@ -3472,19 +3552,16 @@
        {
        if (flags.flagged && this.env.flaggedicon)
          col.innerHTML = '<img src="'+this.env.flaggedicon+'" alt="" />';
        else if(this.env.unflaggedicon)
        else if(!flags.flagged && this.env.unflaggedicon)
          col.innerHTML = '<img src="'+this.env.unflaggedicon+'" alt="" />';
   }
      }
      else if (c=='attachment')
        col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" />' : '&nbsp;';
      else
        col.innerHTML = cols[c];
      row.appendChild(col);
      }
    col = document.createElement('TD');
    col.className = 'icon';
    col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" />' : '';
    row.appendChild(col);
    this.message_list.insert_row(row, attop);
@@ -3886,12 +3963,14 @@
    };
  // send periodic request to check for recent messages
  this.check_for_recent = function()
  this.check_for_recent = function(setbusy)
    {
    if (this.busy)
      return;
    this.set_busy(true, 'checkingmail');
    if (setbusy)
      this.set_busy(true, 'checkingmail');
    this.http_request('check-recent', (this.env.search_request ? '_search='+this.env.search_request+'&' : '') + '_t='+(new Date().getTime()), true);
    };