@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
79 lines • 4.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateBadge = 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 common_1 = require("./toggles/common");
const badgeSizeMap = {
xs: 's',
s: 'm',
m: 'l',
l: 'xl',
};
function migrateBadge({ 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-badge');
elements.forEach(({ attrs, sourceCodeLocation, childNodes }) => {
if (!sourceCodeLocation) {
return;
}
(0, common_1.replaceSizeAttr)(attrs, sourceCodeLocation, recorder, templateOffset, badgeSizeMap);
const valueAttr = (0, inputs_1.findAttr)(attrs, 'value');
// migration for icon-only badges
if (!valueAttr) {
addTodo(recorder, sourceCodeLocation, templateOffset);
return;
}
const svg = childNodes === null || childNodes === void 0 ? void 0 : childNodes.find((node) => node.nodeName === 'tui-svg');
if (svg) {
migrateIcon({ svg, sourceCodeLocation, recorder, templateOffset });
}
migrateBadgeValue({
valueAttr,
sourceCodeLocation,
recorder,
templateOffset,
});
});
}
exports.migrateBadge = migrateBadge;
function migrateIcon({ svg, sourceCodeLocation, recorder, templateOffset, }) {
var _a, _b, _c, _d;
const src = (0, inputs_1.findAttr)(svg.attrs, 'src');
const srcValue = src === null || src === void 0 ? void 0 : src.value;
if (!srcValue) {
return;
}
const insertTo = templateOffset + (((_a = sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.startTag) === null || _a === void 0 ? void 0 : _a.endOffset) || 0) - 1;
recorder.insertRight(insertTo, `${(src === null || src === void 0 ? void 0 : src.name) === 'src' ? 'iconStart' : '[iconStart]'}="${srcValue}"`);
recorder.remove(((_b = svg.sourceCodeLocation) === null || _b === void 0 ? void 0 : _b.startOffset) || 0, (((_c = svg.sourceCodeLocation) === null || _c === void 0 ? void 0 : _c.endOffset) || 0) -
(((_d = svg.sourceCodeLocation) === null || _d === void 0 ? void 0 : _d.startOffset) || 0));
}
function migrateBadgeValue({ valueAttr, sourceCodeLocation, recorder, templateOffset, }) {
var _a, _b, _c, _d, _e;
const attrValue = valueAttr === null || valueAttr === void 0 ? void 0 : valueAttr.value;
const insertTo = (_b = (_a = sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.startTag) === null || _a === void 0 ? void 0 : _a.endOffset) !== null && _b !== void 0 ? _b : 0;
const selfClosing = !(sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.endTag);
if (!attrValue || !insertTo) {
return;
}
const closeTag = selfClosing ? '</tui-badge>' : '';
recorder.insertRight(insertTo + templateOffset, valueAttr.name === 'value'
? `${attrValue}${closeTag}`
: `{{ ${attrValue} }}${closeTag}`);
const attrOffset = (_c = sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.attrs) === null || _c === void 0 ? void 0 : _c[valueAttr.name];
if (attrOffset) {
const { startOffset, endOffset } = attrOffset;
recorder.remove(templateOffset + startOffset - 1, endOffset - startOffset + 1);
}
if (selfClosing) {
recorder.remove(((_e = (_d = sourceCodeLocation.startTag) === null || _d === void 0 ? void 0 : _d.endOffset) !== null && _e !== void 0 ? _e : 2) - 2, 1);
}
}
function addTodo(recorder, sourceCodeLocation, templateOffset) {
var _a, _b;
recorder.insertRight(templateOffset + ((_b = (_a = sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.startTag) === null || _a === void 0 ? void 0 : _a.startOffset) !== null && _b !== void 0 ? _b : 0), '<!-- TODO: (Taiga UI migration) use "<tui-icon>" with "tuiBadge" directive for icon-only badges instead -->\n');
}
//# sourceMappingURL=migrate-badge.js.map