JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
747716e753ef11b762419808e288a29e9471e4bb
[ckeditor.git] / _source / plugins / colorbutton / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, 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                         editor.ui.add( name, CKEDITOR.UI_PANELBUTTON,\r
26                                 {\r
27                                         label : title,\r
28                                         title : title,\r
29                                         className : 'cke_button_' + name.toLowerCase(),\r
30                                         modes : { wysiwyg : 1 },\r
31 \r
32                                         panel :\r
33                                         {\r
34                                                 css : [ CKEDITOR.getUrl( editor.skinPath + 'editor.css' ) ]\r
35                                         },\r
36 \r
37                                         onBlock : function( panel, blockName )\r
38                                         {\r
39                                                 var block = panel.addBlock( blockName );\r
40                                                 block.autoSize = true;\r
41                                                 block.element.addClass( 'cke_colorblock' );\r
42                                                 block.element.setHtml( renderColors( panel, type ) );\r
43 \r
44                                                 var keys = block.keys;\r
45                                                 keys[ 39 ]      = 'next';                                       // ARROW-RIGHT\r
46                                                 keys[ 9 ]       = 'next';                                       // TAB\r
47                                                 keys[ 37 ]      = 'prev';                                       // ARROW-LEFT\r
48                                                 keys[ CKEDITOR.SHIFT + 9 ]      = 'prev';       // SHIFT + TAB\r
49                                                 keys[ 32 ]      = 'click';                                      // SPACE\r
50                                         }\r
51                                 });\r
52                 }\r
53 \r
54 \r
55                 function renderColors( panel, type )\r
56                 {\r
57                         var output = [],\r
58                                 colors = config.colorButton_colors.split( ',' );\r
59 \r
60                         var clickFn = CKEDITOR.tools.addFunction( function( color, type )\r
61                                 {\r
62                                         if ( color == '?' )\r
63                                         {\r
64                                                 // TODO : Implement the colors dialog.\r
65                                                 // editor.openDialog( '' );\r
66                                                 return;\r
67                                         }\r
68 \r
69                                         editor.focus();\r
70 \r
71                                         panel.hide();\r
72 \r
73                                         var style = new CKEDITOR.style( config['colorButton_' + type + 'Style'], color && { color : color } );\r
74 \r
75                                         editor.fire( 'saveSnapshot' );\r
76                                         if ( color )\r
77                                                 style.apply( editor.document );\r
78                                         else\r
79                                                 style.remove( editor.document );\r
80                                         editor.fire( 'saveSnapshot' );\r
81                                 });\r
82 \r
83                         // Render the "Automatic" button.\r
84                         output.push(\r
85                                 '<a class="cke_colorauto" _cke_focus=1 hidefocus=true' +\r
86                                         ' title="', lang.auto, '"' +\r
87                                         ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',null,\'', type, '\');return false;"' +\r
88                                         ' href="javascript:void(\'', lang.auto, '\')">' +\r
89                                         '<table cellspacing=0 cellpadding=0 width="100%">' +\r
90                                                 '<tr>' +\r
91                                                         '<td>' +\r
92                                                                 '<span class="cke_colorbox" style="background-color:#000"></span>' +\r
93                                                         '</td>' +\r
94                                                         '<td colspan=7 align=center>',\r
95                                                                 lang.auto,\r
96                                                         '</td>' +\r
97                                                 '</tr>' +\r
98                                         '</table>' +\r
99                                 '</a>' +\r
100                                 '<table cellspacing=0 cellpadding=0 width="100%">' );\r
101 \r
102                         // Render the color boxes.\r
103                         for ( var i = 0 ; i < colors.length ; i++ )\r
104                         {\r
105                                 if ( ( i % 8 ) === 0 )\r
106                                         output.push( '</tr><tr>' );\r
107 \r
108                                 var colorCode = colors[ i ];\r
109                                 var colorLabel = editor.lang.colors[ colorCode ] || colorCode;\r
110                                 output.push(\r
111                                         '<td>' +\r
112                                                 '<a class="cke_colorbox" _cke_focus=1 hidefocus=true' +\r
113                                                         ' title="', colorLabel, '"' +\r
114                                                         ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'#', colorCode, '\',\'', type, '\'); return false;"' +\r
115                                                         ' href="javascript:void(\'', colorLabel, '\')">' +\r
116                                                         '<span class="cke_colorbox" style="background-color:#', colorCode, '"></span>' +\r
117                                                 '</a>' +\r
118                                         '</td>' );\r
119                         }\r
120 \r
121                         // Render the "More Colors" button.\r
122                         if ( config.colorButton_enableMore )\r
123                         {\r
124                                 output.push(\r
125                                         '</tr>' +\r
126                                         '<tr>' +\r
127                                                 '<td colspan=8 align=center>' +\r
128                                                         '<a class="cke_colormore" _cke_focus=1 hidefocus=true' +\r
129                                                                 ' title="', lang.more, '"' +\r
130                                                                 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'?\',\'', type, '\');return false;"' +\r
131                                                                 ' href="javascript:void(\'', lang.more, '\')">',\r
132                                                                 lang.more,\r
133                                                         '</a>' +\r
134                                                 '</td>' );      // It is later in the code.\r
135                         }\r
136 \r
137                         output.push( '</tr></table>' );\r
138 \r
139                         return output.join( '' );\r
140                 }\r
141         }\r
142 });\r
143 \r
144 /**\r
145  * Whether to enable the "More Colors..." button in the color selectors.\r
146  * @default false\r
147  * @type Boolean\r
148  * @example\r
149  * config.colorButton_enableMore = false;\r
150  */\r
151 CKEDITOR.config.colorButton_enableMore = false;\r
152 \r
153 /**\r
154  * Defines the colors to be displayed in the color selectors. It's a string\r
155  * containing the hexadecimal notation for HTML colors, without the "#" prefix.\r
156  * @type String\r
157  * @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
158  * @example\r
159  * // Brazil colors only.\r
160  * config.colorButton_colors = '00923E,F8C100,28166F';\r
161  */\r
162 CKEDITOR.config.colorButton_colors =\r
163         '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,' +\r
164         'B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,' +\r
165         'F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,' +\r
166         'FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,' +\r
167         'FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF';\r
168 \r
169 /**\r
170  * Holds the style definition to be used to apply the text foreground color.\r
171  * @type Object\r
172  * @example\r
173  * // This is basically the default setting value.\r
174  * config.colorButton_foreStyle =\r
175  *     {\r
176  *         element : 'span',\r
177  *         styles : { 'color' : '#(color)' }\r
178  *     };\r
179  */\r
180 CKEDITOR.config.colorButton_foreStyle =\r
181         {\r
182                 element         : 'span',\r
183                 styles          : { 'color' : '#(color)' },\r
184                 overrides       : [ { element : 'font', attributes : { 'color' : null } } ]\r
185         };\r
186 \r
187 /**\r
188  * Holds the style definition to be used to apply the text background color.\r
189  * @type Object\r
190  * @example\r
191  * // This is basically the default setting value.\r
192  * config.colorButton_backStyle =\r
193  *     {\r
194  *         element : 'span',\r
195  *         styles : { 'background-color' : '#(color)' }\r
196  *     };\r
197  */\r
198 CKEDITOR.config.colorButton_backStyle =\r
199         {\r
200                 element         : 'span',\r
201                 styles          : { 'background-color' : '#(color)' }\r
202         };\r