/** * 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) 2005-2014, 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(a,b){this.ENTER_KEY=13;this.DELETE_KEY=46;this.BACKSPACE_KEY=8;this.tagname=(this.list=a?a:null)?this.list.nodeName.toLowerCase():"table";this.id_regexp=/^rcmrow([a-z0-9\-_=\+\/]+)/i;this.rows={};this.selection=[];this.modkey=this.subject_col=this.colcount=this.rowcount=0;this.aria_listbox=this.toggleselect=this.keyboard=this.column_movable=this.draggable=this.multi_selecting=this.multiexpand=this.multiselect=!1;this.parent_focus=!0;this.col_drag_active=this.drag_active= !1;this.shift_start=this.last_selected=this.column_fixed=null;this.focused=!1;this.drag_mouse_start=null;this.dblclick_time=500;this.row_init=function(){};if(b&&"object"===typeof b)for(var c in b)this[c]=b[c];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 a=this;if(this.tbody){this.rows={};this.rowcount=0;var b,c,d=this.tbody.childNodes;b=0;for(c=d.length;b").attr("class",this.list.className+" fixedcopy").attr("role","presentation").css({position:"fixed"}).append(a).append("");$(this.list).before(this.fixed_header);var b=this;$(window).resize(function(){b.resize()});$(window).scroll(function(){var a=$(window);b.fixed_header.css({marginLeft:-a.scrollLeft()+"px",marginTop:-a.scrollTop()+"px"})})}$(this.list.tHead).find("a.sortcol").attr("tabindex","-1");a.find("a.sortcol").attr("tabindex","0");this.thead=a.get(0);this.resize()}, resize:function(){if(this.fixed_header){var a=[];$(this.tbody).parent().find("thead th,thead td").each(function(b){a[b]=$(this).width()});$(this.thead).parent().width($(this.tbody).parent().width());$(this.thead).find("th,td").each(function(b){$(this).width(a[b])});$(window).scroll()}},clear:function(a){if("table"==this.tagname){var b=document.createElement("tbody");this.list.insertBefore(b,this.tbody);this.list.removeChild(this.list.tBodies[1]);this.tbody=b}else $(this.row_tagname()+":not(.thead)", this.tbody).remove();this.rows={};this.rowcount=0;this.last_selected=null;a&&this.clear_selection();this.frame&&(this.frame.scrollTop=0);this.resize()},remove_row:function(a,b){var c=this,d=this.rows[a]?this.rows[a].obj:null;d&&(d.style.display="none",b&&this.select_next(),delete this.rows[a],this.rowcount--,clearTimeout(this.resize_timeout),this.resize_timeout=setTimeout(function(){c.resize()},50))},insert_row:function(a,b){var c=this,d=this.tbody;if(void 0===a.nodeName){var e=document.createElement(this.row_tagname()); a.id&&(e.id=a.id);a.uid&&(e.uid=a.uid);a.className&&(e.className=a.className);a.style&&$.extend(e.style,a.style);for(var f,g,h,k=0;a.cols&&k=c.depth-1)f=c.depth,$(e).css("display",""),b.expanded=!0,this.triggerEvent("expandcollapse",{uid:b.uid,expanded:b.expanded,obj:e})}else if(a&&(!c||c.depth<=d))break}e=e.nextSibling}this.resize();this.triggerEvent("listupdate"); return!1},collapse_all:function(a){var b,c,d;if(a){if(a.expanded=!1,b=a.depth,c=a.obj.nextSibling,this.update_expando(a.id),this.triggerEvent("expandcollapse",{uid:a.uid,expanded:a.expanded,obj:a.obj}),b&&this.multiexpand)return!1}else c=this.tbody.firstChild,b=0;for(;c;){if(1==c.nodeType&&(d=this.rows[c.uid])){if(a&&(!d.depth||d.depth<=b))break;(a||d.depth)&&$(c).css("display","none");d.has_children&&d.expanded&&(d.expanded=!1,this.update_expando(d.id,!1),this.triggerEvent("expandcollapse",{uid:d.uid, expanded:d.expanded,obj:c}))}c=c.nextSibling}this.resize();this.triggerEvent("listupdate");return!1},expand_all:function(a){var b,c,d;a?(a.expanded=!0,b=a.depth,c=a.obj.nextSibling,this.update_expando(a.id,!0),this.triggerEvent("expandcollapse",{uid:a.uid,expanded:a.expanded,obj:a.obj})):(c=this.tbody.firstChild,b=0);for(;c;){if(1==c.nodeType&&(d=this.rows[c.uid])){if(a&&d.depth<=b)break;$(c).css("display","");d.has_children&&!d.expanded&&(d.expanded=!0,this.update_expando(d.id,!0),this.triggerEvent("expandcollapse", {uid:d.uid,expanded:d.expanded,obj:c}))}c=c.nextSibling}this.resize();this.triggerEvent("listupdate");return!1},update_expando:function(a,b){var c=document.getElementById("rcmexpando"+a);c&&(c.className=b?"expanded":"collapsed")},get_row_uid:function(a){if(a){if(!a.uid){var b=$(a).data("uid");b?a.uid=b:String(a.id).match(this.id_regexp)&&(a.uid=RegExp.$1)}return a.uid}},get_next_row:function(){if(!this.rowcount)return!1;for(var a=this.rows[this.last_selected],a=a?a.obj.nextSibling:null;a&&(1!=a.nodeType|| "none"==a.style.display);)a=a.nextSibling;return a},get_prev_row:function(){if(!this.rowcount)return!1;for(var a=this.rows[this.last_selected],a=a?a.obj.previousSibling:null;a&&(1!=a.nodeType||"none"==a.style.display);)a=a.previousSibling;return a},get_first_row:function(){if(this.rowcount){var a,b,c=this.tbody.childNodes;for(a=0;af?e:f;for(c in this.rows)this._rowIndex(this.rows[c].obj)>=d&&this._rowIndex(this.rows[c].obj)<=e?this.in_selection(c)||this.highlight_row(c,!0):this.in_selection(c)&&!b&&this.highlight_row(c,!0)},_rowIndex:function(a){return void 0!==a.rowIndex?a.rowIndex:$(a).prevAll().length},in_selection:function(a,b){for(var c in this.selection)if(this.selection[c]==a)return b?parseInt(c):!0;return!1},select_all:function(a){if(!this.rowcount)return!1; var b,c=this.selection.join(",");this.selection=[];for(b in this.rows)a&&!0!=this.rows[b][a]?$(this.rows[b].obj).removeClass("selected").removeAttr("aria-selected"):(this.last_selected=b,this.highlight_row(b,!0,!0));this.selection.join(",")!=c&&this.triggerEvent("select");this.focus();return!0},invert_selection:function(){if(!this.rowcount)return!1;var a,b=this.selection.join(",");for(a in this.rows)this.highlight_row(a,!0);this.selection.join(",")!=b&&this.triggerEvent("select");this.focus();return!0}, clear_selection:function(a,b){var c,d=this.selection.length;if(a)for(c in this.selection){if(this.selection[c]==a){this.selection.splice(c,1);break}}else{for(c in this.selection)this.rows[this.selection[c]]&&$(this.rows[this.selection[c]].obj).removeClass("selected").removeAttr("aria-selected");this.selection=[]}!d||this.selection.length||b||(this.triggerEvent("select"),this.last_selected=null)},get_selection:function(a){var b=$.merge([],this.selection);if(!1!==a&&b.length)for(var c,d=0,e=b.length;d< e;d++)if(a=b[d],this.rows[a]&&this.rows[a].has_children&&!this.rows[a].expanded){c=this.row_children(a);for(var f=0,g=c.length;fNumber(this.frame.scrollTop)+Number(this.frame.offsetHeight)&&(this.frame.scrollTop=c+Number(b.offsetHeight)-Number(this.frame.offsetHeight))}},drag_mouse_move:function(a){if("touchmove"==a.type)if(1==a.touches.length&&1==a.changedTouches.length)a=rcube_event.touchevent(a.changedTouches[0]);else return rcube_event.cancel(a);if(this.drag_start){var b=rcube_event.get_mouse_pos(a),c=[],d=this;if(!this.drag_mouse_start||3>Math.abs(b.x-this.drag_mouse_start.x)&& 3>Math.abs(b.y-this.drag_mouse_start.y))return!1;this.drag_start_pos={left:b.x,top:b.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 a=d.get_row_uid(this),b=d.rows[a];if(b&&!(-1<$.inArray(a,c))&&(c.push(a),b.has_children&&!b.expanded&&$.each(d.row_children(a),function(){-1<$.inArray(this,c)||c.push(this)}),11< c.length))return!1});$.each(c,function(a,b){if(10 "+d.col_tagname(),d.rows[b].obj).each(function(a,b){if(0>d.subject_col||0<=d.subject_col&&d.subject_col==a){b=$(b).clone();$(b).find(".skip-on-drag").remove();var c=b.text();if(c)return c=$.trim(c),c=50").text(c)),!1}})});this.draglayer.show();this.drag_active=!0;this.triggerEvent("dragstart")}this.drag_active&&this.draglayer&&(b=rcube_event.get_mouse_pos(a), this.draglayer.css({left:b.x+20+"px",top:b.y-5+(bw.ie?document.documentElement.scrollTop:0)+"px"}),this.triggerEvent("dragmove",a?a:window.event));return this.drag_start=!1},drag_mouse_up:function(a){document.onmousemove=null;if("touchend"==a.type&&1!=a.changedTouches.length)return rcube_event.cancel(a);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",a);return rcube_event.cancel(a)},column_drag_mouse_move:function(a){if(this.drag_start){var b;b=rcube_event.get_mouse_pos(a); if(!this.drag_mouse_start||3>Math.abs(b.x-this.drag_mouse_start.x)&&3>Math.abs(b.y-this.drag_mouse_start.y))return!1;if(!this.col_draglayer){b=$(this.list).offset();var c=this.thead.rows[0].cells;b.top+=this.list.scrollTop+this.list.parentNode.scrollTop;this.col_draglayer=$("
").attr("id","rcmcoldraglayer").css(b).css({position:"absolute","z-index":2001,"background-color":"white",opacity:0.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=b.left;for(b=0;b=this.cols[b]/2+this.list_pos+c)c+=this.cols[b];else break;0==b&&this.list_min_pos>d.x?c=this.list_min_pos-this.list_pos:this.list.rowcount||b!=this.cols.length||(c-=2);$("#rcmcolumnindicator").css({width:c+"px"});this.triggerEvent("column_dragmove",a?a:window.event)}return this.drag_start=!1},column_drag_mouse_up:function(a){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();if(this.col_drag_active&&(this.col_drag_active=!1,this.focus(),this.triggerEvent("column_dragend",a),null!==this.selected_column&&this.cols&&this.cols.length)){var b,c=0,d=rcube_event.get_mouse_pos(a);for(b=0;b=this.cols[b]/2+this.list_pos+c)c+=this.cols[b];else break;b!=this.selected_column&&b!=this.selected_column+ 1&&this.column_replace(this.selected_column,b)}return rcube_event.cancel(a)},row_children:function(a){if(!this.rows[a]||!this.rows[a].has_children)return[];var b=[],c=this.rows[a].depth;for(a=this.rows[a].obj.nextSibling;a;){if(1==a.nodeType&&(r=this.rows[a.uid])){if(!r.depth||r.depth<=c)break;b.push(r.uid)}a=a.nextSibling}return b},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(a,b){if(this.thead&&this.thead.rows){var c;c=this.thead.rows[0].cells;var d=c[a],e=c[b],f=document.createElement("td");e?c[0].parentNode.insertBefore(f,e):c[0].parentNode.appendChild(f);c[0].parentNode.replaceChild(d,f);r=0;for(c=this.tbody.rows.length;ra?b-1:b:this.subject_cola&&b>=this.subject_col&&this.subject_col--;this.fixed_header&&this.init_header();this.triggerEvent("column_replace")}}};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=[];