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