~webaddons-core-editors/web-addons/7.0

« back to all changes in this revision

Viewing changes to web_ckeditor4/static/src/js/web_ckeditor4.js

  • Committer: Sylvain LE GAL
  • Date: 2014-02-21 12:48:30 UTC
  • mfrom: (16 web-addons)
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: sylvain.legal@grap.coop-20140221124830-q5oj8kxqf9ak4of2
[MRG]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    This module copyright (C) 2013 Therp BV (<http://therp.nl>)
 
6
#    All Rights Reserved
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
#
 
21
############################################################################*/
 
22
 
 
23
openerp.web_ckeditor4 = function(openerp)
 
24
{
 
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)
 
31
    {
 
32
        if(scope && scope._ && scope._.attrChanges && scope._.detach)
 
33
        {
 
34
            var scope_reference = scope;
 
35
            return ckeditor_addFunction_org(function()
 
36
                    {
 
37
                        var self = this,
 
38
                            self_arguments=arguments;
 
39
                        setTimeout(function()
 
40
                        {
 
41
                            if(CKEDITOR.instances[self.editor.name])
 
42
                            {
 
43
                                fn.apply(self, self_arguments);
 
44
                            }
 
45
                        }, 0);
 
46
                    }, scope);
 
47
        }
 
48
        return ckeditor_addFunction_org(fn, scope);
 
49
    };
 
50
 
 
51
    CKEDITOR.on('dialogDefinition', function(e)
 
52
        {
 
53
            _.each(e.data.definition.contents, function(element)
 
54
            {
 
55
                if(!element || element.filebrowser!='uploadButton')
 
56
                {
 
57
                    return
 
58
                }
 
59
                _.each(element.elements, function(element)
 
60
                {
 
61
                    if(!element.onClick || element.type!='fileButton')
 
62
                    {
 
63
                        return
 
64
                    }
 
65
                    var onClick_org = element.onClick;
 
66
                    element.onClick = function(e1)
 
67
                    {
 
68
                        onClick_org.apply(this, arguments);
 
69
                        _.each(jQuery('#'+this.domId).closest('table')
 
70
                            .find('iframe').contents().find(':file')
 
71
                            .get(0).files,
 
72
                            function(file)
 
73
                            {
 
74
                                var reader = new FileReader();
 
75
                                reader.onload = function(load_event)
 
76
                                {
 
77
                                    CKEDITOR.tools.callFunction(
 
78
                                        e.editor._.filebrowserFn,
 
79
                                        load_event.target.result,
 
80
                                        '');
 
81
                                }
 
82
                                reader.readAsDataURL(file);
 
83
                            });
 
84
                        return false;
 
85
                    }
 
86
                });
 
87
            });
 
88
        });
 
89
 
 
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');
 
98
 
 
99
    function filter_html(value, ckeditor_filter, ckeditor_writer)
 
100
    {
 
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();
 
106
    };
 
107
 
 
108
    default_ckeditor_filter = new CKEDITOR.filter(
 
109
            {
 
110
                '*':
 
111
                {
 
112
                    attributes: 'href,src,style,alt,width,height,dir',
 
113
                    styles: '*',
 
114
                    classes: '*',
 
115
                },
 
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,
 
117
            });
 
118
    default_ckeditor_writer = new CKEDITOR.htmlParser.basicWriter();
 
119
 
 
120
    openerp.web_ckeditor4.FieldCKEditor4 = openerp.web.form.FieldText.extend({
 
121
        ckeditor_config: {
 
122
            removePlugins: 'iframe,flash,forms,smiley,pagebreak,stylescombo',
 
123
            filebrowserImageUploadUrl: 'dummy',
 
124
            extraPlugins: 'filebrowser',
 
125
        },
 
126
        ckeditor_filter: default_ckeditor_filter,
 
127
        ckeditor_writer: default_ckeditor_writer,
 
128
        start: function()
 
129
        {
 
130
            this._super.apply(this, arguments);
 
131
    
 
132
            CKEDITOR.lang.load(openerp.session.user_context.lang.split('_')[0], 'en', function() {});
 
133
        },
 
134
        initialize_content: function()
 
135
        {
 
136
            var self = this;
 
137
            this._super.apply(this, arguments);
 
138
            if(!this.$textarea)
 
139
            {
 
140
                return;
 
141
            }
 
142
            this.editor = CKEDITOR.replace(this.$textarea.get(0),
 
143
                _.extend(
 
144
                    {
 
145
                        language: openerp.session.user_context.lang.split('_')[0],
 
146
                        on:
 
147
                        {
 
148
                            'change': function()
 
149
                            {
 
150
                                self.store_dom_value();
 
151
                            },
 
152
                        },
 
153
                    }, 
 
154
                    this.ckeditor_config));
 
155
        },
 
156
        store_dom_value: function()
 
157
        {
 
158
            this.internal_set_value(this.editor ? this.editor.getData() : openerp.web.parse_value(this.get('value'), this));
 
159
        },
 
160
        filter_html: function(value)
 
161
        {
 
162
            return filter_html(value, this.ckeditor_filter, this.ckeditor_writer);
 
163
        },
 
164
        render_value: function()
 
165
        {
 
166
            if(this.get("effective_readonly"))
 
167
            {
 
168
                this.$el.html(this.filter_html(this.get('value')));
 
169
            }
 
170
            else
 
171
            {
 
172
                if(this.editor)
 
173
                {
 
174
                    var self = this;
 
175
                    if(this.editor.status != 'ready')
 
176
                    {
 
177
                        var instanceReady = function()
 
178
                        {
 
179
                            self.editor.setData(self.get('value') || '');
 
180
                            self.editor.removeListener('instanceReady', instanceReady);
 
181
                        };
 
182
                        this.editor.on('instanceReady', instanceReady);
 
183
                    }
 
184
                    else
 
185
                    {
 
186
                        self.editor.setData(self.get('value') || '');
 
187
                    }
 
188
                }
 
189
            }
 
190
        },
 
191
        undelegateEvents: function()
 
192
        {
 
193
            this._cleanup_editor();
 
194
            return this._super.apply(this, arguments);
 
195
        },
 
196
        _cleanup_editor: function()
 
197
        {
 
198
            if(this.editor)
 
199
            {
 
200
                CKEDITOR.remove(this.editor);
 
201
                this.editor.removeAllListeners();
 
202
                this.editor = null;
 
203
            }
 
204
        },
 
205
        destroy_content: function()
 
206
        {
 
207
            this._cleanup_editor();
 
208
        }
 
209
    });
 
210
    openerp.web_ckeditor4.FieldCKEditor4Raw = openerp.web_ckeditor4.FieldCKEditor4.extend({
 
211
        filter_html: function(value)
 
212
        {
 
213
            return value;
 
214
        }
 
215
    });
 
216
}
 
217