/* Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ CKEDITOR.dialog.add( 'colordialog', function( editor ) { // Define some shorthands. var $el = CKEDITOR.dom.element, $doc = CKEDITOR.document, $tools = CKEDITOR.tools, lang = editor.lang.colordialog; // Reference the dialog. var dialog; var spacer = { type : 'html', html : ' ' }; function clearSelected() { $doc.getById( selHiColorId ).removeStyle( 'background-color' ); dialog.getContentElement( 'picker', 'selectedColor' ).setValue( '' ); } function updateSelected( evt ) { if ( ! ( evt instanceof CKEDITOR.dom.event ) ) evt = new CKEDITOR.dom.event( evt ); var target = evt.getTarget(), color; if ( target.getName() == 'a' && ( color = target.getChild( 0 ).getHtml() ) ) dialog.getContentElement( 'picker', 'selectedColor' ).setValue( color ); } function updateHighlight( event ) { if ( ! ( event instanceof CKEDITOR.dom.event ) ) event = event.data; var target = event.getTarget(), color; if ( target.getName() == 'a' && ( color = target.getChild( 0 ).getHtml() ) ) { $doc.getById( hicolorId ).setStyle( 'background-color', color ); $doc.getById( hicolorTextId ).setHtml( color ); } } function clearHighlight() { $doc.getById( hicolorId ).removeStyle( 'background-color' ); $doc.getById( hicolorTextId ).setHtml( ' ' ); } var onMouseout = $tools.addFunction( clearHighlight ), onClick = updateSelected, onClickHandler = CKEDITOR.tools.addFunction( onClick ), onFocus = updateHighlight, onBlur = clearHighlight; var onKeydownHandler = CKEDITOR.tools.addFunction( function( ev ) { ev = new CKEDITOR.dom.event( ev ); var element = ev.getTarget(); var relative, nodeToMove; var keystroke = ev.getKeystroke(), rtl = editor.lang.dir == 'rtl'; switch ( keystroke ) { // UP-ARROW case 38 : // relative is TR if ( ( relative = element.getParent().getParent().getPrevious() ) ) { nodeToMove = relative.getChild( [element.getParent().getIndex(), 0] ); nodeToMove.focus(); onBlur( ev, element ); onFocus( ev, nodeToMove ); } ev.preventDefault(); break; // DOWN-ARROW case 40 : // relative is TR if ( ( relative = element.getParent().getParent().getNext() ) ) { nodeToMove = relative.getChild( [ element.getParent().getIndex(), 0 ] ); if ( nodeToMove && nodeToMove.type == 1 ) { nodeToMove.focus(); onBlur( ev, element ); onFocus( ev, nodeToMove ); } } ev.preventDefault(); break; // SPACE // ENTER is already handled as onClick case 32 : onClick( ev ); ev.preventDefault(); break; // RIGHT-ARROW case rtl ? 37 : 39 : // relative is TD if ( ( relative = element.getParent().getNext() ) ) { nodeToMove = relative.getChild( 0 ); if ( nodeToMove.type == 1 ) { nodeToMove.focus(); onBlur( ev, element ); onFocus( ev, nodeToMove ); ev.preventDefault( true ); } else onBlur( null, element ); } // relative is TR else if ( ( relative = element.getParent().getParent().getNext() ) ) { nodeToMove = relative.getChild( [ 0, 0 ] ); if ( nodeToMove && nodeToMove.type == 1 ) { nodeToMove.focus(); onBlur( ev, element ); onFocus( ev, nodeToMove ); ev.preventDefault( true ); } else onBlur( null, element ); } break; // LEFT-ARROW case rtl ? 39 : 37 : // relative is TD if ( ( relative = element.getParent().getPrevious() ) ) { nodeToMove = relative.getChild( 0 ); nodeToMove.focus(); onBlur( ev, element ); onFocus( ev, nodeToMove ); ev.preventDefault( true ); } // relative is TR else if ( ( relative = element.getParent().getParent().getPrevious() ) ) { nodeToMove = relative.getLast().getChild( 0 ); nodeToMove.focus(); onBlur( ev, element ); onFocus( ev, nodeToMove ); ev.preventDefault( true ); } else onBlur( null, element ); break; default : // Do not stop not handled events. return; } }); function createColorTable() { // Create the base colors array. var aColors = [ '00', '33', '66', '99', 'cc', 'ff' ]; // This function combines two ranges of three values from the color array into a row. function appendColorRow( rangeA, rangeB ) { for ( var i = rangeA ; i < rangeA + 3 ; i++ ) { var row = table.$.insertRow( -1 ); for ( var j = rangeB ; j < rangeB + 3 ; j++ ) { for ( var n = 0 ; n < 6 ; n++ ) { appendColorCell( row, '#' + aColors[j] + aColors[n] + aColors[i] ); } } } } // This function create a single color cell in the color table. function appendColorCell( targetRow, color ) { var cell = new $el( targetRow.insertCell( -1 ) ); cell.setAttribute( 'class', 'ColorCell' ); cell.setStyle( 'background-color', color ); cell.setStyle( 'width', '15px' ); cell.setStyle( 'height', '15px' ); var index = cell.$.cellIndex + 1 + 18 * targetRow.rowIndex; cell.append( CKEDITOR.dom.element.createFromHtml( '' + color + ' ', CKEDITOR.document ) ); } appendColorRow( 0, 0 ); appendColorRow( 3, 0 ); appendColorRow( 0, 3 ); appendColorRow( 3, 3 ); // Create the last row. var oRow = table.$.insertRow(-1) ; // Create the gray scale colors cells. for ( var n = 0 ; n < 6 ; n++ ) { appendColorCell( oRow, '#' + aColors[n] + aColors[n] + aColors[n] ) ; } // Fill the row with black cells. for ( var i = 0 ; i < 12 ; i++ ) { appendColorCell( oRow, '#000000' ) ; } } var table = new $el( 'table' ); createColorTable(); var html = table.getHtml(); var numbering = function( id ) { return CKEDITOR.tools.getNextId() + '_' + id; }, hicolorId = numbering( 'hicolor' ), hicolorTextId = numbering( 'hicolortext' ), selHiColorId = numbering( 'selhicolor' ), tableLabelId = numbering( 'color_table_label' ); return { title : lang.title, minWidth : 360, minHeight : 220, onLoad : function() { // Update reference. dialog = this; }, contents : [ { id : 'picker', label : lang.title, accessKey : 'I', elements : [ { type : 'hbox', padding : 0, widths : [ '70%', '10%', '30%' ], children : [ { type : 'html', html : '