2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.dialog.add( 'colordialog', function( editor )
\r
8 // Define some shorthands.
\r
9 var $el = CKEDITOR.dom.element,
\r
10 $doc = CKEDITOR.document,
\r
11 lang = editor.lang.colordialog;
\r
13 // Reference the dialog.
\r
24 function clearSelected()
\r
26 $doc.getById( selHiColorId ).removeStyle( 'background-color' );
\r
27 dialog.getContentElement( 'picker', 'selectedColor' ).setValue( '' );
\r
28 selected && selected.removeAttribute( 'aria-selected' );
\r
32 function updateSelected( evt )
\r
34 var target = evt.data.getTarget(),
\r
37 if ( target.getName() == 'td' &&
\r
38 ( color = target.getChild( 0 ).getHtml() ) )
\r
41 selected.setAttribute( 'aria-selected', true );
\r
42 dialog.getContentElement( 'picker', 'selectedColor' ).setValue( color );
\r
46 // Basing black-white decision off of luma scheme using the Rec. 709 version
\r
47 function whiteOrBlack( color )
\r
49 color = color.replace( /^#/, '' );
\r
50 for ( var i = 0, rgb = []; i <= 2; i++ )
\r
51 rgb[i] = parseInt( color.substr( i * 2, 2 ), 16 );
\r
52 var luma = (0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]);
\r
53 return '#' + ( luma >= 165 ? '000' : 'fff' );
\r
56 // Distinguish focused and hover states.
\r
57 var focused, hovered;
\r
59 // Apply highlight style.
\r
60 function updateHighlight( event )
\r
62 // Convert to event.
\r
63 !event.name && ( event = new CKEDITOR.event( event ) );
\r
65 var isFocus = !(/mouse/).test( event.name ),
\r
66 target = event.data.getTarget(),
\r
69 if ( target.getName() == 'td' && ( color = target.getChild( 0 ).getHtml() ) )
\r
71 removeHighlight( event );
\r
73 isFocus ? focused = target : hovered = target;
\r
75 // Apply outline style to show focus.
\r
78 target.setStyle( 'border-color', whiteOrBlack( color ) );
\r
79 target.setStyle( 'border-style', 'dotted' );
\r
82 $doc.getById( hicolorId ).setStyle( 'background-color', color );
\r
83 $doc.getById( hicolorTextId ).setHtml( color );
\r
87 function clearHighlight()
\r
89 var color = focused.getChild( 0 ).getHtml();
\r
90 focused.setStyle( 'border-color', color );
\r
91 focused.setStyle( 'border-style', 'solid' );
\r
92 $doc.getById( hicolorId ).removeStyle( 'background-color' );
\r
93 $doc.getById( hicolorTextId ).setHtml( ' ' );
\r
97 // Remove previously focused style.
\r
98 function removeHighlight( event )
\r
100 var isFocus = !(/mouse/).test( event.name ),
\r
101 target = isFocus && focused;
\r
105 var color = target.getChild( 0 ).getHtml();
\r
106 target.setStyle( 'border-color', color );
\r
107 target.setStyle( 'border-style', 'solid' );
\r
110 if ( ! ( focused || hovered ) )
\r
112 $doc.getById( hicolorId ).removeStyle( 'background-color' );
\r
113 $doc.getById( hicolorTextId ).setHtml( ' ' );
\r
117 function onKeyStrokes( evt )
\r
119 var domEvt = evt.data;
\r
121 var element = domEvt.getTarget();
\r
122 var relative, nodeToMove;
\r
123 var keystroke = domEvt.getKeystroke(),
\r
124 rtl = editor.lang.dir == 'rtl';
\r
126 switch ( keystroke )
\r
131 if ( ( relative = element.getParent().getPrevious() ) )
\r
133 nodeToMove = relative.getChild( [ element.getIndex() ] );
\r
134 nodeToMove.focus();
\r
136 domEvt.preventDefault();
\r
141 if ( ( relative = element.getParent().getNext() ) )
\r
143 nodeToMove = relative.getChild( [ element.getIndex() ] );
\r
144 if ( nodeToMove && nodeToMove.type == 1 )
\r
146 nodeToMove.focus();
\r
149 domEvt.preventDefault();
\r
156 updateSelected( evt );
\r
157 domEvt.preventDefault();
\r
161 case rtl ? 37 : 39 :
\r
163 if ( ( nodeToMove = element.getNext() ) )
\r
165 if ( nodeToMove.type == 1 )
\r
167 nodeToMove.focus();
\r
168 domEvt.preventDefault( true );
\r
172 else if ( ( relative = element.getParent().getNext() ) )
\r
174 nodeToMove = relative.getChild( [ 0 ] );
\r
175 if ( nodeToMove && nodeToMove.type == 1 )
\r
177 nodeToMove.focus();
\r
178 domEvt.preventDefault( true );
\r
184 case rtl ? 39 : 37 :
\r
186 if ( ( nodeToMove = element.getPrevious() ) )
\r
188 nodeToMove.focus();
\r
189 domEvt.preventDefault( true );
\r
192 else if ( ( relative = element.getParent().getPrevious() ) )
\r
194 nodeToMove = relative.getLast();
\r
195 nodeToMove.focus();
\r
196 domEvt.preventDefault( true );
\r
200 // Do not stop not handled events.
\r
205 function createColorTable()
\r
207 table = CKEDITOR.dom.element.createFromHtml
\r
209 '<table tabIndex="-1" aria-label="' + lang.options + '"' +
\r
210 ' role="grid" style="border-collapse:separate;" cellspacing="0">' +
\r
211 '<caption class="cke_voice_label">' + lang.options + '</caption>' +
\r
212 '<tbody role="presentation"></tbody></table>'
\r
215 table.on( 'mouseover', updateHighlight );
\r
216 table.on( 'mouseout', removeHighlight );
\r
218 // Create the base colors array.
\r
219 var aColors = [ '00', '33', '66', '99', 'cc', 'ff' ];
\r
221 // This function combines two ranges of three values from the color array into a row.
\r
222 function appendColorRow( rangeA, rangeB )
\r
224 for ( var i = rangeA ; i < rangeA + 3 ; i++ )
\r
226 var row = new $el( table.$.insertRow( -1 ) );
\r
227 row.setAttribute( 'role', 'row' );
\r
229 for ( var j = rangeB ; j < rangeB + 3 ; j++ )
\r
231 for ( var n = 0 ; n < 6 ; n++ )
\r
233 appendColorCell( row.$, '#' + aColors[j] + aColors[n] + aColors[i] );
\r
239 // This function create a single color cell in the color table.
\r
240 function appendColorCell( targetRow, color )
\r
242 var cell = new $el( targetRow.insertCell( -1 ) );
\r
243 cell.setAttribute( 'class', 'ColorCell' );
\r
244 cell.setAttribute( 'tabIndex', -1 );
\r
245 cell.setAttribute( 'role', 'gridcell' );
\r
247 cell.on( 'keydown', onKeyStrokes );
\r
248 cell.on( 'click', updateSelected );
\r
249 cell.on( 'focus', updateHighlight );
\r
250 cell.on( 'blur', removeHighlight );
\r
252 cell.setStyle( 'background-color', color );
\r
253 cell.setStyle( 'border', '1px solid ' + color );
\r
255 cell.setStyle( 'width', '14px' );
\r
256 cell.setStyle( 'height', '14px' );
\r
258 var colorLabel = numbering( 'color_table_cell' );
\r
259 cell.setAttribute( 'aria-labelledby',colorLabel );
\r
260 cell.append( CKEDITOR.dom.element.createFromHtml( '<span id="' + colorLabel + '" class="cke_voice_label">' + color + '</span>', CKEDITOR.document ) );
\r
263 appendColorRow( 0, 0 );
\r
264 appendColorRow( 3, 0 );
\r
265 appendColorRow( 0, 3 );
\r
266 appendColorRow( 3, 3 );
\r
268 // Create the last row.
\r
269 var oRow = new $el( table.$.insertRow( -1 ) ) ;
\r
270 oRow.setAttribute( 'role', 'row' );
\r
272 // Create the gray scale colors cells.
\r
273 for ( var n = 0 ; n < 6 ; n++ )
\r
275 appendColorCell( oRow.$, '#' + aColors[n] + aColors[n] + aColors[n] ) ;
\r
278 // Fill the row with black cells.
\r
279 for ( var i = 0 ; i < 12 ; i++ )
\r
281 appendColorCell( oRow.$, '#000000' ) ;
\r
285 var numbering = function( id )
\r
287 return CKEDITOR.tools.getNextId() + '_' + id;
\r
289 hicolorId = numbering( 'hicolor' ),
\r
290 hicolorTextId = numbering( 'hicolortext' ),
\r
291 selHiColorId = numbering( 'selhicolor' ),
\r
294 createColorTable();
\r
297 title : lang.title,
\r
300 onLoad : function()
\r
302 // Update reference.
\r
305 onHide : function()
\r
313 label : lang.title,
\r
320 widths : [ '70%', '10%', '30%' ],
\r
325 html : '<div></div>',
\r
326 onLoad : function()
\r
328 CKEDITOR.document.getById( this.domId ).append( table );
\r
332 // Restore the previously focused cell,
\r
333 // otherwise put the initial focus on the first table cell.
\r
334 ( focused || this.getElement().getElementsByTag( 'td' ).getItem( 0 ) ).focus();
\r
341 widths : [ '70%', '5%', '25%' ],
\r
346 html : '<span>' + lang.highlight +'</span>\
\r
347 <div id="' + hicolorId + '" style="border: 1px solid; height: 74px; width: 74px;"></div>\
\r
348 <div id="' + hicolorTextId + '"> </div><span>' + lang.selected + '</span>\
\r
349 <div id="' + selHiColorId + '" style="border: 1px solid; height: 20px; width: 74px;"></div>'
\r
353 label : lang.selected,
\r
354 labelStyle: 'display:none',
\r
355 id : 'selectedColor',
\r
356 style : 'width: 74px',
\r
357 onChange : function()
\r
359 // Try to update color preview with new value. If fails, then set it no none.
\r
362 $doc.getById( selHiColorId ).setStyle( 'background-color', this.getValue() );
\r
374 style : 'margin-top: 5px',
\r
375 label : lang.clear,
\r
376 onClick : clearSelected
\r