Ticket #12627: plugin.js

File plugin.js, 6.6 KB (added by Moussi, 10 years ago)

plugin.js

Line 
1/**
2 * Basic sample plugin inserting current date and time into CKEditor editing area.
3 */
4
5// Register the plugin with the editor.
6// http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.plugins.html
7CKEDITOR.plugins.add( 'cellborder',
8{
9    requires: 'menubutton',
10    icons: 'border_bottom,border_double_bottom,border_left,border_outer,border_right,border_top,border_none',
11        // The plugin initialization logic goes inside this method.
12        // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.pluginDefinition.html#init
13    init: function( editor ) {
14        var borderouter = ( editor.config.borderTypeList || [ 'border_bottom:border bottom:border-bottom:1px solid black',
15                'border_top:border top:border-top:1px solid black',
16                'border_right:border right:border-right:1px solid black',
17                'border_left:border left:border-left:1px solid black',
18                'border_double_bottom:border double bottom:border-bottom:3px double black'] ),
19            plugin = this,
20            items = {},
21            parts,
22            curBorderType,
23            i;
24
25        // Registers command.
26        editor.addCommand( 'border', {
27            allowedContent : 'table tr th td[style]{*} caption;',
28            contextSensitive: true,
29            exec: function( editor, item) {
30                if ( item )
31                    editor[ item.style.checkActive( editor.elementPath() ) ? 'removeStyle' : 'applyStyle' ]( item.style );
32            },
33
34            remove: function(editor){
35                editor.removeStyle();
36            },
37
38            refresh: function( editor, path ) {
39                if(path.contains('table')){
40                    var curStyle = plugin.getCurrentBorderType( editor );
41                    if ( curStyle ){
42                        var att = curStyle.getAttribute( 'style' );
43                        if(att){
44                            var parts = att.split( ':' );
45                            this.setState( parts[0].indexOf('border') == 0 ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
46                        }
47                        else
48                            this.setState(CKEDITOR.TRISTATE_OFF );
49                    }
50                    else
51                        this.setState(CKEDITOR.TRISTATE_OFF );
52                }
53                else
54                    this.setState(CKEDITOR.TRISTATE_DISABLED);
55            }
56        } );
57
58        // Parse borderConfigStrings, and create items entry for each border.
59        for ( i = 0; i < borderouter.length; i++ ) {
60            parts = borderouter[ i ].split( ':' );
61            curBorderType = parts[ 2 ];
62            items[ parts[ 0 ] ] = {
63                label: parts[ 1 ],
64                borderType: curBorderType,
65                itemName : parts[0],
66                group: 'border_outer',
67                icon: parts[ 0 ],
68                order: i,
69                onClick: function() {
70                    editor.execCommand( 'border', this);
71                },
72                role: 'menuitemcheckbox'
73            };
74
75            // Init style property.
76            items[ parts[ 0 ] ].style = new CKEDITOR.style( {
77                element: 'td',
78                attributes: {
79                    style:      curBorderType + ":" + parts[3]
80                }
81            } );
82        }
83
84        items.border_none = {
85            label: 'Border none',
86            group: 'border_general',
87            icon: 'border_none',
88            order: items.length,
89            onClick: function() {
90                var curBorderType = plugin.getCurrentBorderType(editor);
91
92                if ( curBorderType ){
93                    var att = curBorderType.getAttribute( 'style' );
94                    if(att){
95                        parts = att.split( ':' );
96                        this.style = new CKEDITOR.style( {
97                            element: 'td',
98                            attributes: {
99                                style:  att,
100                            }
101                        });
102                        if ( curBorderType )
103                            editor.execCommand( 'border', this);
104                    }
105                }
106            }
107        };
108
109
110        items.border= {
111            label: 'border outer',
112            group: 'border_general',
113            icon: 'border_outer',
114            borderType: 'border',
115            order: items.length,
116            onClick: function() {
117                editor.execCommand( 'border', this );
118            }
119        };
120
121        items.border.style = new CKEDITOR.style( {
122            element: 'td',
123            attributes: {
124                style:  'border: 1px solid black',
125            }
126        } );
127
128        // Initialize groups for menu.
129        editor.addMenuGroup( 'border_outer', 1 );
130        editor.addMenuGroup( 'border_general', 2);
131        editor.addMenuItems( items );
132
133        editor.ui.add( 'Border',  CKEDITOR.UI_MENUBUTTON, {
134            label: 'Rahmenlinie hinzufügen',
135            icon: "border_bottom",
136            toolbar: 'insert',
137            command: 'border',
138            allowedContent : 'table tr th td[style]{*} caption;',
139            onMenu: function() {
140                var activeItems = {}, parts;
141                curBorderType = plugin.getCurrentBorderType(editor);
142                for ( var prop in items )
143                    activeItems[ prop ] = CKEDITOR.TRISTATE_OFF;
144                if ( curBorderType ){
145                    var att =   curBorderType.getAttribute( 'style' );
146                    if(att){
147                        parts = att.split( ':' );
148                        borderType = parts[0];
149                        activeItems[ borderType ] = CKEDITOR.TRISTATE_ON;
150                    }
151                }
152                return activeItems;
153            }
154        } );
155    },
156
157    // Gets the bordertype for the current editor selection.
158    // @param {CKEDITOR.editor} editor
159    // @returns {CKEDITOR.dom.element} The bordertype element, if any.
160    getCurrentBorderType: function( editor ) {
161        var elementPath = editor.elementPath(),
162            activePath = elementPath && elementPath.elements,
163            pathMember, ret;
164        // IE8: upon initialization if there is no path elementPath() returns null.
165        if ( elementPath ) {
166            for ( var i = 0; i < activePath.length; i++ ) {
167                pathMember = activePath[ i ];
168                if ( !ret && pathMember.getName() == 'td' && pathMember.hasAttribute( 'style' )){
169                    ret = pathMember;
170                }
171            }
172        }
173        return ret;
174    }
175} );
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy