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