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