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