git @ Cat's Eye Technologies Gemooy / master src / dam / dam-editable.js
master

Tree @master (Download .tar.gz)

dam-editable.js @masterraw · history · blame

// SPDX-FileCopyrightText: Chris Pressey, the creator of this work, has dedicated it to the public domain.
// For more information, please refer to <https://unlicense.org/>
// SPDX-License-Identifier: Unlicense

(function() {
    function makeEditable(config) {
        var button = DAM.maker('button');
        var div = DAM.maker('div');

        var editor = config.editor;
        var display = config.display;
        var onUpdate = config.onUpdate;

        var isEditing = false;

        function toggleView() {
            isEditing = !isEditing;

            if (isEditing) {
                display.style.display = 'none';
                editor.style.display = 'inline-block';
                toggleButton.textContent = 'Done';
            } else {
                editor.style.display = 'none';
                display.style.display = 'inline-block';
                toggleButton.textContent = 'Edit';
                if (typeof onUpdate === "function") {
                    onUpdate(editor.value);
                }
            }
        }

        var toggleButton = button(
            {
                onclick: toggleView,
                class: 'dam-editor-view-toggle'
            },
            'Edit'
        );

        editor.style.display = 'none';
        display.style.display = 'inline-block';

        return div(
            { class: 'dam-editor-view-widget' },
            toggleButton
        );
    }

    if (typeof module !== 'undefined') {
        module.exports = { makeEditable: makeEditable };
    } else if (typeof window !== 'undefined') {
        window.DAM = window.DAM || {};
        DAM.makeEditable = makeEditable;
    }
})();