/** * Roundcube List Widget * * This file is part of the Roundcube Webmail client * * @licstart The following is the entire license notice for the * JavaScript code in this file. * * Copyright (c) The Roundcube Dev Team * * The JavaScript code in this page is free software: you can * redistribute it and/or modify it under the terms of the GNU * General Public License (GNU GPL) as published by the Free Software * Foundation, either version 3 of the License, or (at your option) * any later version. The code is distributed WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details. * * As additional permission under GNU GPL version 3 section 7, you * may distribute non-source (e.g., minimized or compacted) forms of * that code without the copy of the GNU GPL normally required by * section 4, provided you include this license notice and a URL * through which recipients can access the Corresponding Source. * * @licend The above is the entire license notice * for the JavaScript code in this file. * * @author Thomas Bruederli * @author Charles McNulty * * @requires jquery.js, common.js */ function rcube_list_widget(t,e){if(this.ENTER_KEY=13,this.DELETE_KEY=46,this.BACKSPACE_KEY=8,this.list=t||null,this.tagname=this.list?this.list.nodeName.toLowerCase():"table",this.id_regexp=/^rcmrow([a-z0-9\-_=\+\/]+)/i,this.rows={},this.selection=[],this.rowcount=0,this.colcount=0,this.subject_col=0,this.modkey=0,this.multiselect=!1,this.multiexpand=!1,this.multi_selecting=!1,this.draggable=!1,this.column_movable=!1,this.keyboard=!1,this.toggleselect=!1,this.aria_listbox=!1,this.parent_focus=!0,this.checkbox_selection=!1,this.drag_active=!1,this.col_drag_active=!1,this.column_fixed=null,this.last_selected=null,this.shift_start=null,this.focused=!1,this.drag_mouse_start=null,this.dblclick_time=500,this.row_init=function(){},this.touch_start_time=0,this.touch_event_time=500,e&&"object"==typeof e)for(var i in e)this[i]=e[i];rcube_list_widget._instances.push(this)}rcube_list_widget.prototype={init:function(){"table"==this.tagname&&this.list&&this.list.tBodies[0]?(this.thead=this.list.tHead,this.tbody=this.list.tBodies[0]):"table"!=this.tagname&&this.list&&(this.tbody=this.list),"listbox"==$(this.list).attr("role")&&(this.aria_listbox=!0,this.multiselect&&$(this.list).attr("aria-multiselectable","true"));var e=this;if(this.tbody){this.rows={},this.rowcount=0;for(var t=this.tbody.childNodes,i=0,s=t.length;i").attr("id",this.list.id+"-fixedcopy").attr("class",this.list.className+" fixedcopy").attr("role","presentation").css({position:"fixed"}).append(t).append(""),$(this.list).before(this.fixed_header),e=this,$(window).on("resize",function(){e.resize()}),$(this.container).on("scroll",function(){var t=$(this);e.fixed_header.css({marginLeft:-t.scrollLeft()+"px",marginTop:-t.scrollTop()+"px"})})),$(this.list.tHead).find("a.sortcol").attr("tabindex","-1"),t.find("a.sortcol").attr("tabindex","0"),this.thead=t.get(0),this.resize()},resize:function(){var e;this.fixed_header&&(e=[],$(this.tbody).parent().find("thead th,thead td").each(function(t){e[t]=$(this).width()}),$(this.thead).parent().width($(this.tbody).parent().width()),$(this.thead).find("th,td").each(function(t){$(this).width(e[t])}),$(window).scroll())},clear:function(t){var e;"table"==this.tagname?(e=document.createElement("tbody"),this.list.insertBefore(e,this.tbody),this.list.removeChild(this.list.tBodies[1]),this.tbody=e):$(this.row_tagname()+":not(.thead)",this.tbody).remove(),this.rows={},this.rowcount=0,this.last_selected=null,t&&this.clear_selection(),this.frame&&(this.frame.scrollTop=0),this.resize()},remove_row:function(t,e){var i=this,s=this.rows[t]?this.rows[t].obj:null;s&&(s.style.display="none",e&&this.select_next(t),delete this.rows[t],this.rowcount--,clearTimeout(this.resize_timeout),this.resize_timeout=setTimeout(function(){i.resize()},50))},insert_row:function(t,e){var i=this,s=this.tbody;if(void 0===t.nodeName){var o,r,n,h,c=document.createElement(this.row_tagname());for(t.id&&(c.id=t.id),t.uid&&(c.uid=t.uid),t.className&&(c.className=t.className),t.style&&$.extend(c.style,t.style),o=0;t.cols&&o=i.depth-1)&&(r=i.depth,$(s).css("display",""),e.expanded=!0,this.update_expando(e.id,!0),this.triggerEvent("expandcollapse",{uid:e.uid,expanded:e.expanded,obj:s}));else if(t&&(!i||i.depth<=o))break}s=s.nextSibling}return this.resize(),this.triggerEvent("listupdate"),!1},collapse_all:function(t){var e,i,s;if(t){if(t.expanded=!1,e=t.depth,i=t.obj.nextSibling,this.update_expando(t.id),this.triggerEvent("expandcollapse",{uid:t.uid,expanded:t.expanded,obj:t.obj}),e&&this.multiexpand)return!1}else i=this.tbody.firstChild,e=0;for(;i;){if(1==i.nodeType&&(s=this.rows[i.uid])){if(t&&(!s.depth||s.depth<=e))break;(t||s.depth)&&$(i).css("display","none"),s.expanded&&(s.expanded=!1,s.has_children&&(this.update_expando(s.id),this.triggerEvent("expandcollapse",{uid:s.uid,expanded:s.expanded,obj:i})))}i=i.nextSibling}return this.resize(),this.triggerEvent("listupdate"),!1},expand_all:function(t){var e,i,s;for(t?(t.expanded=!0,e=t.depth,i=t.obj.nextSibling,this.update_expando(t.id,!0),this.triggerEvent("expandcollapse",{uid:t.uid,expanded:t.expanded,obj:t.obj})):(i=this.tbody.firstChild,e=0);i;){if(1==i.nodeType&&(s=this.rows[i.uid])){if(t&&s.depth<=e)break;$(i).css("display",""),s.expanded||(s.expanded=!0,s.has_children&&(this.update_expando(s.id,!0),this.triggerEvent("expandcollapse",{uid:s.uid,expanded:s.expanded,obj:i})))}i=i.nextSibling}return this.resize(),this.triggerEvent("listupdate"),!1},update_expando:function(t,e){t=document.getElementById("rcmexpando"+t);t&&(t.className=e?"expanded":"collapsed")},get_row_uid:function(t){var e;if(t)return t.uid||((e=$(t).data("uid"))?t.uid=e:String(t.id).match(this.id_regexp)&&(t.uid=RegExp.$1)),t.uid},get_next_row:function(t){if(!this.rowcount)return!1;for(var t=this.rows[t||this.last_selected],e=t?t.obj.nextSibling:null;e&&(1!=e.nodeType||"none"==e.style.display);)e=e.nextSibling;return e},get_prev_row:function(t){if(!this.rowcount)return!1;for(var t=this.rows[t||this.last_selected],e=t?t.obj.previousSibling:null;e&&(1!=e.nodeType||"none"==e.style.display);)e=e.previousSibling;return e},get_first_row:function(){if(this.rowcount)for(var t,e=this.tbody.childNodes,i=0;i=n&&this._rowIndex(this.rows[i].obj)<=h?this.in_selection(i)||this.highlight_row(i,!0):this.in_selection(i)&&!e&&this.highlight_row(i,!0)},_rowIndex:function(t){return void 0!==t.rowIndex?t.rowIndex:$(t).prevAll().length},in_selection:function(t,e){for(var i in this.selection)if(this.selection[i]==t)return!e||parseInt(i);return!1},select_all:function(t){if(!this.rowcount)return!1;var e,i=this.selection.join(",");for(e in this.selection=[],this.rows)t&&1!=this.rows[e][t]?$(this.rows[e].obj).removeClass("selected").removeAttr("aria-selected"):(this.last_selected=e,this.highlight_row(e,!0,!0));return this.selection.join(",")!=i&&this.triggerEvent("select"),this.focus(),!0},invert_selection:function(){if(!this.rowcount)return!1;var t,e=this.selection.join(",");for(t in this.rows)this.highlight_row(t,!0);return this.selection.join(",")!=e&&this.triggerEvent("select"),this.focus(),!0},clear_selection:function(t,e){var i,s=this.selection.length;if(t){for(i in this.selection)if(this.selection[i]==t){this.selection.splice(i,1);break}}else{for(i in this.selection)this.rows[this.selection[i]]&&$(this.rows[this.selection[i]].obj).removeClass("selected").removeAttr("aria-selected");this.selection=[]}this.checkbox_selection&&$(this.row_tagname()+":not(.selected) > .selection > input:checked",this.list).prop("checked",!1),!s||this.selection.length||e||(this.triggerEvent("select"),this.last_selected=null)},get_selection:function(t){var e=$.merge([],this.selection),i={deep:t,res:e};if(!1===this.triggerEvent("getselection",i))return i.res;if(!1!==t&&e.length)for(var s,o=0,r=e.length;o input",this.rows[t].obj).prop("checked",!0),i||this.rows[t].expanded||this.highlight_children(t,!0)):(e=this.selection.slice(0,s),s=this.selection.slice(s+1,this.selection.length),this.selection=e.concat(s),$(this.rows[t].obj).removeClass("selected").removeAttr("aria-selected"),this.checkbox_selection&&$(".selection > input",this.rows[t].obj).prop("checked",!1),i||this.rows[t].expanded||this.highlight_children(t,!1)):(1 input",this.rows[t].obj).prop("checked",!0)))},highlight_children:function(t,e){for(var i,s=this.row_children(t),o=s.length,r=0;rNumber(this.frame.scrollTop)+Number(this.frame.offsetHeight)&&(this.frame.scrollTop=e+Number(s.offsetHeight)-Number(this.frame.offsetHeight)))},drag_mouse_move:function(t){if("touchmove"==t.type){if(1!=t.touches.length||1!=t.changedTouches.length)return rcube_event.cancel(t);t=rcube_event.touchevent(t.changedTouches[0])}if(this.drag_start){var e=rcube_event.get_mouse_pos(t),i=[],s=this;if(!this.drag_mouse_start||Math.abs(e.x-this.drag_mouse_start.x)<3&&Math.abs(e.y-this.drag_mouse_start.y)<3)return!1;this.drag_start_pos={left:e.x,top:e.y},this.draglayer?this.draglayer.html(""):this.draglayer=$("
").attr("id","rcmdraglayer").css({position:"absolute",display:"none","z-index":2e3}).appendTo(document.body),$(this.row_tagname()+".selected",this.tbody).each(function(){var t=s.get_row_uid(this),e=s.rows[t];if(e&&!(-1<$.inArray(t,i)))return i.push(t),e.has_children&&!e.expanded&&$.each(s.row_children(t),function(){-1<$.inArray(this,i)||i.push(this)}),!(11").text("...")),!1):(r=s.rows[e].obj,n="",$(r).children(s.col_tagname()).each(function(t,e){if((h<0||0<=h&&h==t)&&(n=o(e)))return!1}),void((n=!n.length?o($(r).children(".subject").first()):n).length&&(n=50<(n=n.trim()).length?n.substring(0,50)+"...":n,s.draglayer.append($("
").text(n)))))}),this.draglayer.show(),this.drag_active=!0,this.triggerEvent("dragstart")}return this.drag_active&&this.draglayer&&(e=rcube_event.get_mouse_pos(t),this.draglayer.css({left:e.x+20+"px",top:e.y-5+(bw.ie?document.documentElement.scrollTop:0)+"px"}),this.triggerEvent("dragmove",t||window.event)),this.drag_start=!1},drag_mouse_up:function(t){return document.onmousemove=null,"touchend"==t.type&&1!=t.changedTouches.length||(this.draglayer&&this.draglayer.is(":visible")&&(this.drag_start_pos?this.draglayer.animate(this.drag_start_pos,300,"swing").hide(20):this.draglayer.hide()),this.drag_active&&this.focus(),this.drag_active=!1,rcube_event.remove_listener({event:"mousemove",object:this,method:"drag_mouse_move"}),rcube_event.remove_listener({event:"mouseup",object:this,method:"drag_mouse_up"}),bw.touch&&(rcube_event.remove_listener({event:"touchmove",object:this,method:"drag_mouse_move"}),rcube_event.remove_listener({event:"touchend",object:this,method:"drag_mouse_up"})),this.del_dragfix(),this.triggerEvent("dragend",t)),rcube_event.cancel(t)},column_drag_mouse_move:function(t){if(this.drag_start){var e=rcube_event.get_mouse_pos(t);if(!this.drag_mouse_start||Math.abs(e.x-this.drag_mouse_start.x)<3&&Math.abs(e.y-this.drag_mouse_start.y)<3)return!1;if(!this.col_draglayer){var e=$(this.list).offset(),i=this.thead.rows[0].cells;for(e.top+=this.list.scrollTop+this.list.parentNode.scrollTop,this.col_draglayer=$("
").attr("id","rcmcoldraglayer").css(e).css({position:"absolute","z-index":2001,"background-color":"white",opacity:.75,height:this.frame.offsetHeight-2+"px",width:this.frame.offsetWidth-2+"px"}).appendTo(document.body).append($("
").attr("id","rcmcolumnindicator").css({position:"absolute","border-right":"2px dotted #555","z-index":2002,height:this.frame.offsetHeight-2+"px"})),this.cols=[],this.list_pos=this.list_min_pos=e.left,s=0;s=this.cols[s]/2+this.list_pos+o;s++)o+=this.cols[s];0==s&&this.list_min_pos>r.x?o=this.list_min_pos-this.list_pos:this.list.rowcount||s!=this.cols.length||(o-=2),$("#rcmcolumnindicator").css({width:o+"px"}),this.triggerEvent("column_dragmove",t||window.event)}return this.drag_start=!1},column_drag_mouse_up:function(t){if(document.onmousemove=null,this.col_draglayer&&(this.col_draglayer.remove(),this.col_draglayer=null),rcube_event.remove_listener({event:"mousemove",object:this,method:"column_drag_mouse_move"}),rcube_event.remove_listener({event:"mouseup",object:this,method:"column_drag_mouse_up"}),this.del_dragfix(),this.col_drag_active&&(this.col_drag_active=!1,this.focus(),this.triggerEvent("column_dragend",t),null!==this.selected_column&&this.cols&&this.cols.length)){for(var e=0,i=rcube_event.get_mouse_pos(t),s=0;s=this.cols[s]/2+this.list_pos+e;s++)e+=this.cols[s];s!=this.selected_column&&s!=this.selected_column+1&&this.column_replace(this.selected_column,s)}return rcube_event.cancel(t)},row_children:function(t){if(!this.rows[t]||!this.rows[t].has_children)return[];for(var e=[],i=this.rows[t].depth,s=this.rows[t].obj.nextSibling;s;){if(1==s.nodeType&&(r=this.rows[s.uid])){if(!r.depth||r.depth<=i)break;e.push(r.uid)}s=s.nextSibling}return e},add_dragfix:function(){$("iframe").each(function(){$('
').css({background:"#fff",width:this.offsetWidth+"px",height:this.offsetHeight+"px",position:"absolute",opacity:"0.001",zIndex:1e3}).css($(this).offset()).appendTo(document.body)})},del_dragfix:function(){$("div.iframe-dragdrop-fix").remove()},column_replace:function(t,e){if(this.thead&&this.thead.rows){var i,s=this.thead.rows[0].cells,o=s[t],n=s[e],h=document.createElement("td");for(n?s[0].parentNode.insertBefore(h,n):s[0].parentNode.appendChild(h),s[0].parentNode.replaceChild(o,h),r=0,i=this.tbody.rows.length;rt&&e>=this.subject_col&&this.subject_col--,this.fixed_header&&this.init_header(),this.triggerEvent("column_replace")}},subject_column:function(){return this.subject_col+(this.checkbox_selection?1:0)}},rcube_list_widget.prototype.addEventListener=rcube_event_engine.prototype.addEventListener,rcube_list_widget.prototype.removeEventListener=rcube_event_engine.prototype.removeEventListener,rcube_list_widget.prototype.triggerEvent=rcube_event_engine.prototype.triggerEvent,rcube_list_widget._instances=[];