@botonic/react
Version:
Build Chatbots using React
88 lines • 3.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ButtonsDisabler = void 0;
const button_1 = require("../components/button");
const constants_1 = require("../constants");
const message_utils_1 = require("../message-utils");
const objects_1 = require("../util/objects");
const react_1 = require("../util/react");
const webchat_1 = require("../util/webchat");
class ButtonsDisabler {
static constructBrowserProps(props) {
const disabledProps = { disabled: props.disabled };
if (props.autodisable !== undefined)
disabledProps.autodisable = (0, objects_1.strToBool)(props.autodisable);
if (props.disabledstyle !== undefined)
disabledProps.disabledstyle = props.disabledstyle;
return disabledProps;
}
static constructNodeProps(props) {
const disabledProps = {};
if (props.autodisable !== undefined)
disabledProps.autodisable = String(props.autodisable);
if (props.disabledstyle !== undefined)
disabledProps.disabledstyle = JSON.stringify(props.disabledstyle);
return disabledProps;
}
static withDisabledProps(props) {
return {
disabled: props.disabled,
autodisable: props.autodisable,
disabledstyle: props.disabledstyle,
};
}
static resolveDisabling(theme, props) {
const getThemeProperty = (0, webchat_1._getThemeProperty)(theme);
const autoDisable = props.autodisable !== undefined
? props.autodisable
: getThemeProperty(constants_1.WEBCHAT.CUSTOM_PROPERTIES.buttonAutoDisable, constants_1.WEBCHAT.DEFAULTS.BUTTON_AUTO_DISABLE);
const computedDisabledStyle = props.disabledstyle !== undefined
? props.disabledstyle
: getThemeProperty(constants_1.WEBCHAT.CUSTOM_PROPERTIES.buttonDisabledStyle, {});
const disabledStyle = Object.assign(Object.assign({}, constants_1.WEBCHAT.DEFAULTS.BUTTON_DISABLED_STYLE), computedDisabledStyle);
return { autoDisable, disabledStyle };
}
static updateChildrenButtons(children, additionalProps = undefined) {
return (0, react_1.deepMapWithIndex)(children, n => {
if (n.type === button_1.Button)
return this.updateButtons(n, additionalProps);
return n;
});
}
static updateButtons(node, additionalProps) {
if (!additionalProps)
additionalProps = {};
else {
additionalProps = {
disabled: node.props.disabled === true
? node.props.disabled
: additionalProps.disabled,
setDisabled: additionalProps.setDisabled,
parentId: additionalProps.parentId,
};
}
return Object.assign(Object.assign({}, node), { props: Object.assign(Object.assign({}, node.props), additionalProps) });
}
static getUpdatedMessage(messageToUpdate, { autoDisable, disabledStyle }) {
const updateMsgButton = button => {
return Object.assign(Object.assign({}, button), {
disabled: true,
});
};
if ((0, message_utils_1.isCarousel)(messageToUpdate) &&
messageToUpdate.data &&
messageToUpdate.data.elements) {
messageToUpdate.data.elements = messageToUpdate.data.elements.map(e => (Object.assign(Object.assign({}, e), {
buttons: e.buttons.map(updateMsgButton),
})));
return messageToUpdate;
}
else {
return Object.assign(Object.assign({}, messageToUpdate), {
buttons: messageToUpdate.buttons.map(updateMsgButton),
});
}
}
}
exports.ButtonsDisabler = ButtonsDisabler;
//# sourceMappingURL=buttons-disabler.js.map