[3019] | 1 | /*
|
---|
| 2 | Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
|
---|
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | (function()
|
---|
| 7 | {
|
---|
| 8 | CKEDITOR.plugins.add( 'stylescombo',
|
---|
| 9 | {
|
---|
| 10 | requires : [ 'richcombo', 'styles' ],
|
---|
| 11 |
|
---|
| 12 | init : function( editor )
|
---|
| 13 | {
|
---|
| 14 | var config = editor.config,
|
---|
| 15 | lang = editor.lang.stylesCombo,
|
---|
| 16 | pluginPath = this.path,
|
---|
| 17 | styles;
|
---|
| 18 |
|
---|
| 19 | editor.ui.addRichCombo( 'Styles',
|
---|
| 20 | {
|
---|
| 21 | label : lang.label,
|
---|
| 22 | title : lang.panelTitle,
|
---|
| 23 | voiceLabel : lang.voiceLabel,
|
---|
| 24 | className : 'cke_styles',
|
---|
| 25 | multiSelect : true,
|
---|
| 26 |
|
---|
| 27 | panel :
|
---|
| 28 | {
|
---|
| 29 | css : [ CKEDITOR.getUrl( editor.skinPath + 'editor.css' ) ].concat( config.contentsCss ),
|
---|
| 30 | voiceLabel : lang.panelVoiceLabel
|
---|
| 31 | },
|
---|
| 32 |
|
---|
| 33 | init : function()
|
---|
| 34 | {
|
---|
| 35 | var combo = this,
|
---|
| 36 | stylesSet = config.stylesCombo_stylesSet.split( ':' );
|
---|
| 37 |
|
---|
| 38 | var stylesSetPath = stylesSet[ 1 ] ?
|
---|
| 39 | stylesSet.slice( 1 ).join( ':' ) : // #4481
|
---|
| 40 | CKEDITOR.getUrl( pluginPath + 'styles/' + stylesSet[ 0 ] + '.js' ) ;
|
---|
| 41 |
|
---|
| 42 | stylesSet = stylesSet[ 0 ];
|
---|
| 43 |
|
---|
| 44 | CKEDITOR.loadStylesSet( stylesSet, stylesSetPath, function( stylesDefinitions )
|
---|
| 45 | {
|
---|
| 46 | var style,
|
---|
| 47 | styleName,
|
---|
| 48 | stylesList = [];
|
---|
| 49 |
|
---|
| 50 | styles = {};
|
---|
| 51 |
|
---|
| 52 | // Put all styles into an Array.
|
---|
| 53 | for ( var i = 0 ; i < stylesDefinitions.length ; i++ )
|
---|
| 54 | {
|
---|
| 55 | var styleDefinition = stylesDefinitions[ i ];
|
---|
| 56 |
|
---|
| 57 | styleName = styleDefinition.name;
|
---|
| 58 |
|
---|
| 59 | style = styles[ styleName ] = new CKEDITOR.style( styleDefinition );
|
---|
| 60 | style._name = styleName;
|
---|
| 61 |
|
---|
| 62 | stylesList.push( style );
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | // Sorts the Array, so the styles get grouped
|
---|
| 66 | // by type.
|
---|
| 67 | stylesList.sort( sortStyles );
|
---|
| 68 |
|
---|
| 69 | // Loop over the Array, adding all items to the
|
---|
| 70 | // combo.
|
---|
| 71 | var lastType;
|
---|
| 72 | for ( i = 0 ; i < stylesList.length ; i++ )
|
---|
| 73 | {
|
---|
| 74 | style = stylesList[ i ];
|
---|
| 75 | styleName = style._name;
|
---|
| 76 |
|
---|
| 77 | var type = style.type;
|
---|
| 78 |
|
---|
| 79 | if ( type != lastType )
|
---|
| 80 | {
|
---|
| 81 | combo.startGroup( lang[ 'panelTitle' + String( type ) ] );
|
---|
| 82 | lastType = type;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | combo.add(
|
---|
| 86 | styleName,
|
---|
| 87 | style.type == CKEDITOR.STYLE_OBJECT ? styleName : buildPreview( style._.definition ),
|
---|
| 88 | styleName );
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | combo.commit();
|
---|
| 92 |
|
---|
| 93 | combo.onOpen();
|
---|
| 94 | });
|
---|
| 95 | },
|
---|
| 96 |
|
---|
| 97 | onClick : function( value )
|
---|
| 98 | {
|
---|
| 99 | editor.focus();
|
---|
| 100 | editor.fire( 'saveSnapshot' );
|
---|
| 101 |
|
---|
| 102 | var style = styles[ value ],
|
---|
| 103 | selection = editor.getSelection();
|
---|
| 104 |
|
---|
| 105 | if ( style.type == CKEDITOR.STYLE_OBJECT )
|
---|
| 106 | {
|
---|
| 107 | var element = selection.getSelectedElement();
|
---|
| 108 | if ( element )
|
---|
| 109 | style.applyToObject( element );
|
---|
| 110 |
|
---|
| 111 | return;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | var elementPath = new CKEDITOR.dom.elementPath( selection.getStartElement() );
|
---|
| 115 |
|
---|
| 116 | if ( style.type == CKEDITOR.STYLE_INLINE && style.checkActive( elementPath ) )
|
---|
| 117 | style.remove( editor.document );
|
---|
| 118 | else
|
---|
| 119 | style.apply( editor.document );
|
---|
| 120 |
|
---|
| 121 | editor.fire( 'saveSnapshot' );
|
---|
| 122 | },
|
---|
| 123 |
|
---|
| 124 | onRender : function()
|
---|
| 125 | {
|
---|
| 126 | editor.on( 'selectionChange', function( ev )
|
---|
| 127 | {
|
---|
| 128 | var currentValue = this.getValue();
|
---|
| 129 |
|
---|
| 130 | var elementPath = ev.data.path,
|
---|
| 131 | elements = elementPath.elements;
|
---|
| 132 |
|
---|
| 133 | // For each element into the elements path.
|
---|
| 134 | for ( var i = 0, element ; i < elements.length ; i++ )
|
---|
| 135 | {
|
---|
| 136 | element = elements[i];
|
---|
| 137 |
|
---|
| 138 | // Check if the element is removable by any of
|
---|
| 139 | // the styles.
|
---|
| 140 | for ( var value in styles )
|
---|
| 141 | {
|
---|
| 142 | if ( styles[ value ].checkElementRemovable( element, true ) )
|
---|
| 143 | {
|
---|
| 144 | if ( value != currentValue )
|
---|
| 145 | this.setValue( value );
|
---|
| 146 | return;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | // If no styles match, just empty it.
|
---|
| 152 | this.setValue( '' );
|
---|
| 153 | },
|
---|
| 154 | this);
|
---|
| 155 | },
|
---|
| 156 |
|
---|
| 157 | onOpen : function()
|
---|
| 158 | {
|
---|
| 159 | if ( CKEDITOR.env.ie )
|
---|
| 160 | editor.focus();
|
---|
| 161 |
|
---|
| 162 | var selection = editor.getSelection();
|
---|
| 163 |
|
---|
| 164 | var element = selection.getSelectedElement(),
|
---|
| 165 | elementName = element && element.getName(),
|
---|
| 166 | elementPath = new CKEDITOR.dom.elementPath( element || selection.getStartElement() );
|
---|
| 167 |
|
---|
| 168 | var counter = [ 0, 0, 0, 0 ];
|
---|
| 169 | this.showAll();
|
---|
| 170 | this.unmarkAll();
|
---|
| 171 | for ( var name in styles )
|
---|
| 172 | {
|
---|
| 173 | var style = styles[ name ],
|
---|
| 174 | type = style.type;
|
---|
| 175 |
|
---|
| 176 | if ( type == CKEDITOR.STYLE_OBJECT )
|
---|
| 177 | {
|
---|
| 178 | if ( element && style.element == elementName )
|
---|
| 179 | {
|
---|
| 180 | if ( style.checkElementRemovable( element, true ) )
|
---|
| 181 | this.mark( name );
|
---|
| 182 |
|
---|
| 183 | counter[ type ]++;
|
---|
| 184 | }
|
---|
| 185 | else
|
---|
| 186 | this.hideItem( name );
|
---|
| 187 | }
|
---|
| 188 | else
|
---|
| 189 | {
|
---|
| 190 | if ( style.checkActive( elementPath ) )
|
---|
| 191 | this.mark( name );
|
---|
| 192 |
|
---|
| 193 | counter[ type ]++;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | if ( !counter[ CKEDITOR.STYLE_BLOCK ] )
|
---|
| 198 | this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_BLOCK ) ] );
|
---|
| 199 |
|
---|
| 200 | if ( !counter[ CKEDITOR.STYLE_INLINE ] )
|
---|
| 201 | this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_INLINE ) ] );
|
---|
| 202 |
|
---|
| 203 | if ( !counter[ CKEDITOR.STYLE_OBJECT ] )
|
---|
| 204 | this.hideGroup( lang[ 'panelTitle' + String( CKEDITOR.STYLE_OBJECT ) ] );
|
---|
| 205 | }
|
---|
| 206 | });
|
---|
| 207 | }
|
---|
| 208 | });
|
---|
| 209 |
|
---|
| 210 | var stylesSets = {};
|
---|
| 211 |
|
---|
| 212 | CKEDITOR.addStylesSet = function( name, styles )
|
---|
| 213 | {
|
---|
| 214 | stylesSets[ name ] = styles;
|
---|
| 215 | };
|
---|
| 216 |
|
---|
| 217 | CKEDITOR.loadStylesSet = function( name, url, callback )
|
---|
| 218 | {
|
---|
| 219 | var stylesSet = stylesSets[ name ];
|
---|
| 220 |
|
---|
| 221 | if ( stylesSet )
|
---|
| 222 | {
|
---|
| 223 | callback( stylesSet );
|
---|
| 224 | return ;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | CKEDITOR.scriptLoader.load( url, function()
|
---|
| 228 | {
|
---|
| 229 | callback( stylesSets[ name ] );
|
---|
| 230 | });
|
---|
| 231 | };
|
---|
| 232 |
|
---|
| 233 | function buildPreview( styleDefinition )
|
---|
| 234 | {
|
---|
| 235 | var html = [];
|
---|
| 236 |
|
---|
| 237 | var elementName = styleDefinition.element;
|
---|
| 238 |
|
---|
| 239 | // Avoid <bdo> in the preview.
|
---|
| 240 | if ( elementName == 'bdo' )
|
---|
| 241 | elementName = 'span';
|
---|
| 242 |
|
---|
| 243 | html = [ '<', elementName ];
|
---|
| 244 |
|
---|
| 245 | // Assign all defined attributes.
|
---|
| 246 | var attribs = styleDefinition.attributes;
|
---|
| 247 | if ( attribs )
|
---|
| 248 | {
|
---|
| 249 | for ( var att in attribs )
|
---|
| 250 | {
|
---|
| 251 | html.push( ' ', att, '="', attribs[ att ], '"' );
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | // Assign the style attribute.
|
---|
| 256 | var cssStyle = CKEDITOR.style.getStyleText( styleDefinition );
|
---|
| 257 | if ( cssStyle )
|
---|
| 258 | html.push( ' style="', cssStyle, '"' );
|
---|
| 259 |
|
---|
| 260 | html.push( '>', styleDefinition.name, '</', elementName, '>' );
|
---|
| 261 |
|
---|
| 262 | return html.join( '' );
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | function sortStyles( styleA, styleB )
|
---|
| 266 | {
|
---|
| 267 | var typeA = styleA.type,
|
---|
| 268 | typeB = styleB.type;
|
---|
| 269 |
|
---|
| 270 | return typeA == typeB ? 0 :
|
---|
| 271 | typeA == CKEDITOR.STYLE_OBJECT ? -1 :
|
---|
| 272 | typeB == CKEDITOR.STYLE_OBJECT ? 1 :
|
---|
| 273 | typeB == CKEDITOR.STYLE_BLOCK ? 1 :
|
---|
| 274 | -1;
|
---|
| 275 | }
|
---|
| 276 | })();
|
---|
| 277 |
|
---|
| 278 | /**
|
---|
| 279 | * The "styles definition set" to load into the styles combo. The styles may
|
---|
| 280 | * be defined in the page containing the editor, or can be loaded on demand
|
---|
| 281 | * from an external file when opening the styles combo for the fist time. In
|
---|
| 282 | * the second case, if this setting contains only a name, the styles definition
|
---|
| 283 | * file will be loaded from the "styles" folder inside the stylescombo plugin
|
---|
| 284 | * folder. Otherwise, this setting has the "name:url" syntax, making it
|
---|
| 285 | * possible to set the URL from which loading the styles file.
|
---|
| 286 | * @type string
|
---|
| 287 | * @default 'default'
|
---|
| 288 | * @example
|
---|
| 289 | * // Load from the stylescombo styles folder (mystyles.js file).
|
---|
| 290 | * config.stylesCombo_stylesSet = 'mystyles';
|
---|
| 291 | * @example
|
---|
| 292 | * // Load from a relative URL.
|
---|
| 293 | * config.stylesCombo_stylesSet = 'mystyles:/editorstyles/styles.js';
|
---|
| 294 | * @example
|
---|
| 295 | * // Load from a full URL.
|
---|
| 296 | * config.stylesCombo_stylesSet = 'mystyles:http://www.example.com/editorstyles/styles.js';
|
---|
| 297 | */
|
---|
| 298 | CKEDITOR.config.stylesCombo_stylesSet = 'default';
|
---|