Ticket #8378: plugin.js

File plugin.js, 6.3 KB (added by datalink, 9 years ago)

uioptselect plugin

Line 
1/**************************************
2   
3        This plugin set a select with optgroup as a dialog ui element.
4       
5        If needed, load the plugin and set
6                type : 'optselect'
7        insteed of
8                type : 'select'
9        in your dialog.
10       
11        The items must be in an array like:
12                items : [
13                                [ groupname1,
14                                        [name, value], [name, value], [name, value]
15                                ],
16                                [ groupname2,
17                                        [name, value], [name, value], [name, value]
18                                ]
19                        ]
20       
21**************************************/
22
23(function()
24{
25        CKEDITOR.plugins.add( 'uioptselect',
26        {
27                onLoad : function()
28                {
29                        CKEDITOR.addCss(
30                                'select.cke_dialog_ui_input_select optgroup {' +
31                                        'padding: 0px 5px;' +
32                                        'font-weight: bold;' +
33                                '}' +
34                                'select.cke_dialog_ui_input_select option {' +
35                                        'padding: 1px 4px;' +
36                                '}' +
37                                'select.cke_dialog_ui_input_select optgroup option {' +
38                                        'padding: 1px 4px 1px 15px;' +
39                                '}'
40                        );
41                },
42               
43                init: function()
44                {
45                        var initPrivateObject = function( elementDefinition ) {
46                                        this._ || ( this._ = {} );
47                                        this._[ 'default' ] = this._.initValue = elementDefinition[ 'default' ] || '';
48                                        this._.required = elementDefinition.required || false;
49                                        var args = [ this._ ];
50                                        for ( var i = 1; i < arguments.length; i++ )
51                                                args.push( arguments[ i ] );
52                                        args.push( true );
53                                        CKEDITOR.tools.extend.apply( CKEDITOR.tools, args );
54                                        return this._;
55                                },
56                                commonBuilder = {
57                                        build: function( dialog, elementDefinition, output ) {
58                                                return new CKEDITOR.ui.dialog[ elementDefinition.type ]( dialog, elementDefinition, output );
59                                        }
60                                },
61                                commonPrototype = {
62                                        isChanged: function() {
63                                                return this.getValue() != this.getInitValue();
64                                        },
65                                        reset: function( noChangeEvent ) {
66                                                this.setValue( this.getInitValue(), noChangeEvent );
67                                        },
68                                        setInitValue: function() {
69                                                this._.initValue = this.getValue();
70                                        },
71                                        resetInitValue: function() {
72                                                this._.initValue = this._[ 'default' ];
73                                        },
74                                        getInitValue: function() {
75                                                return this._.initValue;
76                                        }
77                                },
78                                eventRegex = /^on([A-Z]\w+)/,
79                                cleanInnerDefinition = function( def ) {
80                                        // An inner UI element should not have the parent's type, title or events.
81                                        for ( var i in def ) {
82                                                if ( eventRegex.test( i ) || i == 'title' || i == 'type' )
83                                                        delete def[ i ];
84                                        }
85                                        return def;
86                                };
87                       
88                        CKEDITOR.tools.extend( CKEDITOR.ui.dialog, {
89                                optselect:  function( dialog, elementDefinition, htmlList ) {
90                                        if ( arguments.length < 3 )
91                                                return;
92
93                                        var _ = initPrivateObject.call( this, elementDefinition );
94
95                                        if ( elementDefinition.validate )
96                                                this.validate = elementDefinition.validate;
97
98                                        _.inputId = CKEDITOR.tools.getNextId() + '_select';
99
100                                        var innerHTML = function() {
101                                                var myDefinition = CKEDITOR.tools.extend(
102                                                                {},
103                                                                elementDefinition,
104                                                                {
105                                                                        id: ( elementDefinition.id ? elementDefinition.id + '_select' : CKEDITOR.tools.getNextId() + '_select' )
106                                                                },
107                                                                true
108                                                        ),
109                                                        html = [],
110                                                        innerHTML = [],
111                                                        attributes = { 'id': _.inputId, 'class': 'cke_dialog_ui_input_select', 'aria-labelledby': this._.labelId };
112
113                                                html.push( '<div class="cke_dialog_ui_input_select" role="presentation"' );
114                                                if ( elementDefinition.width )
115                                                        html.push( 'style="width:' + elementDefinition.width + '" ' );
116                                                html.push( '>' );
117
118                                                // Add multiple and size attributes from element definition.
119                                                if ( elementDefinition.size !== undefined )
120                                                        attributes.size = elementDefinition.size;
121                                                if ( elementDefinition.multiple !== undefined )
122                                                        attributes.multiple = elementDefinition.multiple;
123
124                                                cleanInnerDefinition( myDefinition );
125                                                for ( var i = 0; i < elementDefinition.items.length; i++ ) {
126                                                        var item = elementDefinition.items[i];
127                                                        if(typeof item[1] === 'object') {
128                                                                innerHTML.push(
129                                                                        '<optgroup label="' + CKEDITOR.tools.htmlEncode( item[0] ) + '">'
130                                                                );
131                                                                for ( var k = 0; k < item[1].length; k++ ) {
132                                                                        var opt = item[1][k];
133                                                                        innerHTML.push(
134                                                                                '<option' + 
135                                                                                CKEDITOR.tools.htmlEncode( opt[2] !== undefined ? ' ' + opt[2] : '' ) +
136                                                                                ' value="' + CKEDITOR.tools.htmlEncode( opt[1] !== undefined ? opt[1] : '' ).replace( /"/g, '&quot;' ) + '">' +
137                                                                                CKEDITOR.tools.htmlEncode( opt[0] ) +
138                                                                                '</option>'
139                                                                        );
140                                                                }
141                                                                innerHTML.push(
142                                                                        '</optgroup>'
143                                                                );
144                                                        }
145                                                        else {
146                                                                innerHTML.push(
147                                                                        '<option' + 
148                                                                        CKEDITOR.tools.htmlEncode( item[2] !== undefined ? ' ' + item[2] : '' ) +
149                                                                        ' value="' + CKEDITOR.tools.htmlEncode( item[1] !== undefined ? item[1] : '' ).replace( /"/g, '&quot;' ) + '">' +
150                                                                        CKEDITOR.tools.htmlEncode( item[0] ) +
151                                                                        '</option>'
152                                                                );
153                                                        }
154                                                }
155
156                                                if ( typeof myDefinition.inputStyle != 'undefined' )
157                                                        myDefinition.style = myDefinition.inputStyle;
158
159                                                _.optselect = new CKEDITOR.ui.dialog.uiElement( dialog, myDefinition, html, 'select', null, attributes, innerHTML.join( '' ) );
160
161                                                html.push( '</div>' );
162
163                                                return html.join( '' );
164                                        };
165
166                                        CKEDITOR.ui.dialog.labeledElement.call( this, dialog, elementDefinition, htmlList, innerHTML );
167                                }
168                        });
169                       
170                        CKEDITOR.ui.dialog.optselect.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.labeledElement(), {
171                                getInputElement: function() {
172                                        return this._.optselect.getElement();
173                                },
174                                add: function( label, value, index ) {
175                                        var option = new CKEDITOR.dom.element( 'option', this.getDialog().getParentEditor().document ),
176                                                selectElement = this.getInputElement().$;
177                                        option.$.text = label;
178                                        option.$.value = ( value === undefined || value === null ) ? label : value;
179                                        if ( index === undefined || index === null ) {
180                                                if ( CKEDITOR.env.ie ) {
181                                                        selectElement.add( option.$ );
182                                                } else {
183                                                        selectElement.add( option.$, null );
184                                                }
185                                        } else {
186                                                selectElement.add( option.$, index );
187                                        }
188                                        return this;
189                                },
190                                remove: function( index ) {
191                                        var selectElement = this.getInputElement().$;
192                                        selectElement.remove( index );
193                                        return this;
194                                },
195                                clear: function() {
196                                        var selectElement = this.getInputElement().$;
197                                        while ( selectElement.length > 0 )
198                                                selectElement.remove( 0 );
199                                        return this;
200                                },
201                                keyboardFocusable: true
202                        }, commonPrototype, true );
203                       
204                        CKEDITOR.dialog.addUIElement( 'optselect', commonBuilder );
205                }
206        });
207})();
208
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy