Element menu customization

Adds item into element menu and show toolbar and toolbox customization

                        

    
        SurveyEditor.StylesManager.applyTheme("bootstrap");
    


var editorOptions = { };
var editor = new SurveyEditor.SurveyEditor("editorElement", editorOptions);

// Add a new item into element menu
editor.onDefineElementMenuItems.add(function(editor, options) {
    options.items.unshift({name: "addtosharedrepo", text: "Add Into Shared Repository", onClick: function(obj){ addIntoCustomItems(obj);}});
});

// Add a button to the toolbar
editor.toolbarItems.push({id: "toolboxCustomization", visible: true, title: "Toolbox Customization", enabled: true, action: function() { showToolboxCustomization(); }});

var allToolboxItems = [];

//You have to replace getCustomItems/setCustomItems functions with the code that get/set data in the database
function getCustomItems() {
    var res = window.localStorage.getItem("sharedquestions");
    if(!res) return {};
    return JSON.parse(res);
}
function setCustomItems(items) {
    var str = JSON.stringify(items);
    window.localStorage.setItem("sharedquestions", str);
}

function addIntoCustomItems(element) {
    var json = new Survey.JsonObject().toJsonObject(element);
    json.type = element.getType();
    var item = { name: element.name, iconName: 'icon-' + element.getType(), title: element.title,
        json: json, isCopied: false, isUsed: isItemUsed(element.name), isStandard: false };
    var items = getCustomItems();
    items[item.name] = item;
    setCustomItems(items);
}
function removeCustomItem(name) {
    var items = getCustomItems();
    delete items[name];
    setCustomItems(items);
    var container = document.getElementById("customItems");
    var div = document.getElementById("toobaritem_" + name);
    container.removeChild(div);
}

function isItemUsed(name) {
    return editor.toolbox.indexOf(name) > -1;
}

function addToolBoxItemIntoList(container, item) {
    allToolboxItems.push(item);
    var div = document.createElement("div");
    div.id = "toobaritem_" + item.name;
    var checkbox = document.createElement('input');
    checkbox.type = "checkbox";
    checkbox.name = item.name;
    checkbox.checked = item.isUsed;
    checkbox.id = item.name;
    checkbox.dataItem = item;
    checkbox.onchange = function() {this.dataItem.isUsed = this.checked; }

    var label = document.createElement('label')
    label.style.marginLeft = "5px";
    label.htmlFor = item.name;
    label.appendChild(document.createTextNode(item.title));

    var removeButton = null;
    if(!item.isStandard) {
        removeButton = document.createElement("button");
        removeButton.style.marginLeft = "7px";
        removeButton.appendChild(document.createTextNode("Remove"));
        removeButton.onclick = function() { removeCustomItem(item.name) };
    }

    div.appendChild(checkbox);
    div.appendChild(label);
    if(removeButton) {
        div.appendChild(removeButton);
    }

    container.appendChild(div);
}

function createDefaultItem(typeName) {
    var question = Survey.ElementFactory.Instance.createElement(typeName, "q1");
    if(!question) {
        question = Survey.JsonObject.metaData.createClass(typeName);
    }
    var json = new Survey.JsonObject().toJsonObject(question);
    json.type = question.getType();
    return { name: typeName, iconName: 'icon-' + typeName, title: SurveyEditor.editorLocalization.getString('qt.' + typeName),
        json: json, isCopied: false, isUsed: isItemUsed(typeName), isStandard: true };
}

function loadAllToolboxItems() {
    allToolboxItems = [];
    var container = document.getElementById("standardItems");
    container.innerHTML = "";
    var allTypes = Survey.ElementFactory.Instance.getAllTypes();
    for(var i = 0; i < allTypes.length; i ++) {
        addToolBoxItemIntoList(container, createDefaultItem(allTypes[i]));
    }
    container = document.getElementById("customItems");
    container.innerHTML = "";
    var customItems = getCustomItems();
    for(var name in customItems) {
        var item = customItems[name];
        item.isUsed = isItemUsed(name);
        addToolBoxItemIntoList(container, item);
    }
    if(container.children.length == 0) {
        container.innerHTML = "Select the question, click on '...' and then click on the first item to add the question here.";
    }
}

function applyToolboxItems() {
    editor.toolbox.clearItems();
    var newItems = [];
    for(var i = 0; i < allToolboxItems.length; i ++) {
        if(allToolboxItems[i].isUsed) {
            editor.toolbox.addItem(allToolboxItems[i]);
        }
    }
}

function showToolboxCustomization() {
    loadAllToolboxItems();
    $('#toolboxCustomization').modal('show');
}
                    
<!DOCTYPE html>
<html>
<head>
    <title>Adds item into element menu and show toolbar and toolbox customization, Survey Builder Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>
<script src="https://surveyjs.azureedge.net/1.0.69/survey.ko.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.0/ace.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.0/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
    <!-- Uncomment to enable Select2
    <script src="https://unpkg.com/jquery"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
    -->
    <link href="https://surveyjs.azureedge.net/1.0.69/surveyeditor.css" type="text/css" rel="stylesheet" />
    <script src="https://surveyjs.azureedge.net/1.0.69/surveyeditor.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/bootstrap@3.3.7/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="./index.css">

</head>
<body>
    <svg style="display: none;">
    <symbol viewBox="0 0 16 16" id="icon-actionaddtosharedrepo"><path d="M7.3 8H1v2h5.2c.2-.7.6-1.4 1.1-2zM10 6.2V5H1v2h7.3c.5-.3 1.1-.6 1.7-.8zM1 2h9v2H1zM11.5 7C9 7 7 9 7 11.5S9 16 11.5 16s4.5-2 4.5-4.5S14 7 11.5 7zm2.5 5h-2v2h-1v-2H9v-1h2V9h1v2h2v1z"></path></symbol>
</svg>
<div id="toolboxCustomization" class="modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Toolbox Customization</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="panel panel-default">
                            <div class="panel-heading">Standard Elements</div>
                            <div class="panel-body">
                                <div id="standardItems"></div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="panel panel-default">
                            <div class="panel-heading">Custom Elements</div>
                            <div class="panel-body">
                                <div id="customItems"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <input type="button" class="btn btn-default" data-dismiss="modal" value="OK" onclick="applyToolboxItems()" style="width:100px" />
                <input type="button" class="btn btn-primary" data-dismiss="modal" value="Cancel" style="width:100px" />
            </div>
        </div>
    </div>
</div>
    <div id="surveyContainer">
        <div id="editorElement"></div>
    </div>

<script type="text/javascript" src="./index.js"></script>

</body>
</html>