You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.js 44KB

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