web/data/jquery.treeview.js
changeset 10097 e62d2c73784f
parent 5767 1d811df051c2
child 10098 e8889211f256
equal deleted inserted replaced
10096:decd60fa8cc5 10097:e62d2c73784f
    11  *   http://www.gnu.org/licenses/gpl.html
    11  *   http://www.gnu.org/licenses/gpl.html
    12  *
    12  *
    13  * Revision: $Id: jquery.treeview.js 4684 2010-02-07 19:08:06Z joern.zaefferer $
    13  * Revision: $Id: jquery.treeview.js 4684 2010-02-07 19:08:06Z joern.zaefferer $
    14  * updated by Aurelien Campeas, 2010-09-01, to handle top-level ajax loads
    14  * updated by Aurelien Campeas, 2010-09-01, to handle top-level ajax loads
    15  *
    15  *
    16  */;(function($){$.extend($.fn,{swapClass:function(c1,c2){var c1Elements=this.filter('.'+c1);this.filter('.'+c2).removeClass(c2).addClass(c1);c1Elements.removeClass(c1).addClass(c2);return this;},replaceClass:function(c1,c2){return this.filter('.'+c1).removeClass(c1).addClass(c2).end();},hoverClass:function(className){className=className||"hover";return this.hover(function(){$(this).addClass(className);},function(){$(this).removeClass(className);});},heightToggle:function(animated,callback){animated?this.animate({height:"toggle"},animated,callback):this.each(function(){jQuery(this)[jQuery(this).is(":hidden")?"show":"hide"]();if(callback)callback.apply(this,arguments);});},heightHide:function(animated,callback){if(animated){this.animate({height:"hide"},animated,callback);}else{this.hide();if(callback)this.each(callback);}},prepareBranches:function(settings){if(!settings.prerendered){this.filter(":last-child:not(ul)").addClass(CLASSES.last);this.filter((settings.collapsed?"":"."+CLASSES.closed)+":not(."+CLASSES.open+")").find(">ul").hide();}return this.filter(":has(>ul)");},applyClasses:function(settings,toggler){this.filter(":has(>ul):not(:has(>a))").find(">span").click(function(event){toggler.apply($(this).next());}).add($("a",this)).hoverClass();if(!settings.prerendered){this.filter(":has(>ul:hidden)").addClass(CLASSES.expandable).replaceClass(CLASSES.last,CLASSES.lastExpandable);this.not(":has(>ul:hidden)").addClass(CLASSES.collapsable).replaceClass(CLASSES.last,CLASSES.lastCollapsable);this.prepend("<div class=\""+CLASSES.hitarea+"\"/>").find("div."+CLASSES.hitarea).each(function(){var classes="";$.each($(this).parent().attr("class").split(" "),function(){classes+=this+"-hitarea ";});$(this).addClass(classes);});}this.find("div."+CLASSES.hitarea).click(toggler);},treeview:function(settings){if(this.attr('cubicweb:type')=='prepared-treeview'){return this;}this.attr('cubicweb:type','prepared-treeview');settings=$.extend({cookieId:"treeview"},settings);if(settings.add){return this.trigger("add",[settings.add]);}if(settings.toggle){var callback=settings.toggle;settings.toggle=function(){return callback.apply($(this).parent()[0],arguments);};}function treeController(tree,control){function handler(filter){return function(){toggler.apply($("div."+CLASSES.hitarea,tree).filter(function(){return filter?$(this).parent("."+filter).length:true;}));return false;};}$("a:eq(0)",control).click(handler(CLASSES.collapsable));$("a:eq(1)",control).click(handler(CLASSES.expandable));$("a:eq(2)",control).click(handler());}function toggler(){$(this).parent().find(">.hitarea").swapClass(CLASSES.collapsableHitarea,CLASSES.expandableHitarea).swapClass(CLASSES.lastCollapsableHitarea,CLASSES.lastExpandableHitarea).end().swapClass(CLASSES.collapsable,CLASSES.expandable).swapClass(CLASSES.lastCollapsable,CLASSES.lastExpandable).find(">ul").heightToggle(settings.animated,settings.toggle);if(settings.unique){$(this).parent().siblings().find(">.hitarea").replaceClass(CLASSES.collapsableHitarea,CLASSES.expandableHitarea).replaceClass(CLASSES.lastCollapsableHitarea,CLASSES.lastExpandableHitarea).end().replaceClass(CLASSES.collapsable,CLASSES.expandable).replaceClass(CLASSES.lastCollapsable,CLASSES.lastExpandable).find(">ul").heightHide(settings.animated,settings.toggle);}}function serialize(){function binary(arg){return arg?1:0;}var data=[];branches.each(function(i,e){data[i]=$(e).is(":has(>ul:visible)")?1:0;});$.cookie(settings.cookieId,data.join(""));}function deserialize(){var stored=$.cookie(settings.cookieId);if(stored){var data=stored.split("");branches.each(function(i,e){$(e).find(">ul")[parseInt(data[i])?"show":"hide"]();});}}this.addClass("treeview");var branches=this.find("li").prepareBranches(settings);switch(settings.persist){case"cookie":var toggleCallback=settings.toggle;settings.toggle=function(){serialize();if(toggleCallback){toggleCallback.apply(this,arguments);}};deserialize();break;case"location":var current=this.find("a").filter(function(){return this.href.toLowerCase()==location.href.toLowerCase();});if(current.length){current.addClass("selected").parents("ul, li").add(current.next()).show();}break;}branches.applyClasses(settings,toggler);if(settings.control){treeController(this,settings.control);$(settings.control).show();}return this.bind("add",function(event,branches){$(branches).prev().removeClass(CLASSES.last).removeClass(CLASSES.lastCollapsable).removeClass(CLASSES.lastExpandable).find(">.hitarea").removeClass(CLASSES.lastCollapsableHitarea).removeClass(CLASSES.lastExpandableHitarea);$(branches).find("li").andSelf().prepareBranches(settings).applyClasses(settings,toggler);});}});var CLASSES=$.fn.treeview.classes={open:"open",closed:"closed",expandable:"expandable",expandableHitarea:"expandable-hitarea",lastExpandableHitarea:"lastExpandable-hitarea",collapsable:"collapsable",collapsableHitarea:"collapsable-hitarea",lastCollapsableHitarea:"lastCollapsable-hitarea",lastCollapsable:"lastCollapsable",lastExpandable:"lastExpandable",last:"last",hitarea:"hitarea"};$.fn.Treeview=$.fn.treeview;})(jQuery);
    16  */
       
    17 (function($) {
       
    18     $.extend($.fn, {
       
    19         swapClass: function(c1, c2) {
       
    20             var c1Elements = this.filter("." + c1);
       
    21             this.filter("." + c2).removeClass(c2).addClass(c1);
       
    22             c1Elements.removeClass(c1).addClass(c2);
       
    23             return this;
       
    24         },
       
    25         replaceClass: function(c1, c2) {
       
    26             return this.filter("." + c1).removeClass(c1).addClass(c2).end();
       
    27         },
       
    28         hoverClass: function(className) {
       
    29             className = className || "hover";
       
    30             return this.hover(function() {
       
    31                 $(this).addClass(className);
       
    32             }, function() {
       
    33                 $(this).removeClass(className);
       
    34             });
       
    35         },
       
    36         heightToggle: function(animated, callback) {
       
    37             animated ? this.animate({
       
    38                 height: "toggle"
       
    39             }, animated, callback) : this.each(function() {
       
    40                 jQuery(this)[jQuery(this).is(":hidden") ? "show" : "hide"]();
       
    41                 if (callback) callback.apply(this, arguments);
       
    42             });
       
    43         },
       
    44         heightHide: function(animated, callback) {
       
    45             if (animated) {
       
    46                 this.animate({
       
    47                     height: "hide"
       
    48                 }, animated, callback);
       
    49             } else {
       
    50                 this.hide();
       
    51                 if (callback) this.each(callback);
       
    52             }
       
    53         },
       
    54         prepareBranches: function(settings) {
       
    55             if (!settings.prerendered) {
       
    56                 this.filter(":last-child:not(ul)").addClass(CLASSES.last);
       
    57                 this.filter((settings.collapsed ? "" : "." + CLASSES.closed) + ":not(." + CLASSES.open + ")").find(">ul").hide();
       
    58             }
       
    59             return this.filter(":has(>ul)");
       
    60         },
       
    61         applyClasses: function(settings, toggler) {
       
    62             this.filter(":has(>ul):not(:has(>a))").find(">span").click(function(event) {
       
    63                 toggler.apply($(this).next());
       
    64             }).add($("a", this)).hoverClass();
       
    65             if (!settings.prerendered) {
       
    66                 this.filter(":has(>ul:hidden)").addClass(CLASSES.expandable).replaceClass(CLASSES.last, CLASSES.lastExpandable);
       
    67                 this.not(":has(>ul:hidden)").addClass(CLASSES.collapsable).replaceClass(CLASSES.last, CLASSES.lastCollapsable);
       
    68                 this.prepend('<div class="' + CLASSES.hitarea + '"/>').find("div." + CLASSES.hitarea).each(function() {
       
    69                     var classes = "";
       
    70                     $.each($(this).parent().attr("class").split(" "), function() {
       
    71                         classes += this + "-hitarea ";
       
    72                     });
       
    73                     $(this).addClass(classes);
       
    74                 });
       
    75             }
       
    76             this.find("div." + CLASSES.hitarea).click(toggler);
       
    77         },
       
    78         treeview: function(settings) {
       
    79             if (this.attr("cubicweb:type") == "prepared-treeview") {
       
    80                 return this;
       
    81             }
       
    82             this.attr("cubicweb:type", "prepared-treeview");
       
    83             settings = $.extend({
       
    84                 cookieId: "treeview"
       
    85             }, settings);
       
    86             if (settings.add) {
       
    87                 return this.trigger("add", [ settings.add ]);
       
    88             }
       
    89             if (settings.toggle) {
       
    90                 var callback = settings.toggle;
       
    91                 settings.toggle = function() {
       
    92                     return callback.apply($(this).parent()[0], arguments);
       
    93                 };
       
    94             }
       
    95             function treeController(tree, control) {
       
    96                 function handler(filter) {
       
    97                     return function() {
       
    98                         toggler.apply($("div." + CLASSES.hitarea, tree).filter(function() {
       
    99                             return filter ? $(this).parent("." + filter).length : true;
       
   100                         }));
       
   101                         return false;
       
   102                     };
       
   103                 }
       
   104                 $("a:eq(0)", control).click(handler(CLASSES.collapsable));
       
   105                 $("a:eq(1)", control).click(handler(CLASSES.expandable));
       
   106                 $("a:eq(2)", control).click(handler());
       
   107             }
       
   108             function toggler() {
       
   109                 $(this).parent().find(">.hitarea").swapClass(CLASSES.collapsableHitarea, CLASSES.expandableHitarea).swapClass(CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea).end().swapClass(CLASSES.collapsable, CLASSES.expandable).swapClass(CLASSES.lastCollapsable, CLASSES.lastExpandable).find(">ul").heightToggle(settings.animated, settings.toggle);
       
   110                 if (settings.unique) {
       
   111                     $(this).parent().siblings().find(">.hitarea").replaceClass(CLASSES.collapsableHitarea, CLASSES.expandableHitarea).replaceClass(CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea).end().replaceClass(CLASSES.collapsable, CLASSES.expandable).replaceClass(CLASSES.lastCollapsable, CLASSES.lastExpandable).find(">ul").heightHide(settings.animated, settings.toggle);
       
   112                 }
       
   113             }
       
   114             function serialize() {
       
   115                 function binary(arg) {
       
   116                     return arg ? 1 : 0;
       
   117                 }
       
   118                 var data = [];
       
   119                 branches.each(function(i, e) {
       
   120                     data[i] = $(e).is(":has(>ul:visible)") ? 1 : 0;
       
   121                 });
       
   122                 $.cookie(settings.cookieId, data.join(""));
       
   123             }
       
   124             function deserialize() {
       
   125                 var stored = $.cookie(settings.cookieId);
       
   126                 if (stored) {
       
   127                     var data = stored.split("");
       
   128                     branches.each(function(i, e) {
       
   129                         $(e).find(">ul")[parseInt(data[i]) ? "show" : "hide"]();
       
   130                     });
       
   131                 }
       
   132             }
       
   133             this.addClass("treeview");
       
   134             var branches = this.find("li").prepareBranches(settings);
       
   135             switch (settings.persist) {
       
   136               case "cookie":
       
   137                 var toggleCallback = settings.toggle;
       
   138                 settings.toggle = function() {
       
   139                     serialize();
       
   140                     if (toggleCallback) {
       
   141                         toggleCallback.apply(this, arguments);
       
   142                     }
       
   143                 };
       
   144                 deserialize();
       
   145                 break;
       
   146 
       
   147               case "location":
       
   148                 var current = this.find("a").filter(function() {
       
   149                     return this.href.toLowerCase() == location.href.toLowerCase();
       
   150                 });
       
   151                 if (current.length) {
       
   152                     current.addClass("selected").parents("ul, li").add(current.next()).show();
       
   153                 }
       
   154                 break;
       
   155             }
       
   156             branches.applyClasses(settings, toggler);
       
   157             if (settings.control) {
       
   158                 treeController(this, settings.control);
       
   159                 $(settings.control).show();
       
   160             }
       
   161             return this.bind("add", function(event, branches) {
       
   162                 $(branches).prev().removeClass(CLASSES.last).removeClass(CLASSES.lastCollapsable).removeClass(CLASSES.lastExpandable).find(">.hitarea").removeClass(CLASSES.lastCollapsableHitarea).removeClass(CLASSES.lastExpandableHitarea);
       
   163                 $(branches).find("li").andSelf().prepareBranches(settings).applyClasses(settings, toggler);
       
   164             });
       
   165         }
       
   166     });
       
   167     var CLASSES = $.fn.treeview.classes = {
       
   168         open: "open",
       
   169         closed: "closed",
       
   170         expandable: "expandable",
       
   171         expandableHitarea: "expandable-hitarea",
       
   172         lastExpandableHitarea: "lastExpandable-hitarea",
       
   173         collapsable: "collapsable",
       
   174         collapsableHitarea: "collapsable-hitarea",
       
   175         lastCollapsableHitarea: "lastCollapsable-hitarea",
       
   176         lastCollapsable: "lastCollapsable",
       
   177         lastExpandable: "lastExpandable",
       
   178         last: "last",
       
   179         hitarea: "hitarea"
       
   180     };
       
   181     $.fn.Treeview = $.fn.treeview;
       
   182 })(jQuery);