UNPKG

@progress/kendo-angular-schematics

Version:

Kendo UI Schematics for Angular

243 lines 13.8 kB
/**----------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the project root for more information *-------------------------------------------------------------------------------------------*/ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const tslib_1 = require("tslib"); const schematics_1 = require("@angular-devkit/schematics"); const third_party_1 = require("../third_party"); const schema_1 = require("./schema"); const financial_1 = tslib_1.__importDefault(require("../third_party/d3fc/financial")); const skipWeekends_1 = tslib_1.__importDefault(require("../third_party/d3fc/filter/skipWeekends")); const kendo_utils_1 = require("../utils/kendo-utils"); // The code for the schematic is forked from: // https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/component/index.ts // We have some added code that we will have to merge. function formatMonth(month) { return month.toLocaleString('en-US', { minimumIntegerDigits: 2, useGrouping: false }); } function templateOptions(options) { if (options.chartType == "pie" || options.chartType == "donut") { options.time = false; if (!options.value) options.value = "value"; options.series = [third_party_1.strings.camelize(options.value)]; if (!options.category) options.category = "season"; const catTitlePrefix = third_party_1.strings.capitalize(options.category) || ""; if (!options.time && !options.categories?.length) options.categories = options.category == "season" ? ["spring", "summer", "fall", "winter"] : [`${catTitlePrefix}1`, `${catTitlePrefix}2`, `${catTitlePrefix}3`, `${catTitlePrefix}4`]; } else if (options.chartType == "scatter") { // nothing to do here } else if (options.chartType == "stock") { // options.time = true; // if (!options.category) options.category = "date"; if (!options.time) options.time = true; if (!options.value) options.value = "value"; if (!options.series?.length) options.series = ["lemons", "apples", "mangos"]; if (!options.category) options.category = options.time ? "date" : "season"; const now = new Date(); const navigatorFrom = new Date(now.getFullYear(), now.getMonth() - 11); const navigatorTo = new Date(now.getFullYear(), now.getMonth() + 0); options.navigatorFrom = `new Date("${navigatorFrom.getFullYear()}-${formatMonth(navigatorFrom.getMonth() + 1)}-01T00:00:00.000Z")`; options.navigatorTo = `new Date("${navigatorTo.getFullYear()}-${formatMonth(navigatorTo.getMonth() + 1)}-01T00:00:00.000Z")`; } else { if (!options.category) options.category = options.time ? "date" : "season"; if (!options.value) options.value = "value"; if (!options.series?.length) options.series = ["lemons", "apples", "mangos"]; options.navigator = false; const catTitlePrefix = third_party_1.strings.capitalize(options.category) || ""; if (!options.time && !options.categories?.length) options.categories = options.category == "season" ? ["spring", "summer", "fall", "winter"] : [`${catTitlePrefix}1`, `${catTitlePrefix}2`, `${catTitlePrefix}3`, `${catTitlePrefix}4`]; } } function createDataSource(options) { return async (tree, _context) => { try { let dataSourceCode; if (options.chartType == "scatter") { dataSourceCode = `export const sampleData = ${JSON.stringify([ { windSpeed: 16.4, rainfall: 5.4 }, { windSpeed: 21.7, rainfall: 2 }, { windSpeed: 25.4, rainfall: 3 }, { windSpeed: 19, rainfall: 2 }, { windSpeed: 10.9, rainfall: 1 }, { windSpeed: 13.6, rainfall: 3.2 }, { windSpeed: 10.9, rainfall: 7.4 }, { windSpeed: 10.9, rainfall: 0 }, { windSpeed: 10.9, rainfall: 8.2 }, { windSpeed: 16.4, rainfall: 0 }, { windSpeed: 16.4, rainfall: 1.8 }, { windSpeed: 13.6, rainfall: 0.3 }, { windSpeed: 29.9, rainfall: 0 }, { windSpeed: 27.1, rainfall: 2.3 }, { windSpeed: 16.4, rainfall: 0 }, ], null, " ")};\nexport default sampleData;`; } else if (options.chartType == "stock") { dataSourceCode = `export const sampleData = [`; const now = new Date(); const seriesFrom = new Date(now.getFullYear(), now.getMonth() - 11); const seriesTo = new Date(now.getFullYear(), now.getMonth() + 0); const stockSomewhatSimilarStartingPoint = 30 + Math.random() * 10; const randomPerSeriesData = options.series.map(series => (0, financial_1.default)() .startDate(seriesFrom) .startPrice(stockSomewhatSimilarStartingPoint * (0.95 + Math.random() * 0.1)) .filter(skipWeekends_1.default) .stream() .until(d => d.date > seriesTo) .map(({ date, open, high, low, close, volume }) => ({ [third_party_1.strings.camelize(options.category)]: `/Date(${date.getTime()})/`, [`${series}Open`]: open, [`${series}High`]: high, [`${series}Low`]: low, [`${series}Close`]: close, [`${series}Volume`]: volume, }))); let l = randomPerSeriesData.reduce((prev, current) => Math.min(prev, current.length), Number.MAX_SAFE_INTEGER); if (l == Number.MAX_SAFE_INTEGER) l = 0; const sampleData = []; for (let i = 0; i < l; i++) { let dataPoint = {}; for (let s = 0; s < randomPerSeriesData.length; s++) { dataPoint = { ...randomPerSeriesData[s][i], ...dataPoint }; } sampleData.push(dataPoint); } dataSourceCode = `export const sampleData = ${JSON.stringify(sampleData, null, " ")};\nexport default sampleData;`; } else { dataSourceCode = `export const sampleData = [`; if (options.time) { const now = new Date(); for (let i = -11; i <= 0; i++) { dataSourceCode += i == -11 ? `{\n` : ` {\n`; const seriesDate = new Date(now.getFullYear(), now.getMonth() + i); dataSourceCode += ` ${third_party_1.strings.camelize(options.category)}: new Date("${seriesDate.getFullYear()}-${formatMonth(seriesDate.getMonth() + 1)}-01T00:00:00.000Z"),\n`; for (let s = 0; s < options.series.length; s++) { if ((0, schema_1.isCategorical)(options)) { const value = Math.random() * 7 + s * 3 + (12 + i); dataSourceCode += ` ${third_party_1.strings.camelize(options.series[s])}: ${value.toFixed(2)},\n`; } else if ((0, schema_1.isCategoricalRange)(options)) { const minValue = Math.random() * 7 + s * 3 + (12 + i); const maxValue = minValue + 2 + Math.random() * 2; dataSourceCode += ` ${third_party_1.strings.camelize(options.series[s])}Min: ${minValue.toFixed(2)},\n`; dataSourceCode += ` ${third_party_1.strings.camelize(options.series[s])}Max: ${maxValue.toFixed(2)},\n`; } } dataSourceCode += i == 0 ? ` }` : ` },`; } } else { const categoryFieldName = third_party_1.strings.camelize(options.category); for (let i = 0; i < options.categories.length; i++) { const category = options.categories[i]; dataSourceCode += i == 0 ? `{\n` : ` {\n`; const categoryValue = JSON.stringify(third_party_1.strings.capitalize(category)); dataSourceCode += ` ${categoryFieldName}: ${categoryValue},\n`; for (let s = 0; s < options.series.length; s++) { if ((0, schema_1.isCategorical)(options) || options.chartType == "pie" || options.chartType == "donut") { const value = Math.random() * 7 + s * 3 + i * 2; dataSourceCode += ` ${third_party_1.strings.camelize(options.series[s])}: ${value.toFixed(2)},\n`; } else if ((0, schema_1.isCategoricalRange)(options)) { const minValue = Math.random() * 7 + s * 3 + i * 2; const maxValue = minValue + 2 + Math.random() * 2; dataSourceCode += ` ${third_party_1.strings.camelize(options.series[s])}Min: ${minValue.toFixed(2)},\n`; dataSourceCode += ` ${third_party_1.strings.camelize(options.series[s])}Max: ${maxValue.toFixed(2)},\n`; } } dataSourceCode += i == (options.categories.length - 1) ? ` }` : ` },`; } } dataSourceCode += `\n];\n`; dataSourceCode += `export default sampleData;`; } tree.create(`${options.path}/${third_party_1.strings.dasherize(options.name)}/${third_party_1.strings.dasherize(options.name)}.sample-data.ts`, dataSourceCode); } catch (e) { console.error(e); } }; } function default_1(options) { return async (host, context) => { const workspace = await (0, third_party_1.getWorkspace)(host); const project = workspace.projects.get(options.project); (0, kendo_utils_1.computeDefaultStandalone)(options, host, project); if (!project) { throw new schematics_1.SchematicsException(`Project "${options.project}" does not exist.`); } if (options.path === undefined) { options.path = (0, third_party_1.buildDefaultPath)(project); } options.module = (0, third_party_1.findModuleFromOptions)(host, options); const parsedPath = (0, third_party_1.parseName)(options.path, options.name); options.name = parsedPath.name; options.path = parsedPath.path; options.selector = options.selector || (0, third_party_1.buildSelector)(options, (project && project.prefix) || ''); (0, third_party_1.validateHtmlSelector)(options.selector); const skipStyleFile = options.inlineStyle || options.style === third_party_1.Style.None; // Kendo specific template options templateOptions(options); const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [ // Kendo specific, filters all .include files used as partials in templates. (0, schematics_1.filter)(path => !path.endsWith('.include')), options.skipTests ? (0, schematics_1.filter)((path) => !path.endsWith('.spec.ts.template')) : (0, schematics_1.noop)(), skipStyleFile ? (0, schematics_1.filter)((path) => !path.endsWith('.__style__.template')) : (0, schematics_1.noop)(), options.inlineTemplate ? (0, schematics_1.filter)((path) => !path.endsWith('.html.template')) : (0, schematics_1.noop)(), (0, schematics_1.applyTemplates)({ ...third_party_1.strings, 'if-flat': (s) => (options.flat ? '' : s), ...options, // Kendo specific, allows the component content to be included in inline and html files. include(path) { return (0, schematics_1.applyContentTemplate)(this)((0, schematics_1.url)("./files")(context).get(path)).content.toString(); }, }), !options.type ? (0, schematics_1.forEach)(((file) => { return file.path.includes('..') ? { content: file.content, path: file.path.replace('..', '.'), } : file; })) : (0, schematics_1.noop)(), (0, schematics_1.move)(parsedPath.path), ]); return (0, schematics_1.chain)([ (0, third_party_1.addDeclarationToNgModule)({ type: 'component', ...options, }), (0, kendo_utils_1.checkOrAddKendoTheme)(options, project), (0, kendo_utils_1.ngAddLocalize)(options, project), (0, kendo_utils_1.npmi)("@progress/kendo-angular-charts"), (0, schematics_1.mergeWith)(templateSource), // Kendo template specific, will add a sample data source file createDataSource(options), ]); }; } //# sourceMappingURL=index.js.map