@contrail/document-generation 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ import { DocumentElement } from "@contrail/documents";
2
+ import { ComponentPropertyDefinition, ComponentPropertyTemplate, ComponentTemplate } from "../interfaces";
3
+ import { PropertyValueFormatter } from "@contrail/types";
4
+ export declare class ComponentGenerator {
5
+ static generateComponent(data: any, options: DocumentElement, template: ComponentTemplate): any;
6
+ static generatedModelBindings(data: any): {
7
+ item: string;
8
+ projectItem: string;
9
+ viewable: string;
10
+ };
11
+ static generateComponentFromPropertyTemplate(data: any, options: DocumentElement, template: ComponentPropertyTemplate): DocumentElement;
12
+ static generatePropertyTextElements(data: any, properties: Array<ComponentPropertyDefinition>, options: DocumentElement): Array<DocumentElement>;
13
+ static generatePropertyTextElement(data: any, property: ComponentPropertyDefinition, options: DocumentElement, formatter: PropertyValueFormatter): DocumentElement;
14
+ }
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ComponentGenerator = void 0;
4
+ const documents_1 = require("@contrail/documents");
5
+ const nanoid_1 = require("nanoid");
6
+ const types_1 = require("@contrail/types");
7
+ const util_1 = require("@contrail/util");
8
+ class ComponentGenerator {
9
+ static generateComponent(data, options, template) {
10
+ let component;
11
+ if (template.propertyTemplate) {
12
+ component = this.generateComponentFromPropertyTemplate(data, options, template.propertyTemplate);
13
+ }
14
+ else {
15
+ throw Error("No component property template defined");
16
+ }
17
+ return component;
18
+ }
19
+ static generatedModelBindings(data) {
20
+ return {
21
+ item: `item:${data.item.id}`,
22
+ projectItem: `project-item:${data.projectItem.id}`,
23
+ viewable: `item:${data.item.id}`,
24
+ };
25
+ }
26
+ static generateComponentFromPropertyTemplate(data, options, template) {
27
+ var _a;
28
+ let componentElement = documents_1.DocumentElementFactory.initNewElement('component');
29
+ if (options) {
30
+ componentElement = Object.assign(componentElement, options);
31
+ }
32
+ let yPosition = 0;
33
+ const imageDimension = template.imageDimension || {};
34
+ const imageHeight = imageDimension.height || imageDimension.width || 125;
35
+ const imageWidth = imageDimension.width || 125;
36
+ const imageUrl = util_1.ObjectUtil.getByPath(data, `viewable.mediumViewableDownloadUrl`);
37
+ const elements = [{
38
+ type: "image",
39
+ size: { width: imageWidth, height: imageHeight },
40
+ propertyBindings: { url: 'viewable.mediumViewableDownloadUrl' },
41
+ url: imageUrl,
42
+ }];
43
+ yPosition += imageHeight;
44
+ const textOptions = {
45
+ style: {
46
+ text: {
47
+ align: template.textHorizontalAlignment
48
+ }
49
+ },
50
+ size: {
51
+ width: imageWidth
52
+ }
53
+ };
54
+ elements.push(...this.generatePropertyTextElements(data, template.properties, textOptions));
55
+ const modelBindings = this.generatedModelBindings(data);
56
+ componentElement.elements = elements;
57
+ componentElement.modelBindings = modelBindings;
58
+ if (((_a = componentElement === null || componentElement === void 0 ? void 0 : componentElement.elements) === null || _a === void 0 ? void 0 : _a.length) > 0) {
59
+ componentElement.elements = componentElement.elements.map((e) => {
60
+ e.id = (0, nanoid_1.nanoid)(16);
61
+ return e;
62
+ });
63
+ }
64
+ return componentElement;
65
+ }
66
+ static generatePropertyTextElements(data, properties, options) {
67
+ const elements = [];
68
+ const formatter = new types_1.PropertyValueFormatter();
69
+ for (let property of properties) {
70
+ let element = this.generatePropertyTextElement(data, property, options, formatter);
71
+ elements.push(element);
72
+ }
73
+ return elements;
74
+ }
75
+ static generatePropertyTextElement(data, property, options, formatter) {
76
+ var _a, _b, _c;
77
+ const key = `${util_1.StringUtil.convertToCamelCase(property.typeRootSlug)}.${property.propertyDefinition.slug}`;
78
+ let style = property.style || {
79
+ font: {
80
+ size: 8,
81
+ },
82
+ color: 'rgba(0,0,0,.5)',
83
+ };
84
+ if ((_a = options === null || options === void 0 ? void 0 : options.style) === null || _a === void 0 ? void 0 : _a.text) {
85
+ style.text = options.style.text;
86
+ }
87
+ let element = Object.assign({
88
+ label: property.includeLabel ? (_b = property.propertyDefinition) === null || _b === void 0 ? void 0 : _b.label : null,
89
+ type: "text",
90
+ position: { x: 0, y: 0 },
91
+ size: { height: 25, width: ((_c = options === null || options === void 0 ? void 0 : options.size) === null || _c === void 0 ? void 0 : _c.width) || 125 },
92
+ style,
93
+ propertyBindings: { text: key }
94
+ });
95
+ const value = util_1.ObjectUtil.getByPath(data, key);
96
+ const display = formatter.formatValueForProperty(value, property.propertyDefinition);
97
+ element.text = display;
98
+ return element;
99
+ }
100
+ }
101
+ exports.ComponentGenerator = ComponentGenerator;
@@ -0,0 +1,7 @@
1
+ import { DocumentElement, PositionDefinition, SizeDefinition } from "@contrail/documents";
2
+ import { ComponentGridTemplate } from "../interfaces";
3
+ export declare class ComponentGridGenerator {
4
+ static generateComponentGrid(data: any, startingPosition: PositionDefinition, template: ComponentGridTemplate, idealSize: SizeDefinition): Array<DocumentElement>;
5
+ private static getComponentSize;
6
+ private static adjustComponentTemplate;
7
+ }
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ComponentGridGenerator = void 0;
4
+ const util_1 = require("@contrail/util");
5
+ const component_generator_1 = require("./component-generator");
6
+ const component_util_1 = require("./component-util");
7
+ class ComponentGridGenerator {
8
+ static generateComponentGrid(data, startingPosition = { x: 0, y: 0 }, template, idealSize) {
9
+ if (!data) {
10
+ console.warn("WARN: data null in generateComponentGrid");
11
+ return [];
12
+ }
13
+ if (!template) {
14
+ console.warn("WARN: template null in generateComponentGrid");
15
+ return [];
16
+ }
17
+ if (!template.componentTemplate) {
18
+ console.warn("WARN: component template is null generateComponentGrid");
19
+ return [];
20
+ }
21
+ const elements = [];
22
+ let dataIndex = 0;
23
+ let gridTemplate = template;
24
+ const optimizeTemplate = true;
25
+ if (optimizeTemplate) {
26
+ gridTemplate = this.adjustComponentTemplate(data, idealSize, template);
27
+ }
28
+ const componentTemplate = gridTemplate.componentTemplate;
29
+ const componentPosition = util_1.ObjectUtil.cloneDeep(startingPosition);
30
+ const componentSize = this.getComponentSize(data, gridTemplate);
31
+ if (componentSize.width < 120) {
32
+ componentSize.width = 120;
33
+ }
34
+ const componentPadding = gridTemplate.componentPadding || 20;
35
+ componentPosition.x += componentPadding;
36
+ componentPosition.y += componentPadding;
37
+ for (let row = 0; row < gridTemplate.gridDimensions.rows; row++) {
38
+ for (let col = 0; col < gridTemplate.gridDimensions.cols; col++) {
39
+ if (dataIndex >= data.length) {
40
+ continue;
41
+ }
42
+ let dataObj = data[dataIndex];
43
+ dataObj.viewable = dataObj.item;
44
+ const position = util_1.ObjectUtil.cloneDeep(componentPosition);
45
+ let component = component_generator_1.ComponentGenerator.generateComponent(dataObj, { position }, componentTemplate);
46
+ elements.push(component);
47
+ dataIndex++;
48
+ componentPosition.x += componentSize.width + (componentPadding * 2);
49
+ }
50
+ componentPosition.y += componentSize.height + (componentPadding * 2);
51
+ componentPosition.x = startingPosition.x + componentPadding;
52
+ }
53
+ return elements;
54
+ }
55
+ static getComponentSize(data, template) {
56
+ if (!(data === null || data === void 0 ? void 0 : data.length)) {
57
+ return null;
58
+ }
59
+ const firstData = data[0];
60
+ const element = component_generator_1.ComponentGenerator.generateComponent(firstData, {}, template.componentTemplate);
61
+ return component_util_1.ComponentUtil.getComponentSize(element);
62
+ }
63
+ static adjustComponentTemplate(data, idealSize, template) {
64
+ const componentSize = this.getComponentSize(data, template);
65
+ const adjustedTemplate = util_1.ObjectUtil.cloneDeep(template);
66
+ const requiredSize = {
67
+ width: componentSize.width * template.gridDimensions.cols + ((template.gridDimensions.cols * 2) * template.componentPadding || 20),
68
+ height: componentSize.height * template.gridDimensions.rows + ((template.gridDimensions.rows * 2) * template.componentPadding || 20)
69
+ };
70
+ const requiredHeightAdjustment = (idealSize.height - requiredSize.height) / template.gridDimensions.rows;
71
+ const requiredWidthAdjustment = (idealSize.width - requiredSize.width) / template.gridDimensions.cols;
72
+ console.log("requiredHeightAdjustment: ", requiredHeightAdjustment, " requiredWidthAdjustment: ", requiredWidthAdjustment);
73
+ if (requiredHeightAdjustment < 0 && requiredHeightAdjustment < requiredWidthAdjustment) {
74
+ adjustedTemplate.componentTemplate.propertyTemplate.imageDimension.width += requiredHeightAdjustment;
75
+ }
76
+ if (requiredWidthAdjustment < 0 && requiredWidthAdjustment < requiredHeightAdjustment) {
77
+ adjustedTemplate.componentTemplate.propertyTemplate.imageDimension.width += requiredWidthAdjustment;
78
+ }
79
+ return adjustedTemplate;
80
+ }
81
+ }
82
+ exports.ComponentGridGenerator = ComponentGridGenerator;
@@ -0,0 +1,4 @@
1
+ import { DocumentElement, SizeDefinition } from "@contrail/documents";
2
+ export declare class ComponentUtil {
3
+ static getComponentSize(element: DocumentElement): SizeDefinition;
4
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ComponentUtil = void 0;
4
+ class ComponentUtil {
5
+ static getComponentSize(element) {
6
+ var _a, _b, _c, _d, _e;
7
+ const sizeDefinition = {
8
+ width: 0,
9
+ height: 0,
10
+ };
11
+ const imageElement = ((_a = element.elements) === null || _a === void 0 ? void 0 : _a.find(el => el.type === 'image')) || {};
12
+ sizeDefinition.width = (_b = imageElement.size) === null || _b === void 0 ? void 0 : _b.width;
13
+ const LINE_PADDING = 8;
14
+ const textElements = (_c = element.elements) === null || _c === void 0 ? void 0 : _c.filter(el => el.type === 'text');
15
+ let textHeight = 0;
16
+ textElements.forEach(el => {
17
+ var _a, _b;
18
+ let fontSize = ((_b = (_a = el.style) === null || _a === void 0 ? void 0 : _a.font) === null || _b === void 0 ? void 0 : _b.size) || 12;
19
+ textHeight += fontSize + LINE_PADDING;
20
+ });
21
+ const imageHeight = (((_d = imageElement.size) === null || _d === void 0 ? void 0 : _d.height) || ((_e = imageElement.size) === null || _e === void 0 ? void 0 : _e.width) || 150);
22
+ sizeDefinition.height = imageHeight + textHeight;
23
+ return sizeDefinition;
24
+ }
25
+ }
26
+ exports.ComponentUtil = ComponentUtil;
@@ -0,0 +1,11 @@
1
+ import { DocumentElement, PositionDefinition } from "@contrail/documents";
2
+ import { DocumentGenerationOptions, DocumentTemplate } from "./interfaces";
3
+ import { DataGroup } from "@contrail/data-grouping";
4
+ export declare class DocumentGenerator {
5
+ constructor();
6
+ static generateDocumentElements(options: DocumentGenerationOptions): Array<DocumentElement>;
7
+ static generateGroup(group: DataGroup, template: any, startingPosition: any, totalDepth: any, currentDepth: any): any[];
8
+ static getGroupSpan(group: DataGroup, totalDepth: any, currentDepth: any): number;
9
+ static generateFrameGroup(dataGroup: DataGroup, template: DocumentTemplate, position: PositionDefinition): Array<DocumentElement>;
10
+ static generateGroupHeading(dataGroup: DataGroup, template: DocumentTemplate, position: PositionDefinition, span: any): any[];
11
+ }
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DocumentGenerator = void 0;
4
+ const documents_1 = require("@contrail/documents");
5
+ const interfaces_1 = require("./interfaces");
6
+ const util_1 = require("@contrail/util");
7
+ const frame_generator_1 = require("./frame-generator");
8
+ class DocumentGenerator {
9
+ constructor() {
10
+ }
11
+ static generateDocumentElements(options) {
12
+ const elements = [];
13
+ const structure = options.data.dataGroup;
14
+ const template = options.documentTemplateDefinition;
15
+ console.log("generateDocumentElements: ", structure.depth, structure.groupingProperties.length);
16
+ let position = options.startingCoordinates || { x: 0, y: 0 };
17
+ const group = structure.rootGroup;
18
+ elements.push(...this.generateGroup(group, template, position, structure.depth, 0));
19
+ return elements;
20
+ }
21
+ static generateGroup(group, template, startingPosition, totalDepth, currentDepth) {
22
+ const elements = [];
23
+ let position = util_1.ObjectUtil.cloneDeep(startingPosition);
24
+ if (currentDepth > 0) {
25
+ let groupHeaderPosition = { x: position.x + template.framePadding, y: position.y + template.framePadding };
26
+ let span = this.getGroupSpan(group, totalDepth, currentDepth);
27
+ elements.push(...this.generateGroupHeading(group, template, groupHeaderPosition, span));
28
+ let groupHeaderDimensions = { width: template.frameSize.width, height: template.frameGroupHeaderTemplate.size.height };
29
+ if (template.frameOrientation === interfaces_1.Orientation.HORIZONTAL) {
30
+ position = { x: position.x + groupHeaderDimensions.height + template.framePadding, y: position.y };
31
+ }
32
+ else {
33
+ position = { x: position.x, y: position.y + groupHeaderDimensions.height + template.framePadding };
34
+ }
35
+ }
36
+ if (currentDepth === totalDepth) {
37
+ elements.push(...this.generateFrameGroup(group, template, position));
38
+ }
39
+ if (currentDepth < totalDepth) {
40
+ for (let subGroup of group.subGroups) {
41
+ elements.push(...this.generateGroup(subGroup, template, position, totalDepth, currentDepth + 1));
42
+ let frameSetMultiple = this.getGroupSpan(subGroup, totalDepth, currentDepth + 1);
43
+ let paddingMultiple = 1;
44
+ if (currentDepth < totalDepth) {
45
+ paddingMultiple = frameSetMultiple;
46
+ }
47
+ if (template.frameOrientation === interfaces_1.Orientation.HORIZONTAL) {
48
+ position = { x: position.x, y: position.y + (template.frameSize.height * frameSetMultiple) + (template.framePadding * paddingMultiple) };
49
+ }
50
+ else {
51
+ position = { x: position.x + (template.frameSize.width * frameSetMultiple) + (template.framePadding * paddingMultiple), y: position.y };
52
+ }
53
+ }
54
+ }
55
+ return elements;
56
+ }
57
+ static getGroupSpan(group, totalDepth, currentDepth) {
58
+ let span = 1;
59
+ if (currentDepth !== totalDepth) {
60
+ span = group.subGroups.length;
61
+ }
62
+ return span;
63
+ }
64
+ static generateFrameGroup(dataGroup, template, position) {
65
+ const elements = [];
66
+ let framePosition = { x: position.x + template.framePadding, y: position.y + template.framePadding };
67
+ for (let group of dataGroup.subGroups) {
68
+ elements.push(...frame_generator_1.FrameGenerator.generateFrameForDataGroup(group, framePosition, template));
69
+ if (template.frameOrientation === interfaces_1.Orientation.HORIZONTAL) {
70
+ framePosition.x = framePosition.x + template.frameSize.width + template.framePadding || 10;
71
+ }
72
+ else {
73
+ framePosition.y = framePosition.y + template.frameSize.height + template.framePadding || 10;
74
+ }
75
+ }
76
+ return elements;
77
+ }
78
+ static generateGroupHeading(dataGroup, template, position, span) {
79
+ const elements = [];
80
+ const heading = documents_1.DocumentElementFactory.createTextElement("Placeholder", {});
81
+ heading.style = { border: { width: 1, color: "rgba(0,0,0,0)" }, color: "#000000", backgroundColor: template.frameGroupHeaderTemplate.style.backgroundColor || 'black' };
82
+ heading.text = `<p><span style=\"font-size: ${template.frameGroupHeaderTemplate.style.font.size}pt;\"><strong><span style=\"color: ${template.frameGroupHeaderTemplate.style.color};\">${dataGroup.name}</span></strong></span></p>`;
83
+ heading.position = position;
84
+ if (template.frameOrientation === interfaces_1.Orientation.HORIZONTAL) {
85
+ let sizeDim = template.frameSize.height * span;
86
+ if (span > 1) {
87
+ sizeDim += (span - 1) * template.framePadding;
88
+ }
89
+ heading.size = { width: sizeDim, height: template.frameGroupHeaderTemplate.size.height };
90
+ heading.rotate = { angle: 270 };
91
+ heading.position.x = heading.position.x - (heading.size.width / 2) + (heading.size.height / 2);
92
+ heading.position.y = heading.position.y + (heading.size.width / 2) - (heading.size.height / 2);
93
+ }
94
+ else {
95
+ let sizeDim = template.frameSize.width * span;
96
+ if (span > 1) {
97
+ sizeDim += (span - 1) * template.framePadding;
98
+ }
99
+ heading.rotate = { angle: 0 };
100
+ heading.size = { width: sizeDim, height: template.frameGroupHeaderTemplate.size.height };
101
+ }
102
+ elements.push(heading);
103
+ return elements;
104
+ }
105
+ }
106
+ exports.DocumentGenerator = DocumentGenerator;
@@ -0,0 +1,8 @@
1
+ import { DocumentElement, PositionDefinition, SizeDefinition } from "@contrail/documents";
2
+ import { DataGroup } from "@contrail/data-grouping";
3
+ import { DocumentTemplate } from "./interfaces";
4
+ export declare class FrameGenerator {
5
+ static generateFrameForDataGroup(dataGroup: DataGroup, framePosition: PositionDefinition, template: DocumentTemplate): any[];
6
+ static generateFrameHeaderForDataGroup(dataGroup: DataGroup, position: PositionDefinition, template: DocumentTemplate, headerSize: SizeDefinition): DocumentElement[];
7
+ static generateFrame(options: DocumentElement): DocumentElement;
8
+ }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FrameGenerator = void 0;
4
+ const documents_1 = require("@contrail/documents");
5
+ const component_grid_generator_1 = require("./components/component-grid-generator");
6
+ const util_1 = require("@contrail/util");
7
+ const document_util_1 = require("./util/document-util");
8
+ class FrameGenerator {
9
+ static generateFrameForDataGroup(dataGroup, framePosition, template) {
10
+ const elements = [];
11
+ const frame = this.generateFrame({
12
+ position: util_1.ObjectUtil.cloneDeep(framePosition),
13
+ size: template.frameSize,
14
+ style: { backgroundColor: "#fff", border: { width: 1, color: "#616161" } }
15
+ });
16
+ frame.name = dataGroup.name;
17
+ const FRAME_PADDING = 20;
18
+ const headerPosition = util_1.ObjectUtil.cloneDeep(framePosition);
19
+ headerPosition.y = headerPosition.y + FRAME_PADDING;
20
+ headerPosition.x = headerPosition.x + FRAME_PADDING;
21
+ const frameHeaderDimensions = { height: 50, width: template.frameSize.width - 50 };
22
+ const HEADER_MARGIN = 20;
23
+ const gridPosition = util_1.ObjectUtil.cloneDeep(headerPosition);
24
+ gridPosition.y = gridPosition.y + frameHeaderDimensions.height + HEADER_MARGIN;
25
+ const relativeGridPosition = document_util_1.DocumentUtil.getRelativePosition(framePosition, gridPosition);
26
+ const frameSize = template.frameSize;
27
+ const idealGridSize = {
28
+ width: frameSize.width - relativeGridPosition.x - FRAME_PADDING,
29
+ height: frameSize.height - relativeGridPosition.y - FRAME_PADDING
30
+ };
31
+ elements.push(util_1.ObjectUtil.cloneDeep(frame));
32
+ elements.push(...this.generateFrameHeaderForDataGroup(dataGroup, headerPosition, template, frameHeaderDimensions));
33
+ elements.push(...component_grid_generator_1.ComponentGridGenerator.generateComponentGrid(dataGroup.data, gridPosition, template.componentGridTemplate, idealGridSize));
34
+ return elements;
35
+ }
36
+ static generateFrameHeaderForDataGroup(dataGroup, position, template, headerSize) {
37
+ const heading = documents_1.DocumentElementFactory.createTextElement(dataGroup.name, template.frameHeaderTemplate);
38
+ heading.style = { border: { width: 1, color: "rgba(0,0,0,0)" }, color: "#000000", backgroundColor: template.frameHeaderTemplate.style.backgroundColor || 'black' };
39
+ heading.text = `<p><span style=\"font-size: ${template.frameHeaderTemplate.style.font.size}pt;\"><strong><span style=\"color: ${template.frameHeaderTemplate.style.color};\">${dataGroup.name}</span></strong></span></p>`;
40
+ heading.size = util_1.ObjectUtil.cloneDeep(headerSize);
41
+ heading.position = util_1.ObjectUtil.cloneDeep(position);
42
+ return [heading];
43
+ }
44
+ static generateFrame(options) {
45
+ const frame = documents_1.DocumentElementFactory.createFrameElement(options);
46
+ return frame;
47
+ }
48
+ }
49
+ exports.FrameGenerator = FrameGenerator;
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './document-generator';
2
+ export * from './interfaces';
package/lib/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./document-generator"), exports);
18
+ __exportStar(require("./interfaces"), exports);
@@ -0,0 +1,47 @@
1
+ import { DataGroupStructure } from "@contrail/data-grouping";
2
+ import { PositionDefinition, SizeDefinition, DocumentElement, StyleDefinition } from "@contrail/documents";
3
+ import { TypeProperty } from "@contrail/types";
4
+ export declare enum Orientation {
5
+ HORIZONTAL = "horizontal",
6
+ VERTICAL = "vertical"
7
+ }
8
+ export interface DocumentGenerationDataDefinition {
9
+ dataGroup: DataGroupStructure;
10
+ }
11
+ export interface DocumentGenerationOptions {
12
+ startingCoordinates: PositionDefinition;
13
+ data: DocumentGenerationDataDefinition;
14
+ documentTemplateDefinition: DocumentTemplate;
15
+ }
16
+ export interface DocumentTemplate {
17
+ frameOrientation: Orientation;
18
+ frameSize: SizeDefinition;
19
+ framePadding?: number;
20
+ frameGroupHeaderTemplate: DocumentElement;
21
+ frameHeaderTemplate: DocumentElement;
22
+ componentGridTemplate: ComponentGridTemplate;
23
+ }
24
+ export interface ComponentGridTemplate {
25
+ componentTemplate: ComponentTemplate;
26
+ componentPadding?: number;
27
+ gridDimensions: {
28
+ rows: number;
29
+ cols: number;
30
+ };
31
+ }
32
+ export interface ComponentTemplate {
33
+ propertyTemplate?: ComponentPropertyTemplate;
34
+ }
35
+ export interface ComponentPropertyTemplate {
36
+ imageDimension?: SizeDefinition;
37
+ imageLocation?: 'top' | 'right' | 'left' | 'bottom';
38
+ textHorizontalAlignment?: 'center' | 'right' | 'left';
39
+ properties: Array<ComponentPropertyDefinition>;
40
+ }
41
+ export interface ComponentPropertyDefinition {
42
+ style?: StyleDefinition;
43
+ propertyDefinition: TypeProperty;
44
+ slug: string;
45
+ typeRootSlug: string;
46
+ includeLabel?: boolean;
47
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Orientation = void 0;
4
+ var Orientation;
5
+ (function (Orientation) {
6
+ Orientation["HORIZONTAL"] = "horizontal";
7
+ Orientation["VERTICAL"] = "vertical";
8
+ })(Orientation = exports.Orientation || (exports.Orientation = {}));
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const document_generator_1 = require("../document-generator");
13
+ const interfaces_1 = require("../interfaces");
14
+ const sdk_1 = require("@contrail/sdk");
15
+ const fs = require("fs");
16
+ const nanoid_1 = require("nanoid");
17
+ const data_grouping_1 = require("@contrail/data-grouping");
18
+ function run() {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ const generator = new document_generator_1.DocumentGenerator();
21
+ const orgSlug = 'converse-prod';
22
+ yield loginSDK(orgSlug);
23
+ const assortmentId = 'Z7nTOauGtuuiU9UC';
24
+ const itemType = yield new sdk_1.Types().getType({ path: 'item' });
25
+ const projectItemType = yield new sdk_1.Types().getType({ path: 'project-item' });
26
+ const PROPERTY_COMPONENT_TEMPLATE = {
27
+ imageDimension: { width: 600 },
28
+ imageLocation: 'top',
29
+ textHorizontalAlignment: 'center',
30
+ properties: [{
31
+ propertyDefinition: itemType.typeProperties.find(p => p.slug === 'name'),
32
+ typeRootSlug: 'item',
33
+ slug: "name",
34
+ style: {
35
+ font: {
36
+ size: 14,
37
+ weight: 'bold',
38
+ },
39
+ color: 'rgba(0,0,0,.5)',
40
+ }
41
+ }, {
42
+ propertyDefinition: itemType.typeProperties.find(p => p.slug === 'optionName'),
43
+ typeRootSlug: 'item',
44
+ slug: "optionName",
45
+ style: {
46
+ font: {
47
+ size: 10,
48
+ weight: 'bold',
49
+ },
50
+ color: 'green',
51
+ }
52
+ }, {
53
+ propertyDefinition: itemType.typeProperties.find(p => p.slug === 'category'),
54
+ typeRootSlug: 'item',
55
+ slug: "category",
56
+ includeLabel: true,
57
+ }, {
58
+ propertyDefinition: itemType.typeProperties.find(p => p.slug === 'designTheme'),
59
+ typeRootSlug: 'item',
60
+ slug: "designTheme",
61
+ includeLabel: true,
62
+ }, {
63
+ propertyDefinition: itemType.typeProperties.find(p => p.slug === 'baseStyleNumber'),
64
+ typeRootSlug: 'item',
65
+ slug: "baseStyleNumber",
66
+ }, {
67
+ propertyDefinition: itemType.typeProperties.find(p => p.slug === 'closureType'),
68
+ typeRootSlug: 'item',
69
+ slug: "closureType",
70
+ includeLabel: true,
71
+ }, {
72
+ propertyDefinition: itemType.typeProperties.find(p => p.slug === 'colorFamily'),
73
+ typeRootSlug: 'item',
74
+ slug: "colorFamily",
75
+ includeLabel: true,
76
+ }, {
77
+ propertyDefinition: projectItemType.typeProperties.find(p => p.slug === 'colorwayStatus'),
78
+ slug: "colorwayStatus",
79
+ typeRootSlug: 'project-item',
80
+ includeLabel: true,
81
+ }]
82
+ };
83
+ const documentTemplateDefinition = {
84
+ frameOrientation: interfaces_1.Orientation.VERTICAL,
85
+ frameSize: { width: 1200, height: 675 },
86
+ framePadding: 30,
87
+ frameHeaderTemplate: {
88
+ style: {
89
+ color: '#000000',
90
+ font: {
91
+ size: 20
92
+ },
93
+ backgroundColor: '#FFFFFF',
94
+ },
95
+ size: {
96
+ height: 60,
97
+ }
98
+ },
99
+ frameGroupHeaderTemplate: {
100
+ style: {
101
+ color: '#ffffff',
102
+ font: {
103
+ size: 20
104
+ },
105
+ backgroundColor: 'black',
106
+ },
107
+ size: {
108
+ height: 60,
109
+ }
110
+ },
111
+ componentGridTemplate: {
112
+ gridDimensions: { cols: 8, rows: 2 },
113
+ componentPadding: 5,
114
+ componentTemplate: {
115
+ propertyTemplate: PROPERTY_COMPONENT_TEMPLATE
116
+ }
117
+ }
118
+ };
119
+ const groupingProperties = [
120
+ {
121
+ property: itemType.typeProperties.find(p => p.slug === 'designPack'),
122
+ scope: 'item',
123
+ sort: sdk_1.SortOrderOptions.ASC,
124
+ values: null,
125
+ }, {
126
+ property: itemType.typeProperties.find(p => p.slug === 'category'),
127
+ scope: 'item',
128
+ sort: sdk_1.SortOrderOptions.ASC,
129
+ values: null,
130
+ }, {
131
+ property: itemType.typeProperties.find(p => p.slug === 'class'),
132
+ scope: 'item',
133
+ sort: sdk_1.SortOrderOptions.ASC,
134
+ values: null,
135
+ },
136
+ ];
137
+ documentTemplateDefinition.componentGridTemplate.componentTemplate.propertyTemplate = PROPERTY_COMPONENT_TEMPLATE;
138
+ const dataGroup = yield getDataGroupStructure(assortmentId, groupingProperties, documentTemplateDefinition);
139
+ const params = {
140
+ startingCoordinates: { x: 500, y: 500 },
141
+ data: {
142
+ dataGroup
143
+ },
144
+ documentTemplateDefinition,
145
+ };
146
+ const elements = document_generator_1.DocumentGenerator.generateDocumentElements(params);
147
+ const documentId = (0, nanoid_1.nanoid)(16);
148
+ elements.forEach(el => {
149
+ el.id = (0, nanoid_1.nanoid)(16);
150
+ });
151
+ const document = {
152
+ size: { width: 1600, height: 900 },
153
+ elements,
154
+ id: documentId,
155
+ };
156
+ yield uploadAndOpen(orgSlug, document);
157
+ });
158
+ }
159
+ function loginSDK(orgSlug) {
160
+ return __awaiter(this, void 0, void 0, function* () {
161
+ yield (0, sdk_1.login)({
162
+ email: 'brian@vibeiq.com',
163
+ password: process.env.CONTRAIL_PW,
164
+ orgSlug: orgSlug,
165
+ });
166
+ });
167
+ }
168
+ function getAssortmentItems(assortmentId) {
169
+ return __awaiter(this, void 0, void 0, function* () {
170
+ const assortment = yield new sdk_1.Entities().get({
171
+ entityName: 'assortment',
172
+ id: assortmentId,
173
+ relations: ['assortmentItems', 'assortmentItems.item', 'assortmentItems.projectItem']
174
+ });
175
+ let assortmentItems = assortment.assortmentItems;
176
+ if (assortment.itemsDownloadURL) {
177
+ const response = yield fetch(assortment.itemsDownloadURL);
178
+ assortmentItems = yield response.json();
179
+ }
180
+ return assortmentItems;
181
+ });
182
+ }
183
+ function getDataGroupStructure(assortmentId, groupingProperties, template) {
184
+ return __awaiter(this, void 0, void 0, function* () {
185
+ const assortmentItems = yield getAssortmentItems(assortmentId);
186
+ const data = fromAssortmentModelToItemModel(assortmentItems);
187
+ const leafNodeDataCount = template.componentGridTemplate.gridDimensions.cols * template.componentGridTemplate.gridDimensions.rows;
188
+ return data_grouping_1.DataGroupGenerator.buildDataGroupStructure(data, groupingProperties, leafNodeDataCount);
189
+ });
190
+ }
191
+ function fromAssortmentModelToItemModel(data) {
192
+ const results = [];
193
+ data.forEach(ai => {
194
+ results.push({
195
+ item: ai.item,
196
+ projectItem: ai.projectItem,
197
+ viewable: ai.item,
198
+ assortmentItem: ai,
199
+ });
200
+ });
201
+ return results;
202
+ }
203
+ function uploadAndOpen(orgSlug, document) {
204
+ return __awaiter(this, void 0, void 0, function* () {
205
+ let config = (0, sdk_1.getConfig)();
206
+ var writeStream = fs.createWriteStream("testdocument.json");
207
+ writeStream.write(JSON.stringify(document));
208
+ writeStream.end();
209
+ const file = fs.createReadStream("testdocument.json");
210
+ const fileEntity = yield new sdk_1.Files().createAndUploadFileFromBuffer(file, 'application/json', 'testdocument.json', null, 300);
211
+ const domain = "http://preview.vibeiq.com";
212
+ const authToken = config.apiUserToken;
213
+ const documentType = 'board';
214
+ let url = domain +
215
+ '?url=' + encodeURIComponent(fileEntity.downloadUrl) +
216
+ '&orgSlug=' + encodeURIComponent(orgSlug) +
217
+ '&apiToken=' + encodeURIComponent(authToken) +
218
+ '&documentType=' + encodeURIComponent(documentType);
219
+ var start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open');
220
+ const encodedUrl = url.replace(/&/g, '^&');
221
+ require('child_process').exec(start + ' ' + encodedUrl);
222
+ });
223
+ }
224
+ run();
@@ -0,0 +1,72 @@
1
+ import { DataGroupStructure } from "@contrail/data-grouping";
2
+ import { ComponentGridTemplate, DocumentTemplate } from "./interfaces";
3
+ export declare const TEST_ITEM_MODEL: {
4
+ item: {
5
+ id: string;
6
+ name: string;
7
+ optionName: string;
8
+ gender: string;
9
+ mediumViewableDownloadUrl: string;
10
+ };
11
+ projectItem: {
12
+ id: string;
13
+ retailPrice: number;
14
+ };
15
+ assortmentItem: {
16
+ id: string;
17
+ targetVolume: number;
18
+ };
19
+ };
20
+ export declare const TEST_GROUP: {
21
+ name: string;
22
+ data: {
23
+ item: {
24
+ id: string;
25
+ name: string;
26
+ optionName: string;
27
+ gender: string;
28
+ mediumViewableDownloadUrl: string;
29
+ };
30
+ projectItem: {
31
+ id: string;
32
+ retailPrice: number;
33
+ };
34
+ assortmentItem: {
35
+ id: string;
36
+ targetVolume: number;
37
+ };
38
+ }[];
39
+ properties: {
40
+ gender: string;
41
+ };
42
+ subGroups: any[];
43
+ };
44
+ export declare const TEST_GROUP_2: {
45
+ name: string;
46
+ data: {
47
+ item: {
48
+ id: string;
49
+ name: string;
50
+ optionName: string;
51
+ gender: string;
52
+ mediumViewableDownloadUrl: string;
53
+ };
54
+ projectItem: {
55
+ id: string;
56
+ retailPrice: number;
57
+ };
58
+ assortmentItem: {
59
+ id: string;
60
+ targetVolume: number;
61
+ };
62
+ }[];
63
+ properties: {
64
+ gender: string;
65
+ };
66
+ subGroups: any[];
67
+ };
68
+ export declare const TEST_ROOT_GROUP: DataGroupStructure;
69
+ export declare const TEST_TWO_LEVEL_ROOT_GROUP: DataGroupStructure;
70
+ export declare const COMPONENT_GRID_TEMPLATE: ComponentGridTemplate;
71
+ export declare const HORIZONTAL_DOCUMENT_TEMPLATE: DocumentTemplate;
72
+ export declare const VERTICAL_DOCUMENT_TEMPLATE: DocumentTemplate;
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VERTICAL_DOCUMENT_TEMPLATE = exports.HORIZONTAL_DOCUMENT_TEMPLATE = exports.COMPONENT_GRID_TEMPLATE = exports.TEST_TWO_LEVEL_ROOT_GROUP = exports.TEST_ROOT_GROUP = exports.TEST_GROUP_2 = exports.TEST_GROUP = exports.TEST_ITEM_MODEL = void 0;
4
+ const types_1 = require("@contrail/types");
5
+ const interfaces_1 = require("./interfaces");
6
+ const sdk_1 = require("@contrail/sdk");
7
+ exports.TEST_ITEM_MODEL = {
8
+ item: {
9
+ id: 'item-1001',
10
+ name: 'Shirt 1',
11
+ optionName: 'Red',
12
+ gender: 'mens',
13
+ mediumViewableDownloadUrl: 'https://api.vibeiq.com/dev/api/files/downloadUrl/-VMrLJrt3clDKPi0%2Fcontent:m3Bd4u-QLci1XiOl%2F2b72d48a-6b19-4ba8-9d54-813745fb8dd4.png',
14
+ },
15
+ projectItem: {
16
+ id: 'projectItem-1001',
17
+ retailPrice: 45.00
18
+ },
19
+ assortmentItem: {
20
+ id: 'assortmentItem-1001',
21
+ targetVolume: 1000
22
+ },
23
+ };
24
+ exports.TEST_GROUP = {
25
+ name: "Mens",
26
+ data: [exports.TEST_ITEM_MODEL,
27
+ exports.TEST_ITEM_MODEL,
28
+ exports.TEST_ITEM_MODEL,
29
+ exports.TEST_ITEM_MODEL,
30
+ exports.TEST_ITEM_MODEL,
31
+ exports.TEST_ITEM_MODEL,
32
+ exports.TEST_ITEM_MODEL,
33
+ exports.TEST_ITEM_MODEL,
34
+ exports.TEST_ITEM_MODEL,
35
+ exports.TEST_ITEM_MODEL],
36
+ properties: {
37
+ gender: 'mens'
38
+ },
39
+ subGroups: [],
40
+ };
41
+ exports.TEST_GROUP_2 = {
42
+ name: "Womens",
43
+ data: [exports.TEST_ITEM_MODEL,
44
+ exports.TEST_ITEM_MODEL,
45
+ exports.TEST_ITEM_MODEL,
46
+ exports.TEST_ITEM_MODEL,
47
+ exports.TEST_ITEM_MODEL,
48
+ exports.TEST_ITEM_MODEL,
49
+ exports.TEST_ITEM_MODEL],
50
+ properties: {
51
+ gender: 'womens'
52
+ },
53
+ subGroups: [],
54
+ };
55
+ exports.TEST_ROOT_GROUP = {
56
+ depth: 1,
57
+ groupingProperties: [{
58
+ property: {
59
+ slug: 'gender',
60
+ propertyType: types_1.PropertyType.SingleSelect,
61
+ label: "Gender",
62
+ },
63
+ sort: sdk_1.SortOrderOptions.ASC,
64
+ scope: 'item',
65
+ values: null,
66
+ }],
67
+ rootGroup: {
68
+ data: [],
69
+ subGroups: [exports.TEST_GROUP, exports.TEST_GROUP, exports.TEST_GROUP],
70
+ name: "Root",
71
+ properties: {},
72
+ }
73
+ };
74
+ exports.TEST_TWO_LEVEL_ROOT_GROUP = {
75
+ depth: 2,
76
+ groupingProperties: [{
77
+ property: {
78
+ slug: 'gender',
79
+ propertyType: types_1.PropertyType.SingleSelect,
80
+ label: "Gender",
81
+ },
82
+ sort: sdk_1.SortOrderOptions.ASC,
83
+ scope: 'item',
84
+ values: null,
85
+ }],
86
+ rootGroup: {
87
+ data: [],
88
+ subGroups: [{
89
+ name: 'Mens',
90
+ subGroups: [exports.TEST_GROUP, exports.TEST_GROUP],
91
+ properties: {
92
+ gender: 'mens'
93
+ },
94
+ data: [],
95
+ }, {
96
+ name: 'Womens',
97
+ subGroups: [exports.TEST_GROUP_2, exports.TEST_GROUP_2, exports.TEST_GROUP_2, exports.TEST_GROUP_2],
98
+ properties: {
99
+ gender: 'mens'
100
+ },
101
+ data: [],
102
+ }],
103
+ name: "Root",
104
+ properties: {},
105
+ }
106
+ };
107
+ exports.COMPONENT_GRID_TEMPLATE = {
108
+ gridDimensions: { cols: 7, rows: 3 },
109
+ componentTemplate: {
110
+ propertyTemplate: {
111
+ properties: [],
112
+ imageLocation: 'top',
113
+ }
114
+ },
115
+ componentPadding: 20
116
+ };
117
+ exports.HORIZONTAL_DOCUMENT_TEMPLATE = {
118
+ frameOrientation: interfaces_1.Orientation.HORIZONTAL,
119
+ frameSize: { width: 1200, height: 675 },
120
+ framePadding: 30,
121
+ frameHeaderTemplate: {
122
+ style: {
123
+ color: '#000000',
124
+ font: {
125
+ size: 20
126
+ },
127
+ backgroundColor: '#FFFFFF',
128
+ },
129
+ size: {
130
+ height: 60,
131
+ }
132
+ },
133
+ frameGroupHeaderTemplate: {
134
+ style: {
135
+ color: '#ffffff',
136
+ font: {
137
+ size: 20
138
+ },
139
+ backgroundColor: 'black',
140
+ },
141
+ size: {
142
+ height: 60,
143
+ }
144
+ },
145
+ componentGridTemplate: exports.COMPONENT_GRID_TEMPLATE,
146
+ };
147
+ exports.VERTICAL_DOCUMENT_TEMPLATE = {
148
+ frameOrientation: interfaces_1.Orientation.VERTICAL,
149
+ frameSize: { width: 1200, height: 675 },
150
+ framePadding: 30,
151
+ frameHeaderTemplate: {
152
+ style: {
153
+ color: '#000000',
154
+ font: {
155
+ size: 20
156
+ },
157
+ backgroundColor: '#FFFFFF',
158
+ },
159
+ size: {
160
+ height: 60,
161
+ }
162
+ },
163
+ frameGroupHeaderTemplate: {
164
+ style: {
165
+ color: '#ffffff',
166
+ font: {
167
+ size: 20
168
+ },
169
+ backgroundColor: 'black',
170
+ },
171
+ size: {
172
+ height: 60,
173
+ }
174
+ },
175
+ componentGridTemplate: exports.COMPONENT_GRID_TEMPLATE,
176
+ };
@@ -0,0 +1,7 @@
1
+ import { PositionDefinition } from "@contrail/documents";
2
+ export declare class DocumentUtil {
3
+ static getRelativePosition(position1: PositionDefinition, position2: PositionDefinition): {
4
+ x: number;
5
+ y: number;
6
+ };
7
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DocumentUtil = void 0;
4
+ class DocumentUtil {
5
+ static getRelativePosition(position1, position2) {
6
+ const relativePosition = {
7
+ x: position2.x - position1.x,
8
+ y: position2.y - position1.y,
9
+ };
10
+ return relativePosition;
11
+ }
12
+ }
13
+ exports.DocumentUtil = DocumentUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/document-generation",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Utilities for automatic generation of documents.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -42,9 +42,9 @@
42
42
  "testEnvironment": "node"
43
43
  },
44
44
  "dependencies": {
45
- "@contrail/data-grouping": "^1.0.11",
45
+ "@contrail/data-grouping": "^1.0.13",
46
46
  "@contrail/documents": "^1.0.44",
47
47
  "@contrail/types": "^3.0.27",
48
- "@contrail/util": "^1.0.26"
48
+ "@contrail/util": "^1.0.27"
49
49
  }
50
50
  }