]> git.donarmstrong.com Git - roundcube.git/blob - program/js/list.js
Imported Upstream version 0.2.1
[roundcube.git] / program / js / list.js
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 2277 2009-02-06 14:42:39Z 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   
37   this.subject_col = -1;
38   this.shiftkey = false;
39   this.multiselect = false;
40   this.multi_selecting = false;
41   this.draggable = false;
42   this.keyboard = false;
43   this.toggleselect = false;
44   
45   this.dont_select = false;
46   this.drag_active = false;
47   this.last_selected = 0;
48   this.shift_start = 0;
49   this.in_selection_before = false;
50   this.focused = false;
51   this.drag_mouse_start = null;
52   this.dblclick_time = 600;
53   this.row_init = function(){};
54   this.events = { click:[], dblclick:[], select:[], keypress:[], dragstart:[], dragmove:[], dragend:[] };
55   
56   // overwrite default paramaters
57   if (p && typeof(p)=='object')
58     for (var n in p)
59       this[n] = p[n];
60   }
61
62
63 rcube_list_widget.prototype = {
64
65
66 /**
67  * get all message rows from HTML table and init each row
68  */
69 init: function()
70 {
71   if (this.list && this.list.tBodies[0])
72   {
73     this.rows = new Array();
74     this.rowcount = 0;
75
76     var row;
77     for(var r=0; r<this.list.tBodies[0].childNodes.length; r++)
78     {
79       row = this.list.tBodies[0].childNodes[r];
80       while (row && (row.nodeType != 1 || row.style.display == 'none'))
81       {
82         row = row.nextSibling;
83         r++;
84       }
85
86       this.init_row(row);
87       this.rowcount++;
88     }
89
90     this.frame = this.list.parentNode;
91
92     // set body events
93     if (this.keyboard) {
94       rcube_event.add_listener({element:document, event:'keyup', object:this, method:'key_press'});
95       rcube_event.add_listener({element:document, event:'keydown', object:this, method:'key_down'});
96     }
97   }
98 },
99
100
101 /**
102  *
103  */
104 init_row: function(row)
105 {
106   // make references in internal array and set event handlers
107   if (row && String(row.id).match(/rcmrow([a-z0-9\-_=]+)/i))
108   {
109     var p = this;
110     var uid = RegExp.$1;
111     row.uid = uid;
112     this.rows[uid] = {uid:uid, id:row.id, obj:row, classname:row.className};
113
114     // set eventhandlers to table row
115     row.onmousedown = function(e){ return p.drag_row(e, this.uid); };
116     row.onmouseup = function(e){ return p.click_row(e, this.uid); };
117
118     if (document.all)
119       row.onselectstart = function() { return false; };
120
121     this.row_init(this.rows[uid]);
122   }
123 },
124
125
126 /**
127  *
128  */
129 clear: function(sel)
130 {
131   var tbody = document.createElement('TBODY');
132   this.list.insertBefore(tbody, this.list.tBodies[0]);
133   this.list.removeChild(this.list.tBodies[1]);
134   this.rows = new Array();
135   this.rowcount = 0;
136   
137   if (sel) this.clear_selection();
138 },
139
140
141 /**
142  * 'remove' message row from list (just hide it)
143  */
144 remove_row: function(uid, sel_next)
145 {
146   if (this.rows[uid].obj)
147     this.rows[uid].obj.style.display = 'none';
148
149   if (sel_next)
150     this.select_next();
151
152   this.rows[uid] = null;
153   this.rowcount--;
154 },
155
156
157 /**
158  *
159  */
160 insert_row: function(row, attop)
161 {
162   var tbody = this.list.tBodies[0];
163
164   if (attop && tbody.rows.length)
165     tbody.insertBefore(row, tbody.firstChild);
166   else
167     tbody.appendChild(row);
168
169   this.init_row(row);
170   this.rowcount++;
171 },
172
173
174
175 /**
176  * Set focus to the list
177  */
178 focus: function(e)
179 {
180   this.focused = true;
181   for (var n=0; n<this.selection.length; n++)
182   {
183     id = this.selection[n];
184     if (this.rows[id] && this.rows[id].obj)
185     {
186       this.set_classname(this.rows[id].obj, 'selected', true);
187       this.set_classname(this.rows[id].obj, 'unfocused', false);
188     }
189   }
190
191   if (e || (e = window.event))
192     rcube_event.cancel(e);
193 },
194
195
196 /**
197  * remove focus from the list
198  */
199 blur: function()
200 {
201   var id;
202   this.focused = false;
203   for (var n=0; n<this.selection.length; n++)
204   {
205     id = this.selection[n];
206     if (this.rows[id] && this.rows[id].obj)
207     {
208       this.set_classname(this.rows[id].obj, 'selected', false);
209       this.set_classname(this.rows[id].obj, 'unfocused', true);
210     }
211   }
212 },
213
214
215 /**
216  * onmousedown-handler of message list row
217  */
218 drag_row: function(e, id)
219 {
220   // don't do anything (another action processed before)
221   var evtarget = rcube_event.get_target(e);
222   if (this.dont_select || (evtarget && (evtarget.tagName == 'INPUT' || evtarget.tagName == 'IMG')))
223     return false;
224     
225   // accept right-clicks
226   if (rcube_event.get_button(e) == 2)
227     return true;
228   
229   this.in_selection_before = this.in_selection(id) ? id : false;
230
231   // selects currently unselected row
232   if (!this.in_selection_before)
233   {
234     var mod_key = rcube_event.get_modifier(e);
235     this.select_row(id, mod_key, false);
236   }
237
238   if (this.draggable && this.selection.length)
239   {
240     this.drag_start = true;
241     this.drag_mouse_start = rcube_event.get_mouse_pos(e);
242     rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'drag_mouse_move'});
243     rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'drag_mouse_up'});
244
245     // add listener for iframes
246     var iframes = document.getElementsByTagName('IFRAME');
247     this.iframe_events = Object();
248     for (var n in iframes)
249     {
250       var iframedoc = null;
251       if (iframes[n].contentDocument)
252         iframedoc = iframes[n].contentDocument;
253       else if (iframes[n].contentWindow)
254         iframedoc = iframes[n].contentWindow.document;
255       else if (iframes[n].document)
256         iframedoc = iframes[n].document;
257
258       if (iframedoc)
259       {
260         var list = this;
261         var pos = rcube_get_object_pos(document.getElementById(iframes[n].id));
262         this.iframe_events[n] = function(e) { e._offset = pos; return list.drag_mouse_move(e); }
263         
264         if (iframedoc.addEventListener)
265           iframedoc.addEventListener('mousemove', this.iframe_events[n], false);
266         else if (iframes[n].attachEvent)
267           iframedoc.attachEvent('onmousemove', this.iframe_events[n]);
268         else
269           iframedoc['onmousemove'] = this.iframe_events[n];
270
271         rcube_event.add_listener({element:iframedoc, event:'mouseup', object:this, method:'drag_mouse_up'});
272       }
273     }                                                             
274   }
275
276   return false;
277 },
278
279
280 /**
281  * onmouseup-handler of message list row
282  */
283 click_row: function(e, id)
284 {
285   var now = new Date().getTime();
286   var mod_key = rcube_event.get_modifier(e);
287   var evtarget = rcube_event.get_target(e);
288   
289   if ((evtarget && (evtarget.tagName == 'INPUT' || evtarget.tagName == 'IMG')))
290     return false;
291   
292   // don't do anything (another action processed before)
293   if (this.dont_select)
294     {
295     this.dont_select = false;
296     return false;
297     }
298     
299   var dblclicked = now - this.rows[id].clicked < this.dblclick_time;
300
301   // unselects currently selected row
302   if (!this.drag_active && this.in_selection_before == id && !dblclicked)
303     this.select_row(id, mod_key, false);
304
305   this.drag_start = false;
306   this.in_selection_before = false;
307
308   // row was double clicked
309   if (this.rows && dblclicked && this.in_selection(id))
310     this.trigger_event('dblclick');
311   else
312     this.trigger_event('click');
313
314   if (!this.drag_active)
315     rcube_event.cancel(e);
316
317   this.rows[id].clicked = now;
318   return false;
319 },
320
321
322 /**
323  * get next/previous/last rows that are not hidden
324  */
325 get_next_row: function()
326 {
327   if (!this.rows)
328     return false;
329
330   var last_selected_row = this.rows[this.last_selected];
331   var new_row = last_selected_row ? last_selected_row.obj.nextSibling : null;
332   while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
333     new_row = new_row.nextSibling;
334
335   return new_row;
336 },
337
338 get_prev_row: function()
339 {
340   if (!this.rows)
341     return false;
342
343   var last_selected_row = this.rows[this.last_selected];
344   var new_row = last_selected_row ? last_selected_row.obj.previousSibling : null;
345   while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
346     new_row = new_row.previousSibling;
347
348   return new_row;
349 },
350
351 get_last_row: function()
352 {
353   if (this.rowcount)
354     {
355     var rows = this.list.tBodies[0].rows;
356
357     for(var i=rows.length-1; i>=0; i--)
358       if(rows[i].id && String(rows[i].id).match(/rcmrow([a-z0-9\-_=]+)/i) && this.rows[RegExp.$1] != null)
359         return RegExp.$1;
360     }
361
362   return null;
363 },
364
365
366 /**
367  * selects or unselects the proper row depending on the modifier key pressed
368  */
369 select_row: function(id, mod_key, with_mouse)
370 {
371   var select_before = this.selection.join(',');
372   if (!this.multiselect)
373     mod_key = 0;
374     
375   if (!this.shift_start)
376     this.shift_start = id
377
378   if (!mod_key)
379   {
380     this.shift_start = id;
381     this.highlight_row(id, false);
382     this.multi_selecting = false;
383   }
384   else
385   {
386     switch (mod_key)
387     {
388       case SHIFT_KEY:
389         this.shift_select(id, false);
390         break;
391
392       case CONTROL_KEY:
393         if (!with_mouse)
394           this.highlight_row(id, true);
395         break; 
396
397       case CONTROL_SHIFT_KEY:
398         this.shift_select(id, true);
399         break;
400
401       default:
402         this.highlight_row(id, false);
403         break;
404     }
405     this.multi_selecting = true;
406   }
407
408   // trigger event if selection changed
409   if (this.selection.join(',') != select_before)
410     this.trigger_event('select');
411
412   if (this.last_selected != 0 && this.rows[this.last_selected])
413     this.set_classname(this.rows[this.last_selected].obj, 'focused', false);
414
415   // unselect if toggleselect is active and the same row was clicked again
416   if (this.toggleselect && this.last_selected == id)
417   {
418     this.clear_selection();
419     id = null;
420   }
421   else
422     this.set_classname(this.rows[id].obj, 'focused', true);
423
424   if (!this.selection.length)
425     this.shift_start = null;
426
427   this.last_selected = id;
428 },
429
430
431 /**
432  * Alias method for select_row
433  */
434 select: function(id)
435 {
436   this.select_row(id, false);
437   this.scrollto(id);
438 },
439
440
441 /**
442  * Select row next to the last selected one.
443  * Either below or above.
444  */
445 select_next: function()
446 {
447   var next_row = this.get_next_row();
448   var prev_row = this.get_prev_row();
449   var new_row = (next_row) ? next_row : prev_row;
450   if (new_row)
451     this.select_row(new_row.uid, false, false);  
452 },
453
454
455 /**
456  * Perform selection when shift key is pressed
457  */
458 shift_select: function(id, control)
459 {
460   if (!this.rows[this.shift_start] || !this.selection.length)
461     this.shift_start = id;
462
463   var from_rowIndex = this.rows[this.shift_start].obj.rowIndex;
464   var to_rowIndex = this.rows[id].obj.rowIndex;
465
466   var i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex);
467   var j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
468
469   // iterate through the entire message list
470   for (var n in this.rows)
471   {
472     if ((this.rows[n].obj.rowIndex >= i) && (this.rows[n].obj.rowIndex <= j))
473     {
474       if (!this.in_selection(n))
475         this.highlight_row(n, true);
476     }
477     else
478     {
479       if  (this.in_selection(n) && !control)
480         this.highlight_row(n, true);
481     }
482   }
483 },
484
485
486 /**
487  * Check if given id is part of the current selection
488  */
489 in_selection: function(id)
490 {
491   for(var n in this.selection)
492     if (this.selection[n]==id)
493       return true;
494
495   return false;    
496 },
497
498
499 /**
500  * Select each row in list
501  */
502 select_all: function(filter)
503 {
504   if (!this.rows || !this.rows.length)
505     return false;
506
507   // reset but remember selection first
508   var select_before = this.selection.join(',');
509   this.selection = new Array();
510   
511   for (var n in this.rows)
512   {
513     if (!filter || (this.rows[n] && this.rows[n][filter] == true))
514     {
515       this.last_selected = n;
516       this.highlight_row(n, true);
517     }
518     else if (this.rows[n])
519     {
520       this.set_classname(this.rows[n].obj, 'selected', false);
521       this.set_classname(this.rows[n].obj, 'unfocused', false);
522     }
523   }
524
525   // trigger event if selection changed
526   if (this.selection.join(',') != select_before)
527     this.trigger_event('select');
528
529   this.focus();
530
531   return true;
532 },
533
534
535 /**
536  * Unselect selected row(s)
537  */
538 clear_selection: function(id)
539 {
540   var num_select = this.selection.length;
541
542   // one row
543   if (id)
544     {
545     for (var n=0; n<this.selection.length; n++)
546       if (this.selection[n] == id)
547         {
548         this.selection.splice(n,1);
549         break;
550         }
551     }
552   // all rows
553   else
554     {
555     for (var n=0; n<this.selection.length; n++)
556       if (this.rows[this.selection[n]])
557         {
558         this.set_classname(this.rows[this.selection[n]].obj, 'selected', false);
559         this.set_classname(this.rows[this.selection[n]].obj, 'unfocused', false);
560         }
561     
562     this.selection = new Array();
563     }
564
565   if (num_select && !this.selection.length)
566     this.trigger_event('select');
567 },
568
569
570 /**
571  * Getter for the selection array
572  */
573 get_selection: function()
574 {
575   return this.selection;
576 },
577
578
579 /**
580  * Return the ID if only one row is selected
581  */
582 get_single_selection: function()
583 {
584   if (this.selection.length == 1)
585     return this.selection[0];
586   else
587     return null;
588 },
589
590
591 /**
592  * Highlight/unhighlight a row
593  */
594 highlight_row: function(id, multiple)
595 {
596   if (this.rows[id] && !multiple)
597   {
598     if (this.selection.length > 1 || !this.in_selection(id))
599     {
600       this.clear_selection();
601       this.selection[0] = id;
602       this.set_classname(this.rows[id].obj, 'selected', true);
603     }
604   }
605   else if (this.rows[id])
606   {
607     if (!this.in_selection(id))  // select row
608     {
609       this.selection[this.selection.length] = id;
610       this.set_classname(this.rows[id].obj, 'selected', true);
611     }
612     else  // unselect row
613     {
614       var p = find_in_array(id, this.selection);
615       var a_pre = this.selection.slice(0, p);
616       var a_post = this.selection.slice(p+1, this.selection.length);
617       this.selection = a_pre.concat(a_post);
618       this.set_classname(this.rows[id].obj, 'selected', false);
619       this.set_classname(this.rows[id].obj, 'unfocused', false);
620     }
621   }
622 },
623
624
625 /**
626  * Handler for keyboard events
627  */
628 key_press: function(e)
629 {
630   if (this.focused != true)
631     return true;
632
633   var keyCode = rcube_event.get_keycode(e);
634   var mod_key = rcube_event.get_modifier(e);
635   switch (keyCode)
636   {
637     case 40:
638     case 38: 
639     case 63233: // "down", in safari keypress
640     case 63232: // "up", in safari keypress
641       // Stop propagation so that the browser doesn't scroll
642       rcube_event.cancel(e);
643       return this.use_arrow_key(keyCode, mod_key);
644     default:
645       this.shiftkey = e.shiftKey;
646       this.key_pressed = keyCode;
647       this.trigger_event('keypress');
648       
649       if (this.key_pressed == this.BACKSPACE_KEY)
650         return rcube_event.cancel(e);
651   }
652   
653   return true;
654 },
655
656 /**
657  * Handler for keydown events
658  */
659 key_down: function(e)
660 {
661   switch (rcube_event.get_keycode(e))
662   {
663     case 40:
664     case 38: 
665     case 63233:
666     case 63232:
667       if (!rcube_event.get_modifier(e) && this.focused)
668         return rcube_event.cancel(e);
669         
670     default:
671   }
672   
673   return true;
674 },
675
676
677 /**
678  * Special handling method for arrow keys
679  */
680 use_arrow_key: function(keyCode, mod_key)
681 {
682   var new_row;
683   // Safari uses the nonstandard keycodes 63232/63233 for up/down, if we're
684   // using the keypress event (but not the keydown or keyup event).
685   if (keyCode == 40 || keyCode == 63233) // down arrow key pressed
686     new_row = this.get_next_row();
687   else if (keyCode == 38 || keyCode == 63232) // up arrow key pressed
688     new_row = this.get_prev_row();
689
690   if (new_row)
691   {
692     this.select_row(new_row.uid, mod_key, true);
693     this.scrollto(new_row.uid);
694   }
695
696   return false;
697 },
698
699
700 /**
701  * Try to scroll the list to make the specified row visible
702  */
703 scrollto: function(id)
704 {
705   var row = this.rows[id].obj;
706   if (row && this.frame)
707   {
708     var scroll_to = Number(row.offsetTop);
709
710     if (scroll_to < Number(this.frame.scrollTop))
711       this.frame.scrollTop = scroll_to;
712     else if (scroll_to + Number(row.offsetHeight) > Number(this.frame.scrollTop) + Number(this.frame.offsetHeight))
713       this.frame.scrollTop = (scroll_to + Number(row.offsetHeight)) - Number(this.frame.offsetHeight);
714   }
715 },
716
717
718 /**
719  * Handler for mouse move events
720  */
721 drag_mouse_move: function(e)
722 {
723   if (this.drag_start)
724   {
725     // check mouse movement, of less than 3 pixels, don't start dragging
726     var m = rcube_event.get_mouse_pos(e);
727
728     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))
729       return false;
730   
731     if (!this.draglayer)
732       this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, vis:0, zindex:2000});
733   
734     // get subjects of selectedd messages
735     var names = '';
736     var c, i, node, subject, obj;
737     for(var n=0; n<this.selection.length; n++)
738     {
739       if (n>12)  // only show 12 lines
740       {
741         names += '...';
742         break;
743       }
744
745       if (this.rows[this.selection[n]].obj)
746       {
747         obj = this.rows[this.selection[n]].obj;
748         subject = '';
749
750         for(c=0, i=0; i<obj.childNodes.length; i++)
751         {
752           if (obj.childNodes[i].nodeName == 'TD')
753           {
754             if (((node = obj.childNodes[i].firstChild) && (node.nodeType==3 || node.nodeName=='A')) &&
755               (this.subject_col < 0 || (this.subject_col >= 0 && this.subject_col == c)))
756             {
757               subject = node.nodeType==3 ? node.data : node.innerHTML;
758               names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
759               break;
760             }
761             c++;
762           }
763         }
764       }
765     }
766
767     this.draglayer.write(names);
768     this.draglayer.show(1);
769
770     this.drag_active = true;
771     this.trigger_event('dragstart');
772   }
773
774   if (this.drag_active && this.draglayer)
775   {
776     var pos = rcube_event.get_mouse_pos(e);
777     this.draglayer.move(pos.x+20, pos.y-5);
778     this.trigger_event('dragmove', e);
779   }
780
781   this.drag_start = false;
782
783   return false;
784 },
785
786
787 /**
788  * Handler for mouse up events
789  */
790 drag_mouse_up: function(e)
791 {
792   document.onmousemove = null;
793
794   if (this.draglayer && this.draglayer.visible)
795     this.draglayer.show(0);
796
797   this.drag_active = false;
798   this.trigger_event('dragend');
799
800   rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'drag_mouse_move'});
801   rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'drag_mouse_up'});
802
803   var iframes = document.getElementsByTagName('IFRAME');
804   for (var n in iframes) {
805     var iframedoc;
806     
807     if (iframes[n].contentDocument)
808       iframedoc = iframes[n].contentDocument;
809     else if (iframes[n].contentWindow)
810       iframedoc = iframes[n].contentWindow.document;
811     else if (iframes[n].document)
812       iframedoc = iframes[n].document;
813
814     if (iframedoc) {
815       if (this.iframe_events[n]) {
816         if (iframedoc.removeEventListener)
817           iframedoc.removeEventListener('mousemove', this.iframe_events[n], false);
818         else if (iframedoc.detachEvent)
819           iframedoc.detachEvent('onmousemove', this.iframe_events[n]);
820         else
821           iframedoc['onmousemove'] = null;
822         }
823       rcube_event.remove_listener({element:iframedoc, event:'mouseup', object:this, method:'drag_mouse_up'});
824       }
825     }
826
827   this.focus();
828   
829   return rcube_event.cancel(e);
830 },
831
832
833
834 /**
835  * set/unset a specific class name
836  */
837 set_classname: function(obj, classname, set)
838 {
839   var reg = new RegExp('\s*'+classname, 'i');
840   if (!set && obj.className.match(reg))
841     obj.className = obj.className.replace(reg, '');
842   else if (set && !obj.className.match(reg))
843     obj.className += ' '+classname;
844 },
845
846
847 /**
848  * Setter for object event handlers
849  *
850  * @param {String}   Event name
851  * @param {Function} Handler function
852  * @return Listener ID (used to remove this handler later on)
853  */
854 addEventListener: function(evt, handler)
855 {
856   if (this.events[evt]) {
857     var handle = this.events[evt].length;
858     this.events[evt][handle] = handler;
859     return handle;
860   }
861   else
862     return false;
863 },
864
865
866 /**
867  * Removes a specific event listener
868  *
869  * @param {String} Event name
870  * @param {Int}    Listener ID to remove
871  */
872 removeEventListener: function(evt, handle)
873 {
874   if (this.events[evt] && this.events[evt][handle])
875     this.events[evt][handle] = null;
876 },
877
878
879 /**
880  * This will execute all registered event handlers
881  * @private
882  */
883 trigger_event: function(evt, p)
884 {
885   if (this.events[evt] && this.events[evt].length) {
886     for (var i=0; i<this.events[evt].length; i++)
887       if (typeof(this.events[evt][i]) == 'function')
888         this.events[evt][i](this, p);
889   }
890 }
891
892
893 };
894