@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
119 lines (118 loc) • 5.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateBadgedContent = void 0;
const elements_1 = require("../../../../utils/templates/elements");
const inputs_1 = require("../../../../utils/templates/inputs");
const template_resource_1 = require("../../../../utils/templates/template-resource");
const remove_attrs_1 = require("../utils/remove-attrs");
function migrateBadgedContent({ resource, recorder, fileSystem, }) {
const template = (0, template_resource_1.getTemplateFromTemplateResource)(resource, fileSystem);
const templateOffset = (0, template_resource_1.getTemplateOffset)(resource);
const elements = (0, elements_1.findElementsByTagName)(template, 'tui-badged-content');
elements.forEach(({ attrs, sourceCodeLocation }) => {
var _a, _b;
if (!sourceCodeLocation) {
return;
}
const colorBottomAttr = (0, inputs_1.findAttr)(attrs, 'colorbottom');
const colorTopAttr = (0, inputs_1.findAttr)(attrs, 'colortop');
const contentBottomAttr = (0, inputs_1.findAttr)(attrs, 'contentbottom');
const contentTopAttr = (0, inputs_1.findAttr)(attrs, 'contenttop');
const sizeAttr = (0, inputs_1.findAttr)(attrs, 'size');
const rounded = (0, inputs_1.findAttr)(attrs, 'rounded');
const size = (sizeAttr === null || sizeAttr === void 0 ? void 0 : sizeAttr.value) || 'm';
if (!contentBottomAttr) {
migrateColor({
attr: colorBottomAttr,
sourceCodeLocation,
recorder,
templateOffset,
slot: 'bottom',
});
}
if (!contentTopAttr) {
migrateColor({
attr: colorTopAttr,
sourceCodeLocation,
recorder,
templateOffset,
slot: 'top',
});
}
migrateContent({
attr: contentTopAttr,
colorAttr: colorTopAttr,
sourceCodeLocation,
recorder,
templateOffset,
slot: 'top',
size,
});
migrateContent({
attr: contentBottomAttr,
colorAttr: colorBottomAttr,
sourceCodeLocation,
recorder,
templateOffset,
slot: 'bottom',
size,
});
if (!rounded || rounded.value === 'true') {
const insertTo = (_b = (_a = sourceCodeLocation.startTag) === null || _a === void 0 ? void 0 : _a.endOffset) !== null && _b !== void 0 ? _b : 0;
recorder.insertRight(insertTo + templateOffset - 1, ' [style.--tui-radius.%]="50"');
}
const attrsToRemove = [
colorBottomAttr,
colorTopAttr,
contentTopAttr,
contentBottomAttr,
sizeAttr,
rounded,
].filter((attr) => attr !== undefined);
(0, remove_attrs_1.removeAttrs)(attrsToRemove, sourceCodeLocation, recorder, templateOffset);
});
}
exports.migrateBadgedContent = migrateBadgedContent;
function migrateColor({ attr, sourceCodeLocation, recorder, templateOffset, slot, }) {
var _a, _b;
if (!attr) {
return;
}
const { value } = attr;
const insertTo = (_b = (_a = sourceCodeLocation.startTag) === null || _a === void 0 ? void 0 : _a.endOffset) !== null && _b !== void 0 ? _b : 0;
const hasBinding = Boolean(attr) && (0, inputs_1.isBinding)(attr);
recorder.insertRight(insertTo + templateOffset + 1, `<tui-badge-notification
size="xs"
tuiSlot="${slot}"
[style.color]="${hasBinding ? value : `'${value}'`}"
></tui-badge-notification>\n`);
}
function migrateContent({ attr, colorAttr, sourceCodeLocation, recorder, templateOffset, slot, size, }) {
var _a, _b, _c, _d;
if (!attr) {
return;
}
const { value } = attr;
const colorAttrValue = colorAttr === null || colorAttr === void 0 ? void 0 : colorAttr.value;
const insertTo = (_b = (_a = sourceCodeLocation.startTag) === null || _a === void 0 ? void 0 : _a.endOffset) !== null && _b !== void 0 ? _b : 0;
if (value.startsWith('tuiIcon')) {
recorder.insertRight(insertTo + templateOffset, `<tui-icon
appearance="accent"
icon="${value}"
size="${size}"
tuiBadge
tuiSlot="${slot}"
${colorAttr ? `[style.color]="'${colorAttrValue}'"` : 'appearance="success"'}
></tui-icon>\n`);
}
else if (Number.isNaN(Number(value))) {
recorder.insertRight(templateOffset + ((_d = (_c = sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.startTag) === null || _c === void 0 ? void 0 : _c.startOffset) !== null && _d !== void 0 ? _d : 0), '<!-- TODO: (Taiga UI migration) contentTop and contentBottom inputs has been removed. Use ng-content, see taiga-ui.dev/components/badged-content -->\n');
}
else {
recorder.insertRight(insertTo + templateOffset, `<tui-badge-notification
size="${size}"
tuiSlot="${slot}"
${colorAttr ? `[style.color]="'${colorAttrValue}'"` : ''}
></tui-badge-notification>\n`);
}
}