Dennis1993
2013-09-02 7dff4437c06369da69841e3a4fee2b931bac0ccc
commit | author | age
6b47de 1 /*
T 2  +-----------------------------------------------------------------------+
e019f2 3  | Roundcube List Widget                                                 |
6b47de 4  |                                                                       |
e019f2 5  | This file is part of the Roundcube Webmail client                     |
d94a71 6  | Copyright (C) 2006-2013, The Roundcube Dev Team                       |
7fe381 7  |                                                                       |
T 8  | Licensed under the GNU General Public License version 3 or            |
9  | any later version with exceptions for skins & plugins.                |
10  | See the README file for a full license statement.                     |
6b47de 11  |                                                                       |
T 12  +-----------------------------------------------------------------------+
13  | Authors: Thomas Bruederli <roundcube@gmail.com>                       |
14  |          Charles McNulty <charles@charlesmcnulty.com>                 |
15  +-----------------------------------------------------------------------+
16  | Requires: common.js                                                   |
17  +-----------------------------------------------------------------------+
18 */
19
20
21 /**
e019f2 22  * Roundcube List Widget class
6b47de 23  * @contructor
T 24  */
25 function rcube_list_widget(list, p)
8fa922 26 {
6b47de 27   // static contants
T 28   this.ENTER_KEY = 13;
29   this.DELETE_KEY = 46;
6e6e89 30   this.BACKSPACE_KEY = 8;
8fa922 31
6b47de 32   this.list = list ? list : null;
517dae 33   this.tagname = this.list ? this.list.nodeName.toLowerCase() : 'table';
TB 34   this.thead;
35   this.tbody;
73ad4f 36   this.fixed_header;
6b47de 37   this.frame = null;
T 38   this.rows = [];
39   this.selection = [];
0dbac3 40   this.rowcount = 0;
b62c48 41   this.colcount = 0;
8fa922 42
d24d20 43   this.subject_col = -1;
699a25 44   this.modkey = 0;
6b47de 45   this.multiselect = false;
f52c93 46   this.multiexpand = false;
21168d 47   this.multi_selecting = false;
6b47de 48   this.draggable = false;
b62c48 49   this.column_movable = false;
6b47de 50   this.keyboard = false;
68b6a9 51   this.toggleselect = false;
8fa922 52
6b47de 53   this.dont_select = false;
T 54   this.drag_active = false;
b62c48 55   this.col_drag_active = false;
A 56   this.column_fixed = null;
6b47de 57   this.last_selected = 0;
b2fb95 58   this.shift_start = 0;
6b47de 59   this.in_selection_before = false;
T 60   this.focused = false;
61   this.drag_mouse_start = null;
c0e364 62   this.dblclick_time = 500; // default value on MS Windows is 500
517dae 63   this.row_init = function(){};  // @deprecated; use list.addEventListener('initrow') instead
8fa922 64
6b47de 65   // overwrite default paramaters
ef4f59 66   if (p && typeof p === 'object')
6b47de 67     for (var n in p)
T 68       this[n] = p[n];
8fa922 69 };
6b47de 70
T 71
72 rcube_list_widget.prototype = {
73
74
75 /**
76  * get all message rows from HTML table and init each row
77  */
78 init: function()
79 {
517dae 80   if (this.tagname == 'table' && this.list && this.list.tBodies[0]) {
TB 81     this.thead = this.list.tHead;
82     this.tbody = this.list.tBodies[0];
83   }
84   else if (this.tagname != 'table' && this.list) {
85     this.tbody = this.list;
86   }
87
88   if (this.tbody) {
8fa922 89     this.rows = [];
0dbac3 90     this.rowcount = 0;
6b47de 91
517dae 92     var r, len, rows = this.tbody.childNodes;
6b47de 93
0e7b66 94     for (r=0, len=rows.length; r<len; r++) {
A 95       this.init_row(rows[r]);
0dbac3 96       this.rowcount++;
6b47de 97     }
T 98
b62c48 99     this.init_header();
6b47de 100     this.frame = this.list.parentNode;
T 101
102     // set body events
17a8fb 103     if (this.keyboard)
AM 104       rcube_event.add_listener({event:'keydown', object:this, method:'key_press'});
6b47de 105   }
T 106 },
107
108
109 /**
c4b819 110  * Init list row and set mouse events on it
6b47de 111  */
T 112 init_row: function(row)
113 {
114   // make references in internal array and set event handlers
bbd4ca 115   if (row && String(row.id).match(/^rcmrow([a-z0-9\-_=\+\/]+)/i)) {
1633bc 116     var self = this,
A 117       uid = RegExp.$1;
6b47de 118     row.uid = uid;
f52c93 119     this.rows[uid] = {uid:uid, id:row.id, obj:row};
6b47de 120
T 121     // set eventhandlers to table row
8ef2f3 122     row.onmousedown = function(e){ return self.drag_row(e, this.uid); };
T 123     row.onmouseup = function(e){ return self.click_row(e, this.uid); };
1ce442 124
4910b0 125     if (bw.touch) {
8ef2f3 126       row.addEventListener('touchstart', function(e) {
T 127         if (e.touches.length == 1) {
dc8400 128           self.touchmoved = false;
TB 129           self.drag_row(rcube_event.touchevent(e.touches[0]), this.uid)
8ef2f3 130         }
T 131       }, false);
132       row.addEventListener('touchend', function(e) {
dc8400 133         if (e.changedTouches.length == 1) {
TB 134           if (!self.touchmoved && !self.click_row(rcube_event.touchevent(e.changedTouches[0]), this.uid))
8ef2f3 135             e.preventDefault();
dc8400 136         }
TB 137       }, false);
138       row.addEventListener('touchmove', function(e) {
139         if (e.changedTouches.length == 1) {
140           self.touchmoved = true;
141           if (self.drag_active)
142             e.preventDefault();
143         }
8ef2f3 144       }, false);
T 145     }
6b47de 146
T 147     if (document.all)
148       row.onselectstart = function() { return false; };
149
517dae 150     this.row_init(this.rows[uid]);  // legacy support
TB 151     this.triggerEvent('initrow', this.rows[uid]);
b62c48 152   }
A 153 },
154
155
156 /**
157  * Init list column headers and set mouse events on them
158  */
159 init_header: function()
160 {
517dae 161   if (this.thead) {
b62c48 162     this.colcount = 0;
A 163
73ad4f 164     if (this.fixed_header) {  // copy (modified) fixed header back to the actual table
TB 165       $(this.list.tHead).replaceWith($(this.fixed_header).find('thead').clone());
166       $(this.list.tHead).find('tr td').attr('style', '');  // remove fixed widths
167     }
4910b0 168     else if (!bw.touch && this.list.className.indexOf('fixedheader') >= 0) {
73ad4f 169       this.init_fixed_header();
TB 170     }
171
b62c48 172     var col, r, p = this;
A 173     // add events for list columns moving
517dae 174     if (this.column_movable && this.thead && this.thead.rows) {
TB 175       for (r=0; r<this.thead.rows[0].cells.length; r++) {
b62c48 176         if (this.column_fixed == r)
A 177           continue;
517dae 178         col = this.thead.rows[0].cells[r];
b62c48 179         col.onmousedown = function(e){ return p.drag_column(e, this); };
A 180         this.colcount++;
181       }
182     }
6b47de 183   }
T 184 },
185
73ad4f 186 init_fixed_header: function()
TB 187 {
188   var clone = $(this.list.tHead).clone();
189
190   if (!this.fixed_header) {
191     this.fixed_header = $('<table>')
8efdd9 192       .attr('class', this.list.className + ' fixedcopy')
73ad4f 193       .css({ position:'fixed' })
TB 194       .append(clone)
195       .append('<tbody></tbody>');
196     $(this.list).before(this.fixed_header);
197
198     var me = this;
199     $(window).resize(function(){ me.resize() });
200   }
201   else {
202     $(this.fixed_header).find('thead').replaceWith(clone);
203   }
204
205   this.thead = clone.get(0);
206   this.resize();
207 },
208
209 resize: function()
210 {
211     if (!this.fixed_header)
212       return;
213
214     var column_widths = [];
215
216     // get column widths from original thead
217     $(this.tbody).parent().find('thead tr td').each(function(index) {
218       column_widths[index] = $(this).width();
219     });
220
221     // apply fixed widths to fixed table header
222     $(this.thead).parent().width($(this.tbody).parent().width());
223     $(this.thead).find('tr td').each(function(index) {
224       $(this).css('width', column_widths[index]);
225     });
226 },
6b47de 227
T 228 /**
c4b819 229  * Remove all list rows
6b47de 230  */
f11541 231 clear: function(sel)
6b47de 232 {
517dae 233   if (this.tagname == 'table') {
TB 234     var tbody = document.createElement('tbody');
235     this.list.insertBefore(tbody, this.tbody);
236     this.list.removeChild(this.list.tBodies[1]);
237     this.tbody = tbody;
238   }
239   else {
240     $(this.row_tagname() + ':not(.thead)', this.tbody).remove();
241   }
54531f 242
8fa922 243   this.rows = [];
0dbac3 244   this.rowcount = 0;
8fa922 245
A 246   if (sel)
247     this.clear_selection();
1633bc 248
A 249   // reset scroll position (in Opera)
250   if (this.frame)
251     this.frame.scrollTop = 0;
6b47de 252 },
T 253
254
255 /**
256  * 'remove' message row from list (just hide it)
257  */
b62656 258 remove_row: function(uid, sel_next)
6b47de 259 {
517dae 260   var node = this.rows[uid] ? this.rows[uid].obj : null;
d741a9 261
517dae 262   if (!node)
d741a9 263     return;
A 264
517dae 265   node.style.display = 'none';
6b47de 266
b62656 267   if (sel_next)
T 268     this.select_next();
269
0e7b66 270   delete this.rows[uid];
0dbac3 271   this.rowcount--;
6b47de 272 },
T 273
274
275 /**
c4b819 276  * Add row to the list and initialize it
6b47de 277  */
ea6d69 278 insert_row: function(row, before)
6b47de 279 {
517dae 280   var tbody = this.tbody;
6b47de 281
517dae 282   // create a real dom node first
TB 283   if (row.nodeName === undefined) {
284     // for performance reasons use DOM instead of jQuery here
285     var domrow = document.createElement(this.row_tagname());
286     if (row.id) domrow.id = row.id;
287     if (row.className) domrow.className = row.className;
288     if (row.style) $.extend(domrow.style, row.style);
289
290     for (var domcell, col, i=0; row.cols && i < row.cols.length; i++) {
291       col = row.cols[i];
292       domcell = document.createElement(this.col_tagname());
293       if (col.className) domcell.className = col.className;
294       if (col.innerHTML) domcell.innerHTML = col.innerHTML;
295       domrow.appendChild(domcell);
296     }
297
298     row = domrow;
299   }
300
a52297 301   if (before && tbody.childNodes.length)
ea6d69 302     tbody.insertBefore(row, (typeof before == 'object' && before.parentNode == tbody) ? before : tbody.firstChild);
6b47de 303   else
c4b819 304     tbody.appendChild(row);
6b47de 305
c4b819 306   this.init_row(row);
0dbac3 307   this.rowcount++;
6b47de 308 },
T 309
517dae 310 /**
TB 311  * 
312  */
313 update_row: function(id, cols, newid, select)
314 {
315   var row = this.rows[id];
316   if (!row) return false;
317
318   var domrow = row.obj;
319   for (var domcell, col, i=0; cols && i < cols.length; i++) {
320     this.get_cell(domrow, i).html(cols[i]);
321   }
322
323   if (newid) {
324     delete this.rows[id];
325     domrow.id = 'rcmrow' + newid;
326     this.init_row(domrow);
327
328     if (select)
329       this.selection[0] = newid;
330   }
331 },
6b47de 332
T 333
334 /**
25c35c 335  * Set focus to the list
6b47de 336  */
T 337 focus: function(e)
338 {
1633bc 339   var n, id;
6b47de 340   this.focused = true;
2c2000 341
1633bc 342   for (n in this.selection) {
6b47de 343     id = this.selection[n];
cc97ea 344     if (this.rows[id] && this.rows[id].obj) {
T 345       $(this.rows[id].obj).addClass('selected').removeClass('unfocused');
6b47de 346     }
T 347   }
348
e26399 349   // Un-focus already focused elements (#1487123, #1487316, #1488600, #1488620)
3db62c 350   // It looks that window.focus() does the job for all browsers, but not Firefox (#1489058)
e26399 351   $(':focus:not(body)').blur();
3db62c 352   window.focus();
2c2000 353
6b47de 354   if (e || (e = window.event))
T 355     rcube_event.cancel(e);
356 },
357
358
359 /**
360  * remove focus from the list
361  */
362 blur: function()
363 {
1633bc 364   var n, id;
6b47de 365   this.focused = false;
1633bc 366   for (n in this.selection) {
6b47de 367     id = this.selection[n];
cc97ea 368     if (this.rows[id] && this.rows[id].obj) {
3c6715 369       $(this.rows[id].obj).removeClass('selected focused').addClass('unfocused');
6b47de 370     }
T 371   }
372 },
373
374
375 /**
b62c48 376  * onmousedown-handler of message list column
A 377  */
378 drag_column: function(e, col)
379 {
380   if (this.colcount > 1) {
381     this.drag_start = true;
382     this.drag_mouse_start = rcube_event.get_mouse_pos(e);
383
384     rcube_event.add_listener({event:'mousemove', object:this, method:'column_drag_mouse_move'});
385     rcube_event.add_listener({event:'mouseup', object:this, method:'column_drag_mouse_up'});
386
387     // enable dragging over iframes
388     this.add_dragfix();
389
390     // find selected column number
517dae 391     for (var i=0; i<this.thead.rows[0].cells.length; i++) {
TB 392       if (col == this.thead.rows[0].cells[i]) {
b62c48 393         this.selected_column = i;
A 394         break;
395       }
396     }
397   }
398
399   return false;
400 },
401
402
403 /**
6b47de 404  * onmousedown-handler of message list row
T 405  */
406 drag_row: function(e, id)
407 {
408   // don't do anything (another action processed before)
54531f 409   var evtarget = rcube_event.get_target(e),
A 410     tagname = evtarget.tagName.toLowerCase();
411
91a35e 412   if (this.dont_select || (evtarget && (tagname == 'input' || tagname == 'img')))
40d7c2 413     return true;
8fa922 414
f89f03 415   // accept right-clicks
T 416   if (rcube_event.get_button(e) == 2)
417     return true;
8fa922 418
88b423 419   this.in_selection_before = e && e.istouch || this.in_selection(id) ? id : false;
bf36a9 420
6b47de 421   // selects currently unselected row
8fa922 422   if (!this.in_selection_before) {
6b47de 423     var mod_key = rcube_event.get_modifier(e);
T 424     this.select_row(id, mod_key, false);
425   }
426
dc8400 427   if (this.draggable && this.selection.length && this.in_selection(id)) {
6b47de 428     this.drag_start = true;
b2fb95 429     this.drag_mouse_start = rcube_event.get_mouse_pos(e);
6c11ee 430     rcube_event.add_listener({event:'mousemove', object:this, method:'drag_mouse_move'});
A 431     rcube_event.add_listener({event:'mouseup', object:this, method:'drag_mouse_up'});
4910b0 432     if (bw.touch) {
8ef2f3 433       rcube_event.add_listener({event:'touchmove', object:this, method:'drag_mouse_move'});
T 434       rcube_event.add_listener({event:'touchend', object:this, method:'drag_mouse_up'});
435     }
cf6bc5 436
6c11ee 437     // enable dragging over iframes
b62c48 438     this.add_dragfix();
6b47de 439   }
T 440
441   return false;
442 },
443
444
445 /**
446  * onmouseup-handler of message list row
447  */
448 click_row: function(e, id)
449 {
54531f 450   var now = new Date().getTime(),
A 451     mod_key = rcube_event.get_modifier(e),
452     evtarget = rcube_event.get_target(e),
453     tagname = evtarget.tagName.toLowerCase();
40d7c2 454
91a35e 455   if ((evtarget && (tagname == 'input' || tagname == 'img')))
40d7c2 456     return true;
A 457
6b47de 458   // don't do anything (another action processed before)
8fa922 459   if (this.dont_select) {
6b47de 460     this.dont_select = false;
T 461     return false;
8ef2f3 462   }
8fa922 463
6b47de 464   var dblclicked = now - this.rows[id].clicked < this.dblclick_time;
T 465
466   // unselects currently selected row
467   if (!this.drag_active && this.in_selection_before == id && !dblclicked)
468     this.select_row(id, mod_key, false);
469
470   this.drag_start = false;
471   this.in_selection_before = false;
472
473   // row was double clicked
fc643e 474   if (this.rows && dblclicked && this.in_selection(id)) {
cc97ea 475     this.triggerEvent('dblclick');
fc643e 476     now = 0;
T 477   }
6b47de 478   else
cc97ea 479     this.triggerEvent('click');
6b47de 480
dd51b7 481   if (!this.drag_active) {
A 482     // remove temp divs
b62c48 483     this.del_dragfix();
6b47de 484     rcube_event.cancel(e);
dd51b7 485   }
6b47de 486
T 487   this.rows[id].clicked = now;
488   return false;
489 },
490
491
bc2acc 492 /*
A 493  * Returns thread root ID for specified row ID
494  */
495 find_root: function(uid)
496 {
497    var r = this.rows[uid];
498
499    if (r && r.parent_uid)
500      return this.find_root(r.parent_uid);
501    else
502      return uid;
503 },
504
505
f52c93 506 expand_row: function(e, id)
T 507 {
54531f 508   var row = this.rows[id],
A 509     evtarget = rcube_event.get_target(e),
510     mod_key = rcube_event.get_modifier(e);
f52c93 511
T 512   // Don't select this message
513   this.dont_select = true;
514   // Don't treat double click on the expando as double click on the message.
515   row.clicked = 0;
516
517   if (row.expanded) {
54531f 518     evtarget.className = 'collapsed';
f52c93 519     if (mod_key == CONTROL_KEY || this.multiexpand)
T 520       this.collapse_all(row);
521     else
522       this.collapse(row);
523   }
524   else {
54531f 525     evtarget.className = 'expanded';
f52c93 526     if (mod_key == CONTROL_KEY || this.multiexpand)
T 527       this.expand_all(row);
528     else
529      this.expand(row);
530   }
531 },
532
533 collapse: function(row)
534 {
535   row.expanded = false;
32afef 536   this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded, obj:row.obj });
f52c93 537   var depth = row.depth;
T 538   var new_row = row ? row.obj.nextSibling : null;
539   var r;
540
541   while (new_row) {
542     if (new_row.nodeType == 1) {
543       var r = this.rows[new_row.uid];
544       if (r && r.depth <= depth)
545         break;
a3c9bd 546       $(new_row).css('display', 'none');
54531f 547       if (r.expanded) {
A 548         r.expanded = false;
32afef 549         this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded, obj:new_row });
54531f 550       }
f52c93 551     }
T 552     new_row = new_row.nextSibling;
553   }
554
73ad4f 555   this.resize();
d94a71 556   this.triggerEvent('listupdate');
f52c93 557   return false;
T 558 },
559
560 expand: function(row)
561 {
1633bc 562   var r, p, depth, new_row, last_expanded_parent_depth;
f52c93 563
T 564   if (row) {
565     row.expanded = true;
566     depth = row.depth;
567     new_row = row.obj.nextSibling;
bc2acc 568     this.update_expando(row.uid, true);
32afef 569     this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded, obj:row.obj });
f52c93 570   }
T 571   else {
517dae 572     var tbody = this.tbody;
f52c93 573     new_row = tbody.firstChild;
T 574     depth = 0;
575     last_expanded_parent_depth = 0;
576   }
577
578   while (new_row) {
579     if (new_row.nodeType == 1) {
1633bc 580       r = this.rows[new_row.uid];
f52c93 581       if (r) {
T 582         if (row && (!r.depth || r.depth <= depth))
583           break;
584
585         if (r.parent_uid) {
1633bc 586           p = this.rows[r.parent_uid];
f52c93 587           if (p && p.expanded) {
T 588             if ((row && p == row) || last_expanded_parent_depth >= p.depth - 1) {
589               last_expanded_parent_depth = p.depth;
a3c9bd 590               $(new_row).css('display', '');
f52c93 591               r.expanded = true;
32afef 592               this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded, obj:new_row });
f52c93 593             }
T 594           }
595           else
596             if (row && (! p || p.depth <= depth))
597               break;
598         }
599       }
600     }
601     new_row = new_row.nextSibling;
602   }
603
73ad4f 604   this.resize();
d94a71 605   this.triggerEvent('listupdate');
f52c93 606   return false;
T 607 },
608
609
610 collapse_all: function(row)
611 {
54531f 612   var depth, new_row, r;
f52c93 613
T 614   if (row) {
615     row.expanded = false;
616     depth = row.depth;
617     new_row = row.obj.nextSibling;
bc2acc 618     this.update_expando(row.uid);
32afef 619     this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded, obj:row.obj });
8fa922 620
f52c93 621     // don't collapse sub-root tree in multiexpand mode 
T 622     if (depth && this.multiexpand)
54531f 623       return false;
f52c93 624   }
T 625   else {
517dae 626     new_row = this.tbody.firstChild;
f52c93 627     depth = 0;
T 628   }
629
630   while (new_row) {
631     if (new_row.nodeType == 1) {
54531f 632       if (r = this.rows[new_row.uid]) {
f52c93 633         if (row && (!r.depth || r.depth <= depth))
T 634           break;
635
636         if (row || r.depth)
a3c9bd 637           $(new_row).css('display', 'none');
54531f 638         if (r.has_children && r.expanded) {
f52c93 639           r.expanded = false;
54531f 640           this.update_expando(r.uid, false);
32afef 641           this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded, obj:new_row });
f52c93 642         }
T 643       }
644     }
645     new_row = new_row.nextSibling;
646   }
647
73ad4f 648   this.resize();
d94a71 649   this.triggerEvent('listupdate');
f52c93 650   return false;
T 651 },
652
2b55d4 653
f52c93 654 expand_all: function(row)
T 655 {
54531f 656   var depth, new_row, r;
f52c93 657
T 658   if (row) {
659     row.expanded = true;
660     depth = row.depth;
661     new_row = row.obj.nextSibling;
bc2acc 662     this.update_expando(row.uid, true);
32afef 663     this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded, obj:row.obj });
f52c93 664   }
T 665   else {
517dae 666     new_row = this.tbody.firstChild;
f52c93 667     depth = 0;
T 668   }
669
670   while (new_row) {
671     if (new_row.nodeType == 1) {
54531f 672       if (r = this.rows[new_row.uid]) {
f52c93 673         if (row && r.depth <= depth)
T 674           break;
675
a3c9bd 676         $(new_row).css('display', '');
54531f 677         if (r.has_children && !r.expanded) {
f52c93 678           r.expanded = true;
54531f 679           this.update_expando(r.uid, true);
32afef 680           this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded, obj:new_row });
f52c93 681         }
T 682       }
683     }
684     new_row = new_row.nextSibling;
685   }
d94a71 686
73ad4f 687   this.resize();
d94a71 688   this.triggerEvent('listupdate');
f52c93 689   return false;
T 690 },
2b55d4 691
bc2acc 692
A 693 update_expando: function(uid, expanded)
694 {
695   var expando = document.getElementById('rcmexpando' + uid);
696   if (expando)
697     expando.className = expanded ? 'expanded' : 'collapsed';
698 },
699
f52c93 700
6b47de 701 /**
49771b 702  * get first/next/previous/last rows that are not hidden
6b47de 703  */
T 704 get_next_row: function()
705 {
706   if (!this.rows)
707     return false;
708
54531f 709   var last_selected_row = this.rows[this.last_selected],
A 710     new_row = last_selected_row ? last_selected_row.obj.nextSibling : null;
711
6b47de 712   while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
T 713     new_row = new_row.nextSibling;
714
715   return new_row;
716 },
717
718 get_prev_row: function()
719 {
720   if (!this.rows)
721     return false;
722
54531f 723   var last_selected_row = this.rows[this.last_selected],
A 724     new_row = last_selected_row ? last_selected_row.obj.previousSibling : null;
725
6b47de 726   while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
T 727     new_row = new_row.previousSibling;
728
729   return new_row;
49771b 730 },
A 731
732 get_first_row: function()
733 {
8fa922 734   if (this.rowcount) {
517dae 735     var i, len, rows = this.tbody.childNodes;
49771b 736
0e7b66 737     for (i=0, len=rows.length-1; i<len; i++)
bbd4ca 738       if (rows[i].id && String(rows[i].id).match(/^rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
3c6715 739         return RegExp.$1;
8fa922 740   }
49771b 741
A 742   return null;
6b47de 743 },
T 744
095d05 745 get_last_row: function()
A 746 {
8fa922 747   if (this.rowcount) {
517dae 748     var i, rows = this.tbody.childNodes;
6b47de 749
0e7b66 750     for (i=rows.length-1; i>=0; i--)
bbd4ca 751       if (rows[i].id && String(rows[i].id).match(/^rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
0e7b66 752         return RegExp.$1;
8fa922 753   }
095d05 754
A 755   return null;
756 },
757
517dae 758 row_tagname: function()
TB 759 {
760   var row_tagnames = { table:'tr', ul:'li', '*':'div' };
761   return row_tagnames[this.tagname] || row_tagnames['*'];
762 },
763
764 col_tagname: function()
765 {
766   var col_tagnames = { table:'td', '*':'span' };
767   return col_tagnames[this.tagname] || col_tagnames['*'];
768 },
769
770 get_cell: function(row, index)
771 {
772   return $(this.col_tagname(), row).eq(index);
773 },
095d05 774
A 775 /**
776  * selects or unselects the proper row depending on the modifier key pressed
777  */
6b47de 778 select_row: function(id, mod_key, with_mouse)
T 779 {
780   var select_before = this.selection.join(',');
781   if (!this.multiselect)
782     mod_key = 0;
8fa922 783
b2fb95 784   if (!this.shift_start)
T 785     this.shift_start = id
6b47de 786
8fa922 787   if (!mod_key) {
6b47de 788     this.shift_start = id;
T 789     this.highlight_row(id, false);
21168d 790     this.multi_selecting = false;
6b47de 791   }
8fa922 792   else {
A 793     switch (mod_key) {
6b47de 794       case SHIFT_KEY:
b2fb95 795         this.shift_select(id, false);
6b47de 796         break;
T 797
798       case CONTROL_KEY:
799         if (!with_mouse)
b2fb95 800           this.highlight_row(id, true);
699a25 801         break;
6b47de 802
T 803       case CONTROL_SHIFT_KEY:
804         this.shift_select(id, true);
805         break;
806
807       default:
b2fb95 808         this.highlight_row(id, false);
6b47de 809         break;
T 810     }
21168d 811     this.multi_selecting = true;
6b47de 812   }
T 813
814   // trigger event if selection changed
815   if (this.selection.join(',') != select_before)
cc97ea 816     this.triggerEvent('select');
6b47de 817
T 818   if (this.last_selected != 0 && this.rows[this.last_selected])
cc97ea 819     $(this.rows[this.last_selected].obj).removeClass('focused');
a9dda5 820
T 821   // unselect if toggleselect is active and the same row was clicked again
8fa922 822   if (this.toggleselect && this.last_selected == id) {
a9dda5 823     this.clear_selection();
T 824     id = null;
825   }
826   else
cc97ea 827     $(this.rows[id].obj).addClass('focused');
a9dda5 828
b2fb95 829   if (!this.selection.length)
T 830     this.shift_start = null;
6b47de 831
T 832   this.last_selected = id;
833 },
834
835
836 /**
837  * Alias method for select_row
838  */
839 select: function(id)
840 {
841   this.select_row(id, false);
842   this.scrollto(id);
843 },
844
845
846 /**
847  * Select row next to the last selected one.
848  * Either below or above.
849  */
850 select_next: function()
851 {
1633bc 852   var next_row = this.get_next_row(),
A 853     prev_row = this.get_prev_row(),
854     new_row = (next_row) ? next_row : prev_row;
855
6b47de 856   if (new_row)
1ce442 857     this.select_row(new_row.uid, false, false);
6b47de 858 },
T 859
bc2acc 860
49771b 861 /**
A 862  * Select first row 
863  */
bc2acc 864 select_first: function(mod_key)
49771b 865 {
bc2acc 866   var row = this.get_first_row();
1633bc 867   if (row) {
A 868     if (mod_key) {
869       this.shift_select(row, mod_key);
870       this.triggerEvent('select');
871       this.scrollto(row);
872     }
873     else {
874       this.select(row);
875     }
bc2acc 876   }
49771b 877 },
bc2acc 878
A 879
880 /**
2b55d4 881  * Select last row
bc2acc 882  */
A 883 select_last: function(mod_key)
884 {
885   var row = this.get_last_row();
1633bc 886   if (row) {
A 887     if (mod_key) {
888       this.shift_select(row, mod_key);
889       this.triggerEvent('select');
890       this.scrollto(row);
891     }
892     else {
893       this.select(row);
894     }
bc2acc 895   }
A 896 },
897
49771b 898
84a331 899 /**
T 900  * Add all childs of the given row to selection
901  */
2b55d4 902 select_children: function(uid)
84a331 903 {
2b55d4 904   var i, children = this.row_children(uid), len = children.length;
8fa922 905
2b55d4 906   for (i=0; i<len; i++)
AM 907     if (!this.in_selection(children[i]))
908       this.select_row(children[i], CONTROL_KEY);
84a331 909 },
T 910
6b47de 911
T 912 /**
913  * Perform selection when shift key is pressed
914  */
915 shift_select: function(id, control)
916 {
b00bd0 917   if (!this.rows[this.shift_start] || !this.selection.length)
f2892d 918     this.shift_start = id;
A 919
50cc5b 920   var n, i, j, to_row = this.rows[id],
517dae 921     from_rowIndex = this._rowIndex(this.rows[this.shift_start].obj),
TB 922     to_rowIndex = this._rowIndex(to_row.obj);
50cc5b 923
AM 924   if (!to_row.expanded && to_row.has_children)
925     if (to_row = this.rows[(this.row_children(id)).pop()])
517dae 926       to_rowIndex = this._rowIndex(to_row.obj);
50cc5b 927
AM 928   i = ((from_rowIndex < to_rowIndex) ? from_rowIndex : to_rowIndex),
929   j = ((from_rowIndex > to_rowIndex) ? from_rowIndex : to_rowIndex);
6b47de 930
T 931   // iterate through the entire message list
1633bc 932   for (n in this.rows) {
517dae 933     if (this._rowIndex(this.rows[n].obj) >= i && this._rowIndex(this.rows[n].obj) <= j) {
f52c93 934       if (!this.in_selection(n)) {
6b47de 935         this.highlight_row(n, true);
f52c93 936       }
6b47de 937     }
8fa922 938     else {
1633bc 939       if (this.in_selection(n) && !control) {
6b47de 940         this.highlight_row(n, true);
f52c93 941       }
6b47de 942     }
T 943   }
944 },
945
517dae 946 /**
TB 947  * Helper method to emulate the rowIndex property of non-tr elements
948  */
949 _rowIndex: function(obj)
950 {
951   return (obj.rowIndex !== undefined) ? obj.rowIndex : $(obj).prevAll().length;
952 },
6b47de 953
T 954 /**
955  * Check if given id is part of the current selection
956  */
957 in_selection: function(id)
958 {
1633bc 959   for (var n in this.selection)
6b47de 960     if (this.selection[n]==id)
T 961       return true;
962
f52c93 963   return false;
6b47de 964 },
T 965
966
967 /**
968  * Select each row in list
969  */
970 select_all: function(filter)
971 {
972   if (!this.rows || !this.rows.length)
973     return false;
974
1094cd 975   // reset but remember selection first
1633bc 976   var n, select_before = this.selection.join(',');
8fa922 977   this.selection = [];
A 978
1633bc 979   for (n in this.rows) {
0e7b66 980     if (!filter || this.rows[n][filter] == true) {
6b47de 981       this.last_selected = n;
ad827b 982       this.highlight_row(n, true, true);
6b47de 983     }
0e7b66 984     else {
eaacbe 985       $(this.rows[n].obj).removeClass('selected').removeClass('unfocused');
874717 986     }
6b47de 987   }
T 988
1094cd 989   // trigger event if selection changed
T 990   if (this.selection.join(',') != select_before)
cc97ea 991     this.triggerEvent('select');
1094cd 992
d7c226 993   this.focus();
A 994
1094cd 995   return true;
6b47de 996 },
T 997
998
999 /**
528185 1000  * Invert selection
A 1001  */
1002 invert_selection: function()
1003 {
1004   if (!this.rows || !this.rows.length)
1005     return false;
1006
1007   // remember old selection
1633bc 1008   var n, select_before = this.selection.join(',');
8fa922 1009
1633bc 1010   for (n in this.rows)
f52c93 1011     this.highlight_row(n, true);
528185 1012
A 1013   // trigger event if selection changed
1014   if (this.selection.join(',') != select_before)
1015     this.triggerEvent('select');
1016
1017   this.focus();
1018
1019   return true;
1020 },
1021
1022
1023 /**
095d05 1024  * Unselect selected row(s)
6b47de 1025  */
095d05 1026 clear_selection: function(id)
6b47de 1027 {
1633bc 1028   var n, num_select = this.selection.length;
095d05 1029
A 1030   // one row
8fa922 1031   if (id) {
1633bc 1032     for (n in this.selection)
cc97ea 1033       if (this.selection[n] == id) {
T 1034         this.selection.splice(n,1);
1035         break;
1036       }
8fa922 1037   }
095d05 1038   // all rows
8fa922 1039   else {
1633bc 1040     for (n in this.selection)
cc97ea 1041       if (this.rows[this.selection[n]]) {
T 1042         $(this.rows[this.selection[n]].obj).removeClass('selected').removeClass('unfocused');
8fa922 1043       }
A 1044
1045     this.selection = [];
1046   }
6b47de 1047
095d05 1048   if (num_select && !this.selection.length)
cc97ea 1049     this.triggerEvent('select');
6b47de 1050 },
T 1051
1052
1053 /**
1054  * Getter for the selection array
1055  */
1056 get_selection: function()
1057 {
1058   return this.selection;
1059 },
1060
1061
1062 /**
1063  * Return the ID if only one row is selected
1064  */
1065 get_single_selection: function()
1066 {
1067   if (this.selection.length == 1)
1068     return this.selection[0];
1069   else
1070     return null;
1071 },
1072
1073
1074 /**
1075  * Highlight/unhighlight a row
1076  */
ad827b 1077 highlight_row: function(id, multiple, norecur)
6b47de 1078 {
2b55d4 1079   if (!this.rows[id])
AM 1080     return;
1081
1082   if (!multiple) {
8fa922 1083     if (this.selection.length > 1 || !this.in_selection(id)) {
df015d 1084       this.clear_selection();
T 1085       this.selection[0] = id;
cc97ea 1086       $(this.rows[id].obj).addClass('selected');
df015d 1087     }
6b47de 1088   }
2b55d4 1089   else {
8fa922 1090     if (!this.in_selection(id)) { // select row
2b55d4 1091       this.selection.push(id);
cc97ea 1092       $(this.rows[id].obj).addClass('selected');
ad827b 1093       if (!norecur && !this.rows[id].expanded)
2b55d4 1094         this.highlight_children(id, true);
6b47de 1095     }
8fa922 1096     else { // unselect row
1633bc 1097       var p = $.inArray(id, this.selection),
A 1098         a_pre = this.selection.slice(0, p),
1099         a_post = this.selection.slice(p+1, this.selection.length);
1100
6b47de 1101       this.selection = a_pre.concat(a_post);
cc97ea 1102       $(this.rows[id].obj).removeClass('selected').removeClass('unfocused');
ad827b 1103       if (!norecur && !this.rows[id].expanded)
2b55d4 1104         this.highlight_children(id, false);
6b47de 1105     }
2b55d4 1106   }
AM 1107 },
1108
1109
1110 /**
1111  * Highlight/unhighlight all childs of the given row
1112  */
1113 highlight_children: function(id, status)
1114 {
1115   var i, selected,
1116     children = this.row_children(id), len = children.length;
1117
1118   for (i=0; i<len; i++) {
1119     selected = this.in_selection(children[i]);
1120     if ((status && !selected) || (!status && selected))
ad827b 1121       this.highlight_row(children[i], true, true);
6b47de 1122   }
T 1123 },
1124
1125
1126 /**
1127  * Handler for keyboard events
1128  */
1129 key_press: function(e)
1130 {
ebee2a 1131   var target = e.target || {};
T 1132   if (this.focused != true || target.nodeName == 'INPUT' || target.nodeName == 'TEXTAREA' || target.nodeName == 'SELECT')
6b47de 1133     return true;
T 1134
1633bc 1135   var keyCode = rcube_event.get_keycode(e),
A 1136     mod_key = rcube_event.get_modifier(e);
91d1a1 1137
8fa922 1138   switch (keyCode) {
6b47de 1139     case 40:
699a25 1140     case 38:
26f5b0 1141     case 63233: // "down", in safari keypress
T 1142     case 63232: // "up", in safari keypress
1143       // Stop propagation so that the browser doesn't scroll
1144       rcube_event.cancel(e);
6b47de 1145       return this.use_arrow_key(keyCode, mod_key);
f52c93 1146     case 61:
T 1147     case 107: // Plus sign on a numeric keypad (fc11 + firefox 3.5.2)
1148     case 109:
1149     case 32:
1150       // Stop propagation
1151       rcube_event.cancel(e);
1152       var ret = this.use_plusminus_key(keyCode, mod_key);
1153       this.key_pressed = keyCode;
699a25 1154       this.modkey = mod_key;
f52c93 1155       this.triggerEvent('keypress');
699a25 1156       this.modkey = 0;
f52c93 1157       return ret;
bc2acc 1158     case 36: // Home
A 1159       this.select_first(mod_key);
1160       return rcube_event.cancel(e);
1161     case 35: // End
1162       this.select_last(mod_key);
1163       return rcube_event.cancel(e);
17a8fb 1164     case 27:
AM 1165       if (this.drag_active)
1166         return this.drag_mouse_up(e);
1167       if (this.col_drag_active) {
1168         this.selected_column = null;
1169         return this.column_drag_mouse_up(e);
1170       }
1171       return rcube_event.cancel(e);
6b47de 1172     default:
T 1173       this.key_pressed = keyCode;
699a25 1174       this.modkey = mod_key;
cc97ea 1175       this.triggerEvent('keypress');
699a25 1176       this.modkey = 0;
8fa922 1177
f89f03 1178       if (this.key_pressed == this.BACKSPACE_KEY)
6e6e89 1179         return rcube_event.cancel(e);
9e7a1b 1180   }
8fa922 1181
9e7a1b 1182   return true;
T 1183 },
1184
6b47de 1185
T 1186 /**
1187  * Special handling method for arrow keys
1188  */
1189 use_arrow_key: function(keyCode, mod_key)
1190 {
1191   var new_row;
e9b57b 1192   // Safari uses the nonstandard keycodes 63232/63233 for up/down, if we're
A 1193   // using the keypress event (but not the keydown or keyup event).
1194   if (keyCode == 40 || keyCode == 63233) // down arrow key pressed
6b47de 1195     new_row = this.get_next_row();
e9b57b 1196   else if (keyCode == 38 || keyCode == 63232) // up arrow key pressed
6b47de 1197     new_row = this.get_prev_row();
T 1198
8fa922 1199   if (new_row) {
699a25 1200     this.select_row(new_row.uid, mod_key, false);
6b47de 1201     this.scrollto(new_row.uid);
T 1202   }
f52c93 1203
T 1204   return false;
1205 },
1206
1207
1208 /**
1209  * Special handling method for +/- keys
1210  */
1211 use_plusminus_key: function(keyCode, mod_key)
1212 {
1213   var selected_row = this.rows[this.last_selected];
1214   if (!selected_row)
1215     return;
1216
1217   if (keyCode == 32)
1218     keyCode = selected_row.expanded ? 109 : 61;
1219   if (keyCode == 61 || keyCode == 107)
1220     if (mod_key == CONTROL_KEY || this.multiexpand)
1221       this.expand_all(selected_row);
1222     else
1223      this.expand(selected_row);
1224   else
1225     if (mod_key == CONTROL_KEY || this.multiexpand)
1226       this.collapse_all(selected_row);
1227     else
1228       this.collapse(selected_row);
1229
403b45 1230   this.update_expando(selected_row.uid, selected_row.expanded);
6b47de 1231
T 1232   return false;
1233 },
1234
1235
1236 /**
1237  * Try to scroll the list to make the specified row visible
1238  */
1239 scrollto: function(id)
1240 {
1241   var row = this.rows[id].obj;
8fa922 1242   if (row && this.frame) {
6b47de 1243     var scroll_to = Number(row.offsetTop);
T 1244
bc2acc 1245     // expand thread if target row is hidden (collapsed)
A 1246     if (!scroll_to && this.rows[id].parent_uid) {
1247       var parent = this.find_root(this.rows[id].uid);
1248       this.expand_all(this.rows[parent]);
1249       scroll_to = Number(row.offsetTop);
1250     }
1251
6b47de 1252     if (scroll_to < Number(this.frame.scrollTop))
T 1253       this.frame.scrollTop = scroll_to;
1254     else if (scroll_to + Number(row.offsetHeight) > Number(this.frame.scrollTop) + Number(this.frame.offsetHeight))
1255       this.frame.scrollTop = (scroll_to + Number(row.offsetHeight)) - Number(this.frame.offsetHeight);
1256   }
1257 },
1258
1259
1260 /**
1261  * Handler for mouse move events
1262  */
1263 drag_mouse_move: function(e)
1264 {
8ef2f3 1265   // convert touch event
T 1266   if (e.type == 'touchmove') {
dc8400 1267     if (e.touches.length == 1 && e.changedTouches.length == 1)
8ef2f3 1268       e = rcube_event.touchevent(e.changedTouches[0]);
T 1269     else
1270       return rcube_event.cancel(e);
1271   }
2b55d4 1272
8fa922 1273   if (this.drag_start) {
6b47de 1274     // check mouse movement, of less than 3 pixels, don't start dragging
T 1275     var m = rcube_event.get_mouse_pos(e);
cf6bc5 1276
6b47de 1277     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))
T 1278       return false;
8fa922 1279
6b47de 1280     if (!this.draglayer)
b62c48 1281       this.draglayer = $('<div>').attr('id', 'rcmdraglayer')
A 1282         .css({ position:'absolute', display:'none', 'z-index':2000 })
1283         .appendTo(document.body);
8fa922 1284
2ecb7f 1285     // also select childs of (collapsed) threads for dragging
0e7b66 1286     var n, uid, selection = $.merge([], this.selection);
A 1287     for (n in selection) {
2ecb7f 1288       uid = selection[n];
2b55d4 1289       if (!this.rows[uid].expanded)
AM 1290         this.select_children(uid);
2ecb7f 1291     }
a80304 1292
74cd6c 1293     // reset content
A 1294     this.draglayer.html('');
1295
f52c93 1296     // get subjects of selected messages
517dae 1297     var i, n, obj, me;
74cd6c 1298     for (n=0; n<this.selection.length; n++) {
8fa922 1299       // only show 12 lines
A 1300       if (n>12) {
74cd6c 1301         this.draglayer.append('...');
6b47de 1302         break;
T 1303       }
1304
517dae 1305       me = this;
8fa922 1306       if (obj = this.rows[this.selection[n]].obj) {
517dae 1307         $('> '+this.col_tagname(), obj).each(function(i,elem){
TB 1308           if (n == 0)
1309             me.drag_start_pos = $(elem).offset();
f52c93 1310
517dae 1311           if (me.subject_col < 0 || (me.subject_col >= 0 && me.subject_col == i)) {
TB 1312             var subject = $(elem).text();
8fa922 1313
517dae 1314             if (subject) {
4383e0 1315               // remove leading spaces
ef17c5 1316               subject = $.trim(subject);
451637 1317               // truncate line to 50 characters
74cd6c 1318               subject = (subject.length > 50 ? subject.substring(0, 50) + '...' : subject);
A 1319
f41edf 1320               var entry = $('<div>').text(subject);
517dae 1321               me.draglayer.append(entry);
d24d20 1322             }
517dae 1323
TB 1324             return false;  // break
6b47de 1325           }
517dae 1326         });
6b47de 1327       }
T 1328     }
1329
cc97ea 1330     this.draglayer.show();
6b47de 1331     this.drag_active = true;
cc97ea 1332     this.triggerEvent('dragstart');
6b47de 1333   }
T 1334
8fa922 1335   if (this.drag_active && this.draglayer) {
6b47de 1336     var pos = rcube_event.get_mouse_pos(e);
cc97ea 1337     this.draglayer.css({ left:(pos.x+20)+'px', top:(pos.y-5 + (bw.ie ? document.documentElement.scrollTop : 0))+'px' });
9489ad 1338     this.triggerEvent('dragmove', e?e:window.event);
6b47de 1339   }
T 1340
1341   this.drag_start = false;
1342
1343   return false;
1344 },
1345
1346
1347 /**
1348  * Handler for mouse up events
1349  */
1350 drag_mouse_up: function(e)
1351 {
1352   document.onmousemove = null;
1257dd 1353
8ef2f3 1354   if (e.type == 'touchend') {
T 1355     if (e.changedTouches.length != 1)
1356       return rcube_event.cancel(e);
1357   }
6b47de 1358
cc97ea 1359   if (this.draglayer && this.draglayer.is(':visible')) {
T 1360     if (this.drag_start_pos)
1361       this.draglayer.animate(this.drag_start_pos, 300, 'swing').hide(20);
1362     else
1363       this.draglayer.hide();
1364   }
6b47de 1365
da8f11 1366   if (this.drag_active)
A 1367     this.focus();
6b47de 1368   this.drag_active = false;
6c11ee 1369
A 1370   rcube_event.remove_listener({event:'mousemove', object:this, method:'drag_mouse_move'});
1371   rcube_event.remove_listener({event:'mouseup', object:this, method:'drag_mouse_up'});
1257dd 1372
4910b0 1373   if (bw.touch) {
8ef2f3 1374     rcube_event.remove_listener({event:'touchmove', object:this, method:'drag_mouse_move'});
T 1375     rcube_event.remove_listener({event:'touchend', object:this, method:'drag_mouse_up'});
1376   }
6c11ee 1377
A 1378   // remove temp divs
b62c48 1379   this.del_dragfix();
6c11ee 1380
76a98d 1381   this.triggerEvent('dragend', e);
da8f11 1382
6b47de 1383   return rcube_event.cancel(e);
c4b819 1384 },
A 1385
1386
1387 /**
b62c48 1388  * Handler for mouse move events for dragging list column
A 1389  */
1390 column_drag_mouse_move: function(e)
1391 {
1392   if (this.drag_start) {
1393     // check mouse movement, of less than 3 pixels, don't start dragging
1394     var i, m = rcube_event.get_mouse_pos(e);
1395
1396     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))
1397       return false;
1398
1399     if (!this.col_draglayer) {
1400       var lpos = $(this.list).offset(),
517dae 1401         cells = this.thead.rows[0].cells;
b62c48 1402
A 1403       // create dragging layer
1404       this.col_draglayer = $('<div>').attr('id', 'rcmcoldraglayer')
1405         .css(lpos).css({ position:'absolute', 'z-index':2001,
1406            'background-color':'white', opacity:0.75,
1407            height: (this.frame.offsetHeight-2)+'px', width: (this.frame.offsetWidth-2)+'px' })
1408         .appendTo(document.body)
1409         // ... and column position indicator
1410        .append($('<div>').attr('id', 'rcmcolumnindicator')
1411           .css({ position:'absolute', 'border-right':'2px dotted #555', 
1412           'z-index':2002, height: (this.frame.offsetHeight-2)+'px' }));
1413
1414       this.cols = [];
1415       this.list_pos = this.list_min_pos = lpos.left;
1416       // save columns positions
1417       for (i=0; i<cells.length; i++) {
1418         this.cols[i] = cells[i].offsetWidth;
1419         if (this.column_fixed !== null && i <= this.column_fixed) {
1420           this.list_min_pos += this.cols[i];
1421         }
1422       }
1423     }
1424
1425     this.col_draglayer.show();
1426     this.col_drag_active = true;
1427     this.triggerEvent('column_dragstart');
1428   }
1429
1430   // set column indicator position
1431   if (this.col_drag_active && this.col_draglayer) {
1432     var i, cpos = 0, pos = rcube_event.get_mouse_pos(e);
1433
1434     for (i=0; i<this.cols.length; i++) {
1435       if (pos.x >= this.cols[i]/2 + this.list_pos + cpos)
1436         cpos += this.cols[i];
1437       else
1438         break;
1439     }
1440
1441     // handle fixed columns on left
1442     if (i == 0 && this.list_min_pos > pos.x)
1443       cpos = this.list_min_pos - this.list_pos;
1444     // empty list needs some assignment
1445     else if (!this.list.rowcount && i == this.cols.length)
1446       cpos -= 2;
1447     $('#rcmcolumnindicator').css({ width: cpos+'px'});
1448     this.triggerEvent('column_dragmove', e?e:window.event);
1449   }
1450
1451   this.drag_start = false;
1452
1453   return false;
1454 },
1455
1456
1457 /**
1458  * Handler for mouse up events for dragging list columns
1459  */
1460 column_drag_mouse_up: function(e)
1461 {
1462   document.onmousemove = null;
1463
1464   if (this.col_draglayer) {
1465     (this.col_draglayer).remove();
1466     this.col_draglayer = null;
1467   }
1468
1469   if (this.col_drag_active)
1470     this.focus();
1471   this.col_drag_active = false;
1472
1473   rcube_event.remove_listener({event:'mousemove', object:this, method:'column_drag_mouse_move'});
1474   rcube_event.remove_listener({event:'mouseup', object:this, method:'column_drag_mouse_up'});
1475   // remove temp divs
1476   this.del_dragfix();
1477
1478   if (this.selected_column !== null && this.cols && this.cols.length) {
1479     var i, cpos = 0, pos = rcube_event.get_mouse_pos(e);
1480
1481     // find destination position
1482     for (i=0; i<this.cols.length; i++) {
1483       if (pos.x >= this.cols[i]/2 + this.list_pos + cpos)
1484         cpos += this.cols[i];
1485       else
1486         break;
1487     }
1488
1489     if (i != this.selected_column && i != this.selected_column+1) {
1490       this.column_replace(this.selected_column, i);
1491     }
1492   }
1493
76a98d 1494   this.triggerEvent('column_dragend', e);
b62c48 1495
A 1496   return rcube_event.cancel(e);
1497 },
1498
1499
1500 /**
2b55d4 1501  * Returns IDs of all rows in a thread (except root) for specified root
AM 1502  */
1503 row_children: function(uid)
1504 {
1505   if (!this.rows[uid] || !this.rows[uid].has_children)
1506     return [];
1507
1508   var res = [], depth = this.rows[uid].depth,
1509     row = this.rows[uid].obj.nextSibling;
1510
1511   while (row) {
1512     if (row.nodeType == 1) {
1513       if ((r = this.rows[row.uid])) {
1514         if (!r.depth || r.depth <= depth)
1515           break;
1516         res.push(r.uid);
1517       }
1518     }
1519     row = row.nextSibling;
1520   }
1521
1522   return res;
1523 },
1524
1525
1526 /**
b62c48 1527  * Creates a layer for drag&drop over iframes
A 1528  */
1529 add_dragfix: function()
1530 {
1531   $('iframe').each(function() {
1532     $('<div class="iframe-dragdrop-fix"></div>')
1533       .css({background: '#fff',
1534         width: this.offsetWidth+'px', height: this.offsetHeight+'px',
1535         position: 'absolute', opacity: '0.001', zIndex: 1000
1536       })
1537       .css($(this).offset())
1538       .appendTo(document.body);
677e1f 1539   });
b62c48 1540 },
A 1541
1542
1543 /**
1544  * Removes the layer for drag&drop over iframes
1545  */
1546 del_dragfix: function()
1547 {
1548   $('div.iframe-dragdrop-fix').each(function() { this.parentNode.removeChild(this); });
1549 },
1550
1551
1552 /**
1553  * Replaces two columns
1554  */
1555 column_replace: function(from, to)
1556 {
517dae 1557   // only supported for <table> lists
TB 1558   if (!this.thead || !this.thead.rows)
1559     return;
1560
1561   var len, cells = this.thead.rows[0].cells,
b62c48 1562     elem = cells[from],
A 1563     before = cells[to],
1564     td = document.createElement('td');
1565
1566   // replace header cells
1567   if (before)
1568     cells[0].parentNode.insertBefore(td, before);
1569   else
1570     cells[0].parentNode.appendChild(td);
1571   cells[0].parentNode.replaceChild(elem, td);
1572
1573   // replace list cells
517dae 1574   for (r=0, len=this.tbody.rows.length; r<len; r++) {
TB 1575     row = this.tbody.rows[r];
b62c48 1576
A 1577     elem = row.cells[from];
1578     before = row.cells[to];
1579     td = document.createElement('td');
1580
1581     if (before)
1582       row.insertBefore(td, before);
1583     else
1584       row.appendChild(td);
1585     row.replaceChild(elem, td);
1586   }
1587
1588   // update subject column position
1589   if (this.subject_col == from)
1590     this.subject_col = to > from ? to - 1 : to;
8e32dc 1591   else if (this.subject_col < from && to <= this.subject_col)
T 1592     this.subject_col++;
1593   else if (this.subject_col > from && to >= this.subject_col)
1594     this.subject_col--;
b62c48 1595
73ad4f 1596   if (this.fixed_header)
TB 1597     this.init_header();
1598
b62c48 1599   this.triggerEvent('column_replace');
6b47de 1600 }
T 1601
1602 };
1603
cc97ea 1604 rcube_list_widget.prototype.addEventListener = rcube_event_engine.prototype.addEventListener;
T 1605 rcube_list_widget.prototype.removeEventListener = rcube_event_engine.prototype.removeEventListener;
1606 rcube_list_widget.prototype.triggerEvent = rcube_event_engine.prototype.triggerEvent;