@netgrif/components
Version:
Netgrif Application Engine frontend Angular components
154 lines • 8.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTabView = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const utility_functions_1 = require("../../../../_utility/utility-functions");
const core_1 = require("@angular-devkit/core");
const view_utility_functions_1 = require("../../../_utility/view-utility-functions");
const view_service_functions_1 = require("../../../_utility/view-service-functions");
const tab_content_template_1 = require("../../models/tab-content-template");
const import_to_add_1 = require("../../../../_commons/import-to-add");
const view_class_info_1 = require("../../../../_commons/view-class-info");
function createTabView(tree, args, addViewToService, createViewFunctionRef) {
const projectInfo = (0, utility_functions_1.getProjectInfo)(tree);
const view = new view_class_info_1.ViewClassInfo(args.path, 'tabView', args.componentName);
const params = args.layoutParams;
const tabViews = newTabViews();
let viewCounterStart = 0;
if (!!params.defaultTaskView) {
processEmbeddedView(params.defaultTaskView, tabViews, view, args.path, viewCounterStart, tree, createViewFunctionRef, true);
viewCounterStart++;
}
pushTabViews(tabViews, processTabViewContents(tree, params, args.path, view, createViewFunctionRef, viewCounterStart));
if (!!params.defaultTaskView) {
for (let i = 1; i < tabViews.tabTemplates.length; i++) {
if (tabViews.tabViewImports[i].className.endsWith('CaseViewComponent')) {
const injectedData = {};
injectedData.tabViewComponent = tabViews.tabViewImports[0].className;
injectedData.tabViewOrder = params.defaultTaskView.order ? params.defaultTaskView.order : 0;
tabViews.tabTemplates[i].injectedObject = injectedData;
}
}
// we don't want to generate the default tab view as a tab
tabViews.tabTemplates.splice(0, 1);
}
const rules = tabViews.rules;
rules.push((0, utility_functions_1.createFilesFromTemplates)('./views/tab-view/files', `${projectInfo.path}/views/${args.path}`, {
prefix: projectInfo.projectPrefixDasherized,
className: view.nameWithoutComponent,
tabs: tabViews.tabTemplates,
imports: tabViews.tabViewImports,
dasherize: core_1.strings.dasherize,
classify: core_1.strings.classify,
modulePath: (0, utility_functions_1.createRelativePath)(view.fileImportPath, './app.module'),
viewIdSegment: (0, view_utility_functions_1.getViewIdSegmentFromPath)(args.path)
}));
(0, view_utility_functions_1.updateAppModule)(tree, view.className, view.fileImportPath, [
new import_to_add_1.ImportToAdd('FlexModule', '@ngbracket/ngx-layout'),
new import_to_add_1.ImportToAdd('TabsComponentModule', '@netgrif/components')
]);
if (addViewToService) {
(0, view_service_functions_1.addViewToViewService)(tree, view);
}
return (0, schematics_1.chain)(rules);
}
exports.createTabView = createTabView;
function processTabViewContents(tree, tabViewParams, hostViewPath, hostClassName, createViewFunctionRef, viewCounterStartValue = 0) {
const result = newTabViews();
if (tabViewParams.tabs === undefined) {
return result;
}
let viewCounter = viewCounterStartValue;
tabViewParams.tabs.forEach(tab => {
processEmbeddedView(tab, result, hostClassName, hostViewPath, viewCounter, tree, createViewFunctionRef);
viewCounter++;
});
return result;
}
function processEmbeddedView(embeddedView, result, hostClassName, hostViewPath, viewNumber, tree, createViewFunctionRef, isDefaultTabbedTaskView = false) {
let tabTemplate;
if (embeddedView.component !== undefined) {
tabTemplate = processEmbeddedComponent(embeddedView, result, hostClassName);
}
else if (embeddedView.view !== undefined) {
tabTemplate = processEmbeddedNewView(embeddedView, result, hostClassName, `${hostViewPath}/content/${viewNumber}`, tree, createViewFunctionRef, isDefaultTabbedTaskView);
}
else {
throw new schematics_1.SchematicsException('TabView content must contain either a \'component\' or a \'view\' attribute');
}
if (embeddedView.canBeClosed !== undefined) {
tabTemplate.canBeDeleted = embeddedView.canBeClosed;
}
else {
tabTemplate.canBeDeleted = false;
}
if (embeddedView.label !== undefined) {
if (embeddedView.label.icon !== undefined) {
tabTemplate.icon = embeddedView.label.icon;
}
if (embeddedView.label.text !== undefined) {
tabTemplate.text = embeddedView.label.text;
}
}
if (embeddedView.order !== undefined) {
tabTemplate.order = embeddedView.order;
}
result.tabTemplates.push(tabTemplate);
}
function processEmbeddedComponent(embeddedComponent, result, hostClassName) {
if (!embeddedComponent.component) {
throw new schematics_1.SchematicsException('processEmbeddedComponent can\'t be called with EmbeddedView object' +
' that doesn\'t contain the \'component\' attribute!');
}
if (embeddedComponent.component.class === undefined || embeddedComponent.component.classPath === undefined) {
throw new schematics_1.SchematicsException('TabView content Component must define both a \'class\' and a \'classPath\' attribute');
}
if (!embeddedComponent.component.classPath.startsWith('./')) {
embeddedComponent.component.classPath = `./${embeddedComponent.component.classPath}`;
}
result.tabViewImports.push(new import_to_add_1.ImportToAdd(embeddedComponent.component.class, (0, utility_functions_1.createRelativePath)(hostClassName.fileImportPath, embeddedComponent.component.classPath)));
result.entryComponentsImports.push(new import_to_add_1.ImportToAdd(embeddedComponent.component.class, embeddedComponent.component.classPath));
return new tab_content_template_1.TabContentTemplate(embeddedComponent.component.class);
}
function processEmbeddedNewView(embeddedView, result, hostClassName, newViewPath, tree, createViewFunctionRef, isDefaultTabbedTaskView = false) {
if (!embeddedView.view) {
throw new schematics_1.SchematicsException('processEmbeddedNewView can\'t be called with EmbeddedView object' +
' that doesn\'t contain the \'view\' attribute!');
}
if (embeddedView.view.name === undefined) {
throw new schematics_1.SchematicsException('TabView content View must define a \'name\' attribute');
}
const createViewArguments = {
path: newViewPath,
viewType: embeddedView.view.name,
layoutParams: embeddedView.view.params,
componentName: embeddedView.view.componentName,
isTabbed: true,
access: 'private',
isDefaultTabbedTaskView,
enableCaseTitle: embeddedView.enableCaseTitle === undefined ? true : embeddedView.enableCaseTitle,
isCaseTitleRequired: embeddedView.isCaseTitleRequired === undefined ? true : embeddedView.isCaseTitleRequired
};
result.rules.push(createViewFunctionRef(tree, createViewArguments, false));
const newComponentName = new view_class_info_1.ViewClassInfo(newViewPath, embeddedView.view.name);
result.tabViewImports.push(new import_to_add_1.ImportToAdd(newComponentName.className, (0, utility_functions_1.createRelativePath)(hostClassName.fileImportPath, newComponentName.fileImportPath)));
result.entryComponentsImports.push(new import_to_add_1.ImportToAdd(newComponentName.className, newComponentName.fileImportPath));
return new tab_content_template_1.TabContentTemplate(newComponentName.className);
}
function pushTabViews(destination, source) {
// iteration over Object.keys() caused a type compilation error
destination.entryComponentsImports.push(...source.entryComponentsImports);
destination.tabViewImports.push(...source.tabViewImports);
destination.tabTemplates.push(...source.tabTemplates);
destination.rules.push(...source.rules);
return destination;
}
function newTabViews() {
return {
rules: [],
tabTemplates: [],
tabViewImports: [],
entryComponentsImports: []
};
}
//# sourceMappingURL=create-tab-view.js.map