@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
65 lines • 4.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateNotification = void 0;
const ng_morph_1 = require("ng-morph");
const add_import_to_closest_module_1 = require("../../../../utils/add-import-to-closest-module");
const elements_1 = require("../../../../utils/templates/elements");
const inputs_1 = require("../../../../utils/templates/inputs");
const template_resource_1 = require("../../../../utils/templates/template-resource");
function migrateNotification({ 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.findElementsByTagNames)(template, ['tui-notification']),
...(0, elements_1.findElementsWithAttributeOnTag)(template, ['tuiNotification'], ['button', 'a']),
];
elements.forEach(({ attrs, sourceCodeLocation, tagName, childNodes }) => {
var _a, _b, _c, _d;
const sizeAttr = (0, inputs_1.findAttr)(attrs, 'size');
const hasIconAttr = (0, inputs_1.findAttr)(attrs, 'hasIcon');
const hideCloseAttr = (0, inputs_1.findAttr)(attrs, 'hideClose');
const closeListener = (0, inputs_1.findAttr)(attrs, '(close)');
const { startTag, endTag } = sourceCodeLocation || {};
const hideCloseAttrLocation = (_a = sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.attrs) === null || _a === void 0 ? void 0 : _a[(hideCloseAttr === null || hideCloseAttr === void 0 ? void 0 : hideCloseAttr.name) || ''];
const hasIconAttrLocation = (_b = sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.attrs) === null || _b === void 0 ? void 0 : _b[(hasIconAttr === null || hasIconAttr === void 0 ? void 0 : hasIconAttr.name) || ''];
if (!sizeAttr) {
recorder.insertRight(templateOffset + ((_c = startTag === null || startTag === void 0 ? void 0 : startTag.startOffset) !== null && _c !== void 0 ? _c : 0) + `<${tagName}`.length, ' size="m"');
}
if (closeListener) {
const hasProprietaryPackages = !!(0, ng_morph_1.getPackageJsonDependency)(fileSystem.tree, '@taiga-ui/proprietary-core');
(0, add_import_to_closest_module_1.addImportToClosestModule)(resource.componentPath, 'NgIf', '@angular/common');
const ifCondition = hideCloseAttr ? ` *ngIf="!${hideCloseAttr.value}"` : '';
const closeIconName = hasProprietaryPackages
? '@tui.pragmatic.small.cross'
: '@tui.x';
const closeButtonTemplate = ` <button${ifCondition} tuiIconButton iconStart="${closeIconName}"></button>`;
recorder.insertRight(templateOffset + ((_d = endTag === null || endTag === void 0 ? void 0 : endTag.startOffset) !== null && _d !== void 0 ? _d : 0), closeButtonTemplate);
if (hideCloseAttrLocation) {
const { startOffset, endOffset } = hideCloseAttrLocation;
recorder.remove(templateOffset + startOffset, endOffset - startOffset);
}
}
if (hasIconAttr && hasIconAttrLocation) {
const { startOffset, endOffset } = hasIconAttrLocation;
const attrOffset = templateOffset + startOffset;
const attrLength = endOffset - startOffset;
switch (hasIconAttr.value) {
case 'false':
recorder.remove(attrOffset, attrLength);
recorder.insertRight(attrOffset, 'icon=""');
break;
case 'true':
recorder.remove(attrOffset, attrLength);
break;
default:
recorder.insertLeft(templateOffset + ((startTag === null || startTag === void 0 ? void 0 : startTag.startOffset) || 0), '<!-- TODO: (Taiga UI migration) "hasIcon" is deleted. Use icon="" to hide icon. Or pass TUI_NOTIFICATION_DEFAULT_OPTIONS["icon"] to show it again. Learn more: https://taiga-ui.dev/components/notification -->\n');
}
}
if (childNodes.length > 1 && startTag && endTag) {
recorder.insertRight(templateOffset + startTag.endOffset, '<div>');
recorder.insertLeft(templateOffset + endTag.startOffset, '</div>');
}
});
}
exports.migrateNotification = migrateNotification;
//# sourceMappingURL=migrate-notification.js.map