~lepistone/web-addons/6.1-export-view-float-field-pep8

« back to all changes in this revision

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

  • Committer: Stefan Rijnhart
  • Author(s): hbrunn at therp
  • Date: 2013-05-29 22:55:12 UTC
  • mfrom: (11.1.10 web-ckeditor4)
  • Revision ID: stefan@therp.nl-20130529225512-jwz5q1jej6lozmcn
[ADD] web_ckeditor

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.lang.load(openerp.connection.user_context.lang.split('_')[0], 'en', function() {});
 
52
 
 
53
    CKEDITOR.on('dialogDefinition', function(e)
 
54
        {
 
55
            _.each(e.data.definition.contents, function(element)
 
56
            {
 
57
                if(element.filebrowser!='uploadButton')
 
58
                {
 
59
                    return
 
60
                }
 
61
                _.each(element.elements, function(element)
 
62
                {
 
63
                    if(!element.onClick || element.type!='fileButton')
 
64
                    {
 
65
                        return
 
66
                    }
 
67
                    var onClick_org = element.onClick;
 
68
                    element.onClick = function(e1)
 
69
                    {
 
70
                        onClick_org.apply(this, arguments);
 
71
                        _.each(jQuery('#'+this.domId).closest('table')
 
72
                            .find('iframe').contents().find(':file')
 
73
                            .get(0).files,
 
74
                            function(file)
 
75
                            {
 
76
                                var reader = new FileReader();
 
77
                                reader.onload = function(load_event)
 
78
                                {
 
79
                                    CKEDITOR.tools.callFunction(
 
80
                                        e.editor._.filebrowserFn,
 
81
                                        load_event.target.result,
 
82
                                        '');
 
83
                                }
 
84
                                reader.readAsDataURL(file);
 
85
                            });
 
86
                        return false;
 
87
                    }
 
88
                });
 
89
            });
 
90
        });
 
91
 
 
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');
 
104
 
 
105
    function filter_html(value, ckeditor_filter, ckeditor_writer)
 
106
    {
 
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();
 
112
    };
 
113
 
 
114
    default_ckeditor_filter = new CKEDITOR.filter(
 
115
            {
 
116
                '*':
 
117
                {
 
118
                    attributes: 'href,src,style,alt,width,height',
 
119
                    styles: '*',
 
120
                    classes: '*',
 
121
                },
 
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,
 
123
            });
 
124
    default_ckeditor_writer = new CKEDITOR.htmlParser.basicWriter();
 
125
 
 
126
    openerp.web_ckeditor4.FieldCKEditor4 = openerp.web.form.FieldText.extend({
 
127
        ckeditor_config: {
 
128
            removePlugins: 'iframe,flash,forms,smiley,pagebreak,stylescombo',
 
129
            filebrowserImageUploadUrl: 'dummy',
 
130
            extraPlugins: 'filebrowser',
 
131
        },
 
132
        ckeditor_filter: default_ckeditor_filter,
 
133
        ckeditor_writer: default_ckeditor_writer,
 
134
        start: function()
 
135
        {
 
136
            var self = this;
 
137
            this._super.apply(this, arguments);
 
138
 
 
139
            if(this.modifiers.readonly)
 
140
            {
 
141
                return;
 
142
            }
 
143
 
 
144
            self.editor = CKEDITOR.replace(this.$element.find('textarea').get(0),
 
145
                _.extend(
 
146
                    {
 
147
                        language: openerp.connection.user_context.lang.split('_')[0],
 
148
                        on:
 
149
                        {
 
150
                            'beforeUndoImage': function()
 
151
                            {
 
152
                                self.on_ui_change();
 
153
                            },
 
154
                        },
 
155
                    }, 
 
156
                    this.ckeditor_config));
 
157
        },
 
158
        get_value: function()
 
159
        {
 
160
            return this.editor ? openerp.web.parse_value(this.editor.getData(), this) : this.value;
 
161
        },
 
162
        filter_html: function(value)
 
163
        {
 
164
            return filter_html(value, this.ckeditor_filter, this.ckeditor_writer);
 
165
        },
 
166
        set_value: function(value)
 
167
        {
 
168
            if(this.modifiers.readonly)
 
169
            {
 
170
                this._super.apply(this, [value]);
 
171
 
 
172
                this.$element.html(this.filter_html(value));
 
173
                return value;
 
174
            }
 
175
            else
 
176
            {
 
177
                if(this.editor)
 
178
                {
 
179
                    var self = this;
 
180
                    if(this.editor.status != 'ready')
 
181
                    {
 
182
                        this.editor.on('instanceReady',
 
183
                            function()
 
184
                            {
 
185
                                self.editor.setData(value || '');
 
186
                            });
 
187
                    }
 
188
                    else
 
189
                    {
 
190
                        self.editor.setData(value || '');
 
191
                    }
 
192
                }
 
193
                this._super.apply(this, arguments);
 
194
            }
 
195
        },
 
196
 
 
197
        stop: function()
 
198
        {
 
199
            if(this.editor)
 
200
            {
 
201
                CKEDITOR.remove(this.editor);
 
202
            }
 
203
            return this._super.apply(this, arguments);
 
204
        }
 
205
    });
 
206
    openerp.web_ckeditor4.FieldCKEditor4Raw = openerp.web_ckeditor4.FieldCKEditor4.extend({
 
207
        filter_html: function(value)
 
208
        {
 
209
            return value;
 
210
        }
 
211
    });
 
212
 
 
213
 
 
214
    openerp.web_ckeditor4.FieldCKEditor4ReadonlyRaw = openerp.web.page.FieldCharReadonly.extend({
 
215
        set_value: function (value)
 
216
        {
 
217
            this._super.apply(this, arguments);
 
218
            this.$element.find('div').html(value);
 
219
            return value;
 
220
        }
 
221
    });
 
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)
 
226
        {
 
227
            this._super.apply(this, arguments);
 
228
            this.$element.find('div').html(filter_html(value, this.ckeditor_filter, this.ckeditor_writer));
 
229
            return value;
 
230
        }
 
231
    });
 
232
}
 
233