text: /**
   * Roundcube Treelist 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) 2013-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 <roundcube@gmail.com>
   * @requires jquery.js, common.js
   */
  function rcube_treelist_widget(ha,g){var I,J,K;function A(a,b,d){var c;if(c=h[a]){c.collapsed="undefined"==typeof d||d;var f=l(c.id,!0);f.attr("aria-expanded",c.collapsed?"false":"true");f.children("ul").first()[c.collapsed?"hide":"show"]();f.children("div.treetoggle").removeClass("collapsed expanded").addClass(c.collapsed?"collapsed":"expanded");t.triggerEvent("toggle",c);if(b&&c.children)for(f=0;f<c.children.length;f++)A(c.children[f].id,b,d);t.triggerEvent(c.collapsed?"collapse":"expand",c);b=
  c.collapsed;g.save_state&&window.rcmail&&(d="treelist-"+S,u||(u=rcmail.local_storage_get_item(d,{})),u[a]!=b&&(u[a]=b,rcmail.local_storage_set_item(d,u)))}}function T(a,b){A(a,b,!1)}function F(a){if(!1!==t.triggerEvent("beforeselect",h[a])&&(k&&(l(k,!0).removeClass("selected").removeAttr("aria-selected"),m&&l(k).removeClass("selected").removeAttr("aria-selected"),k=null),a)){var b=l(a,!0);b.length&&(b.addClass("selected").attr("aria-selected","true"),k=a,m&&l(a).addClass("selected").attr("aria-selected",
  "true"),U(b));t.triggerEvent("select",h[a])}}function V(a,b){return l(a,b).get(0)}function W(a,b){var d,c,f=a.get(0).id,q=a.children().first().text().toUpperCase();a.parent().children("li"+b).each(function(a,b){0==a&&(d=b);if(b.id!=f)if(b.id!=f&&q>=$(b).children().first().text().toUpperCase())c=b;else return!1});c?a.insertAfter(c):d&&d.id!=f&&a.insertBefore(d);n=G(e,0)}function X(a,b){a=String(a).toLowerCase();if(!a.length)return B();if(a==C&&!b)return 0;var d=[],c=function(b){$.each(b,function(b,
  f){if(!f.virtual&&!f.deleted&&0<=String(f.text).toLowerCase().indexOf(a)&&0>d.indexOf(f.id)){b=l(f.id);if(b.data("filtered"))return;$("<li>").attr("id",b.attr("id")+"--xsR").attr("class",b.attr("class")).addClass("searchresult__").append(b.children(":not(div.treetoggle,ul)").clone(!0,!0)).appendTo(e);d.push(f.id)}f.children&&f.children.length&&c(f.children)})};m&&($(e).children("li.searchresult__").remove(),m=!1);$(e).children("li").hide().removeClass("selected");c(n);m=!0;t.triggerEvent("search",
  {query:a,last:C,count:d.length,ids:d,execute:b||!1});C=a;return d.count}function B(){L&&L.val("");$(e).children("li.searchresult__").remove();$(e).children("li").filter(function(){return!$(this).data("filtered")}).show();m=!1;t.triggerEvent("search",{query:!1,last:C});C="";k&&F(k)}function y(a,b,d){if(!a.deleted){var c=$("<li>").attr("id",g.id_prefix+(g.id_encode?g.id_encode(a.id):a.id)).attr("role","treeitem").addClass((a.classes||[]).join(" ")).data("id",a.id);d?(d.replaceWith(c),b&&c.appendTo(b)):
  c.appendTo(b);"string"==typeof a.html?c.html(a.html):"object"==typeof a.html&&c.append(a.html);a.text||(a.text=c.children().first().text());a.virtual&&c.addClass("virtual");a.id==k&&c.addClass("selected");if(a.children&&a.children.length)for(c.attr("aria-expanded",a.collapsed?"false":"true"),$('<div class="treetoggle '+(a.collapsed?"collapsed":"expanded")+'">&nbsp;</div>').appendTo(c),b=$("<ul>").appendTo(c).attr("class",a.childlistclass).attr("role","group"),a.collapsed&&b.hide(),d=0;d<a.children.length;d++)a.children[d].level=
  a.level+1,y(a.children[d],b);return c}}function G(a,b){var d=[];a.children("li").each(function(a,f){f=$(f);var c=f.children("ul"),e={id:z(f),classes:String(f.attr("class")).split(" "),virtual:f.hasClass("virtual"),level:b,html:f.children().first().get(0).outerHTML,text:f.children().first().text(),children:G(c,b+1)};c.length&&(e.childlistclass=c.attr("class"));e.children.length&&(void 0===e.collapsed&&(e.collapsed="none"==c.css("display")),a=Y(e.id,e.collapsed),void 0!==a&&(e.collapsed=a,c[a?"hide":
  "show"]()),f.children("div.treetoggle").length||$('<div class="treetoggle '+(e.collapsed?"collapsed":"expanded")+'">&nbsp;</div>').appendTo(f),f.attr("aria-expanded",e.collapsed?"false":"true"));f.hasClass("selected")&&(f.attr("aria-selected","true"),k=e.id);f.data("id",e.id);f.attr("role","treeitem").attr("aria-level",e.level+1);e.virtual&&f.children("a:first").attr("tabindex","0");d.push(e);h[e.id]=e});a.attr("role",0==b?"tree":"group");return d}function Z(a){a.id&&(h[a.id]=a);for(var b=0;a.children&&
  b<a.children.length;b++)Z(a.children[b])}function z(a){a=String(a.attr("id")).replace(new RegExp("^"+g.id_prefix||"%"),"").replace(/--xsR$/,"");return g.id_decode?g.id_decode(a):a}function l(a,b){a=g.id_encode?g.id_encode(a):a;return $("#"+g.id_prefix+a+(m&&!b?"--xsR":""),e)}function U(a){var b=e.parent(),d=b.scrollTop(),c=a.offset().top-b.offset().top;(0>c||c+a.height()>b.height())&&b.scrollTop(c+d)}function Y(a){if(g.save_state&&window.rcmail)return u||(u=rcmail.local_storage_get_item("treelist-"+
  S,{})),u[a]}function aa(a,b,d){var c=a[0>b?"prev":"next"]();0<b&&!d&&a.children("ul[role=group]:visible").length?a.children("ul").children("li:first").find("a:first").focus():0>b&&!d&&c.children("ul[role=group]:visible").length?c.children("ul").children("li:last").find("a:first").focus():c.length&&c.find("a:first").focus().length||(a=a.parent().closest("li[role=treeitem]"),a.length&&(0>b?a.find("a:first").focus():aa(a,b,!0)))}function M(a){if(a||!w){w=!0;var b,d=e.offset();ba=bw.ie?0:window.pageYOffset;
  x=e.parent().scrollTop();d.top+=x;I=d.left;J=d.top;K=d.left+e.width();ca=d.top+e.height();H=[];for(var c in h)a=l(c),(a=a.children().first().get(0))&&(b=a.offsetHeight)&&(d=$(a).offset(),d.top+=x,H[c]={x1:d.left,y1:d.top,x2:d.left+a.offsetWidth,y2:d.top+b,on:c==v});e.height()>e.parent().height()&&e.parent().mousemove(function(a){var b=0;a=rcube_event.get_mouse_pos(a);a.y-=e.parent().offset().top;25>a.y&&0<x?b=-1:a.y>e.parent().height()-25&&(b=1);w&&0!=b?p||(p=window.setTimeout(function(){da(b)},g.scroll_delay)):
  p&&(window.clearTimeout(p),p=null)}).mouseleave(function(){p&&(window.clearTimeout(p),p=null)})}}function ea(){w&&(w=!1,p=null,r&&(clearTimeout(r),v=r=null),$("li.droptarget",e).removeClass("droptarget"))}function da(a){if(w){var b=x;e.parent().get(0).scrollTop+=g.scroll_step*a;x=e.parent().scrollTop();p=null;x!=b&&(p=window.setTimeout(function(){da(a)},g.scroll_speed))}}function fa(a,b){var d=bw.ie?-document.documentElement.scrollTop:ba,c=e.parent().scrollTop(),f=null;a.top=a.y+c-d;if(a.x<I||a.x>=
  K||a.top<J||a.top>=ca)return b&&$("li.droptarget",e).removeClass("droptarget"),f;for(var q in H)d=H[q],a.x>=d.x1&&a.x<d.x2&&a.top>=d.y1&&a.top<d.y2?(f=h[q],f.children&&f.children.length&&f.collapsed&&g.autoexpand&&v!=q?(r&&clearTimeout(r),v=q,r=setTimeout(function(){T(v);M(!0);v=null;D&&$.ui.ddmanager.prepareOffsets($.ui.ddmanager.current,null)},g.autoexpand)):r&&v!=q&&(clearTimeout(r),r=v=null),g.check_droptarget(f)?(b&&(l(q).addClass("droptarget"),d.on=!0),f=q):f=null):d.on&&(l(q).removeClass("droptarget"),
  d.on=!1);return f}function N(a){a||(a={});if("string"==$.type(a))return"destroy"==a&&(D=null),$("li:not(.virtual)",e).droppable(a),this;O=a;var b=$.extend({greedy:!0,tolerance:"pointer",hoverClass:"droptarget",addClasses:!1},a);b.activate=function(b,c){M();D=c;a.activate&&a.activate(b,c)};b.deactivate=function(b,c){ea();D=null;a.deactivate&&a.deactivate(b,c)};b.over=function(b,c){fa(rcube_event.get_mouse_pos(b),!1);a.over&&a.over(b,c)};$("li:not(.virtual)",e).droppable(b);return this}function P(a){a||
  (a={});if("string"==$.type(a))return"destroy"==a&&(Q=null),$("li:not(.virtual)",e).draggable(a),this;R=a;a=$.extend({appendTo:"body",revert:"invalid",iframeFix:!0,addClasses:!1,cursorAt:{left:-20,top:5},create:function(a,d){Q=d},helper:function(a){return $("<div>").attr("id","rcmdraglayer").text($.trim($(a.target).first().text()))}},a);$("li:not(.virtual)",e).draggable(a);return this}g=$.extend({id_prefix:"",autoexpand:1E3,selectable:!1,scroll_delay:500,scroll_step:5,scroll_speed:20,save_state:!1,
  keyboard:!0,tabexit:!0,parent_focus:!1,check_droptarget:function(a){return!a.virtual}},g||{});var e=$(ha),n=g.data||[],h={},k=null,w=!1,m=!1,C="",E=!1;var ca=K=J=I=void 0;var H=[],r,v,ba=0,x=0,p,u,D,Q,R,O,S=e.attr("id")||g.id_prefix||"0",t=this;this.container=e;this.expand=T;this.collapse=A;this.select=F;this.render=function(){if(!1!==t.triggerEvent("renderBefore",n)){e.html("");for(var a=0;a<n.length;a++)n[a].level=0,y(n[a],e);t.triggerEvent("renderAfter",e)}};this.reset=function(a){F("");n=[];h=
  {};w=!1;a?(R&&(Q&&P("destroy"),P(R)),O&&(D&&N("destroy"),N(O)),n=G(e,0)):e.html("");B()};this.drag_start=M;this.drag_end=ea;this.intersects=fa;this.droppable=N;this.draggable=P;this.update=function(a,b,d){var c,f,g=h[a];if(g){var k=l(a);var m=k.parent();if(b.id||b.html||b.children||b.classes||b.parent)b.parent&&(c=h[b.parent])?(m.closest("li").length&&(f=h[z(m.closest("li"))])&&(f.children=$.grep(f.children,function(a,b){return a.id!=g.id})),m=l(b.parent).children("ul").first(),c.children||(c.children=
  []),c.children.push(g)):void 0!==b.parent&&(m=e),$.extend(g,b),k=y(g,m,k);g.id!=a&&(delete h[a],h[g.id]=g);d&&W(k,"string"==typeof d?'[class~="'+d+'"]':"")}};this.insert=function(a,b,d){var c=b?h[b]:null;search_=m;h[a.id]||(state=Y(a.id,a.collapsed),void 0!==state&&(a.collapsed=state),c?(a.level=c.level+1,c.children||(c.children=[]),m=!1,c.children.push(a),b=l(b),1==c.children.length?(y(c,null,b),c=l(a.id)):c=y(a,b.children("ul").first()),search_&&(m=search_,c.is(":visible")||$("<li>").attr("id",
  c.attr("id")+"--xsR").attr("class",c.attr("class")).addClass("searchresult__").append(c.children().first().clone(!0,!0)).appendTo(e))):(a.level=0,n.push(a),c=y(a,e)),h[a.id]=a,"object"==typeof a.html&&(h[a.id].html=l(a.id,!0).children()),d&&W(c,"string"==typeof d?'[class~="'+d+'"]':""))};this.remove=function(a){var b;if(b=h[a]){var d=l(a,!0);var c=d.parent();d.remove();b.deleted=!0;delete h[a];m&&l(a,!1).remove();c.children().length||(c.parent().find("div.treetoggle").remove(),c.remove());return!0}return!1};
  this.get_item=V;this.get_node=function(a){return h[a]};this.get_selection=function(){return k};this.is_search=function(){return m};this.reset_search=B;if(e.length){g.data?Z({children:n}):n=G(e,0);k&&U(l(k,!0));e.attr("role","tree").on("focusin",function(a){E=!0}).on("focusout",function(a){E=!1}).on("click","div.treetoggle",function(a){var b=z($(this).parent()),d;(d=h[b])&&A(b,void 0,!d.collapsed);a.stopPropagation()}).on("click","li",function(a){if($(a.target).is("input"))return!0;var b=g.selectable?
  h[z($(this))]:null;b&&!b.virtual&&(F(b.id),a.stopPropagation())}).on("mousedown","a",function(a){var b=$(a.target),d=h[z(b.closest("li"))];if(d&&d.virtual&&!b.attr("href"))return a.preventDefault(),a.stopPropagation(),!1});if(g.searchbox){var L=$(g.searchbox).off("keyup.treelist").on("keyup.treelist",function(a){var b=rcube_event.get_keycode(a);rcube_event.get_modifier(a);switch(b){case 9:break;case 13:return X(this.value,!0),rcube_event.cancel(a);case 27:B();break;case 38:case 37:case 39:case 40:break;
  default:X(this.value,!1)}}).attr("autocomplete","off");L.parent().find("a.reset").off("click.treelist").on("click.treelist",function(a){B();return!1})}$(document.body).on("keydown",function(a){var b=a.target||{},d=rcube_event.get_keycode(a);if(!E||"INPUT"==b.nodeName&&38!=d&&40!=d||"TEXTAREA"==b.nodeName||"SELECT"==b.nodeName)return!0;switch(d){case 38:case 40:case 63232:case 63233:return b=g.keyboard?e.find(":focus").closest("li"):[],b.length&&aa(b,mod=38==d||63232==d?-1:1),rcube_event.cancel(a);
  case 37:case 39:var c;b=e.find(":focus").closest("li");if(b.length&&(b=z(b),(c=h[b])&&c.children.length&&c.collapsed!=(37==d))){a=rcube_event.get_modifier(a)==SHIFT_KEY;var f;(f=h[b])&&A(b,a,!f.collapsed)}return!1;case 9:g.keyboard&&g.tabexit&&(f=rcube_event.get_modifier(a)==SHIFT_KEY?"first":"last",f=e.find("li[role=treeitem]:has(a)")[f]().find("a:"+f),f.length&&(a=e.parent().get(0)||{scrollTop:0},d=a.scrollTop||a.scrollY,f.focus(),a.scrollTop=d))}return!0});g.parent_focus&&e.parent(":not(body)").click(function(a){if($(a.target).is("input"))return!0;
  !E&&k?$(V(k)).find(":focusable").first().focus():E||e.children("li:has(:focusable)").first().find(":focusable").first().focus()})}}rcube_treelist_widget.prototype.addEventListener=rcube_event_engine.prototype.addEventListener;rcube_treelist_widget.prototype.removeEventListener=rcube_event_engine.prototype.removeEventListener;rcube_treelist_widget.prototype.triggerEvent=rcube_event_engine.prototype.triggerEvent;
  