tr8n 3.2.0 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/Gemfile.lock +3 -1
  2. data/app/assets/javascripts/tr8n/tr8n-compiled.js +336 -0
  3. data/app/assets/stylesheets/tr8n/admin.css.scss +23 -0
  4. data/app/assets/stylesheets/tr8n/components.css.scss +23 -0
  5. data/app/assets/stylesheets/tr8n/keyboard_1_49.css +271 -0
  6. data/app/assets/stylesheets/tr8n/layout.css.scss +23 -0
  7. data/app/assets/stylesheets/tr8n/tr8n.css.scss +1 -1
  8. data/app/controllers/tr8n/api/v1/base_controller.rb +6 -0
  9. data/app/controllers/tr8n/api/v1/language_controller.rb +7 -10
  10. data/app/controllers/tr8n/api/v1/translation_controller.rb +0 -1
  11. data/app/javascript/compile.rb +37 -0
  12. data/app/javascript/compile.sh +1 -0
  13. data/app/javascript/compressors/google/COPYING +202 -0
  14. data/app/javascript/compressors/google/README +292 -0
  15. data/app/javascript/compressors/google/compiler.jar +0 -0
  16. data/app/javascript/config.yml +66 -0
  17. data/app/javascript/src/base.js +35 -0
  18. data/app/javascript/src/effects.js +67 -0
  19. data/app/javascript/src/ext/inflector.js +309 -0
  20. data/app/{assets/javascripts/tr8n → javascript/src/ext}/jsDraw2D.js +0 -0
  21. data/app/{assets/javascripts/tr8n → javascript/src/ext}/keyboard_1_36.js +0 -0
  22. data/app/{assets/javascripts/tr8n → javascript/src/ext}/keyboard_1_44.js +0 -0
  23. data/app/javascript/src/ext/keyboard_1_49.js +1796 -0
  24. data/app/javascript/src/ext/md5.js +209 -0
  25. data/app/{assets/javascripts/tr8n → javascript/src/ext}/shortcut.js +0 -0
  26. data/app/javascript/src/init.js +47 -0
  27. data/app/javascript/src/language.js +38 -0
  28. data/app/javascript/src/logger.js +203 -0
  29. data/app/{assets/javascripts/tr8n/tr8n_prototype_effects.js → javascript/src/prototype/effects.js} +5 -5
  30. data/app/javascript/src/proxy.js +254 -0
  31. data/app/javascript/src/rules/date_rule.js +37 -0
  32. data/app/javascript/src/rules/gender_list_rule.js +37 -0
  33. data/app/javascript/src/rules/gender_rule.js +87 -0
  34. data/app/javascript/src/rules/language_rule.js +55 -0
  35. data/app/javascript/src/rules/list_rule.js +37 -0
  36. data/app/javascript/src/rules/numeric_rule.js +95 -0
  37. data/app/javascript/src/tml/label.js +60 -0
  38. data/app/javascript/src/tml/token.js +107 -0
  39. data/app/javascript/src/tokens/data_token.js +45 -0
  40. data/app/javascript/src/tokens/decoration_token.js +82 -0
  41. data/app/javascript/src/tokens/token.js +121 -0
  42. data/app/javascript/src/tokens/transform_token.js +106 -0
  43. data/app/javascript/src/translation_key.js +183 -0
  44. data/app/javascript/src/ui/language_case_manager.js +155 -0
  45. data/app/javascript/src/ui/language_selector.js +126 -0
  46. data/app/javascript/src/ui/lightbox.js +72 -0
  47. data/app/javascript/src/ui/translator.js +257 -0
  48. data/app/javascript/src/utils.js +286 -0
  49. data/app/models/tr8n/language_case.rb +27 -16
  50. data/app/models/tr8n/language_case_rule.rb +24 -7
  51. data/app/models/tr8n/translation_key.rb +23 -21
  52. data/app/views/tr8n/common/_scripts.html.erb +8 -10
  53. data/app/views/tr8n/language_cases/_manager_header.html.erb +3 -3
  54. data/app/views/tr8n/language_cases/manager.html.erb +2 -2
  55. data/app/views/tr8n/translations/_original_phrase.html.erb +1 -1
  56. data/lib/tr8n/extensions/action_view_extension.rb +8 -1
  57. data/lib/tr8n/keyboard_mapping.rb +101 -0
  58. data/lib/tr8n/token.rb +1 -2
  59. data/lib/tr8n/version.rb +1 -1
  60. data/spec/models/tr8n/gender_rule_spec.rb +6 -6
  61. data/spec/models/tr8n/language_case_rule_spec.rb +219 -1
  62. data/spec/models/tr8n/language_case_spec.rb +51 -1
  63. data/spec/models/tr8n/translation_key_spec.rb +256 -0
  64. data/tr8n.gemspec +2 -1
  65. metadata +129 -47
  66. data/app/assets/javascripts/tr8n/tr8n.js +0 -905
  67. data/app/assets/javascripts/tr8n/tr8n_client_sdk.js +0 -1472
@@ -0,0 +1,37 @@
1
+ /****************************************************************************
2
+ Copyright (c) 2010-2012 Michael Berkovich, Ian McDaniel, tr8n.net
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ ****************************************************************************/
23
+
24
+ Tr8n.Proxy.ListRule = function(definition, options) {
25
+ this.definition = definition;
26
+ this.options = options;
27
+ }
28
+
29
+ Tr8n.Proxy.ListRule.prototype = new Tr8n.Proxy.LanguageRule();
30
+
31
+ Tr8n.Proxy.ListRule.transform = function(object, values) {
32
+ return "";
33
+ }
34
+
35
+ Tr8n.Proxy.ListRule.prototype.evaluate = function(token, token_values) {
36
+ return true;
37
+ }
@@ -0,0 +1,95 @@
1
+ /****************************************************************************
2
+ Copyright (c) 2010-2012 Michael Berkovich, Ian McDaniel, tr8n.net
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ ****************************************************************************/
23
+
24
+ Tr8n.Proxy.NumericRule = function(definition, options) {
25
+ this.definition = definition;
26
+ this.options = options;
27
+ }
28
+
29
+ Tr8n.Proxy.NumericRule.prototype = new Tr8n.Proxy.LanguageRule();
30
+
31
+ // English based transform method
32
+ // FORM: [singular, plural]
33
+ // {count | message, messages}
34
+ // {count | person, people}
35
+ Tr8n.Proxy.NumericRule.transform = function(count, values) {
36
+ if (count == 1) return values[0];
37
+ if (values.length == 2) {
38
+ return values[1];
39
+ }
40
+ return values[0].pluralize();
41
+ }
42
+
43
+ Tr8n.Proxy.NumericRule.prototype.evaluate = function(token_name, token_values){
44
+ // "count":{"value1":"2,3,4","operator":"and","type":"number","multipart":true,"part2":"does_not_end_in","value2":"12,13,14","part1":"ends_in"}
45
+
46
+ var object = this.getTokenValue(token_name, token_values);
47
+ if (object == null) return false;
48
+
49
+ var token_value = null;
50
+ if (typeof object == 'string' || typeof object == 'number') {
51
+ token_value = "" + object;
52
+ } else if (typeof object == 'object' && object['subject']) {
53
+ token_value = "" + object['subject'];
54
+ } else {
55
+ this.getLogger().error("Invalid token value for numeric token: " + token_name);
56
+ return false;
57
+ }
58
+
59
+ this.getLogger().debug("Rule value: '" + token_value + "' for definition: " + this.getDefinitionDescription());
60
+
61
+ var result1 = this.evaluatePartialRule(token_value, this.definition['part1'], this.sanitizeArrayValue(this.definition['value1']));
62
+ if (this.definition['multipart'] == 'false' || this.definition['multipart'] == false || this.definition['multipart'] == null) return result1;
63
+ this.getLogger().debug("Part 1: " + result1 + " Processing part 2...");
64
+
65
+ var result2 = this.evaluatePartialRule(token_value, this.definition['part2'], this.sanitizeArrayValue(this.definition['value2']));
66
+ this.getLogger().debug("Part 2: " + result2 + " Completing evaluation...");
67
+
68
+ if (this.definition['operator'] == "or") return (result1 || result2);
69
+ return (result1 && result2);
70
+ }
71
+
72
+
73
+ Tr8n.Proxy.NumericRule.prototype.evaluatePartialRule = function(token_value, name, values) {
74
+ if (name == 'is') {
75
+ if (Tr8n.Utils.indexOf(values, token_value)!=-1) return true;
76
+ return false;
77
+ }
78
+ if (name == 'is_not') {
79
+ if (Tr8n.Utils.indexOf(values, token_value)==-1) return true;
80
+ return false;
81
+ }
82
+ if (name == 'ends_in') {
83
+ for(var i=0; i<values.length; i++) {
84
+ if (token_value.match(values[i] + "$")) return true;
85
+ }
86
+ return false;
87
+ }
88
+ if (name == 'does_not_end_in') {
89
+ for(var i=0; i<values.length; i++) {
90
+ if (token_value.match(values[i] + "$")) return false;
91
+ }
92
+ return true;
93
+ }
94
+ return false;
95
+ }
@@ -0,0 +1,60 @@
1
+ /****************************************************************************
2
+ Copyright (c) 2010-2012 Michael Berkovich, Ian McDaniel, tr8n.net
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ ****************************************************************************/
23
+
24
+ Tr8n.Tml = Tr8n.Tml || {};
25
+
26
+ Tr8n.Tml.Label = function(node, proxy) {
27
+ this.node = node;
28
+ this.label = "";
29
+ this.description = "";
30
+ this.tokens = {};
31
+ this.options = {};
32
+ this.proxy = proxy;
33
+
34
+ for (var i=0; i < this.node.childNodes.length; i++) {
35
+ var childNode = this.node.childNodes[i];
36
+
37
+ // text should just be added to the label
38
+ if (childNode.nodeType == 3) {
39
+ this.label = this.label + " " + Tr8n.Utils.trim(childNode.nodeValue);
40
+ } else if (childNode.nodeName == "TML:TOKEN") {
41
+ var token = new Tr8n.Tml.Token(childNode, this.tokens);
42
+ this.label = Tr8n.Utils.trim(this.label) + " " + token.toTokenString();
43
+ }
44
+
45
+ }
46
+
47
+ this.description = this.node.attributes['desc'] || this.node.attributes['description'];
48
+ this.description = this.description ? this.description.value : null;
49
+
50
+ this.label = this.label.replace(/\n/g, '');
51
+ this.label = Tr8n.Utils.trim(this.label);
52
+
53
+ // console.log(this.label + " : " + this.description);
54
+ }
55
+
56
+ Tr8n.Tml.Label.prototype = {
57
+ translate: function() {
58
+ this.node.innerHTML = this.proxy.translate(this.label, this.description, this.tokens, this.options);
59
+ }
60
+ }
@@ -0,0 +1,107 @@
1
+ /****************************************************************************
2
+ Copyright (c) 2010-2012 Michael Berkovich, Ian McDaniel, tr8n.net
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ ****************************************************************************/
23
+
24
+ Tr8n.Tml = Tr8n.Tml || {};
25
+
26
+ Tr8n.Tml.Token = function(node, tokens) {
27
+ this.node = node;
28
+
29
+ this.type = this.node.attributes['type'];
30
+ this.type = this.type ? this.type.value : 'data';
31
+
32
+ this.name = this.node.attributes['name'];
33
+ this.name = this.name ? this.name.value : 'unknown';
34
+ this.name = this.name.toLowerCase();
35
+
36
+ this.context = this.node.attributes['context'];
37
+ this.context = this.context ? this.context.value : null;
38
+
39
+ this.content = "";
40
+
41
+ for (var i=0; i < this.node.childNodes.length; i++) {
42
+ var childNode = this.node.childNodes[i];
43
+ // console.log(childNode.nodeType + " " + childNode.nodeValue);
44
+ var token_type = this.node.attributes['type'] ? this.node.attributes['type'].nodeValue : 'data';
45
+ // console.log(this.name + " " + token_type);
46
+
47
+ if (childNode.nodeType == 3) {
48
+ // text should just be added to the label
49
+ // <tml:label>You have <tml:token type="data" name="count" context="number">2</tml:token> messages.</tml:label>
50
+
51
+ if (node.attributes['context'] && node.attributes['context'].nodeValue == 'gender') {
52
+ tokens[this.name] = {subject: node.attributes['value'].nodeValue, value: Tr8n.Utils.trim(childNode.nodeValue)};
53
+ } else {
54
+ tokens[this.name] = Tr8n.Utils.trim(childNode.nodeValue);
55
+ }
56
+
57
+ } else {
58
+ // the first element inside the token must be a decoration span, bold, etc...
59
+ // <tml:label>Hello
60
+ // <tml:token type="decoration" name="span">
61
+ // <span style='color:brown;font-weight:bold;'>
62
+ // World
63
+ // </span>
64
+ // </tml:token>
65
+ // </tml:label>
66
+
67
+ var html_tag = childNode.nodeName.toLowerCase();
68
+ var attributes = [];
69
+ if (childNode.attributes['style']) {
70
+ attributes.push("style='" + childNode.attributes['style'].nodeValue + "'");
71
+ }
72
+ if (childNode.attributes['class']) {
73
+ attributes.push("class='" + childNode.attributes['class'].nodeValue + "'");
74
+ }
75
+
76
+ tokens[this.name] = "<" + html_tag + " " + attributes.join(' ') + ">{$0}</" + html_tag + ">";
77
+
78
+ // console.log(this.name + " has value of " + tokens[this.name]);
79
+
80
+ this.content = "";
81
+
82
+ for (var j=0; j<childNode.childNodes.length; j++) {
83
+ var grandChildNode = childNode.childNodes[j];
84
+ if (grandChildNode.nodeType == 3) {
85
+ this.content = Tr8n.Utils.trim(this.content) + " " + Tr8n.Utils.trim(grandChildNode.nodeValue);
86
+ } else if (grandChildNode.nodeName == "TML:TOKEN") {
87
+ var token = new Tr8n.Tml.Token(grandChildNode, tokens);
88
+ this.content = Tr8n.Utils.trim(this.content) + " " + token.toTokenString();
89
+ }
90
+ }
91
+ }
92
+ }
93
+
94
+ this.content = this.content.replace(/\n/g, '');
95
+ this.content = Tr8n.Utils.trim(this.content);
96
+ }
97
+
98
+ Tr8n.Tml.Token.prototype = {
99
+ toTokenString: function() {
100
+ if (this.type == "data") {
101
+ // TODO: we may need to add dependencies here: gender, number and language cases
102
+ return "{" + this.name + "}";
103
+ } else {
104
+ return "[" + this.name + ": " + this.content + "]";
105
+ }
106
+ }
107
+ }
@@ -0,0 +1,45 @@
1
+ /****************************************************************************
2
+ Copyright (c) 2010-2012 Michael Berkovich, Ian McDaniel, tr8n.net
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ ****************************************************************************/
23
+
24
+ Tr8n.Proxy.DataToken = function(label, token, options) {
25
+ this.label = label;
26
+ this.full_name = token;
27
+ this.options = options;
28
+ }
29
+
30
+ Tr8n.Proxy.DataToken.prototype = new Tr8n.Proxy.Token();
31
+
32
+ Tr8n.Proxy.DataToken.parse = function(label, options) {
33
+ var tokens = label.match(/(\{[^_][\w]+(:[\w]+)?\})/g);
34
+ if (!tokens) return [];
35
+
36
+ var objects = [];
37
+ var uniq = {};
38
+ for(i=0; i<tokens.length; i++) {
39
+ if (uniq[tokens[i]]) continue;
40
+ options['proxy'].debug("Registering data token: " + tokens[i]);
41
+ objects.push(new Tr8n.Proxy.DataToken(label, tokens[i], options));
42
+ uniq[tokens[i]] = true;
43
+ }
44
+ return objects;
45
+ }
@@ -0,0 +1,82 @@
1
+ /****************************************************************************
2
+ Copyright (c) 2010-2012 Michael Berkovich, Ian McDaniel, tr8n.net
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ ****************************************************************************/
23
+
24
+ Tr8n.Proxy.DecorationToken = function(label, token, options) {
25
+ this.label = label;
26
+ this.full_name = token;
27
+ this.options = options;
28
+ }
29
+
30
+ Tr8n.Proxy.DecorationToken.prototype = new Tr8n.Proxy.Token();
31
+
32
+ Tr8n.Proxy.DecorationToken.parse = function(label, options) {
33
+ var tokens = label.match(/(\[\w+:[^\]]+\])/g);
34
+ if (!tokens) return [];
35
+
36
+ var objects = [];
37
+ var uniq = {};
38
+ for(i=0; i<tokens.length; i++) {
39
+ if (uniq[tokens[i]]) continue;
40
+ options['proxy'].debug("Registering decoration token: " + tokens[i]);
41
+ objects.push(new Tr8n.Proxy.DecorationToken(label, tokens[i], options));
42
+ uniq[tokens[i]] = true;
43
+ }
44
+ return objects;
45
+ }
46
+
47
+ Tr8n.Proxy.DecorationToken.prototype.getDecoratedValue = function() {
48
+ if (!this.decorated_value) {
49
+ var value = this.getFullName().replace(/[\]]/g, '');
50
+ value = value.substring(value.indexOf(':') + 1, value.length);
51
+ this.decorated_value = Tr8n.Utils.trim(value);
52
+ }
53
+ return this.decorated_value;
54
+ }
55
+
56
+ Tr8n.Proxy.DecorationToken.prototype.substitute = function(label, token_values) {
57
+ var object = token_values[this.getName()];
58
+ var decoration = object;
59
+
60
+ if (!object || typeof object == 'object') {
61
+ // look for the default decoration
62
+ decoration = this.getProxy().getDecorationFor(this.getName());
63
+ if (!decoration) {
64
+ this.getLogger().error("Default decoration is not defined for token " + this.getName());
65
+ return label;
66
+ }
67
+
68
+ decoration = Tr8n.Utils.replaceAll(decoration, '{$0}', this.getDecoratedValue());
69
+ if (object) {
70
+ for (var key in object) {
71
+ decoration = Tr8n.Utils.replaceAll(decoration, '{$' + key + '}', object[key]);
72
+ }
73
+ }
74
+ } else if (typeof object == 'string') {
75
+ decoration = Tr8n.Utils.replaceAll(decoration, '{$0}', this.getDecoratedValue());
76
+ } else {
77
+ this.getLogger().error("Unknown type of decoration token " + this.getFullName());
78
+ return label;
79
+ }
80
+
81
+ return Tr8n.Utils.replaceAll(label, this.getFullName(), decoration);
82
+ }
@@ -0,0 +1,121 @@
1
+ /****************************************************************************
2
+ Copyright (c) 2010-2012 Michael Berkovich, Ian McDaniel, tr8n.net
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ ****************************************************************************/
23
+
24
+ Tr8n.Proxy.Token = function() {}
25
+
26
+ Tr8n.Proxy.Token.prototype = {
27
+ getProxy: function() {
28
+ return this.options['proxy'];
29
+ },
30
+ getLogger: function() {
31
+ return this.getProxy().logger;
32
+ },
33
+ getExpression: function() {
34
+ // must be implemented by the extending class
35
+ return null;
36
+ },
37
+ register: function(label, options) {
38
+ if (this.getExpression() == null)
39
+ alert("Token expression must be provided");
40
+
41
+ var tokens = label.match(this.getExpression());
42
+ if (!tokens) return [];
43
+
44
+ var objects = [];
45
+ var uniq = {};
46
+ for(i=0; i<tokens.length; i++) {
47
+ if (uniq[tokens[i]]) continue;
48
+ options['proxy'].debug("Registering data token: " + tokens[i]);
49
+ objects.push(new Tr8n.Proxy.TransformToken(label, tokens[i], options));
50
+ uniq[tokens[i]] = true;
51
+ }
52
+ return objects;
53
+ },
54
+ getFullName: function() {
55
+ return this.full_name;
56
+ },
57
+ getDeclaredName: function() {
58
+ if (!this.declared_name) {
59
+ this.declared_name = this.getFullName().replace(/[{}\[\]]/g, '');
60
+ }
61
+ return this.declared_name;
62
+ },
63
+ getName: function() {
64
+ if (!this.name) {
65
+ this.name = Tr8n.Utils.trim(this.getDeclaredName().split(':')[0]);
66
+ }
67
+ return this.name;
68
+ },
69
+ getLanguageRule: function() {
70
+
71
+ return null;
72
+ },
73
+ substitute: function(label, token_values) {
74
+ var value = token_values[this.getName()];
75
+
76
+ if (value == null) {
77
+ this.getLogger().error("Value for token: " + this.getFullName() + " was not provided");
78
+ return label;
79
+ }
80
+
81
+ return Tr8n.Utils.replaceAll(label, this.getFullName(), this.getTokenValue(value));
82
+ },
83
+ getTokenValue: function(token_value) {
84
+ if (typeof token_value == 'string') return token_value;
85
+ if (typeof token_value == 'number') return token_value;
86
+ return token_value['value'];
87
+ },
88
+ getTokenObject: function(token_value) {
89
+ if (typeof token_value == 'string') return token_value;
90
+ if (typeof token_value == 'number') return token_value;
91
+ return token_value['subject'];
92
+ },
93
+ getType: function() {
94
+ if (this.getDeclaredName().indexOf(':') == -1)
95
+ return null;
96
+
97
+ if (!this.type) {
98
+ this.type = this.getDeclaredName().split('|')[0].split(':');
99
+ this.type = this.type[this.type.length - 1];
100
+ }
101
+
102
+ return this.type;
103
+ },
104
+ getSuffix: function() {
105
+ if (!this.suffix) {
106
+ this.suffix = this.getName().split('_');
107
+ this.suffix = this.suffix[this.suffix.length - 1];
108
+ }
109
+ return this.suffix;
110
+ },
111
+ getLanguageRule: function() {
112
+ if (!this.language_rule) {
113
+ if (this.getType()) {
114
+ this.language_rule = this.getProxy().getLanguageRuleForType(this.getType());
115
+ } else {
116
+ this.language_rule = this.getProxy().getLanguageRuleForTokenSuffix(this.getSuffix());
117
+ }
118
+ }
119
+ return this.language_rule;
120
+ }
121
+ }