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
@@ -1,1472 +0,0 @@
1
- /****************************************************************************
2
- Copyright (c) 2010-2012 Michael Berkovich
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
- /****************************************************************************
25
- ****
26
- **** This JavaScript Client SDK supports English as the native site language.
27
- **** If your site native language is other than English, please read
28
- **** the integration guide for details on how to make the SDK work with
29
- **** your native language.
30
- ****
31
- ****************************************************************************/
32
-
33
- var Tr8n = Tr8n || {
34
- element:function(element_id) {
35
- if (typeof element_id == 'string') return document.getElementById(element_id);
36
- return element_id;
37
- },
38
- value:function(element_id) {
39
- return Tr8n.element(element_id).value;
40
- }
41
- };
42
-
43
- /****************************************************************************
44
- **** Tr8n Proxy
45
- ****************************************************************************/
46
-
47
- Tr8n.Proxy = function(options) {
48
- var self = this;
49
- this.options = options;
50
- this.options['url'] = this.options['url'] || '/tr8n/api/v1/language/translate';
51
- this.options['scheduler_interval'] = this.options['scheduler_interval'] || 20000;
52
- this.logger_enabled = false;
53
- this.missing_translations_locked = false;
54
- this.inline_translations_enabled = this.options['enable_inline_translations'];
55
- this.logger = new Tr8n.Proxy.Logger({
56
- 'proxy': self,
57
- 'element_id': options['logger_element_id'] || 'tr8n_debugger'
58
- });
59
-
60
- this.language = new Tr8n.Proxy.Language({
61
- 'proxy': self
62
- });
63
-
64
- this.initTranslations();
65
- this.runScheduledTasks();
66
- }
67
-
68
- Tr8n.Proxy.prototype = {
69
- log: function(msg) {
70
- this.logger.debug(msg);
71
- },
72
- logSettings: function() {
73
- this.logger.clear();
74
- this.logger.logObject(this.options);
75
- },
76
- logTranslations: function() {
77
- this.logger.clear();
78
- this.translations = this.translations || {};
79
- this.logger.logObject(this.translations);
80
- },
81
- logMissingTranslations: function() {
82
- this.logger.clear();
83
- this.missing_translation_keys = this.missing_translation_keys || {};
84
- this.logger.logObject(this.missing_translation_keys);
85
- },
86
- disableLogger: function() {
87
- this.logger_enabled = false;
88
- },
89
- enableLogger: function() {
90
- this.logger_enabled = true;
91
- },
92
- debug: function(msg) {
93
- this.logger.debug(msg);
94
- },
95
- error: function(msg) {
96
- this.logger.error(msg);
97
- },
98
- translate: function(label, description, tokens, options) {
99
- if (!label) return "";
100
- description = description || "";
101
- tokens = tokens || {};
102
- options = options || {};
103
- return this.language.translate(label, description, tokens, options);
104
- },
105
- tr: function(label, description, tokens, options) {
106
- return this.translate(label, description, tokens, options);
107
- },
108
- trl: function(label, description, tokens, options) {
109
- options = options || {};
110
- options['skip_decorations'] = true;
111
- return this.translate(label, description, tokens, options);
112
- },
113
- getTranslations: function() {
114
- this.translations = this.translations || {};
115
- return this.translations;
116
- },
117
- getDecorationFor: function(decoration_name) {
118
- if (!this.options['default_decorations'])
119
- return null;
120
- return this.options['default_decorations'][decoration_name];
121
- },
122
- getLanguageRuleForType: function(rule_type) {
123
- // modify this section to add more rules
124
- if (rule_type == 'number') return 'Tr8n.Proxy.NumericRule';
125
- if (rule_type == 'gender') return 'Tr8n.Proxy.GenderRule';
126
- if (rule_type == 'date') return 'Tr8n.Proxy.DateRule';
127
- if (rule_type == 'list') return 'Tr8n.Proxy.ListRule';
128
- if (rule_type == 'gender_list') return 'Tr8n.Proxy.GenderListRule';
129
- return null;
130
- },
131
- getLanguageRuleForTokenSuffix: function(token_suffix) {
132
- if (!this.options['rules']) return null;
133
-
134
- for (rule_type in this.options['rules']) {
135
- var suffixes = this.options['rules'][rule_type]['token_suffixes'];
136
- if (!suffixes) continue;
137
-
138
- if (Tr8n.Proxy.Utils.indexOf(suffixes, token_suffix) != -1 )
139
- return this.getLanguageRuleForType(rule_type);
140
- }
141
- return null;
142
- },
143
-
144
- registerTranslationKeys: function(translations) {
145
- this.log("Found " + translations.length + " registered phrases");
146
- for (i = 0; i < translations.length; i++) {
147
- var translation_key = translations[i];
148
- this.log("Registering " + translation_key['key']);
149
- this.translations[translation_key['key']] = translation_key;
150
- }
151
- },
152
-
153
- initTranslations: function(forced) {
154
- if (!forced && this.translations) return;
155
-
156
- this.translations = {};
157
-
158
- // Check for page variable to load translations from, if variable was provided
159
- if (this.options['translations_cache_id']) {
160
- this.log("Registering page translations from translations cache...");
161
- this.updateTranslations(eval(this.options['translations_cache_id']));
162
- }
163
-
164
- var self = this;
165
-
166
- // Optionally, fetch translations from the server
167
- if (this.options['fetch_translations_on_init']) {
168
- this.log("Fetching translations from the server...");
169
- Tr8n.Proxy.Utils.ajax(this.options['url'], {
170
- method: 'get',
171
- parameters: {'batch': true, 'source': self.options['default_source']},
172
- onSuccess: function(response) {
173
- self.log("Received response from the server");
174
- self.log(response.responseText);
175
- self.updateTranslations(eval("[" + response.responseText + "]")[0]['phrases']);
176
- }
177
- });
178
- }
179
- },
180
-
181
- updateTranslations: function(new_translations) {
182
- this.translations = this.translations || {};
183
- this.log("Updating page translations...");
184
- this.registerTranslationKeys(new_translations);
185
- },
186
-
187
- registerMissingTranslationKey: function(translation_key, token_values, options) {
188
- this.missing_translation_keys = this.missing_translation_keys || {};
189
- if (!this.missing_translation_keys[translation_key.key]) {
190
- this.log('Adding missing translation to the queue: ' + translation_key.key);
191
- this.missing_translation_keys[translation_key.key] = {translation_key: translation_key, token_values: token_values, options:options};
192
- }
193
- },
194
- submitMissingTranslationKeys: function() {
195
- if (this.missing_translations_locked) {
196
- this.log('Missing translations are being processed, postponding registration task.');
197
- return;
198
- }
199
-
200
- this.missing_translation_keys = this.missing_translation_keys || {};
201
-
202
- var phrases = "[";
203
- for (var key in this.missing_translation_keys) {
204
- var translation_key = this.missing_translation_keys[key].translation_key;
205
- if (translation_key == null) continue;
206
- if (phrases!="[") phrases = phrases + ",";
207
- phrases = phrases + "{";
208
- phrases = phrases + '"label":"' + translation_key.label + '", ';
209
- phrases = phrases + '"description":"' + translation_key.description + '"';
210
- phrases = phrases + "}";
211
- }
212
- phrases = phrases + "]";
213
-
214
- if (phrases == '[]') {
215
- // this.log('No missing translation keys to submit...');
216
- return;
217
- }
218
-
219
- var self = this;
220
- this.debug('Submitting missing translation keys: ' + phrases);
221
- Tr8n.Proxy.Utils.ajax(this.options['url'], {
222
- method: 'put',
223
- parameters: {'source': self.options['default_source'], 'phrases': phrases},
224
- onSuccess: function(response) {
225
- self.log("Received response from the server");
226
- self.log(response.responseText);
227
- self.updateMissingTranslationKeys(eval("[" + response.responseText + "]")[0]['phrases']);
228
- }
229
- });
230
- },
231
- updateMissingTranslationKeys: function(translations) {
232
- this.missing_translations_locked = true;
233
- this.log("Received " + translations.length + " registered phrases...");
234
- for (i = 0; i < translations.length; i++) {
235
- var translation_key_data = translations[i];
236
-
237
- this.log("Registering new key " + translation_key_data.key);
238
- this.translations[translation_key_data.key] = translation_key_data;
239
- var missing_key_data = this.missing_translation_keys[translation_key_data.key];
240
- var tr8nElement = Tr8n.element(translation_key_data.key);
241
-
242
- if (tr8nElement && missing_key_data.translation_key) {
243
- tr8nElement.innerHTML = missing_key_data.translation_key.translate(this.language, missing_key_data.token_values, {skip_decorations:true});
244
- tr8nElement.setAttribute('translation_key_id', translation_key_data['id']);
245
- if (this.inline_translations_enabled) {
246
- tr8nElement.className = 'tr8n_translatable tr8n_not_translated';
247
- }
248
- }
249
-
250
- delete this.missing_translation_keys[missing_key_data.translation_key.key];
251
- }
252
- this.missing_translations_locked = false;
253
- },
254
- runScheduledTasks: function() {
255
- var self = this;
256
-
257
- // this.log("Running scheduled tasks...");
258
- this.submitMissingTranslationKeys();
259
-
260
- window.setTimeout(function() {
261
- self.runScheduledTasks();
262
- }, this.options['scheduler_interval']);
263
- },
264
- }
265
-
266
- /****************************************************************************
267
- **** Tr8n Proxy Language
268
- ****************************************************************************/
269
-
270
- Tr8n.Proxy.Language = function(options) {
271
- this.options = options;
272
- }
273
-
274
- Tr8n.Proxy.Language.prototype = {
275
- getProxy: function() {
276
- return this.options['proxy'];
277
- },
278
- getLogger: function() {
279
- return this.getProxy().logger;
280
- },
281
- translate: function(label, description, tokens, options) {
282
- return (new Tr8n.Proxy.TranslationKey(label, description, {'proxy': this.getProxy()}).translate(this, tokens, options));
283
- }
284
- }
285
-
286
- /****************************************************************************
287
- **** Tr8n Proxy TranslationKey
288
- ****************************************************************************/
289
-
290
- Tr8n.Proxy.TranslationKey = function(label, description, options) {
291
- this.label = label;
292
- this.description = description;
293
- this.options = options;
294
- this.generateKey();
295
- }
296
-
297
- Tr8n.Proxy.TranslationKey.prototype = {
298
- getProxy: function() {
299
- return this.options['proxy'];
300
- },
301
- getLogger: function() {
302
- return this.getProxy().logger;
303
- },
304
- findFirstAcceptableTranslation: function(translations, token_values) {
305
- // check for a single translation case - no context rules
306
- if (translations['label']!=null) {
307
- this.getLogger().debug('Found a single translation: ' + translations['label']);
308
- return translations;
309
- }
310
-
311
- translations = translations['labels'];
312
- if (!translations) {
313
- this.getLogger().error("Translations are in a weird form...");
314
- return null;
315
- }
316
-
317
- this.getLogger().debug('Found translations: ' + translations.length);
318
- for (var i=0; i<translations.length; i++) {
319
- this.getLogger().debug("Checking context rules for:" + translations[i]['label']);
320
-
321
- if (!translations[i]['context']) {
322
- this.getLogger().debug("Translation has no context, using it by default");
323
- return translations[i];
324
- }
325
- var valid_context = true;
326
-
327
- for (var token in translations[i]['context']) {
328
- if (!valid_context) continue;
329
- var token_context = translations[i]['context'][token];
330
- var rule_name = this.getProxy().getLanguageRuleForType(token_context['type']);
331
- this.getLogger().debug("Evaluating rule: " + rule_name);
332
- var options = {'proxy': this.getProxy()};
333
- var rule = eval("new " + rule_name + "(token_context, options)");
334
- valid_context = valid_context && rule.evaluate(token, token_values);
335
- }
336
-
337
- if (valid_context) {
338
- this.getLogger().debug("Found valid translation: " + translations[i].label);
339
- return translations[i];
340
- } else {
341
- this.getLogger().debug("The rules were not matched for: " + translations[i].label);
342
- }
343
- }
344
-
345
- this.getLogger().debug('No acceptable ranslations found');
346
- return null;
347
- },
348
-
349
- translate: function(language, token_values, options) {
350
- if (!this.label) {
351
- this.getLogger().error('Label must always be provided for the translate method');
352
- return '';
353
- }
354
-
355
- var translations = this.getProxy().getTranslations();
356
- var translation_key = translations[this.key];
357
-
358
- if (translation_key) {
359
- this.getLogger().debug("Found translations, evaluating rules...");
360
-
361
- this.id = translation_key.id;
362
- this.original = translation_key.original;
363
- var translation = this.findFirstAcceptableTranslation(translation_key, token_values);
364
-
365
- if (translation) {
366
- this.getLogger().debug("Found a valid match: " + translation.label);
367
- return this.substituteTokens(translation['label'], token_values, options);
368
- } else {
369
- this.getLogger().debug("No valid match found, using default language");
370
- return this.substituteTokens(this.label, token_values, options);
371
- }
372
-
373
- } else {
374
- this.getLogger().debug("Translation not found, using default language");
375
- }
376
-
377
- this.getProxy().registerMissingTranslationKey(this, token_values, options);
378
- this.getLogger().debug('No translation found. Using default...');
379
- return this.substituteTokens(this.label, token_values, options);
380
- },
381
-
382
- generateKey: function() {
383
- this.key = this.label + ";;;";
384
- if (this.description) this.key = this.key + this.description;
385
-
386
- this.getLogger().debug('Preparing label signature: ' + this.key);
387
- this.key = MD5(this.key);
388
- this.getLogger().debug('Label signature: ' + this.key);
389
- },
390
-
391
- registerDataTokens: function(label) {
392
- this.data_tokens = [];
393
- this.data_tokens = this.data_tokens.concat(Tr8n.Proxy.DataToken.parse(label, {'key': this, 'proxy':this.getProxy()}));
394
- this.data_tokens = this.data_tokens.concat(Tr8n.Proxy.TransformToken.parse(label, {'key': this, 'proxy':this.getProxy()}));
395
- },
396
-
397
- registerDecorationTokens: function(label) {
398
- this.decoration_tokens = [];
399
- this.decoration_tokens = this.decoration_tokens.concat(Tr8n.Proxy.DecorationToken.parse(label, {'key': this, 'proxy':this.getProxy()}));
400
- },
401
-
402
- substituteTokens: function(label, token_values, options) {
403
- this.registerDataTokens(label);
404
- if (!this.data_tokens) return this.decorateLabel(label, options);
405
- for (var i = 0; i < this.data_tokens.length; i++) {
406
- label = this.data_tokens[i].substitute(label, token_values || {});
407
- }
408
-
409
- this.registerDecorationTokens(label);
410
- if (!this.decoration_tokens) return label;
411
- for (var i = 0; i < this.decoration_tokens.length; i++) {
412
- label = this.decoration_tokens[i].substitute(label, token_values || {});
413
- }
414
-
415
- return this.decorateLabel(label, options);
416
- },
417
-
418
- decorateLabel: function(label, options){
419
- options = options || {};
420
- if (options['skip_decorations'])
421
- return label;
422
-
423
- html = [];
424
- html.push("<tr8n ");
425
-
426
- if (this.id)
427
- html.push(" translation_key_id='" + this.id + "' ");
428
-
429
- if (this.key)
430
- html.push(" id='" + this.key + "' ");
431
-
432
- var klasses = ['tr8n_translatable'];
433
-
434
- if (this.original)
435
- klasses.push('tr8n_not_translated');
436
- else
437
- klasses.push('tr8n_translated');
438
-
439
- if (this.getProxy().inline_translations_enabled && this.id)
440
- html.push(" class='" + klasses.join(' ') + "'");
441
-
442
- html.push(">");
443
- html.push(label);
444
- html.push("</tr8n>");
445
- return html.join("");
446
- }
447
- }
448
-
449
- /****************************************************************************
450
- **** Tr8n Proxy LanguageRule
451
- ****************************************************************************/
452
-
453
- Tr8n.Proxy.LanguageRule = function() {}
454
-
455
- Tr8n.Proxy.LanguageRule.prototype = {
456
- getProxy: function() {
457
- return this.options['proxy'];
458
- },
459
- getLogger: function() {
460
- return this.getProxy().logger;
461
- },
462
- getTokenValue: function(token_name, token_values) {
463
- var object = token_values[token_name];
464
- if (object == null) {
465
- this.getLogger().error("Invalid token value for token: " + token_name);
466
- }
467
-
468
- return object;
469
- },
470
- getDefinitionDescription: function() {
471
- var result = [];
472
- for (var key in this.definition)
473
- result.push(key + ": '" + this.definition[key] + "'");
474
- return "{" + result.join(", ") + "}";
475
- },
476
- sanitizeArrayValue: function(value) {
477
- var results = [];
478
- var arr = value.split(',');
479
- for (var index = 0; index < arr.length; index++) {
480
- results.push(Tr8n.Proxy.Utils.trim(arr[index]));
481
- }
482
- return results;
483
- }
484
- }
485
-
486
- /****************************************************************************
487
- **** Tr8n Proxy NumericRule
488
- ****************************************************************************/
489
-
490
- Tr8n.Proxy.NumericRule = function(definition, options) {
491
- this.definition = definition;
492
- this.options = options;
493
- }
494
-
495
- Tr8n.Proxy.NumericRule.prototype = new Tr8n.Proxy.LanguageRule();
496
-
497
- // English based transform method
498
- // FORM: [singular, plural]
499
- // {count | message, messages}
500
- // {count | person, people}
501
- Tr8n.Proxy.NumericRule.transform = function(count, values) {
502
- if (count == 1) return values[0];
503
- return values[1];
504
- }
505
-
506
- Tr8n.Proxy.NumericRule.prototype.evaluate = function(token_name, token_values){
507
- // "count":{"value1":"2,3,4","operator":"and","type":"number","multipart":true,"part2":"does_not_end_in","value2":"12,13,14","part1":"ends_in"}
508
-
509
- var object = this.getTokenValue(token_name, token_values);
510
- if (object == null) return false;
511
-
512
- var token_value = null;
513
- if (typeof object == 'string' || typeof object == 'number') {
514
- token_value = "" + object;
515
- } else if (typeof object == 'object' && object['subject']) {
516
- token_value = "" + object['subject'];
517
- } else {
518
- this.getLogger().error("Invalid token value for numeric token: " + token_name);
519
- return false;
520
- }
521
-
522
- this.getLogger().debug("Rule value: '" + token_value + "' for definition: " + this.getDefinitionDescription());
523
-
524
- var result1 = this.evaluatePartialRule(token_value, this.definition['part1'], this.sanitizeArrayValue(this.definition['value1']));
525
- if (this.definition['multipart'] == 'false' || this.definition['multipart'] == false || this.definition['multipart'] == null) return result1;
526
- this.getLogger().debug("Part 1: " + result1 + " Processing part 2...");
527
-
528
- var result2 = this.evaluatePartialRule(token_value, this.definition['part2'], this.sanitizeArrayValue(this.definition['value2']));
529
- this.getLogger().debug("Part 2: " + result2 + " Completing evaluation...");
530
-
531
- if (this.definition['operator'] == "or") return (result1 || result2);
532
- return (result1 && result2);
533
- }
534
-
535
-
536
- Tr8n.Proxy.NumericRule.prototype.evaluatePartialRule = function(token_value, name, values) {
537
- if (name == 'is') {
538
- if (Tr8n.Proxy.Utils.indexOf(values, token_value)!=-1) return true;
539
- return false;
540
- }
541
- if (name == 'is_not') {
542
- if (Tr8n.Proxy.Utils.indexOf(values, token_value)==-1) return true;
543
- return false;
544
- }
545
- if (name == 'ends_in') {
546
- for(var i=0; i<values.length; i++) {
547
- if (token_value.match(values[i] + "$")) return true;
548
- }
549
- return false;
550
- }
551
- if (name == 'does_not_end_in') {
552
- for(var i=0; i<values.length; i++) {
553
- if (token_value.match(values[i] + "$")) return false;
554
- }
555
- return true;
556
- }
557
- return false;
558
- }
559
-
560
-
561
- /****************************************************************************
562
- **** Tr8n Proxy GenderRule
563
- ****************************************************************************/
564
-
565
- Tr8n.Proxy.GenderRule = function(definition, options) {
566
- this.definition = definition;
567
- this.options = options;
568
- }
569
-
570
- Tr8n.Proxy.GenderRule.prototype = new Tr8n.Proxy.LanguageRule();
571
-
572
- // FORM: [male, female, unknown]
573
- // {user | registered on}
574
- // {user | he, she}
575
- // {user | he, she, he/she}
576
- Tr8n.Proxy.GenderRule.transform = function(object, values) {
577
- if (values.length == 1) return values[0];
578
-
579
- if (typeof object == 'string') {
580
- if (object == 'male') return values[0];
581
- if (object == 'female') return values[1];
582
- } else if (typeof object == 'object') {
583
- if (object['gender'] == 'male') return values[0];
584
- if (object['gender'] == 'female') return values[1];
585
- }
586
-
587
- if (values.length == 3) return values[2];
588
- return values[0] + "/" + values[1];
589
- }
590
-
591
- Tr8n.Proxy.GenderRule.prototype.evaluate = function(token_name, token_values) {
592
-
593
- var object = this.getTokenValue(token_name, token_values);
594
- if (!object) return false;
595
-
596
- var gender = "";
597
-
598
- if (typeof object != 'object') {
599
- this.getLogger().error("Invalid token value for gender based token: " + token_name + ". Token value must be an object.");
600
- return false;
601
- }
602
-
603
- if (!object['subject']) {
604
- this.getLogger().error("Invalid token subject for gender based token: " + token_name + ". Token value must contain a subject. Subject can be a string or an object with a gender.");
605
- return false;
606
- }
607
-
608
- if (typeof object['subject'] == 'string') {
609
- gender = object['subject'];
610
- } else if (typeof object['subject'] == 'object') {
611
- gender = object['subject']['gender'];
612
- if (!gender) {
613
- this.getLogger().error("Cannot determine gender for token subject: " + token_name);
614
- return false;
615
- }
616
- } else {
617
- this.getLogger().error("Invalid token subject for gender based token: " + token_name + ". Subject does not have a gender.");
618
- return false;
619
- }
620
-
621
- if (this.definition['operator'] == "is") {
622
- return (gender == this.definition['value']);
623
- } else if (this.definition['operator'] == "is_not") {
624
- return (gender != this.definition['value']);
625
- }
626
-
627
- return false;
628
- }
629
-
630
- /****************************************************************************
631
- **** Tr8n Proxy ListRule
632
- ****************************************************************************/
633
-
634
- Tr8n.Proxy.ListRule = function(definition, options) {
635
- this.definition = definition;
636
- this.options = options;
637
- }
638
-
639
- Tr8n.Proxy.ListRule.prototype = new Tr8n.Proxy.LanguageRule();
640
-
641
- Tr8n.Proxy.ListRule.transform = function(object, values) {
642
- return "";
643
- }
644
-
645
- Tr8n.Proxy.ListRule.prototype.evaluate = function(token, token_values) {
646
- return true;
647
- }
648
-
649
- /****************************************************************************
650
- **** Tr8n Proxy GenderListRule
651
- ****************************************************************************/
652
-
653
- Tr8n.Proxy.GenderListRule = function(definition, options) {
654
- this.definition = definition;
655
- this.options = options;
656
- }
657
-
658
- Tr8n.Proxy.GenderListRule.prototype = new Tr8n.Proxy.LanguageRule();
659
-
660
- Tr8n.Proxy.GenderListRule.transform = function(object, values) {
661
- return "";
662
- }
663
-
664
- Tr8n.Proxy.GenderListRule.prototype.evaluate = function(token, token_values) {
665
- return true;
666
- }
667
-
668
- /****************************************************************************
669
- **** Tr8n Proxy DateRule
670
- ****************************************************************************/
671
-
672
- Tr8n.Proxy.DateRule = function(definition, options) {
673
- this.definition = definition;
674
- this.options = options;
675
- }
676
-
677
- Tr8n.Proxy.DateRule.prototype = new Tr8n.Proxy.LanguageRule();
678
-
679
- Tr8n.Proxy.DateRule.transform = function(object, values) {
680
- return "";
681
- }
682
-
683
- Tr8n.Proxy.DateRule.prototype.evaluate = function(token, token_values) {
684
- return true;
685
- }
686
-
687
- /****************************************************************************
688
- **** Tr8n Proxy Token
689
- ****************************************************************************/
690
-
691
- Tr8n.Proxy.Token = function() {}
692
-
693
- Tr8n.Proxy.Token.prototype = {
694
- getProxy: function() {
695
- return this.options['proxy'];
696
- },
697
- getLogger: function() {
698
- return this.getProxy().logger;
699
- },
700
- getExpression: function() {
701
- // must be implemented by the extending class
702
- return null;
703
- },
704
- register: function(label, options) {
705
- if (this.getExpression() == null)
706
- alert("Token expression must be provided");
707
-
708
- var tokens = label.match(this.getExpression());
709
- if (!tokens) return [];
710
-
711
- var objects = [];
712
- var uniq = {};
713
- for(i=0; i<tokens.length; i++) {
714
- if (uniq[tokens[i]]) continue;
715
- options['proxy'].debug("Registering data token: " + tokens[i]);
716
- objects.push(new Tr8n.Proxy.TransformToken(label, tokens[i], options));
717
- uniq[tokens[i]] = true;
718
- }
719
- return objects;
720
- },
721
- getFullName: function() {
722
- return this.full_name;
723
- },
724
- getDeclaredName: function() {
725
- if (!this.declared_name) {
726
- this.declared_name = this.getFullName().replace(/[{}\[\]]/g, '');
727
- }
728
- return this.declared_name;
729
- },
730
- getName: function() {
731
- if (!this.name) {
732
- this.name = Tr8n.Proxy.Utils.trim(this.getDeclaredName().split(':')[0]);
733
- }
734
- return this.name;
735
- },
736
- getLanguageRule: function() {
737
-
738
- return null;
739
- },
740
- substitute: function(label, token_values) {
741
- var value = token_values[this.getName()];
742
-
743
- if (value == null) {
744
- this.getLogger().error("Value for token: " + this.getFullName() + " was not provided");
745
- return label;
746
- }
747
-
748
- return Tr8n.Proxy.Utils.replaceAll(label, this.getFullName(), this.getTokenValue(value));
749
- },
750
- getTokenValue: function(token_value) {
751
- if (typeof token_value == 'string') return token_value;
752
- if (typeof token_value == 'number') return token_value;
753
- return token_value['value'];
754
- },
755
- getTokenObject: function(token_value) {
756
- if (typeof token_value == 'string') return token_value;
757
- if (typeof token_value == 'number') return token_value;
758
- return token_value['subject'];
759
- },
760
- getType: function() {
761
- if (this.getDeclaredName().indexOf(':') == -1)
762
- return null;
763
-
764
- if (!this.type) {
765
- this.type = this.getDeclaredName().split('|')[0].split(':');
766
- this.type = this.type[this.type.length - 1];
767
- }
768
-
769
- return this.type;
770
- },
771
- getSuffix: function() {
772
- if (!this.suffix) {
773
- this.suffix = this.getName().split('_');
774
- this.suffix = this.suffix[this.suffix.length - 1];
775
- }
776
- return this.suffix;
777
- },
778
- getLanguageRule: function() {
779
- if (!this.language_rule) {
780
- if (this.getType()) {
781
- this.language_rule = this.getProxy().getLanguageRuleForType(this.getType());
782
- } else {
783
- this.language_rule = this.getProxy().getLanguageRuleForTokenSuffix(this.getSuffix());
784
- }
785
- }
786
- return this.language_rule;
787
- }
788
- }
789
-
790
- /****************************************************************************
791
- **** Tr8n Proxy Data Token
792
- ****************************************************************************/
793
-
794
- Tr8n.Proxy.DataToken = function(label, token, options) {
795
- this.label = label;
796
- this.full_name = token;
797
- this.options = options;
798
- }
799
-
800
- Tr8n.Proxy.DataToken.prototype = new Tr8n.Proxy.Token();
801
-
802
- Tr8n.Proxy.DataToken.parse = function(label, options) {
803
- var tokens = label.match(/(\{[^_][\w]+(:[\w]+)?\})/g);
804
- if (!tokens) return [];
805
-
806
- var objects = [];
807
- var uniq = {};
808
- for(i=0; i<tokens.length; i++) {
809
- if (uniq[tokens[i]]) continue;
810
- options['proxy'].debug("Registering data token: " + tokens[i]);
811
- objects.push(new Tr8n.Proxy.DataToken(label, tokens[i], options));
812
- uniq[tokens[i]] = true;
813
- }
814
- return objects;
815
- }
816
-
817
- /****************************************************************************
818
- **** Tr8n Proxy Transform Token
819
- ****************************************************************************/
820
-
821
- Tr8n.Proxy.TransformToken = function(label, token, options) {
822
- this.label = label;
823
- this.full_name = token;
824
- this.options = options;
825
- }
826
-
827
- Tr8n.Proxy.TransformToken.prototype = new Tr8n.Proxy.Token();
828
-
829
- Tr8n.Proxy.TransformToken.parse = function(label, options) {
830
- var tokens = label.match(/(\{[^_][\w]+(:[\w]+)?\s*\|\|?[^{^}]+\})/g);
831
- if (!tokens) return [];
832
-
833
- var objects = [];
834
- var uniq = {};
835
- for(i=0; i<tokens.length; i++) {
836
- if (uniq[tokens[i]]) continue;
837
- options['proxy'].debug("Registering transform token: " + tokens[i]);
838
- objects.push(new Tr8n.Proxy.TransformToken(label, tokens[i], options));
839
- uniq[tokens[i]] = true;
840
- }
841
- return objects;
842
- }
843
-
844
- Tr8n.Proxy.TransformToken.prototype.getName = function() {
845
- if (!this.name) {
846
- this.name = Tr8n.Proxy.Utils.trim(this.getDeclaredName().split('|')[0].split(':')[0]);
847
- }
848
- return this.name;
849
- }
850
-
851
- Tr8n.Proxy.TransformToken.prototype.getPipedParams = function() {
852
- if (!this.piped_params) {
853
- var temp = this.getDeclaredName().split('|');
854
- temp = temp[temp.length - 1].split(",");
855
- this.piped_params = [];
856
- for (i=0; i<temp.length; i++) {
857
- this.piped_params.push(Tr8n.Proxy.Utils.trim(temp[i]));
858
- }
859
- }
860
- return this.piped_params;
861
- }
862
-
863
- Tr8n.Proxy.TransformToken.prototype.substitute = function(label, token_values) {
864
- var object = token_values[this.getName()];
865
- if (object == null) {
866
- this.getLogger().error("Value for token: " + this.getFullName() + " was not provided");
867
- return label;
868
- }
869
-
870
- var token_object = this.getTokenObject(object);
871
- this.getLogger().debug("Registered " + this.getPipedParams().length + " piped params");
872
-
873
- var lang_rule_name = this.getLanguageRule();
874
-
875
- if (!lang_rule_name) {
876
- this.getLogger().error("Rule type cannot be determined for the transform token: " + this.getFullName());
877
- return label;
878
- } else {
879
- this.getLogger().debug("Transform token uses rule: " + lang_rule_name);
880
- }
881
-
882
- var transform_value = eval(lang_rule_name).transform(token_object, this.getPipedParams());
883
- this.getLogger().debug("Registered transform value: " + transform_value);
884
-
885
- // for double pipes - show the actual value as well
886
- if (this.isAllowedInTranslation()) {
887
- var token_value = this.getTokenValue(object);
888
- transform_value = token_value + " " + transform_value;
889
- }
890
-
891
- return Tr8n.Proxy.Utils.replaceAll(label, this.getFullName(), transform_value);
892
- }
893
-
894
- Tr8n.Proxy.TransformToken.prototype.getPipedSeparator = function() {
895
- if (!this.piped_separator) {
896
- this.piped_separator = (this.getFullName().indexOf("||") != -1 ? "||" : "|");
897
- }
898
- return this.piped_separator;
899
- }
900
-
901
- Tr8n.Proxy.TransformToken.prototype.isAllowedInTranslation = function(){
902
- return this.getPipedSeparator() == "||";
903
- }
904
-
905
- /****************************************************************************
906
- **** Tr8n Proxy Decoration Token
907
- ****************************************************************************/
908
-
909
- Tr8n.Proxy.DecorationToken = function(label, token, options) {
910
- this.label = label;
911
- this.full_name = token;
912
- this.options = options;
913
- }
914
-
915
- Tr8n.Proxy.DecorationToken.prototype = new Tr8n.Proxy.Token();
916
-
917
- Tr8n.Proxy.DecorationToken.parse = function(label, options) {
918
- var tokens = label.match(/(\[\w+:[^\]]+\])/g);
919
- if (!tokens) return [];
920
-
921
- var objects = [];
922
- var uniq = {};
923
- for(i=0; i<tokens.length; i++) {
924
- if (uniq[tokens[i]]) continue;
925
- options['proxy'].debug("Registering decoration token: " + tokens[i]);
926
- objects.push(new Tr8n.Proxy.DecorationToken(label, tokens[i], options));
927
- uniq[tokens[i]] = true;
928
- }
929
- return objects;
930
- }
931
-
932
- Tr8n.Proxy.DecorationToken.prototype.getDecoratedValue = function() {
933
- if (!this.decorated_value) {
934
- var value = this.getFullName().replace(/[\]]/g, '');
935
- value = value.substring(value.indexOf(':') + 1, value.length);
936
- this.decorated_value = Tr8n.Proxy.Utils.trim(value);
937
- }
938
- return this.decorated_value;
939
- }
940
-
941
- Tr8n.Proxy.DecorationToken.prototype.substitute = function(label, token_values) {
942
- var object = token_values[this.getName()];
943
- var decoration = object;
944
-
945
- if (!object || typeof object == 'object') {
946
- // look for the default decoration
947
- decoration = this.getProxy().getDecorationFor(this.getName());
948
- if (!decoration) {
949
- this.getLogger().error("Default decoration is not defined for token " + this.getName());
950
- return label;
951
- }
952
-
953
- decoration = Tr8n.Proxy.Utils.replaceAll(decoration, '{$0}', this.getDecoratedValue());
954
- if (object) {
955
- for (var key in object) {
956
- decoration = Tr8n.Proxy.Utils.replaceAll(decoration, '{$' + key + '}', object[key]);
957
- }
958
- }
959
- } else if (typeof object == 'string') {
960
- decoration = Tr8n.Proxy.Utils.replaceAll(decoration, '{$0}', this.getDecoratedValue());
961
- } else {
962
- this.getLogger().error("Unknown type of decoration token " + this.getFullName());
963
- return label;
964
- }
965
-
966
- return Tr8n.Proxy.Utils.replaceAll(label, this.getFullName(), decoration);
967
- }
968
-
969
- /****************************************************************************
970
- **** Tr8n Proxy Logger
971
- ****************************************************************************/
972
-
973
- Tr8n.Proxy.Logger = function(options) {
974
- this.options = options;
975
- this.object_keys = [];
976
- }
977
-
978
- Tr8n.Proxy.Logger.prototype = {
979
- clear: function() {
980
- if (!this.options['proxy'].logger_enabled) return;
981
- if (!this.options['element_id']) return;
982
- if (!Tr8n.Proxy.Utils.element(this.options['element_id'])) return;
983
- Tr8n.element(this.options['element_id']).innerHTML = "";
984
- },
985
- append: function(msg) {
986
- if (!this.options['proxy'].logger_enabled) return;
987
- if (!this.options['element_id']) return;
988
- if (!Tr8n.Proxy.Utils.element(this.options['element_id'])) return;
989
-
990
- var str = msg + "<br>" + Tr8n.Proxy.Utils.element(this.options['element_id']).innerHTML;
991
- Tr8n.element(this.options['element_id']).innerHTML = str;
992
- },
993
- log: function(msg) {
994
- if (!this.options['proxy'].logger_enabled) return;
995
- var now = new Date();
996
- var str = "<span style='color:#ccc;'>" + (now.toLocaleDateString() + " " + now.toLocaleTimeString()) + "</span>: " + msg;
997
- this.append(str);
998
- },
999
- debug: function(msg) {
1000
- this.log("<span style='color:grey'>" + msg + "</span>");
1001
- },
1002
- error: function(msg) {
1003
- this.log("<span style='color:red'>" + msg + "</span>");
1004
- },
1005
- S4: function() {
1006
- return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
1007
- },
1008
- guid: function() {
1009
- return (this.S4()+this.S4()+"-"+this.S4()+"-"+this.S4()+"-"+this.S4()+"-"+this.S4()+this.S4()+this.S4());
1010
- },
1011
- escapeHTML: function(str) {
1012
- return( str.replace(/&/g,'&amp;').replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/"/g,'&quot;'));
1013
- },
1014
- showObject: function (obj_key, flag) {
1015
- if (flag) {
1016
- Tr8n.Proxy.Utils.hide("no_object_" + obj_key);
1017
- Tr8n.Proxy.Utils.show("object_" + obj_key);
1018
- Tr8n.element("expander_" + obj_key).innerHTML = "<img src='/assets/tr8n/minus_node.png'>";
1019
- } else {
1020
- Tr8n.Proxy.Utils.hide("object_" + obj_key);
1021
- Tr8n.Proxy.Utils.show("no_object_" + obj_key);
1022
- Tr8n.element("expander_" + obj_key).innerHTML = "<img src='/assets/tr8n/plus_node.png'>";
1023
- }
1024
- },
1025
- toggleNode: function(obj_key) {
1026
- this.showObject(obj_key, (Tr8n.element("object_" + obj_key).style.display == 'none'));
1027
- },
1028
- expandAllNodes: function() {
1029
- for (var i=0; i<this.object_keys.length; i++) {
1030
- this.showObject(this.object_keys[i], true);
1031
- }
1032
- },
1033
- collapseAllNodes: function() {
1034
- for (var i=0; i<this.object_keys.length; i++) {
1035
- this.showObject(this.object_keys[i], false);
1036
- }
1037
- },
1038
- logObject: function(data) {
1039
- this.object_keys = [];
1040
- html = []
1041
- html.push("<div style='float:right;padding-right:10px;'>");
1042
- html.push("<span style='padding:2px;' onClick=\"tr8nProxy.logger.expandAllNodes()\"><img src='/assets/tr8n/plus_node.png'></span>");
1043
- html.push("<span style='padding:2px;' onClick=\"tr8nProxy.logger.collapseAllNodes()\"><img src='/assets/tr8n/minus_node.png'></span>");
1044
- html.push("</div>");
1045
-
1046
- var results = data;
1047
- if (typeof results == 'string') {
1048
- try {
1049
- results = eval("[" + results + "]")[0];
1050
- }
1051
- catch (err) {
1052
- this.push(results);
1053
- return;
1054
- }
1055
- }
1056
- if (typeof results == 'object') {
1057
- html.push(this.formatObject(results, 1));
1058
- } else {
1059
- html.push(results);
1060
- }
1061
- this.append(html.join(""));
1062
- },
1063
- formatObject: function(obj, level) {
1064
- if (obj == null) return "{<br>}";
1065
-
1066
- var html = [];
1067
- var obj_key = this.guid();
1068
- html.push("<span class='tr8n_logger_expander' id='expander_" + obj_key + "' onClick=\"tr8nProxy.logger.toggleNode('" + obj_key + "')\"><img src='/assets/tr8n/minus_node.png'></span> <span style='display:none' id='no_object_" + obj_key + "'>{...}</span> <span id='object_" + obj_key + "'>{");
1069
- this.object_keys.push(obj_key);
1070
-
1071
- var keys = Object.keys(obj).sort();
1072
-
1073
- for (var i=0; i<keys.length; i++) {
1074
- key = keys[i];
1075
- if (this.isObject(obj[key])) {
1076
- if (this.isArray(obj[key])) {
1077
- html.push(this.createSpacer(level) + "<span class='tr8n_logger_obj_key'>" + key + ":</span>" + this.formatArray(obj[key], level + 1) + ",");
1078
- } else {
1079
- html.push(this.createSpacer(level) + "<span class='tr8n_logger_obj_key'>" + key + ":</span>" + this.formatObject(obj[key], level + 1) + ",");
1080
- }
1081
- } else {
1082
- html.push(this.createSpacer(level) + this.formatProperty(key, obj[key]) + ",");
1083
- }
1084
- }
1085
- html.push(this.createSpacer(level-1) + "}</span>");
1086
- return html.join("<br>");
1087
- },
1088
- formatArray: function(arr, level) {
1089
- if (arr == null) return "[<br>]";
1090
-
1091
- var html = [];
1092
- var obj_key = this.guid();
1093
- html.push("<span class='tr8n_logger_expander' id='expander_" + obj_key + "' onClick=\"tr8nProxy.logger.toggleNode('" + obj_key + "')\"><img src='/assets/tr8n/minus_node.png'></span> <span style='display:none' id='no_object_" + obj_key + "'>[...]</span> <span id='object_" + obj_key + "'>[");
1094
- this.object_keys.push(obj_key);
1095
-
1096
- for (var i=0; i<arr.length; i++) {
1097
- if (this.isObject(arr[i])) {
1098
- if (this.isArray(arr[i])) {
1099
- html.push(this.createSpacer(level) + this.formatArray(arr[i], level + 1) + ",");
1100
- } else {
1101
- html.push(this.createSpacer(level) + this.formatObject(arr[i], level + 1) + ",");
1102
- }
1103
- } else {
1104
- html.push(this.createSpacer(level) + this.formatProperty(null, arr[i]) + ",");
1105
- }
1106
- }
1107
- html.push(this.createSpacer(level-1) + "]</span>");
1108
- return html.join("<br>");
1109
- },
1110
- formatProperty: function(key, value) {
1111
- if (value == null) return "<span class='tr8n_logger_obj_key'>" + key + ":</span><span class='obj_value_null'>null</span>";
1112
-
1113
- var cls = "tr8n_logger_obj_value_" + (typeof value);
1114
- var value_span = "";
1115
-
1116
- if (this.isString(value))
1117
- value_span = "<span class='" + cls + "'>\"" + this.escapeHTML(value) + "\"</span>";
1118
- else
1119
- value_span = "<span class='" + cls + "'>" + value + "</span>";
1120
-
1121
- if (key == null)
1122
- return value_span;
1123
-
1124
- return "<span class='tr8n_logger_obj_key'>" + key + ":</span>" + value_span;
1125
- },
1126
- createSpacer: function(level) {
1127
- return "<img src='/assets/tr8n/pixel.gif' style='height:1px;width:" + (level * 20) + "px;'>";
1128
- },
1129
- isArray: function(obj) {
1130
- if (obj == null) return false;
1131
- return !(obj.constructor.toString().indexOf("Array") == -1);
1132
- },
1133
- isObject: function(obj) {
1134
- if (obj == null) return false;
1135
- return (typeof obj == 'object');
1136
- },
1137
- isString: function(obj) {
1138
- return (typeof obj == 'string');
1139
- },
1140
- isURL: function(str) {
1141
- str = "" + str;
1142
- return (str.indexOf("http://") != -1) || (str.indexOf("https://") != -1);
1143
- }
1144
- }
1145
-
1146
-
1147
-
1148
- /****************************************************************************
1149
- **** Tr8n Proxy Utils
1150
- ****************************************************************************/
1151
-
1152
- Tr8n.Proxy.Utils = {
1153
-
1154
- element:function(element_id) {
1155
- if (typeof element_id == 'string') return document.getElementById(element_id);
1156
- return element_id;
1157
- },
1158
-
1159
- hide: function(element_id) {
1160
- Tr8n.element(element_id).style.display = "none";
1161
- },
1162
-
1163
- show: function(element_id) {
1164
- var style = (Tr8n.element(element_id).tagName == "SPAN") ? "inline" : "block";
1165
- Tr8n.element(element_id).style.display = style;
1166
- },
1167
-
1168
- indexOf: function(array, item, i) {
1169
- i || (i = 0);
1170
- var length = array.length;
1171
- if (i < 0) i = length + i;
1172
- for (; i < length; i++)
1173
- if (array[i] === item) return i;
1174
- return -1;
1175
- },
1176
-
1177
- replaceAll: function(label, key, value) {
1178
- while (label.indexOf(key) != -1) {
1179
- label = label.replace(key, value);
1180
- }
1181
- return label;
1182
- },
1183
-
1184
- trim: function(string) {
1185
- return string.replace(/^\s+|\s+$/g,"");
1186
- },
1187
-
1188
- ltrim: function(string) {
1189
- return string.replace(/^\s+/,"");
1190
- },
1191
-
1192
- rtrim: function(string) {
1193
- return string.replace(/\s+$/,"");
1194
- },
1195
-
1196
- addEvent: function(elm, evType, fn, useCapture) {
1197
- useCapture = useCapture || false;
1198
- if (elm.addEventListener) {
1199
- elm.addEventListener(evType, fn, useCapture);
1200
- return true;
1201
- } else if (elm.attachEvent) {
1202
- var r = elm.attachEvent('on' + evType, fn);
1203
- return r;
1204
- } else {
1205
- elm['on' + evType] = fn;
1206
- }
1207
- },
1208
-
1209
- toQueryParams: function (obj) {
1210
- if (typeof obj == 'undefined' || obj == null) return "";
1211
- if (typeof obj == 'string') return obj;
1212
-
1213
- var qs = [];
1214
- for(p in obj) {
1215
- qs.push(p + "=" + encodeURIComponent(obj[p]))
1216
- }
1217
- return qs.join("&")
1218
- },
1219
-
1220
- getRequest: function() {
1221
- var factories = [
1222
- function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
1223
- function() { return new XMLHttpRequest(); },
1224
- function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
1225
- ];
1226
- for(var i = 0; i < factories.length; i++) {
1227
- try {
1228
- var request = factories[i]();
1229
- if (request != null) return request;
1230
- } catch(e) {continue;}
1231
- }
1232
- },
1233
-
1234
- ajax: function(url, options) {
1235
- options = options || {};
1236
- options.parameters = Tr8n.Proxy.Utils.toQueryParams(options.parameters);
1237
- options.method = options.method || 'get';
1238
-
1239
- var self=this;
1240
- if (options.method == 'get' && options.parameters != '') {
1241
- url = url + (url.indexOf('?') == -1 ? '?' : '&') + options.parameters;
1242
- }
1243
-
1244
- var request = this.getRequest();
1245
-
1246
- request.onreadystatechange = function() {
1247
- if(request.readyState == 4) {
1248
- if (request.status == 200) {
1249
- if(options.onSuccess) options.onSuccess(request);
1250
- if(options.onComplete) options.onComplete(request);
1251
- if(options.evalScripts) self.evalScripts(request.responseText);
1252
- } else {
1253
- if(options.onFailure) options.onFailure(request)
1254
- if(options.onComplete) options.onComplete(request)
1255
- }
1256
- }
1257
- }
1258
-
1259
- request.open(options.method, url, true);
1260
- request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
1261
- request.setRequestHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*');
1262
- request.send(options.parameters);
1263
- }
1264
- }
1265
-
1266
- /****************************************************************************
1267
- ***
1268
- *** MD5 (Message-Digest Algorithm)
1269
- *** http://www.webtoolkit.info/
1270
- ***
1271
- ****************************************************************************/
1272
-
1273
- var MD5 = function (string) {
1274
-
1275
- function RotateLeft(lValue, iShiftBits) {
1276
- return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
1277
- }
1278
-
1279
- function AddUnsigned(lX,lY) {
1280
- var lX4,lY4,lX8,lY8,lResult;
1281
- lX8 = (lX & 0x80000000);
1282
- lY8 = (lY & 0x80000000);
1283
- lX4 = (lX & 0x40000000);
1284
- lY4 = (lY & 0x40000000);
1285
- lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);
1286
- if (lX4 & lY4) {
1287
- return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
1288
- }
1289
- if (lX4 | lY4) {
1290
- if (lResult & 0x40000000) {
1291
- return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
1292
- } else {
1293
- return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
1294
- }
1295
- } else {
1296
- return (lResult ^ lX8 ^ lY8);
1297
- }
1298
- }
1299
-
1300
- function F(x,y,z) { return (x & y) | ((~x) & z); }
1301
- function G(x,y,z) { return (x & z) | (y & (~z)); }
1302
- function H(x,y,z) { return (x ^ y ^ z); }
1303
- function I(x,y,z) { return (y ^ (x | (~z))); }
1304
-
1305
- function FF(a,b,c,d,x,s,ac) {
1306
- a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
1307
- return AddUnsigned(RotateLeft(a, s), b);
1308
- };
1309
-
1310
- function GG(a,b,c,d,x,s,ac) {
1311
- a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
1312
- return AddUnsigned(RotateLeft(a, s), b);
1313
- };
1314
-
1315
- function HH(a,b,c,d,x,s,ac) {
1316
- a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
1317
- return AddUnsigned(RotateLeft(a, s), b);
1318
- };
1319
-
1320
- function II(a,b,c,d,x,s,ac) {
1321
- a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
1322
- return AddUnsigned(RotateLeft(a, s), b);
1323
- };
1324
-
1325
- function ConvertToWordArray(string) {
1326
- var lWordCount;
1327
- var lMessageLength = string.length;
1328
- var lNumberOfWords_temp1=lMessageLength + 8;
1329
- var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;
1330
- var lNumberOfWords = (lNumberOfWords_temp2+1)*16;
1331
- var lWordArray=Array(lNumberOfWords-1);
1332
- var lBytePosition = 0;
1333
- var lByteCount = 0;
1334
- while ( lByteCount < lMessageLength ) {
1335
- lWordCount = (lByteCount-(lByteCount % 4))/4;
1336
- lBytePosition = (lByteCount % 4)*8;
1337
- lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<<lBytePosition));
1338
- lByteCount++;
1339
- }
1340
- lWordCount = (lByteCount-(lByteCount % 4))/4;
1341
- lBytePosition = (lByteCount % 4)*8;
1342
- lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);
1343
- lWordArray[lNumberOfWords-2] = lMessageLength<<3;
1344
- lWordArray[lNumberOfWords-1] = lMessageLength>>>29;
1345
- return lWordArray;
1346
- };
1347
-
1348
- function WordToHex(lValue) {
1349
- var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;
1350
- for (lCount = 0;lCount<=3;lCount++) {
1351
- lByte = (lValue>>>(lCount*8)) & 255;
1352
- WordToHexValue_temp = "0" + lByte.toString(16);
1353
- WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);
1354
- }
1355
- return WordToHexValue;
1356
- };
1357
-
1358
- function Utf8Encode(string) {
1359
- string = string.replace(/\r\n/g,"\n");
1360
- var utftext = "";
1361
-
1362
- for (var n = 0; n < string.length; n++) {
1363
-
1364
- var c = string.charCodeAt(n);
1365
-
1366
- if (c < 128) {
1367
- utftext += String.fromCharCode(c);
1368
- }
1369
- else if((c > 127) && (c < 2048)) {
1370
- utftext += String.fromCharCode((c >> 6) | 192);
1371
- utftext += String.fromCharCode((c & 63) | 128);
1372
- }
1373
- else {
1374
- utftext += String.fromCharCode((c >> 12) | 224);
1375
- utftext += String.fromCharCode(((c >> 6) & 63) | 128);
1376
- utftext += String.fromCharCode((c & 63) | 128);
1377
- }
1378
-
1379
- }
1380
-
1381
- return utftext;
1382
- };
1383
-
1384
- var x=Array();
1385
- var k,AA,BB,CC,DD,a,b,c,d;
1386
- var S11=7, S12=12, S13=17, S14=22;
1387
- var S21=5, S22=9 , S23=14, S24=20;
1388
- var S31=4, S32=11, S33=16, S34=23;
1389
- var S41=6, S42=10, S43=15, S44=21;
1390
-
1391
- string = Utf8Encode(string);
1392
-
1393
- x = ConvertToWordArray(string);
1394
-
1395
- a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;
1396
-
1397
- for (k=0;k<x.length;k+=16) {
1398
- AA=a; BB=b; CC=c; DD=d;
1399
- a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);
1400
- d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);
1401
- c=FF(c,d,a,b,x[k+2], S13,0x242070DB);
1402
- b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE);
1403
- a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF);
1404
- d=FF(d,a,b,c,x[k+5], S12,0x4787C62A);
1405
- c=FF(c,d,a,b,x[k+6], S13,0xA8304613);
1406
- b=FF(b,c,d,a,x[k+7], S14,0xFD469501);
1407
- a=FF(a,b,c,d,x[k+8], S11,0x698098D8);
1408
- d=FF(d,a,b,c,x[k+9], S12,0x8B44F7AF);
1409
- c=FF(c,d,a,b,x[k+10],S13,0xFFFF5BB1);
1410
- b=FF(b,c,d,a,x[k+11],S14,0x895CD7BE);
1411
- a=FF(a,b,c,d,x[k+12],S11,0x6B901122);
1412
- d=FF(d,a,b,c,x[k+13],S12,0xFD987193);
1413
- c=FF(c,d,a,b,x[k+14],S13,0xA679438E);
1414
- b=FF(b,c,d,a,x[k+15],S14,0x49B40821);
1415
- a=GG(a,b,c,d,x[k+1], S21,0xF61E2562);
1416
- d=GG(d,a,b,c,x[k+6], S22,0xC040B340);
1417
- c=GG(c,d,a,b,x[k+11],S23,0x265E5A51);
1418
- b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA);
1419
- a=GG(a,b,c,d,x[k+5], S21,0xD62F105D);
1420
- d=GG(d,a,b,c,x[k+10],S22,0x2441453);
1421
- c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681);
1422
- b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8);
1423
- a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6);
1424
- d=GG(d,a,b,c,x[k+14],S22,0xC33707D6);
1425
- c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87);
1426
- b=GG(b,c,d,a,x[k+8], S24,0x455A14ED);
1427
- a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905);
1428
- d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8);
1429
- c=GG(c,d,a,b,x[k+7], S23,0x676F02D9);
1430
- b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A);
1431
- a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942);
1432
- d=HH(d,a,b,c,x[k+8], S32,0x8771F681);
1433
- c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122);
1434
- b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C);
1435
- a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44);
1436
- d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9);
1437
- c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60);
1438
- b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70);
1439
- a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6);
1440
- d=HH(d,a,b,c,x[k+0], S32,0xEAA127FA);
1441
- c=HH(c,d,a,b,x[k+3], S33,0xD4EF3085);
1442
- b=HH(b,c,d,a,x[k+6], S34,0x4881D05);
1443
- a=HH(a,b,c,d,x[k+9], S31,0xD9D4D039);
1444
- d=HH(d,a,b,c,x[k+12],S32,0xE6DB99E5);
1445
- c=HH(c,d,a,b,x[k+15],S33,0x1FA27CF8);
1446
- b=HH(b,c,d,a,x[k+2], S34,0xC4AC5665);
1447
- a=II(a,b,c,d,x[k+0], S41,0xF4292244);
1448
- d=II(d,a,b,c,x[k+7], S42,0x432AFF97);
1449
- c=II(c,d,a,b,x[k+14],S43,0xAB9423A7);
1450
- b=II(b,c,d,a,x[k+5], S44,0xFC93A039);
1451
- a=II(a,b,c,d,x[k+12],S41,0x655B59C3);
1452
- d=II(d,a,b,c,x[k+3], S42,0x8F0CCC92);
1453
- c=II(c,d,a,b,x[k+10],S43,0xFFEFF47D);
1454
- b=II(b,c,d,a,x[k+1], S44,0x85845DD1);
1455
- a=II(a,b,c,d,x[k+8], S41,0x6FA87E4F);
1456
- d=II(d,a,b,c,x[k+15],S42,0xFE2CE6E0);
1457
- c=II(c,d,a,b,x[k+6], S43,0xA3014314);
1458
- b=II(b,c,d,a,x[k+13],S44,0x4E0811A1);
1459
- a=II(a,b,c,d,x[k+4], S41,0xF7537E82);
1460
- d=II(d,a,b,c,x[k+11],S42,0xBD3AF235);
1461
- c=II(c,d,a,b,x[k+2], S43,0x2AD7D2BB);
1462
- b=II(b,c,d,a,x[k+9], S44,0xEB86D391);
1463
- a=AddUnsigned(a,AA);
1464
- b=AddUnsigned(b,BB);
1465
- c=AddUnsigned(c,CC);
1466
- d=AddUnsigned(d,DD);
1467
- }
1468
-
1469
- var temp = WordToHex(a)+WordToHex(b)+WordToHex(c)+WordToHex(d);
1470
-
1471
- return temp.toLowerCase();
1472
- }