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
19 function loadStylesSet( callback )
\r
21 editor.getStylesSet( function( stylesDefinitions )
\r
23 if ( !stylesList.length )
\r
28 // Put all styles into an Array.
\r
29 for ( var i = 0, count = stylesDefinitions.length ; i < count ; i++ )
\r
31 var styleDefinition = stylesDefinitions[ i ];
\r
33 styleName = styleDefinition.name;
\r
35 style = styles[ styleName ] = new CKEDITOR.style( styleDefinition );
\r
36 style._name = styleName;
\r
37 style._.enterMode = config.enterMode;
\r
39 stylesList.push( style );
\r
42 // Sorts the Array, so the styles get grouped by type.
\r
43 stylesList.sort( sortStyles );
\r
46 callback && callback();
\r
50 editor.ui.addRichCombo( 'Styles',
\r
53 title : lang.panelTitle,
\r
54 className : 'cke_styles',
\r
58 css : editor.skin.editor.css.concat( config.contentsCss ),
\r
60 attributes : { 'aria-label' : lang.panelTitle }
\r
67 loadStylesSet( function()
\r
69 var style, styleName;
\r
71 // Loop over the Array, adding all items to the
\r
74 for ( var i = 0, count = stylesList.length ; i < count ; i++ )
\r
76 style = stylesList[ i ];
\r
77 styleName = style._name;
\r
79 var type = style.type;
\r
81 if ( type != lastType )
\r
83 combo.startGroup( lang[ 'panelTitle' + String( type ) ] );
\r
89 style.type == CKEDITOR.STYLE_OBJECT ? styleName : style.buildPreview(),
\r
99 onClick : function( value )
\r
102 editor.fire( 'saveSnapshot' );
\r
104 var style = styles[ value ],
\r
105 selection = editor.getSelection();
\r
107 var elementPath = new CKEDITOR.dom.elementPath( selection.getStartElement() );
\r
109 style[ style.checkActive( elementPath ) ? 'remove' : 'apply' ]( editor.document );
\r
111 editor.fire( 'saveSnapshot' );
\r
114 onRender : function()
\r
116 editor.on( 'selectionChange', function( ev )
\r
118 var currentValue = this.getValue();
\r
120 var elementPath = ev.data.path,
\r
121 elements = elementPath.elements;
\r
123 // For each element into the elements path.
\r
124 for ( var i = 0, count = elements.length, element ; i < count ; i++ )
\r
126 element = elements[i];
\r
128 // Check if the element is removable by any of
\r
130 for ( var value in styles )
\r
132 if ( styles[ value ].checkElementRemovable( element, true ) )
\r
134 if ( value != currentValue )
\r
135 this.setValue( value );
\r
141 // If no styles match, just empty it.
\r
142 this.setValue( '' );
\r
147 onOpen : function()
\r
149 if ( CKEDITOR.env.ie || CKEDITOR.env.webkit )
\r
152 var selection = editor.getSelection(),
\r
153 element = selection.getSelectedElement(),
\r
154 elementPath = new CKEDITOR.dom.elementPath( element || selection.getStartElement() );
\r
156 var counter = [ 0, 0, 0, 0 ];
\r
159 for ( var name in styles )
\r
161 var style = styles[ name ],
\r
164 if ( style.checkActive( elementPath ) )
\r
166 else if ( type == CKEDITOR.STYLE_OBJECT && !style.checkApplicable( elementPath ) )
\r
168 this.hideItem( name );
\r
175 if ( !counter[ CKEDITOR.STYLE_BLOCK ] )
\r
176 this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_BLOCK ) ] );
\r
178 if ( !counter[ CKEDITOR.STYLE_INLINE ] )
\r
179 this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_INLINE ) ] );
\r
181 if ( !counter[ CKEDITOR.STYLE_OBJECT ] )
\r
182 this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_OBJECT ) ] );
\r
186 editor.on( 'instanceReady', function() { loadStylesSet(); } );
\r
190 function sortStyles( styleA, styleB )
\r
192 var typeA = styleA.type,
\r
193 typeB = styleB.type;
\r
195 return typeA == typeB ? 0 :
\r
196 typeA == CKEDITOR.STYLE_OBJECT ? -1 :
\r
197 typeB == CKEDITOR.STYLE_OBJECT ? 1 :
\r
198 typeB == CKEDITOR.STYLE_BLOCK ? 1 :
\r