JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
402672dbfe916b61599002fc154a09e538f4c9dd
[ckeditor.git] / _source / plugins / format / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 CKEDITOR.plugins.add( 'format',\r
7 {\r
8         requires : [ 'richcombo', 'styles' ],\r
9 \r
10         init : function( editor )\r
11         {\r
12                 var config = editor.config,\r
13                         lang = editor.lang.format;\r
14 \r
15                 // Gets the list of tags from the settings.\r
16                 var tags = config.format_tags.split( ';' );\r
17 \r
18                 // Create style objects for all defined styles.\r
19                 var styles = {};\r
20                 for ( var i = 0 ; i < tags.length ; i++ )\r
21                 {\r
22                         var tag = tags[ i ];\r
23                         styles[ tag ] = new CKEDITOR.style( config[ 'format_' + tag ] );\r
24                 }\r
25 \r
26                 editor.ui.addRichCombo( 'Format',\r
27                         {\r
28                                 label : lang.label,\r
29                                 title : lang.panelTitle,\r
30                                 className : 'cke_format',\r
31                                 panel :\r
32                                 {\r
33                                         css : editor.skin.editor.css.concat( config.contentsCss ),\r
34                                         multiSelect : false,\r
35                                         attributes : { 'aria-label' : lang.panelTitle }\r
36                                 },\r
37 \r
38                                 init : function()\r
39                                 {\r
40                                         this.startGroup( lang.panelTitle );\r
41 \r
42                                         for ( var tag in styles )\r
43                                         {\r
44                                                 var label = lang[ 'tag_' + tag ];\r
45 \r
46                                                 // Add the tag entry to the panel list.\r
47                                                 this.add( tag, '<' + tag + '>' + label + '</' + tag + '>', label );\r
48                                         }\r
49                                 },\r
50 \r
51                                 onClick : function( value )\r
52                                 {\r
53                                         editor.focus();\r
54                                         editor.fire( 'saveSnapshot' );\r
55 \r
56                                         styles[ value ].apply( editor.document );\r
57 \r
58                                         // Save the undo snapshot after all changes are affected. (#4899)\r
59                                         setTimeout( function()\r
60                                         {\r
61                                                 editor.fire( 'saveSnapshot' );\r
62                                         }, 0 );\r
63                                 },\r
64 \r
65                                 onRender : function()\r
66                                 {\r
67                                         editor.on( 'selectionChange', function( ev )\r
68                                                 {\r
69                                                         var currentTag = this.getValue();\r
70 \r
71                                                         var elementPath = ev.data.path;\r
72 \r
73                                                         for ( var tag in styles )\r
74                                                         {\r
75                                                                 if ( styles[ tag ].checkActive( elementPath ) )\r
76                                                                 {\r
77                                                                         if ( tag != currentTag )\r
78                                                                                 this.setValue( tag, editor.lang.format[ 'tag_' + tag ] );\r
79                                                                         return;\r
80                                                                 }\r
81                                                         }\r
82 \r
83                                                         // If no styles match, just empty it.\r
84                                                         this.setValue( '' );\r
85                                                 },\r
86                                                 this);\r
87                                 }\r
88                         });\r
89         }\r
90 });\r
91 \r
92 /**\r
93  * A list of semi colon separated style names (by default tags) representing\r
94  * the style definition for each entry to be displayed in the Format combo in\r
95  * the toolbar. Each entry must have its relative definition configuration in a\r
96  * setting named "format_(tagName)". For example, the "p" entry has its\r
97  * definition taken from config.format_p.\r
98  * @type String\r
99  * @default 'p;h1;h2;h3;h4;h5;h6;pre;address;div'\r
100  * @example\r
101  * config.format_tags = 'p;h2;h3;pre'\r
102  */\r
103 CKEDITOR.config.format_tags = 'p;h1;h2;h3;h4;h5;h6;pre;address;div';\r
104 \r
105 /**\r
106  * The style definition to be used to apply the "Normal" format.\r
107  * @type Object\r
108  * @default { element : 'p' }\r
109  * @example\r
110  * config.format_p = { element : 'p', attributes : { class : 'normalPara' } };\r
111  */\r
112 CKEDITOR.config.format_p = { element : 'p' };\r
113 \r
114 /**\r
115  * The style definition to be used to apply the "Normal (DIV)" format.\r
116  * @type Object\r
117  * @default { element : 'div' }\r
118  * @example\r
119  * config.format_div = { element : 'div', attributes : { class : 'normalDiv' } };\r
120  */\r
121 CKEDITOR.config.format_div = { element : 'div' };\r
122 \r
123 /**\r
124  * The style definition to be used to apply the "Formatted" format.\r
125  * @type Object\r
126  * @default { element : 'pre' }\r
127  * @example\r
128  * config.format_pre = { element : 'pre', attributes : { class : 'code' } };\r
129  */\r
130 CKEDITOR.config.format_pre = { element : 'pre' };\r
131 \r
132 /**\r
133  * The style definition to be used to apply the "Address" format.\r
134  * @type Object\r
135  * @default { element : 'address' }\r
136  * @example\r
137  * config.format_address = { element : 'address', attributes : { class : 'styledAddress' } };\r
138  */\r
139 CKEDITOR.config.format_address = { element : 'address' };\r
140 \r
141 /**\r
142  * The style definition to be used to apply the "Heading 1" format.\r
143  * @type Object\r
144  * @default { element : 'h1' }\r
145  * @example\r
146  * config.format_h1 = { element : 'h1', attributes : { class : 'contentTitle1' } };\r
147  */\r
148 CKEDITOR.config.format_h1 = { element : 'h1' };\r
149 \r
150 /**\r
151  * The style definition to be used to apply the "Heading 1" format.\r
152  * @type Object\r
153  * @default { element : 'h2' }\r
154  * @example\r
155  * config.format_h2 = { element : 'h2', attributes : { class : 'contentTitle2' } };\r
156  */\r
157 CKEDITOR.config.format_h2 = { element : 'h2' };\r
158 \r
159 /**\r
160  * The style definition to be used to apply the "Heading 1" format.\r
161  * @type Object\r
162  * @default { element : 'h3' }\r
163  * @example\r
164  * config.format_h3 = { element : 'h3', attributes : { class : 'contentTitle3' } };\r
165  */\r
166 CKEDITOR.config.format_h3 = { element : 'h3' };\r
167 \r
168 /**\r
169  * The style definition to be used to apply the "Heading 1" format.\r
170  * @type Object\r
171  * @default { element : 'h4' }\r
172  * @example\r
173  * config.format_h4 = { element : 'h4', attributes : { class : 'contentTitle4' } };\r
174  */\r
175 CKEDITOR.config.format_h4 = { element : 'h4' };\r
176 \r
177 /**\r
178  * The style definition to be used to apply the "Heading 1" format.\r
179  * @type Object\r
180  * @default { element : 'h5' }\r
181  * @example\r
182  * config.format_h5 = { element : 'h5', attributes : { class : 'contentTitle5' } };\r
183  */\r
184 CKEDITOR.config.format_h5 = { element : 'h5' };\r
185 \r
186 /**\r
187  * The style definition to be used to apply the "Heading 1" format.\r
188  * @type Object\r
189  * @default { element : 'h6' }\r
190  * @example\r
191  * config.format_h6 = { element : 'h6', attributes : { class : 'contentTitle6' } };\r
192  */\r
193 CKEDITOR.config.format_h6 = { element : 'h6' };\r