UNPKG

igniteui-angular

Version:

Ignite UI for Angular is a dependency-free Angular toolkit for building modern web apps

126 lines (125 loc) • 7.17 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const UpdateChanges_1 = require("../common/UpdateChanges"); const util_1 = require("../common/util"); // use bare specifier to escape the schematics encapsulation for the dynamic import: const import_helper_js_1 = require("igniteui-angular/migrations/common/import-helper.js"); const version = '12.1.0'; exports.default = () => (host, context) => __awaiter(void 0, void 0, void 0, function* () { var _a; context.logger.info(`Applying migration for Ignite UI for Angular to version ${version}`); const { HtmlParser, getHtmlTagDefinition } = yield (0, import_helper_js_1.nativeImport)('@angular/compiler'); const update = new UpdateChanges_1.UpdateChanges(__dirname, host, context); const TAGS = ['igx-grid', 'igx-tree-grid', 'igx-hierarchical-grid']; const prop = ['[paging]', 'paging']; const changes = new Map(); const warnMsg = `\n<!-- Auto migrated template content. Please, check your bindings! -->\n`; const deprecatedToken = 'IgxGridTransaction'; const providerWarnMsg = `/* Injection token 'IgxGridTransaction' has been deprecated. ` + `Please refer to the update guide for more details. */`; const templateNames = []; const applyChanges = () => { for (const [path, change] of changes.entries()) { let buffer = host.read(path).toString(); change.sort((c, c1) => c.position - c1.position) .reverse() .forEach(c => buffer = c.apply(buffer)); host.overwrite(path, buffer); } }; const addChange = (path, change) => { if (changes.has(path)) { changes.get(path).push(change); } else { changes.set(path, [change]); } }; const checkForPaginatorInTemplate = (path, name) => { const ngTemplates = (0, util_1.findElementNodes)((0, util_1.parseFile)(new HtmlParser(), host, path), 'ng-template'); const paginatorTemplate = ngTemplates.filter(template => (0, util_1.hasAttribute)(template, `#${name}`))[0]; return paginatorTemplate ? !!(0, util_1.findElementNodes)(paginatorTemplate.children, 'igx-paginator').length : false; }; const moveTemplate = (paginatorTemplate) => { if (paginatorTemplate) { return `${warnMsg}\n<igx-paginator-content> ${(0, util_1.serializeNodes)(paginatorTemplate.children, getHtmlTagDefinition).join('')} </igx-paginator-content>\n`; } return ''; }; const buildPaginator = (node, path, propName, value, isChildGrid = false) => { const paginationTemplateName = (0, util_1.getAttribute)(node, '[paginationTemplate]')[0]; const ngTemplates = (0, util_1.findElementNodes)((0, util_1.parseFile)(new HtmlParser(), host, path), 'ng-template'); templateNames.push('#' + (paginationTemplateName === null || paginationTemplateName === void 0 ? void 0 : paginationTemplateName.value)); const paginatorTemplate = ngTemplates.filter(template => (0, util_1.hasAttribute)(template, `#${paginationTemplateName === null || paginationTemplateName === void 0 ? void 0 : paginationTemplateName.value}`))[0]; if (paginatorTemplate && checkForPaginatorInTemplate(path, paginationTemplateName === null || paginationTemplateName === void 0 ? void 0 : paginationTemplateName.value)) { const pgCmpt = (0, util_1.findElementNodes)(paginatorTemplate.children, 'igx-paginator')[0]; return `\n<igx-paginator${isChildGrid ? ' *igxPaginator' : ''}${(0, util_1.stringifyAttributes)(pgCmpt.attrs)}></igx-paginator>`; } else { // eslint-disable-next-line max-len return `\n<igx-paginator${isChildGrid ? ' *igxPaginator' : ''}${(0, util_1.makeNgIf)(propName, value) ? ` *ngIf="${value}"` : ''}>${moveTemplate(paginatorTemplate)}</igx-paginator>`; } }; // migrate paging and pagination template for grid, tree grid and hierarchical grid for (const path of update.templateFiles) { (0, util_1.findElementNodes)((0, util_1.parseFile)(new HtmlParser(), host, path), TAGS) .filter(grid => (0, util_1.hasAttribute)(grid, prop)) .map(node => (0, util_1.getSourceOffset)(node)) .forEach(offset => { const { startTag, file, node } = offset; const { name, value } = (0, util_1.getAttribute)(node, prop)[0]; const text = buildPaginator(node, path, name, value); addChange(file.url, new util_1.FileChange(startTag.end, text)); }); } applyChanges(); changes.clear(); // apply the migrations to the rowIsland for (const path of update.templateFiles) { (0, util_1.findElementNodes)((0, util_1.parseFile)(new HtmlParser(), host, path), 'igx-row-island') .filter(island => (0, util_1.hasAttribute)(island, prop)) .map(island => (0, util_1.getSourceOffset)(island)) .forEach(offset => { const { startTag, file, node } = offset; const { name, value } = (0, util_1.getAttribute)(node, prop)[0]; const text = buildPaginator(node, path, name, value, true); addChange(file.url, new util_1.FileChange(startTag.end, text)); }); } applyChanges(); changes.clear(); // clear paginationTemplate definitions for (const path of update.templateFiles) { (0, util_1.findElementNodes)((0, util_1.parseFile)(new HtmlParser(), host, path), 'ng-template') .filter(template => (0, util_1.hasAttribute)(template, templateNames)) .forEach(node => { const { startTag, endTag, file } = (0, util_1.getSourceOffset)(node); const replaceText = file.content.substring(startTag.start, endTag.end); addChange(file.url, new util_1.FileChange(startTag.start, '', replaceText, 'replace')); }); } applyChanges(); changes.clear(); const matchStr = String.raw `({\s*provide:\s*${deprecatedToken}[^}]*},?)`; const matchExpr = new RegExp(matchStr, 'g'); for (const path of update.tsFiles) { let content = (_a = host.read(path)) === null || _a === void 0 ? void 0 : _a.toString(); if (content.indexOf(deprecatedToken) < 0) { continue; } content = content.replace(matchExpr, `${providerWarnMsg}\n$1`); host.overwrite(path, content); } update.applyChanges(); });