Aleksander Machniak
2015-01-16 7c26dbd36fdfa4f2f70d878d420ee59ad5c2aaac
commit | author | age
6b47de 1 /*
T 2  +-----------------------------------------------------------------------+
e019f2 3  | Roundcube List Widget                                                 |
6b47de 4  |                                                                       |
e019f2 5  | This file is part of the Roundcube Webmail client                     |
d94a71 6  | Copyright (C) 2006-2013, The Roundcube Dev Team                       |
7fe381 7  |                                                                       |
T 8  | Licensed under the GNU General Public License version 3 or            |
9  | any later version with exceptions for skins & plugins.                |
10  | See the README file for a full license statement.                     |
6b47de 11  |                                                                       |
T 12  +-----------------------------------------------------------------------+
13  | Authors: Thomas Bruederli <roundcube@gmail.com>                       |
14  |          Charles McNulty <charles@charlesmcnulty.com>                 |
15  +-----------------------------------------------------------------------+
16  | Requires: common.js                                                   |
17  +-----------------------------------------------------------------------+
18 */
19
20
21 /**
e019f2 22  * Roundcube List Widget class
538e64 23  * @constructor
6b47de 24  */
T 25 function rcube_list_widget(list, p)
8fa922 26 {
6b47de 27   // static contants
T 28   this.ENTER_KEY = 13;
29   this.DELETE_KEY = 46;
6e6e89 30   this.BACKSPACE_KEY = 8;
8fa922 31
6b47de 32   this.list = list ? list : null;
517dae 33   this.tagname = this.list ? this.list.nodeName.toLowerCase() : 'table';
6a9144 34   this.id_regexp = /^rcmrow([a-z0-9\-_=\+\/]+)/i;
85fece 35   this.rows = {};
6b47de 36   this.selection = [];
0dbac3 37   this.rowcount = 0;
b62c48 38   this.colcount = 0;
8fa922 39
d24d20 40   this.subject_col = -1;
699a25 41   this.modkey = 0;
6b47de 42   this.multiselect = false;
f52c93 43   this.multiexpand = false;
21168d 44   this.multi_selecting = false;
6b47de 45   this.draggable = false;
b62c48 46   this.column_movable = false;
6b47de 47   this.keyboard = false;
68b6a9 48   this.toggleselect = false;
8fa922 49
6b47de 50   this.drag_active = false;
b62c48 51   this.col_drag_active = false;
A 52   this.column_fixed = null;
6b47de 53   this.last_selected = 0;
b2fb95 54   this.shift_start = 0;
6b47de 55   this.focused = false;
T 56   this.drag_mouse_start = null;
c0e364 57   this.dblclick_time = 500; // default value on MS Windows is 500
517dae 58   this.row_init = function(){};  // @deprecated; use list.addEventListener('initrow') instead
8fa922 59
6b47de 60   // overwrite default paramaters
ef4f59 61   if (p && typeof p === 'object')
6b47de 62     for (var n in p)
T 63       this[n] = p[n];
8fa922 64 };
6b47de 65
T 66
67 rcube_list_widget.prototype = {
68
69
70 /**
71  * get all message rows from HTML table and init each row
72  */
73 init: function()
74 {
517dae 75   if (this.tagname == 'table' && this.list && this.list.tBodies[0]) {
TB 76     this.thead = this.list.tHead;
77     this.tbody = this.list.tBodies[0];
78   }
79   else if (this.tagname != 'table' && this.list) {
80     this.tbody = this.list;
81   }
82
83   if (this.tbody) {
85fece 84     this.rows = {};
0dbac3 85     this.rowcount = 0;
6b47de 86
517dae 87     var r, len, rows = this.tbody.childNodes;
6b47de 88
0e7b66 89     for (r=0, len=rows.length; r<len; r++) {
3ab616 90       this.rowcount += this.init_row(rows[r]) ? 1 : 0;
6b47de 91     }
T 92
b62c48 93     this.init_header();
6b47de 94     this.frame = this.list.parentNode;
T 95
96     // set body events
17a8fb 97     if (this.keyboard)
AM 98       rcube_event.add_listener({event:'keydown', object:this, method:'key_press'});
6b47de 99   }
772bec 100
AM 101   return this;
6b47de 102 },
T 103
104
105 /**
c4b819 106  * Init list row and set mouse events on it
6b47de 107  */
T 108 init_row: function(row)
109 {
110   // make references in internal array and set event handlers
6a9144 111   if (row && String(row.id).match(this.id_regexp)) {
1633bc 112     var self = this,
A 113       uid = RegExp.$1;
6b47de 114     row.uid = uid;
f52c93 115     this.rows[uid] = {uid:uid, id:row.id, obj:row};
6b47de 116
T 117     // set eventhandlers to table row
8ef2f3 118     row.onmousedown = function(e){ return self.drag_row(e, this.uid); };
T 119     row.onmouseup = function(e){ return self.click_row(e, this.uid); };
1ce442 120
7c26db 121     if (bw.touch && row.addEventListener) {
8ef2f3 122       row.addEventListener('touchstart', function(e) {
T 123         if (e.touches.length == 1) {
dc8400 124           self.touchmoved = false;
TB 125           self.drag_row(rcube_event.touchevent(e.touches[0]), this.uid)
8ef2f3 126         }
T 127       }, false);
128       row.addEventListener('touchend', function(e) {
dc8400 129         if (e.changedTouches.length == 1) {
TB 130           if (!self.touchmoved && !self.click_row(rcube_event.touchevent(e.changedTouches[0]), this.uid))
8ef2f3 131             e.preventDefault();
dc8400 132         }
TB 133       }, false);
134       row.addEventListener('touchmove', function(e) {
135         if (e.changedTouches.length == 1) {
136           self.touchmoved = true;
137           if (self.drag_active)
138             e.preventDefault();
139         }
8ef2f3 140       }, false);
T 141     }
6b47de 142
T 143     if (document.all)
144       row.onselectstart = function() { return false; };
145
517dae 146     this.row_init(this.rows[uid]);  // legacy support
TB 147     this.triggerEvent('initrow', this.rows[uid]);
3ab616 148
AM 149     return true;
b62c48 150   }
A 151 },
152
153
154 /**
155  * Init list column headers and set mouse events on them
156  */
157 init_header: function()
158 {
517dae 159   if (this.thead) {
b62c48 160     this.colcount = 0;
A 161
73ad4f 162     if (this.fixed_header) {  // copy (modified) fixed header back to the actual table
TB 163       $(this.list.tHead).replaceWith($(this.fixed_header).find('thead').clone());
164       $(this.list.tHead).find('tr td').attr('style', '');  // remove fixed widths
165     }
4910b0 166     else if (!bw.touch && this.list.className.indexOf('fixedheader') >= 0) {
73ad4f 167       this.init_fixed_header();
TB 168     }
169
b62c48 170     var col, r, p = this;
A 171     // add events for list columns moving
517dae 172     if (this.column_movable && this.thead && this.thead.rows) {
TB 173       for (r=0; r<this.thead.rows[0].cells.length; r++) {
b62c48 174         if (this.column_fixed == r)
A 175           continue;
517dae 176         col = this.thead.rows[0].cells[r];
b62c48 177         col.onmousedown = function(e){ return p.drag_column(e, this); };
A 178         this.colcount++;
179       }
180     }
6b47de 181   }
T 182 },
183
73ad4f 184 init_fixed_header: function()
TB 185 {
186   var clone = $(this.list.tHead).clone();
187
188   if (!this.fixed_header) {
189     this.fixed_header = $('<table>')
8efdd9 190       .attr('class', this.list.className + ' fixedcopy')
73ad4f 191       .css({ position:'fixed' })
TB 192       .append(clone)
193       .append('<tbody></tbody>');
194     $(this.list).before(this.fixed_header);
195
196     var me = this;
197     $(window).resize(function(){ me.resize() });
4ae28f 198     $(window).scroll(function(){
TB 199       var w = $(window);
200       me.fixed_header.css('marginLeft', (-w.scrollLeft()) + 'px');
201       if (!bw.webkit)
202         me.fixed_header.css('marginTop', (-w.scrollTop()) + 'px');
203     });
73ad4f 204   }
TB 205   else {
206     $(this.fixed_header).find('thead').replaceWith(clone);
207   }
208
209   this.thead = clone.get(0);
210   this.resize();
211 },
212
213 resize: function()
214 {
215     if (!this.fixed_header)
216       return;
217
218     var column_widths = [];
219
220     // get column widths from original thead
221     $(this.tbody).parent().find('thead tr td').each(function(index) {
222       column_widths[index] = $(this).width();
223     });
224
225     // apply fixed widths to fixed table header
226     $(this.thead).parent().width($(this.tbody).parent().width());
227     $(this.thead).find('tr td').each(function(index) {
228       $(this).css('width', column_widths[index]);
229     });
cbd8f7 230
TB 231     $(window).scroll();
73ad4f 232 },
6b47de 233
T 234 /**
c4b819 235  * Remove all list rows
6b47de 236  */
f11541 237 clear: function(sel)
6b47de 238 {
517dae 239   if (this.tagname == 'table') {
TB 240     var tbody = document.createElement('tbody');
241     this.list.insertBefore(tbody, this.tbody);
242     this.list.removeChild(this.list.tBodies[1]);
243     this.tbody = tbody;
244   }
245   else {
246     $(this.row_tagname() + ':not(.thead)', this.tbody).remove();
247   }
54531f 248
85fece 249   this.rows = {};
0dbac3 250   this.rowcount = 0;
8fa922 251
A 252   if (sel)
253     this.clear_selection();
1633bc 254
A 255   // reset scroll position (in Opera)
256   if (this.frame)
257     this.frame.scrollTop = 0;
fd0c12 258
AM 259   // fix list header after removing any rows
260   this.resize();
6b47de 261 },
T 262
263
264 /**
265  * 'remove' message row from list (just hide it)
266  */
b62656 267 remove_row: function(uid, sel_next)
6b47de 268 {
fd0c12 269   var self = this, node = this.rows[uid] ? this.rows[uid].obj : null;
d741a9 270
517dae 271   if (!node)
d741a9 272     return;
A 273
517dae 274   node.style.display = 'none';
6b47de 275
b62656 276   if (sel_next)
T 277     this.select_next();
278
0e7b66 279   delete this.rows[uid];
0dbac3 280   this.rowcount--;
fd0c12 281
AM 282   // fix list header after removing any rows
283   clearTimeout(this.resize_timeout)
284   this.resize_timeout = setTimeout(function() { self.resize(); }, 50);
6b47de 285 },
T 286
287
288 /**
c4b819 289  * Add row to the list and initialize it
6b47de 290  */
ea6d69 291 insert_row: function(row, before)
6b47de 292 {
fd0c12 293   var self = this, tbody = this.tbody;
6b47de 294
517dae 295   // create a real dom node first
TB 296   if (row.nodeName === undefined) {
297     // for performance reasons use DOM instead of jQuery here
298     var domrow = document.createElement(this.row_tagname());
299     if (row.id) domrow.id = row.id;
300     if (row.className) domrow.className = row.className;
301     if (row.style) $.extend(domrow.style, row.style);
302
3a04a3 303     for (var e, domcell, col, i=0; row.cols && i < row.cols.length; i++) {
517dae 304       col = row.cols[i];
TB 305       domcell = document.createElement(this.col_tagname());
306       if (col.className) domcell.className = col.className;
307       if (col.innerHTML) domcell.innerHTML = col.innerHTML;
3a04a3 308       for (e in col.events)
AM 309         domcell['on' + e] = col.events[e];
517dae 310       domrow.appendChild(domcell);
TB 311     }
312
313     row = domrow;
314   }
315
a52297 316   if (before && tbody.childNodes.length)
ea6d69 317     tbody.insertBefore(row, (typeof before == 'object' && before.parentNode == tbody) ? before : tbody.firstChild);
6b47de 318   else
c4b819 319     tbody.appendChild(row);
6b47de 320
c4b819 321   this.init_row(row);
0dbac3 322   this.rowcount++;
fd0c12 323
AM 324   // fix list header after adding any rows
325   clearTimeout(this.resize_timeout)
326   this.resize_timeout = setTimeout(function() { self.resize(); }, 50);
6b47de 327 },
T 328
517dae 329 /**
TB 330  * 
331  */
332 update_row: function(id, cols, newid, select)
333 {
334   var row = this.rows[id];
335   if (!row) return false;
336
337   var domrow = row.obj;
338   for (var domcell, col, i=0; cols && i < cols.length; i++) {
339     this.get_cell(domrow, i).html(cols[i]);
340   }
341
342   if (newid) {
343     delete this.rows[id];
344     domrow.id = 'rcmrow' + newid;
345     this.init_row(domrow);
346
347     if (select)
348       this.selection[0] = newid;
349   }
350 },
6b47de 351
T 352
353 /**
25c35c 354  * Set focus to the list
6b47de 355  */
T 356 focus: function(e)
357 {
1633bc 358   var n, id;
6b47de 359   this.focused = true;
2c2000 360
1633bc 361   for (n in this.selection) {
6b47de 362     id = this.selection[n];
cc97ea 363     if (this.rows[id] && this.rows[id].obj) {
T 364       $(this.rows[id].obj).addClass('selected').removeClass('unfocused');
6b47de 365     }
T 366   }
367
e26399 368   // Un-focus already focused elements (#1487123, #1487316, #1488600, #1488620)
3db62c 369   // It looks that window.focus() does the job for all browsers, but not Firefox (#1489058)
c0d8ec 370   $('iframe,:focus:not(body)').blur();
3db62c 371   window.focus();
2c2000 372
6b47de 373   if (e || (e = window.event))
T 374     rcube_event.cancel(e);
375 },
376
377
378 /**
379  * remove focus from the list
380  */
381 blur: function()
382 {
1633bc 383   var n, id;
6b47de 384   this.focused = false;
1633bc 385   for (n in this.selection) {
6b47de 386     id = this.selection[n];
cc97ea 387     if (this.rows[id] && this.rows[id].obj) {
3c6715 388       $(this.rows[id].obj).removeClass('selected focused').addClass('unfocused');
6b47de 389     }
T 390   }
391 },
392
393
394 /**
b62c48 395  * onmousedown-handler of message list column
A 396  */
397 drag_column: function(e, col)
398 {
399   if (this.colcount > 1) {
400     this.drag_start = true;
401     this.drag_mouse_start = rcube_event.get_mouse_pos(e);
402
403     rcube_event.add_listener({event:'mousemove', object:this, method:'column_drag_mouse_move'});
404     rcube_event.add_listener({event:'mouseup', object:this, method:'column_drag_mouse_up'});
405
406     // enable dragging over iframes
407     this.add_dragfix();
408
409     // find selected column number
517dae 410     for (var i=0; i<this.thead.rows[0].cells.length; i++) {
TB 411       if (col == this.thead.rows[0].cells[i]) {
b62c48 412         this.selected_column = i;
A 413         break;
414       }
415     }
416   }
417
418   return false;
419 },
420
421
422 /**
6b47de 423  * onmousedown-handler of message list row
T 424  */
425 drag_row: function(e, id)
426 {
427   // don't do anything (another action processed before)
bc35e8 428   if (!this.is_event_target(e))
40d7c2 429     return true;
8fa922 430
f89f03 431   // accept right-clicks
T 432   if (rcube_event.get_button(e) == 2)
433     return true;
8fa922 434
052a6a 435   this.in_selection_before = e && e.istouch || this.in_selection(id) ? id : false;
AM 436
6b47de 437   // selects currently unselected row
052a6a 438   if (!this.in_selection_before) {
6b47de 439     var mod_key = rcube_event.get_modifier(e);
052a6a 440     this.select_row(id, mod_key, true);
6b47de 441   }
T 442
dc8400 443   if (this.draggable && this.selection.length && this.in_selection(id)) {
6b47de 444     this.drag_start = true;
b2fb95 445     this.drag_mouse_start = rcube_event.get_mouse_pos(e);
052a6a 446
6c11ee 447     rcube_event.add_listener({event:'mousemove', object:this, method:'drag_mouse_move'});
A 448     rcube_event.add_listener({event:'mouseup', object:this, method:'drag_mouse_up'});
4910b0 449     if (bw.touch) {
8ef2f3 450       rcube_event.add_listener({event:'touchmove', object:this, method:'drag_mouse_move'});
T 451       rcube_event.add_listener({event:'touchend', object:this, method:'drag_mouse_up'});
452     }
cf6bc5 453
6c11ee 454     // enable dragging over iframes
b62c48 455     this.add_dragfix();
6b47de 456   }
T 457
458   return false;
459 },
460
461
462 /**
463  * onmouseup-handler of message list row
464  */
465 click_row: function(e, id)
466 {
bc35e8 467   // don't do anything (another action processed before)
AM 468   if (!this.is_event_target(e))
40d7c2 469     return true;
8fa922 470
bc35e8 471   var now = new Date().getTime(),
AM 472     dblclicked = now - this.rows[id].clicked < this.dblclick_time;
6b47de 473
052a6a 474   // unselects currently selected row
AM 475   if (!this.drag_active && !dblclicked && this.in_selection_before == id)
bc35e8 476     this.select_row(id, rcube_event.get_modifier(e), true);
89e507 477
6b47de 478   this.drag_start = false;
052a6a 479   this.in_selection_before = false;
6b47de 480
T 481   // row was double clicked
85fece 482   if (this.rowcount && dblclicked && this.in_selection(id)) {
cc97ea 483     this.triggerEvent('dblclick');
fc643e 484     now = 0;
T 485   }
6b47de 486   else
cc97ea 487     this.triggerEvent('click');
6b47de 488
dd51b7 489   if (!this.drag_active) {
A 490     // remove temp divs
b62c48 491     this.del_dragfix();
6b47de 492     rcube_event.cancel(e);
dd51b7 493   }
6b47de 494
T 495   this.rows[id].clicked = now;
496   return false;
497 },
498
499
bc35e8 500 /**
AM 501  * Check target of the current event
502  */
503 is_event_target: function(e)
504 {
505   var target = rcube_event.get_target(e),
506     tagname = target.tagName.toLowerCase();
507
508   return !(target && (tagname == 'input' || tagname == 'img' || (tagname != 'a' && target.onclick)));
509 },
510
511
bc2acc 512 /*
A 513  * Returns thread root ID for specified row ID
514  */
515 find_root: function(uid)
516 {
517    var r = this.rows[uid];
518
519    if (r && r.parent_uid)
520      return this.find_root(r.parent_uid);
521    else
522      return uid;
523 },
524
525
f52c93 526 expand_row: function(e, id)
T 527 {
54531f 528   var row = this.rows[id],
A 529     evtarget = rcube_event.get_target(e),
530     mod_key = rcube_event.get_modifier(e);
f52c93 531
T 532   // Don't treat double click on the expando as double click on the message.
533   row.clicked = 0;
534
535   if (row.expanded) {
54531f 536     evtarget.className = 'collapsed';
f52c93 537     if (mod_key == CONTROL_KEY || this.multiexpand)
T 538       this.collapse_all(row);
539     else
540       this.collapse(row);
541   }
542   else {
54531f 543     evtarget.className = 'expanded';
f52c93 544     if (mod_key == CONTROL_KEY || this.multiexpand)
T 545       this.expand_all(row);
546     else
547      this.expand(row);
548   }
549 },
550
551 collapse: function(row)
552 {
7eecf8 553   var r, depth = row.depth,
AM 554     new_row = row ? row.obj.nextSibling : null;
555
f52c93 556   row.expanded = false;
32afef 557   this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded, obj:row.obj });
f52c93 558
T 559   while (new_row) {
560     if (new_row.nodeType == 1) {
7eecf8 561       r = this.rows[new_row.uid];
f52c93 562       if (r && r.depth <= depth)
T 563         break;
7eecf8 564
a3c9bd 565       $(new_row).css('display', 'none');
54531f 566       if (r.expanded) {
A 567         r.expanded = false;
32afef 568         this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded, obj:new_row });
54531f 569       }
f52c93 570     }
T 571     new_row = new_row.nextSibling;
572   }
573
73ad4f 574   this.resize();
d94a71 575   this.triggerEvent('listupdate');
7eecf8 576
f52c93 577   return false;
T 578 },
579
580 expand: function(row)
581 {
1633bc 582   var r, p, depth, new_row, last_expanded_parent_depth;
f52c93 583
T 584   if (row) {
585     row.expanded = true;
586     depth = row.depth;
587     new_row = row.obj.nextSibling;
bc2acc 588     this.update_expando(row.uid, true);
32afef 589     this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded, obj:row.obj });
f52c93 590   }
T 591   else {
517dae 592     var tbody = this.tbody;
f52c93 593     new_row = tbody.firstChild;
T 594     depth = 0;
595     last_expanded_parent_depth = 0;
596   }
597
598   while (new_row) {
599     if (new_row.nodeType == 1) {
1633bc 600       r = this.rows[new_row.uid];
f52c93 601       if (r) {
T 602         if (row && (!r.depth || r.depth <= depth))
603           break;
604
605         if (r.parent_uid) {
1633bc 606           p = this.rows[r.parent_uid];
f52c93 607           if (p && p.expanded) {
T 608             if ((row && p == row) || last_expanded_parent_depth >= p.depth - 1) {
609               last_expanded_parent_depth = p.depth;
a3c9bd 610               $(new_row).css('display', '');
f52c93 611               r.expanded = true;
32afef 612               this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded, obj:new_row });
f52c93 613             }
T 614           }
615           else
616             if (row && (! p || p.depth <= depth))
617               break;
618         }
619       }
620     }
621     new_row = new_row.nextSibling;
622   }
623
73ad4f 624   this.resize();
d94a71 625   this.triggerEvent('listupdate');
f52c93 626   return false;
T 627 },
628
629
630 collapse_all: function(row)
631 {
54531f 632   var depth, new_row, r;
f52c93 633
T 634   if (row) {
635     row.expanded = false;
636     depth = row.depth;
637     new_row = row.obj.nextSibling;
bc2acc 638     this.update_expando(row.uid);
32afef 639     this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded, obj:row.obj });
8fa922 640
f52c93 641     // don't collapse sub-root tree in multiexpand mode 
T 642     if (depth && this.multiexpand)
54531f 643       return false;
f52c93 644   }
T 645   else {
517dae 646     new_row = this.tbody.firstChild;
f52c93 647     depth = 0;
T 648   }
649
650   while (new_row) {
651     if (new_row.nodeType == 1) {
54531f 652       if (r = this.rows[new_row.uid]) {
f52c93 653         if (row && (!r.depth || r.depth <= depth))
T 654           break;
655
656         if (row || r.depth)
a3c9bd 657           $(new_row).css('display', 'none');
54531f 658         if (r.has_children && r.expanded) {
f52c93 659           r.expanded = false;
54531f 660           this.update_expando(r.uid, false);
32afef 661           this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded, obj:new_row });
f52c93 662         }
T 663       }
664     }
665     new_row = new_row.nextSibling;
666   }
667
73ad4f 668   this.resize();
d94a71 669   this.triggerEvent('listupdate');
f52c93 670   return false;
T 671 },
672
2b55d4 673
f52c93 674 expand_all: function(row)
T 675 {
54531f 676   var depth, new_row, r;
f52c93 677
T 678   if (row) {
679     row.expanded = true;
680     depth = row.depth;
681     new_row = row.obj.nextSibling;
bc2acc 682     this.update_expando(row.uid, true);
32afef 683     this.triggerEvent('expandcollapse', { uid:row.uid, expanded:row.expanded, obj:row.obj });
f52c93 684   }
T 685   else {
517dae 686     new_row = this.tbody.firstChild;
f52c93 687     depth = 0;
T 688   }
689
690   while (new_row) {
691     if (new_row.nodeType == 1) {
54531f 692       if (r = this.rows[new_row.uid]) {
f52c93 693         if (row && r.depth <= depth)
T 694           break;
695
a3c9bd 696         $(new_row).css('display', '');
54531f 697         if (r.has_children && !r.expanded) {
f52c93 698           r.expanded = true;
54531f 699           this.update_expando(r.uid, true);
32afef 700           this.triggerEvent('expandcollapse', { uid:r.uid, expanded:r.expanded, obj:new_row });
f52c93 701         }
T 702       }
703     }
704     new_row = new_row.nextSibling;
705   }
d94a71 706
73ad4f 707   this.resize();
d94a71 708   this.triggerEvent('listupdate');
f52c93 709   return false;
T 710 },
2b55d4 711
bc2acc 712
A 713 update_expando: function(uid, expanded)
714 {
715   var expando = document.getElementById('rcmexpando' + uid);
716   if (expando)
717     expando.className = expanded ? 'expanded' : 'collapsed';
718 },
719
f52c93 720
6b47de 721 /**
49771b 722  * get first/next/previous/last rows that are not hidden
6b47de 723  */
T 724 get_next_row: function()
725 {
85fece 726   if (!this.rowcount)
6b47de 727     return false;
T 728
54531f 729   var last_selected_row = this.rows[this.last_selected],
A 730     new_row = last_selected_row ? last_selected_row.obj.nextSibling : null;
731
6b47de 732   while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
T 733     new_row = new_row.nextSibling;
734
735   return new_row;
736 },
737
738 get_prev_row: function()
739 {
85fece 740   if (!this.rowcount)
6b47de 741     return false;
T 742
54531f 743   var last_selected_row = this.rows[this.last_selected],
A 744     new_row = last_selected_row ? last_selected_row.obj.previousSibling : null;
745
6b47de 746   while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none'))
T 747     new_row = new_row.previousSibling;
748
749   return new_row;
49771b 750 },
A 751
752 get_first_row: function()
753 {
8fa922 754   if (this.rowcount) {
517dae 755     var i, len, rows = this.tbody.childNodes;
49771b 756
0e7b66 757     for (i=0, len=rows.length-1; i<len; i++)
6a9144 758       if (rows[i].id && String(rows[i].id).match(this.id_regexp) && this.rows[RegExp.$1] != null)
3c6715 759         return RegExp.$1;
8fa922 760   }
49771b 761
A 762   return null;
6b47de 763 },
T 764
095d05 765 get_last_row: function()
A 766 {
8fa922 767   if (this.rowcount) {
517dae 768     var i, rows = this.tbody.childNodes;
6b47de 769
0e7b66 770     for (i=rows.length-1; i>=0; i--)
6a9144 771       if (rows[i].id && String(rows[i].id).match(this.id_regexp) && this.rows[RegExp.$1] != null)
0e7b66 772         return RegExp.$1;
8fa922 773   }
095d05 774
A 775   return null;
776 },
777
517dae 778 row_tagname: function()
TB 779 {
780   var row_tagnames = { table:'tr', ul:'li', '*':'div' };
781   return row_tagnames[this.tagname] || row_tagnames['*'];
782 },
783
784 col_tagname: function()
785 {
786   var col_tagnames = { table:'td', '*':'span' };
787   return col_tagnames[this.tagname] || col_tagnames['*'];
788 },
789
790 get_cell: function(row, index)
791 {
792   return $(this.col_tagname(), row).eq(index);
793 },
095d05 794
A 795 /**
796  * selects or unselects the proper row depending on the modifier key pressed
797  */
6b47de 798 select_row: function(id, mod_key, with_mouse)
T 799 {
800   var select_before = this.selection.join(',');
052a6a 801
6b47de 802   if (!this.multiselect)
T 803     mod_key = 0;
8fa922 804
b2fb95 805   if (!this.shift_start)
T 806     this.shift_start = id
6b47de 807
8fa922 808   if (!mod_key) {
6b47de 809     this.shift_start = id;
T 810     this.highlight_row(id, false);
21168d 811     this.multi_selecting = false;
6b47de 812   }
8fa922 813   else {
A 814     switch (mod_key) {
6b47de 815       case SHIFT_KEY:
b2fb95 816         this.shift_select(id, false);
6b47de 817         break;
T 818
819       case CONTROL_KEY:
e769a7 820         if (with_mouse) {
C 821           this.shift_start = id;
b2fb95 822           this.highlight_row(id, true);
e769a7 823         }
699a25 824         break;
6b47de 825
T 826       case CONTROL_SHIFT_KEY:
827         this.shift_select(id, true);
828         break;
829
830       default:
b2fb95 831         this.highlight_row(id, false);
6b47de 832         break;
T 833     }
052a6a 834
21168d 835     this.multi_selecting = true;
6b47de 836   }
T 837
838   // trigger event if selection changed
839   if (this.selection.join(',') != select_before)
cc97ea 840     this.triggerEvent('select');
6b47de 841
T 842   if (this.last_selected != 0 && this.rows[this.last_selected])
cc97ea 843     $(this.rows[this.last_selected].obj).removeClass('focused');
a9dda5 844
T 845   // unselect if toggleselect is active and the same row was clicked again
8fa922 846   if (this.toggleselect && this.last_selected == id) {
a9dda5 847     this.clear_selection();
T 848     id = null;
849   }
850   else
cc97ea 851     $(this.rows[id].obj).addClass('focused');
a9dda5 852
b2fb95 853   if (!this.selection.length)
T 854     this.shift_start = null;
6b47de 855
T 856   this.last_selected = id;
857 },
858
859
860 /**
861  * Alias method for select_row
862  */
863 select: function(id)
864 {
865   this.select_row(id, false);
866   this.scrollto(id);
867 },
868
869
870 /**
871  * Select row next to the last selected one.
872  * Either below or above.
873  */
874 select_next: function()
875 {
1633bc 876   var next_row = this.get_next_row(),
A 877     prev_row = this.get_prev_row(),
878     new_row = (next_row) ? next_row : prev_row;
879
6b47de 880   if (new_row)
1ce442 881     this.select_row(new_row.uid, false, false);
6b47de 882 },
T 883
bc2acc 884
49771b 885 /**
A 886  * Select first row 
887  */
bc2acc 888 select_first: function(mod_key)
49771b 889 {
bc2acc 890   var row = this.get_first_row();
1633bc 891   if (row) {
03da10 892     this.select_row(row, mod_key, false);
AM 893     this.scrollto(row);
bc2acc 894   }
49771b 895 },
bc2acc 896
A 897
898 /**
2b55d4 899  * Select last row
bc2acc 900  */
A 901 select_last: function(mod_key)
902 {
903   var row = this.get_last_row();
1633bc 904   if (row) {
03da10 905     this.select_row(row, mod_key, false);
AM 906     this.scrollto(row);
bc2acc 907   }
A 908 },
909
49771b 910
84a331 911 /**
T 912  * Add all childs of the given row to selection
913  */
2b55d4 914 select_children: function(uid)
84a331 915 {
2b55d4 916   var i, children = this.row_children(uid), len = children.length;
8fa922 917
2b55d4 918   for (i=0; i<len; i++)
AM 919     if (!this.in_selection(children[i]))
5c7bbf 920       this.select_row(children[i], CONTROL_KEY, true);
84a331 921 },
T 922
6b47de 923
T 924 /**
925  * Perform selection when shift key is pressed
926  */
927 shift_select: function(id, control)
928 {
b00bd0 929   if (!this.rows[this.shift_start] || !this.selection.length)
f2892d 930     this.shift_start = id;
A 931
50cc5b 932   var n, i, j, to_row = this.rows[id],
517dae 933     from_rowIndex = this._rowIndex(this.rows[this.shift_start].obj),
TB 934     to_rowIndex = this._rowIndex(to_row.obj);
50cc5b 935
d19417 936   // if we're going down the list, and we hit a thread, and it's closed, select the whole thread
CM 937   if (from_rowIndex < to_rowIndex && !to_row.expanded && to_row.has_children)
50cc5b 938     if (to_row = this.rows[(this.row_children(id)).pop()])
517dae 939       to_rowIndex = this._rowIndex(to_row.obj);
50cc5b 940
AM 941   i = ((from_rowIndex < to_rowIndex) ? from_rowIndex : to_rowIndex),
942   j = ((from_rowIndex > to_rowIndex) ? from_rowIndex : to_rowIndex);
6b47de 943
T 944   // iterate through the entire message list
1633bc 945   for (n in this.rows) {
517dae 946     if (this._rowIndex(this.rows[n].obj) >= i && this._rowIndex(this.rows[n].obj) <= j) {
f52c93 947       if (!this.in_selection(n)) {
6b47de 948         this.highlight_row(n, true);
f52c93 949       }
6b47de 950     }
8fa922 951     else {
1633bc 952       if (this.in_selection(n) && !control) {
6b47de 953         this.highlight_row(n, true);
f52c93 954       }
6b47de 955     }
T 956   }
957 },
958
d19417 959
517dae 960 /**
TB 961  * Helper method to emulate the rowIndex property of non-tr elements
962  */
963 _rowIndex: function(obj)
964 {
965   return (obj.rowIndex !== undefined) ? obj.rowIndex : $(obj).prevAll().length;
966 },
6b47de 967
T 968 /**
969  * Check if given id is part of the current selection
970  */
971 in_selection: function(id)
972 {
1633bc 973   for (var n in this.selection)
7eecf8 974     if (this.selection[n] == id)
6b47de 975       return true;
T 976
f52c93 977   return false;
6b47de 978 },
T 979
980
981 /**
982  * Select each row in list
983  */
984 select_all: function(filter)
985 {
85fece 986   if (!this.rowcount)
6b47de 987     return false;
T 988
1094cd 989   // reset but remember selection first
1633bc 990   var n, select_before = this.selection.join(',');
8fa922 991   this.selection = [];
A 992
1633bc 993   for (n in this.rows) {
0e7b66 994     if (!filter || this.rows[n][filter] == true) {
6b47de 995       this.last_selected = n;
ad827b 996       this.highlight_row(n, true, true);
6b47de 997     }
0e7b66 998     else {
eaacbe 999       $(this.rows[n].obj).removeClass('selected').removeClass('unfocused');
874717 1000     }
6b47de 1001   }
T 1002
1094cd 1003   // trigger event if selection changed
T 1004   if (this.selection.join(',') != select_before)
cc97ea 1005     this.triggerEvent('select');
1094cd 1006
d7c226 1007   this.focus();
A 1008
1094cd 1009   return true;
6b47de 1010 },
T 1011
1012
1013 /**
528185 1014  * Invert selection
A 1015  */
1016 invert_selection: function()
1017 {
85fece 1018   if (!this.rowcount)
528185 1019     return false;
A 1020
1021   // remember old selection
1633bc 1022   var n, select_before = this.selection.join(',');
8fa922 1023
1633bc 1024   for (n in this.rows)
f52c93 1025     this.highlight_row(n, true);
528185 1026
A 1027   // trigger event if selection changed
1028   if (this.selection.join(',') != select_before)
1029     this.triggerEvent('select');
1030
1031   this.focus();
1032
1033   return true;
1034 },
1035
1036
1037 /**
095d05 1038  * Unselect selected row(s)
6b47de 1039  */
de3c33 1040 clear_selection: function(id, no_event)
6b47de 1041 {
1633bc 1042   var n, num_select = this.selection.length;
095d05 1043
A 1044   // one row
8fa922 1045   if (id) {
1633bc 1046     for (n in this.selection)
cc97ea 1047       if (this.selection[n] == id) {
T 1048         this.selection.splice(n,1);
1049         break;
1050       }
8fa922 1051   }
095d05 1052   // all rows
8fa922 1053   else {
1633bc 1054     for (n in this.selection)
cc97ea 1055       if (this.rows[this.selection[n]]) {
T 1056         $(this.rows[this.selection[n]].obj).removeClass('selected').removeClass('unfocused');
8fa922 1057       }
A 1058
1059     this.selection = [];
1060   }
6b47de 1061
de3c33 1062   if (num_select && !this.selection.length && !no_event)
cc97ea 1063     this.triggerEvent('select');
6b47de 1064 },
T 1065
1066
1067 /**
1068  * Getter for the selection array
1069  */
7eecf8 1070 get_selection: function(deep)
6b47de 1071 {
7eecf8 1072   var res = $.merge([], this.selection);
AM 1073
1074   // return children of selected threads even if only root is selected
1075   if (deep !== false && res.length) {
1076     for (var uid, uids, i=0, len=res.length; i<len; i++) {
1077       uid = res[i];
f67037 1078       if (this.rows[uid] && this.rows[uid].has_children && !this.rows[uid].expanded) {
7eecf8 1079         uids = this.row_children(uid);
AM 1080         for (var j=0, uids_len=uids.length; j<uids_len; j++) {
1081           uid = uids[j];
1082           if (!this.in_selection(uid))
1083             res.push(uid);
1084         }
1085       }
1086     }
1087   }
1088
1089   return res;
6b47de 1090 },
T 1091
1092
1093 /**
1094  * Return the ID if only one row is selected
1095  */
1096 get_single_selection: function()
1097 {
1098   if (this.selection.length == 1)
1099     return this.selection[0];
1100   else
1101     return null;
1102 },
1103
1104
1105 /**
1106  * Highlight/unhighlight a row
1107  */
ad827b 1108 highlight_row: function(id, multiple, norecur)
6b47de 1109 {
2b55d4 1110   if (!this.rows[id])
AM 1111     return;
1112
1113   if (!multiple) {
8fa922 1114     if (this.selection.length > 1 || !this.in_selection(id)) {
de3c33 1115       this.clear_selection(null, true);
df015d 1116       this.selection[0] = id;
cc97ea 1117       $(this.rows[id].obj).addClass('selected');
df015d 1118     }
6b47de 1119   }
2b55d4 1120   else {
8fa922 1121     if (!this.in_selection(id)) { // select row
2b55d4 1122       this.selection.push(id);
cc97ea 1123       $(this.rows[id].obj).addClass('selected');
ad827b 1124       if (!norecur && !this.rows[id].expanded)
2b55d4 1125         this.highlight_children(id, true);
6b47de 1126     }
8fa922 1127     else { // unselect row
1633bc 1128       var p = $.inArray(id, this.selection),
A 1129         a_pre = this.selection.slice(0, p),
1130         a_post = this.selection.slice(p+1, this.selection.length);
1131
6b47de 1132       this.selection = a_pre.concat(a_post);
cc97ea 1133       $(this.rows[id].obj).removeClass('selected').removeClass('unfocused');
ad827b 1134       if (!norecur && !this.rows[id].expanded)
2b55d4 1135         this.highlight_children(id, false);
6b47de 1136     }
2b55d4 1137   }
AM 1138 },
1139
1140
1141 /**
1142  * Highlight/unhighlight all childs of the given row
1143  */
1144 highlight_children: function(id, status)
1145 {
1146   var i, selected,
1147     children = this.row_children(id), len = children.length;
1148
1149   for (i=0; i<len; i++) {
1150     selected = this.in_selection(children[i]);
1151     if ((status && !selected) || (!status && selected))
ad827b 1152       this.highlight_row(children[i], true, true);
6b47de 1153   }
T 1154 },
1155
1156
1157 /**
1158  * Handler for keyboard events
1159  */
1160 key_press: function(e)
1161 {
ebee2a 1162   var target = e.target || {};
808055 1163
ebee2a 1164   if (this.focused != true || target.nodeName == 'INPUT' || target.nodeName == 'TEXTAREA' || target.nodeName == 'SELECT')
6b47de 1165     return true;
T 1166
1633bc 1167   var keyCode = rcube_event.get_keycode(e),
A 1168     mod_key = rcube_event.get_modifier(e);
91d1a1 1169
8fa922 1170   switch (keyCode) {
6b47de 1171     case 40:
699a25 1172     case 38:
26f5b0 1173     case 63233: // "down", in safari keypress
T 1174     case 63232: // "up", in safari keypress
1175       // Stop propagation so that the browser doesn't scroll
1176       rcube_event.cancel(e);
6b47de 1177       return this.use_arrow_key(keyCode, mod_key);
5c7bbf 1178
f52c93 1179     case 32:
9806c7 1180       rcube_event.cancel(e);
C 1181       return this.select_row(this.last_selected, mod_key, true);
5c7bbf 1182
9806c7 1183     case 37: // Left arrow key
C 1184     case 39: // Right arrow key
f52c93 1185       // Stop propagation
T 1186       rcube_event.cancel(e);
808055 1187       var ret = this.use_arrow_key(keyCode, mod_key);
f52c93 1188       this.key_pressed = keyCode;
699a25 1189       this.modkey = mod_key;
f52c93 1190       this.triggerEvent('keypress');
699a25 1191       this.modkey = 0;
f52c93 1192       return ret;
5c7bbf 1193
bc2acc 1194     case 36: // Home
A 1195       this.select_first(mod_key);
1196       return rcube_event.cancel(e);
5c7bbf 1197
bc2acc 1198     case 35: // End
A 1199       this.select_last(mod_key);
1200       return rcube_event.cancel(e);
5c7bbf 1201
17a8fb 1202     case 27:
AM 1203       if (this.drag_active)
1204         return this.drag_mouse_up(e);
5c7bbf 1205
17a8fb 1206       if (this.col_drag_active) {
AM 1207         this.selected_column = null;
1208         return this.column_drag_mouse_up(e);
1209       }
5c7bbf 1210
17a8fb 1211       return rcube_event.cancel(e);
5c7bbf 1212
6b47de 1213     default:
T 1214       this.key_pressed = keyCode;
699a25 1215       this.modkey = mod_key;
cc97ea 1216       this.triggerEvent('keypress');
699a25 1217       this.modkey = 0;
8fa922 1218
f89f03 1219       if (this.key_pressed == this.BACKSPACE_KEY)
6e6e89 1220         return rcube_event.cancel(e);
9e7a1b 1221   }
8fa922 1222
9e7a1b 1223   return true;
T 1224 },
1225
6b47de 1226
T 1227 /**
1228  * Special handling method for arrow keys
1229  */
1230 use_arrow_key: function(keyCode, mod_key)
1231 {
808055 1232   var new_row,
AM 1233     selected_row = this.rows[this.last_selected];
1234
e9b57b 1235   // Safari uses the nonstandard keycodes 63232/63233 for up/down, if we're
A 1236   // using the keypress event (but not the keydown or keyup event).
1237   if (keyCode == 40 || keyCode == 63233) // down arrow key pressed
6b47de 1238     new_row = this.get_next_row();
e9b57b 1239   else if (keyCode == 38 || keyCode == 63232) // up arrow key pressed
6b47de 1240     new_row = this.get_prev_row();
808055 1241   else {
AM 1242     if (!selected_row || !selected_row.has_children)
1243       return;
1244
1245     // expand
1246     if (keyCode == 39) {
1247       if (selected_row.expanded)
1248         return;
1249
1250       if (mod_key == CONTROL_KEY || this.multiexpand)
1251         this.expand_all(selected_row);
1252       else
1253         this.expand(selected_row);
1254     }
1255     // collapse
1256     else {
1257       if (!selected_row.expanded)
1258         return;
1259
1260       if (mod_key == CONTROL_KEY || this.multiexpand)
1261         this.collapse_all(selected_row);
1262       else
1263         this.collapse(selected_row);
1264     }
1265
1266     this.update_expando(selected_row.uid, selected_row.expanded);
1267
1268     return false;
1269   }
6b47de 1270
8fa922 1271   if (new_row) {
699a25 1272     this.select_row(new_row.uid, mod_key, false);
6b47de 1273     this.scrollto(new_row.uid);
T 1274   }
1275
1276   return false;
1277 },
1278
1279
1280 /**
1281  * Try to scroll the list to make the specified row visible
1282  */
1283 scrollto: function(id)
1284 {
e6f211 1285   var row = this.rows[id] ? this.rows[id].obj : null;
AM 1286
8fa922 1287   if (row && this.frame) {
741f38 1288     var scroll_to = Number(row.offsetTop),
C 1289       head_offset = 0;
6b47de 1290
bc2acc 1291     // expand thread if target row is hidden (collapsed)
A 1292     if (!scroll_to && this.rows[id].parent_uid) {
1293       var parent = this.find_root(this.rows[id].uid);
1294       this.expand_all(this.rows[parent]);
1295       scroll_to = Number(row.offsetTop);
1296     }
1297
8f8e26 1298     if (this.fixed_header)
741f38 1299       head_offset = Number(this.thead.offsetHeight);
8f8e26 1300
741f38 1301     // if row is above the frame (or behind header)
C 1302     if (scroll_to < Number(this.frame.scrollTop) + head_offset) {
1303       // scroll window so that row isn't behind header
1304       this.frame.scrollTop = scroll_to - head_offset;
8f8e26 1305     }
AM 1306     else if (scroll_to + Number(row.offsetHeight) > Number(this.frame.scrollTop) + Number(this.frame.offsetHeight))
6b47de 1307       this.frame.scrollTop = (scroll_to + Number(row.offsetHeight)) - Number(this.frame.offsetHeight);
T 1308   }
1309 },
1310
1311
1312 /**
1313  * Handler for mouse move events
1314  */
1315 drag_mouse_move: function(e)
1316 {
8ef2f3 1317   // convert touch event
T 1318   if (e.type == 'touchmove') {
dc8400 1319     if (e.touches.length == 1 && e.changedTouches.length == 1)
8ef2f3 1320       e = rcube_event.touchevent(e.changedTouches[0]);
T 1321     else
1322       return rcube_event.cancel(e);
1323   }
2b55d4 1324
8fa922 1325   if (this.drag_start) {
6b47de 1326     // check mouse movement, of less than 3 pixels, don't start dragging
f89637 1327     var m = rcube_event.get_mouse_pos(e),
AM 1328       limit = 10, selection = [], self = this;
cf6bc5 1329
6b47de 1330     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))
T 1331       return false;
8fa922 1332
f89637 1333     // remember dragging start position
AM 1334     this.drag_start_pos = {left: m.x, top: m.y};
1335
1336     // initialize drag layer
6b47de 1337     if (!this.draglayer)
b62c48 1338       this.draglayer = $('<div>').attr('id', 'rcmdraglayer')
f89637 1339         .css({position: 'absolute', display: 'none', 'z-index': 2000})
b62c48 1340         .appendTo(document.body);
f89637 1341     else
AM 1342       this.draglayer.html('');
8fa922 1343
f89637 1344     // get selected rows (in display order), don't use this.selection here
AM 1345     $(this.row_tagname() + '.selected', this.tbody).each(function() {
1346       if (!String(this.id).match(self.id_regexp))
1347         return;
a80304 1348
f89637 1349       var uid = RegExp.$1, row = self.rows[uid];
74cd6c 1350
e445e0 1351       if (!row || $.inArray(uid, selection) > -1)
f89637 1352         return;
6b47de 1353
f89637 1354       selection.push(uid);
f52c93 1355
f89637 1356       // also handle children of (collapsed) trees for dragging (they might be not selected)
AM 1357       if (row.has_children && !row.expanded)
1358         $.each(self.row_children(uid), function() {
1359           if ($.inArray(this, selection) > -1)
1360             return;
1361           selection.push(this);
517dae 1362         });
f89637 1363
AM 1364       // break the loop asap
1365       if (selection.length > limit + 1)
1366         return false;
1367     });
1368
1369     // append subject (of every row up to the limit) to the drag layer
1370     $.each(selection, function(i, uid) {
1371       if (i > limit) {
1372         self.draglayer.append('...');
1373         return false;
6b47de 1374       }
f89637 1375
AM 1376       $('> ' + self.col_tagname(), self.rows[uid].obj).each(function(n, cell) {
1377         if (self.subject_col < 0 || (self.subject_col >= 0 && self.subject_col == n)) {
1378           var subject = $(cell).text();
1379
1380           if (subject) {
1381             // remove leading spaces
1382             subject = $.trim(subject);
1383             // truncate line to 50 characters
1384             subject = (subject.length > 50 ? subject.substring(0, 50) + '...' : subject);
1385
1386             self.draglayer.append($('<div>').text(subject));
1387             return false;
1388           }
1389         }
1390       });
1391     });
6b47de 1392
cc97ea 1393     this.draglayer.show();
6b47de 1394     this.drag_active = true;
cc97ea 1395     this.triggerEvent('dragstart');
6b47de 1396   }
T 1397
8fa922 1398   if (this.drag_active && this.draglayer) {
6b47de 1399     var pos = rcube_event.get_mouse_pos(e);
cc97ea 1400     this.draglayer.css({ left:(pos.x+20)+'px', top:(pos.y-5 + (bw.ie ? document.documentElement.scrollTop : 0))+'px' });
9489ad 1401     this.triggerEvent('dragmove', e?e:window.event);
6b47de 1402   }
T 1403
1404   this.drag_start = false;
1405
1406   return false;
1407 },
1408
1409
1410 /**
1411  * Handler for mouse up events
1412  */
1413 drag_mouse_up: function(e)
1414 {
1415   document.onmousemove = null;
1257dd 1416
8ef2f3 1417   if (e.type == 'touchend') {
T 1418     if (e.changedTouches.length != 1)
1419       return rcube_event.cancel(e);
1420   }
6b47de 1421
cc97ea 1422   if (this.draglayer && this.draglayer.is(':visible')) {
T 1423     if (this.drag_start_pos)
1424       this.draglayer.animate(this.drag_start_pos, 300, 'swing').hide(20);
1425     else
1426       this.draglayer.hide();
1427   }
6b47de 1428
da8f11 1429   if (this.drag_active)
A 1430     this.focus();
6b47de 1431   this.drag_active = false;
6c11ee 1432
A 1433   rcube_event.remove_listener({event:'mousemove', object:this, method:'drag_mouse_move'});
1434   rcube_event.remove_listener({event:'mouseup', object:this, method:'drag_mouse_up'});
1257dd 1435
4910b0 1436   if (bw.touch) {
8ef2f3 1437     rcube_event.remove_listener({event:'touchmove', object:this, method:'drag_mouse_move'});
T 1438     rcube_event.remove_listener({event:'touchend', object:this, method:'drag_mouse_up'});
1439   }
6c11ee 1440
A 1441   // remove temp divs
b62c48 1442   this.del_dragfix();
6c11ee 1443
76a98d 1444   this.triggerEvent('dragend', e);
da8f11 1445
6b47de 1446   return rcube_event.cancel(e);
c4b819 1447 },
A 1448
1449
1450 /**
b62c48 1451  * Handler for mouse move events for dragging list column
A 1452  */
1453 column_drag_mouse_move: function(e)
1454 {
1455   if (this.drag_start) {
1456     // check mouse movement, of less than 3 pixels, don't start dragging
1457     var i, m = rcube_event.get_mouse_pos(e);
1458
1459     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))
1460       return false;
1461
1462     if (!this.col_draglayer) {
1463       var lpos = $(this.list).offset(),
517dae 1464         cells = this.thead.rows[0].cells;
b62c48 1465
0c8049 1466       // fix layer position when list is scrolled
AM 1467       lpos.top += this.list.scrollTop + this.list.parentNode.scrollTop;
1468
b62c48 1469       // create dragging layer
A 1470       this.col_draglayer = $('<div>').attr('id', 'rcmcoldraglayer')
1471         .css(lpos).css({ position:'absolute', 'z-index':2001,
1472            'background-color':'white', opacity:0.75,
1473            height: (this.frame.offsetHeight-2)+'px', width: (this.frame.offsetWidth-2)+'px' })
1474         .appendTo(document.body)
1475         // ... and column position indicator
1476        .append($('<div>').attr('id', 'rcmcolumnindicator')
1477           .css({ position:'absolute', 'border-right':'2px dotted #555', 
1478           'z-index':2002, height: (this.frame.offsetHeight-2)+'px' }));
1479
1480       this.cols = [];
1481       this.list_pos = this.list_min_pos = lpos.left;
1482       // save columns positions
1483       for (i=0; i<cells.length; i++) {
1484         this.cols[i] = cells[i].offsetWidth;
1485         if (this.column_fixed !== null && i <= this.column_fixed) {
1486           this.list_min_pos += this.cols[i];
1487         }
1488       }
1489     }
1490
1491     this.col_draglayer.show();
1492     this.col_drag_active = true;
1493     this.triggerEvent('column_dragstart');
1494   }
1495
1496   // set column indicator position
1497   if (this.col_drag_active && this.col_draglayer) {
1498     var i, cpos = 0, pos = rcube_event.get_mouse_pos(e);
1499
1500     for (i=0; i<this.cols.length; i++) {
1501       if (pos.x >= this.cols[i]/2 + this.list_pos + cpos)
1502         cpos += this.cols[i];
1503       else
1504         break;
1505     }
1506
1507     // handle fixed columns on left
1508     if (i == 0 && this.list_min_pos > pos.x)
1509       cpos = this.list_min_pos - this.list_pos;
1510     // empty list needs some assignment
1511     else if (!this.list.rowcount && i == this.cols.length)
1512       cpos -= 2;
1513     $('#rcmcolumnindicator').css({ width: cpos+'px'});
1514     this.triggerEvent('column_dragmove', e?e:window.event);
1515   }
1516
1517   this.drag_start = false;
1518
1519   return false;
1520 },
1521
1522
1523 /**
1524  * Handler for mouse up events for dragging list columns
1525  */
1526 column_drag_mouse_up: function(e)
1527 {
1528   document.onmousemove = null;
1529
1530   if (this.col_draglayer) {
1531     (this.col_draglayer).remove();
1532     this.col_draglayer = null;
1533   }
1534
1535   if (this.col_drag_active)
1536     this.focus();
1537   this.col_drag_active = false;
1538
1539   rcube_event.remove_listener({event:'mousemove', object:this, method:'column_drag_mouse_move'});
1540   rcube_event.remove_listener({event:'mouseup', object:this, method:'column_drag_mouse_up'});
1541   // remove temp divs
1542   this.del_dragfix();
1543
1544   if (this.selected_column !== null && this.cols && this.cols.length) {
1545     var i, cpos = 0, pos = rcube_event.get_mouse_pos(e);
1546
1547     // find destination position
1548     for (i=0; i<this.cols.length; i++) {
1549       if (pos.x >= this.cols[i]/2 + this.list_pos + cpos)
1550         cpos += this.cols[i];
1551       else
1552         break;
1553     }
1554
1555     if (i != this.selected_column && i != this.selected_column+1) {
1556       this.column_replace(this.selected_column, i);
1557     }
1558   }
1559
76a98d 1560   this.triggerEvent('column_dragend', e);
b62c48 1561
A 1562   return rcube_event.cancel(e);
1563 },
1564
1565
1566 /**
2b55d4 1567  * Returns IDs of all rows in a thread (except root) for specified root
AM 1568  */
1569 row_children: function(uid)
1570 {
1571   if (!this.rows[uid] || !this.rows[uid].has_children)
1572     return [];
1573
1574   var res = [], depth = this.rows[uid].depth,
1575     row = this.rows[uid].obj.nextSibling;
1576
1577   while (row) {
1578     if (row.nodeType == 1) {
7eecf8 1579       if (r = this.rows[row.uid]) {
2b55d4 1580         if (!r.depth || r.depth <= depth)
AM 1581           break;
1582         res.push(r.uid);
1583       }
1584     }
1585     row = row.nextSibling;
1586   }
1587
1588   return res;
1589 },
1590
1591
1592 /**
b62c48 1593  * Creates a layer for drag&drop over iframes
A 1594  */
1595 add_dragfix: function()
1596 {
1597   $('iframe').each(function() {
1598     $('<div class="iframe-dragdrop-fix"></div>')
1599       .css({background: '#fff',
1600         width: this.offsetWidth+'px', height: this.offsetHeight+'px',
1601         position: 'absolute', opacity: '0.001', zIndex: 1000
1602       })
1603       .css($(this).offset())
1604       .appendTo(document.body);
677e1f 1605   });
b62c48 1606 },
A 1607
1608
1609 /**
1610  * Removes the layer for drag&drop over iframes
1611  */
1612 del_dragfix: function()
1613 {
acc900 1614   $('div.iframe-dragdrop-fix').remove();
b62c48 1615 },
A 1616
1617
1618 /**
1619  * Replaces two columns
1620  */
1621 column_replace: function(from, to)
1622 {
517dae 1623   // only supported for <table> lists
TB 1624   if (!this.thead || !this.thead.rows)
1625     return;
1626
1627   var len, cells = this.thead.rows[0].cells,
b62c48 1628     elem = cells[from],
A 1629     before = cells[to],
1630     td = document.createElement('td');
1631
1632   // replace header cells
1633   if (before)
1634     cells[0].parentNode.insertBefore(td, before);
1635   else
1636     cells[0].parentNode.appendChild(td);
1637   cells[0].parentNode.replaceChild(elem, td);
1638
1639   // replace list cells
517dae 1640   for (r=0, len=this.tbody.rows.length; r<len; r++) {
TB 1641     row = this.tbody.rows[r];
b62c48 1642
A 1643     elem = row.cells[from];
1644     before = row.cells[to];
1645     td = document.createElement('td');
1646
1647     if (before)
1648       row.insertBefore(td, before);
1649     else
1650       row.appendChild(td);
1651     row.replaceChild(elem, td);
1652   }
1653
1654   // update subject column position
1655   if (this.subject_col == from)
1656     this.subject_col = to > from ? to - 1 : to;
8e32dc 1657   else if (this.subject_col < from && to <= this.subject_col)
T 1658     this.subject_col++;
1659   else if (this.subject_col > from && to >= this.subject_col)
1660     this.subject_col--;
b62c48 1661
73ad4f 1662   if (this.fixed_header)
TB 1663     this.init_header();
1664
b62c48 1665   this.triggerEvent('column_replace');
6b47de 1666 }
T 1667
1668 };
1669
cc97ea 1670 rcube_list_widget.prototype.addEventListener = rcube_event_engine.prototype.addEventListener;
T 1671 rcube_list_widget.prototype.removeEventListener = rcube_event_engine.prototype.removeEventListener;
1672 rcube_list_widget.prototype.triggerEvent = rcube_event_engine.prototype.triggerEvent;