-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathtemplate-engine.ts
187 lines (173 loc) · 6.88 KB
/
template-engine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types */
/**
* Template Engine Bridge
*/
import { compile as render } from './template';
import { createElement } from './dom';
import { isNullOrUndefined, isBlazor } from './util';
const HAS_ROW: RegExp = /^[\n\r.]+<tr|^<tr/;
const HAS_SVG: RegExp = /^[\n\r.]+<svg|^<path|^<g/;
export const blazorTemplates: object = {};
/**
*
* @returns {string} ?
*/
export function getRandomId(): string {
return '-' + Math.random().toString(36).substr(2, 5);
}
/**
* Interface for Template Engine.
*/
export interface ITemplateEngine {
compile: (templateString: string | Function, helper?: Object, ignorePrefix?: boolean) => (data: Object | JSON) => string;
}
/**
* Compile the template string into template function.
*
* @param {string | Function} templateString - The template string which is going to convert.
* @param {Object} helper - Helper functions as an object.
* @param {boolean} ignorePrefix ?
* @returns {NodeList} ?
* @private
*/
export function compile(templateString: string | Function, helper?: Object, ignorePrefix?: boolean):
(data: Object | JSON, component?: any, propName?: any) => NodeList {
const compiler: Function = engineObj.compile(templateString, helper, ignorePrefix);
return (data: Object, component?: any, propName?: any, templateId?: any, isStringTemplate?: boolean,
index?: number, element?: any, root?: any): NodeList => {
const result: object = compiler(data, component, propName, element, root);
const blazorTemplateId: string = 'BlazorTemplateId';
if (isBlazor() && !isStringTemplate) {
const randomId: string = getRandomId();
let blazorId: string = templateId + randomId;
if (!blazorTemplates[`${templateId}`]) {
blazorTemplates[`${templateId}`] = [];
}
if (!isNullOrUndefined(index)) {
const keys: string[] = Object.keys(blazorTemplates[`${templateId}`][parseInt(index.toString(), 10)]);
for (const key of keys) {
if (key !== blazorTemplateId && data[`${key}`]) {
blazorTemplates[`${templateId}`][parseInt(index.toString(), 10)][`${key}`] = data[`${key}`];
}
if (key === blazorTemplateId) {
blazorId = blazorTemplates[`${templateId}`][parseInt(index.toString(), 10)][`${key}`];
}
}
} else {
data[`${blazorTemplateId}`] = blazorId;
blazorTemplates[`${templateId}`].push(data);
}
return propName === 'rowTemplate' ? [createElement('tr', { id: blazorId, className: 'e-blazor-template' })] as any :
[createElement('div', { id: blazorId, className: 'e-blazor-template' })] as any;
}
if (typeof result === 'string') {
if (HAS_SVG.test(result)) {
const ele: HTMLElement = createElement('svg', { innerHTML: result });
return <NodeList>ele.childNodes;
} else {
const ele: HTMLElement = createElement((HAS_ROW.test(result) ? 'table' : 'div'), { innerHTML: result });
return <NodeList>ele.childNodes;
}
} else {
return <NodeList>result;
}
};
}
/**
*
* @param {string} templateId ?
* @param {string} templateName ?
* @param {string} comp ?
* @param {boolean} isEmpty ?
* @param {Function} callBack ?
* @returns {void} ?
*/
export function updateBlazorTemplate(
templateId?: string, templateName?: string, comp?: object,
isEmpty?: boolean, callBack?: Function): void {
if (isBlazor()) {
const ejsIntrop: string = 'sfBlazor';
window[`${ejsIntrop}`].updateTemplate(templateName, blazorTemplates[`${templateId}`], templateId, comp, callBack);
if (isEmpty !== false) {
blazorTemplates[`${templateId}`] = [];
}
}
}
/**
*
* @param {string} templateId ?
* @param {string} templateName ?
* @param {number} index ?
* @returns {void} ?
*/
export function resetBlazorTemplate(templateId?: string, templateName?: string, index?: number): void {
const templateDiv: HTMLElement = document.getElementById(templateId);
if (templateDiv) {
const innerTemplates: HTMLElement[] = templateDiv.getElementsByClassName('blazor-inner-template') as any;
for (let i: number = 0; i < innerTemplates.length; i++) {
let tempId: string = ' ';
if (!isNullOrUndefined(index)) {
tempId = innerTemplates[parseInt(index.toString(), 10)].getAttribute('data-templateId');
} else {
tempId = innerTemplates[parseInt(i.toString(), 10)].getAttribute('data-templateId');
}
const tempElement: HTMLElement = document.getElementById(tempId);
if (tempElement) {
const length: number = tempElement.childNodes.length;
for (let j: number = 0; j < length; j++) {
if (!isNullOrUndefined(index)) {
innerTemplates[parseInt(index.toString(), 10)].appendChild(tempElement.childNodes[0]);
i = innerTemplates.length;
} else {
innerTemplates[parseInt(i.toString(), 10)].appendChild(tempElement.childNodes[0]);
}
}
}
}
}
}
/**
* Set your custom template engine for template rendering.
*
* @param {ITemplateEngine} classObj - Class object for custom template.
* @returns {void} ?
* @private
*/
export function setTemplateEngine(classObj: ITemplateEngine): void {
engineObj.compile = classObj.compile;
}
/**
* Get current template engine for template rendering
*
* @returns {string} ?
* @private
*/
export function getTemplateEngine(): (template: string, helper?: Object) => (data: Object | JSON) => string {
return engineObj.compile;
}
/**
* Set the current template function to support Content Security Policy.
*
* @param {Function} template - The template function that is going to render.
* @param {any} helper - The data utilized by the template from the helper.
* @returns {Function} ?
* @private
*/
export function initializeCSPTemplate (template : Function, helper?: any): Function {
let boundFunc : Function;
template.prototype.CSPTemplate = true;
if (!isNullOrUndefined(helper)) {
boundFunc = template.bind(helper);
boundFunc.prototype = Object.create(template.prototype);
} else {
boundFunc = template;
}
return boundFunc;
}
//Default Engine Class
class Engine implements ITemplateEngine {
public compile(templateString: string, helper: Object = {}, ignorePrefix?: boolean): (data: Object | JSON) => string {
return render(templateString, helper);
}
}
const engineObj: ITemplateEngine = { compile: new Engine().compile };