]>
git.immae.eu Git - perso/Immae/Projets/packagist/connexionswing-ckeditor-component.git/blob - sources/plugins/colordialog/dialogs/colordialog.js
2 * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
6 CKEDITOR
.dialog
.add( 'colordialog', function( editor
) {
7 // Define some shorthands.
8 var $el
= CKEDITOR
.dom
.element
,
9 $doc
= CKEDITOR
.document
,
10 lang
= editor
.lang
.colordialog
;
12 // Reference the dialog.
22 function clearSelected() {
23 $doc
.getById( selHiColorId
).removeStyle( 'background-color' );
24 dialog
.getContentElement( 'picker', 'selectedColor' ).setValue( '' );
25 selected
&& selected
.removeAttribute( 'aria-selected' );
29 function updateSelected( evt
) {
30 var target
= evt
.data
.getTarget(),
33 if ( target
.getName() == 'td' && ( color
= target
.getChild( 0 ).getHtml() ) ) {
35 selected
.setAttribute( 'aria-selected', true );
36 dialog
.getContentElement( 'picker', 'selectedColor' ).setValue( color
);
40 // Basing black-white decision off of luma scheme using the Rec. 709 version
41 function whiteOrBlack( color
) {
42 color
= color
.replace( /^#/, '' );
43 for ( var i
= 0, rgb
= []; i
<= 2; i
++ )
44 rgb
[ i
] = parseInt( color
.substr( i
* 2, 2 ), 16 );
45 var luma
= ( 0.2126 * rgb
[ 0 ] ) + ( 0.7152 * rgb
[ 1 ] ) + ( 0.0722 * rgb
[ 2 ] );
46 return '#' + ( luma
>= 165 ? '000' : 'fff' );
49 // Distinguish focused and hover states.
52 // Apply highlight style.
53 function updateHighlight( event
) {
55 !event
.name
&& ( event
= new CKEDITOR
.event( event
) );
57 var isFocus
= !( /mouse/ ).test( event
.name
),
58 target
= event
.data
.getTarget(),
61 if ( target
.getName() == 'td' && ( color
= target
.getChild( 0 ).getHtml() ) ) {
62 removeHighlight( event
);
64 isFocus
? focused
= target : hovered
= target
;
66 // Apply outline style to show focus.
68 target
.setStyle( 'border-color', whiteOrBlack( color
) );
69 target
.setStyle( 'border-style', 'dotted' );
72 $doc
.getById( hicolorId
).setStyle( 'background-color', color
);
73 $doc
.getById( hicolorTextId
).setHtml( color
);
77 function clearHighlight() {
78 var color
= focused
.getChild( 0 ).getHtml();
79 focused
.setStyle( 'border-color', color
);
80 focused
.setStyle( 'border-style', 'solid' );
81 $doc
.getById( hicolorId
).removeStyle( 'background-color' );
82 $doc
.getById( hicolorTextId
).setHtml( ' ' );
86 // Remove previously focused style.
87 function removeHighlight( event
) {
88 var isFocus
= !( /mouse/ ).test( event
.name
),
89 target
= isFocus
&& focused
;
92 var color
= target
.getChild( 0 ).getHtml();
93 target
.setStyle( 'border-color', color
);
94 target
.setStyle( 'border-style', 'solid' );
97 if ( !( focused
|| hovered
) ) {
98 $doc
.getById( hicolorId
).removeStyle( 'background-color' );
99 $doc
.getById( hicolorTextId
).setHtml( ' ' );
103 function onKeyStrokes( evt
) {
104 var domEvt
= evt
.data
;
106 var element
= domEvt
.getTarget();
107 var relative
, nodeToMove
;
108 var keystroke
= domEvt
.getKeystroke(),
109 rtl
= editor
.lang
.dir
== 'rtl';
111 switch ( keystroke
) {
115 if ( ( relative
= element
.getParent().getPrevious() ) ) {
116 nodeToMove
= relative
.getChild( [ element
.getIndex() ] );
119 domEvt
.preventDefault();
124 if ( ( relative
= element
.getParent().getNext() ) ) {
125 nodeToMove
= relative
.getChild( [ element
.getIndex() ] );
126 if ( nodeToMove
&& nodeToMove
.type
== 1 )
130 domEvt
.preventDefault();
137 updateSelected( evt
);
138 domEvt
.preventDefault();
144 if ( ( nodeToMove
= element
.getNext() ) ) {
145 if ( nodeToMove
.type
== 1 ) {
147 domEvt
.preventDefault( true );
151 else if ( ( relative
= element
.getParent().getNext() ) ) {
152 nodeToMove
= relative
.getChild( [ 0 ] );
153 if ( nodeToMove
&& nodeToMove
.type
== 1 ) {
155 domEvt
.preventDefault( true );
163 if ( ( nodeToMove
= element
.getPrevious() ) ) {
165 domEvt
.preventDefault( true );
168 else if ( ( relative
= element
.getParent().getPrevious() ) ) {
169 nodeToMove
= relative
.getLast();
171 domEvt
.preventDefault( true );
175 // Do not stop not handled events.
180 function createColorTable() {
181 table
= CKEDITOR
.dom
.element
.createFromHtml( '<table tabIndex="-1" aria-label="' + lang
.options
+ '"' +
182 ' role="grid" style="border-collapse:separate;" cellspacing="0">' +
183 '<caption class="cke_voice_label">' + lang
.options
+ '</caption>' +
184 '<tbody role="presentation"></tbody></table>' );
186 table
.on( 'mouseover', updateHighlight
);
187 table
.on( 'mouseout', removeHighlight
);
189 // Create the base colors array.
190 var aColors
= [ '00', '33', '66', '99', 'cc', 'ff' ];
192 // This function combines two ranges of three values from the color array into a row.
193 function appendColorRow( rangeA
, rangeB
) {
194 for ( var i
= rangeA
; i
< rangeA
+ 3; i
++ ) {
195 var row
= new $el( table
.$.insertRow( -1 ) );
196 row
.setAttribute( 'role', 'row' );
198 for ( var j
= rangeB
; j
< rangeB
+ 3; j
++ ) {
199 for ( var n
= 0; n
< 6; n
++ ) {
200 appendColorCell( row
.$, '#' + aColors
[ j
] + aColors
[ n
] + aColors
[ i
] );
206 // This function create a single color cell in the color table.
207 function appendColorCell( targetRow
, color
) {
208 var cell
= new $el( targetRow
.insertCell( -1 ) );
209 cell
.setAttribute( 'class', 'ColorCell' );
210 cell
.setAttribute( 'tabIndex', -1 );
211 cell
.setAttribute( 'role', 'gridcell' );
213 cell
.on( 'keydown', onKeyStrokes
);
214 cell
.on( 'click', updateSelected
);
215 cell
.on( 'focus', updateHighlight
);
216 cell
.on( 'blur', removeHighlight
);
218 cell
.setStyle( 'background-color', color
);
219 cell
.setStyle( 'border', '1px solid ' + color
);
221 cell
.setStyle( 'width', '14px' );
222 cell
.setStyle( 'height', '14px' );
224 var colorLabel
= numbering( 'color_table_cell' );
225 cell
.setAttribute( 'aria-labelledby', colorLabel
);
226 cell
.append( CKEDITOR
.dom
.element
.createFromHtml( '<span id="' + colorLabel
+ '" class="cke_voice_label">' + color
+ '</span>', CKEDITOR
.document
) );
229 appendColorRow( 0, 0 );
230 appendColorRow( 3, 0 );
231 appendColorRow( 0, 3 );
232 appendColorRow( 3, 3 );
234 // Create the last row.
235 var oRow
= new $el( table
.$.insertRow( -1 ) );
236 oRow
.setAttribute( 'role', 'row' );
238 // Create the gray scale colors cells.
239 appendColorCell( oRow
.$, '#000000' );
240 for ( var n
= 0; n
< 16; n
++ ) {
241 var c
= n
.toString( 16 );
242 appendColorCell( oRow
.$, '#' + c
+ c
+ c
+ c
+ c
+ c
);
244 appendColorCell( oRow
.$, '#ffffff' );
247 var numbering = function( id
) {
248 return CKEDITOR
.tools
.getNextId() + '_' + id
;
250 hicolorId
= numbering( 'hicolor' ),
251 hicolorTextId
= numbering( 'hicolortext' ),
252 selHiColorId
= numbering( 'selhicolor' ),
276 widths: [ '70%', '10%', '30%' ],
281 CKEDITOR
.document
.getById( this.domId
).append( table
);
284 // Restore the previously focused cell,
285 // otherwise put the initial focus on the first table cell.
286 ( focused
|| this.getElement().getElementsByTag( 'td' ).getItem( 0 ) ).focus();
293 widths: [ '70%', '5%', '25%' ],
296 html: '<span>' + lang
.highlight
+ '</span>' +
297 '<div id="' + hicolorId
+ '" style="border: 1px solid; height: 74px; width: 74px;"></div>' +
298 '<div id="' + hicolorTextId
+ '"> </div><span>' + lang
.selected
+ '</span>' +
299 '<div id="' + selHiColorId
+ '" style="border: 1px solid; height: 20px; width: 74px;"></div>'
303 label: lang
.selected
,
304 labelStyle: 'display:none',
306 style: 'width: 76px;margin-top:4px',
307 onChange: function() {
308 // Try to update color preview with new value. If fails, then set it no none.
310 $doc
.getById( selHiColorId
).setStyle( 'background-color', this.getValue() );
321 onClick: clearSelected