j1-template 2022.0.2 → 2022.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/_includes/themes/j1/modules/connectors/analytic/google-analytics.html +3 -13
  3. data/_includes/themes/j1/modules/connectors/comment/hyvor.html +4 -5
  4. data/_includes/themes/j1/procedures/global/create_bs_button.proc +4 -4
  5. data/assets/data/private.json +1 -5
  6. data/assets/themes/j1/adapter/js/cookieConsent.js +12 -9
  7. data/assets/themes/j1/adapter/js/j1.js +17 -17
  8. data/assets/themes/j1/adapter/js/translator.js +4 -2
  9. data/assets/themes/j1/core/js/template.js +59 -0
  10. data/assets/themes/j1/core/js/template.min.js +5 -5
  11. data/assets/themes/j1/core/js/template.min.js.map +1 -1
  12. data/assets/themes/j1/modules/cryptoJS/js/aes.js +35 -0
  13. data/assets/themes/j1/modules/cryptoJS/js/aes.min.js +35 -0
  14. data/assets/themes/j1/modules/cryptoJS/js/hmac-md5.js +21 -0
  15. data/assets/themes/j1/modules/cryptoJS/js/hmac-ripemd160.js +31 -0
  16. data/assets/themes/j1/modules/cryptoJS/js/hmac-sha1.js +17 -0
  17. data/assets/themes/j1/modules/cryptoJS/js/hmac-sha224.js +19 -0
  18. data/assets/themes/j1/modules/cryptoJS/js/hmac-sha256.js +18 -0
  19. data/assets/themes/j1/modules/cryptoJS/js/hmac-sha3.js +21 -0
  20. data/assets/themes/j1/modules/cryptoJS/js/hmac-sha384.js +27 -0
  21. data/assets/themes/j1/modules/cryptoJS/js/hmac-sha512.js +25 -0
  22. data/assets/themes/j1/modules/cryptoJS/js/md5.js +19 -0
  23. data/assets/themes/j1/modules/cryptoJS/js/pbkdf2.js +19 -0
  24. data/assets/themes/j1/modules/cryptoJS/js/rabbit-legacy.js +36 -0
  25. data/assets/themes/j1/modules/cryptoJS/js/rabbit.js +36 -0
  26. data/assets/themes/j1/modules/cryptoJS/js/rc4.js +33 -0
  27. data/assets/themes/j1/modules/cryptoJS/js/ripemd160.js +29 -0
  28. data/assets/themes/j1/modules/cryptoJS/js/sha1.js +15 -0
  29. data/assets/themes/j1/modules/cryptoJS/js/sha224.js +17 -0
  30. data/assets/themes/j1/modules/cryptoJS/js/sha256.js +16 -0
  31. data/assets/themes/j1/modules/cryptoJS/js/sha3.js +19 -0
  32. data/assets/themes/j1/modules/cryptoJS/js/sha384.js +25 -0
  33. data/assets/themes/j1/modules/cryptoJS/js/sha512.js +23 -0
  34. data/assets/themes/j1/modules/cryptoJS/js/tripledes.js +51 -0
  35. data/assets/themes/j1/modules/deeplAPI/js/deeplAPI.2.js +460 -0
  36. data/assets/themes/j1/modules/deeplAPI/js/deeplAPI.js +379 -353
  37. data/assets/themes/j1/modules/deeplAPI/js/deeplAPI.min.js +1 -1
  38. data/assets/themes/j1/modules/gtag-opt-in/js/gtag-opt-in.js +93 -1
  39. data/assets/themes/j1/modules/scroller/js/scroller.js +6 -6
  40. data/assets/themes/j1/modules/scroller/js/scroller.min.js +1 -1
  41. data/exe/j1 +6 -7
  42. data/lib/j1/commands/generate.rb +15 -16
  43. data/lib/j1/version.rb +1 -1
  44. data/lib/starter_web/Gemfile +7 -1
  45. data/lib/starter_web/_config.yml +1 -1
  46. data/lib/starter_web/_data/_defaults/private.yml +3 -2
  47. data/lib/starter_web/_data/builder/defaults/_blog_navigator.yml +396 -0
  48. data/lib/starter_web/_data/builder/defaults/blog_navigator.yml +48 -42
  49. data/lib/starter_web/_data/modules/translator.yml +3 -3
  50. data/lib/starter_web/_data/private.yml +3 -2
  51. data/lib/starter_web/_data/resources.yml +27 -2
  52. data/lib/starter_web/_includes/attributes.asciidoc +1 -1
  53. data/lib/starter_web/_plugins/encodeBase64.rb +46 -0
  54. data/lib/starter_web/_plugins/encryptAES.rb +53 -0
  55. data/lib/starter_web/_plugins/filters.rb +2 -1
  56. data/lib/starter_web/_plugins/lunr_index.rb +1 -1
  57. data/lib/starter_web/_plugins/minifyJSON.rb +2 -2
  58. data/lib/starter_web/package.json +1 -1
  59. data/lib/starter_web/pages/public/asciidoc_skeletons/documentation/documentation.adoc +1 -1
  60. data/lib/starter_web/pages/public/asciidoc_skeletons/multi-document/multi.adoc +1 -1
  61. data/lib/starter_web/pages/public/asciidoc_skeletons/simple-document/simple.adoc +1 -1
  62. data/lib/starter_web/pages/public/blog/navigator/archive/categoryview.html +0 -21
  63. data/lib/starter_web/pages/public/blog/navigator/archive/dateview.html +1 -1
  64. data/lib/starter_web/pages/public/blog/navigator/archive/tagview.html +1 -0
  65. data/lib/starter_web/pages/public/blog/navigator/archive.html +1 -1
  66. data/lib/starter_web/pages/public/learn/quickstart.adoc +1 -1
  67. data/lib/starter_web/pages/public/learn/where_to_go.adoc +1 -1
  68. data/lib/starter_web/pages/public/legal/de/100_copyright.adoc +1 -1
  69. data/lib/starter_web/pages/public/legal/de/100_impress.adoc +1 -1
  70. data/lib/starter_web/pages/public/legal/de/300_privacy.adoc +1 -1
  71. data/lib/starter_web/pages/public/legal/de/400_comment_policy.adoc +1 -1
  72. data/lib/starter_web/pages/public/legal/en/100_copyright.adoc +1 -1
  73. data/lib/starter_web/pages/public/legal/en/200_impress.adoc +1 -1
  74. data/lib/starter_web/pages/public/legal/en/300_privacy.adoc +1 -1
  75. data/lib/starter_web/pages/public/legal/en/400_comment_policy.adoc +1 -1
  76. data/lib/starter_web/utilsrv/_defaults/package.json +1 -1
  77. data/lib/starter_web/utilsrv/package.json +1 -1
  78. metadata +30 -5
  79. data/lib/j1/commands/help.rb +0 -32
  80. data/lib/j1/commands/module.rb +0 -69
@@ -21,393 +21,419 @@
21
21
  // properly.
22
22
  //
23
23
  ;(function($, window, document, undefined) {
24
-
25
- 'use strict';
26
-
27
- // Create the defaults
28
- var pluginName = 'deeplAPI',
29
- defaults = {
30
- api: 'free', // free (default) | pro
31
- auth_key: '', // API authorization key
32
- source_lang: 'auto', // autodetection (default: auto)|supported language. Specifies the language for the input text.
33
- target_lang: 'DE', // language to be tranlasted in.
34
- max_chars: false, // false (unlimited) or number. Number of chars from the source text passed for translation.
35
- split_sentences: '1', // enabled (1, default)|disabled (0)|nonewlines. Sets the translation engine to first split the input text into sentences.
36
- preserve_formatting: '0', // disabled (0, default)|enabled (1). Sets the translation engine to respect the original formatting.
37
- formality: 'default', // default|more|less. Sets the translated text should lean towards formal or informal language.
38
- tag_handling: false, // false (value=0)|xml. Sets which kind of tags should be handled. If set, API is able to process structured XML content.
39
- outline_detection: true, // false (value=0) or true (NO value passed). Controls the automatic mechanism on XML tags for splitting. If disabled, all splitting_tags are to be specified.
40
- non_splitting_tags: false, // false or comma-separated list of XML tags. Disable automated splitting on the tags specified.
41
- splitting_tags: false, // false or comma-separated list of XML tags which always cause splitting.
42
- ignore_tags: false, // false or comma-separated list of XML tags that indicate text NOT to be translated.
43
- onInit: function (){}, // callback after plugin has initialized.
44
- onBeforeTranslation: function (){}, // callback before translation started.
45
- onAfterTranslation: function (){} // callback after translation finished.
46
- };
47
-
48
- // -------------------------------------------------------------------------
49
- // plugin constructor
50
- // create the jquery plugin
51
- // -------------------------------------------------------------------------
52
- function Plugin (element, options) {
53
- this.element = element;
54
- this.settings = $.extend( {}, defaults, options);
55
- this.settings.elementID = '#' + this.element.id;
56
- this.xhr = new XMLHttpRequest();
57
-
58
- // call the plugin initializer
59
- this.init(this.settings);
60
- }
61
-
62
- // Avoid plugin prototype conflicts
63
- $.extend(Plugin.prototype, {
64
- // -----------------------------------------------------------------------
65
- // init()
66
- // initialize|run the translation
67
- // -----------------------------------------------------------------------
68
- init: function(options) {
69
- var _this = this;
70
- var logger = log4javascript.getLogger('deeplAPI.init');
71
- var data_url = '/assets/data/private.json';
72
- var settings = options;
73
- var auth_key;
74
-
75
- logger.info('\n' + 'initializing plugin: started');
76
-
77
- // loading private data (auth key)
78
- $.ajax({
79
- url: data_url,
80
- dataType: 'json',
81
- success: function (data) {
82
- auth_key = data.translators.deepl.auth_key;
83
- },
84
- error: function (jqXHR, textStatus, errorThrown) {
85
- logger.error('\n' + 'failed to retrieve Yaml data from: ' + data_url);
86
- }
87
- });
88
-
89
- // run the translation (if private data loaded)
90
- var dependencies_met_page_ready = setInterval (function () {
91
- if (typeof auth_key !== 'undefined' ) {
92
- settings.auth_key = auth_key;
93
- _this.translate(settings);
94
- logger.info('\n' + 'translation: in progress');
95
- clearInterval(dependencies_met_page_ready);
96
- }
97
- });
98
- },
99
-
100
- // -----------------------------------------------------------------------
101
- // prepareXHR
102
- // Prepare a XHR request according to DeepL API specification
103
- // for the required HTTP header settings
104
- // See: https://www.deepl.com/de/docs-api/simulator/
105
- // -----------------------------------------------------------------------
106
- // NOTE: "User-Agent" NOT used as the use is qualified UNSECURE
107
- // -----------------------------------------------------------------------
108
- prepareXHR: function (settings) {
109
- if (settings.api === 'free') {
110
- this.xhr.open("POST", "https://api-free.deepl.com/v2/translate", true);
111
- } else if (settings.api === 'pro') {
112
- this.xhr.open("POST", "https://api.deepl.com/v2/translate", true);
113
- } else {
114
- this.xhr.open("POST", "https://api-free.deepl.com/v2/translate", true);
24
+ 'use strict';
25
+
26
+ // Create the defaults
27
+ var pluginName = 'deeplAPI',
28
+ defaults = {
29
+ api: 'free', // free (default) | pro
30
+ auth_key: '', // API authorization key
31
+ source_lang: 'auto', // autodetection (default: auto)|supported language. Specifies the language for the input text.
32
+ target_lang: 'DE', // language to be tranlasted in.
33
+ max_chars: false, // false (unlimited) or number. Number of chars from the source text passed for translation.
34
+ split_sentences: '1', // enabled (1, default)|disabled (0)|nonewlines. Sets the translation engine to first split the input text into sentences.
35
+ preserve_formatting: '0', // disabled (0, default)|enabled (1). Sets the translation engine to respect the original formatting.
36
+ formality: 'default', // default|more|less. Sets the translated text should lean towards formal or informal language.
37
+ tag_handling: false, // false (value=0)|xml. Sets which kind of tags should be handled. If set, API is able to process structured XML content.
38
+ outline_detection: true, // false (value=0) or true (NO value passed). Controls the automatic mechanism on XML tags for splitting. If disabled, all splitting_tags are to be specified.
39
+ non_splitting_tags: false, // false or comma-separated list of XML tags. Disable automated splitting on the tags specified.
40
+ splitting_tags: false, // false or comma-separated list of XML tags which always cause splitting.
41
+ ignore_tags: false, // false or comma-separated list of XML tags that indicate text NOT to be translated.
42
+ onInit: function (){}, // callback after plugin has initialized.
43
+ onBeforeTranslation: function (){}, // callback before translation started.
44
+ onAfterTranslation: function (){} // callback after translation finished.
45
+ };
46
+
47
+ // -------------------------------------------------------------------------
48
+ // plugin constructor
49
+ // create the jquery plugin
50
+ // -------------------------------------------------------------------------
51
+ function Plugin (element, options) {
52
+ this.element = element;
53
+ this.settings = $.extend( {}, defaults, options);
54
+ this.settings.elementID = '#' + this.element.id;
55
+ this.xhr = new XMLHttpRequest();
56
+
57
+ // call the plugin initializer
58
+ this.init(this.settings);
59
+ }
60
+
61
+ // Avoid plugin prototype conflicts
62
+ $.extend(Plugin.prototype, {
63
+ // -----------------------------------------------------------------------
64
+ // init()
65
+ // initialize|run the translation
66
+ // -----------------------------------------------------------------------
67
+ init: function(options) {
68
+ var _this = this;
69
+ var logger = log4javascript.getLogger('deeplAPI.init');
70
+ var data_url = '/assets/data/private.json';
71
+ var settings = options;
72
+ var auth_key;
73
+ var encodedString;
74
+ var encryptedString;
75
+ var jsonObject;
76
+
77
+ logger.info('\n' + 'initialize: started');
78
+
79
+ // loading private data (auth key)
80
+ $.ajax({
81
+ url: data_url,
82
+ dataType: 'text',
83
+ success: function (data) {
84
+ encodedString = atob(data);
85
+ jsonObject = JSON.parse(encodedString);
86
+ auth_key = jsonObject.translators.deepl.auth_key;
87
+ },
88
+ error: function (jqXHR, textStatus, errorThrown) {
89
+ logger.error('\n' + 'failed to retrieve JSON data from: ' + data_url);
115
90
  }
116
- // Set required header parameters
117
- this.xhr.setRequestHeader("Accept", "*/*");
118
- this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
119
- },
120
-
121
- // -----------------------------------------------------------------------
122
- // prepareText
123
- // Prepare text function used to parse, or arrange text, designed as
124
- // a module. Currently it splits all text whenever a newline ("\n")
125
- // is met, so that it preserves the original layout of the text, which
126
- // would have otherwise been lost because of the way DeepL accepts
127
- // multiple sentences.
128
- // -----------------------------------------------------------------------
129
- prepareText: function (source_text) {
130
- return source_text.split("\n");
131
- },
132
-
133
- // -----------------------------------------------------------------------
134
- // translate
135
- // Translate text function which uses all the other modules, in order to
136
- // create a request, which is sent to the DeepL API to translate, and
137
- // then display the result, designed as a module.
138
- // -----------------------------------------------------------------------
139
- translate: function (settings) {
140
- const logger = log4javascript.getLogger('deeplAPI.translate');
141
- const READYSTATE_DONE = 4;
142
- const STATUS_OK = 200;
143
- const SUPPORTED_LANG = ['BG', 'CS', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'IT', 'JA', 'LT', 'LV', 'NL', 'PL', 'PT-PT', 'PT-BR', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'ZH'];
144
- const ALLOWED_FORMALITY_LANG = ['DE', 'FR', 'IT', 'ES', 'NL', 'PL', 'PT', 'PT-BR', 'RU'];
145
- const ALLOWED_TAG_HANDLING = ['xml'];
146
- const TARGET_ELEMENT = settings.targetElement;
147
- const TARGET_ELEMENT_EXISTS = $(TARGET_ELEMENT).length;
148
- const ERROR_TEXT = 'Translation failed.' + '\n' + 'Reason: ';
149
- const WARNING_TEXT = 'Translation skipped.' + '\n' + 'Reason: ';
150
-
151
- var BASE_TARGET_ELEMENT;
152
- var ELEMENT_TYPE;
153
- var SOURCE_TEXT_FOUND;
154
-
155
- var API_RESPONSE = {};
156
- API_RESPONSE['400'] = 'Bad request. Please check error message and your parameters.';
157
- API_RESPONSE['401'] = 'Authorization failed. Please supply a valid DeepL-Auth-Key.';
158
- API_RESPONSE['403'] = 'Forbidden. The access to the requested resource is denied, because of insufficient access rights.';
159
- API_RESPONSE['404'] = 'The requested resource could not be found.';
160
- API_RESPONSE['413'] = 'The request size exceeds the limit.';
161
- API_RESPONSE['415'] = 'The requested entries format specified in the Accept header is not supported.';
162
- API_RESPONSE['429'] = 'Too many requests. Please wait and resend your request.';
163
- API_RESPONSE['456'] = 'Quota exceeded. The maximum amount of glossaries has been reached.';
164
- API_RESPONSE['500'] = 'Internal server error';
165
- API_RESPONSE['503'] = 'Resource currently unavailable. Try again later.';
166
- API_RESPONSE['529'] = 'Too many requests. Please wait and resend your request.';
167
-
168
- var reason_text;
169
- var request = '';
170
- var element = '';
171
- var source_text = '';
172
- var source_text_lines = '';
173
- var source_lang;
174
- var target_lang;
175
-
176
- // check if passed HTML element or ID exists
177
- if (TARGET_ELEMENT_EXISTS) {
178
- BASE_TARGET_ELEMENT = (TARGET_ELEMENT.includes('.')||TARGET_ELEMENT.includes('#')) ? TARGET_ELEMENT.substring(1) : TARGET_ELEMENT;
179
- ELEMENT_TYPE = $(TARGET_ELEMENT).get(0).nodeName;
180
-
181
- // Read the text to be translated from the given HTML element
182
- if (ELEMENT_TYPE === 'TEXTAREA') {
183
- source_text = this.element.value;
184
- } else if (ELEMENT_TYPE === 'P') {
185
- element = this.element;
186
- source_text = $(TARGET_ELEMENT).text();
187
- }
188
- SOURCE_TEXT_FOUND = source_text.length;
189
- } else {
190
- logger.error('\n' + 'target element does not exists: ' + TARGET_ELEMENT);
191
- return false;
91
+ });
92
+
93
+ // run the translation (if private data loaded)
94
+ var dependencies_met_page_ready = setInterval (function () {
95
+ if (typeof auth_key !== 'undefined' ) {
96
+ settings.auth_key = auth_key;
97
+ _this.translate(settings);
98
+ logger.info('\n' + 'initialize: finished');
99
+ logger.info('\n' + 'translate: started');
100
+ clearInterval(dependencies_met_page_ready);
192
101
  }
193
-
194
- // limit the source text if required
195
- if (settings.max_char && source_text.length > settings.max_char ) {
196
- var source_text_limited = source_text.substring(0, settings.max_char -3);
197
- source_text = source_text_limited + ' ...';
198
- logger.info('\n' + 'limit for source text ' + '(max: ' + settings.max_char + ')' + ' reached: ' + source_text.length);
102
+ });
103
+ },
104
+
105
+ // decryptData: function (message = '', key = '', iv = '') {
106
+ // var test_key_32 = "2e35f242a46d67eeb74aabc37d5e5d05";
107
+ // var data = CryptoJS.AES.encrypt("Message", test_key_32); // Encryption Part
108
+ // var decrypted = CryptoJS.AES.decrypt(data, test_key_32).toString(CryptoJS.enc.Utf8);
109
+ //
110
+ // // Decode the base64 data so we can separate iv and crypt text.
111
+ // var rawData = atob(message);
112
+ // var iv = rawData.substring(0,16);
113
+ // var crypttext = rawData.substring(16);
114
+ //
115
+ // // Decrypt...
116
+ // var plaintextArray = CryptoJS.AES.decrypt(
117
+ // { ciphertext: CryptoJS.enc.Latin1.parse(crypttext) },
118
+ // CryptoJS.enc.Hex.parse(key),
119
+ // { iv: CryptoJS.enc.Latin1.parse(iv) }
120
+ // );
121
+ //
122
+ // var decrypted = CryptoJS.enc.Latin1.stringify(plaintextArray);
123
+ // return decrypted;
124
+ // },
125
+
126
+ // -----------------------------------------------------------------------
127
+ // prepareXHR
128
+ // Prepare a XHR request according to DeepL API specification
129
+ // for the required HTTP header settings
130
+ // See: https://www.deepl.com/de/docs-api/simulator/
131
+ // -----------------------------------------------------------------------
132
+ // NOTE: "User-Agent" NOT used as the use is qualified UNSECURE
133
+ // -----------------------------------------------------------------------
134
+ prepareXHR: function (settings) {
135
+ if (settings.api === 'free') {
136
+ this.xhr.open("POST", "https://api-free.deepl.com/v2/translate", true);
137
+ } else if (settings.api === 'pro') {
138
+ this.xhr.open("POST", "https://api.deepl.com/v2/translate", true);
139
+ } else {
140
+ this.xhr.open("POST", "https://api-free.deepl.com/v2/translate", true);
141
+ }
142
+ // Set required header parameters
143
+ this.xhr.setRequestHeader("Accept", "*/*");
144
+ this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
145
+ },
146
+
147
+ // -----------------------------------------------------------------------
148
+ // prepareText
149
+ // Prepare text function used to parse, or arrange text, designed as
150
+ // a module. Currently it splits all text whenever a newline ("\n")
151
+ // is met, so that it preserves the original layout of the text, which
152
+ // would have otherwise been lost because of the way DeepL accepts
153
+ // multiple sentences.
154
+ // -----------------------------------------------------------------------
155
+ prepareText: function (source_text) {
156
+ return source_text.split("\n");
157
+ },
158
+
159
+ // -----------------------------------------------------------------------
160
+ // translate
161
+ // Translate text function which uses all the other modules, in order to
162
+ // create a request, which is sent to the DeepL API to translate, and
163
+ // then display the result, designed as a module.
164
+ // -----------------------------------------------------------------------
165
+ translate: function (settings) {
166
+ const logger = log4javascript.getLogger('deeplAPI.translate');
167
+ const READYSTATE_DONE = 4;
168
+ const STATUS_OK = 200;
169
+ const SUPPORTED_LANG = ['BG', 'CS', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'IT', 'JA', 'LT', 'LV', 'NL', 'PL', 'PT-PT', 'PT-BR', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'ZH'];
170
+ const ALLOWED_FORMALITY_LANG = ['DE', 'FR', 'IT', 'ES', 'NL', 'PL', 'PT', 'PT-BR', 'RU'];
171
+ const ALLOWED_TAG_HANDLING = ['xml'];
172
+ const TARGET_ELEMENT = settings.targetElement;
173
+ const TARGET_ELEMENT_EXISTS = $(TARGET_ELEMENT).length;
174
+ const ERROR_TEXT = 'Translation failed.' + '\n' + 'Reason: ';
175
+ const WARNING_TEXT = 'Translation skipped.' + '\n' + 'Reason: ';
176
+
177
+ var BASE_TARGET_ELEMENT;
178
+ var ELEMENT_TYPE;
179
+ var SOURCE_TEXT_FOUND;
180
+
181
+ var API_RESPONSE = {};
182
+ API_RESPONSE['400'] = 'Bad request. Please check error message and your parameters.';
183
+ API_RESPONSE['401'] = 'Authorization failed. Please supply a valid DeepL-Auth-Key.';
184
+ API_RESPONSE['403'] = 'Forbidden. The access to the requested resource is denied, because of insufficient access rights.';
185
+ API_RESPONSE['404'] = 'The requested resource could not be found.';
186
+ API_RESPONSE['413'] = 'The request size exceeds the limit.';
187
+ API_RESPONSE['415'] = 'The requested entries format specified in the Accept header is not supported.';
188
+ API_RESPONSE['429'] = 'Too many requests. Please wait and resend your request.';
189
+ API_RESPONSE['456'] = 'Quota exceeded. The maximum amount of glossaries has been reached.';
190
+ API_RESPONSE['500'] = 'Internal server error';
191
+ API_RESPONSE['503'] = 'Resource currently unavailable. Try again later.';
192
+ API_RESPONSE['529'] = 'Too many requests. Please wait and resend your request.';
193
+
194
+ var reason_text;
195
+ var request = '';
196
+ var element = '';
197
+ var source_text = '';
198
+ var source_text_lines = '';
199
+ var source_lang;
200
+ var target_lang;
201
+
202
+ // check if passed HTML element or ID exists
203
+ if (TARGET_ELEMENT_EXISTS) {
204
+ BASE_TARGET_ELEMENT = (TARGET_ELEMENT.includes('.')||TARGET_ELEMENT.includes('#')) ? TARGET_ELEMENT.substring(1) : TARGET_ELEMENT;
205
+ ELEMENT_TYPE = $(TARGET_ELEMENT).get(0).nodeName;
206
+
207
+ // Read the text to be translated from the given HTML element
208
+ if (ELEMENT_TYPE === 'TEXTAREA') {
209
+ source_text = this.element.value;
210
+ } else if (ELEMENT_TYPE === 'P') {
211
+ element = this.element;
212
+ source_text = $(TARGET_ELEMENT).text();
199
213
  }
200
- source_text_lines = this.prepareText(source_text);
214
+ SOURCE_TEXT_FOUND = source_text.length;
215
+ } else {
216
+ logger.error('\n' + 'target element does not exists: ' + TARGET_ELEMENT);
217
+ return false;
218
+ }
219
+
220
+ // limit the source text if required
221
+ if (settings.max_char && source_text.length > settings.max_char ) {
222
+ var source_text_limited = source_text.substring(0, settings.max_char -3);
223
+ source_text = source_text_limited + ' ...';
224
+ logger.info('\n' + 'limit for source text ' + '(max: ' + settings.max_char + ')' + ' reached: ' + source_text.length);
225
+ }
226
+ source_text_lines = this.prepareText(source_text);
201
227
 
202
- // prepare the XHR request for the API (free/pro) requested
203
- this.prepareXHR(settings);
228
+ // prepare the XHR request for the API (free/pro) requested
229
+ this.prepareXHR(settings);
204
230
 
205
- // Makes a request with every line, as a new text to translate
206
- var source_text_request = "";
207
- for(var i = 0; i < source_text_lines.length; i++) {
208
- source_text_request += "&text=" + source_text_lines[i];
209
- }
231
+ // Makes a request with every line, as a new text to translate
232
+ var source_text_request = "";
233
+ for(var i = 0; i < source_text_lines.length; i++) {
234
+ source_text_request += "&text=" + source_text_lines[i];
235
+ }
210
236
 
211
- this.xhr.onload = function () {
212
- if (this.readyState === READYSTATE_DONE) {
213
- if (this.status === STATUS_OK) {
214
- // JSON parse the response
215
- var result = JSON.parse(this.responseText);
216
-
217
- // recreate the response as one text to keep its original layout
218
- var translated_text = "";
219
- for(var i = 0; i < result.translations.length; i++) {
220
- translated_text += result.translations[i].text;
221
- translated_text += "\n";
222
- }
223
- logger.info('\n' + 'translation: finished');
224
-
225
- // update the HTM element (content) by the tranlation
226
- if (ELEMENT_TYPE === 'TEXTAREA') {
227
- $(TARGET_ELEMENT).val(translated_text);
228
- } else if (ELEMENT_TYPE === 'P') {
229
- $(TARGET_ELEMENT).text(translated_text);
230
- }
231
- } else {
232
- logger.error('\n' + 'API returned ' + this.status + ': ' + API_RESPONSE[this.status]);
237
+ this.xhr.onload = function () {
238
+ if (this.readyState === READYSTATE_DONE) {
239
+ if (this.status === STATUS_OK) {
240
+ // JSON parse the response
241
+ var result = JSON.parse(this.responseText);
242
+
243
+ // recreate the response as one text to keep its original layout
244
+ var translated_text = "";
245
+ for(var i = 0; i < result.translations.length; i++) {
246
+ translated_text += result.translations[i].text;
247
+ translated_text += "\n";
233
248
  }
249
+ logger.info('\n' + 'translation: finished');
250
+
251
+ // update the HTM element (content) by the tranlation
252
+ if (ELEMENT_TYPE === 'TEXTAREA') {
253
+ $(TARGET_ELEMENT).val(translated_text);
254
+ } else if (ELEMENT_TYPE === 'P') {
255
+ $(TARGET_ELEMENT).text(translated_text);
256
+ }
257
+ } else {
258
+ logger.error('\n' + 'API returned ' + this.status + ': ' + API_RESPONSE[this.status]);
234
259
  }
235
- };
260
+ }
261
+ };
236
262
 
237
- // ---------------------------------------------------------------------
238
- // preflight (validity check) on given parameters
239
- // ---------------------------------------------------------------------
263
+ // ---------------------------------------------------------------------
264
+ // preflight (validity check) on given parameters
265
+ // ---------------------------------------------------------------------
240
266
 
241
- // Check for SUPPORTED language by option source_lang|target_lang
242
- source_lang = this.settings.source_lang;
243
- target_lang = this.settings.target_lang;
267
+ // Check for SUPPORTED language by option source_lang|target_lang
268
+ source_lang = this.settings.source_lang;
269
+ target_lang = this.settings.target_lang;
244
270
 
245
- if (this.settings.source_lang !== 'auto') {
246
- this.settings.source_lang = (SUPPORTED_LANG.indexOf(this.settings.source_lang) > -1) ? this.settings.source_lang : false;
247
- }
248
- this.settings.target_lang = (SUPPORTED_LANG.indexOf(this.settings.target_lang) > -1) ? this.settings.target_lang : false;
271
+ if (this.settings.source_lang !== 'auto') {
272
+ this.settings.source_lang = (SUPPORTED_LANG.indexOf(this.settings.source_lang) > -1) ? this.settings.source_lang : false;
273
+ }
274
+ this.settings.target_lang = (SUPPORTED_LANG.indexOf(this.settings.target_lang) > -1) ? this.settings.target_lang : false;
249
275
 
250
- // stop processing if invalid language or NO AUTH key passed
251
- if (!this.settings.auth_key || !this.settings.source_lang || !this.settings.target_lang) {
252
- if (!this.settings.auth_key) {
253
- reason_text = 'NO AUTH key passed.';
254
- logger.error('\n' + 'invalid option found. ' + reason_text);
255
- }
256
- if (!this.settings.source_lang) {
257
- reason_text = 'WRONG source language passed: ' + source_lang;
258
- logger.error('\n' + 'invalid option found. ' + reason_text);
259
- }
260
- if (!this.settings.target_lang) {
261
- reason_text = 'WRONG target language passed: ' + target_lang;
262
- logger.error('\n' + 'invalid option found. ' + reason_text);
263
- }
276
+ // stop processing if invalid language or NO AUTH key passed
277
+ if (!this.settings.auth_key || !this.settings.source_lang || !this.settings.target_lang) {
278
+ if (!this.settings.auth_key) {
279
+ reason_text = 'NO AUTH key passed.';
280
+ logger.error('\n' + 'invalid option found. ' + reason_text);
281
+ }
282
+ if (!this.settings.source_lang) {
283
+ reason_text = 'WRONG source language passed: ' + source_lang;
284
+ logger.error('\n' + 'invalid option found. ' + reason_text);
285
+ }
286
+ if (!this.settings.target_lang) {
287
+ reason_text = 'WRONG target language passed: ' + target_lang;
288
+ logger.error('\n' + 'invalid option found. ' + reason_text);
289
+ }
264
290
 
265
- // update the HTML element (content) by an error text
266
- if (ELEMENT_TYPE === 'TEXTAREA') {
267
- $(TARGET_ELEMENT).val(ERROR_TEXT + reason_text);
268
- } else if (ELEMENT_TYPE === 'P') {
269
- $(TARGET_ELEMENT).text(ERROR_TEXT + reason_text);
270
- }
271
- return false;
291
+ // update the HTML element (content) by an error text
292
+ if (ELEMENT_TYPE === 'TEXTAREA') {
293
+ $(TARGET_ELEMENT).val(ERROR_TEXT + reason_text);
294
+ } else if (ELEMENT_TYPE === 'P') {
295
+ $(TARGET_ELEMENT).text(ERROR_TEXT + reason_text);
272
296
  }
297
+ return false;
298
+ }
273
299
 
274
- if(!SOURCE_TEXT_FOUND) {
275
- reason_text = 'NO text found for translation';
276
- // update the HTML element (content) by an error text
277
- if (ELEMENT_TYPE === 'TEXTAREA') {
278
- $(TARGET_ELEMENT).val(WARNING_TEXT + reason_text);
279
- } else if (ELEMENT_TYPE === 'P') {
280
- $(TARGET_ELEMENT).text(WARNING_TEXT + reason_text);
281
- }
282
- logger.warn('\n' + 'no text found for translation');
283
- return false;
300
+ if(!SOURCE_TEXT_FOUND) {
301
+ reason_text = 'NO text found for translation';
302
+ // update the HTML element (content) by an error text
303
+ if (ELEMENT_TYPE === 'TEXTAREA') {
304
+ $(TARGET_ELEMENT).val(WARNING_TEXT + reason_text);
305
+ } else if (ELEMENT_TYPE === 'P') {
306
+ $(TARGET_ELEMENT).text(WARNING_TEXT + reason_text);
284
307
  }
308
+ logger.warn('\n' + 'no text found for translation');
309
+ return false;
310
+ }
285
311
 
286
- // Check for VALID language supported by option 'formality'
287
- if (this.settings.formality !== 'default' ) {
288
- if (!(ALLOWED_FORMALITY_LANG.indexOf(this.settings.target_lang) > -1)) {
289
- logger.warn('\n' + 'wrong language found for formality setting: ' + this.settings.target_lang);
290
- }
291
- this.settings.formality = (ALLOWED_FORMALITY_LANG.indexOf(this.settings.target_lang) > -1) ? this.settings.formality : 'default';
312
+ // Check for VALID language supported by option 'formality'
313
+ if (this.settings.formality !== 'default' ) {
314
+ if (!(ALLOWED_FORMALITY_LANG.indexOf(this.settings.target_lang) > -1)) {
315
+ logger.warn('\n' + 'wrong language found for formality setting: ' + this.settings.target_lang);
292
316
  }
317
+ this.settings.formality = (ALLOWED_FORMALITY_LANG.indexOf(this.settings.target_lang) > -1) ? this.settings.formality : 'default';
318
+ }
293
319
 
294
- // Check for VALID tag handling and options
320
+ // Check for VALID tag handling and options
321
+ if (this.settings.tag_handling) {
322
+ var tag_handling = this.settings.tag_handling;
323
+ // check for VALID method
324
+ this.settings.tag_handling = (ALLOWED_TAG_HANDLING.indexOf(this.settings.tag_handling) > -1) ? this.settings.tag_handling : false;
295
325
  if (this.settings.tag_handling) {
296
- var tag_handling = this.settings.tag_handling;
297
- // check for VALID method
298
- this.settings.tag_handling = (ALLOWED_TAG_HANDLING.indexOf(this.settings.tag_handling) > -1) ? this.settings.tag_handling : false;
299
- if (this.settings.tag_handling) {
300
- this.settings.non_splitting_tags = this.settings.non_splitting_tags ? encodeURIComponent(this.settings.non_splitting_tags) : false;
301
- this.settings.splitting_tags = this.settings.splitting_tags ? encodeURIComponent(this.settings.splitting_tags) : false;
302
- this.settings.ignore_tags = this.settings.ignore_tags ? encodeURIComponent(this.settings.ignore_tags) : false;
303
- } else {
304
- logger.error('\n' + 'invalid option found for tag handling : ' + tag_handling);
305
- logger.warn('\n' + 'disable option: ' + 'tag_handling');
306
- // reset to defaults if WRONG method detected
307
- if (this.settings.non_splitting_tags) {
308
- logger.warn('\n' + 'disable option: ' + this.settings.non_splitting_tags);
309
- this.settings.non_splitting_tags = false;
310
- }
311
- if (this.settings.splitting_tags) {
312
- logger.warn('\n' + 'disable option : ' + this.settings.splitting_tags);
313
- this.settings.splitting_tags = false;
314
- }
315
- if (this.settings.ignore_tags) {
316
- logger.warn('\n' + 'disable option: ' + this.settings.ignore_tags);
317
- this.settings.ignore_tags = false;
318
- }
319
- }
326
+ this.settings.non_splitting_tags = this.settings.non_splitting_tags ? encodeURIComponent(this.settings.non_splitting_tags) : false;
327
+ this.settings.splitting_tags = this.settings.splitting_tags ? encodeURIComponent(this.settings.splitting_tags) : false;
328
+ this.settings.ignore_tags = this.settings.ignore_tags ? encodeURIComponent(this.settings.ignore_tags) : false;
320
329
  } else {
321
- // reset tags to their defaults if NO tag_handling is enabled but settings passed
330
+ logger.error('\n' + 'invalid option found for tag handling : ' + tag_handling);
331
+ logger.warn('\n' + 'disable option: ' + 'tag_handling');
332
+ // reset to defaults if WRONG method detected
322
333
  if (this.settings.non_splitting_tags) {
323
- logger.warn('\n' + 'invalid option found: ' + this.settings.non_splitting_tags);
334
+ logger.warn('\n' + 'disable option: ' + this.settings.non_splitting_tags);
324
335
  this.settings.non_splitting_tags = false;
325
336
  }
326
337
  if (this.settings.splitting_tags) {
327
- logger.warn('\n' + 'invalid option found: ' + this.settings.splitting_tags);
338
+ logger.warn('\n' + 'disable option : ' + this.settings.splitting_tags);
328
339
  this.settings.splitting_tags = false;
329
340
  }
330
341
  if (this.settings.ignore_tags) {
331
- logger.warn('\n' + 'invalid option found: ' + this.settings.ignore_tags);
342
+ logger.warn('\n' + 'disable option: ' + this.settings.ignore_tags);
332
343
  this.settings.ignore_tags = false;
333
344
  }
334
345
  }
346
+ } else {
347
+ // reset tags to their defaults if NO tag_handling is enabled but settings passed
348
+ if (this.settings.non_splitting_tags) {
349
+ logger.warn('\n' + 'invalid option found: ' + this.settings.non_splitting_tags);
350
+ this.settings.non_splitting_tags = false;
351
+ }
352
+ if (this.settings.splitting_tags) {
353
+ logger.warn('\n' + 'invalid option found: ' + this.settings.splitting_tags);
354
+ this.settings.splitting_tags = false;
355
+ }
356
+ if (this.settings.ignore_tags) {
357
+ logger.warn('\n' + 'invalid option found: ' + this.settings.ignore_tags);
358
+ this.settings.ignore_tags = false;
359
+ }
360
+ }
335
361
 
336
- // ---------------------------------------------------------------------
337
- // construct the API request body
338
- // ---------------------------------------------------------------------
339
- request = "auth_key=" + this.settings.auth_key;
340
- request += (this.settings.source_lang !== 'auto' ) ? "&source_lang=" + this.settings.source_lang : '';
341
- request += "&target_lang=" + this.settings.target_lang;
342
- request += (this.settings.formality !== 'default' ) ? "&formality=" + this.settings.formality : '';
343
- request += (this.settings.split_sentences) ? "&split_sentences=" + this.settings.formality : '';
344
- request += (this.settings.tag_handling) ? "&tag_handling=" + this.settings.tag_handling : "&tag_handling=0";
345
- request += (this.settings.tag_handling && this.settings.non_splitting_tags) ? "&non_splitting_tags=" + this.settings.non_splitting_tags : '';
346
- request += (this.settings.tag_handling && this.settings.splitting_tags) ? "&splitting_tags=" + this.settings.splitting_tags : '';
347
- request += (this.settings.tag_handling && this.settings.ignore_tags) ? "&ignore_tags=" + this.settings.ignore_tags : '';
348
- request += source_text_request;
349
-
350
- // Send the constructed request to the API for translation
351
- this.xhr.send(request);
352
- } // END translate
353
-
354
- }); // END prototype
355
-
356
- // ---------------------------------------------------------------------------
357
- // plugin wrapper
358
- // A really lightweight plugin wrapper around the constructor,
359
- // wrapper around the constructor to prevent multiple instantiations
360
- // preventing against multiple instantiations and allowing any
361
- // public function (ie. a function whose name doesn't start
362
- // with an underscore) to be called via the jQuery plugin,
363
- // e.g. $(element).defaultPluginName('functionName', arg1, arg2)
364
- // ---------------------------------------------------------------------------
365
- $.fn[pluginName] = function ( options ) {
366
- var args = arguments;
367
-
368
- // Is the first parameter an object (options), or was omitted,
369
- // instantiate a new instance of the plugin.
370
- //
371
- if (options === undefined || typeof options === 'object') {
372
- return this.each(function () {
373
- // Only allow the plugin to be instantiated once,
374
- // so we check that the element has no plugin instantiation yet
375
- if (!$.data(this, 'plugin_' + pluginName)) {
376
- // if it has no instance, create a new one, pass options to the
377
- // plugin constructor, and store the plugin instance in the
378
- // elements jQuery data object.
379
- $.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
362
+ // ---------------------------------------------------------------------
363
+ // construct the API request body
364
+ // ---------------------------------------------------------------------
365
+ request = "auth_key=" + this.settings.auth_key;
366
+ request += (this.settings.source_lang !== 'auto' ) ? "&source_lang=" + this.settings.source_lang : '';
367
+ request += "&target_lang=" + this.settings.target_lang;
368
+ request += (this.settings.formality !== 'default' ) ? "&formality=" + this.settings.formality : '';
369
+ request += (this.settings.split_sentences) ? "&split_sentences=" + this.settings.formality : '';
370
+ request += (this.settings.tag_handling) ? "&tag_handling=" + this.settings.tag_handling : "&tag_handling=0";
371
+ request += (this.settings.tag_handling && this.settings.non_splitting_tags) ? "&non_splitting_tags=" + this.settings.non_splitting_tags : '';
372
+ request += (this.settings.tag_handling && this.settings.splitting_tags) ? "&splitting_tags=" + this.settings.splitting_tags : '';
373
+ request += (this.settings.tag_handling && this.settings.ignore_tags) ? "&ignore_tags=" + this.settings.ignore_tags : '';
374
+ request += source_text_request;
375
+
376
+ // Send the constructed request to the API for translation
377
+ this.xhr.send(request);
378
+ } // END translate
379
+
380
+ }); // END prototype
381
+
382
+ // ---------------------------------------------------------------------------
383
+ // plugin wrapper
384
+ // A really lightweight plugin wrapper around the constructor,
385
+ // wrapper around the constructor to prevent multiple instantiations
386
+ // preventing against multiple instantiations and allowing any
387
+ // public function (ie. a function whose name doesn't start
388
+ // with an underscore) to be called via the jQuery plugin,
389
+ // e.g. $(element).defaultPluginName('functionName', arg1, arg2)
390
+ // ---------------------------------------------------------------------------
391
+ $.fn[pluginName] = function ( options ) {
392
+ var args = arguments;
393
+
394
+ // Is the first parameter an object (options), or was omitted,
395
+ // instantiate a new instance of the plugin.
396
+ //
397
+ if (options === undefined || typeof options === 'object') {
398
+ return this.each(function () {
399
+ // Only allow the plugin to be instantiated once,
400
+ // so we check that the element has no plugin instantiation yet
401
+ if (!$.data(this, 'plugin_' + pluginName)) {
402
+ // if it has no instance, create a new one, pass options to the
403
+ // plugin constructor, and store the plugin instance in the
404
+ // elements jQuery data object.
405
+ $.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
406
+ }
407
+ });
408
+ } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
409
+ // If the first parameter is a string and it doesn't start
410
+ // with an underscore or "contains" the `init`-function,
411
+ // treat this as a call to a public method.
412
+
413
+ // Cache the method call to make it possible to return a value
414
+ var returns;
415
+
416
+ this.each(function () {
417
+ var instance = $.data(this, 'plugin_' + pluginName);
418
+
419
+ // Tests that there's already a plugin-instance
420
+ // and checks that the requested public method exists
421
+ if (instance instanceof Plugin && typeof instance[options] === 'function') {
422
+ // Call the method of our plugin instance,
423
+ // and pass it the supplied arguments.
424
+ returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
380
425
  }
381
- });
382
- } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
383
- // If the first parameter is a string and it doesn't start
384
- // with an underscore or "contains" the `init`-function,
385
- // treat this as a call to a public method.
386
-
387
- // Cache the method call to make it possible to return a value
388
- var returns;
389
-
390
- this.each(function () {
391
- var instance = $.data(this, 'plugin_' + pluginName);
392
-
393
- // Tests that there's already a plugin-instance
394
- // and checks that the requested public method exists
395
- if (instance instanceof Plugin && typeof instance[options] === 'function') {
396
- // Call the method of our plugin instance,
397
- // and pass it the supplied arguments.
398
- returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
399
- }
400
426
 
401
- // Allow instances to be destroyed via the 'destroy' method
402
- if (options === 'destroy') {
403
- $.data(this, 'plugin_' + pluginName, null);
404
- }
405
- });
427
+ // Allow instances to be destroyed via the 'destroy' method
428
+ if (options === 'destroy') {
429
+ $.data(this, 'plugin_' + pluginName, null);
430
+ }
431
+ });
406
432
 
407
- // If the earlier cached method gives a value back return the value,
408
- // otherwise return "this" to preserve chainability.
409
- return returns !== undefined ? returns : this;
410
- }
411
- }; // END plugin wrapper
433
+ // If the earlier cached method gives a value back return the value,
434
+ // otherwise return "this" to preserve chainability.
435
+ return returns !== undefined ? returns : this;
436
+ }
437
+ }; // END plugin wrapper
412
438
 
413
439
  })(jQuery, window, document);