wpml-tm-editor-job-wysiwyg-field-view.js
4.72 KB
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
/*global jQuery, tinymce, tinyMCE, _, quicktags, tmEditor, WpmlTmEditorModel*/
var WPML_TM = WPML_TM || {};
(function () {
"use strict";
WPML_TM.editorWysiwygFieldView = WPML_TM.editorJobFieldView.extend({
getTemplate: function () {
return 'templates/translation-editor/wysiwyg.html';
},
getOriginal: function () {
return this.$el.find('textarea.original_value').text();
},
getTranslation: function () {
var self = this;
var editor = tinymce.get(self.field.field_type);
if (editor && editor instanceof tinyMCE.Editor && !editor.isHidden()) {
return editor.getContent();
} else {
return self.getTextAreaElement().val();
}
},
setTranslation: function (value) {
var self = this;
var editor = tinymce.get(self.field.field_type);
var shouldToggle = editor && editor instanceof tinyMCE.Editor && !editor.isHidden();
if (shouldToggle) {
tinymce.execCommand('mceToggleEditor', false, self.field.field_type);
}
self.getTextAreaElement().val(value);
if (shouldToggle) {
tinymce.execCommand('mceToggleEditor', false, self.field.field_type);
}
this.updateUI();
},
setup: function () {
var self = this;
self.replacePlaceHolder('original');
self.replacePlaceHolder('translated');
self.$el.find('textarea.original_value').prop('readonly', true);
self.getTextAreaElement().on('input', _.bind(self.updateUI, self));
self.getTextAreaElement().on('input', function () {
tmEditor.model.trigger('translationUpdated', true);
});
if (!WpmlTmEditorModel.show_media_button) {
self.$el.find('.wp-media-buttons').hide();
}
_.delay(_.bind(self.waitForEditorAndThenInstallHooks, self, self.translationCompleteCheckbox.is( ':checked' )), 1000);
_.delay(_.bind(self.setInputStatus, self, 1000 ) );
},
getTextAreaElement: function () {
return this.$el.find('textarea#' + this.field.field_type);
},
waitForEditorAndThenInstallHooks: function (checked) {
var self = this;
var editor = tinymce.get(self.field.field_type);
if (editor && editor instanceof tinyMCE.Editor) {
editor.on('nodechange keyup', function (e) {
var lazyOnChange = _.debounce(_.bind(self.updateUI, self), 1000);
lazyOnChange(editor);
editor.save();
});
editor.on('change', function (e) {
tmEditor.model.trigger('translationUpdated', true);
});
_.delay(function () {
self.$el.find('.mce_editor_origin .mce-toolbar-grp').height(self.$el.find('.mce_editor .mce-toolbar-grp').height());
}, 1000);
self.setRtlAttributes(editor);
self.setOriginalBackgroundGray( editor );
if( checked ) {
self.translationCompleteCheckbox.prop('checked', true);
self.translationCompleteCheckbox.prop('disabled', false);
self.translationCompleteCheckbox.trigger('change');
}
} else {
_.delay(_.bind(self.waitForEditorAndThenInstallHooks, self), 1000);
}
},
replacePlaceHolder: function (type) {
var self = this;
var $placeholder = self.$el.find('#' + type + '_' + self.field.field_type + '_placeholder');
jQuery('#' + self.field.field_type + '_' + type + '_editor').detach().insertAfter($placeholder);
$placeholder.remove();
},
setRtlAttributes: function (editor) {
var self = this, dir, body, html;
dir = WpmlTmEditorModel.rtl_translation ? 'rtl' : 'ltr';
html = jQuery(editor.iframeElement).contents().find('html');
html.attr('dir', dir);
body = html.find('body');
if (body.length) {
body.attr('dir', dir);
}
self.getTextAreaElement().attr('dir', dir);
dir = WpmlTmEditorModel.rtl_original ? 'rtl' : 'ltr';
html = self.$el.find('.mce_editor_origin').find('iframe').contents().find('html');
html.attr('dir', dir);
body = html.find('body');
if (body.length) {
body.attr('dir', dir);
}
self.$el.find('textarea.original_value').attr('dir', dir);
},
setOriginalBackgroundGray: function ( editor ) {
var self = this,
html = self.$el.find( '.mce_editor_origin' ).find( 'iframe' ).contents().find( 'html' ),
body = html.find('body'),
sizer = self.$el.find( '.mce_editor_origin' ).find( '.mce-statusbar' ).find( '.mce-flow-layout' );
body.css( 'background-color', '#eee' );
sizer.css( 'background-color', '#eee' );
},
setTranslatedColor: function ( css ) {
var input = this.$el.find( '.translated_value' ),
html = input.find( 'iframe' ).contents().find( 'html' ),
body = html.find( 'body' ),
sizer = input.find( '.mce-statusbar' ).find( '.mce-flow-layout' ),
textArea = input.find( '.translated_value' );
body.css( 'background-color', css.background );
sizer.css( 'background-color', css.background );
textArea.css( 'background-color', css.background );
}
});
}());