author | Dan Fuhry <dan@enanocms.org> |
Mon, 24 Nov 2014 11:40:24 -0500 | |
changeset 1380 | 0e400f946644 |
parent 1311 | a228f7e8fb15 |
permissions | -rw-r--r-- |
1 | 1 |
// An implementation of Enano's template compiler in Javascript. Same exact API |
2 |
// as the PHP version - constructor accepts text, then the assign_vars, assign_bool, and run methods. |
|
3 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
4 |
window.templateParser = function(text) |
1 | 5 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
6 |
this.tpl_code = text; |
1311
a228f7e8fb15
Added a new "hide-with-mce" CSS class in Dynano; all elements which have it will be hidden when TinyMCE is activated, and re-shown when it is destroyed.
Dan Fuhry <dan@enanocms.org>
parents:
1291
diff
changeset
|
7 |
this.tpl_strings = {}; |
a228f7e8fb15
Added a new "hide-with-mce" CSS class in Dynano; all elements which have it will be hidden when TinyMCE is activated, and re-shown when it is destroyed.
Dan Fuhry <dan@enanocms.org>
parents:
1291
diff
changeset
|
8 |
this.tpl_bool = {}; |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
9 |
this.assign_vars = __tpAssignVars; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
10 |
this.assign_bool = __tpAssignBool; |
1269
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
11 |
this.fetch_hook = __tpFetchHook; |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
12 |
this.run = __tpRun; |
1 | 13 |
} |
14 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
15 |
window.__tpAssignVars = function(vars) |
1 | 16 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
17 |
for(var i in vars) |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
18 |
{ |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
19 |
this.tpl_strings[i] = vars[i]; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
20 |
} |
1 | 21 |
} |
22 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
23 |
window.__tpAssignBool = function(vars) |
1 | 24 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
25 |
for(var i in vars) |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
26 |
{ |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
27 |
this.tpl_bool[i] = ( vars[i] ) ? true : false; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
28 |
} |
1 | 29 |
} |
30 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
31 |
window.__tpRun = function() |
1 | 32 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
33 |
if(typeof(this.tpl_code) == 'string') |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
34 |
{ |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
35 |
tpl_code = __tpCompileTemplate(this.tpl_code); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
36 |
try { |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
37 |
compiled = eval(tpl_code); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
38 |
} |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
39 |
catch(e) |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
40 |
{ |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
41 |
alert(e); |
1280
871f17a0d27d
Improved display of comments from foes, and fixed some general issues with the friend and foe lists
Dan Fuhry <dan@enanocms.org>
parents:
1269
diff
changeset
|
42 |
load_component('acl'); |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
43 |
aclDebug(tpl_code); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
44 |
} |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
45 |
return compiled; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
46 |
} |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
47 |
return false; |
1 | 48 |
} |
49 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
50 |
window.__tpCompileTemplate = function(code) |
1 | 51 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
52 |
// Compile plaintext/template code to javascript code |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
53 |
code = code.replace(/\\/g, "\\\\"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
54 |
code = code.replace(/\'/g, "\\'"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
55 |
code = code.replace(/\"/g, '\\"'); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
56 |
code = code.replace(new RegExp(unescape('%0A'), 'g'), '\\n'); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
57 |
code = "'" + code + "'"; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
58 |
code = code.replace(/\{([A-z0-9_-]+)\}/ig, "' + this.tpl_strings['$1'] + '"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
59 |
code = code.replace(/\{lang:([a-z0-9_]+)\}/g, "' + $lang.get('$1') + '"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
60 |
code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '$3' ) + '"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
61 |
code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '' ) + '"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
62 |
code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '$3' ) + '"); |
1291
a971f6efcb7b
Visual refresh on the textarea in the enanium page editor. Also fixed missing image on the Save Draft button.
Dan Fuhry <dan@enanocms.org>
parents:
1280
diff
changeset
|
63 |
code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( Boolean(this.tpl_bool['$1']) == true ) ? '$2' : '' ) + '"); |
a971f6efcb7b
Visual refresh on the textarea in the enanium page editor. Also fixed missing image on the Save Draft button.
Dan Fuhry <dan@enanocms.org>
parents:
1280
diff
changeset
|
64 |
code = code.replace(/\<!-- BEGINNOT ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( Boolean(this.tpl_bool['$1']) == false ) ? '$2' : '' ) + '"); |
1269
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
65 |
code = code.replace(/\<!-- HOOK ([A-z0-9_-]+) --\>/ig, "' + this.fetch_hook('$1') + '"); |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
66 |
return code; |
1 | 67 |
} |
68 |
||
582
a38876c0793c
Majorly reworked Javascript runtime stuff to use on-demand loading.
Dan
parents:
212
diff
changeset
|
69 |
window.__tpExtractVars = function(code) |
1 | 70 |
{ |
1227
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
71 |
code = code.replace('\\', "\\\\"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
72 |
code = code.replace("'", "\\'"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
73 |
code = code.replace('"', '\\"'); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
74 |
code = code.replace(new RegExp(unescape('%0A'), 'g'), "\\n"); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
75 |
code = code.match(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
76 |
code2 = ''; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
77 |
for(var i in code) |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
78 |
if(typeof(code[i]) == 'string') |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
79 |
code2 = code2 + code[i]; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
80 |
code = code2.replace(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g, "'$1' : \"$2\","); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
81 |
code = '( { ' + code + ' "________null________" : false } )'; |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
82 |
vars = eval(code); |
bdac73ed481e
Going ahead with the switch to tabs. This is a major coding standards change! If any unusual parser bugs show up, check this changeset. Converted all .php, .js, .tpl, .css, and .json files and did basic testing.
Dan
parents:
832
diff
changeset
|
83 |
return vars; |
1 | 84 |
} |
85 |
||
1269
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
86 |
window.__tpFetchHook = function(hookid) |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
87 |
{ |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
88 |
var _ob = ''; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
89 |
window.Template = this; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
90 |
window.Echo = function(h) |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
91 |
{ |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
92 |
_ob += h; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
93 |
} |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
94 |
eval(setHook('thook_' + hookid)); |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
95 |
window.Echo = window.Template = false; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
96 |
return _ob; |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
97 |
} |
35986c6b2150
Added template hooks in the JS template compiler. Attach to thook_<template hook name>. Use Echo() to echo HTML; access compiler instance with Template.tpl_{strings,bool}, etc.
Dan
parents:
1227
diff
changeset
|
98 |