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