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].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 var from_rowIndex = this.rows[this.shift_start].obj.rowIndex;
402 var to_rowIndex = this.rows[id].obj.rowIndex;
404 var i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex);
405 var j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
407 // iterate through the entire message list
408 for (var n in this.rows)
410 if ((this.rows[n].obj.rowIndex >= i) && (this.rows[n].obj.rowIndex <= j))
412 if (!this.in_selection(n))
413 this.highlight_row(n, true);
417 if (this.in_selection(n) && !control)
418 this.highlight_row(n, true);
425 * Check if given id is part of the current selection
427 in_selection: function(id)
429 for(var n in this.selection)
430 if (this.selection[n]==id)
438 * Select each row in list
440 select_all: function(filter)
442 if (!this.rows || !this.rows.length)
445 // reset but remember selection first
446 var select_before = this.selection.join(',');
447 this.clear_selection();
449 for (var n in this.rows)
451 if (!filter || this.rows[n][filter]==true)
453 this.last_selected = n;
454 this.highlight_row(n, true);
458 // trigger event if selection changed
459 if (this.selection.join(',') != select_before)
460 this.trigger_event('select');
469 * Unselect all selected rows
471 clear_selection: function()
473 var num_select = this.selection.length;
474 for (var n=0; n<this.selection.length; n++)
475 if (this.rows[this.selection[n]])
477 this.set_classname(this.rows[this.selection[n]].obj, 'selected', false);
478 this.set_classname(this.rows[this.selection[n]].obj, 'unfocused', false);
481 this.selection = new Array();
484 this.trigger_event('select');
489 * Getter for the selection array
491 get_selection: function()
493 return this.selection;
498 * Return the ID if only one row is selected
500 get_single_selection: function()
502 if (this.selection.length == 1)
503 return this.selection[0];
510 * Highlight/unhighlight a row
512 highlight_row: function(id, multiple)
514 if (this.rows[id] && !multiple)
516 if (this.selection.length > 1 || !this.in_selection(id))
518 this.clear_selection();
519 this.selection[0] = id;
520 this.set_classname(this.rows[id].obj, 'selected', true);
523 else if (this.rows[id])
525 if (!this.in_selection(id)) // select row
527 this.selection[this.selection.length] = id;
528 this.set_classname(this.rows[id].obj, 'selected', true);
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);
544 * Handler for keyboard events
546 key_press: function(e)
548 if (this.focused != true)
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);
557 return this.use_arrow_key(keyCode, mod_key);
561 this.shiftkey = e.shiftKey;
562 this.key_pressed = keyCode;
563 this.trigger_event('keypress');
571 * Special handling method for arrow keys
573 use_arrow_key: function(keyCode, mod_key)
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();
583 this.select_row(new_row.uid, mod_key, true);
584 this.scrollto(new_row.uid);
592 * Try to scroll the list to make the specified row visible
594 scrollto: function(id)
596 var row = this.rows[id].obj;
597 if (row && this.frame)
599 var scroll_to = Number(row.offsetTop);
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);
610 * Handler for mouse move events
612 drag_mouse_move: function(e)
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))
622 this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, width:300, vis:0, zindex:2000});
624 // get subjects of selectedd messages
626 var c, i, node, subject, obj;
627 for(var n=0; n<this.selection.length; n++)
629 if (n>12) // only show 12 lines
635 if (this.rows[this.selection[n]].obj)
637 obj = this.rows[this.selection[n]].obj;
640 for(c=0, i=0; i<obj.childNodes.length; i++)
642 if (obj.childNodes[i].nodeName == 'TD')
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)))
647 subject = node.nodeType==3 ? node.data : node.innerHTML;
648 names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
657 this.draglayer.write(names);
658 this.draglayer.show(1);
660 this.drag_active = true;
661 this.trigger_event('dragstart');
664 if (this.drag_active && this.draglayer)
666 var pos = rcube_event.get_mouse_pos(e);
667 this.draglayer.move(pos.x+20, pos.y-5);
670 this.drag_start = false;
677 * Handler for mouse up events
679 drag_mouse_up: function(e)
681 document.onmousemove = null;
683 if (this.draglayer && this.draglayer.visible)
684 this.draglayer.show(0);
686 this.drag_active = false;
687 this.trigger_event('dragend');
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'});
692 return rcube_event.cancel(e);
698 * set/unset a specific class name
700 set_classname: function(obj, classname, set)
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;
711 * Setter for object event handlers
713 * @param {String} Event name
714 * @param {Function} Handler function
715 * @return Listener ID (used to remove this handler later on)
717 addEventListener: function(evt, handler)
719 if (this.events[evt]) {
720 var handle = this.events[evt].length;
721 this.events[evt][handle] = handler;
730 * Removes a specific event listener
732 * @param {String} Event name
733 * @param {Int} Listener ID to remove
735 removeEventListener: function(evt, handle)
737 if (this.events[evt] && this.events[evt][handle])
738 this.events[evt][handle] = null;
743 * This will execute all registered event handlers
746 trigger_event: function(evt)
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);