]> git.donarmstrong.com Git - roundcube.git/blob - program/js/list.js
Imported Upstream version 0.2~alpha
[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] && 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   if (!this.rows[this.shift_start] || !this.selection.length)
402     this.shift_start = id;
403
404   var from_rowIndex = this.rows[this.shift_start].obj.rowIndex;
405   var to_rowIndex = this.rows[id].obj.rowIndex;
406
407   var i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex);
408   var j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
409
410   // iterate through the entire message list
411   for (var n in this.rows)
412   {
413     if ((this.rows[n].obj.rowIndex >= i) && (this.rows[n].obj.rowIndex <= j))
414     {
415       if (!this.in_selection(n))
416         this.highlight_row(n, true);
417     }
418     else
419     {
420       if  (this.in_selection(n) && !control)
421         this.highlight_row(n, true);
422     }
423   }
424 },
425
426
427 /**
428  * Check if given id is part of the current selection
429  */
430 in_selection: function(id)
431 {
432   for(var n in this.selection)
433     if (this.selection[n]==id)
434       return true;
435
436   return false;    
437 },
438
439
440 /**
441  * Select each row in list
442  */
443 select_all: function(filter)
444 {
445   if (!this.rows || !this.rows.length)
446     return false;
447
448   // reset but remember selection first
449   var select_before = this.selection.join(',');
450   this.clear_selection();
451
452   for (var n in this.rows)
453   {
454     if (!filter || this.rows[n][filter]==true)
455     {
456       this.last_selected = n;
457       this.highlight_row(n, true);
458     }
459   }
460
461   // trigger event if selection changed
462   if (this.selection.join(',') != select_before)
463     this.trigger_event('select');
464
465   this.focus();
466
467   return true;
468 },
469
470
471 /**
472  * Unselect all selected rows
473  */
474 clear_selection: function()
475 {
476   var num_select = this.selection.length;
477   for (var n=0; n<this.selection.length; n++)
478     if (this.rows[this.selection[n]])
479     {
480       this.set_classname(this.rows[this.selection[n]].obj, 'selected', false);
481       this.set_classname(this.rows[this.selection[n]].obj, 'unfocused', false);
482     }
483
484   this.selection = new Array();
485   
486   if (num_select)
487     this.trigger_event('select');
488 },
489
490
491 /**
492  * Getter for the selection array
493  */
494 get_selection: function()
495 {
496   return this.selection;
497 },
498
499
500 /**
501  * Return the ID if only one row is selected
502  */
503 get_single_selection: function()
504 {
505   if (this.selection.length == 1)
506     return this.selection[0];
507   else
508     return null;
509 },
510
511
512 /**
513  * Highlight/unhighlight a row
514  */
515 highlight_row: function(id, multiple)
516 {
517   if (this.rows[id] && !multiple)
518   {
519     if (this.selection.length > 1 || !this.in_selection(id))
520     {
521       this.clear_selection();
522       this.selection[0] = id;
523       this.set_classname(this.rows[id].obj, 'selected', true);
524     }
525   }
526   else if (this.rows[id])
527   {
528     if (!this.in_selection(id))  // select row
529     {
530       this.selection[this.selection.length] = id;
531       this.set_classname(this.rows[id].obj, 'selected', true);
532     }
533     else  // unselect row
534     {
535       var p = find_in_array(id, this.selection);
536       var a_pre = this.selection.slice(0, p);
537       var a_post = this.selection.slice(p+1, this.selection.length);
538       this.selection = a_pre.concat(a_post);
539       this.set_classname(this.rows[id].obj, 'selected', false);
540       this.set_classname(this.rows[id].obj, 'unfocused', false);
541     }
542   }
543 },
544
545
546 /**
547  * Handler for keyboard events
548  */
549 key_press: function(e)
550 {
551   if (this.focused != true) 
552     return true;
553
554   var keyCode = document.layers ? e.which : document.all ? event.keyCode : document.getElementById ? e.keyCode : 0;
555   var mod_key = rcube_event.get_modifier(e);
556   switch (keyCode)
557   {
558     case 40:
559     case 38: 
560       return this.use_arrow_key(keyCode, mod_key);
561       break;
562
563     default:
564       this.shiftkey = e.shiftKey;
565       this.key_pressed = keyCode;
566       this.trigger_event('keypress');
567   }
568   
569   return true;
570 },
571
572
573 /**
574  * Special handling method for arrow keys
575  */
576 use_arrow_key: function(keyCode, mod_key)
577 {
578   var new_row;
579   if (keyCode == 40) // down arrow key pressed
580     new_row = this.get_next_row();
581   else if (keyCode == 38) // up arrow key pressed
582     new_row = this.get_prev_row();
583
584   if (new_row)
585   {
586     this.select_row(new_row.uid, mod_key, true);
587     this.scrollto(new_row.uid);
588   }
589
590   return false;
591 },
592
593
594 /**
595  * Try to scroll the list to make the specified row visible
596  */
597 scrollto: function(id)
598 {
599   var row = this.rows[id].obj;
600   if (row && this.frame)
601   {
602     var scroll_to = Number(row.offsetTop);
603
604     if (scroll_to < Number(this.frame.scrollTop))
605       this.frame.scrollTop = scroll_to;
606     else if (scroll_to + Number(row.offsetHeight) > Number(this.frame.scrollTop) + Number(this.frame.offsetHeight))
607       this.frame.scrollTop = (scroll_to + Number(row.offsetHeight)) - Number(this.frame.offsetHeight);
608   }
609 },
610
611
612 /**
613  * Handler for mouse move events
614  */
615 drag_mouse_move: function(e)
616 {
617   if (this.drag_start)
618   {
619     // check mouse movement, of less than 3 pixels, don't start dragging
620     var m = rcube_event.get_mouse_pos(e);
621     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))
622       return false;
623   
624     if (!this.draglayer)
625       this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, width:300, vis:0, zindex:2000});
626   
627     // get subjects of selectedd messages
628     var names = '';
629     var c, i, node, subject, obj;
630     for(var n=0; n<this.selection.length; n++)
631     {
632       if (n>12)  // only show 12 lines
633       {
634         names += '...';
635         break;
636       }
637
638       if (this.rows[this.selection[n]].obj)
639       {
640         obj = this.rows[this.selection[n]].obj;
641         subject = '';
642
643         for(c=0, i=0; i<obj.childNodes.length; i++)
644         {
645           if (obj.childNodes[i].nodeName == 'TD')
646           {
647             if (((node = obj.childNodes[i].firstChild) && (node.nodeType==3 || node.nodeName=='A')) &&
648               (this.subject_col < 0 || (this.subject_col >= 0 && this.subject_col == c)))
649             {
650               subject = node.nodeType==3 ? node.data : node.innerHTML;
651               names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
652               break;
653             }
654             c++;
655           }
656         }
657       }
658     }
659
660     this.draglayer.write(names);
661     this.draglayer.show(1);
662
663     this.drag_active = true;
664     this.trigger_event('dragstart');
665   }
666
667   if (this.drag_active && this.draglayer)
668   {
669     var pos = rcube_event.get_mouse_pos(e);
670     this.draglayer.move(pos.x+20, pos.y-5);
671   }
672
673   this.drag_start = false;
674
675   return false;
676 },
677
678
679 /**
680  * Handler for mouse up events
681  */
682 drag_mouse_up: function(e)
683 {
684   document.onmousemove = null;
685
686   if (this.draglayer && this.draglayer.visible)
687     this.draglayer.show(0);
688
689   this.drag_active = false;
690   this.trigger_event('dragend');
691
692   rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'drag_mouse_move'});
693   rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'drag_mouse_up'});
694
695   return rcube_event.cancel(e);
696 },
697
698
699
700 /**
701  * set/unset a specific class name
702  */
703 set_classname: function(obj, classname, set)
704 {
705   var reg = new RegExp('\s*'+classname, 'i');
706   if (!set && obj.className.match(reg))
707     obj.className = obj.className.replace(reg, '');
708   else if (set && !obj.className.match(reg))
709     obj.className += ' '+classname;
710 },
711
712
713 /**
714  * Setter for object event handlers
715  *
716  * @param {String}   Event name
717  * @param {Function} Handler function
718  * @return Listener ID (used to remove this handler later on)
719  */
720 addEventListener: function(evt, handler)
721 {
722   if (this.events[evt]) {
723     var handle = this.events[evt].length;
724     this.events[evt][handle] = handler;
725     return handle;
726   }
727   else
728     return false;
729 },
730
731
732 /**
733  * Removes a specific event listener
734  *
735  * @param {String} Event name
736  * @param {Int}    Listener ID to remove
737  */
738 removeEventListener: function(evt, handle)
739 {
740   if (this.events[evt] && this.events[evt][handle])
741     this.events[evt][handle] = null;
742 },
743
744
745 /**
746  * This will execute all registered event handlers
747  * @private
748  */
749 trigger_event: function(evt)
750 {
751   if (this.events[evt] && this.events[evt].length) {
752     for (var i=0; i<this.events[evt].length; i++)
753       if (typeof(this.events[evt][i]) == 'function')
754         this.events[evt][i](this);
755   }
756 }
757
758
759 };
760