MooTools.MultiSelectTree.js

MooTools select tree built off a multiselect element.

by khrome

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/mootools/1.4.5/mootools-core-full-compat.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mootools-more/1.4.0.1/mootools-more-yui-compressed.js"></script>
<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 .children_selected:before {
    color : black;
    content: '\2713'; }
  .multiselecttree .partially_selected_node:before {
    font-size:38px;
    line-height:6px;
    color : black;
    content: '-'; }
  .multiselecttree ul {
    margin-left: 20px; }
.multiselecttree_label {
  color: grey;
  margin-left: 3px; }

JavaScript

// todo: add class to inactive nodes
// todo: bug deselecting sibling node
// todo: add events
if(!MultiSelectTree){
    var MultiSelectTree = new Class({
        Implements : [Events, Options],
        options : {
            allowOrphans : true, //remove any orphaned nodes without complaining
            relationshipAttribute : 'ancestry',
            openGlyph : '&#x25BC;',
            closedGlyph : '&#x25B6;',
            selectChildren : true,
            selectNode : false,
            openToSelected : true,
            partialSelect : true,
            openDepth : 1
        },
        initialize : function(element, options){
            this.element = document.id(element);
            this.setOptions(options);
            this.options.openDepth++;
            var tree = {};
            this.element.getChildren('option').each(function(item, key){
                var descention = item.getAttribute(this.options.relationshipAttribute);
                descention = descention=='' ? item.value : descention+'>'+item.value;
                tree = this.buildTree(tree, descention, item.value);
            }.bind(this));
            var el = this.buildList(tree);
            el.addClass('multiselecttree');
            el.inject(this.element, 'after');
            this.element.hide();
            el.show();
            this.root = el;
        },
        getOption : function(id){
            var result;
            this.element.getChildren('option').each(function(element){
                if(element.get('value') == id) result = element;
            });
            return result;
        },
        buildTree : function(tree, id, value){
            var node = tree;
            id.split('>').each(function(key, index){
                var cleanKey = this.getOption(key.trim()).get('html');
                if(!node[cleanKey]) node[cleanKey] = {'*':value};
                else node[cleanKey]['*'] = key;
                node = node[cleanKey];
            }.bind(this));
      ...