X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fcolordialog%2Fdialogs%2Fcolordialog.js;h=e4a2a015fb8e3df87c93445b9a3e3dbddd4c71ed;hb=a272c66d841421f8bf933c16535bdcde1c4649fc;hp=e8e5d31784d4bbc5e102f8bf7532bbceab2f37a2;hpb=8665a7c6c60586526e32e8941fe2896739b6ebfb;p=ckeditor.git diff --git a/_source/plugins/colordialog/dialogs/colordialog.js b/_source/plugins/colordialog/dialogs/colordialog.js index e8e5d31..e4a2a01 100644 --- a/_source/plugins/colordialog/dialogs/colordialog.js +++ b/_source/plugins/colordialog/dialogs/colordialog.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -8,53 +8,229 @@ 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; - function spacer() + var spacer = { - return { - type : 'html', - html : ' ' - }; + type : 'html', + html : ' ' + }; + + var selected; + + function clearSelected() + { + $doc.getById( selHiColorId ).removeStyle( 'background-color' ); + dialog.getContentElement( 'picker', 'selectedColor' ).setValue( '' ); + selected && selected.removeAttribute( 'aria-selected' ); + selected = null; } - var table = new $el( 'table' ); - createColorTable(); + function updateSelected( evt ) + { + var target = evt.data.getTarget(), + color; + + if ( target.getName() == 'td' && + ( color = target.getChild( 0 ).getHtml() ) ) + { + selected = target; + selected.setAttribute( 'aria-selected', true ); + dialog.getContentElement( 'picker', 'selectedColor' ).setValue( color ); + } + } - var cellMouseover = function( event ) + // Basing black-white decision off of luma scheme using the Rec. 709 version + function whiteOrBlack( color ) { - var color = new $el( event.data.getTarget() ).getAttribute( 'title' ); - $doc.getById( hicolorId ).setStyle( 'background-color', color ); - $doc.getById( hicolorTextId ).setHtml( color ); - }; + color = color.replace( /^#/, '' ); + for ( var i = 0, rgb = []; i <= 2; i++ ) + rgb[i] = parseInt( color.substr( i * 2, 2 ), 16 ); + var luma = (0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]); + return '#' + ( luma >= 165 ? '000' : 'fff' ); + } - var cellClick = function( event ) + // Distinguish focused and hover states. + var focused, hovered; + + // Apply highlight style. + function updateHighlight( event ) { - var color = new $el( event.data.getTarget() ).getAttribute( 'title' ); - dialog.getContentElement( 'picker', 'selectedColor' ).setValue( color ); - }; + // Convert to event. + !event.name && ( event = new CKEDITOR.event( event ) ); + + var isFocus = !(/mouse/).test( event.name ), + target = event.data.getTarget(), + color; + + if ( target.getName() == 'td' && ( color = target.getChild( 0 ).getHtml() ) ) + { + removeHighlight( event ); + + isFocus ? focused = target : hovered = target; + + // Apply outline style to show focus. + if ( isFocus ) + { + target.setStyle( 'border-color', whiteOrBlack( color ) ); + target.setStyle( 'border-style', 'dotted' ); + } + + $doc.getById( hicolorId ).setStyle( 'background-color', color ); + $doc.getById( hicolorTextId ).setHtml( color ); + } + } + + function clearHighlight() + { + var color = focused.getChild( 0 ).getHtml(); + focused.setStyle( 'border-color', color ); + focused.setStyle( 'border-style', 'solid' ); + $doc.getById( hicolorId ).removeStyle( 'background-color' ); + $doc.getById( hicolorTextId ).setHtml( ' ' ); + focused = null; + } + + // Remove previously focused style. + function removeHighlight( event ) + { + var isFocus = !(/mouse/).test( event.name ), + target = isFocus && focused; + + if ( target ) + { + var color = target.getChild( 0 ).getHtml(); + target.setStyle( 'border-color', color ); + target.setStyle( 'border-style', 'solid' ); + } + + if ( ! ( focused || hovered ) ) + { + $doc.getById( hicolorId ).removeStyle( 'background-color' ); + $doc.getById( hicolorTextId ).setHtml( ' ' ); + } + } + + function onKeyStrokes( evt ) + { + var domEvt = evt.data; + + var element = domEvt.getTarget(); + var relative, nodeToMove; + var keystroke = domEvt.getKeystroke(), + rtl = editor.lang.dir == 'rtl'; + + switch ( keystroke ) + { + // UP-ARROW + case 38 : + // relative is TR + if ( ( relative = element.getParent().getPrevious() ) ) + { + nodeToMove = relative.getChild( [ element.getIndex() ] ); + nodeToMove.focus(); + } + domEvt.preventDefault(); + break; + // DOWN-ARROW + case 40 : + // relative is TR + if ( ( relative = element.getParent().getNext() ) ) + { + nodeToMove = relative.getChild( [ element.getIndex() ] ); + if ( nodeToMove && nodeToMove.type == 1 ) + { + nodeToMove.focus(); + } + } + domEvt.preventDefault(); + break; + + // SPACE + // ENTER + case 32 : + case 13 : + updateSelected( evt ); + domEvt.preventDefault(); + break; + + // RIGHT-ARROW + case rtl ? 37 : 39 : + // relative is TD + if ( ( nodeToMove = element.getNext() ) ) + { + if ( nodeToMove.type == 1 ) + { + nodeToMove.focus(); + domEvt.preventDefault( true ); + } + } + // relative is TR + else if ( ( relative = element.getParent().getNext() ) ) + { + nodeToMove = relative.getChild( [ 0 ] ); + if ( nodeToMove && nodeToMove.type == 1 ) + { + nodeToMove.focus(); + domEvt.preventDefault( true ); + } + } + break; + + // LEFT-ARROW + case rtl ? 39 : 37 : + // relative is TD + if ( ( nodeToMove = element.getPrevious() ) ) + { + nodeToMove.focus(); + domEvt.preventDefault( true ); + } + // relative is TR + else if ( ( relative = element.getParent().getPrevious() ) ) + { + nodeToMove = relative.getLast(); + nodeToMove.focus(); + domEvt.preventDefault( true ); + } + break; + default : + // Do not stop not handled events. + return; + } + } function createColorTable() { + table = CKEDITOR.dom.element.createFromHtml + ( + '' + + '' + + '
' + lang.options + '
' + ); + + table.on( 'mouseover', updateHighlight ); + table.on( 'mouseout', removeHighlight ); + // Create the base colors array. - var aColors = ['00','33','66','99','cc','ff']; + 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); + var row = new $el( table.$.insertRow( -1 ) ); + row.setAttribute( 'role', 'row' ); for ( var j = rangeB ; j < rangeB + 3 ; j++ ) { for ( var n = 0 ; n < 6 ; n++ ) { - appendColorCell( row, '#' + aColors[j] + aColors[n] + aColors[i] ); + appendColorCell( row.$, '#' + aColors[j] + aColors[n] + aColors[i] ); } } } @@ -65,13 +241,23 @@ CKEDITOR.dialog.add( 'colordialog', function( editor ) { var cell = new $el( targetRow.insertCell( -1 ) ); cell.setAttribute( 'class', 'ColorCell' ); + cell.setAttribute( 'tabIndex', -1 ); + cell.setAttribute( 'role', 'gridcell' ); + + cell.on( 'keydown', onKeyStrokes ); + cell.on( 'click', updateSelected ); + cell.on( 'focus', updateHighlight ); + cell.on( 'blur', removeHighlight ); + cell.setStyle( 'background-color', color ); + cell.setStyle( 'border', '1px solid ' + color ); - cell.setStyle( 'width', '15px' ); - cell.setStyle( 'height', '15px' ); + cell.setStyle( 'width', '14px' ); + cell.setStyle( 'height', '14px' ); - // Pass unparsed color value in some markup-degradable form. - cell.setAttribute( 'title', color ); + var colorLabel = numbering( 'color_table_cell' ); + cell.setAttribute( 'aria-labelledby',colorLabel ); + cell.append( CKEDITOR.dom.element.createFromHtml( '' + color + '', CKEDITOR.document ) ); } appendColorRow( 0, 0 ); @@ -80,37 +266,32 @@ CKEDITOR.dialog.add( 'colordialog', function( editor ) appendColorRow( 3, 3 ); // Create the last row. - var oRow = table.$.insertRow(-1) ; + var oRow = new $el( table.$.insertRow( -1 ) ) ; + oRow.setAttribute( 'role', 'row' ); // Create the gray scale colors cells. for ( var n = 0 ; n < 6 ; n++ ) { - appendColorCell( oRow, '#' + aColors[n] + aColors[n] + aColors[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' ) ; + appendColorCell( oRow.$, '#000000' ) ; } } - function clear() - { - $doc.getById( selHiColorId ).removeStyle( 'background-color' ); - dialog.getContentElement( 'picker', 'selectedColor' ).setValue( '' ); - } - - var clearActual = $tools.addFunction( function() - { - $doc.getById( hicolorId ).removeStyle( 'background-color' ); - $doc.getById( hicolorTextId ).setHtml( ' ' ); - } ); - - var numbering = function( id ){ return id + CKEDITOR.tools.getNextNumber(); }, + var numbering = function( id ) + { + return CKEDITOR.tools.getNextId() + '_' + id; + }, hicolorId = numbering( 'hicolor' ), hicolorTextId = numbering( 'hicolortext' ), - selHiColorId = numbering( 'selhicolor' ); + selHiColorId = numbering( 'selhicolor' ), + table; + + createColorTable(); return { title : lang.title, @@ -121,6 +302,11 @@ CKEDITOR.dialog.add( 'colordialog', function( editor ) // Update reference. dialog = this; }, + onHide : function() + { + clearSelected(); + clearHighlight(); + }, contents : [ { id : 'picker', @@ -136,15 +322,19 @@ CKEDITOR.dialog.add( 'colordialog', function( editor ) [ { type : 'html', - html : '' + table.getHtml() + '
', + html : '
', onLoad : function() { - var table = CKEDITOR.document.getById( this.domId ); - table.on( 'mouseover', cellMouseover ); - table.on( 'click', cellClick ); + CKEDITOR.document.getById( this.domId ).append( table ); + }, + focus : function() + { + // Restore the previously focused cell, + // otherwise put the initial focus on the first table cell. + ( focused || this.getElement().getElementsByTag( 'td' ).getItem( 0 ) ).focus(); } }, - spacer(), + spacer, { type : 'vbox', padding : 0, @@ -155,12 +345,13 @@ CKEDITOR.dialog.add( 'colordialog', function( editor ) type : 'html', html : '' + lang.highlight +'\
\ -
 
\ - ' + lang.selected +'\ +
 
' + lang.selected + '\
' }, { type : 'text', + label : lang.selected, + labelStyle: 'display:none', id : 'selectedColor', style : 'width: 74px', onChange : function() @@ -172,17 +363,17 @@ CKEDITOR.dialog.add( 'colordialog', function( editor ) } catch ( e ) { - clear(); + clearSelected(); } } }, - spacer(), + spacer, { type : 'button', id : 'clear', style : 'margin-top: 5px', label : lang.clear, - onClick : clear + onClick : clearSelected } ] }