JSFiddle - React, Tailwind, and code Playground

by Graham Dixon

HTML

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dottie.min.js"></script>

JavaScript

var collapsibleList = function() {

    // allItems would globally contain any referencable objects
    var allItems = [],
        list = this;

    // assign items to the allItems object
    this.setItems = function(scope, items) {
        // assign the items into the allItems object
        allItems[scope] = items;

        // return the newly set object
        return list.getItems(scope);
    };

    // get items from the allItems object
    this.getItems = function(scope) {
        // remove any empty objects from the items object
        for (var key in allItems) {
            if (allItems[key].length === 0) {
                delete(allItems[key]);
            }
        }

        // return the newly set object
        return (scope ? allItems[scope] : allItems);
    };

    // discover scope from original items and return the remainder of the original scope
    this.findItemsByScope = function(scope, originalScope) {
        // check is this item macthes items in allItems
        if (this.getItems(scope)) {

            // when it does return a decorated response of the requested instruction
            return {
                "scope": scope,
                "discover": (originalScope ? originalScope.replace(scope + ".", "") : "")
            };
        } else if (scope.split(".").length > 1) {

            // check if the scope minus last segment matches an allItems scope
            return this.findItemsByScope(scope.split(".").filter(function(v, k, orig) {

                // remove the last item from the . seperated string
                return (k < orig.length - 1);
            }).join("."), (originalScope ? originalScope : scope));
        } else {

            // nothing mactched
            return false;
        }
    };
    
    // get an obj back from scope by discovering the obj in an allItems set which matches the scope
    this.getItemsByScope = function(scope) {
        // auto return failure unless success overides response
        var...