-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoffle.js
More file actions
1334 lines (1113 loc) · 53.9 KB
/
toffle.js
File metadata and controls
1334 lines (1113 loc) · 53.9 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function toffle() {};
/**
* ToffleJS token types.
*/
toffle.tokenType = {
IF: function() {
this.type = "if";
this.condition = "";
this.tokens = [];
this.wrapsTokens = true;
},
NOT: function() {
this.type = "not";
this.condition = "";
this.tokens = [];
this.wrapsTokens = true;
},
REF: function() {
this.type = "ref";
this.value = "";
this.wrapsTokens = false;
},
TEMP: function() {
this.type = "template";
this.template = "";
this.params = "";
this.wrapsTokens = false;
},
EACH: function() {
this.type = "each";
this.pointer = "";
this.reference = "";
this.tokens = [];
this.wrapsTokens = true;
},
MATCH: function() {
this.type = "match";
this.pointer = "";
this.reference = "";
this.func = "";
this.tokens = [];
this.wrapsTokens = true;
},
CONTENT: function() {
this.type = "content";
this.value = "";
this.wrapsTokens = false;
},
HELPER: function() {
this.type = "helper";
this.arguments = "";
this.func = "";
this.tokens = [];
this.wrapsTokens = true;
},
CLS: function() {
this.type = "cls";
this.wrapsTokens = false;
}
};
/**
* Tokenifies our ToffleJS templates and returns an object containing our parsed template and any referenced templates, along
* with a number of functions that can generate our markup and wrap ajax calls to remotely fetch JSON datasets to use as input
* when generating output markup.
*/
toffle.template = function(template) {
// Reference for custom user helper functions.
var helperFunctions = {};
// Compiled Templates
var templates = [];
// Identifiers of templates that are currently being processed
var pendingTemplates = [];
// Check to see if we were passed a DOM element(our template) or an object(defines template object, helpers, etc...)
if (template) {
// check to see whether this is a DOM node (our template)
if (template.nodeType && template.nodeType === 1) {
// if our element is not a toffle-template type then were are not good to go
if (!(template.type == "text/toffle-template")) {
throw "toffle: template DOM element must be defined as type 'text/toffle-template'";
}
} else {
// ensure that the user has defined an intiial template
if (!('template' in template)) {
throw "toffle: error! you must provide an initial template";
}
// has the user defined any helper function, if so then grab them
if ('helpers' in template) {
// set each function on our 'helperFunctions' object for later use when were parsing our template body
for (var helper in template.helpers) {
helperFunctions[helper] = template.helpers[helper];
}
}
// set our actual template to be the one defined in the input object
template = template.template;
}
}
// Compile the template, and any subsequent referenced templates
toffle.compileTemplate(template, true, pendingTemplates, templates);
var returnObject = {
templates: {},
hlprFunctions: helperFunctions,
go: function(inputParams) {
// Our output.
var output = '';
// Our call stack. Is an array of token arrays.
var workStack = [];
// Our root context
var context = {
pos: 0,
tokens: [],
params: {}, // Contains template context params and variables that are set in the scope of this context.
pointer: null, // Only used in looping (The current item in iterative).
iterative: null, // Only used in looping (The object we are looping through).
counter: 1, // Only really for looping, 1 otherwise.
iterations: 1 // Only really for looping, 1 otherwise.
}
// Get the initial template and add its tokens into a root context.
for (var templateId in this.templates) {
var currentTemplate = this.templates[templateId];
if (currentTemplate.isInitialTemplate) {
// Add the root AST tokens to the context.
for (var i = 0; i < currentTemplate.ast.tokens.length; i++) {
// Push this token to our root context
context.tokens.push(currentTemplate.ast.tokens[i]);
}
// Add context params.
context.params = inputParams;
// Push this context onto the work stack.
workStack.push(context);
// We've found our initial template, break.
break;
}
}
// Now we have root context we can begin.
while (true) {
// Our current context.
var context;
// Get the current context, if we dont have one then we are done.
if (workStack.length == 0) {
break;
} else {
// Set the current context.
context = workStack[workStack.length - 1];
}
// If our position is on the last token in our context then we are done with this context. pop it from the stack.
if (context.pos == context.tokens.length) {
// We could have to repeat a context multiple times (looping).
// If this is true, and the context count is less than the number of iterations required
// to complete the loop, then we just increment the counter, reset the position and keep the context on the stack.
if (context.counter < context.iterations) {
// Increment the counter.
context.counter = context.counter + 1;
// Are we dealing with a match or else statement?
if (context.matchIndexes) {
// Set the context pointer to be the next object that matches
context.pointer = context.iterative[context.matchIndexes[context.counter - 1]];
} else {
// Set the context pointer to be the object at position 'context.counter - 1' of context.iterative
context.pointer = context.iterative[context.counter - 1];
}
// Reset the position.
context.pos = 0;
} else {
// Pop it from the stack.
workStack.pop();
// We have gotten rid of a context, continue to attempt to get another.
continue;
}
}
// Get the current token position.
var pos = context.pos;
// Get the token at the current position.
var token = context.tokens[context.pos];
// ------- Process Token ---------
switch (token.type) {
// If the token is just content then just spit it out.
case 'content':
output = output + token.value;
break;
// If the token is 'ref' then spit out evaluated value
case 'ref':
try {
var paramPool = this.getParamPool(workStack);
var idents = this.parseReference(token.value).idents;
var value = this.grabValue(paramPool, idents);
output = output + value;
} catch (err) {
throw "toffle: error evaluating value '" + token.value + "'";
}
break;
// If the token is 'if' then evaluate its condition, if true then raise context
case 'if':
// Evaluate the condition for this 'if'.
try {
var paramPool = this.getParamPool(workStack);
var idents = this.parseReference(token.condition).idents;
var value = this.grabValue(paramPool, idents);
var condition = value;
} catch (err) {
throw "toffle: error evaluating condition '" + token.condition + "'";
}
// If 'true' then push a new context onto the stack.
if (condition) {
workStack.push({
pos: 0,
tokens: token.tokens,
params: {},
pointer: null,
iterative: null,
counter: 1,
iterations: 1
});
}
break;
// If the token is 'not' then evaluate its condition, if false then raise context
case 'not':
// Evaluate the condition for this 'not'.
try {
var paramPool = this.getParamPool(workStack);
var idents = this.parseReference(token.condition).idents;
var value = this.grabValue(paramPool, idents);
var condition = value;
} catch (err) {
throw "toffle: error evaluating condition '" + token.condition + "'";
}
// If condition is false then push a new context onto the stack.
if (!condition) {
workStack.push({
pos: 0,
tokens: token.tokens,
params: {},
pointer: null,
iterative: null,
counter: 1,
iterations: 1
});
}
break;
// If the token is 'each' then raise context and set pointer
case 'each':
// Evaluate the reference and get it's length.
var ref = {};
var refLength = 0;
// Was this 'each' token added in 'goOver()'? if so then this token encompasses all others and iterates over the input as a whole
if (token.wrapsAllTokens) {
// The input MUST be an array
if (!(inputParams instanceof Array)) {
throw "toffle: cannot iterate over input dataset, not an array.";
}
// This 'each' token iterates over the input JSON as a whole (must me an array)
ref = inputParams;
refLength = ref.length;
} else {
try {
var paramPool = this.getParamPool(workStack);
var idents = this.parseReference(token.reference).idents;
var value = this.grabValue(paramPool, idents);
// The value MUST be an array
if (!(value instanceof Array)) {
throw "toffle: cannot iterate over '" + token.reference + "', not an array.";
}
var ref = value;
refLength = ref.length;
} catch (err) {
throw "toffle: error evaluating reference '" + token.reference + "'";
}
}
// Don't bother adding a context if we have no values
if (refLength > 0) {
// Push new context onto the stack.
workStack.push({
pos: 0,
tokens: token.tokens,
params: {},
pointer: ref[0],
pointerIdent: token.pointer,
iterative: ref,
counter: 1,
iterations: refLength
});
}
break;
// If the token is 'each' then raise context and set pointer
case 'match':
// Evaluate the reference and get it's length.
var ref = {};
var refLength = 0;
var matches = [];
try {
var paramPool = this.getParamPool(workStack);
var idents = this.parseReference(token.reference).idents;
var value = this.grabValue(paramPool, idents);
var ref = value;
} catch (err) {
throw "toffle: error evaluating reference '" + token.reference + "'";
}
for (var itemIndex = 0; itemIndex < ref.length; itemIndex++) {
// get the item
var item = ref[itemIndex];
// create argument array for our helper function
var args = [];
// helper functions used in match statements should always have their first argument being the current item to evaluate
args.push(item);
if (this.hlprFunctions[token.func]) {
// Call the helper function, passing the evaluated arguments, and get the boolean result.
var condition = this.hlprFunctions[token.func].apply(this, args);
// If condition is true then we have a match.
if (condition) {
// Increment the references length.
refLength++;
// Add this item index to array of idexes representing items that match.
matches.push(itemIndex);
}
} else {
throw "toffle: error! Helper function not specified for match: " + token.func;
}
}
// Don't bother adding a context if we have no values
if (refLength > 0) {
// Push new context onto the stack.
workStack.push({
pos: 0,
tokens: token.tokens,
params: {},
pointer: ref[matches[0]], // our first pointer should point to first MATCHING item
pointerIdent: token.pointer,
iterative: ref,
counter: 1,
matchIndexes: matches,
iterations: refLength
});
}
break;
// If the token is 'toffle' then raise context and do some stuff.
case 'template':
// Get the target template
var targetTemplate = this.templates[token.template];
// Create a new context for this template.
var templateContext = {
pos: 0,
tokens: [],
params: {},
pointer: {},
iterative: {},
counter: 1,
iterations: 1
};
// Add the new template tokens to the template context.
for (var i = 0; i < targetTemplate.ast.tokens.length; i++) {
// Push this token to our context.
templateContext.tokens.push(targetTemplate.ast.tokens[i]);
}
// Parse the template params and set them in the context.
templateContext.params = toffle.parseRawArgumentList(token.params.trim());
// need to get the user define identifiers for these paramaters (saved in the template div 'data-params' attribute)
var paramIdentifiers = document.getElementById(token.template).getAttribute("data-params");
// user defined template parameter identifiers are split using "|"
var paramIdentifierArray;
// Do we even have an identifiers?
if (paramIdentifiers) {
paramIdentifierArray = paramIdentifiers.split("|");
} else {
paramIdentifierArray = [];
}
// If the number of our arguments and parameter identifiers don't match then error
if (paramIdentifierArray.length != templateContext.params.length) {
throw "toffle: error! Template reference arguments do not match parameters.";
}
var paramIdentifierArrayCounter;
// Iterate over both the array of arguments and the array of identifers, link them, and set them on the current context.
for (paramIdentifierArrayCounter = 0; paramIdentifierArrayCounter < paramIdentifierArray.length; paramIdentifierArrayCounter++) {
var paramIdentifier = paramIdentifierArray[paramIdentifierArrayCounter];
var argument = templateContext.params[paramIdentifierArrayCounter];
// Now get the actual value of our argument
if (((typeof argument) === "boolean") || argument === null) {
// We have a boolean or a null value, do nothing
templateContext.params[paramIdentifier] = argument;
} else if ((argument.charAt(0) == "'" && argument.charAt(argument.length - 1) == "'") ||
(argument.charAt(0) == '"' && argument.charAt(argument.length - 1) == '"')) {
templateContext.params[paramIdentifier] = argument.substring(1, argument.length - 1);
} else if (!isNaN(argument)) {
// We have a number, set it
templateContext.params[paramIdentifier] = Number(argument);
} else {
// Get the current parameter pool
var paramPool = this.getParamPool(workStack);
// we must have a property accessor. get the value
var idents = this.parseReference(argument).idents;
var value = this.grabValue(paramPool, idents);
// Set the evaluated value
templateContext.params[paramIdentifier] = value;
}
}
// Push the template context onto the stack.
workStack.push(templateContext);
break;
case 'helper':
// Call the helper function (if it exists) passing the evaluated argument values.
// Helpers can only return true/false so treat this as a if/not context
// Check that there is a matching helper function.
if (this.hlprFunctions[token.func]) {
// Parse the helper arguments into an array of arguments, only if they have not already been parsed in an earlier call to go()
if (!(token.arguments instanceof Array)) {
token.arguments = toffle.parseRawArgumentList(token.arguments);
}
// Keep a list of evaluated argument values.
var evaluatedArguments = [];
// Iterate over each helper function argument and replace it with the actual evaluated value.
for (var argumentIndex = 0; argumentIndex < token.arguments.length; argumentIndex++) {
// Get the current argument
var currentArgument = token.arguments[argumentIndex];
// set each argument to be its evaluated value
if (((typeof currentArgument) === "boolean") || currentArgument === null) {
// We have a boolean or a null value, do nothing
evaluatedArguments[argumentIndex] = currentArgument;
} else if ((currentArgument.charAt(0) == "'" && currentArgument.charAt(currentArgument.length - 1) == "'") ||
(currentArgument.charAt(0) == '"' && currentArgument.charAt(currentArgument.length - 1) == '"')) {
evaluatedArguments[argumentIndex] = currentArgument.substring(1, currentArgument.length - 1);
} else if (!isNaN(currentArgument)) {
// We have a number, set it
evaluatedArguments[argumentIndex] = Number(currentArgument);
} else {
// we must have a property accessor. get the value
var paramPool = this.getParamPool(workStack);
var idents = this.parseReference(currentArgument).idents;
var value = this.grabValue(paramPool, idents);
// Set the evaluated value
evaluatedArguments[argumentIndex] = value;
}
}
// Call the helper function, passing the evaluated arguments, and get the boolean result.
var condition = this.hlprFunctions[token.func].apply(this, evaluatedArguments);
// If condition is true then push a new context onto the stack.
if (condition) {
workStack.push({
pos: 0,
tokens: token.tokens,
params: {},
pointer: null,
iterative: null,
counter: 1,
iterations: 1
});
}
} else {
throw "toffle: error! Helper function not specified: " + token.func;
}
break;
// Token type is not recognised. Throw error.
default:
throw "toffle: error! Unknown token type: " + token.type;
}
// -------------------------------
// Increment the position.
context.pos = context.pos + 1;
}
return output;
},
/**
* Carries out a get/post ajax call to asynchronously remotely fetch JSON from elsewhere, compile it, process the templates
* and plug the output to a location (dropoff value in details parameter, if specified) when done. Users can also add a 'finished'
* callback which we will pass the template output and fetched JSON to.
*/
goAway: function(details) {
// check that we have been given our details object
if (!details) {
throw "toffle: error! you must specify details for this function";
}
// check that the user specified a url
if (!('url' in details)) {
throw "toffle: error! no url specified";
}
// check that the user specified a url
if (!('type' in details)) {
throw "toffle: error! no type specified";
}
// do our ajax request
try {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// get a reference to this so we can reach 'handleAjaxError' in 'onreadystatechange'
var returnObj = this;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// our response as JSON
var responseJSON;
// attempt to parse our response text as JSON
try {
// parse the response
responseJSON = JSON.parse(xmlhttp.responseText);
} catch (err) {
returnObj.handleAjaxError(details, err, "toffle: error parsing returned data as JSON");
return;
}
// check the details for a 'parse' function, if one is there then pass it the raw json.
if ('parse' in details) {
// wrap this in a try, in case the user was naughty and didn't pass us a valid function
try {
// parse the JSON, the user will have to return either true or false. true to continue, false to abort.
var continueProcessing = details.parse(responseJSON);
// has the user requested that the operation be aborted.
if (!continueProcessing) {
return;
}
} catch (err) {
returnObj.handleAjaxError(details, err, "toffle: Error parsing JSON.");
return;
}
}
// our template output
var templateOutput;
// now lets try to actually generate some HTML
try {
templateOutput = returnObj.go(responseJSON);
} catch (err) {
returnObj.handleAjaxError(details, err, "toffle: error generating output");
return;
}
// if the user has specified a dropoff, then fill that container with our template output
if ('dropoff' in details) {
// wrap this in a try, in case the user was naughty and didn't pass us a valid object
try {
details.dropoff.innerHTML = templateOutput;
} catch (err) {
returnObj.handleAjaxError(details, err, "toffle: error! invalid dropoff");
return;
}
}
// everything seems to have went well. If the user specified a 'finished' method then call it
if ('finished' in details) {
// pass our user the json with which we generated the output, and the output itself
details.finished({
responseJSON: responseJSON,
output: templateOutput
});
}
}
}
xmlhttp.open(details.type, details.url, true);
if ('data' in details && details.type.toUpperCase() == 'POST') {
xmlhttp.send(details.data);
} else {
xmlhttp.send();
}
} catch (err) {
this.handleAjaxError(details, err, "toffle: error carrying out ajax request");
}
},
/**
* A convenience method. Assumes that the input JSON is , at the top level, an array. Iterates over this array
* rather than the user having to add an 'each' statement to the initial template.
*/
goOver: function(inputJSON, pointerIdentifier) {
// The initial template
var intlTemplate;
// Find our initial template as this will be encapsulated in an 'each' token.
for (var templateName in this.templates) {
if (this.templates.hasOwnProperty(templateName)) {
var temp = this.templates[templateName];
// Check to see if this is the initial template
if (temp.isInitialTemplate) {
// We found our initial template
intlTemplate = temp;
// Done searching
break;
}
}
}
// Get a new 'each' token
wrappingEachtoken = new toffle.tokenType.EACH();
// Set the pointer
wrappingEachtoken.pointer = pointerIdentifier;
// Set the reference
wrappingEachtoken.reference = "WILLNOTWORK";
// Set a flag that will let Toffle know that this token encompasses all other tokens
wrappingEachtoken.wrapsAllTokens = true;
// Wrap the initial template's root tokens with our 'each' token
wrappingEachtoken.tokens = intlTemplate.ast.tokens;
// Set the 'each' token as our sole root token in the intial template
intlTemplate.ast.tokens = [wrappingEachtoken];
// Call go() and return the output
return this.go(inputJSON);
},
/**
* Takes a parsed user defined JSON reference and an object containing our accessible data objects and
* carries out an eval-less get for the value.
*/
grabValue: function(params, ref) {
// get data object from params
var currentIdent = params;
// if we hit an undefined value, return it
if (currentIdent == undefined) {
return undefined;
}
for (var i = 0; i < ref.length; i++) {
var indexIdents = ref[i].sub;
if (indexIdents.length > 0) {
// recursively call grabValue to determine our index value
currentIdent = currentIdent[ref[i].name][this.grabValue(params, ref[i].sub)];
// check if we have trailing square bracket property accessors.
if (ref[i].subTrail) {
// if we do, recursively call grabValue() to reach our value
for (var s = 0; s < ref[i].subTrail.length; s++) {
// get sub
var sub = ref[i].subTrail[s];
// get next value
currentIdent = currentIdent[grabValue(params, sub)];
}
}
} else {
// set the current value
currentIdent = currentIdent[ref[i].name];
}
// if we hit an undefined value, return it
if (currentIdent == undefined) {
return undefined;
}
}
// return the target value
return currentIdent;
},
/**
* Parses a user defined JSON reference into a tree structure of identifiers that make up a property accessor.
*/
parseReference: function(input) {
// a count of the characters iterated over in 'input'
var charCount = 0;
// our array of identifiers
var idents = [];
// current identifier
var currentIdent = '';
// iterate over all the characters in our input and process the accordingly
for (var i = 0; i < input.length; i++) {
// geth the character the the current position
var ch = input.charAt(i);
if (ch == '.') {
// the '.' defines the seperation of scope, record 'currentIdent' as an identifier
if (currentIdent.length > 0) {
idents.push({
name: currentIdent.trim(),
sub: []
});
}
// reset the identifier
currentIdent = '';
} else if (ch == '[') {
// The '[' defines that we are dealing with the identifier for an array and we have hit the squre brackets wrapping its index
// first of all, we have a vaild identifier, ONLY if it is not empty, if it is then this index is directly following another e.g. 'me[details][age]'
if (currentIdent.length > 0) {
idents.push({
name: currentIdent.trim(),
sub: []
});
}
// reset the identifier
currentIdent = '';
// check to see if the user wants to use a literal string identifier '[[some.pants#but/valid.json@identifier]]'
if (input.charAt(i + 1) == '[') {
var literalClosed = false;
// loop through the input to find the closing ']]'
for (var x = (i + 2); x < (input.length - 1); x++) {
// we have found the closing ']]'
if (input.substring(x, x + 2) == ']]') {
literalClosed = true;
idents.push({
name: input.substring(i + 2, x),
sub: [],
literal: true
});
break;
}
}
if (!literalClosed) {
throw "toffle: No closing ']]' for literal identifier";
}
charCount += (x - i) + 1;
i += (x - i) + 1;
} else {
// recursively call this function to parse the reference in the square brackets
var subIdents = this.parseReference(input.substring(i + 1));
if (idents[idents.length - 1].sub.length == 0) {
// set the index reference identifiers on the current identifier
idents[idents.length - 1].sub = subIdents.idents;
} else {
if (!idents[idents.length - 1].subTrail) {
idents[idents.length - 1].subTrail = [];
idents[idents.length - 1].subTrail.push(subIdents.idents);
} else {
idents[idents.length - 1].subTrail.push(subIdents.idents);
}
}
// we have to increment our count by the amount of characters processed in our 'parseReference()' call
charCount += subIdents.count;
i += subIdents.count;
}
} else if (ch == ']') {
// the ']' defines that we are dealing with the identifier for an array and we have hit the closing square brackets wrapping its index
// first of all, if we have a 'currentIdent' then add it before we pass our gathered identifiers back to the caller.
if (currentIdent.length > 0) {
idents.push({
name: currentIdent.trim(),
sub: []
});
}
return {
idents: idents,
count: charCount + 1
};
} else {
// add the current character to our current identifier
currentIdent = currentIdent + ch;
}
charCount++;
}
// if we have a value for 'currentIdent' then add it as the last identifier will not be followed by a '.'
if (currentIdent.length > 0) {
idents.push({
name: currentIdent.trim(),
sub: []
});
}
// add the length of our trailing identifer to the number of characters processed
charCount += currentIdent.length;
return {
idents: idents,
count: charCount
};
},
/**
* Returns an object containing all data items that are available to the current stack context.
*/
getParamPool: function(stack) {
var pool = {};
// go up through the contexts, from root (initial template) to the context that has the current scope
for (var contextIndex = 0; contextIndex < stack.length; contextIndex++) {
// get the current context
var context = stack[contextIndex];
// add the parameters to the pool
for (var key in context.params) {
pool[key] = context.params[key];
}
// if we are dealing with an 'each' context we will need to add the pointer reference
if (context.pointerIdent) {
pool[context.pointerIdent] = context.pointer;
}
}
return pool;
},
/**
* Convenience method for handling Ajax Errors.
*/
handleAjaxError: function(details, error, message) {
// something went wrong, call the user specified 'failed' function, if there is one.
if ('failed' in details) {
details.failed(error);
} else {
// If the user doesnt want to handle this by specifying a 'failed' callback, then just error.
throw message;
}
}
}
// Add our templates to our returning object.
for (var i = 0; i < templates.length; i++) {
returnObject.templates[templates[i].name] = {
ast: templates[i].temp.AST,
isInitialTemplate: templates[i].temp.initialTemplate,
varList: []
};
}
return returnObject;
};
/**
* Compiles a single ToffleJS template.
*/
toffle.compileTemplate = function(template, initialTemplate, pendingTemplates, templates) {
// Ensure that this template even exists in the document.
if (template == null) {
throw "toffle: Compilation failed! Could not find Template.";
}
var currentTemplate = {
initialTemplate: initialTemplate,
tokens: [],
openTokens: [],
AST: {},
templateString: template.innerHTML
};
// Add this templates id to the list of pending compilations as reference for when we wish to compile sub-referenced templates.
pendingTemplates.push(template.id);
// Flag that defines when we've found an opening marker but not its accompanying closing marker.
var isTokenUnclosed = false;
// Variable to store template content.
var content = "";
// Tokenise our template
for (var i = 0; i < (currentTemplate.templateString.length); i++) {
var expectingOpeningToken = false;
if (currentTemplate.templateString.slice(i, i + 2) == '<^') {
if (content.length > 0) {
var contentToken = new toffle.tokenType.CONTENT();
contentToken.value = content;
currentTemplate.tokens.push(contentToken);
content = "";
}
currentTemplate.openTokens.push(i);
isTokenUnclosed = true;
} else if (currentTemplate.templateString.slice(i, i + 2) == '^>') {
// Iterate over closing marker.
i += 2;
// Are we expecting an open token on the next iteration.
if (currentTemplate.templateString.slice(i, i + 2) == '<^') {
expectingOpeningToken = true;
}
isTokenUnclosed = false;
var openingTokenIndex = currentTemplate.openTokens.pop();
var newToken = currentTemplate.templateString.slice(openingTokenIndex, i);
// Parse the plain text token into an uppidt token.
newToken = toffle.tokenify(newToken, template.id, pendingTemplates, templates);
newToken.tokenIndex = i - openingTokenIndex;
// Add the token to the template.
currentTemplate.tokens.push(newToken);
}
// If we are expecting an opening token on the next iteration. Therefore we wont be appending to
// content and need to decrement 'i' to make sure we dont eat the opening '<' of the next token.
if(expectingOpeningToken) {
i = i-1;