b3e5d9cc17143aa32f10c731beeecf6424ea1515
[ckeditor.git] / _source / plugins / colorbutton / plugin.js
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
5
6 CKEDITOR.plugins.add( 'colorbutton',
7 {
8 requires : [ 'panelbutton', 'floatpanel', 'styles' ],
9
10 init : function( editor )
11 {
12 var config = editor.config,
13 lang = editor.lang.colorButton;
14
15 var clickFn;
16
17 if ( !CKEDITOR.env.hc )
18 {
19 addButton( 'TextColor', 'fore', lang.textColorTitle );
20 addButton( 'BGColor', 'back', lang.bgColorTitle );
21 }
22
23 function addButton( name, type, title )
24 {
25 var colorBoxId = CKEDITOR.tools.getNextId() + '_colorBox';
26 editor.ui.add( name, CKEDITOR.UI_PANELBUTTON,
27 {
28 label : title,
29 title : title,
30 className : 'cke_button_' + name.toLowerCase(),
31 modes : { wysiwyg : 1 },
32
33 panel :
34 {
35 css : editor.skin.editor.css,
36 attributes : { role : 'listbox', 'aria-label' : lang.panelTitle }
37 },
38
39 onBlock : function( panel, block )
40 {
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' );
46
47 CKEDITOR.ui.fire( 'ready', this );
48
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
58 },
59
60 // The automatic colorbox should represent the real color (#6010)
61 onOpen : function()
62 {
63 var selection = editor.getSelection(),
64 block = selection && selection.getStartElement(),
65 path = new CKEDITOR.dom.elementPath( block ),
66 color;
67
68 // Find the closest block element.
69 block = path.block || path.blockLimit || editor.document.getBody();
70
71 // The background color might be transparent. In that case, look up the color in the DOM tree.
72 do
73 {
74 color = block && block.getComputedStyle( type == 'back' ? 'background-color' : 'color' ) || 'transparent';
75 }
76 while ( type == 'back' && color == 'transparent' && block && ( block = block.getParent() ) );
77
78 // The box should never be transparent.
79 if ( !color || color == 'transparent' )
80 color = '#ffffff';
81
82 this._.panel._.iframe.getFrameDocument().getById( colorBoxId ).setStyle( 'background-color', color );
83 }
84 });
85 }
86
87
88 function renderColors( panel, type, colorBoxId )
89 {
90 var output = [],
91 colors = config.colorButton_colors.split( ',' ),
92 total = colors.length + ( config.colorButton_enableMore ? 2 : 1 );
93
94 var clickFn = CKEDITOR.tools.addFunction( function( color, type )
95 {
96 if ( color == '?' )
97 {
98 var applyColorStyle = arguments.callee;
99 function onColorDialogClose( evt )
100 {
101 this.removeListener( 'ok', onColorDialogClose );
102 this.removeListener( 'cancel', onColorDialogClose );
103
104 evt.name == 'ok' && applyColorStyle( this.getContentElement( 'picker', 'selectedColor' ).getValue(), type );
105 }
106
107 editor.openDialog( 'colordialog', function()
108 {
109 this.on( 'ok', onColorDialogClose );
110 this.on( 'cancel', onColorDialogClose );
111 } );
112
113 return;
114 }
115
116 editor.focus();
117
118 panel.hide( false );
119
120 editor.fire( 'saveSnapshot' );
121
122 // Clean up any conflicting style within the range.
123 new CKEDITOR.style( config['colorButton_' + type + 'Style'], { color : 'inherit' } ).remove( editor.document );
124
125 if ( color )
126 {
127 var colorStyle = config['colorButton_' + type + 'Style'];
128
129 colorStyle.childRule = type == 'back' ?
130 function( element )
131 {
132 // It's better to apply background color as the innermost style. (#3599)
133 // Except for "unstylable elements". (#6103)
134 return isUnstylable( element );
135 }
136 :
137 function( element )
138 {
139 // Fore color style must be applied inside links instead of around it.
140 return element.getName() != 'a' || isUnstylable( element );
141 };
142
143 new CKEDITOR.style( colorStyle, { color : color } ).apply( editor.document );
144 }
145
146 editor.fire( 'saveSnapshot' );
147 });
148
149 // Render the "Automatic" button.
150 output.push(
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%">' +
157 '<tr>' +
158 '<td>' +
159 '<span class="cke_colorbox" id="', colorBoxId, '"></span>' +
160 '</td>' +
161 '<td colspan=7 align=center>',
162 lang.auto,
163 '</td>' +
164 '</tr>' +
165 '</table>' +
166 '</a>' +
167 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );
168
169 // Render the color boxes.
170 for ( var i = 0 ; i < colors.length ; i++ )
171 {
172 if ( ( i % 8 ) === 0 )
173 output.push( '</tr><tr>' );
174
175 var parts = colors[ i ].split( '/' ),
176 colorName = parts[ 0 ],
177 colorCode = parts[ 1 ] || colorName;
178
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
182 if (!parts[1])
183 colorName = '#' + colorName.replace( /^(.)(.)(.)$/, '$1$1$2$2$3$3' );
184
185 var colorLabel = editor.lang.colors[ colorCode ] || colorCode;
186 output.push(
187 '<td>' +
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>' +
194 '</a>' +
195 '</td>' );
196 }
197
198 // Render the "More Colors" button.
199 if ( config.colorButton_enableMore === undefined || config.colorButton_enableMore )
200 {
201 output.push(
202 '</tr>' +
203 '<tr>' +
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, '">',
210 lang.more,
211 '</a>' +
212 '</td>' ); // tr is later in the code.
213 }
214
215 output.push( '</tr></table>' );
216
217 return output.join( '' );
218 }
219
220 function isUnstylable( ele )
221 {
222 return ( ele.getAttribute( 'contentEditable' ) == 'false' ) || ele.getAttribute( 'data-nostyle' );
223 }
224 }
225 });
226
227 /**
228 * Whether to enable the "More Colors..." button in the color selectors.
229 * @name CKEDITOR.config.colorButton_enableMore
230 * @default true
231 * @type Boolean
232 * @example
233 * config.colorButton_enableMore = false;
234 */
235
236 /**
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.
239 *
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
244 * @type String
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'
246 * @example
247 * // Brazil colors only.
248 * config.colorButton_colors = '00923E,F8C100,28166F';
249 * @example
250 * config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00'
251 */
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';
258
259 /**
260 * Holds the style definition to be used to apply the text foreground color.
261 * @name CKEDITOR.config.colorButton_foreStyle
262 * @type Object
263 * @example
264 * // This is basically the default setting value.
265 * config.colorButton_foreStyle =
266 * {
267 * element : 'span',
268 * styles : { 'color' : '#(color)' }
269 * };
270 */
271 CKEDITOR.config.colorButton_foreStyle =
272 {
273 element : 'span',
274 styles : { 'color' : '#(color)' },
275 overrides : [ { element : 'font', attributes : { 'color' : null } } ]
276 };
277
278 /**
279 * Holds the style definition to be used to apply the text background color.
280 * @name CKEDITOR.config.colorButton_backStyle
281 * @type Object
282 * @example
283 * // This is basically the default setting value.
284 * config.colorButton_backStyle =
285 * {
286 * element : 'span',
287 * styles : { 'background-color' : '#(color)' }
288 * };
289 */
290 CKEDITOR.config.colorButton_backStyle =
291 {
292 element : 'span',
293 styles : { 'background-color' : '#(color)' }
294 };