2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 CKEDITOR.plugins.add( 'stylescombo',
\r
10 requires : [ 'richcombo', 'styles' ],
\r
12 init : function( editor )
\r
14 var config = editor.config,
\r
15 lang = editor.lang.stylesCombo,
\r
20 function loadStylesSet( callback )
\r
22 editor.getStylesSet( function( stylesDefinitions )
\r
24 if ( !stylesList.length )
\r
29 // Put all styles into an Array.
\r
30 for ( var i = 0, count = stylesDefinitions.length ; i < count ; i++ )
\r
32 var styleDefinition = stylesDefinitions[ i ];
\r
34 styleName = styleDefinition.name;
\r
36 style = styles[ styleName ] = new CKEDITOR.style( styleDefinition );
\r
37 style._name = styleName;
\r
38 style._.enterMode = config.enterMode;
\r
40 stylesList.push( style );
\r
43 // Sorts the Array, so the styles get grouped by type.
\r
44 stylesList.sort( sortStyles );
\r
47 callback && callback();
\r
51 editor.ui.addRichCombo( 'Styles',
\r
54 title : lang.panelTitle,
\r
55 className : 'cke_styles',
\r
59 css : editor.skin.editor.css.concat( config.contentsCss ),
\r
61 attributes : { 'aria-label' : lang.panelTitle }
\r
68 loadStylesSet( function()
\r
77 // Loop over the Array, adding all items to the
\r
79 for ( i = 0, count = stylesList.length ; i < count ; i++ )
\r
81 style = stylesList[ i ];
\r
82 styleName = style._name;
\r
85 if ( type != lastType )
\r
87 combo.startGroup( lang[ 'panelTitle' + String( type ) ] );
\r
93 style.type == CKEDITOR.STYLE_OBJECT ? styleName : style.buildPreview(),
\r
103 onClick : function( value )
\r
106 editor.fire( 'saveSnapshot' );
\r
108 var style = styles[ value ],
\r
109 selection = editor.getSelection(),
\r
110 elementPath = new CKEDITOR.dom.elementPath( selection.getStartElement() );
\r
112 style[ style.checkActive( elementPath ) ? 'remove' : 'apply' ]( editor.document );
\r
114 editor.fire( 'saveSnapshot' );
\r
117 onRender : function()
\r
119 editor.on( 'selectionChange', function( ev )
\r
121 var currentValue = this.getValue(),
\r
122 elementPath = ev.data.path,
\r
123 elements = elementPath.elements;
\r
125 // For each element into the elements path.
\r
126 for ( var i = 0, count = elements.length, element ; i < count ; i++ )
\r
128 element = elements[i];
\r
130 // Check if the element is removable by any of
\r
132 for ( var value in styles )
\r
134 if ( styles[ value ].checkElementRemovable( element, true ) )
\r
136 if ( value != currentValue )
\r
137 this.setValue( value );
\r
143 // If no styles match, just empty it.
\r
144 this.setValue( '' );
\r
149 onOpen : function()
\r
151 if ( CKEDITOR.env.ie || CKEDITOR.env.webkit )
\r
154 var selection = editor.getSelection(),
\r
155 element = selection.getSelectedElement(),
\r
156 elementPath = new CKEDITOR.dom.elementPath( element || selection.getStartElement() ),
\r
157 counter = [ 0, 0, 0, 0 ];
\r
161 for ( var name in styles )
\r
163 var style = styles[ name ],
\r
166 if ( style.checkActive( elementPath ) )
\r
168 else if ( type == CKEDITOR.STYLE_OBJECT && !style.checkApplicable( elementPath ) )
\r
170 this.hideItem( name );
\r
177 if ( !counter[ CKEDITOR.STYLE_BLOCK ] )
\r
178 this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_BLOCK ) ] );
\r
180 if ( !counter[ CKEDITOR.STYLE_INLINE ] )
\r
181 this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_INLINE ) ] );
\r
183 if ( !counter[ CKEDITOR.STYLE_OBJECT ] )
\r
184 this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_OBJECT ) ] );
\r
187 // Force a reload of the data
\r
192 delete combo._.panel;
\r
193 delete combo._.list;
\r
194 combo._.committed = 0;
\r
195 combo._.items = {};
\r
196 combo._.state = CKEDITOR.TRISTATE_OFF;
\r
204 editor.on( 'instanceReady', function() { loadStylesSet(); } );
\r
208 function sortStyles( styleA, styleB )
\r
210 var typeA = styleA.type,
\r
211 typeB = styleB.type;
\r
213 return typeA == typeB ? 0 :
\r
214 typeA == CKEDITOR.STYLE_OBJECT ? -1 :
\r
215 typeB == CKEDITOR.STYLE_OBJECT ? 1 :
\r
216 typeB == CKEDITOR.STYLE_BLOCK ? 1 :
\r