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 CKEDITOR.plugins.add( 'format',
  7 {
  8 	requires : [ 'richcombo', 'styles' ],
  9
 10 	init : function( editor )
 11 	{
 12 		var config = editor.config,
 13 			lang = editor.lang.format;
 14
 15 		var saveRanges;
 16
 17 		// Gets the list of tags from the settings.
 18 		var tags = config.format_tags.split( ';' );
 19
 20 		// Create style objects for all defined styles.
 21 		var styles = {};
 22 		for ( var i = 0 ; i < tags.length ; i++ )
 23 		{
 24 			var tag = tags[ i ];
 25 			styles[ tag ] = new CKEDITOR.style( config[ 'format_' + tag ] );
 26 		}
 27
 28 		editor.ui.addRichCombo( 'Format',
 29 			{
 30 				label : lang.label,
 31 				title : lang.panelTitle,
 32 				className : 'cke_format',
 33 				multiSelect : false,
 34
 35 				panel :
 36 				{
 37 					css : [ config.contentsCss, CKEDITOR.getUrl( editor.skinPath + 'editor.css' ) ]
 38 				},
 39
 40 				init : function()
 41 				{
 42 					this.startGroup( lang.panelTitle );
 43
 44 					for ( var tag in styles )
 45 					{
 46 						var label = lang[ 'tag_' + tag ];
 47
 48 						// Add the tag entry to the panel list.
 49 						this.add( tag, '<' + tag + '>' + label + '</' + tag + '>', label );
 50 					}
 51 				},
 52
 53 				onClick : function( value )
 54 				{
 55 					editor.focus();
 56
 57 					if ( saveRanges )
 58 					{
 59 						editor.getSelection().selectRanges( saveRanges );
 60 						saveRanges = false;
 61 					}
 62
 63 					styles[ value ].apply( editor.document );
 64 				},
 65
 66 				onRender : function()
 67 				{
 68 					editor.on( 'selectionChange', function( ev )
 69 						{
 70 							var currentTag = this.getValue();
 71
 72 							var elementPath = ev.data.path;
 73
 74 							for ( var tag in styles )
 75 							{
 76 								if ( styles[ tag ].checkActive( elementPath ) )
 77 								{
 78 									if ( tag != currentTag )
 79 										this.setValue( tag, editor.lang.format[ 'tag_' + tag ] );
 80 									return;
 81 								}
 82 							}
 83
 84 							// If no styles match, just empty it.
 85 							this.setValue( '' );
 86 						},
 87 						this);
 88 				},
 89
 90 				onOpen : function()
 91 				{
 92 					if ( CKEDITOR.env.ie )
 93 					{
 94 						editor.focus();
 95 						saveRanges = editor.getSelection().getRanges();
 96 					}
 97 				},
 98
 99 				onClose : function()
100 				{
101 					saveRanges = null;
102 				}
103 			});
104 	}
105 });
106
107 CKEDITOR.config.format_tags = 'p;h1;h2;h3;h4;h5;h6;pre;address;div';
108
109 CKEDITOR.config.format_p		= { element : 'p' };
110 CKEDITOR.config.format_div		= { element : 'div' };
111 CKEDITOR.config.format_pre		= { element : 'pre' };
112 CKEDITOR.config.format_address	= { element : 'address' };
113 CKEDITOR.config.format_h1		= { element : 'h1' };
114 CKEDITOR.config.format_h2		= { element : 'h2' };
115 CKEDITOR.config.format_h3		= { element : 'h3' };
116 CKEDITOR.config.format_h4		= { element : 'h4' };
117 CKEDITOR.config.format_h5		= { element : 'h5' };
118 CKEDITOR.config.format_h6		= { element : 'h6' };
119