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