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