forked from PowerKiKi/qTranslate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtranslate_javascript.php
404 lines (361 loc) · 12.6 KB
/
qtranslate_javascript.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php // encoding: utf-8
/* Copyright 2008 Qian Qin (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// qTranslate Javascript functions
function qtrans_initJS() {
global $q_config;
$q_config['js']['qtrans_xsplit'] = "
String.prototype.xsplit = function(_regEx){
// Most browsers can do this properly, so let them — they'll do it faster
if ('a~b'.split(/(~)/).length === 3) { return this.split(_regEx); }
if (!_regEx.global)
{ _regEx = new RegExp(_regEx.source, 'g' + (_regEx.ignoreCase ? 'i' : '')); }
// IE (and any other browser that can't capture the delimiter)
// will, unfortunately, have to be slowed down
var start = 0, arr=[];
var result;
while((result = _regEx.exec(this)) != null){
arr.push(this.slice(start, result.index));
if(result.length > 1) arr.push(result[1]);
start = _regEx.lastIndex;
}
if(start < this.length) arr.push(this.slice(start));
if(start == this.length) arr.push(''); //delim at the end
return arr;
};
";
$q_config['js']['qtrans_is_array'] = "
qtrans_isArray = function(obj) {
if (obj.constructor.toString().indexOf('Array') == -1)
return false;
else
return true;
}
";
$q_config['js']['qtrans_split'] = "
qtrans_split = function(text) {
var split_regex = /(<!--.*?-->)/gi;
var lang_begin_regex = /<!--:([a-z]{2})-->/gi;
var lang_end_regex = /<!--:-->/gi;
var morenextpage_regex = /(<!--more-->|<!--nextpage-->)+$/gi;
var matches = null;
var result = new Object;
var matched = false;
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_split'].= "
result['".$language."'] = '';
";
$q_config['js']['qtrans_split'].= "
var blocks = text.xsplit(split_regex);
if(qtrans_isArray(blocks)) {
for (var i = 0;i<blocks.length;i++) {
if((matches = lang_begin_regex.exec(blocks[i])) != null) {
matched = matches[1];
} else if(lang_end_regex.test(blocks[i])) {
matched = false;
} else {
if(matched) {
result[matched] += blocks[i];
} else {
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_split'].= "
result['".$language."'] += blocks[i];
";
$q_config['js']['qtrans_split'].= "
}
}
}
}
for (var i = 0;i<result.length;i++) {
result[i] = result[i].replace(morenextpage_regex,'');
}
return result;
}
";
$q_config['js']['qtrans_use'] = "
qtrans_use = function(lang, text) {
var result = qtrans_split(text);
return result[lang];
}
";
$q_config['js']['qtrans_integrate'] = "
qtrans_integrate = function(lang, lang_text, text) {
var texts = qtrans_split(text);
var moreregex = /<!--more-->/i;
var text = '';
var max = 0;
var morenextpage_regex = /(<!--more-->|<!--nextpage-->)+$/gi;
texts[lang] = lang_text;
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_integrate'].= "
texts['".$language."'] = texts['".$language."'].split(moreregex);
if(!qtrans_isArray(texts['".$language."'])) {
texts['".$language."'] = [texts['".$language."']];
}
if(max < texts['".$language."'].length) max = texts['".$language."'].length;
";
$q_config['js']['qtrans_integrate'].= "
for(var i=0; i<max; i++) {
if(i >= 1) {
text += '<!--more-->';
}
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_integrate'].= "
if(texts['".$language."'][i] && texts['".$language."'][i]!=''){
text += '<!--:".$language."-->';
text += texts['".$language."'][i];
text += '<!--:-->';
}
";
$q_config['js']['qtrans_integrate'].= "
}
text = text.replace(morenextpage_regex,'');
return text;
}
";
$q_config['js']['qtrans_save'] = "
qtrans_save = function(text) {
var ta = document.getElementById('content');
ta.value = qtrans_integrate(qtrans_get_active_language(),text,ta.value);
return ta.value;
}
";
$q_config['js']['qtrans_integrate_category'] = "
qtrans_integrate_category = function() {
var t = document.getElementById('cat_name');
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_integrate_category'].= "
if(document.getElementById('qtrans_category_".$language."').value!='')
t.value = qtrans_integrate('".$language."',document.getElementById('qtrans_category_".$language."').value,t.value);
";
$q_config['js']['qtrans_integrate_category'].= "
}
";
$q_config['js']['qtrans_integrate_tag'] = "
qtrans_integrate_tag = function() {
var t = document.getElementById('name');
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_integrate_tag'].= "
if(document.getElementById('qtrans_tag_".$language."').value!='')
t.value = qtrans_integrate('".$language."',document.getElementById('qtrans_tag_".$language."').value,t.value);
";
$q_config['js']['qtrans_integrate_tag'].= "
}
";
$q_config['js']['qtrans_integrate_link_category'] = "
qtrans_integrate_link_category = function() {
var t = document.getElementById('name');
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_integrate_link_category'].= "
if(document.getElementById('qtrans_link_category_".$language."').value!='')
t.value = qtrans_integrate('".$language."',document.getElementById('qtrans_link_category_".$language."').value,t.value);
";
$q_config['js']['qtrans_integrate_link_category'].= "
}
";
$q_config['js']['qtrans_integrate_title'] = "
qtrans_integrate_title = function() {
var t = document.getElementById('title');
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_integrate_title'].= "
t.value = qtrans_integrate('".$language."',document.getElementById('qtrans_title_".$language."').value,t.value);
";
$q_config['js']['qtrans_integrate_title'].= "
}
";
$q_config['js']['qtrans_assign'] = "
qtrans_assign = function(id, text) {
var inst = tinyMCE.get(id);
var ta = document.getElementById(id);
if(inst && ! inst.isHidden()) {
text = switchEditors.wpautop(text);
inst.execCommand('mceSetContent', null, text);
} else {
ta.value = text;
}
}
";
$q_config['js']['qtrans_tinyMCEOverload'] = "
tinyMCE.get2 = tinyMCE.get;
tinyMCE.get = function(id) {
if(id=='content'&&this.get2('qtrans_textarea_'+id)!=undefined)
return this.get2('qtrans_textarea_'+id);
return this.get2(id);
}
";
$q_config['js']['qtrans_wpActiveEditorOverload'] = "
jQuery('.wp-editor-wrap').unbind('mousedown');
jQuery('.wp-editor-wrap').mousedown(function(e){
wpActiveEditor = 'qtrans_textarea_'+this.id.slice(3, -5);
});
";
$q_config['js']['qtrans_wpOnload'] = "
if(typeof(wpOnload)!='undefined') wpOnload2 = wpOnload;
wpOnload = function() {
for(var i in tinyMCEPreInit.qtInit) {
var tmp = tinyMCEPreInit.qtInit[i];
tmp.id = 'qtrans_textarea_'+tmp.id;
try { quicktags( tmp ); } catch(e){}
jQuery('#ed_toolbar').hide();
}
// remove hook so tinymce doesn't load for content
var hook = tinyMCEPreInit.mceInit['content']
hook.elements='hook-to-nothing';
delete tinyMCEPreInit.mceInit['content'];
tinyMCEPreInit.mceInit['qtrans_textarea_content'] = hook;
if(typeof(wpOnload2)=='function') wpOnload2();
}
jQuery(document).ready(function() {
qtrans_editorInit();
});
";
$q_config['js']['qtrans_editorInit'] = "
qtrans_editorInit = function() {
qtrans_editorInit1();
qtrans_editorInit2();
jQuery('#qtrans_imsg').hide();
qtrans_editorInit3();
var h = getUserSetting( 'ed_size' );
if(h<300) h = 300;
jQuery('#content').hide();
if ( getUserSetting( 'editor' ) == 'html' ) {
if ( h )
jQuery('#qtrans_textarea_content').css('height', h - 20 + 'px');
jQuery('#qtrans_textarea_content').show();
} else {
// Activate TinyMCE if it's the user's default editor
jQuery('#qtrans_textarea_content').show();
// correct p for tinymce
jQuery('#qtrans_textarea_content').val(switchEditors.wpautop(jQuery('#qtrans_textarea_content').val()))
// let wp3.5 autohook take care of init
qtrans_hook_on_tinyMCE('qtrans_textarea_content');
}
}
";
$q_config['js']['qtrans_hook_on_tinyMCE'] = "
qtrans_hook_on_tinyMCE = function(id) {
tinyMCEPreInit.mceInit[id].setup = function(ed) {
ed.onSaveContent.add(function(ed, o) {
if (!ed.isHidden()) {
qtrans_save(switchEditors.pre_wpautop(o.content));
}
});
};
ed = new tinymce.Editor(id, tinyMCEPreInit.mceInit[id]);
ed.render();
}
";
$q_config['js']['qtrans_get_active_language'] = "
qtrans_get_active_language = function() {
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_get_active_language'].= "
if(document.getElementById('qtrans_select_".$language."').className=='wp-switch-editor switch-tmce switch-html')
return '".$language."';
";
$q_config['js']['qtrans_get_active_language'].= "
}
";
$q_config['js']['qtrans_switch_postbox'] = "
function qtrans_switch_postbox(parent, target, lang, focus) {
if(typeof(focus)==='undefined') focus = true;
";
foreach($q_config['enabled_languages'] as $language)
$q_config['js']['qtrans_switch_postbox'].= "
jQuery('#'+target).val(qtrans_integrate('".$language."', jQuery('#qtrans_textarea_'+target+'_'+'".$language."').val(), jQuery('#'+target).val()));
jQuery('#'+parent+' .qtranslate_lang_div').removeClass('switch-html');
jQuery('#'+parent+' .qtranslate_lang_div').removeClass('switch-tmce');
if(lang!=false) jQuery('#qtrans_textarea_'+target+'_'+'".$language."').hide();
";
$q_config['js']['qtrans_switch_postbox'].= "
if(lang!=false) {
jQuery('#qtrans_switcher_'+parent+'_'+lang).addClass('switch-tmce');
jQuery('#qtrans_switcher_'+parent+'_'+lang).addClass('switch-html');
jQuery('#qtrans_textarea_'+target+'_'+lang).show();
if(focus)
jQuery('#qtrans_textarea_'+target+'_'+lang).focus();
}
}
";
$q_config['js']['qtrans_switch'] = "
switchEditors.go = function(id, lang) {
id = id || 'content';
lang = lang || 'toggle';
if ( 'toggle' == lang ) {
if ( ed && !ed.isHidden() )
lang = 'html';
else
lang = 'tmce';
} else if( 'tinymce' == lang )
lang = 'tmce';
var inst = tinyMCE.get('qtrans_textarea_' + id);
var vta = document.getElementById('qtrans_textarea_' + id);
var ta = document.getElementById(id);
var dom = tinymce.DOM;
var wrap_id = 'wp-'+id+'-wrap';
// update merged content
if(inst && ! inst.isHidden()) {
tinyMCE.triggerSave();
} else {
qtrans_save(vta.value);
}
// check if language is already active
if(lang!='tmce' && lang!='html' && document.getElementById('qtrans_select_'+lang).className=='wp-switch-editor switch-tmce switch-html') {
return;
}
if(lang!='tmce' && lang!='html') {
document.getElementById('qtrans_select_'+qtrans_get_active_language()).className='wp-switch-editor';
document.getElementById('qtrans_select_'+lang).className='wp-switch-editor switch-tmce switch-html';
}
if(lang=='html') {
if ( inst && inst.isHidden() )
return false;
if ( inst ) {
vta.style.height = inst.getContentAreaContainer().offsetHeight + 20 + 'px';
inst.hide();
}
dom.removeClass(wrap_id, 'tmce-active');
dom.addClass(wrap_id, 'html-active');
setUserSetting( 'editor', 'html' );
} else if(lang=='tmce') {
if(inst && ! inst.isHidden())
return false;
if ( typeof(QTags) != 'undefined' )
QTags.closeAllTags(id);
if ( tinyMCEPreInit.mceInit['qtrans_textarea_'+id] && tinyMCEPreInit.mceInit['qtrans_textarea_'+id].wpautop )
vta.value = this.wpautop(qtrans_use(qtrans_get_active_language(),ta.value));
if (inst) {
inst.show();
} else {
qtrans_hook_on_tinyMCE('qtrans_textarea_'+id);
}
dom.removeClass(wrap_id, 'html-active');
dom.addClass(wrap_id, 'tmce-active');
setUserSetting('editor', 'tinymce');
} else {
// switch content
qtrans_assign('qtrans_textarea_'+id,qtrans_use(lang,ta.value));
}
}
";
}
?>