JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[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();\r
119 \r
120 \r
121                                         editor.fire( 'saveSnapshot' );\r
122 \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
125 \r
126                                         if ( color )\r
127                                         {\r
128                                                 var colorStyle = config['colorButton_' + type + 'Style'];\r
129 \r
130                                                 colorStyle.childRule = type == 'back' ?\r
131                                                         function( element )\r
132                                                         {\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
136                                                         }\r
137                                                         :\r
138                                                         function( element )\r
139                                                         {\r
140                                                                 // Fore color style must be applied inside links instead of around it.\r
141                                                                 return element.getName() != 'a' || isUnstylable( element );\r
142                                                         };\r
143 \r
144                                                 new CKEDITOR.style( colorStyle, { color : color } ).apply( editor.document );\r
145                                         }\r
146 \r
147                                         editor.fire( 'saveSnapshot' );\r
148                                 });\r
149 \r
150                         // Render the "Automatic" button.\r
151                         output.push(\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
158                                                 '<tr>' +\r
159                                                         '<td>' +\r
160                                                                 '<span class="cke_colorbox" id="', colorBoxId, '"></span>' +\r
161                                                         '</td>' +\r
162                                                         '<td colspan=7 align=center>',\r
163                                                                 lang.auto,\r
164                                                         '</td>' +\r
165                                                 '</tr>' +\r
166                                         '</table>' +\r
167                                 '</a>' +\r
168                                 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );\r
169 \r
170                         // Render the color boxes.\r
171                         for ( var i = 0 ; i < colors.length ; i++ )\r
172                         {\r
173                                 if ( ( i % 8 ) === 0 )\r
174                                         output.push( '</tr><tr>' );\r
175 \r
176                                 var parts = colors[ i ].split( '/' ),\r
177                                         colorName = parts[ 0 ],\r
178                                         colorCode = parts[ 1 ] || colorName;\r
179 \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
183                                 if (!parts[1])\r
184                                         colorName = '#' + colorName.replace( /^(.)(.)(.)$/, '$1$1$2$2$3$3' );\r
185 \r
186                                 var colorLabel = editor.lang.colors[ colorCode ] || colorCode;\r
187                                 output.push(\r
188                                         '<td>' +\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
195                                                 '</a>' +\r
196                                         '</td>' );\r
197                         }\r
198 \r
199                         // Render the "More Colors" button.\r
200                         if ( config.colorButton_enableMore === undefined || config.colorButton_enableMore )\r
201                         {\r
202                                 output.push(\r
203                                         '</tr>' +\r
204                                         '<tr>' +\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
211                                                                 lang.more,\r
212                                                         '</a>' +\r
213                                                 '</td>' );      // tr is later in the code.\r
214                         }\r
215 \r
216                         output.push( '</tr></table>' );\r
217 \r
218                         return output.join( '' );\r
219                 }\r
220 \r
221                 function isUnstylable( ele )\r
222                 {\r
223                         return ( ele.getAttribute( 'contentEditable' ) == 'false' ) || ele.getAttribute( 'data-nostyle' );\r
224                 }\r
225         }\r
226 });\r
227 \r
228 /**\r
229  * Whether to enable the "More Colors..." button in the color selectors.\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  * @type String\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
245  * @example\r
246  * // Brazil colors only.\r
247  * config.colorButton_colors = '00923E,F8C100,28166F';\r
248  * @example\r
249  * config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00'\r
250  */\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
257 \r
258 /**\r
259  * Holds the style definition to be used to apply the text foreground color.\r
260  * @type Object\r
261  * @example\r
262  * // This is basically the default setting value.\r
263  * config.colorButton_foreStyle =\r
264  *     {\r
265  *         element : 'span',\r
266  *         styles : { 'color' : '#(color)' }\r
267  *     };\r
268  */\r
269 CKEDITOR.config.colorButton_foreStyle =\r
270         {\r
271                 element         : 'span',\r
272                 styles          : { 'color' : '#(color)' },\r
273                 overrides       : [ { element : 'font', attributes : { 'color' : null } } ]\r
274         };\r
275 \r
276 /**\r
277  * Holds the style definition to be used to apply the text background color.\r
278  * @type Object\r
279  * @example\r
280  * // This is basically the default setting value.\r
281  * config.colorButton_backStyle =\r
282  *     {\r
283  *         element : 'span',\r
284  *         styles : { 'background-color' : '#(color)' }\r
285  *     };\r
286  */\r
287 CKEDITOR.config.colorButton_backStyle =\r
288         {\r
289                 element         : 'span',\r
290                 styles          : { 'background-color' : '#(color)' }\r
291         };\r