cubicweb/web/data/jquery-treeview/jquery.treeview.async.js
changeset 11057 0b59724cb3f2
parent 10100 6718c03f8938
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 /*
       
     2  * Async Treeview 0.1 - Lazy-loading extension for Treeview
       
     3  *
       
     4  * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
       
     5  *
       
     6  * Copyright 2010 Jörn Zaefferer
       
     7  * Released under the MIT license:
       
     8  *   http://www.opensource.org/licenses/mit-license.php
       
     9  */
       
    10 
       
    11 ;(function($) {
       
    12 
       
    13 function load(settings, root, child, container) {
       
    14 	function createNode(parent) {
       
    15 		var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
       
    16 		if (this.classes) {
       
    17 			current.children("span").addClass(this.classes);
       
    18 		}
       
    19 		if (this.expanded) {
       
    20 			current.addClass("open");
       
    21 		}
       
    22 		if (this.hasChildren || this.children && this.children.length) {
       
    23 			var branch = $("<ul/>").appendTo(current);
       
    24 			if (this.hasChildren) {
       
    25 				current.addClass("hasChildren");
       
    26 				createNode.call({
       
    27 					classes: "placeholder",
       
    28 					text: "&nbsp;",
       
    29 					children:[]
       
    30 				}, branch);
       
    31 			}
       
    32 			if (this.children && this.children.length) {
       
    33 				$.each(this.children, createNode, [branch])
       
    34 			}
       
    35 		}
       
    36 	}
       
    37 	$.ajax($.extend(true, {
       
    38 		url: settings.url,
       
    39 		dataType: "json",
       
    40 		data: {
       
    41 			root: root
       
    42 		},
       
    43 		success: function(response) {
       
    44 			child.empty();
       
    45 			$.each(response, createNode, [child]);
       
    46 	        $(container).treeview({add: child});
       
    47 	    }
       
    48 	}, settings.ajax));
       
    49 	/*
       
    50 	$.getJSON(settings.url, {root: root}, function(response) {
       
    51 		function createNode(parent) {
       
    52 			var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
       
    53 			if (this.classes) {
       
    54 				current.children("span").addClass(this.classes);
       
    55 			}
       
    56 			if (this.expanded) {
       
    57 				current.addClass("open");
       
    58 			}
       
    59 			if (this.hasChildren || this.children && this.children.length) {
       
    60 				var branch = $("<ul/>").appendTo(current);
       
    61 				if (this.hasChildren) {
       
    62 					current.addClass("hasChildren");
       
    63 					createNode.call({
       
    64 						classes: "placeholder",
       
    65 						text: "&nbsp;",
       
    66 						children:[]
       
    67 					}, branch);
       
    68 				}
       
    69 				if (this.children && this.children.length) {
       
    70 					$.each(this.children, createNode, [branch])
       
    71 				}
       
    72 			}
       
    73 		}
       
    74 		child.empty();
       
    75 		$.each(response, createNode, [child]);
       
    76         $(container).treeview({add: child});
       
    77     });
       
    78     */
       
    79 }
       
    80 
       
    81 var proxied = $.fn.treeview;
       
    82 $.fn.treeview = function(settings) {
       
    83 	if (!settings.url) {
       
    84 		return proxied.apply(this, arguments);
       
    85 	}
       
    86 	if (!settings.root) {
       
    87 		settings.root = "source";
       
    88 	}
       
    89 	var container = this;
       
    90 	if (!container.children().size())
       
    91 		load(settings, settings.root, this, container);
       
    92 	var userToggle = settings.toggle;
       
    93 	return proxied.call(this, $.extend({}, settings, {
       
    94 		collapsed: true,
       
    95 		toggle: function() {
       
    96 			var $this = $(this);
       
    97 			if ($this.hasClass("hasChildren")) {
       
    98 				var childList = $this.removeClass("hasChildren").find("ul");
       
    99 				load(settings, this.id, childList, container);
       
   100 			}
       
   101 			if (userToggle) {
       
   102 				userToggle.apply(this, arguments);
       
   103 			}
       
   104 		}
       
   105 	}));
       
   106 };
       
   107 
       
   108 })(jQuery);