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