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 4343 2010-12-16 09:26:30Z alec $
21 * Roundcube List Widget class
24 function rcube_list_widget(list, p)
29 this.BACKSPACE_KEY = 8;
31 this.list = list ? list : null;
38 this.subject_col = -1;
39 this.shiftkey = false;
40 this.multiselect = false;
41 this.multiexpand = false;
42 this.multi_selecting = false;
43 this.draggable = false;
44 this.column_movable = false;
45 this.keyboard = false;
46 this.toggleselect = false;
48 this.dont_select = false;
49 this.drag_active = false;
50 this.col_drag_active = false;
51 this.column_fixed = null;
52 this.last_selected = 0;
54 this.in_selection_before = false;
56 this.drag_mouse_start = null;
57 this.dblclick_time = 600;
58 this.row_init = function(){};
60 // overwrite default paramaters
61 if (p && typeof(p) == 'object')
67 rcube_list_widget.prototype = {
71 * get all message rows from HTML table and init each row
75 if (this.list && this.list.tBodies[0]) {
79 var r, len, rows = this.list.tBodies[0].rows;
81 for (r=0, len=rows.length; r<len; r++) {
82 this.init_row(rows[r]);
87 this.frame = this.list.parentNode;
91 rcube_event.add_listener({event:bw.opera?'keypress':'keydown', object:this, method:'key_press'});
92 rcube_event.add_listener({event:'keydown', object:this, method:'key_down'});
99 * Init list row and set mouse events on it
101 init_row: function(row)
103 // make references in internal array and set event handlers
104 if (row && String(row.id).match(/rcmrow([a-z0-9\-_=\+\/]+)/i)) {
108 this.rows[uid] = {uid:uid, id:row.id, obj:row};
110 // set eventhandlers to table row
111 row.onmousedown = function(e){ return self.drag_row(e, this.uid); };
112 row.onmouseup = function(e){ return self.click_row(e, this.uid); };
114 if (bw.iphone || bw.ipad) {
115 row.addEventListener('touchstart', function(e) {
116 if (e.touches.length == 1) {
117 if (!self.drag_row(rcube_event.touchevent(e.touches[0]), this.uid))
121 row.addEventListener('touchend', function(e) {
122 if (e.changedTouches.length == 1)
123 if (!self.click_row(rcube_event.touchevent(e.changedTouches[0]), this.uid))
129 row.onselectstart = function() { return false; };
131 this.row_init(this.rows[uid]);
137 * Init list column headers and set mouse events on them
139 init_header: function()
141 if (this.list && this.list.tHead) {
144 var col, r, p = this;
145 // add events for list columns moving
146 if (this.column_movable && this.list.tHead && this.list.tHead.rows) {
147 for (r=0; r<this.list.tHead.rows[0].cells.length; r++) {
148 if (this.column_fixed == r)
150 col = this.list.tHead.rows[0].cells[r];
151 col.onmousedown = function(e){ return p.drag_column(e, this); };
160 * Remove all list rows
164 var tbody = document.createElement('tbody');
166 this.list.insertBefore(tbody, this.list.tBodies[0]);
167 this.list.removeChild(this.list.tBodies[1]);
172 this.clear_selection();
177 * 'remove' message row from list (just hide it)
179 remove_row: function(uid, sel_next)
181 if (this.rows[uid].obj)
182 this.rows[uid].obj.style.display = 'none';
187 delete this.rows[uid];
193 * Add row to the list and initialize it
195 insert_row: function(row, attop)
197 var tbody = this.list.tBodies[0];
199 if (attop && tbody.rows.length)
200 tbody.insertBefore(row, tbody.firstChild);
202 tbody.appendChild(row);
211 * Set focus to the list
218 for (var n in this.selection) {
219 id = this.selection[n];
220 if (this.rows[id] && this.rows[id].obj) {
221 $(this.rows[id].obj).addClass('selected').removeClass('unfocused');
225 // Un-focus already focused elements
226 $('*:focus', window).blur();
227 $('iframe').each(function() { this.blur(); });
229 if (e || (e = window.event))
230 rcube_event.cancel(e);
235 * remove focus from the list
240 this.focused = false;
241 for (var n in this.selection) {
242 id = this.selection[n];
243 if (this.rows[id] && this.rows[id].obj) {
244 $(this.rows[id].obj).removeClass('selected').addClass('unfocused');
251 * onmousedown-handler of message list column
253 drag_column: function(e, col)
255 if (this.colcount > 1) {
256 this.drag_start = true;
257 this.drag_mouse_start = rcube_event.get_mouse_pos(e);
259 rcube_event.add_listener({event:'mousemove', object:this, method:'column_drag_mouse_move'});
260 rcube_event.add_listener({event:'mouseup', object:this, method:'column_drag_mouse_up'});
262 // enable dragging over iframes
265 // find selected column number
266 for (var i=0; i<this.list.tHead.rows[0].cells.length; i++) {
267 if (col == this.list.tHead.rows[0].cells[i]) {
268 this.selected_column = i;
279 * onmousedown-handler of message list row
281 drag_row: function(e, id)
283 // don't do anything (another action processed before)
284 var evtarget = rcube_event.get_target(e),
285 tagname = evtarget.tagName.toLowerCase();
287 if (this.dont_select || (evtarget && (tagname == 'input' || tagname == 'img')))
290 // accept right-clicks
291 if (rcube_event.get_button(e) == 2)
294 this.in_selection_before = this.in_selection(id) ? id : false;
296 // selects currently unselected row
297 if (!this.in_selection_before) {
298 var mod_key = rcube_event.get_modifier(e);
299 this.select_row(id, mod_key, false);
302 if (this.draggable && this.selection.length) {
303 this.drag_start = true;
304 this.drag_mouse_start = rcube_event.get_mouse_pos(e);
305 rcube_event.add_listener({event:'mousemove', object:this, method:'drag_mouse_move'});
306 rcube_event.add_listener({event:'mouseup', object:this, method:'drag_mouse_up'});
307 if (bw.iphone || bw.ipad) {
308 rcube_event.add_listener({event:'touchmove', object:this, method:'drag_mouse_move'});
309 rcube_event.add_listener({event:'touchend', object:this, method:'drag_mouse_up'});
312 // enable dragging over iframes
321 * onmouseup-handler of message list row
323 click_row: function(e, id)
325 var now = new Date().getTime(),
326 mod_key = rcube_event.get_modifier(e),
327 evtarget = rcube_event.get_target(e),
328 tagname = evtarget.tagName.toLowerCase();
330 if ((evtarget && (tagname == 'input' || tagname == 'img')))
333 // don't do anything (another action processed before)
334 if (this.dont_select) {
335 this.dont_select = false;
339 var dblclicked = now - this.rows[id].clicked < this.dblclick_time;
341 // unselects currently selected row
342 if (!this.drag_active && this.in_selection_before == id && !dblclicked)
343 this.select_row(id, mod_key, false);
345 this.drag_start = false;
346 this.in_selection_before = false;
348 // row was double clicked
349 if (this.rows && dblclicked && this.in_selection(id))
350 this.triggerEvent('dblclick');
352 this.triggerEvent('click');
354 if (!this.drag_active) {
357 rcube_event.cancel(e);
360 this.rows[id].clicked = now;
366 * Returns thread root ID for specified row ID
368 find_root: function(uid)
370 var r = this.rows[uid];
372 if (r && r.parent_uid)
373 return this.find_root(r.parent_uid);
379 expand_row: function(e, id)
381 var row = this.rows[id],
382 evtarget = rcube_event.get_target(e),
383 mod_key = rcube_event.get_modifier(e);
385 // Don't select this message
386 this.dont_select = true;
387 // Don't treat double click on the expando as double click on the message.
391 evtarget.className = 'collapsed';
392 if (mod_key == CONTROL_KEY || this.multiexpand)
393 this.collapse_all(row);
398 evtarget.className = 'expanded';
399 if (mod_key == CONTROL_KEY || this.multiexpand)
400 this.expand_all(row);
406 collapse: function(row)
408 row.expanded = false;
409 this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded });
410 var depth = row.depth;
411 var new_row = row ? row.obj.nextSibling : null;
415 if (new_row.nodeType == 1) {
416 var r = this.rows[new_row.uid];
417 if (r && r.depth <= depth)
419 $(new_row).css('display', 'none');
422 this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded });
425 new_row = new_row.nextSibling;
431 expand: function(row)
434 var last_expanded_parent_depth;
439 new_row = row.obj.nextSibling;
440 this.update_expando(row.uid, true);
441 this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded });
444 var tbody = this.list.tBodies[0];
445 new_row = tbody.firstChild;
447 last_expanded_parent_depth = 0;
451 if (new_row.nodeType == 1) {
452 var r = this.rows[new_row.uid];
454 if (row && (!r.depth || r.depth <= depth))
458 var p = this.rows[r.parent_uid];
459 if (p && p.expanded) {
460 if ((row && p == row) || last_expanded_parent_depth >= p.depth - 1) {
461 last_expanded_parent_depth = p.depth;
462 $(new_row).css('display', '');
464 this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded });
468 if (row && (! p || p.depth <= depth))
473 new_row = new_row.nextSibling;
480 collapse_all: function(row)
482 var depth, new_row, r;
485 row.expanded = false;
487 new_row = row.obj.nextSibling;
488 this.update_expando(row.uid);
489 this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded });
491 // don't collapse sub-root tree in multiexpand mode
492 if (depth && this.multiexpand)
496 new_row = this.list.tBodies[0].firstChild;
501 if (new_row.nodeType == 1) {
502 if (r = this.rows[new_row.uid]) {
503 if (row && (!r.depth || r.depth <= depth))
507 $(new_row).css('display', 'none');
508 if (r.has_children && r.expanded) {
510 this.update_expando(r.uid, false);
511 this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded });
515 new_row = new_row.nextSibling;
521 expand_all: function(row)
523 var depth, new_row, r;
528 new_row = row.obj.nextSibling;
529 this.update_expando(row.uid, true);
530 this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded });
533 new_row = this.list.tBodies[0].firstChild;
538 if (new_row.nodeType == 1) {
539 if (r = this.rows[new_row.uid]) {
540 if (row && r.depth <= depth)
543 $(new_row).css('display', '');
544 if (r.has_children && !r.expanded) {
546 this.update_expando(r.uid, true);
547 this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded });
551 new_row = new_row.nextSibling;
556 update_expando: function(uid, expanded)
558 var expando = document.getElementById('rcmexpando' + uid);
560 expando.className = expanded ? 'expanded' : 'collapsed';
565 * get first/next/previous/last rows that are not hidden
567 get_next_row: function()
572 var last_selected_row = this.rows[this.last_selected],
573 new_row = last_selected_row ? last_selected_row.obj.nextSibling : null;
575 while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
576 new_row = new_row.nextSibling;
581 get_prev_row: function()
586 var last_selected_row = this.rows[this.last_selected],
587 new_row = last_selected_row ? last_selected_row.obj.previousSibling : null;
589 while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
590 new_row = new_row.previousSibling;
595 get_first_row: function()
598 var i, len, rows = this.list.tBodies[0].rows;
600 for (i=0, len=rows.length-1; i<len; i++)
601 if (rows[i].id && String(rows[i].id).match(/rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
608 get_last_row: function()
611 var i, rows = this.list.tBodies[0].rows;
613 for (i=rows.length-1; i>=0; i--)
614 if (rows[i].id && String(rows[i].id).match(/rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
623 * selects or unselects the proper row depending on the modifier key pressed
625 select_row: function(id, mod_key, with_mouse)
627 var select_before = this.selection.join(',');
628 if (!this.multiselect)
631 if (!this.shift_start)
632 this.shift_start = id
635 this.shift_start = id;
636 this.highlight_row(id, false);
637 this.multi_selecting = false;
642 this.shift_select(id, false);
647 this.highlight_row(id, true);
650 case CONTROL_SHIFT_KEY:
651 this.shift_select(id, true);
655 this.highlight_row(id, false);
658 this.multi_selecting = true;
661 // trigger event if selection changed
662 if (this.selection.join(',') != select_before)
663 this.triggerEvent('select');
665 if (this.last_selected != 0 && this.rows[this.last_selected])
666 $(this.rows[this.last_selected].obj).removeClass('focused');
668 // unselect if toggleselect is active and the same row was clicked again
669 if (this.toggleselect && this.last_selected == id) {
670 this.clear_selection();
674 $(this.rows[id].obj).addClass('focused');
676 if (!this.selection.length)
677 this.shift_start = null;
679 this.last_selected = id;
684 * Alias method for select_row
688 this.select_row(id, false);
694 * Select row next to the last selected one.
695 * Either below or above.
697 select_next: function()
699 var next_row = this.get_next_row();
700 var prev_row = this.get_prev_row();
701 var new_row = (next_row) ? next_row : prev_row;
703 this.select_row(new_row.uid, false, false);
710 select_first: function(mod_key)
712 var row = this.get_first_row();
713 if (row && mod_key) {
714 this.shift_select(row, mod_key);
715 this.triggerEvent('select');
726 select_last: function(mod_key)
728 var row = this.get_last_row();
729 if (row && mod_key) {
730 this.shift_select(row, mod_key);
731 this.triggerEvent('select');
740 * Add all childs of the given row to selection
742 select_childs: function(uid)
744 if (!this.rows[uid] || !this.rows[uid].has_children)
747 var depth = this.rows[uid].depth;
748 var row = this.rows[uid].obj.nextSibling;
750 if (row.nodeType == 1) {
751 if ((r = this.rows[row.uid])) {
752 if (!r.depth || r.depth <= depth)
754 if (!this.in_selection(r.uid))
755 this.select_row(r.uid, CONTROL_KEY);
758 row = row.nextSibling;
764 * Perform selection when shift key is pressed
766 shift_select: function(id, control)
768 if (!this.rows[this.shift_start] || !this.selection.length)
769 this.shift_start = id;
771 var from_rowIndex = this.rows[this.shift_start].obj.rowIndex,
772 to_rowIndex = this.rows[id].obj.rowIndex,
773 i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex),
774 j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
776 // iterate through the entire message list
777 for (var n in this.rows) {
778 if (this.rows[n].obj.rowIndex >= i && this.rows[n].obj.rowIndex <= j) {
779 if (!this.in_selection(n)) {
780 this.highlight_row(n, true);
784 if (this.in_selection(n) && !control) {
785 this.highlight_row(n, true);
793 * Check if given id is part of the current selection
795 in_selection: function(id)
797 for(var n in this.selection)
798 if (this.selection[n]==id)
806 * Select each row in list
808 select_all: function(filter)
810 if (!this.rows || !this.rows.length)
813 // reset but remember selection first
814 var select_before = this.selection.join(',');
817 for (var n in this.rows) {
818 if (!filter || this.rows[n][filter] == true) {
819 this.last_selected = n;
820 this.highlight_row(n, true);
823 $(this.rows[n].obj).removeClass('selected').removeClass('unfocused');
827 // trigger event if selection changed
828 if (this.selection.join(',') != select_before)
829 this.triggerEvent('select');
840 invert_selection: function()
842 if (!this.rows || !this.rows.length)
845 // remember old selection
846 var select_before = this.selection.join(',');
848 for (var n in this.rows)
849 this.highlight_row(n, true);
851 // trigger event if selection changed
852 if (this.selection.join(',') != select_before)
853 this.triggerEvent('select');
862 * Unselect selected row(s)
864 clear_selection: function(id)
866 var num_select = this.selection.length;
870 for (var n in this.selection)
871 if (this.selection[n] == id) {
872 this.selection.splice(n,1);
878 for (var n in this.selection)
879 if (this.rows[this.selection[n]]) {
880 $(this.rows[this.selection[n]].obj).removeClass('selected').removeClass('unfocused');
886 if (num_select && !this.selection.length)
887 this.triggerEvent('select');
892 * Getter for the selection array
894 get_selection: function()
896 return this.selection;
901 * Return the ID if only one row is selected
903 get_single_selection: function()
905 if (this.selection.length == 1)
906 return this.selection[0];
913 * Highlight/unhighlight a row
915 highlight_row: function(id, multiple)
917 if (this.rows[id] && !multiple) {
918 if (this.selection.length > 1 || !this.in_selection(id)) {
919 this.clear_selection();
920 this.selection[0] = id;
921 $(this.rows[id].obj).addClass('selected');
924 else if (this.rows[id]) {
925 if (!this.in_selection(id)) { // select row
926 this.selection[this.selection.length] = id;
927 $(this.rows[id].obj).addClass('selected');
929 else { // unselect row
930 var p = $.inArray(id, this.selection);
931 var a_pre = this.selection.slice(0, p);
932 var a_post = this.selection.slice(p+1, this.selection.length);
933 this.selection = a_pre.concat(a_post);
934 $(this.rows[id].obj).removeClass('selected').removeClass('unfocused');
941 * Handler for keyboard events
943 key_press: function(e)
945 if (this.focused != true)
948 var keyCode = rcube_event.get_keycode(e);
949 var mod_key = rcube_event.get_modifier(e);
954 case 63233: // "down", in safari keypress
955 case 63232: // "up", in safari keypress
956 // Stop propagation so that the browser doesn't scroll
957 rcube_event.cancel(e);
958 return this.use_arrow_key(keyCode, mod_key);
960 case 107: // Plus sign on a numeric keypad (fc11 + firefox 3.5.2)
964 rcube_event.cancel(e);
965 var ret = this.use_plusminus_key(keyCode, mod_key);
966 this.key_pressed = keyCode;
967 this.triggerEvent('keypress');
970 this.select_first(mod_key);
971 return rcube_event.cancel(e);
973 this.select_last(mod_key);
974 return rcube_event.cancel(e);
976 this.shiftkey = e.shiftKey;
977 this.key_pressed = keyCode;
978 this.triggerEvent('keypress');
980 if (this.key_pressed == this.BACKSPACE_KEY)
981 return rcube_event.cancel(e);
988 * Handler for keydown events
990 key_down: function(e)
992 switch (rcube_event.get_keycode(e)) {
994 if (this.drag_active)
995 return this.drag_mouse_up(e);
996 if (this.col_drag_active) {
997 this.selected_column = null;
998 return this.column_drag_mouse_up(e);
1009 if (!rcube_event.get_modifier(e) && this.focused)
1010 return rcube_event.cancel(e);
1020 * Special handling method for arrow keys
1022 use_arrow_key: function(keyCode, mod_key)
1025 // Safari uses the nonstandard keycodes 63232/63233 for up/down, if we're
1026 // using the keypress event (but not the keydown or keyup event).
1027 if (keyCode == 40 || keyCode == 63233) // down arrow key pressed
1028 new_row = this.get_next_row();
1029 else if (keyCode == 38 || keyCode == 63232) // up arrow key pressed
1030 new_row = this.get_prev_row();
1033 this.select_row(new_row.uid, mod_key, true);
1034 this.scrollto(new_row.uid);
1042 * Special handling method for +/- keys
1044 use_plusminus_key: function(keyCode, mod_key)
1046 var selected_row = this.rows[this.last_selected];
1051 keyCode = selected_row.expanded ? 109 : 61;
1052 if (keyCode == 61 || keyCode == 107)
1053 if (mod_key == CONTROL_KEY || this.multiexpand)
1054 this.expand_all(selected_row);
1056 this.expand(selected_row);
1058 if (mod_key == CONTROL_KEY || this.multiexpand)
1059 this.collapse_all(selected_row);
1061 this.collapse(selected_row);
1063 this.update_expando(selected_row.uid, selected_row.expanded);
1070 * Try to scroll the list to make the specified row visible
1072 scrollto: function(id)
1074 var row = this.rows[id].obj;
1075 if (row && this.frame) {
1076 var scroll_to = Number(row.offsetTop);
1078 // expand thread if target row is hidden (collapsed)
1079 if (!scroll_to && this.rows[id].parent_uid) {
1080 var parent = this.find_root(this.rows[id].uid);
1081 this.expand_all(this.rows[parent]);
1082 scroll_to = Number(row.offsetTop);
1085 if (scroll_to < Number(this.frame.scrollTop))
1086 this.frame.scrollTop = scroll_to;
1087 else if (scroll_to + Number(row.offsetHeight) > Number(this.frame.scrollTop) + Number(this.frame.offsetHeight))
1088 this.frame.scrollTop = (scroll_to + Number(row.offsetHeight)) - Number(this.frame.offsetHeight);
1094 * Handler for mouse move events
1096 drag_mouse_move: function(e)
1098 // convert touch event
1099 if (e.type == 'touchmove') {
1100 if (e.changedTouches.length == 1)
1101 e = rcube_event.touchevent(e.changedTouches[0]);
1103 return rcube_event.cancel(e);
1106 if (this.drag_start) {
1107 // check mouse movement, of less than 3 pixels, don't start dragging
1108 var m = rcube_event.get_mouse_pos(e);
1110 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))
1113 if (!this.draglayer)
1114 this.draglayer = $('<div>').attr('id', 'rcmdraglayer')
1115 .css({ position:'absolute', display:'none', 'z-index':2000 })
1116 .appendTo(document.body);
1118 // also select childs of (collapsed) threads for dragging
1119 var n, uid, selection = $.merge([], this.selection);
1120 for (n in selection) {
1122 if (this.rows[uid].has_children && !this.rows[uid].expanded)
1123 this.select_childs(uid);
1127 this.draglayer.html('');
1129 // get subjects of selected messages
1130 var c, i, n, subject, obj;
1131 for (n=0; n<this.selection.length; n++) {
1132 // only show 12 lines
1134 this.draglayer.append('...');
1138 if (obj = this.rows[this.selection[n]].obj) {
1141 for (c=0, i=0; i<obj.childNodes.length; i++) {
1142 if (obj.childNodes[i].nodeName == 'TD') {
1144 this.drag_start_pos = $(obj.childNodes[i]).offset();
1146 if (this.subject_col < 0 || (this.subject_col >= 0 && this.subject_col == c)) {
1147 var entry, node, tmp_node, nodes = obj.childNodes[i].childNodes;
1149 for (m=0; m<nodes.length; m++) {
1150 if ((tmp_node = obj.childNodes[i].childNodes[m]) && (tmp_node.nodeType==3 || tmp_node.nodeName=='A'))
1157 subject = $(node).text();
1158 // remove leading spaces
1159 subject = $.trim(subject);
1160 // truncate line to 50 characters
1161 subject = (subject.length > 50 ? subject.substring(0, 50) + '...' : subject);
1163 entry = $('<div>').text(subject);
1164 this.draglayer.append(entry);
1173 this.draglayer.show();
1174 this.drag_active = true;
1175 this.triggerEvent('dragstart');
1178 if (this.drag_active && this.draglayer) {
1179 var pos = rcube_event.get_mouse_pos(e);
1180 this.draglayer.css({ left:(pos.x+20)+'px', top:(pos.y-5 + (bw.ie ? document.documentElement.scrollTop : 0))+'px' });
1181 this.triggerEvent('dragmove', e?e:window.event);
1184 this.drag_start = false;
1191 * Handler for mouse up events
1193 drag_mouse_up: function(e)
1195 document.onmousemove = null;
1197 if (e.type == 'touchend') {
1198 if (e.changedTouches.length != 1)
1199 return rcube_event.cancel(e);
1202 if (this.draglayer && this.draglayer.is(':visible')) {
1203 if (this.drag_start_pos)
1204 this.draglayer.animate(this.drag_start_pos, 300, 'swing').hide(20);
1206 this.draglayer.hide();
1209 if (this.drag_active)
1211 this.drag_active = false;
1213 rcube_event.remove_listener({event:'mousemove', object:this, method:'drag_mouse_move'});
1214 rcube_event.remove_listener({event:'mouseup', object:this, method:'drag_mouse_up'});
1216 if (bw.iphone || bw.ipad) {
1217 rcube_event.remove_listener({event:'touchmove', object:this, method:'drag_mouse_move'});
1218 rcube_event.remove_listener({event:'touchend', object:this, method:'drag_mouse_up'});
1224 this.triggerEvent('dragend');
1226 return rcube_event.cancel(e);
1231 * Handler for mouse move events for dragging list column
1233 column_drag_mouse_move: function(e)
1235 if (this.drag_start) {
1236 // check mouse movement, of less than 3 pixels, don't start dragging
1237 var i, m = rcube_event.get_mouse_pos(e);
1239 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))
1242 if (!this.col_draglayer) {
1243 var lpos = $(this.list).offset(),
1244 cells = this.list.tHead.rows[0].cells;
1246 // create dragging layer
1247 this.col_draglayer = $('<div>').attr('id', 'rcmcoldraglayer')
1248 .css(lpos).css({ position:'absolute', 'z-index':2001,
1249 'background-color':'white', opacity:0.75,
1250 height: (this.frame.offsetHeight-2)+'px', width: (this.frame.offsetWidth-2)+'px' })
1251 .appendTo(document.body)
1252 // ... and column position indicator
1253 .append($('<div>').attr('id', 'rcmcolumnindicator')
1254 .css({ position:'absolute', 'border-right':'2px dotted #555',
1255 'z-index':2002, height: (this.frame.offsetHeight-2)+'px' }));
1258 this.list_pos = this.list_min_pos = lpos.left;
1259 // save columns positions
1260 for (i=0; i<cells.length; i++) {
1261 this.cols[i] = cells[i].offsetWidth;
1262 if (this.column_fixed !== null && i <= this.column_fixed) {
1263 this.list_min_pos += this.cols[i];
1268 this.col_draglayer.show();
1269 this.col_drag_active = true;
1270 this.triggerEvent('column_dragstart');
1273 // set column indicator position
1274 if (this.col_drag_active && this.col_draglayer) {
1275 var i, cpos = 0, pos = rcube_event.get_mouse_pos(e);
1277 for (i=0; i<this.cols.length; i++) {
1278 if (pos.x >= this.cols[i]/2 + this.list_pos + cpos)
1279 cpos += this.cols[i];
1284 // handle fixed columns on left
1285 if (i == 0 && this.list_min_pos > pos.x)
1286 cpos = this.list_min_pos - this.list_pos;
1287 // empty list needs some assignment
1288 else if (!this.list.rowcount && i == this.cols.length)
1290 $('#rcmcolumnindicator').css({ width: cpos+'px'});
1291 this.triggerEvent('column_dragmove', e?e:window.event);
1294 this.drag_start = false;
1301 * Handler for mouse up events for dragging list columns
1303 column_drag_mouse_up: function(e)
1305 document.onmousemove = null;
1307 if (this.col_draglayer) {
1308 (this.col_draglayer).remove();
1309 this.col_draglayer = null;
1312 if (this.col_drag_active)
1314 this.col_drag_active = false;
1316 rcube_event.remove_listener({event:'mousemove', object:this, method:'column_drag_mouse_move'});
1317 rcube_event.remove_listener({event:'mouseup', object:this, method:'column_drag_mouse_up'});
1321 if (this.selected_column !== null && this.cols && this.cols.length) {
1322 var i, cpos = 0, pos = rcube_event.get_mouse_pos(e);
1324 // find destination position
1325 for (i=0; i<this.cols.length; i++) {
1326 if (pos.x >= this.cols[i]/2 + this.list_pos + cpos)
1327 cpos += this.cols[i];
1332 if (i != this.selected_column && i != this.selected_column+1) {
1333 this.column_replace(this.selected_column, i);
1337 this.triggerEvent('column_dragend');
1339 return rcube_event.cancel(e);
1344 * Creates a layer for drag&drop over iframes
1346 add_dragfix: function()
1348 $('iframe').each(function() {
1349 $('<div class="iframe-dragdrop-fix"></div>')
1350 .css({background: '#fff',
1351 width: this.offsetWidth+'px', height: this.offsetHeight+'px',
1352 position: 'absolute', opacity: '0.001', zIndex: 1000
1354 .css($(this).offset())
1355 .appendTo(document.body);
1361 * Removes the layer for drag&drop over iframes
1363 del_dragfix: function()
1365 $('div.iframe-dragdrop-fix').each(function() { this.parentNode.removeChild(this); });
1370 * Replaces two columns
1372 column_replace: function(from, to)
1374 var cells = this.list.tHead.rows[0].cells,
1377 td = document.createElement('td');
1379 // replace header cells
1381 cells[0].parentNode.insertBefore(td, before);
1383 cells[0].parentNode.appendChild(td);
1384 cells[0].parentNode.replaceChild(elem, td);
1386 // replace list cells
1387 for (r=0; r<this.list.tBodies[0].rows.length; r++) {
1388 row = this.list.tBodies[0].rows[r];
1390 elem = row.cells[from];
1391 before = row.cells[to];
1392 td = document.createElement('td');
1395 row.insertBefore(td, before);
1397 row.appendChild(td);
1398 row.replaceChild(elem, td);
1401 // update subject column position
1402 if (this.subject_col == from)
1403 this.subject_col = to > from ? to - 1 : to;
1404 else if (this.subject_col < from && to <= this.subject_col)
1406 else if (this.subject_col > from && to >= this.subject_col)
1409 this.triggerEvent('column_replace');
1414 rcube_list_widget.prototype.addEventListener = rcube_event_engine.prototype.addEventListener;
1415 rcube_list_widget.prototype.removeEventListener = rcube_event_engine.prototype.removeEventListener;
1416 rcube_list_widget.prototype.triggerEvent = rcube_event_engine.prototype.triggerEvent;