-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzenCore.ts
199 lines (186 loc) · 7.37 KB
/
zenCore.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
188
189
190
191
192
193
194
195
196
197
198
199
const numberDel = '⏰';
export interface Loop<T>{
'➰': T[],
'🎬': (t: T) => any,
}
export interface LoopTemplate<TItemContainer, TItem>{
'➰': (t: TItemContainer) => TItem[],
'🎬': (t: TItem) => any,
}
export interface IProperty{
type?: any,
setter?: any,
readOnly?: boolean
//uid?: string,
}
export function zen(strings : any, ...values){
const sArr = strings as string[];
const sArrWithSiblings = [];
for(let i = 0, ii = sArr.length; i < ii; i++){
const word = sArr[i];
const sArrElement = word + numberDel + i;
if(sArrElement.substr(0, 1) === '+'){
sArrWithSiblings[sArrWithSiblings.length - 1] += sArrElement;
}else{
sArrWithSiblings.push(sArrElement);
}
}
const outputArr = [] as any[];
let allTags = [];
for(const tagSequence of sArrWithSiblings){
const tags = tagSequence.split('>');
allTags = allTags.concat(tags);
}
processTags(allTags, outputArr, values);
return outputArr;
}
const camelToSnakeRegEx = /([A-Z])/g;
const toDashLowerCase = function($1){return "-"+$1.toLowerCase();}
function camelToSnake(str: string){
return str.replace(camelToSnakeRegEx, toDashLowerCase);
}
const splitRegExps: {[key: string]: RegExp} = {};
function splitWithEscape(s: string, chr: string){
let reg = splitRegExps[chr];
if(!reg){
const r = `([^\\\\\\][^${chr}]|\\\\${chr})+`;
reg = new RegExp(r, 'g');
splitRegExps[chr] = reg;
}
const ret = (' ' +s).match(reg);
ret[0] = ret[0].substr(1);
return ret as string[];
}
function processTag(tag: string, outputArr: any[], values, fnInside){
if(tag.length === 0) return;
const tagWNumber = tag.split(numberDel);
const tagWONumber = tagWNumber[0];
const tagWAttributes = splitWithEscape(tagWONumber, '@');
const tagWOAttributes = tagWAttributes[0];
const tagWClasses = tagWOAttributes.split('.');
const tagWOClasses = tagWClasses[0];
const tagWID = tagWOClasses.split('#', 2);
const tagWOIDAndNoDiv = tagWID[0];
const idx = (tagWNumber.length > 1) ? parseInt(tagWNumber[1]) : -1;
const val = idx > -1 ? values[idx] : undefined;
if(tagWOIDAndNoDiv.length === 0){
if(tagWID.length === 1 && tagWClasses.length === 1 && tagWAttributes.length === 1){
if(typeof val === 'undefined') return;
}
}
const tagWOID = tagWOIDAndNoDiv ? tagWOIDAndNoDiv.trim() : 'div';
if(tagWOID.indexOf('<') === 0){
const identifier = 'global.' + tagWOID.substr(1);
const obj = eval(identifier);
obj['🏷'] = identifier;
obj['⚙'] = {
attribs: tagWAttributes.length > 1 ? tagWAttributes.slice(1) : null,
classList: tagWClasses.length > 1 ? tagWClasses.slice(1) : null,
};
outputArr.push(obj);
}else{
//#region string based tag
outputArr.push('<' + tagWOID);
if(tagWID.length > 1){
outputArr.push(` id="${tagWID[1]}"`);
}
if(tagWAttributes.length > 1){
const attribs = tagWAttributes.slice(1).map(s=> {
//const lhsRhs = s.match(scSplitRegExp);
const lhsRhs = splitWithEscape(s, ':');
const key = camelToSnake(lhsRhs[0]);
return lhsRhs.length === 1 ? key : `${key}="${lhsRhs[1]}"`;
}).join(' ');
outputArr.push(` ${attribs}`);
}
if(tagWClasses.length > 1){
outputArr.push(` class="${tagWClasses.slice(1).join(' ')}"`)
}
if(typeof val !== 'undefined'){
switch(typeof val){
case 'string':
outputArr.push('>');
outputArr.push(val);
break;
case 'object':
let props = val;
let content = null;
if(Array.isArray(val)){
throw "Not Implemented";
}
let loop;
let action;
for(const key in props){
const atV = props[key];
switch(key){
case '🎬':
action = atV;
break;
case '➰':
loop = atV;
break;
case 'innerHTML':
content = atV;
break;
//case 'each'
default:
switch(typeof atV){
case 'boolean':
if(atV){
outputArr.push(` ${camelToSnake(key)}`);
}
break;
default:
outputArr.push(` ${camelToSnake(key)}="${atV}"`);
}
}
}
outputArr.push('>');
if(loop && action){
switch(typeof loop){
case 'function':
const loopInfo : Loop<any> = {
'➰': loop,
'🎬':action,
}
outputArr.push(loopInfo);
break;
default:
for(const item of loop){
const output = action(item);
for(const oi of output){
outputArr.push(oi);
}
}
}
}
if(content){
outputArr.push(content);
}
break;
case 'function':
outputArr.push('>');
outputArr.push(val);
break;
}
}else{
outputArr.push('>');
}
if(fnInside) fnInside();
outputArr.push('</' + tagWOID + '>');
//#endregion
}
}
function processTags(tags: string[], outputArr: any[], values){
if(tags.length === 0) return;
let surroundingTag = tags.shift();
if(surroundingTag.indexOf('+') > -1){
const siblingTags = surroundingTag.split('+');
for(const tag of siblingTags){
processTag(tag, outputArr, values, null);
}
}else{
const innerFun = () => processTags(tags, outputArr, values);
processTag(surroundingTag, outputArr, values, innerFun)
}
}