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