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
76 // Loop over the Array, adding all items to the
\r
78 for ( i = 0, count = stylesList.length ; i < count ; i++ )
\r
80 style = stylesList[ i ];
\r
81 styleName = style._name;
\r
84 if ( type != lastType )
\r
86 combo.startGroup( lang[ 'panelTitle' + String( type ) ] );
\r
92 style.type == CKEDITOR.STYLE_OBJECT ? styleName : style.buildPreview(),
\r
102 onClick : function( value )
\r
105 editor.fire( 'saveSnapshot' );
\r
107 var style = styles[ value ],
\r
108 selection = editor.getSelection(),
\r
109 elementPath = new CKEDITOR.dom.elementPath( selection.getStartElement() );
\r
111 style[ style.checkActive( elementPath ) ? 'remove' : 'apply' ]( editor.document );
\r
113 editor.fire( 'saveSnapshot' );
\r
116 onRender : function()
\r
118 editor.on( 'selectionChange', function( ev )
\r
120 var currentValue = this.getValue(),
\r
121 elementPath = ev.data.path,
\r
122 elements = elementPath.elements;
\r
124 // For each element into the elements path.
\r
125 for ( var i = 0, count = elements.length, element ; i < count ; i++ )
\r
127 element = elements[i];
\r
129 // Check if the element is removable by any of
\r
131 for ( var value in styles )
\r
133 if ( styles[ value ].checkElementRemovable( element, true ) )
\r
135 if ( value != currentValue )
\r
136 this.setValue( value );
\r
142 // If no styles match, just empty it.
\r
143 this.setValue( '' );
\r
148 onOpen : function()
\r
150 if ( CKEDITOR.env.ie || CKEDITOR.env.webkit )
\r
153 var selection = editor.getSelection(),
\r
154 element = selection.getSelectedElement(),
\r
155 elementPath = new CKEDITOR.dom.elementPath( element || selection.getStartElement() ),
\r
156 counter = [ 0, 0, 0, 0 ];
\r
160 for ( var name in styles )
\r
162 var style = styles[ name ],
\r
165 if ( style.checkActive( elementPath ) )
\r
167 else if ( type == CKEDITOR.STYLE_OBJECT && !style.checkApplicable( elementPath ) )
\r
169 this.hideItem( name );
\r
176 if ( !counter[ CKEDITOR.STYLE_BLOCK ] )
\r
177 this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_BLOCK ) ] );
\r
179 if ( !counter[ CKEDITOR.STYLE_INLINE ] )
\r
180 this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_INLINE ) ] );
\r
182 if ( !counter[ CKEDITOR.STYLE_OBJECT ] )
\r
183 this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_OBJECT ) ] );
\r
187 editor.on( 'instanceReady', function() { loadStylesSet(); } );
\r
191 function sortStyles( styleA, styleB )
\r
193 var typeA = styleA.type,
\r
194 typeB = styleB.type;
\r
196 return typeA == typeB ? 0 :
\r
197 typeA == CKEDITOR.STYLE_OBJECT ? -1 :
\r
198 typeB == CKEDITOR.STYLE_OBJECT ? 1 :
\r
199 typeB == CKEDITOR.STYLE_BLOCK ? 1 :
\r