/
autoglobalize.js
97 lines (71 loc) · 1.83 KB
/
autoglobalize.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
App = Ember.Application.create();
App.Router.map(function() {
// put your routes here
});
var templateModel=Ember.Object.extend({
input:'',
output:'',
helper:'loc',
});
App.IndexRoute = Ember.Route.extend({
model: function() {
return templateModel.create();
}
});
App.IndexController=Ember.ObjectController.extend({
localize:function(text){
var val=$.trim(text);
if(val && !this.isHandlebarExpression(val))
return "{{loc "+text+"}}";
return text;
},
isHandlebarExpression:function(text){
// pass trimmed text
if(/^[{{]/.test(text)){
return true;
}
return false;
},
replace:function (node) {
if(node.nodeType==node.Text_NODE){
node.textContent=this.location(node.textContent);
}
var nodes = node.childNodes;
for (var i = 0, m = nodes.length; i < m; i++) {
var n = nodes[i];
if (n.nodeType == n.TEXT_NODE) {
// do some swappy text to html here?
n.textContent = this.localize(n.textContent);
} else {
this.replace(n);
}
}
},
resetEmberAttributes:function(html){
html=html.split('action=""').join('action');
html=html.split('=""').join('');
return html;
},
process:function(input){
var nodes=$.parseHTML(input);
nodes.forEach($.proxy(function(val){
this.replace(val);
},this));
$('#jappendHtml').empty();
var m=$('#jappendHtml').append(nodes);
return this.resetEmberAttributes(m[0].innerHTML);
},
actions:{
globalize:function(){
//console.log(Handlebars.compile(this.get('input'))());
if(this.get('helper')){
this.set('showError',false);
var output=this.process(this.get('input'));
this.set('output',output);
}
else{
this.set('showError',true);
}
}
}
});