2 +-----------------------------------------------------------------------+
3 | RoundCube List Widget |
5 | This file is part of the RoundCube Webmail client |
6 | Copyright (C) 2006-2009, RoundCube Dev, - Switzerland |
7 | Licensed under the GNU GPL |
9 +-----------------------------------------------------------------------+
10 | Authors: Thomas Bruederli <roundcube@gmail.com> |
11 | Charles McNulty <charles@charlesmcnulty.com> |
12 +-----------------------------------------------------------------------+
13 | Requires: common.js |
14 +-----------------------------------------------------------------------+
16 $Id: list.js 2483 2009-05-15 10:22:29Z thomasb $
21 * RoundCube List Widget class
24 function rcube_list_widget(list, p)
29 this.BACKSPACE_KEY = 8;
31 this.list = list ? list : null;
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;
45 this.dont_select = false;
46 this.drag_active = false;
47 this.last_selected = 0;
49 this.in_selection_before = 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:[] };
56 // overwrite default paramaters
57 if (p && typeof(p)=='object')
63 rcube_list_widget.prototype = {
67 * get all message rows from HTML table and init each row
71 if (this.list && this.list.tBodies[0])
73 this.rows = new Array();
77 for(var r=0; r<this.list.tBodies[0].childNodes.length; r++)
79 row = this.list.tBodies[0].childNodes[r];
80 while (row && (row.nodeType != 1 || row.style.display == 'none'))
82 row = row.nextSibling;
90 this.frame = this.list.parentNode;
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'});
104 init_row: function(row)
106 // make references in internal array and set event handlers
107 if (row && String(row.id).match(/rcmrow([a-z0-9\-_=\+\/]+)/i))
112 this.rows[uid] = {uid:uid, id:row.id, obj:row, classname:row.className};
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); };
119 row.onselectstart = function() { return false; };
121 this.row_init(this.rows[uid]);
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();
137 if (sel) this.clear_selection();
142 * 'remove' message row from list (just hide it)
144 remove_row: function(uid, sel_next)
146 if (this.rows[uid].obj)
147 this.rows[uid].obj.style.display = 'none';
152 this.rows[uid] = null;
160 insert_row: function(row, attop)
162 var tbody = this.list.tBodies[0];
164 if (attop && tbody.rows.length)
165 tbody.insertBefore(row, tbody.firstChild);
167 tbody.appendChild(row);
176 * Set focus to the list
181 for (var n=0; n<this.selection.length; n++)
183 id = this.selection[n];
184 if (this.rows[id] && this.rows[id].obj)
186 this.set_classname(this.rows[id].obj, 'selected', true);
187 this.set_classname(this.rows[id].obj, 'unfocused', false);
191 if (e || (e = window.event))
192 rcube_event.cancel(e);
197 * remove focus from the list
202 this.focused = false;
203 for (var n=0; n<this.selection.length; n++)
205 id = this.selection[n];
206 if (this.rows[id] && this.rows[id].obj)
208 this.set_classname(this.rows[id].obj, 'selected', false);
209 this.set_classname(this.rows[id].obj, 'unfocused', true);
216 * onmousedown-handler of message list row
218 drag_row: function(e, id)
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')))
225 // accept right-clicks
226 if (rcube_event.get_button(e) == 2)
229 this.in_selection_before = this.in_selection(id) ? id : false;
231 // selects currently unselected row
232 if (!this.in_selection_before)
234 var mod_key = rcube_event.get_modifier(e);
235 this.select_row(id, mod_key, false);
238 if (this.draggable && this.selection.length)
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'});
245 // add listener for iframes
246 var iframes = document.getElementsByTagName('IFRAME');
247 this.iframe_events = Object();
248 for (var n in iframes)
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;
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); }
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]);
269 iframedoc['onmousemove'] = this.iframe_events[n];
271 rcube_event.add_listener({element:iframedoc, event:'mouseup', object:this, method:'drag_mouse_up'});
281 * onmouseup-handler of message list row
283 click_row: function(e, id)
285 var now = new Date().getTime();
286 var mod_key = rcube_event.get_modifier(e);
287 var evtarget = rcube_event.get_target(e);
289 if ((evtarget && (evtarget.tagName == 'INPUT' || evtarget.tagName == 'IMG')))
292 // don't do anything (another action processed before)
293 if (this.dont_select)
295 this.dont_select = false;
299 var dblclicked = now - this.rows[id].clicked < this.dblclick_time;
301 // unselects currently selected row
302 if (!this.drag_active && this.in_selection_before == id && !dblclicked)
303 this.select_row(id, mod_key, false);
305 this.drag_start = false;
306 this.in_selection_before = false;
308 // row was double clicked
309 if (this.rows && dblclicked && this.in_selection(id))
310 this.trigger_event('dblclick');
312 this.trigger_event('click');
314 if (!this.drag_active)
315 rcube_event.cancel(e);
317 this.rows[id].clicked = now;
323 * get next/previous/last rows that are not hidden
325 get_next_row: function()
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;
338 get_prev_row: function()
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;
351 get_last_row: function()
355 var rows = this.list.tBodies[0].rows;
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)
367 * selects or unselects the proper row depending on the modifier key pressed
369 select_row: function(id, mod_key, with_mouse)
371 var select_before = this.selection.join(',');
372 if (!this.multiselect)
375 if (!this.shift_start)
376 this.shift_start = id
380 this.shift_start = id;
381 this.highlight_row(id, false);
382 this.multi_selecting = false;
389 this.shift_select(id, false);
394 this.highlight_row(id, true);
397 case CONTROL_SHIFT_KEY:
398 this.shift_select(id, true);
402 this.highlight_row(id, false);
405 this.multi_selecting = true;
408 // trigger event if selection changed
409 if (this.selection.join(',') != select_before)
410 this.trigger_event('select');
412 if (this.last_selected != 0 && this.rows[this.last_selected])
413 this.set_classname(this.rows[this.last_selected].obj, 'focused', false);
415 // unselect if toggleselect is active and the same row was clicked again
416 if (this.toggleselect && this.last_selected == id)
418 this.clear_selection();
422 this.set_classname(this.rows[id].obj, 'focused', true);
424 if (!this.selection.length)
425 this.shift_start = null;
427 this.last_selected = id;
432 * Alias method for select_row
436 this.select_row(id, false);
442 * Select row next to the last selected one.
443 * Either below or above.
445 select_next: function()
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;
451 this.select_row(new_row.uid, false, false);
456 * Perform selection when shift key is pressed
458 shift_select: function(id, control)
460 if (!this.rows[this.shift_start] || !this.selection.length)
461 this.shift_start = id;
463 var from_rowIndex = this.rows[this.shift_start].obj.rowIndex;
464 var to_rowIndex = this.rows[id].obj.rowIndex;
466 var i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex);
467 var j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
469 // iterate through the entire message list
470 for (var n in this.rows)
472 if ((this.rows[n].obj.rowIndex >= i) && (this.rows[n].obj.rowIndex <= j))
474 if (!this.in_selection(n))
475 this.highlight_row(n, true);
479 if (this.in_selection(n) && !control)
480 this.highlight_row(n, true);
487 * Check if given id is part of the current selection
489 in_selection: function(id)
491 for(var n in this.selection)
492 if (this.selection[n]==id)
500 * Select each row in list
502 select_all: function(filter)
504 if (!this.rows || !this.rows.length)
507 // reset but remember selection first
508 var select_before = this.selection.join(',');
509 this.selection = new Array();
511 for (var n in this.rows)
513 if (!filter || (this.rows[n] && this.rows[n][filter] == true))
515 this.last_selected = n;
516 this.highlight_row(n, true);
518 else if (this.rows[n])
520 this.set_classname(this.rows[n].obj, 'selected', false);
521 this.set_classname(this.rows[n].obj, 'unfocused', false);
525 // trigger event if selection changed
526 if (this.selection.join(',') != select_before)
527 this.trigger_event('select');
536 * Unselect selected row(s)
538 clear_selection: function(id)
540 var num_select = this.selection.length;
545 for (var n=0; n<this.selection.length; n++)
546 if (this.selection[n] == id)
548 this.selection.splice(n,1);
555 for (var n=0; n<this.selection.length; n++)
556 if (this.rows[this.selection[n]])
558 this.set_classname(this.rows[this.selection[n]].obj, 'selected', false);
559 this.set_classname(this.rows[this.selection[n]].obj, 'unfocused', false);
562 this.selection = new Array();
565 if (num_select && !this.selection.length)
566 this.trigger_event('select');
571 * Getter for the selection array
573 get_selection: function()
575 return this.selection;
580 * Return the ID if only one row is selected
582 get_single_selection: function()
584 if (this.selection.length == 1)
585 return this.selection[0];
592 * Highlight/unhighlight a row
594 highlight_row: function(id, multiple)
596 if (this.rows[id] && !multiple)
598 if (this.selection.length > 1 || !this.in_selection(id))
600 this.clear_selection();
601 this.selection[0] = id;
602 this.set_classname(this.rows[id].obj, 'selected', true);
605 else if (this.rows[id])
607 if (!this.in_selection(id)) // select row
609 this.selection[this.selection.length] = id;
610 this.set_classname(this.rows[id].obj, 'selected', true);
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);
626 * Handler for keyboard events
628 key_press: function(e)
630 if (this.focused != true)
633 var keyCode = rcube_event.get_keycode(e);
634 var mod_key = rcube_event.get_modifier(e);
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);
645 this.shiftkey = e.shiftKey;
646 this.key_pressed = keyCode;
647 this.trigger_event('keypress');
649 if (this.key_pressed == this.BACKSPACE_KEY)
650 return rcube_event.cancel(e);
657 * Handler for keydown events
659 key_down: function(e)
661 switch (rcube_event.get_keycode(e))
667 if (!rcube_event.get_modifier(e) && this.focused)
668 return rcube_event.cancel(e);
678 * Special handling method for arrow keys
680 use_arrow_key: function(keyCode, mod_key)
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();
692 this.select_row(new_row.uid, mod_key, true);
693 this.scrollto(new_row.uid);
701 * Try to scroll the list to make the specified row visible
703 scrollto: function(id)
705 var row = this.rows[id].obj;
706 if (row && this.frame)
708 var scroll_to = Number(row.offsetTop);
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);
719 * Handler for mouse move events
721 drag_mouse_move: function(e)
725 // check mouse movement, of less than 3 pixels, don't start dragging
726 var m = rcube_event.get_mouse_pos(e);
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))
732 this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, vis:0, zindex:2000});
734 // get subjects of selectedd messages
736 var c, i, node, subject, obj;
737 for(var n=0; n<this.selection.length; n++)
739 if (n>12) // only show 12 lines
745 if (this.rows[this.selection[n]].obj)
747 obj = this.rows[this.selection[n]].obj;
750 for(c=0, i=0; i<obj.childNodes.length; i++)
752 if (obj.childNodes[i].nodeName == 'TD')
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)))
757 subject = node.nodeType==3 ? node.data : node.innerHTML;
758 // remove leading spaces
759 subject = subject.replace(/^\s+/i, '');
760 // truncate line to 50 characters
761 names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
770 this.draglayer.write(names);
771 this.draglayer.show(1);
773 this.drag_active = true;
774 this.trigger_event('dragstart');
777 if (this.drag_active && this.draglayer)
779 var pos = rcube_event.get_mouse_pos(e);
780 this.draglayer.move(pos.x+20, bw.ie ? pos.y-5+document.documentElement.scrollTop : pos.y-5);
781 this.trigger_event('dragmove', e);
784 this.drag_start = false;
791 * Handler for mouse up events
793 drag_mouse_up: function(e)
795 document.onmousemove = null;
797 if (this.draglayer && this.draglayer.visible)
798 this.draglayer.show(0);
800 this.drag_active = false;
801 this.trigger_event('dragend');
803 rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'drag_mouse_move'});
804 rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'drag_mouse_up'});
806 var iframes = document.getElementsByTagName('IFRAME');
807 for (var n in iframes) {
810 if (iframes[n].contentDocument)
811 iframedoc = iframes[n].contentDocument;
812 else if (iframes[n].contentWindow)
813 iframedoc = iframes[n].contentWindow.document;
814 else if (iframes[n].document)
815 iframedoc = iframes[n].document;
818 if (this.iframe_events[n]) {
819 if (iframedoc.removeEventListener)
820 iframedoc.removeEventListener('mousemove', this.iframe_events[n], false);
821 else if (iframedoc.detachEvent)
822 iframedoc.detachEvent('onmousemove', this.iframe_events[n]);
824 iframedoc['onmousemove'] = null;
826 rcube_event.remove_listener({element:iframedoc, event:'mouseup', object:this, method:'drag_mouse_up'});
830 return rcube_event.cancel(e);
836 * set/unset a specific class name
838 set_classname: function(obj, classname, set)
840 var reg = new RegExp('\s*'+classname, 'i');
841 if (!set && obj.className.match(reg))
842 obj.className = obj.className.replace(reg, '');
843 else if (set && !obj.className.match(reg))
844 obj.className += ' '+classname;
849 * Setter for object event handlers
851 * @param {String} Event name
852 * @param {Function} Handler function
853 * @return Listener ID (used to remove this handler later on)
855 addEventListener: function(evt, handler)
857 if (this.events[evt]) {
858 var handle = this.events[evt].length;
859 this.events[evt][handle] = handler;
868 * Removes a specific event listener
870 * @param {String} Event name
871 * @param {Int} Listener ID to remove
873 removeEventListener: function(evt, handle)
875 if (this.events[evt] && this.events[evt][handle])
876 this.events[evt][handle] = null;
881 * This will execute all registered event handlers
884 trigger_event: function(evt, p)
886 if (this.events[evt] && this.events[evt].length) {
887 for (var i=0; i<this.events[evt].length; i++)
888 if (typeof(this.events[evt][i]) == 'function')
889 this.events[evt][i](this, p);