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.lang.load(openerp.connection.user_context.lang.split('_')[0], 'en', function() {});
53
CKEDITOR.on('dialogDefinition', function(e)
55
_.each(e.data.definition.contents, function(element)
57
if(element.filebrowser!='uploadButton')
61
_.each(element.elements, function(element)
63
if(!element.onClick || element.type!='fileButton')
67
var onClick_org = element.onClick;
68
element.onClick = function(e1)
70
onClick_org.apply(this, arguments);
71
_.each(jQuery('#'+this.domId).closest('table')
72
.find('iframe').contents().find(':file')
76
var reader = new FileReader();
77
reader.onload = function(load_event)
79
CKEDITOR.tools.callFunction(
80
e.editor._.filebrowserFn,
81
load_event.target.result,
84
reader.readAsDataURL(file);
92
openerp.web.form.widgets.add('text_ckeditor4',
93
'openerp.web_ckeditor4.FieldCKEditor4');
94
openerp.web.page.readonly.add('text_ckeditor4',
95
'openerp.web_ckeditor4.FieldCKEditor4Readonly');
96
openerp.web.form.widgets.add('text_ckeditor4_raw',
97
'openerp.web_ckeditor4.FieldCKEditor4Raw');
98
openerp.web.page.readonly.add('text_ckeditor4_raw',
99
'openerp.web_ckeditor4.FieldCKEditor4ReadonlyRaw');
100
openerp.web.form.widgets.add('text_html',
101
'openerp.web_ckeditor4.FieldCKEditor4');
102
openerp.web.page.readonly.add('text_html',
103
'openerp.web_ckeditor4.FieldCKEditor4Readonly');
105
function filter_html(value, ckeditor_filter, ckeditor_writer)
107
var fragment = CKEDITOR.htmlParser.fragment.fromHtml(value);
108
ckeditor_filter.applyTo(fragment);
109
ckeditor_writer.reset();
110
fragment.writeHtml(ckeditor_writer);
111
return ckeditor_writer.getHtml();
114
default_ckeditor_filter = new CKEDITOR.filter(
118
attributes: 'href,src,style,alt,width,height',
122
'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': true,
124
default_ckeditor_writer = new CKEDITOR.htmlParser.basicWriter();
126
openerp.web_ckeditor4.FieldCKEditor4 = openerp.web.form.FieldText.extend({
128
removePlugins: 'iframe,flash,forms,smiley,pagebreak,stylescombo',
129
filebrowserImageUploadUrl: 'dummy',
130
extraPlugins: 'filebrowser',
132
ckeditor_filter: default_ckeditor_filter,
133
ckeditor_writer: default_ckeditor_writer,
137
this._super.apply(this, arguments);
139
if(this.modifiers.readonly)
144
self.editor = CKEDITOR.replace(this.$element.find('textarea').get(0),
147
language: openerp.connection.user_context.lang.split('_')[0],
150
'beforeUndoImage': function()
156
this.ckeditor_config));
158
get_value: function()
160
return this.editor ? openerp.web.parse_value(this.editor.getData(), this) : this.value;
162
filter_html: function(value)
164
return filter_html(value, this.ckeditor_filter, this.ckeditor_writer);
166
set_value: function(value)
168
if(this.modifiers.readonly)
170
this._super.apply(this, [value]);
172
this.$element.html(this.filter_html(value));
180
if(this.editor.status != 'ready')
182
this.editor.on('instanceReady',
185
self.editor.setData(value || '');
190
self.editor.setData(value || '');
193
this._super.apply(this, arguments);
201
CKEDITOR.remove(this.editor);
203
return this._super.apply(this, arguments);
206
openerp.web_ckeditor4.FieldCKEditor4Raw = openerp.web_ckeditor4.FieldCKEditor4.extend({
207
filter_html: function(value)
214
openerp.web_ckeditor4.FieldCKEditor4ReadonlyRaw = openerp.web.page.FieldCharReadonly.extend({
215
set_value: function (value)
217
this._super.apply(this, arguments);
218
this.$element.find('div').html(value);
222
openerp.web_ckeditor4.FieldCKEditor4Readonly = openerp.web.page.FieldCharReadonly.extend({
223
ckeditor_filter: default_ckeditor_filter,
224
ckeditor_writer: default_ckeditor_writer,
225
set_value: function (value)
227
this._super.apply(this, arguments);
228
this.$element.find('div').html(filter_html(value, this.ckeditor_filter, this.ckeditor_writer));