Online IDE DDS Create

shortcuts to create DDS library component HTML

目前為 2022-11-22 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/454586/1120010/Online%20IDE%20DDS%20Create.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

const create = {
    actionMenu: (options = {}) => {
        const classAm = options.class || ``;
        const trigger = options.trigger || {};
        if (!options.trigger) {
            options.trigger = {};
        }
        if (!options.trigger.class) {
            options.trigger.class = `dds__button--secondary`;
        }
        if (!options.trigger.text) {
            options.trigger.text = `Actions`;
        }
        const id = options.id || `actionMenu${pen.utils.random()}`;
        const useChevron = options.useChevron || false;

        const noo = {};
        noo.actionMenu = pen.utils.createElement(`div`, {
            id: id,
            class: `dds__action-menu ${classAm}`,
            'data-trigger': `#${id}--trigger`,
            'data-dds': `action-menu`,
        });
        noo.trigger = pen.utils.createElement(`button`, {
            id: noo.actionMenu.getAttribute(`data-trigger`).replace(`#`, ''),
            type: `button`,
            class: `dds__button ${trigger.class}`,
        });
        noo.trigger.appendChild(pen.utils.createElement(`span`, {
            class: `ddsc__action-menu__trigger-label`,
            text: trigger.text
        }));
        if (useChevron) {
            const handleActionClick = (e) => {
                e.target.querySelector(`.dds__icon`).classList.toggle(`action-rotated`);
            }
            noo.chevron = pen.utils.createElement(`i`, {
                class: `dds__icon dds__icon--chevron-down action-chevron`,
            });
            noo.trigger.appendChild(noo.chevron);
            create.listener(`#${id}`, `ddsActionMenuOpenEvent`, handleActionClick);
            create.listener(`#${id}`, `ddsActionMenuCloseEvent`, handleActionClick);
        }
        noo.container = pen.utils.createElement(`div`, {
            class: `dds__action-menu__container`,
            tabindex: `-1`,
            role: `presentation`,
            'aria-hidden': `true`,
        });
        noo.menu = pen.utils.createElement(`div`, {
            class: `dds__action-menu__menu`,
            role: `menu`,
            tabindex: `-1`,
        });
        noo.menuLi = pen.utils.createElement(`li`, {
            role: `presentation`,
        });
        noo.group = pen.utils.createElement(`span`, {
            id: `${id}--group`
        });
        noo.groupUl = pen.utils.createElement(`ul`, {
            id: `${id}--groupUl`,
            class: `ddsc__action-menu--groupUl`,
            role: `group`,
            'aria-labelledby': noo.group.getAttribute(`id`)
        });
        noo.actionMenu.appendChild(noo.trigger);
        noo.actionMenu.appendChild(noo.container);
        noo.container.appendChild(noo.menu);
        noo.menu.appendChild(noo.menuLi);
        noo.menuLi.appendChild(noo.group);
        noo.group.appendChild(noo.groupUl);
        // Adding a method to the element doesn't seem to be work
        // const observerDefs = [
        //     {
        //       selector: `#${id}`,
        //       callback: (elem) => {
        //         elem.addItem = (itemOptions) => {
        //             document.getElementById(`${id}--groupUl`).appendChild(create.actionMenuItem(itemOptions));
        //         };
        //       }
        //     }
        //   ];
        // createObserver(observerDefs);
        return noo.actionMenu;
    },
    actionMenuItem: (options = {}) => {
        const noo = {};
        const label = options.text || `Item Text`;
        const asOption = options[`data-value`] != null || false;
        const dataValue = options[`data-value`] || undefined;
        const id = `${label.replace(/[^0-9a-zA-Z]+/, ``)}_${pen.utils.random()}`;
        noo.item = pen.utils.createElement(`li`, {
            class: asOption ? `dds__action-menu__option` : `dds__action-menu__item`,
            role: `none`,
        });
        noo.itemButton = pen.utils.createElement(`button`, {
            id: id,
            type: `button`,
            role: asOption ? `menuitemcheckbox` : `menuitem`,
            tabindex: `-1`,
            'aria-disabled': `false`,
            'aria-checked': `false`,
            'data-value': dataValue,
        });
        noo.itemSvg = pen.utils.createElement(`svg`, {
            class: `dds__action-menu__icon`,
            'aria-hidden': `true`,
        });
        noo.itemSvgUse = pen.utils.createElement(`use`, {
            'xlink:href': `#dds__icon--copy-alt`,
        });
        noo.itemText = pen.utils.createElement(`span`, {
            text: label,
        });
        noo.item.appendChild(noo.itemButton);
        noo.itemButton.appendChild(noo.itemSvg);
        noo.itemButton.appendChild(noo.itemText);
        noo.itemSvg.appendChild(noo.itemSvgUse);
        if (options.onclick) {
            create.listener(`#${id}`, `click`, options.onclick);
        }
        return noo.item;
    },
    button: (options = {}) => {
        const id = options.id || `button_${pen.utils.random()}`;
        const bClass = options.class || ``;
        const bText = options.text || `Button`;
        const iconIsString = options.icon && typeof options.icon === `string`;
        const iconObjectName = !iconIsString && options.icon ? options.icon.name : undefined;
        const iconObjectClass = !iconIsString && options.icon && options.icon.class ? options.icon.class : ``;
        const bIcon = {
            name: iconIsString ? options.icon : iconObjectName,
            class: iconObjectClass
        };
        const noo = {};
        noo.button = pen.utils.createElement(`button`, {
            id: id,
            class: `dds__button ${bClass}`,
            type: `button`,
            text: bText,
        });
        if (bIcon.name) {
            noo.icon = pen.utils.createElement(`i`, {
                class: `dds__icon dds__icon--${bIcon.name} ${bIcon.class}`,
                'aria-hidden': `true`,
            });
            if (options.icon.class.indexOf(`--end`) > -1) {
                noo.button.appendChild(noo.icon);
            } else {
                noo.button.prepend(noo.icon);
            }
        }
        if (options.onclick) {
            create.listener(`#${id}`, `click`, options.onclick);
        }
        return noo.button;
    },
    listener: function (selector, event, handler) {
        let rootElement = document.querySelector('body');
        //since the root element is set to be body for our current dealings
        rootElement.addEventListener(event, function (evt) {
            var targetElement = evt.target;
            while (targetElement != null) {
                if (targetElement.matches(selector)) {
                    handler(evt);
                    return;
                }
                targetElement = targetElement.parentElement;
            }
        },
            true
        );
    },
    popover: (options = {}) => {
        const id = options.id || `popover_${pen.utils.random()}`;
        const dataTrigger = options['data-trigger'] || undefined;
        const dataPlacement = options['data-placement'] || `bottom-end`;
        const classPo = options.class || ``;
        const callback = options.callback || undefined;
        const arrow = options.arrow === undefined ? true : options.arrow;
        const close = options.close === undefined ? true : options.close;
        if (!dataTrigger) {
            console.error(`Send a data-trigger with a value like "#your-element-id"`);
            return;
        }
        if (!arrow) {
            // if (dataPlacement.indexOf(`bottom`) > -1) {
            //     pen.utils.addStyle(`#${id} { top: -12px;}`);
            // } else if (dataPlacement.indexOf(`top`) > -1) {
            //     pen.utils.addStyle(`#${id} { top: 12px;}`);
            // } else if (dataPlacement.indexOf(`left`) > -1) {
            //     pen.utils.addStyle(`#${id} { left: 12px;}`);
            // } else if (dataPlacement.indexOf(`right`) > -1) {
            //     pen.utils.addStyle(`#${id} { left: -12px;}`);
            // }
            pen.utils.addStyle(`#${id} .dds__popover__pointer { display: none !important;}`);
        }
        if (!close) {
            pen.utils.addStyle(`#${id} .dds__popover__close { display: none !important;}`);
        }
        if (!options.title) {
            pen.utils.addStyle(`#${id} .dds__popover__header { display: none !important;}`);
        }
        const nooPopover = pen.utils.createElement(`div`, {
            id: id,
            class: `dds__popover ${classPo}`,
            role: `dialog`,
            'aria-labelledby': `${id}--title`,
            'data-placement': dataPlacement,
            'data-dds': `popover`,
            'data-trigger': dataTrigger,
        });
        const nooContent = pen.utils.createElement(`div`, {
            class: `dds__popover__content`,
        });
        const nooHeader = pen.utils.createElement(`div`, {
            class: `dds__popover__header`,
        });
        const nooHeadline = pen.utils.createElement(`h6`, {
            id: `${id}--title`,
            class: `dds__popover__headline`,
            text: options.title,
        });
        const nooBody = pen.utils.createElement(`div`, {
            class: `dds__popover__body`,
            text: options.body,
        });
        if (callback) {
            callback(nooBody);
        }
        nooPopover.appendChild(nooContent);
        nooContent.appendChild(nooHeader);
        nooContent.appendChild(nooBody);
        nooHeader.appendChild(nooHeadline);
        return nooPopover;
    },
    radioButton: (options = {}) => {
        const id = options.id || `radiobutton_${pen.utils.random()}`;
        const componentClass = options.class || ``;
        const legend = options.legend || ``;
        const nooSet = pen.utils.createElement(`fieldset`, {
            class: `dds__fieldset dds__radio-button-group ${componentClass}`,
            role: `radiogroup`,
        });
        if (options.required) {
            nooSet.setAttribute(`required`, true);
            nooSet.setAttribute(`aria-required`, true);
        }
        const nooLegend = pen.utils.createElement(`legend`, {
            text: legend,
        })
        if (legend) nooSet.appendChild(nooLegend);
        options.buttons.forEach((radio, rIndex) => {
            const radioClass = radio.class || ``;
            const radioValue = radio.value || ``;
            const radioLabel = radio.label || ``;
            const nooButton = pen.utils.createElement(`div`, {
                class: `dds__radio-button ${radioClass}`,
            });
            const nooInput = pen.utils.createElement(`input`, {
                class: `dds__radio-button__input`,
                type: `radio`,
                name: `${id}--button-name`,
                id: `${id}--button${rIndex}`,
                value: radioValue,
            });
            const nooLabel = pen.utils.createElement(`label`, {
                class: `dds__radio-button__label`,
                id: `${id}--button-label${rIndex}`,
                for: `${id}--button${rIndex}`,
                text: radioLabel,
            });
            nooButton.appendChild(nooInput);
            nooButton.appendChild(nooLabel);
            nooSet.appendChild(nooButton);
        });
        const nooError = pen.utils.createElement(`div`, {
            id: `${id}--error`,
            class: `dds__invalid-feedback`,
            text: options.error || ``,
        });
        nooSet.appendChild(nooError);
        return nooSet;
    },
    textInput: (options = {}) => {
        const id = options.id || `textinput_${pen.utils.random()}`;
        const componentClass = options.class || ``;
        const nooTextInput = pen.utils.createElement(`div`, {
            id: id,
            class: `dds__text-input ${componentClass}`,            
        });
        const nooLabel = pen.utils.createElement(`label`, {
            label: `${id}--label`,
            for: `${id}--input`,
            text: options.label,
        });
        const nooWrapper = pen.utils.createElement(`div`, {
            class: `dds__input-text__wrapper`,
        });
        const nooInput = pen.utils.createElement(`input`, {
            type: `text`,
            class: `dds__input-text`,
            name: `${id}--input`,
            id: `${id}--input`,
            'aria-labelledby': `{$id}--input`,
        });
        if (options.required) {
            nooInput.setAttribute(`required`, true);
        }
        if (options[`max-length`]) {
            nooInput.setAttribute(`max-length`, options[`max-length`]);
        }
        const nooHelper = pen.utils.createElement(`small`, {
            id: `${id}--helper`,
            class: `dds__input-text__helper ${!options.helper ? `dds__d-none` : ``}`,
            text: options.helper,
        });
        const nooError = pen.utils.createElement(`div`, {
            id: `${id}--error`,
            class: `dds__invalid-feedback ${!options.error ? `dds__d-none` : ``}`,
            text: options.error,
        });
        if (!options.helper) {
            pen.utils.addStyle(`#${id} { margin-top: -12px; margin-bottom: 9px;}`);
        }
        nooTextInput.appendChild(nooLabel);
        nooTextInput.appendChild(nooWrapper);
        nooWrapper.appendChild(nooInput);
        nooWrapper.appendChild(nooHelper);
        nooWrapper.appendChild(nooError);
        return nooTextInput;
    },
}