From 5d3ab0ed5fe2738b5f59c01839797b614e7d42a6 Mon Sep 17 00:00:00 2001 From: AlexeyKosov Date: Wed, 23 Jul 2014 10:38:42 +0400 Subject: [PATCH] Update jquery.select-hierarchy.js --- lib/js/jquery.select-hierarchy.js | 268 +++++++++++++++--------------- 1 file changed, 138 insertions(+), 130 deletions(-) diff --git a/lib/js/jquery.select-hierarchy.js b/lib/js/jquery.select-hierarchy.js index 3cd5f48..d6157f4 100644 --- a/lib/js/jquery.select-hierarchy.js +++ b/lib/js/jquery.select-hierarchy.js @@ -3,137 +3,145 @@ * * Turns a single select containing breadcrumb trails into multiple dynamic selects to allow easy drill-down * - * Author: Andrew Ingram (andy@andrewingram.net) */ (function($){ - $.fn.selectHierarchy = function(options) { - var defaults = { - separator: ' > ', - hideOriginal: true, - placeholder: '------' - }; - var options = $.extend(defaults, options); - var obj = $(this); - var max_depth = 1; - - var choices = obj.find('option').map(function(){ - var val = $(this).val(); - - if (val) { - var txt = $(this).text(); - var segments = txt.split(options.separator); - var depth = segments.length; - - if (depth > max_depth) { - max_depth = depth; - } - - var result = { - label: txt, - short_label: segments[depth-1], - value: val, - depth: depth, - children: [] - }; - - return result; - } - }); - - var roots = []; - - // Build up child values - for (var depth=1; depth<=max_depth; depth++) { - $.each(choices, function() { - var parent = this; - - if (parent.depth==depth) { - if (depth===1) { - roots.push(this); - } - - $.each(choices, function() { - var child = this; - if (child.depth == depth+1 && child.label.match("^"+parent.label)==parent.label) { - parent.children.push(child); - } - }); - } - }); - } - - if (options.hideOriginal) { - obj.hide(); - } - obj.wrap(''); - obj.after(''); - var root_select = obj.next(); - - root_select.data('depth', 1); - - $.each(roots, function(){ - var opt = $('