2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'colorbutton',
\r
8 requires : [ 'panelbutton', 'floatpanel', 'styles' ],
\r
10 init : function( editor )
\r
12 var config = editor.config,
\r
13 lang = editor.lang.colorButton;
\r
17 if ( !CKEDITOR.env.hc )
\r
19 addButton( 'TextColor', 'fore', lang.textColorTitle );
\r
20 addButton( 'BGColor', 'back', lang.bgColorTitle );
\r
23 function addButton( name, type, title )
\r
25 var colorBoxId = CKEDITOR.tools.getNextId() + '_colorBox';
\r
26 editor.ui.add( name, CKEDITOR.UI_PANELBUTTON,
\r
30 className : 'cke_button_' + name.toLowerCase(),
\r
31 modes : { wysiwyg : 1 },
\r
35 css : editor.skin.editor.css,
\r
36 attributes : { role : 'listbox', 'aria-label' : lang.panelTitle }
\r
39 onBlock : function( panel, block )
\r
41 block.autoSize = true;
\r
42 block.element.addClass( 'cke_colorblock' );
\r
43 block.element.setHtml( renderColors( panel, type, colorBoxId ) );
\r
44 // The block should not have scrollbars (#5933, #6056)
\r
45 block.element.getDocument().getBody().setStyle( 'overflow', 'hidden' );
\r
47 CKEDITOR.ui.fire( 'ready', this );
\r
49 var keys = block.keys;
\r
50 var rtl = editor.lang.dir == 'rtl';
\r
51 keys[ rtl ? 37 : 39 ] = 'next'; // ARROW-RIGHT
\r
52 keys[ 40 ] = 'next'; // ARROW-DOWN
\r
53 keys[ 9 ] = 'next'; // TAB
\r
54 keys[ rtl ? 39 : 37 ] = 'prev'; // ARROW-LEFT
\r
55 keys[ 38 ] = 'prev'; // ARROW-UP
\r
56 keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
\r
57 keys[ 32 ] = 'click'; // SPACE
\r
60 // The automatic colorbox should represent the real color (#6010)
\r
63 var selection = editor.getSelection(),
\r
64 block = selection && selection.getStartElement(),
\r
65 path = new CKEDITOR.dom.elementPath( block ),
\r
68 // Find the closest block element.
\r
69 block = path.block || path.blockLimit || editor.document.getBody();
\r
71 // The background color might be transparent. In that case, look up the color in the DOM tree.
\r
74 color = block && block.getComputedStyle( type == 'back' ? 'background-color' : 'color' ) || 'transparent';
\r
76 while ( type == 'back' && color == 'transparent' && block && ( block = block.getParent() ) );
\r
78 // The box should never be transparent.
\r
79 if ( !color || color == 'transparent' )
\r
82 this._.panel._.iframe.getFrameDocument().getById( colorBoxId ).setStyle( 'background-color', color );
\r
88 function renderColors( panel, type, colorBoxId )
\r
91 colors = config.colorButton_colors.split( ',' ),
\r
92 total = colors.length + ( config.colorButton_enableMore ? 2 : 1 );
\r
94 var clickFn = CKEDITOR.tools.addFunction( function( color, type )
\r
98 var applyColorStyle = arguments.callee;
\r
99 function onColorDialogClose( evt )
\r
101 this.removeListener( 'ok', onColorDialogClose );
\r
102 this.removeListener( 'cancel', onColorDialogClose );
\r
104 evt.name == 'ok' && applyColorStyle( this.getContentElement( 'picker', 'selectedColor' ).getValue(), type );
\r
107 editor.openDialog( 'colordialog', function()
\r
109 this.on( 'ok', onColorDialogClose );
\r
110 this.on( 'cancel', onColorDialogClose );
\r
118 panel.hide( false );
\r
120 editor.fire( 'saveSnapshot' );
\r
122 // Clean up any conflicting style within the range.
\r
123 new CKEDITOR.style( config['colorButton_' + type + 'Style'], { color : 'inherit' } ).remove( editor.document );
\r
127 var colorStyle = config['colorButton_' + type + 'Style'];
\r
129 colorStyle.childRule = type == 'back' ?
\r
130 function( element )
\r
132 // It's better to apply background color as the innermost style. (#3599)
\r
133 // Except for "unstylable elements". (#6103)
\r
134 return isUnstylable( element );
\r
137 function( element )
\r
139 // Fore color style must be applied inside links instead of around it.
\r
140 return element.getName() != 'a' || isUnstylable( element );
\r
143 new CKEDITOR.style( colorStyle, { color : color } ).apply( editor.document );
\r
146 editor.fire( 'saveSnapshot' );
\r
149 // Render the "Automatic" button.
\r
151 '<a class="cke_colorauto" _cke_focus=1 hidefocus=true' +
\r
152 ' title="', lang.auto, '"' +
\r
153 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',null,\'', type, '\');return false;"' +
\r
154 ' href="javascript:void(\'', lang.auto, '\')"' +
\r
155 ' role="option" aria-posinset="1" aria-setsize="', total, '">' +
\r
156 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' +
\r
159 '<span class="cke_colorbox" id="', colorBoxId, '"></span>' +
\r
161 '<td colspan=7 align=center>',
\r
167 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );
\r
169 // Render the color boxes.
\r
170 for ( var i = 0 ; i < colors.length ; i++ )
\r
172 if ( ( i % 8 ) === 0 )
\r
173 output.push( '</tr><tr>' );
\r
175 var parts = colors[ i ].split( '/' ),
\r
176 colorName = parts[ 0 ],
\r
177 colorCode = parts[ 1 ] || colorName;
\r
179 // The data can be only a color code (without #) or colorName + color code
\r
180 // If only a color code is provided, then the colorName is the color with the hash
\r
181 // Convert the color from RGB to RRGGBB for better compatibility with IE and <font>. See #5676
\r
183 colorName = '#' + colorName.replace( /^(.)(.)(.)$/, '$1$1$2$2$3$3' );
\r
185 var colorLabel = editor.lang.colors[ colorCode ] || colorCode;
\r
188 '<a class="cke_colorbox" _cke_focus=1 hidefocus=true' +
\r
189 ' title="', colorLabel, '"' +
\r
190 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'', colorName, '\',\'', type, '\'); return false;"' +
\r
191 ' href="javascript:void(\'', colorLabel, '\')"' +
\r
192 ' role="option" aria-posinset="', ( i + 2 ), '" aria-setsize="', total, '">' +
\r
193 '<span class="cke_colorbox" style="background-color:#', colorCode, '"></span>' +
\r
198 // Render the "More Colors" button.
\r
199 if ( config.colorButton_enableMore === undefined || config.colorButton_enableMore )
\r
204 '<td colspan=8 align=center>' +
\r
205 '<a class="cke_colormore" _cke_focus=1 hidefocus=true' +
\r
206 ' title="', lang.more, '"' +
\r
207 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'?\',\'', type, '\');return false;"' +
\r
208 ' href="javascript:void(\'', lang.more, '\')"',
\r
209 ' role="option" aria-posinset="', total, '" aria-setsize="', total, '">',
\r
212 '</td>' ); // tr is later in the code.
\r
215 output.push( '</tr></table>' );
\r
217 return output.join( '' );
\r
220 function isUnstylable( ele )
\r
222 return ( ele.getAttribute( 'contentEditable' ) == 'false' ) || ele.getAttribute( 'data-nostyle' );
\r
228 * Whether to enable the "More Colors..." button in the color selectors.
\r
229 * @name CKEDITOR.config.colorButton_enableMore
\r
233 * config.colorButton_enableMore = false;
\r
237 * Defines the colors to be displayed in the color selectors. It's a string
\r
238 * containing the hexadecimal notation for HTML colors, without the "#" prefix.
\r
240 * Since 3.3: A name may be optionally defined by prefixing the entries with the
\r
241 * name and the slash character. For example, "FontColor1/FF9900" will be
\r
242 * displayed as the color #FF9900 in the selector, but will be outputted as "FontColor1".
\r
243 * @name CKEDITOR.config.colorButton_colors
\r
245 * @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
247 * // Brazil colors only.
\r
248 * config.colorButton_colors = '00923E,F8C100,28166F';
\r
250 * config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00'
\r
252 CKEDITOR.config.colorButton_colors =
\r
253 '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,' +
\r
254 'B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,' +
\r
255 'F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,' +
\r
256 'FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,' +
\r
257 'FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF';
\r
260 * Holds the style definition to be used to apply the text foreground color.
\r
261 * @name CKEDITOR.config.colorButton_foreStyle
\r
264 * // This is basically the default setting value.
\r
265 * config.colorButton_foreStyle =
\r
267 * element : 'span',
\r
268 * styles : { 'color' : '#(color)' }
\r
271 CKEDITOR.config.colorButton_foreStyle =
\r
274 styles : { 'color' : '#(color)' },
\r
275 overrides : [ { element : 'font', attributes : { 'color' : null } } ]
\r
279 * Holds the style definition to be used to apply the text background color.
\r
280 * @name CKEDITOR.config.colorButton_backStyle
\r
283 * // This is basically the default setting value.
\r
284 * config.colorButton_backStyle =
\r
286 * element : 'span',
\r
287 * styles : { 'background-color' : '#(color)' }
\r
290 CKEDITOR.config.colorButton_backStyle =
\r
293 styles : { 'background-color' : '#(color)' }
\r