JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
2a14bb07dcfaa54f73dd87750cd2a50cae53e239
[ckeditor.git] / _source / plugins / colorbutton / 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( 'colorbutton',\r
7 {\r
8         requires : [ 'panelbutton', 'floatpanel', 'styles' ],\r
9 \r
10         init : function( editor )\r
11         {\r
12                 var config = editor.config,\r
13                         lang = editor.lang.colorButton;\r
14 \r
15                 var clickFn;\r
16 \r
17                 if ( !CKEDITOR.env.hc )\r
18                 {\r
19                         addButton( 'TextColor', 'fore', lang.textColorTitle );\r
20                         addButton( 'BGColor', 'back', lang.bgColorTitle );\r
21                 }\r
22 \r
23                 function addButton( name, type, title )\r
24                 {\r
25                         editor.ui.add( name, CKEDITOR.UI_PANELBUTTON,\r
26                                 {\r
27                                         label : title,\r
28                                         title : title,\r
29                                         className : 'cke_button_' + name.toLowerCase(),\r
30                                         modes : { wysiwyg : 1 },\r
31 \r
32                                         panel :\r
33                                         {\r
34                                                 css : editor.skin.editor.css,\r
35                                                 attributes : { role : 'listbox', 'aria-label' : lang.panelTitle }\r
36                                         },\r
37 \r
38                                         onBlock : function( panel, block )\r
39                                         {\r
40                                                 block.autoSize = true;\r
41                                                 block.element.addClass( 'cke_colorblock' );\r
42                                                 block.element.setHtml( renderColors( panel, type ) );\r
43 \r
44                                                 var keys = block.keys;\r
45                                                 keys[ 39 ]      = 'next';                                       // ARROW-RIGHT\r
46                                                 keys[ 40 ]      = 'next';                                       // ARROW-DOWN\r
47                                                 keys[ 9 ]       = 'next';                                       // TAB\r
48                                                 keys[ 37 ]      = 'prev';                                       // ARROW-LEFT\r
49                                                 keys[ 38 ]      = 'prev';                                       // ARROW-UP\r
50                                                 keys[ CKEDITOR.SHIFT + 9 ]      = 'prev';       // SHIFT + TAB\r
51                                                 keys[ 32 ]      = 'click';                                      // SPACE\r
52                                         }\r
53                                 });\r
54                 }\r
55 \r
56 \r
57                 function renderColors( panel, type )\r
58                 {\r
59                         var output = [],\r
60                                 colors = config.colorButton_colors.split( ',' ),\r
61                                 total = colors.length + ( config.colorButton_enableMore ? 2 : 1 );\r
62 \r
63                         var clickFn = CKEDITOR.tools.addFunction( function( color, type )\r
64                                 {\r
65                                         if ( color == '?' )\r
66                                         {\r
67                                                 var applyColorStyle = arguments.callee;\r
68                                                 function onColorDialogClose( evt )\r
69                                                 {\r
70                                                         this.removeListener( 'ok', onColorDialogClose );\r
71                                                         this.removeListener( 'cancel', onColorDialogClose );\r
72 \r
73                                                         evt.name == 'ok' && applyColorStyle( this.getContentElement( 'picker', 'selectedColor' ).getValue(), type );\r
74                                                 }\r
75 \r
76                                                 editor.openDialog( 'colordialog', function()\r
77                                                 {\r
78                                                         this.on( 'ok', onColorDialogClose );\r
79                                                         this.on( 'cancel', onColorDialogClose );\r
80                                                 } );\r
81 \r
82                                                 return;\r
83                                         }\r
84 \r
85                                         editor.focus();\r
86 \r
87                                         panel.hide();\r
88 \r
89 \r
90                                         editor.fire( 'saveSnapshot' );\r
91 \r
92                                         // Clean up any conflicting style within the range.\r
93                                         new CKEDITOR.style( config['colorButton_' + type + 'Style'], { color : 'inherit' } ).remove( editor.document );\r
94 \r
95                                         if ( color )\r
96                                         {\r
97                                                 var colorStyle = config['colorButton_' + type + 'Style'];\r
98 \r
99                                                 colorStyle.childRule = type == 'back' ?\r
100                                                         // It's better to apply background color as the innermost style. (#3599)\r
101                                                         function(){ return false; } :\r
102                                                         // Fore color style must be applied inside links instead of around it.\r
103                                                         function( element ){ return element.getName() != 'a'; };\r
104 \r
105                                                 new CKEDITOR.style( colorStyle, { color : color } ).apply( editor.document );\r
106                                         }\r
107 \r
108                                         editor.fire( 'saveSnapshot' );\r
109                                 });\r
110 \r
111                         // Render the "Automatic" button.\r
112                         output.push(\r
113                                 '<a class="cke_colorauto" _cke_focus=1 hidefocus=true' +\r
114                                         ' title="', lang.auto, '"' +\r
115                                         ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',null,\'', type, '\');return false;"' +\r
116                                         ' href="javascript:void(\'', lang.auto, '\')"' +\r
117                                         ' role="option" aria-posinset="1" aria-setsize="', total, '">' +\r
118                                         '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' +\r
119                                                 '<tr>' +\r
120                                                         '<td>' +\r
121                                                                 '<span class="cke_colorbox" style="background-color:#000"></span>' +\r
122                                                         '</td>' +\r
123                                                         '<td colspan=7 align=center>',\r
124                                                                 lang.auto,\r
125                                                         '</td>' +\r
126                                                 '</tr>' +\r
127                                         '</table>' +\r
128                                 '</a>' +\r
129                                 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );\r
130 \r
131                         // Render the color boxes.\r
132                         for ( var i = 0 ; i < colors.length ; i++ )\r
133                         {\r
134                                 if ( ( i % 8 ) === 0 )\r
135                                         output.push( '</tr><tr>' );\r
136 \r
137                                 var parts = colors[ i ].split( '/' ),\r
138                                         colorName = parts[ 0 ],\r
139                                         colorCode = parts[ 1 ] || colorName;\r
140 \r
141                                 // The data can be only a color code (without #) or colorName + color code\r
142                                 // If only a color code is provided, then the colorName is the color with the hash\r
143                                 if (!parts[1])\r
144                                         colorName = '#' + colorName;\r
145 \r
146                                 var colorLabel = editor.lang.colors[ colorCode ] || colorCode;\r
147                                 output.push(\r
148                                         '<td>' +\r
149                                                 '<a class="cke_colorbox" _cke_focus=1 hidefocus=true' +\r
150                                                         ' title="', colorLabel, '"' +\r
151                                                         ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'', colorName, '\',\'', type, '\'); return false;"' +\r
152                                                         ' href="javascript:void(\'', colorLabel, '\')"' +\r
153                                                         ' role="option" aria-posinset="', ( i + 2 ), '" aria-setsize="', total, '">' +\r
154                                                         '<span class="cke_colorbox" style="background-color:#', colorCode, '"></span>' +\r
155                                                 '</a>' +\r
156                                         '</td>' );\r
157                         }\r
158 \r
159                         // Render the "More Colors" button.\r
160                         if ( config.colorButton_enableMore )\r
161                         {\r
162                                 output.push(\r
163                                         '</tr>' +\r
164                                         '<tr>' +\r
165                                                 '<td colspan=8 align=center>' +\r
166                                                         '<a class="cke_colormore" _cke_focus=1 hidefocus=true' +\r
167                                                                 ' title="', lang.more, '"' +\r
168                                                                 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'?\',\'', type, '\');return false;"' +\r
169                                                                 ' href="javascript:void(\'', lang.more, '\')"',\r
170                                                                 ' role="option" aria-posinset="', total, '" aria-setsize="', total, '">',\r
171                                                                 lang.more,\r
172                                                         '</a>' +\r
173                                                 '</td>' );      // It is later in the code.\r
174                         }\r
175 \r
176                         output.push( '</tr></table>' );\r
177 \r
178                         return output.join( '' );\r
179                 }\r
180         }\r
181 });\r
182 \r
183 /**\r
184  * Whether to enable the "More Colors..." button in the color selectors.\r
185  * @default false\r
186  * @type Boolean\r
187  * @example\r
188  * config.colorButton_enableMore = false;\r
189  */\r
190 CKEDITOR.config.colorButton_enableMore = true;\r
191 \r
192 /**\r
193  * Defines the colors to be displayed in the color selectors. It's a string\r
194  * containing the hexadecimal notation for HTML colors, without the "#" prefix.\r
195  *\r
196  * Since 3.3: A name may be optionally defined by prefixing the entries with the\r
197  * name and the slash character. For example, "FontColor1/FF9900" will be\r
198  * displayed as the color #FF9900 in the selector, but will be outputted as "FontColor1".\r
199  * @type String\r
200  * @default '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF'\r
201  * @example\r
202  * // Brazil colors only.\r
203  * config.colorButton_colors = '00923E,F8C100,28166F';\r
204  * @example\r
205  * config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00'\r
206  */\r
207 CKEDITOR.config.colorButton_colors =\r
208         '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,' +\r
209         'B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,' +\r
210         'F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,' +\r
211         'FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,' +\r
212         'FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF';\r
213 \r
214 /**\r
215  * Holds the style definition to be used to apply the text foreground color.\r
216  * @type Object\r
217  * @example\r
218  * // This is basically the default setting value.\r
219  * config.colorButton_foreStyle =\r
220  *     {\r
221  *         element : 'span',\r
222  *         styles : { 'color' : '#(color)' }\r
223  *     };\r
224  */\r
225 CKEDITOR.config.colorButton_foreStyle =\r
226         {\r
227                 element         : 'span',\r
228                 styles          : { 'color' : '#(color)' },\r
229                 overrides       : [ { element : 'font', attributes : { 'color' : null } } ]\r
230         };\r
231 \r
232 /**\r
233  * Holds the style definition to be used to apply the text background color.\r
234  * @type Object\r
235  * @example\r
236  * // This is basically the default setting value.\r
237  * config.colorButton_backStyle =\r
238  *     {\r
239  *         element : 'span',\r
240  *         styles : { 'background-color' : '#(color)' }\r
241  *     };\r
242  */\r
243 CKEDITOR.config.colorButton_backStyle =\r
244         {\r
245                 element         : 'span',\r
246                 styles          : { 'background-color' : '#(color)' }\r
247         };\r