jQuery.MultiSelectTree.js

jQuery select tree built off a multiselect element.

by khrome

HTML

<select multiple name="selector" class="treeselect" id="treeselect">
    <option value="1" heredity="">An Item</option>
    <option value="2" heredity="1" disabled>A Test</option>
    <option value="3" heredity="1>2">Another Test</option>
    <option value="4" heredity="1>2">A Test3</option>
    <option value="5" heredity="1">A Test4</option>
    <option value="6" heredity="1">A Test5</option>
    <option value="7" heredity="1>5">A Test2</option>
    <option value="8" heredity="">fsfdsfg</option>
    <option value="9" heredity="">werttg</option>
</select>

CSS

.multiselecttree {
  line-height: 18px; }
  .multiselecttree .selected_node {
    color: black; }
  .multiselecttree .multiselecttree_label {
    padding-left: 1px;
    cursor: pointer; }
  .multiselecttree .multiselecttree_label:before {
    margin-right: 3px;
    display: inline-block;
    height: 13px;
    width: 13px;
    background-color: #CCCCCC;
    content: '\A0';
    background-color: #ececec;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#ececec), to(#a8a8a8));
    background-image: -webkit-linear-gradient(top, #ececec, #a8a8a8);
    background-image: -moz-linear-gradient(top, #ececec, #a8a8a8);
    background-image: -o-linear-gradient(top, #ececec, #a8a8a8);
    background-image: -ms-linear-gradient(top, #ececec, #a8a8a8);
    background-image: linear-gradient(top, #ececec, #a8a8a8);
    box-shadow: inset 0px 0px 3px #686868;
    -moz-box-shadow: inset 0px 0px 3px #686868;
    -webkit-box-shadow: inset 0px 0px 3px #686868;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    border-radius: 2px;
    border: 1px solid #626262; }
  .multiselecttree .selected_node:before {
    content: '\2713'; }
  .multiselecttree ul {
    margin-left: 20px; }
.multiselecttree_label {
  color: grey;
  margin-left: 3px; }

JavaScript

/*
 * jsMultiSelectTree
 *
 * I just stepped in some jQuery, could someone hand me something to wipe this off?
 *
 * Abbey Hawk Sparrow
 * 
 */


(function(){
    if(jQuery && jQuery.MultiSelectTree) { return; }
    (function(jQuery){ //jqrysx
        jQuery.fn.MultiSelectTree = function(options) { //jqrysx
            var object = this;
            var defaults = {
                element : '#multiselecttree',
                allowOrphans : true, //remove any orphaned nodes without complaining
                relationshipAttribute : 'ancestry',
                openGlyph : '&#x25BC;',
                closedGlyph : '&#x25B6;',
                selectChildren : true,
                selectNode : false,
                openToSelected : true,
                openDepth : 1
            }
            var closedGlyphValue;
            var settings = jQuery.extend(defaults, options); //jqrysx
            if(typeof settings.element == 'string') settings.element = jQuery('#'+settings.element)[0];
            if(!settings.element) settings.element = jQuery(this);
            settings.openDepth++;
            var tree = {};
            var getOption = function(id){
                result = false;
                jQuery(settings.element).children('option').each(function(key, item){
                    if(item.value == id) result = item;
                });
                return result;
            };
            var countFieldsInObject = function(obj){ //jqrysx
                var count = 0;
                for (k in obj) if (obj.hasOwnProperty(k)) count++;
                return count;
            };
            var is_object = function(mixed_var){
                if (Object.prototype.toString.call(mixed_var) === '[object Array]') return false;
                return mixed_var !== null && typeof mixed_var == 'object';
            };
            var addToTree = function(tree, id, value){
                var node = tree;
                jQuery.each(id.split('>'),...