-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprint.js
380 lines (325 loc) · 11 KB
/
sprint.js
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
Sprint
Prototype Stage
By Shivank Kacker
Work in progress
*/
const sprint = {
element_identifier : "__sprint",
element_dev_identifier : "__sprint_dev",
load : function(url, cacheName){
$.ajax({
async: false,
type: 'GET',
cache: false,
url: url+'.spr',
success: function(data) {
sprint.cache[cacheName] = data;
}
});
},
lastRefreshData : [],
lastParseData : [],
checkRefresh: (url) => {
const newId = sprint.newId();
let flag = false;
$.ajax({
async: false,
type: 'GET',
cache: false,
url: url+'.spr',
success: function(data) {
if(sprint.lastRefreshData[url] != data){
sprint.lastRefreshData[url] = data;
sprint.cache[newId] = data;
flag = true;
}
}
});
if(flag){
return sprint.parse(sprint.cache[newId]);
}
//startRefreshes();
},
createSandBox: () => {
const aID = sprint.sandBoxID;
if($('#'+aID).length > 0){
$('')
}
$('body').append(`
<div id="${aID}" style="display:none;">
</div>
`);
return true;
},
terminateSandBox: () => {
$(`#${sprint.sandBoxID}`).remove();
return true;
},
sandBoxID : '__sprintSandBox',
cache : {},
parsed : {},
parsedKeys : [],
indentation : {
space : ' ',
spaceNum : 4,
indent : function(){
return sprint.indentation.space.repeat(sprint.indentation.spaceNum)
}
},
parse : function(data){
let divideLB = data.split(`\r\n`);
let lastLevel = 0;
let lastEleID = '';
sprint.createSandBox();
divideLB.forEach((l, li) => {
//$('body').append(l);
let indent = sprint.indentation.indent();
let spaceNum = sprint.indentation.spaceNum;
let preceeding = l.search(/\S/);
if (!l.replace(/\s/g, '').length) {
return;
}
let inLevel = preceeding / spaceNum;
if(preceeding % spaceNum != 0){
console.error('Parse Error: Invalid indentation on line '+ (li+1));
return false;
}
let appendEle = [];
const eleID = sprint.newId(); //Giving each node a unique ID
let toAppend = ``;
if(l.startsWith(indent.repeat(inLevel)+':')){ //New Element
let eString = l.split(':')[1];
let element = eString.split(' ')[0];
let props = l.split(element+' ')[1];
let cont = {};
if(typeof props == 'undefined'){
props = '';
}
sprint.shortElements.forEach(s => {
if(element.includes(s.identifier)){
if(element.startsWith(s.identifier)){
props += ' '+s.property+'="'+element.replace(s.identifier, '')+'"';
element = 'div';
}else{
let eleSplit = element.split(s.identifier);
props += ' '+s.property+'="'+eleSplit[1]+'"';
element = eleSplit[0];
}
return false;
}
});
if(element == 'title'){
cont = props;
props = '';
}
let parsedProps = sprint.parseProps(props);
appendEle = {
"__element" : element,
"__properties" : parsedProps,
"__content" : cont,
"__level" : inLevel
};
toAppend = `
<${appendEle.__element} ${appendEle.__properties} __sprintID = ${eleID} __sprintLevel = ${inLevel}>
</${appendEle.__element}>
`;
}else{ //Is string
let cont = l.replace(indent.repeat(inLevel),'');
appendEle = {
"__element" : "string",
"__properties" : '',
"__content" : cont,
"__level" : inLevel
};
toAppend = `<span __sprintID="${eleID}" __sprintString>${appendEle.__content}</span>`;
}
sprint.parsed[eleID] = appendEle;
if(lastEleID == ''){
$(`#${sprint.sandBoxID}`).append(toAppend);
lastEleID = eleID;
}else{
let lastEle = $(`[__sprintID = "${lastEleID}"]`);
if(lastLevel < inLevel){ //Is a child element
lastEle.append(toAppend);
lastEleID = eleID;
}else if(lastLevel > inLevel){ //Is a parent element
let pointer = lastEle.parent();
let diff = lastLevel - inLevel;
for(i = 0; i < diff; i++){
pointer = pointer.parent();
}
pointer.append(toAppend);
lastEleID = eleID;
}else{ //Is in the same heirarchy
lastEle.parent().append(toAppend);
lastEleID = eleID;
}
}
lastLevel = inLevel;
});
let sandBox = $(`#${sprint.sandBoxID}`);
let sand = sandBox.html();
sandBox.find('[__sprintID]').each(function(){
if($(this).is('span') && $(this).hasAttr('__sprintString')){
const strCont = $(this).html();
let eID = $(this).attr('__sprintID');
//console.log(strCont);
//console.log(sandBox.html());
sandBox.html(sand.replace(`<span __sprintid="${eID}" __sprintstring="">${strCont}</span>`,strCont));
}
});
sandBox.find('[__sprintID]').each(function(){
$(this).removeAttr('__sprintID').removeAttr('__sprintLevel').removeAttr('undefined').removeAttr('__sprintString');
});
sand = sandBox.html();
sprint.terminateSandBox();
return sand;
},
get: function(fName){
const newId = sprint.newId();
sprint.load(fName,newId);
return sprint.parse(sprint.cache[newId]);
},
shortElements : [
{
identifier : '.',
property : 'class',
},
{
identifier : '#',
property : 'id',
},
{
identifier : '<',
property : '__sprint_templated_element',
},
{
identifier : '/',
property : '__sprint_template_element',
}
],
newId : function(){
return Math.random().toString(16).slice(2);
},
parseProps : (props) => {
if(props == ''){
return props;
}
//Magic part begins
const regex = /([^\r\n\t\f\v= '"]+)(?:=(["'])?((?:.(?!\2?\s+(?:\S+)=|\2))+.)\2?)?/gm;
const str = `<sample sample="" ${props}></sample>`;
let m;
let propA = [];
let propB = [];
let propF = [];
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
if(groupIndex == 3 || groupIndex == 1){
//console.log(`Found match, group ${groupIndex}: ${match}`);
}
if(groupIndex == 1){
propA.push(match);
}else if(groupIndex == 3){
propB.push(match);
}
});
}
propA.forEach((e,i) => {
if(i == 0 || i==1 || i == (propA.length - 1)){
return;
}
let property = e;
let value = propB[i];
propF[property] = value;
});
//magic part ends
//console.log(propF);
var hasStyle = checkValue(propF.style);
if(hasStyle){
if(propF.style != '' && propF.style.endsWith(";") == false){
propF.style += ';';
}
}else{
propF.style = '';
}
sprint.customStyleProperties.forEach(e => {
if(checkValue(propF[e.prop])){
//console.log(propF.style);
//console.log(e.sval);
propF.style += e.sval + ":" + propF[e.prop] + ';';
delete propF[e.prop];
}
});
//console.log(propF);
let propString = '';
if(propF.style == ''){
delete propF.style;
}
for (var key in propF) {
let val = propF[key];
propString += key + '=' + '"'+ val +'" ';
}
//console.log(propString);
return propString;
},
customStyleProperties : [
{
"prop" : "color",
"sval" : "color"
},
{
"prop" : "font-size",
"sval" : "font-size"
},
{
"prop" : "bg",
"sval" : "background"
},
{
"prop" : "height",
"sval" : "height"
},
{
"prop" : "width",
"sval" : "width"
}
]
}
let checkValue = (val) => (typeof val != 'undefined');
$.fn.hasAttr = function(name) {
return this.attr(name) !== undefined;
};
$(window).on('load',()=>{
const identifier = sprint.element_identifier;
const dev_identifier = sprint.element_dev_identifier;
const dev_elements = $(`[${dev_identifier}]`);
$(`[${identifier}]`).each(function(){
let url = $(this).attr(identifier);
$(this).html(
sprint.get(url)
);
});
if(dev_elements.length){
console.warn('You are fetching Sprint components in dev mode. This is not suitable for production. Change the "__sprint_dev" attribute to "__sprint" to use for production');
}
startRefreshes();
});
const startRefreshes = () => {
setInterval(() => {
let dev_identifier = sprint.element_dev_identifier;
let dev_elements = $(`[${dev_identifier}]`).not('[__sprint_init]');
dev_elements.each(function(){
let url = $(this).attr(dev_identifier);
//$(this).attr('__sprint_init','');
$(this).html(
sprint.checkRefresh(url)
);
});
}, 100);
}