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