2 +-----------------------------------------------------------------------+
3 | RoundCube List Widget |
5 | This file is part of the RoundCube Webmail client |
6 | Copyright (C) 2006, 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.shiftkey = false;
37 this.multiselect = false;
38 this.draggable = false;
39 this.keyboard = false;
41 this.dont_select = false;
42 this.drag_active = false;
43 this.last_selected = 0;
44 this.in_selection_before = false;
46 this.drag_mouse_start = null;
47 this.dblclick_time = 600;
48 this.row_init = function(){};
49 this.events = { click:[], dblclick:[], select:[], keypress:[], dragstart:[], dragend:[] };
51 // overwrite default paramaters
52 if (p && typeof(p)=='object')
58 rcube_list_widget.prototype = {
62 * get all message rows from HTML table and init each row
66 if (this.list && this.list.tBodies[0])
68 this.rows = new Array();
71 for(var r=0; r<this.list.tBodies[0].childNodes.length; r++)
73 row = this.list.tBodies[0].childNodes[r];
74 while (row && (row.nodeType != 1 || row.style.display == 'none'))
76 row = row.nextSibling;
83 this.frame = this.list.parentNode;
87 rcube_event.add_listener({element:document, event:'keydown', object:this, method:'key_press'});
95 init_row: function(row)
97 // make references in internal array and set event handlers
98 if (row && String(row.id).match(/rcmrow([a-z0-9\-_=]+)/i))
103 this.rows[uid] = {uid:uid, id:row.id, obj:row, classname:row.className};
105 // set eventhandlers to table row
106 row.onmousedown = function(e){ return p.drag_row(e, this.uid); };
107 row.onmouseup = function(e){ return p.click_row(e, this.uid); };
110 row.onselectstart = function() { return false; };
112 this.row_init(this.rows[uid]);
122 var tbody = document.createElement('TBODY');
123 this.list.insertBefore(tbody, this.list.tBodies[0]);
124 this.list.removeChild(this.list.tBodies[1]);
125 this.rows = new Array();
127 if (sel) this.clear_selection();
132 * 'remove' message row from list (just hide it)
134 remove_row: function(uid)
136 if (this.rows[uid].obj)
137 this.rows[uid].obj.style.display = 'none';
139 this.rows[uid] = null;
146 insert_row: function(row, attop)
148 var tbody = this.list.tBodies[0];
150 if (attop && tbody.rows.length)
151 tbody.insertBefore(row, tbody.firstChild);
153 tbody.appendChild(row);
161 * Set focur to the list
166 for (var n=0; n<this.selection.length; n++)
168 id = this.selection[n];
169 if (this.rows[id].obj)
171 this.set_classname(this.rows[id].obj, 'selected', true);
172 this.set_classname(this.rows[id].obj, 'unfocused', false);
176 if (e || (e = window.event))
177 rcube_event.cancel(e);
182 * remove focus from the list
187 this.focused = false;
188 for (var n=0; n<this.selection.length; n++)
190 id = this.selection[n];
191 if (this.rows[id] && this.rows[id].obj)
193 this.set_classname(this.rows[id].obj, 'selected', false);
194 this.set_classname(this.rows[id].obj, 'unfocused', true);
201 * onmousedown-handler of message list row
203 drag_row: function(e, id)
205 this.in_selection_before = this.in_selection(id) ? id : false;
207 // don't do anything (another action processed before)
208 if (this.dont_select)
211 // selects currently unselected row
212 if (!this.in_selection_before)
214 var mod_key = rcube_event.get_modifier(e);
215 this.select_row(id, mod_key, false);
218 if (this.draggable && this.selection.length)
220 this.drag_start = true;
221 this.drag_mouse_start = rcube_event.get_mouse_pos(e);
222 rcube_event.add_listener({element:document, event:'mousemove', object:this, method:'drag_mouse_move'});
223 rcube_event.add_listener({element:document, event:'mouseup', object:this, method:'drag_mouse_up'});
231 * onmouseup-handler of message list row
233 click_row: function(e, id)
235 var now = new Date().getTime();
236 var mod_key = rcube_event.get_modifier(e);
238 // don't do anything (another action processed before)
239 if (this.dont_select)
241 this.dont_select = false;
245 var dblclicked = now - this.rows[id].clicked < this.dblclick_time;
247 // unselects currently selected row
248 if (!this.drag_active && this.in_selection_before == id && !dblclicked)
249 this.select_row(id, mod_key, false);
251 this.drag_start = false;
252 this.in_selection_before = false;
254 // row was double clicked
255 if (this.rows && dblclicked && this.in_selection(id))
256 this.trigger_event('dblclick');
258 this.trigger_event('click');
260 if (!this.drag_active)
261 rcube_event.cancel(e);
263 this.rows[id].clicked = now;
269 * get next and previous rows that are not hidden
271 get_next_row: function()
276 var last_selected_row = this.rows[this.last_selected];
277 var new_row = last_selected_row ? last_selected_row.obj.nextSibling : null;
278 while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
279 new_row = new_row.nextSibling;
284 get_prev_row: function()
289 var last_selected_row = this.rows[this.last_selected];
290 var new_row = last_selected_row ? last_selected_row.obj.previousSibling : null;
291 while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
292 new_row = new_row.previousSibling;
298 // selects or unselects the proper row depending on the modifier key pressed
299 select_row: function(id, mod_key, with_mouse)
301 var select_before = this.selection.join(',');
302 if (!this.multiselect)
307 this.shift_start = id;
308 this.highlight_row(id, false);
315 this.shift_select(id, false);
319 this.shift_start = id;
321 this.highlight_row(id, true);
324 case CONTROL_SHIFT_KEY:
325 this.shift_select(id, true);
329 this.highlight_row(id, false);
334 // trigger event if selection changed
335 if (this.selection.join(',') != select_before)
336 this.trigger_event('select');
338 if (this.last_selected != 0 && this.rows[this.last_selected])
339 this.set_classname(this.rows[this.last_selected].obj, 'focused', false);
341 this.last_selected = id;
342 this.set_classname(this.rows[id].obj, 'focused', true);
347 * Alias method for select_row
351 this.select_row(id, false);
357 * Select row next to the last selected one.
358 * Either below or above.
360 select_next: function()
362 var next_row = this.get_next_row();
363 var prev_row = this.get_prev_row();
364 var new_row = (next_row) ? next_row : prev_row;
366 this.select_row(new_row.uid, false, false);
371 * Perform selection when shift key is pressed
373 shift_select: function(id, control)
375 var from_rowIndex = this.rows[this.shift_start].obj.rowIndex;
376 var to_rowIndex = this.rows[id].obj.rowIndex;
378 var i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex);
379 var j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
381 // iterate through the entire message list
382 for (var n in this.rows)
384 if ((this.rows[n].obj.rowIndex >= i) && (this.rows[n].obj.rowIndex <= j))
386 if (!this.in_selection(n))
387 this.highlight_row(n, true);
391 if (this.in_selection(n) && !control)
392 this.highlight_row(n, true);
399 * Check if given id is part of the current selection
401 in_selection: function(id)
403 for(var n in this.selection)
404 if (this.selection[n]==id)
412 * Select each row in list
414 select_all: function(filter)
416 if (!this.rows || !this.rows.length)
419 // reset selection first
420 this.clear_selection();
422 for (var n in this.rows)
424 if (!filter || this.rows[n][filter]==true)
426 this.last_selected = n;
427 this.highlight_row(n, true);
436 * Unselect all selected rows
438 clear_selection: function()
440 for(var n=0; n<this.selection.length; n++)
441 if (this.rows[this.selection[n]])
443 this.set_classname(this.rows[this.selection[n]].obj, 'selected', false);
444 this.set_classname(this.rows[this.selection[n]].obj, 'unfocused', false);
447 this.selection = new Array();
452 * Getter for the selection array
454 get_selection: function()
456 return this.selection;
461 * Return the ID if only one row is selected
463 get_single_selection: function()
465 if (this.selection.length == 1)
466 return this.selection[0];
473 * Highlight/unhighlight a row
475 highlight_row: function(id, multiple)
477 if (this.rows[id] && !multiple)
479 this.clear_selection();
480 this.selection[0] = id;
481 this.set_classname(this.rows[id].obj, 'selected', true)
483 else if (this.rows[id])
485 if (!this.in_selection(id)) // select row
487 this.selection[this.selection.length] = id;
488 this.set_classname(this.rows[id].obj, 'selected', true);
492 var p = find_in_array(id, this.selection);
493 var a_pre = this.selection.slice(0, p);
494 var a_post = this.selection.slice(p+1, this.selection.length);
495 this.selection = a_pre.concat(a_post);
496 this.set_classname(this.rows[id].obj, 'selected', false);
497 this.set_classname(this.rows[id].obj, 'unfocused', false);
504 * Handler for keyboard events
506 key_press: function(e)
508 if (this.focused != true)
511 this.shiftkey = e.shiftKey;
513 var keyCode = document.layers ? e.which : document.all ? event.keyCode : document.getElementById ? e.keyCode : 0;
514 var mod_key = rcube_event.get_modifier(e);
519 return this.use_arrow_key(keyCode, mod_key);
523 this.key_pressed = keyCode;
524 this.trigger_event('keypress');
532 * Special handling method for arrow keys
534 use_arrow_key: function(keyCode, mod_key)
537 if (keyCode == 40) // down arrow key pressed
538 new_row = this.get_next_row();
539 else if (keyCode == 38) // up arrow key pressed
540 new_row = this.get_prev_row();
544 this.select_row(new_row.uid, mod_key, true);
545 this.scrollto(new_row.uid);
553 * Try to scroll the list to make the specified row visible
555 scrollto: function(id)
557 var row = this.rows[id].obj;
558 if (row && this.frame)
560 var scroll_to = Number(row.offsetTop);
562 if (scroll_to < Number(this.frame.scrollTop))
563 this.frame.scrollTop = scroll_to;
564 else if (scroll_to + Number(row.offsetHeight) > Number(this.frame.scrollTop) + Number(this.frame.offsetHeight))
565 this.frame.scrollTop = (scroll_to + Number(row.offsetHeight)) - Number(this.frame.offsetHeight);
571 * Handler for mouse move events
573 drag_mouse_move: function(e)
577 // check mouse movement, of less than 3 pixels, don't start dragging
578 var m = rcube_event.get_mouse_pos(e);
579 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))
583 this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, width:300, vis:0, zindex:2000});
585 // get subjects of selectedd messages
587 var c, node, subject, obj;
588 for(var n=0; n<this.selection.length; n++)
590 if (n>12) // only show 12 lines
596 if (this.rows[this.selection[n]].obj)
598 obj = this.rows[this.selection[n]].obj;
601 for(c=0; c<obj.childNodes.length; c++)
602 if (obj.childNodes[c].nodeName=='TD' && (node = obj.childNodes[c].firstChild) && (node.nodeType==3 || node.nodeName=='A'))
604 subject = node.nodeType==3 ? node.data : node.innerHTML;
605 names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
611 this.draglayer.write(names);
612 this.draglayer.show(1);
614 this.drag_active = true;
615 this.trigger_event('dragstart');
618 if (this.drag_active && this.draglayer)
620 var pos = rcube_event.get_mouse_pos(e);
621 this.draglayer.move(pos.x+20, pos.y-5);
624 this.drag_start = false;
631 * Handler for mouse up events
633 drag_mouse_up: function(e)
635 document.onmousemove = null;
637 if (this.draglayer && this.draglayer.visible)
638 this.draglayer.show(0);
640 this.drag_active = false;
641 this.trigger_event('dragend');
643 rcube_event.remove_listener({element:document, event:'mousemove', object:this, method:'drag_mouse_move'});
644 rcube_event.remove_listener({element:document, event:'mouseup', object:this, method:'drag_mouse_up'});
646 return rcube_event.cancel(e);
652 * set/unset a specific class name
654 set_classname: function(obj, classname, set)
656 var reg = new RegExp('\s*'+classname, 'i');
657 if (!set && obj.className.match(reg))
658 obj.className = obj.className.replace(reg, '');
659 else if (set && !obj.className.match(reg))
660 obj.className += ' '+classname;
665 * Setter for object event handlers
667 * @param {String} Event name
668 * @param {Function} Handler function
669 * @return Listener ID (used to remove this handler later on)
671 addEventListener: function(evt, handler)
673 if (this.events[evt]) {
674 var handle = this.events[evt].length;
675 this.events[evt][handle] = handler;
684 * Removes a specific event listener
686 * @param {String} Event name
687 * @param {Int} Listener ID to remove
689 removeEventListener: function(evt, handle)
691 if (this.events[evt] && this.events[evt][handle])
692 this.events[evt][handle] = null;
697 * This will execute all registered event handlers
700 trigger_event: function(evt)
702 if (this.events[evt] && this.events[evt].length) {
703 for (var i=0; i<this.events[evt].length; i++)
704 if (typeof(this.events[evt][i]) == 'function')
705 this.events[evt][i](this);