2 +-----------------------------------------------------------------------+
3 | RoundCube List Widget |
5 | This file is part of the RoundCube Webmail client |
6 | Copyright (C) 2006-2008, 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 344 2006-09-18 03:49:28Z thomasb $
21 * RoundCube List Widget class
24 function rcube_list_widget(list, p)
30 this.list = list ? list : null;
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;
43 this.dont_select = false;
44 this.drag_active = false;
45 this.last_selected = 0;
47 this.in_selection_before = 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:[] };
54 // overwrite default paramaters
55 if (p && typeof(p)=='object')
61 rcube_list_widget.prototype = {
65 * get all message rows from HTML table and init each row
69 if (this.list && this.list.tBodies[0])
71 this.rows = new Array();
74 for(var r=0; r<this.list.tBodies[0].childNodes.length; r++)
76 row = this.list.tBodies[0].childNodes[r];
77 while (row && (row.nodeType != 1 || row.style.display == 'none'))
79 row = row.nextSibling;
86 this.frame = this.list.parentNode;
90 rcube_event.add_listener({element:document, event:'keydown', object:this, method:'key_press'});
98 init_row: function(row)
100 // make references in internal array and set event handlers
101 if (row && String(row.id).match(/rcmrow([a-z0-9\-_=]+)/i))
106 this.rows[uid] = {uid:uid, id:row.id, obj:row, classname:row.className};
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); };
113 row.onselectstart = function() { return false; };
115 this.row_init(this.rows[uid]);
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();
130 if (sel) this.clear_selection();
135 * 'remove' message row from list (just hide it)
137 remove_row: function(uid, sel_next)
139 if (this.rows[uid].obj)
140 this.rows[uid].obj.style.display = 'none';
145 this.rows[uid] = null;
152 insert_row: function(row, attop)
154 var tbody = this.list.tBodies[0];
156 if (attop && tbody.rows.length)
157 tbody.insertBefore(row, tbody.firstChild);
159 tbody.appendChild(row);
167 * Set focur to the list
172 for (var n=0; n<this.selection.length; n++)
174 id = this.selection[n];
175 if (this.rows[id] && this.rows[id].obj)
177 this.set_classname(this.rows[id].obj, 'selected', true);
178 this.set_classname(this.rows[id].obj, 'unfocused', false);
182 if (e || (e = window.event))
183 rcube_event.cancel(e);
188 * remove focus from the list
193 this.focused = false;
194 for (var n=0; n<this.selection.length; n++)
196 id = this.selection[n];
197 if (this.rows[id] && this.rows[id].obj)
199 this.set_classname(this.rows[id].obj, 'selected', false);
200 this.set_classname(this.rows[id].obj, 'unfocused', true);
207 * onmousedown-handler of message list row
209 drag_row: function(e, id)
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')))
216 this.in_selection_before = this.in_selection(id) ? id : false;
218 // selects currently unselected row
219 if (!this.in_selection_before)
221 var mod_key = rcube_event.get_modifier(e);
222 this.select_row(id, mod_key, false);
225 if (this.draggable && this.selection.length)
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'});
238 * onmouseup-handler of message list row
240 click_row: function(e, id)
242 var now = new Date().getTime();
243 var mod_key = rcube_event.get_modifier(e);
244 var evtarget = rcube_event.get_target(e);
246 if ((evtarget && (evtarget.tagName == 'INPUT' || evtarget.tagName == 'IMG')))
249 // don't do anything (another action processed before)
250 if (this.dont_select)
252 this.dont_select = false;
256 var dblclicked = now - this.rows[id].clicked < this.dblclick_time;
258 // unselects currently selected row
259 if (!this.drag_active && this.in_selection_before == id && !dblclicked)
260 this.select_row(id, mod_key, false);
262 this.drag_start = false;
263 this.in_selection_before = false;
265 // row was double clicked
266 if (this.rows && dblclicked && this.in_selection(id))
267 this.trigger_event('dblclick');
269 this.trigger_event('click');
271 if (!this.drag_active)
272 rcube_event.cancel(e);
274 this.rows[id].clicked = now;
280 * get next and previous rows that are not hidden
282 get_next_row: function()
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;
295 get_prev_row: function()
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;
309 // selects or unselects the proper row depending on the modifier key pressed
310 select_row: function(id, mod_key, with_mouse)
312 var select_before = this.selection.join(',');
313 if (!this.multiselect)
316 if (!this.shift_start)
317 this.shift_start = id
321 this.shift_start = id;
322 this.highlight_row(id, false);
323 this.multi_selecting = false;
330 this.shift_select(id, false);
335 this.highlight_row(id, true);
338 case CONTROL_SHIFT_KEY:
339 this.shift_select(id, true);
343 this.highlight_row(id, false);
346 this.multi_selecting = true;
349 // trigger event if selection changed
350 if (this.selection.join(',') != select_before)
351 this.trigger_event('select');
353 if (this.last_selected != 0 && this.rows[this.last_selected])
354 this.set_classname(this.rows[this.last_selected].obj, 'focused', false);
356 // unselect if toggleselect is active and the same row was clicked again
357 if (this.toggleselect && this.last_selected == id)
359 this.clear_selection();
363 this.set_classname(this.rows[id].obj, 'focused', true);
365 if (!this.selection.length)
366 this.shift_start = null;
368 this.last_selected = id;
373 * Alias method for select_row
377 this.select_row(id, false);
383 * Select row next to the last selected one.
384 * Either below or above.
386 select_next: function()
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;
392 this.select_row(new_row.uid, false, false);
397 * Perform selection when shift key is pressed
399 shift_select: function(id, control)
401 if (!this.rows[this.shift_start] || !this.selection.length)
402 this.shift_start = id;
404 var from_rowIndex = this.rows[this.shift_start].obj.rowIndex;
405 var to_rowIndex = this.rows[id].obj.rowIndex;
407 var i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex);
408 var j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
410 // iterate through the entire message list
411 for (var n in this.rows)
413 if ((this.rows[n].obj.rowIndex >= i) && (this.rows[n].obj.rowIndex <= j))
415 if (!this.in_selection(n))
416 this.highlight_row(n, true);
420 if (this.in_selection(n) && !control)
421 this.highlight_row(n, true);
428 * Check if given id is part of the current selection
430 in_selection: function(id)
432 for(var n in this.selection)
433 if (this.selection[n]==id)
441 * Select each row in list
443 select_all: function(filter)
445 if (!this.rows || !this.rows.length)
448 // reset but remember selection first
449 var select_before = this.selection.join(',');
450 this.clear_selection();
452 for (var n in this.rows)
454 if (!filter || this.rows[n][filter]==true)
456 this.last_selected = n;
457 this.highlight_row(n, true);
461 // trigger event if selection changed
462 if (this.selection.join(',') != select_before)
463 this.trigger_event('select');
472 * Unselect all selected rows
474 clear_selection: function()
476 var num_select = this.selection.length;
477 for (var n=0; n<this.selection.length; n++)
478 if (this.rows[this.selection[n]])
480 this.set_classname(this.rows[this.selection[n]].obj, 'selected', false);
481 this.set_classname(this.rows[this.selection[n]].obj, 'unfocused', false);
484 this.selection = new Array();
487 this.trigger_event('select');
492 * Getter for the selection array
494 get_selection: function()
496 return this.selection;
501 * Return the ID if only one row is selected
503 get_single_selection: function()
505 if (this.selection.length == 1)
506 return this.selection[0];
513 * Highlight/unhighlight a row
515 highlight_row: function(id, multiple)
517 if (this.rows[id] && !multiple)
519 if (this.selection.length > 1 || !this.in_selection(id))
521 this.clear_selection();
522 this.selection[0] = id;
523 this.set_classname(this.rows[id].obj, 'selected', true);
526 else if (this.rows[id])
528 if (!this.in_selection(id)) // select row
530 this.selection[this.selection.length] = id;
531 this.set_classname(this.rows[id].obj, 'selected', true);
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);
547 * Handler for keyboard events
549 key_press: function(e)
551 if (this.focused != true)
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);
560 return this.use_arrow_key(keyCode, mod_key);
564 this.shiftkey = e.shiftKey;
565 this.key_pressed = keyCode;
566 this.trigger_event('keypress');
574 * Special handling method for arrow keys
576 use_arrow_key: function(keyCode, mod_key)
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();
586 this.select_row(new_row.uid, mod_key, true);
587 this.scrollto(new_row.uid);
595 * Try to scroll the list to make the specified row visible
597 scrollto: function(id)
599 var row = this.rows[id].obj;
600 if (row && this.frame)
602 var scroll_to = Number(row.offsetTop);
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);
613 * Handler for mouse move events
615 drag_mouse_move: function(e)
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))
625 this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, width:300, vis:0, zindex:2000});
627 // get subjects of selectedd messages
629 var c, i, node, subject, obj;
630 for(var n=0; n<this.selection.length; n++)
632 if (n>12) // only show 12 lines
638 if (this.rows[this.selection[n]].obj)
640 obj = this.rows[this.selection[n]].obj;
643 for(c=0, i=0; i<obj.childNodes.length; i++)
645 if (obj.childNodes[i].nodeName == 'TD')
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)))
650 subject = node.nodeType==3 ? node.data : node.innerHTML;
651 names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
660 this.draglayer.write(names);
661 this.draglayer.show(1);
663 this.drag_active = true;
664 this.trigger_event('dragstart');
667 if (this.drag_active && this.draglayer)
669 var pos = rcube_event.get_mouse_pos(e);
670 this.draglayer.move(pos.x+20, pos.y-5);
673 this.drag_start = false;
680 * Handler for mouse up events
682 drag_mouse_up: function(e)
684 document.onmousemove = null;
686 if (this.draglayer && this.draglayer.visible)
687 this.draglayer.show(0);
689 this.drag_active = false;
690 this.trigger_event('dragend');
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'});
695 return rcube_event.cancel(e);
701 * set/unset a specific class name
703 set_classname: function(obj, classname, set)
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;
714 * Setter for object event handlers
716 * @param {String} Event name
717 * @param {Function} Handler function
718 * @return Listener ID (used to remove this handler later on)
720 addEventListener: function(evt, handler)
722 if (this.events[evt]) {
723 var handle = this.events[evt].length;
724 this.events[evt][handle] = handler;
733 * Removes a specific event listener
735 * @param {String} Event name
736 * @param {Int} Listener ID to remove
738 removeEventListener: function(evt, handle)
740 if (this.events[evt] && this.events[evt][handle])
741 this.events[evt][handle] = null;
746 * This will execute all registered event handlers
749 trigger_event: function(evt)
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);