1
/* -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# This module copyright (C) 2013 Therp BV (<http://therp.nl>)
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Affero General Public License as
10
# published by the Free Software Foundation, either version 3 of the
11
# License, or (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU Affero General Public License for more details.
18
# You should have received a copy of the GNU Affero General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
############################################################################*/
23
openerp.web_ckeditor4 = function(openerp)
25
var ckeditor_addFunction_org = CKEDITOR.tools.addFunction;
26
//this is a quite complicated way to kind of monkey patch the private
27
//method onDomReady of ckeditor's plugin wysiwigarea, which causes problems
28
//when the editor is about to be destroyed but because of OpenERP's
29
//architecture updated one last time with its current value
30
CKEDITOR.tools.addFunction = function(fn, scope)
32
if(scope && scope._ && scope._.attrChanges && scope._.detach)
34
var scope_reference = scope;
35
return ckeditor_addFunction_org(function()
38
self_arguments=arguments;
41
if(CKEDITOR.instances[self.editor.name])
43
fn.apply(self, self_arguments);
48
return ckeditor_addFunction_org(fn, scope);
51
CKEDITOR.on('dialogDefinition', function(e)
53
_.each(e.data.definition.contents, function(element)
55
if(!element || element.filebrowser!='uploadButton')
59
_.each(element.elements, function(element)
61
if(!element.onClick || element.type!='fileButton')
65
var onClick_org = element.onClick;
66
element.onClick = function(e1)
68
onClick_org.apply(this, arguments);
69
_.each(jQuery('#'+this.domId).closest('table')
70
.find('iframe').contents().find(':file')
74
var reader = new FileReader();
75
reader.onload = function(load_event)
77
CKEDITOR.tools.callFunction(
78
e.editor._.filebrowserFn,
79
load_event.target.result,
82
reader.readAsDataURL(file);
90
openerp.web.form.widgets.add('text_ckeditor4',
91
'openerp.web_ckeditor4.FieldCKEditor4');
92
openerp.web.form.widgets.add('text_ckeditor4_raw',
93
'openerp.web_ckeditor4.FieldCKEditor4Raw');
94
openerp.web.form.widgets.add('text_html',
95
'openerp.web_ckeditor4.FieldCKEditor4');
96
openerp.web.form.widgets.add('html',
97
'openerp.web_ckeditor4.FieldCKEditor4');
99
function filter_html(value, ckeditor_filter, ckeditor_writer)
101
var fragment = CKEDITOR.htmlParser.fragment.fromHtml(value);
102
ckeditor_filter.applyTo(fragment);
103
ckeditor_writer.reset();
104
fragment.writeHtml(ckeditor_writer);
105
return ckeditor_writer.getHtml();
108
default_ckeditor_filter = new CKEDITOR.filter(
112
attributes: 'href,src,style,alt,width,height,dir',
116
'html head title meta style body p div span a h1 h2 h3 h4 h5 img br hr table tr th td ul ol li dd dt strong pre b i': true,
118
default_ckeditor_writer = new CKEDITOR.htmlParser.basicWriter();
120
openerp.web_ckeditor4.FieldCKEditor4 = openerp.web.form.FieldText.extend({
122
removePlugins: 'iframe,flash,forms,smiley,pagebreak,stylescombo',
123
filebrowserImageUploadUrl: 'dummy',
124
extraPlugins: 'filebrowser',
126
ckeditor_filter: default_ckeditor_filter,
127
ckeditor_writer: default_ckeditor_writer,
130
this._super.apply(this, arguments);
132
CKEDITOR.lang.load(openerp.session.user_context.lang.split('_')[0], 'en', function() {});
134
initialize_content: function()
137
this._super.apply(this, arguments);
142
this.editor = CKEDITOR.replace(this.$textarea.get(0),
145
language: openerp.session.user_context.lang.split('_')[0],
150
self.store_dom_value();
154
this.ckeditor_config));
156
store_dom_value: function()
158
this.internal_set_value(this.editor ? this.editor.getData() : openerp.web.parse_value(this.get('value'), this));
160
filter_html: function(value)
162
return filter_html(value, this.ckeditor_filter, this.ckeditor_writer);
164
render_value: function()
166
if(this.get("effective_readonly"))
168
this.$el.html(this.filter_html(this.get('value')));
175
if(this.editor.status != 'ready')
177
var instanceReady = function()
179
self.editor.setData(self.get('value') || '');
180
self.editor.removeListener('instanceReady', instanceReady);
182
this.editor.on('instanceReady', instanceReady);
186
self.editor.setData(self.get('value') || '');
191
undelegateEvents: function()
193
this._cleanup_editor();
194
return this._super.apply(this, arguments);
196
_cleanup_editor: function()
200
CKEDITOR.remove(this.editor);
201
this.editor.removeAllListeners();
205
destroy_content: function()
207
this._cleanup_editor();
210
openerp.web_ckeditor4.FieldCKEditor4Raw = openerp.web_ckeditor4.FieldCKEditor4.extend({
211
filter_html: function(value)