JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
25e6ef444c167446d60869b82126a05672f1c10d
[ckeditor.git] / _source / plugins / colorbutton / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview The "colorbutton" plugin that makes it possible to assign\r
8  *               text and background colors to editor contents.\r
9  *\r
10  */\r
11 CKEDITOR.plugins.add( 'colorbutton',\r
12 {\r
13         requires : [ 'panelbutton', 'floatpanel', 'styles' ],\r
14 \r
15         init : function( editor )\r
16         {\r
17                 var config = editor.config,\r
18                         lang = editor.lang.colorButton;\r
19 \r
20                 var clickFn;\r
21 \r
22                 if ( !CKEDITOR.env.hc )\r
23                 {\r
24                         addButton( 'TextColor', 'fore', lang.textColorTitle );\r
25                         addButton( 'BGColor', 'back', lang.bgColorTitle );\r
26                 }\r
27 \r
28                 function addButton( name, type, title )\r
29                 {\r
30                         var colorBoxId = CKEDITOR.tools.getNextId() + '_colorBox';\r
31                         editor.ui.add( name, CKEDITOR.UI_PANELBUTTON,\r
32                                 {\r
33                                         label : title,\r
34                                         title : title,\r
35                                         className : 'cke_button_' + name.toLowerCase(),\r
36                                         modes : { wysiwyg : 1 },\r
37 \r
38                                         panel :\r
39                                         {\r
40                                                 css : editor.skin.editor.css,\r
41                                                 attributes : { role : 'listbox', 'aria-label' : lang.panelTitle }\r
42                                         },\r
43 \r
44                                         onBlock : function( panel, block )\r
45                                         {\r
46                                                 block.autoSize = true;\r
47                                                 block.element.addClass( 'cke_colorblock' );\r
48                                                 block.element.setHtml( renderColors( panel, type, colorBoxId ) );\r
49                                                 // The block should not have scrollbars (#5933, #6056)\r
50                                                 block.element.getDocument().getBody().setStyle( 'overflow', 'hidden' );\r
51 \r
52                                                 CKEDITOR.ui.fire( 'ready', this );\r
53 \r
54                                                 var keys = block.keys;\r
55                                                 var rtl = editor.lang.dir == 'rtl';\r
56                                                 keys[ rtl ? 37 : 39 ]   = 'next';                                       // ARROW-RIGHT\r
57                                                 keys[ 40 ]      = 'next';                                       // ARROW-DOWN\r
58                                                 keys[ 9 ]       = 'next';                                       // TAB\r
59                                                 keys[ rtl ? 39 : 37 ]   = 'prev';                                       // ARROW-LEFT\r
60                                                 keys[ 38 ]      = 'prev';                                       // ARROW-UP\r
61                                                 keys[ CKEDITOR.SHIFT + 9 ]      = 'prev';       // SHIFT + TAB\r
62                                                 keys[ 32 ]      = 'click';                                      // SPACE\r
63                                         },\r
64 \r
65                                         // The automatic colorbox should represent the real color (#6010)\r
66                                         onOpen : function()\r
67                                         {\r
68                                                 var selection = editor.getSelection(),\r
69                                                         block = selection && selection.getStartElement(),\r
70                                                         path = new CKEDITOR.dom.elementPath( block ),\r
71                                                         color;\r
72 \r
73                                                 // Find the closest block element.\r
74                                                 block = path.block || path.blockLimit || editor.document.getBody();\r
75 \r
76                                                 // The background color might be transparent. In that case, look up the color in the DOM tree.\r
77                                                 do\r
78                                                 {\r
79                                                         color = block && block.getComputedStyle( type == 'back' ? 'background-color' : 'color' ) || 'transparent';\r
80                                                 }\r
81                                                 while ( type == 'back' && color == 'transparent' && block && ( block = block.getParent() ) );\r
82 \r
83                                                 // The box should never be transparent.\r
84                                                 if ( !color || color == 'transparent' )\r
85                                                         color = '#ffffff';\r
86 \r
87                                                 this._.panel._.iframe.getFrameDocument().getById( colorBoxId ).setStyle( 'background-color', color );\r
88                                         }\r
89                                 });\r
90                 }\r
91 \r
92 \r
93                 function renderColors( panel, type, colorBoxId )\r
94                 {\r
95                         var output = [],\r
96                                 colors = config.colorButton_colors.split( ',' ),\r
97                                 total = colors.length + ( config.colorButton_enableMore ? 2 : 1 );\r
98 \r
99                         var clickFn = CKEDITOR.tools.addFunction( function( color, type )\r
100                                 {\r
101                                         if ( color == '?' )\r
102                                         {\r
103                                                 var applyColorStyle = arguments.callee;\r
104                                                 function onColorDialogClose( evt )\r
105                                                 {\r
106                                                         this.removeListener( 'ok', onColorDialogClose );\r
107                                                         this.removeListener( 'cancel', onColorDialogClose );\r
108 \r
109                                                         evt.name == 'ok' && applyColorStyle( this.getContentElement( 'picker', 'selectedColor' ).getValue(), type );\r
110                                                 }\r
111 \r
112                                                 editor.openDialog( 'colordialog', function()\r
113                                                 {\r
114                                                         this.on( 'ok', onColorDialogClose );\r
115                                                         this.on( 'cancel', onColorDialogClose );\r
116                                                 } );\r
117 \r
118                                                 return;\r
119                                         }\r
120 \r
121                                         editor.focus();\r
122 \r
123                                         panel.hide( false );\r
124 \r
125                                         editor.fire( 'saveSnapshot' );\r
126 \r
127                                         // Clean up any conflicting style within the range.\r
128                                         new CKEDITOR.style( config['colorButton_' + type + 'Style'], { color : 'inherit' } ).remove( editor.document );\r
129 \r
130                                         if ( color )\r
131                                         {\r
132                                                 var colorStyle = config['colorButton_' + type + 'Style'];\r
133 \r
134                                                 colorStyle.childRule = type == 'back' ?\r
135                                                         function( element )\r
136                                                         {\r
137                                                                 // It's better to apply background color as the innermost style. (#3599)\r
138                                                                 // Except for "unstylable elements". (#6103)\r
139                                                                 return isUnstylable( element );\r
140                                                         }\r
141                                                         :\r
142                                                         function( element )\r
143                                                         {\r
144                                                                 // Fore color style must be applied inside links instead of around it. (#4772,#6908)\r
145                                                                 return !( element.is( 'a' ) || element.getElementsByTag( 'a' ).count() ) || isUnstylable( element );\r
146                                                         };\r
147 \r
148                                                 new CKEDITOR.style( colorStyle, { color : color } ).apply( editor.document );\r
149                                         }\r
150 \r
151                                         editor.fire( 'saveSnapshot' );\r
152                                 });\r
153 \r
154                         // Render the "Automatic" button.\r
155                         output.push(\r
156                                 '<a class="cke_colorauto" _cke_focus=1 hidefocus=true' +\r
157                                         ' title="', lang.auto, '"' +\r
158                                         ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',null,\'', type, '\');return false;"' +\r
159                                         ' href="javascript:void(\'', lang.auto, '\')"' +\r
160                                         ' role="option" aria-posinset="1" aria-setsize="', total, '">' +\r
161                                         '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' +\r
162                                                 '<tr>' +\r
163                                                         '<td>' +\r
164                                                                 '<span class="cke_colorbox" id="', colorBoxId, '"></span>' +\r
165                                                         '</td>' +\r
166                                                         '<td colspan=7 align=center>',\r
167                                                                 lang.auto,\r
168                                                         '</td>' +\r
169                                                 '</tr>' +\r
170                                         '</table>' +\r
171                                 '</a>' +\r
172                                 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );\r
173 \r
174                         // Render the color boxes.\r
175                         for ( var i = 0 ; i < colors.length ; i++ )\r
176                         {\r
177                                 if ( ( i % 8 ) === 0 )\r
178                                         output.push( '</tr><tr>' );\r
179 \r
180                                 var parts = colors[ i ].split( '/' ),\r
181                                         colorName = parts[ 0 ],\r
182                                         colorCode = parts[ 1 ] || colorName;\r
183 \r
184                                 // The data can be only a color code (without #) or colorName + color code\r
185                                 // If only a color code is provided, then the colorName is the color with the hash\r
186                                 // Convert the color from RGB to RRGGBB for better compatibility with IE and <font>. See #5676\r
187                                 if (!parts[1])\r
188                                         colorName = '#' + colorName.replace( /^(.)(.)(.)$/, '$1$1$2$2$3$3' );\r
189 \r
190                                 var colorLabel = editor.lang.colors[ colorCode ] || colorCode;\r
191                                 output.push(\r
192                                         '<td>' +\r
193                                                 '<a class="cke_colorbox" _cke_focus=1 hidefocus=true' +\r
194                                                         ' title="', colorLabel, '"' +\r
195                                                         ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'', colorName, '\',\'', type, '\'); return false;"' +\r
196                                                         ' href="javascript:void(\'', colorLabel, '\')"' +\r
197                                                         ' role="option" aria-posinset="', ( i + 2 ), '" aria-setsize="', total, '">' +\r
198                                                         '<span class="cke_colorbox" style="background-color:#', colorCode, '"></span>' +\r
199                                                 '</a>' +\r
200                                         '</td>' );\r
201                         }\r
202 \r
203                         // Render the "More Colors" button.\r
204                         if ( config.colorButton_enableMore === undefined || config.colorButton_enableMore )\r
205                         {\r
206                                 output.push(\r
207                                         '</tr>' +\r
208                                         '<tr>' +\r
209                                                 '<td colspan=8 align=center>' +\r
210                                                         '<a class="cke_colormore" _cke_focus=1 hidefocus=true' +\r
211                                                                 ' title="', lang.more, '"' +\r
212                                                                 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'?\',\'', type, '\');return false;"' +\r
213                                                                 ' href="javascript:void(\'', lang.more, '\')"',\r
214                                                                 ' role="option" aria-posinset="', total, '" aria-setsize="', total, '">',\r
215                                                                 lang.more,\r
216                                                         '</a>' +\r
217                                                 '</td>' );      // tr is later in the code.\r
218                         }\r
219 \r
220                         output.push( '</tr></table>' );\r
221 \r
222                         return output.join( '' );\r
223                 }\r
224 \r
225                 function isUnstylable( ele )\r
226                 {\r
227                         return ( ele.getAttribute( 'contentEditable' ) == 'false' ) || ele.getAttribute( 'data-nostyle' );\r
228                 }\r
229         }\r
230 });\r
231 \r
232 /**\r
233  * Whether to enable the <strong>More Colors</strong> button in the color selectors.\r
234  * @name CKEDITOR.config.colorButton_enableMore\r
235  * @default <code>true</code>\r
236  * @type Boolean\r
237  * @example\r
238  * config.colorButton_enableMore = false;\r
239  */\r
240 \r
241 /**\r
242  * Defines the colors to be displayed in the color selectors. This is a string\r
243  * containing hexadecimal notation for HTML colors, without the "#" prefix.\r
244  * <br /><br />\r
245  * Since 3.3: A color name may optionally be defined by prefixing the entries with\r
246  * a name and the slash character. For example, "FontColor1/FF9900" will be\r
247  * displayed as the color #FF9900 in the selector, but will be output as "FontColor1".\r
248  * @name CKEDITOR.config.colorButton_colors\r
249  * @type String\r
250  * @default <code>'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'</code>\r
251  * @example\r
252  * // Brazil colors only.\r
253  * config.colorButton_colors = '00923E,F8C100,28166F';\r
254  * @example\r
255  * config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00'\r
256  */\r
257 CKEDITOR.config.colorButton_colors =\r
258         '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,' +\r
259         'B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,' +\r
260         'F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,' +\r
261         'FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,' +\r
262         'FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF';\r
263 \r
264 /**\r
265  * Stores the style definition that applies the text foreground color.\r
266  * @name CKEDITOR.config.colorButton_foreStyle\r
267  * @type Object\r
268  * @default (see example)\r
269  * @example\r
270  * // This is actually the default value.\r
271  * config.colorButton_foreStyle =\r
272  *     {\r
273  *         element : 'span',\r
274  *         styles : { 'color' : '#(color)' }\r
275  *     };\r
276  */\r
277 CKEDITOR.config.colorButton_foreStyle =\r
278         {\r
279                 element         : 'span',\r
280                 styles          : { 'color' : '#(color)' },\r
281                 overrides       : [ { element : 'font', attributes : { 'color' : null } } ]\r
282         };\r
283 \r
284 /**\r
285  * Stores the style definition that applies the text background color.\r
286  * @name CKEDITOR.config.colorButton_backStyle\r
287  * @type Object\r
288  * @default (see example)\r
289  * @example\r
290  * // This is actually the default value.\r
291  * config.colorButton_backStyle =\r
292  *     {\r
293  *         element : 'span',\r
294  *         styles : { 'background-color' : '#(color)' }\r
295  *     };\r
296  */\r
297 CKEDITOR.config.colorButton_backStyle =\r
298         {\r
299                 element         : 'span',\r
300                 styles          : { 'background-color' : '#(color)' }\r
301         };\r