You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

treelist.min.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Roundcube Treelist Widget
  3. *
  4. * This file is part of the Roundcube Webmail client
  5. *
  6. * @licstart The following is the entire license notice for the
  7. * JavaScript code in this file.
  8. *
  9. * Copyright (c) 2013-2014, The Roundcube Dev Team
  10. *
  11. * The JavaScript code in this page is free software: you can
  12. * redistribute it and/or modify it under the terms of the GNU
  13. * General Public License (GNU GPL) as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option)
  15. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  16. * without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  18. *
  19. * As additional permission under GNU GPL version 3 section 7, you
  20. * may distribute non-source (e.g., minimized or compacted) forms of
  21. * that code without the copy of the GNU GPL normally required by
  22. * section 4, provided you include this license notice and a URL
  23. * through which recipients can access the Corresponding Source.
  24. *
  25. * @licend The above is the entire license notice
  26. * for the JavaScript code in this file.
  27. *
  28. * @author Thomas Bruederli <roundcube@gmail.com>
  29. * @requires jquery.js, common.js
  30. */
  31. function rcube_treelist_widget(fa,e){var I,J,K,L;function z(a,b,c){var d;if(d=k[a]){d.collapsed="undefined"==typeof c||c;var g=l(d.id,!0);g.attr("aria-expanded",d.collapsed?"false":"true");g.children("ul").first()[d.collapsed?"hide":"show"]();g.children("div.treetoggle").removeClass("collapsed expanded").addClass(d.collapsed?"collapsed":"expanded");s.triggerEvent("toggle",d);if(b&&d.children)for(g=0;g<d.children.length;g++)z(d.children[g].id,b,c);s.triggerEvent(d.collapsed?"collapse":"expand",d);
  32. b=d.collapsed;e.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!==s.triggerEvent("beforeselect",k[a])&&(h&&(l(h,!0).removeClass("selected").removeAttr("aria-selected"),n&&l(h).removeClass("selected").removeAttr("aria-selected"),h=null),a)){var b=l(a,!0);b.length&&(b.addClass("selected").attr("aria-selected","true"),h=a,n&&l(a).addClass("selected").attr("aria-selected",
  33. "true"),U(b));s.triggerEvent("select",k[a])}}function V(a,b){return l(a,b).get(0)}function W(a,b){var c,d,g=a.get(0).id,e=a.children().first().text().toUpperCase();a.parent().children("li"+b).each(function(a,b){0==a&&(c=b);if(b.id!=g)if(b.id!=g&&e>=$(b).children().first().text().toUpperCase())d=b;else return!1});d?a.insertAfter(d):c&&c.id!=g&&a.insertBefore(c);m=F(f,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,
  34. g){var e;if(!g.virtual&&!g.deleted&&0<=String(g.text).toLowerCase().indexOf(a)&&0>c.indexOf(g.id)){e=l(g.id);if(e.data("filtered"))return;$("<li>").attr("id",e.attr("id")+"--xsR").attr("class",e.attr("class")).addClass("searchresult__").append(e.children(":not(div.treetoggle,ul)").clone(!0,!0)).appendTo(f);c.push(g.id)}g.children&&g.children.length&&d(g.children)})};n&&($(f).children("li.searchresult__").remove(),n=!1);$(f).children("li").hide().removeClass("selected");d(m);n=!0;s.triggerEvent("search",
  35. {query:a,last:B,count:c.length,ids:c,execute:b||!1});B=a;return c.count}function A(){G&&G.val("");$(f).children("li.searchresult__").remove();$(f).children("li").filter(function(){return!$(this).data("filtered")}).show();n=!1;s.triggerEvent("search",{query:!1,last:B});B="";h&&E(h)}function x(a,b,c){if(!a.deleted){var d=$("<li>").attr("id",e.id_prefix+(e.id_encode?e.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)):
  36. 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==h&&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")+'">&nbsp;</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=
  37. a.level+1,x(a.children[c],b);return d}}function F(a,b){var c=[];a.children("li").each(function(a,g){var f,e=$(g),l=e.children("ul"),p={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(l,b+1)};l.length&&(p.childlistclass=l.attr("class"));p.children.length&&(void 0===p.collapsed&&(p.collapsed="none"==l.css("display")),f=Y(p.id,p.collapsed),void 0!==f&&(p.collapsed=f,l[f?"hide":
  38. "show"]()),e.children("div.treetoggle").length||$('<div class="treetoggle '+(p.collapsed?"collapsed":"expanded")+'">&nbsp;</div>').appendTo(e),e.attr("aria-expanded",p.collapsed?"false":"true"));e.hasClass("selected")&&(e.attr("aria-selected","true"),h=p.id);e.data("id",p.id);e.attr("role","treeitem").attr("aria-level",p.level+1);p.virtual&&e.children("a:first").attr("tabindex","0");c.push(p);k[p.id]=p});a.attr("role",0==b?"tree":"group");return c}function Z(a){a.id&&(k[a.id]=a);for(var b=0;a.children&&
  39. b<a.children.length;b++)Z(a.children[b])}function y(a){a=String(a.attr("id")).replace(RegExp("^"+e.id_prefix||"%"),"").replace(/--xsR$/,"");return e.id_decode?e.id_decode(a):a}function l(a,b){var c=e.id_encode?e.id_encode(a):a;return $("#"+e.id_prefix+c+(n&&!b?"--xsR":""),f)}function U(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 Y(a){if(e.save_state&&window.rcmail)return t||(t=rcmail.local_storage_get_item("treelist-"+
  40. S,{})),t[a]}function ga(a){var b=a.target||{},c=rcube_event.get_keycode(a);if(!C||"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=e.keyboard?f.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=f.find(":focus").closest("li");if(b.length&&(b=y(b),(d=k[b])&&d.children.length&&d.collapsed!=(37==c))){a=rcube_event.get_modifier(a)==SHIFT_KEY;
  41. var g;(g=k[b])&&z(b,a,!g.collapsed)}return!1;case 9:e.keyboard&&e.tabexit&&(g=rcube_event.get_modifier(a)==SHIFT_KEY?"first":"last",g=f.find("li[role=treeitem]:has(a)")[g]().find("a:"+g),g.length&&(a=f.parent().get(0)||{scrollTop:0},c=a.scrollTop||a.scrollY,g.focus(),a.scrollTop=c))}return!0}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?
  42. 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=f.offset();ba=bw.ie?0:window.pageYOffset;w=f.parent().scrollTop();c.top+=w;I=c.left;J=c.top;K=c.left+f.width();L=c.top+f.height();H=[];for(var d in k)a=l(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+
  43. a.offsetWidth,y2:c.top+b,on:d==u});f.height()>f.parent().height()&&f.parent().mousemove(function(a){var b=0;a=rcube_event.get_mouse_pos(a);a.y-=f.parent().offset().top;25>a.y&&0<w?b=-1:a.y>f.parent().height()-25&&(b=1);v&&0!=b?q||(q=window.setTimeout(function(){ca(b)},e.scroll_delay)):q&&(window.clearTimeout(q),q=null)}).mouseleave(function(){q&&(window.clearTimeout(q),q=null)})}}function da(){v&&(v=!1,q=null,r&&(clearTimeout(r),u=r=null),$("li.droptarget",f).removeClass("droptarget"))}function ca(a){if(v){var b=
  44. w;f.parent().get(0).scrollTop+=e.scroll_step*a;w=f.parent().scrollTop();q=null;w!=b&&(q=window.setTimeout(function(){ca(a)},e.scroll_speed))}}function ea(a,b){var c=bw.ie?-document.documentElement.scrollTop:ba,d=f.parent().scrollTop(),g=null;a.top=a.y+d-c;if(a.x<I||a.x>=K||a.top<J||a.top>=L)return b&&$("li.droptarget",f).removeClass("droptarget"),g;for(var h in H)c=H[h],a.x>=c.x1&&a.x<c.x2&&a.top>=c.y1&&a.top<c.y2?(g=k[h],g.children&&g.children.length&&g.collapsed&&e.autoexpand&&u!=h?(r&&clearTimeout(r),
  45. u=h,r=setTimeout(function(){T(u);M(!0);u=null;D&&$.ui.ddmanager.prepareOffsets($.ui.ddmanager.current,null)},e.autoexpand)):r&&u!=h&&(clearTimeout(r),r=u=null),e.check_droptarget(g)?(b&&(l(h).addClass("droptarget"),c.on=!0),g=h):g=null):c.on&&(l(h).removeClass("droptarget"),c.on=!1);return g}function N(a){a||(a={});if("string"==$.type(a))return"destroy"==a&&(D=null),$("li:not(.virtual)",f).droppable(a),this;O=a;var b=$.extend({greedy:!0,tolerance:"pointer",hoverClass:"droptarget",addClasses:!1},a);
  46. b.activate=function(b,d){M();D=d;a.activate&&a.activate(b,d)};b.deactivate=function(b,d){da();D=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)",f).droppable(b);return this}function P(a){a||(a={});if("string"==$.type(a))return"destroy"==a&&(Q=null),$("li:not(.virtual)",f).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=
  47. c},helper:function(a){return $("<div>").attr("id","rcmdraglayer").text($.trim($(a.target).first().text()))}},a);$("li:not(.virtual)",f).draggable(a);return this}e=$.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}},e||{});var f=$(fa),m=e.data||[],k={},h=null,v=!1,n=!1,B="",C=!1;L=K=J=I=void 0;var H=[],r,u,ba=0,w=0,q,G,t,D,Q,R,O,S=f.attr("id")||e.id_prefix||
  48. "0",s=this;this.container=f;this.expand=T;this.collapse=z;this.select=E;this.render=function(){if(!1!==s.triggerEvent("renderBefore",m)){f.html("");for(var a=0;a<m.length;a++)m[a].level=0,x(m[a],f);s.triggerEvent("renderAfter",f)}};this.reset=function(a){E("");m=[];k={};v=!1;a?(R&&(Q&&P("destroy"),P(R)),O&&(D&&N("destroy"),N(O)),m=F(f,0)):f.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,e,h,n,m=k[a];if(m){d=l(a);
  49. e=d.parent();if(b.id||b.html||b.children||b.classes||b.parent)b.parent&&(h=k[b.parent])?(e.closest("li").length&&(n=k[y(e.closest("li"))])&&(n.children=$.grep(n.children,function(a,b){return a.id!=m.id})),e=l(b.parent).children("ul").first(),h.children||(h.children=[]),h.children.push(m)):void 0!==b.parent&&(e=f),$.extend(m,b),d=x(m,e,d);m.id!=a&&(delete k[a],k[m.id]=m);c&&W(d,"string"==typeof c?'[class~="'+c+'"]':"")}};this.insert=function(a,b,c){var d;d=b?k[b]:null;search_=n;k[a.id]||(state=Y(a.id,
  50. a.collapsed),void 0!==state&&(a.collapsed=state),d?(a.level=d.level+1,d.children||(d.children=[]),n=!1,d.children.push(a),b=l(b),1==d.children.length?(x(d,null,b),d=l(a.id)):d=x(a,b.children("ul").first()),search_&&(n=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,m.push(a),d=x(a,f)),k[a.id]=a,"object"==typeof a.html&&(k[a.id].html=l(a.id,!0).children()),
  51. c&&W(d,"string"==typeof c?'[class~="'+c+'"]':""))};this.remove=function(a){var b,c;return(b=k[a])?(c=l(a,!0),c.remove(),b.deleted=!0,delete k[a],n&&l(a,!1).remove(),!0):!1};this.get_item=V;this.get_node=function(a){return k[a]};this.get_selection=function(){return h};this.is_search=function(){return n};this.reset_search=A;f.length&&(e.data?Z({children:m}):m=F(f,0),h&&U(l(h,!0)),f.attr("role","tree").on("focusin",function(a){C=!0}).on("focusout",function(a){C=!1}).on("click","div.treetoggle",function(a){var b=
  52. y($(this).parent()),c;(c=k[b])&&z(b,void 0,!c.collapsed);a.stopPropagation()}).on("click","li",function(a){if($(a.target).is("input"))return!0;var b=e.selectable?k[y($(this))]:null;b&&!b.virtual&&(E(b.id),a.stopPropagation())}).on("mousedown","a",function(a){var b=$(a.target),c=k[y(b.closest("li"))];if(c&&c.virtual&&!b.attr("href"))return a.preventDefault(),a.stopPropagation(),!1}),e.searchbox&&(G=$(e.searchbox).off("keyup.treelist").on("keyup.treelist",function(a){var b=rcube_event.get_keycode(a);
  53. 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",ga),e.parent_focus&&f.parent(":not(body)").click(function(a){!C&&h?$(V(h)).find(":focusable").first().focus():C||f.children("li:has(:focusable)").first().find(":focusable").first().focus()}))}
  54. 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;