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