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
121 editor.fire( 'saveSnapshot' );
\r
123 // Clean up any conflicting style within the range.
\r
124 new CKEDITOR.style( config['colorButton_' + type + 'Style'], { color : 'inherit' } ).remove( editor.document );
\r
128 var colorStyle = config['colorButton_' + type + 'Style'];
\r
130 colorStyle.childRule = type == 'back' ?
\r
131 function( element )
\r
133 // It's better to apply background color as the innermost style. (#3599)
\r
134 // Except for "unstylable elements". (#6103)
\r
135 return isUnstylable( element );
\r
138 function( element )
\r
140 // Fore color style must be applied inside links instead of around it.
\r
141 return element.getName() != 'a' || isUnstylable( element );
\r
144 new CKEDITOR.style( colorStyle, { color : color } ).apply( editor.document );
\r
147 editor.fire( 'saveSnapshot' );
\r
150 // Render the "Automatic" button.
\r
152 '<a class="cke_colorauto" _cke_focus=1 hidefocus=true' +
\r
153 ' title="', lang.auto, '"' +
\r
154 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',null,\'', type, '\');return false;"' +
\r
155 ' href="javascript:void(\'', lang.auto, '\')"' +
\r
156 ' role="option" aria-posinset="1" aria-setsize="', total, '">' +
\r
157 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' +
\r
160 '<span class="cke_colorbox" id="', colorBoxId, '"></span>' +
\r
162 '<td colspan=7 align=center>',
\r
168 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );
\r
170 // Render the color boxes.
\r
171 for ( var i = 0 ; i < colors.length ; i++ )
\r
173 if ( ( i % 8 ) === 0 )
\r
174 output.push( '</tr><tr>' );
\r
176 var parts = colors[ i ].split( '/' ),
\r
177 colorName = parts[ 0 ],
\r
178 colorCode = parts[ 1 ] || colorName;
\r
180 // The data can be only a color code (without #) or colorName + color code
\r
181 // If only a color code is provided, then the colorName is the color with the hash
\r
182 // Convert the color from RGB to RRGGBB for better compatibility with IE and <font>. See #5676
\r
184 colorName = '#' + colorName.replace( /^(.)(.)(.)$/, '$1$1$2$2$3$3' );
\r
186 var colorLabel = editor.lang.colors[ colorCode ] || colorCode;
\r
189 '<a class="cke_colorbox" _cke_focus=1 hidefocus=true' +
\r
190 ' title="', colorLabel, '"' +
\r
191 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'', colorName, '\',\'', type, '\'); return false;"' +
\r
192 ' href="javascript:void(\'', colorLabel, '\')"' +
\r
193 ' role="option" aria-posinset="', ( i + 2 ), '" aria-setsize="', total, '">' +
\r
194 '<span class="cke_colorbox" style="background-color:#', colorCode, '"></span>' +
\r
199 // Render the "More Colors" button.
\r
200 if ( config.colorButton_enableMore === undefined || config.colorButton_enableMore )
\r
205 '<td colspan=8 align=center>' +
\r
206 '<a class="cke_colormore" _cke_focus=1 hidefocus=true' +
\r
207 ' title="', lang.more, '"' +
\r
208 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'?\',\'', type, '\');return false;"' +
\r
209 ' href="javascript:void(\'', lang.more, '\')"',
\r
210 ' role="option" aria-posinset="', total, '" aria-setsize="', total, '">',
\r
213 '</td>' ); // tr is later in the code.
\r
216 output.push( '</tr></table>' );
\r
218 return output.join( '' );
\r
221 function isUnstylable( ele )
\r
223 return ( ele.getAttribute( 'contentEditable' ) == 'false' ) || ele.getAttribute( 'data-nostyle' );
\r
229 * Whether to enable the "More Colors..." button in the color selectors.
\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
244 * @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
246 * // Brazil colors only.
\r
247 * config.colorButton_colors = '00923E,F8C100,28166F';
\r
249 * config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00'
\r
251 CKEDITOR.config.colorButton_colors =
\r
252 '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,' +
\r
253 'B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,' +
\r
254 'F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,' +
\r
255 'FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,' +
\r
256 'FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF';
\r
259 * Holds the style definition to be used to apply the text foreground color.
\r
262 * // This is basically the default setting value.
\r
263 * config.colorButton_foreStyle =
\r
265 * element : 'span',
\r
266 * styles : { 'color' : '#(color)' }
\r
269 CKEDITOR.config.colorButton_foreStyle =
\r
272 styles : { 'color' : '#(color)' },
\r
273 overrides : [ { element : 'font', attributes : { 'color' : null } } ]
\r
277 * Holds the style definition to be used to apply the text background color.
\r
280 * // This is basically the default setting value.
\r
281 * config.colorButton_backStyle =
\r
283 * element : 'span',
\r
284 * styles : { 'background-color' : '#(color)' }
\r
287 CKEDITOR.config.colorButton_backStyle =
\r
290 styles : { 'background-color' : '#(color)' }
\r