]> git.immae.eu Git - perso/Immae/Projets/packagist/connexionswing-ckeditor-component.git/blame - sources/plugins/colordialog/dialogs/colordialog.js
Upgrade to 4.5.7 and add some plugin
[perso/Immae/Projets/packagist/connexionswing-ckeditor-component.git] / sources / plugins / colordialog / dialogs / colordialog.js
CommitLineData
7adcb81e 1/**\r
3b35bd27 2 * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.\r
7adcb81e
IB
3 * For licensing, see LICENSE.md or http://ckeditor.com/license\r
4 */\r
5\r
6CKEDITOR.dialog.add( 'colordialog', function( editor ) {\r
7 // Define some shorthands.\r
8 var $el = CKEDITOR.dom.element,\r
9 $doc = CKEDITOR.document,\r
10 lang = editor.lang.colordialog;\r
11\r
12 // Reference the dialog.\r
13 var dialog;\r
14\r
15 var spacer = {\r
16 type: 'html',\r
17 html: ' '\r
18 };\r
19\r
20 var selected;\r
21\r
22 function clearSelected() {\r
23 $doc.getById( selHiColorId ).removeStyle( 'background-color' );\r
24 dialog.getContentElement( 'picker', 'selectedColor' ).setValue( '' );\r
25 selected && selected.removeAttribute( 'aria-selected' );\r
26 selected = null;\r
27 }\r
28\r
29 function updateSelected( evt ) {\r
30 var target = evt.data.getTarget(),\r
31 color;\r
32\r
33 if ( target.getName() == 'td' && ( color = target.getChild( 0 ).getHtml() ) ) {\r
34 selected = target;\r
35 selected.setAttribute( 'aria-selected', true );\r
36 dialog.getContentElement( 'picker', 'selectedColor' ).setValue( color );\r
37 }\r
38 }\r
39\r
40 // Basing black-white decision off of luma scheme using the Rec. 709 version\r
41 function whiteOrBlack( color ) {\r
42 color = color.replace( /^#/, '' );\r
43 for ( var i = 0, rgb = []; i <= 2; i++ )\r
44 rgb[ i ] = parseInt( color.substr( i * 2, 2 ), 16 );\r
45 var luma = ( 0.2126 * rgb[ 0 ] ) + ( 0.7152 * rgb[ 1 ] ) + ( 0.0722 * rgb[ 2 ] );\r
46 return '#' + ( luma >= 165 ? '000' : 'fff' );\r
47 }\r
48\r
49 // Distinguish focused and hover states.\r
50 var focused, hovered;\r
51\r
52 // Apply highlight style.\r
53 function updateHighlight( event ) {\r
54 // Convert to event.\r
55 !event.name && ( event = new CKEDITOR.event( event ) );\r
56\r
57 var isFocus = !( /mouse/ ).test( event.name ),\r
58 target = event.data.getTarget(),\r
59 color;\r
60\r
61 if ( target.getName() == 'td' && ( color = target.getChild( 0 ).getHtml() ) ) {\r
62 removeHighlight( event );\r
63\r
64 isFocus ? focused = target : hovered = target;\r
65\r
66 // Apply outline style to show focus.\r
67 if ( isFocus ) {\r
68 target.setStyle( 'border-color', whiteOrBlack( color ) );\r
69 target.setStyle( 'border-style', 'dotted' );\r
70 }\r
71\r
72 $doc.getById( hicolorId ).setStyle( 'background-color', color );\r
73 $doc.getById( hicolorTextId ).setHtml( color );\r
74 }\r
75 }\r
76\r
77 function clearHighlight() {\r
78 var color = focused.getChild( 0 ).getHtml();\r
79 focused.setStyle( 'border-color', color );\r
80 focused.setStyle( 'border-style', 'solid' );\r
81 $doc.getById( hicolorId ).removeStyle( 'background-color' );\r
82 $doc.getById( hicolorTextId ).setHtml( '&nbsp;' );\r
83 focused = null;\r
84 }\r
85\r
86 // Remove previously focused style.\r
87 function removeHighlight( event ) {\r
88 var isFocus = !( /mouse/ ).test( event.name ),\r
89 target = isFocus && focused;\r
90\r
91 if ( target ) {\r
92 var color = target.getChild( 0 ).getHtml();\r
93 target.setStyle( 'border-color', color );\r
94 target.setStyle( 'border-style', 'solid' );\r
95 }\r
96\r
97 if ( !( focused || hovered ) ) {\r
98 $doc.getById( hicolorId ).removeStyle( 'background-color' );\r
99 $doc.getById( hicolorTextId ).setHtml( '&nbsp;' );\r
100 }\r
101 }\r
102\r
103 function onKeyStrokes( evt ) {\r
104 var domEvt = evt.data;\r
105\r
106 var element = domEvt.getTarget();\r
107 var relative, nodeToMove;\r
108 var keystroke = domEvt.getKeystroke(),\r
109 rtl = editor.lang.dir == 'rtl';\r
110\r
111 switch ( keystroke ) {\r
112 // UP-ARROW\r
113 case 38:\r
114 // relative is TR\r
115 if ( ( relative = element.getParent().getPrevious() ) ) {\r
116 nodeToMove = relative.getChild( [ element.getIndex() ] );\r
117 nodeToMove.focus();\r
118 }\r
119 domEvt.preventDefault();\r
120 break;\r
121 // DOWN-ARROW\r
122 case 40:\r
123 // relative is TR\r
124 if ( ( relative = element.getParent().getNext() ) ) {\r
125 nodeToMove = relative.getChild( [ element.getIndex() ] );\r
126 if ( nodeToMove && nodeToMove.type == 1 )\r
127 nodeToMove.focus();\r
128\r
129 }\r
130 domEvt.preventDefault();\r
131 break;\r
132\r
133 // SPACE\r
134 // ENTER\r
135 case 32:\r
136 case 13:\r
137 updateSelected( evt );\r
138 domEvt.preventDefault();\r
139 break;\r
140\r
141 // RIGHT-ARROW\r
142 case rtl ? 37 : 39:\r
143 // relative is TD\r
144 if ( ( nodeToMove = element.getNext() ) ) {\r
145 if ( nodeToMove.type == 1 ) {\r
146 nodeToMove.focus();\r
147 domEvt.preventDefault( true );\r
148 }\r
149 }\r
150 // relative is TR\r
151 else if ( ( relative = element.getParent().getNext() ) ) {\r
152 nodeToMove = relative.getChild( [ 0 ] );\r
153 if ( nodeToMove && nodeToMove.type == 1 ) {\r
154 nodeToMove.focus();\r
155 domEvt.preventDefault( true );\r
156 }\r
157 }\r
158 break;\r
159\r
160 // LEFT-ARROW\r
161 case rtl ? 39 : 37:\r
162 // relative is TD\r
163 if ( ( nodeToMove = element.getPrevious() ) ) {\r
164 nodeToMove.focus();\r
165 domEvt.preventDefault( true );\r
166 }\r
167 // relative is TR\r
168 else if ( ( relative = element.getParent().getPrevious() ) ) {\r
169 nodeToMove = relative.getLast();\r
170 nodeToMove.focus();\r
171 domEvt.preventDefault( true );\r
172 }\r
173 break;\r
174 default:\r
175 // Do not stop not handled events.\r
176 return;\r
177 }\r
178 }\r
179\r
180 function createColorTable() {\r
181 table = CKEDITOR.dom.element.createFromHtml( '<table tabIndex="-1" aria-label="' + lang.options + '"' +\r
182 ' role="grid" style="border-collapse:separate;" cellspacing="0">' +\r
183 '<caption class="cke_voice_label">' + lang.options + '</caption>' +\r
184 '<tbody role="presentation"></tbody></table>' );\r
185\r
186 table.on( 'mouseover', updateHighlight );\r
187 table.on( 'mouseout', removeHighlight );\r
188\r
189 // Create the base colors array.\r
190 var aColors = [ '00', '33', '66', '99', 'cc', 'ff' ];\r
191\r
192 // This function combines two ranges of three values from the color array into a row.\r
193 function appendColorRow( rangeA, rangeB ) {\r
194 for ( var i = rangeA; i < rangeA + 3; i++ ) {\r
195 var row = new $el( table.$.insertRow( -1 ) );\r
196 row.setAttribute( 'role', 'row' );\r
197\r
198 for ( var j = rangeB; j < rangeB + 3; j++ ) {\r
199 for ( var n = 0; n < 6; n++ ) {\r
200 appendColorCell( row.$, '#' + aColors[ j ] + aColors[ n ] + aColors[ i ] );\r
201 }\r
202 }\r
203 }\r
204 }\r
205\r
206 // This function create a single color cell in the color table.\r
207 function appendColorCell( targetRow, color ) {\r
208 var cell = new $el( targetRow.insertCell( -1 ) );\r
209 cell.setAttribute( 'class', 'ColorCell' );\r
210 cell.setAttribute( 'tabIndex', -1 );\r
211 cell.setAttribute( 'role', 'gridcell' );\r
212\r
213 cell.on( 'keydown', onKeyStrokes );\r
214 cell.on( 'click', updateSelected );\r
215 cell.on( 'focus', updateHighlight );\r
216 cell.on( 'blur', removeHighlight );\r
217\r
218 cell.setStyle( 'background-color', color );\r
219 cell.setStyle( 'border', '1px solid ' + color );\r
220\r
221 cell.setStyle( 'width', '14px' );\r
222 cell.setStyle( 'height', '14px' );\r
223\r
224 var colorLabel = numbering( 'color_table_cell' );\r
225 cell.setAttribute( 'aria-labelledby', colorLabel );\r
226 cell.append( CKEDITOR.dom.element.createFromHtml( '<span id="' + colorLabel + '" class="cke_voice_label">' + color + '</span>', CKEDITOR.document ) );\r
227 }\r
228\r
229 appendColorRow( 0, 0 );\r
230 appendColorRow( 3, 0 );\r
231 appendColorRow( 0, 3 );\r
232 appendColorRow( 3, 3 );\r
233\r
234 // Create the last row.\r
235 var oRow = new $el( table.$.insertRow( -1 ) );\r
236 oRow.setAttribute( 'role', 'row' );\r
237\r
238 // Create the gray scale colors cells.\r
239 appendColorCell( oRow.$, '#000000' );\r
240 for ( var n = 0; n < 16; n++ ) {\r
241 var c = n.toString( 16 );\r
242 appendColorCell( oRow.$, '#' + c + c + c + c + c + c );\r
243 }\r
244 appendColorCell( oRow.$, '#ffffff' );\r
245 }\r
246\r
247 var numbering = function( id ) {\r
248 return CKEDITOR.tools.getNextId() + '_' + id;\r
249 },\r
250 hicolorId = numbering( 'hicolor' ),\r
251 hicolorTextId = numbering( 'hicolortext' ),\r
252 selHiColorId = numbering( 'selhicolor' ),\r
253 table;\r
254\r
255 createColorTable();\r
256\r
257 return {\r
258 title: lang.title,\r
259 minWidth: 360,\r
260 minHeight: 220,\r
261 onLoad: function() {\r
262 // Update reference.\r
263 dialog = this;\r
264 },\r
265 onHide: function() {\r
266 clearSelected();\r
267 clearHighlight();\r
268 },\r
269 contents: [ {\r
270 id: 'picker',\r
271 label: lang.title,\r
272 accessKey: 'I',\r
273 elements: [ {\r
274 type: 'hbox',\r
275 padding: 0,\r
276 widths: [ '70%', '10%', '30%' ],\r
277 children: [ {\r
278 type: 'html',\r
279 html: '<div></div>',\r
280 onLoad: function() {\r
281 CKEDITOR.document.getById( this.domId ).append( table );\r
282 },\r
283 focus: function() {\r
284 // Restore the previously focused cell,\r
285 // otherwise put the initial focus on the first table cell.\r
286 ( focused || this.getElement().getElementsByTag( 'td' ).getItem( 0 ) ).focus();\r
287 }\r
288 },\r
289 spacer,\r
290 {\r
291 type: 'vbox',\r
292 padding: 0,\r
293 widths: [ '70%', '5%', '25%' ],\r
294 children: [ {\r
295 type: 'html',\r
296 html: '<span>' + lang.highlight + '</span>' +\r
297 '<div id="' + hicolorId + '" style="border: 1px solid; height: 74px; width: 74px;"></div>' +\r
298 '<div id="' + hicolorTextId + '">&nbsp;</div><span>' + lang.selected + '</span>' +\r
299 '<div id="' + selHiColorId + '" style="border: 1px solid; height: 20px; width: 74px;"></div>'\r
300 },\r
301 {\r
302 type: 'text',\r
303 label: lang.selected,\r
304 labelStyle: 'display:none',\r
305 id: 'selectedColor',\r
306 style: 'width: 76px;margin-top:4px',\r
307 onChange: function() {\r
308 // Try to update color preview with new value. If fails, then set it no none.\r
309 try {\r
310 $doc.getById( selHiColorId ).setStyle( 'background-color', this.getValue() );\r
311 } catch ( e ) {\r
312 clearSelected();\r
313 }\r
314 }\r
315 },\r
316 spacer,\r
317 {\r
318 type: 'button',\r
319 id: 'clear',\r
320 label: lang.clear,\r
321 onClick: clearSelected\r
322 } ]\r
323 } ]\r
324 } ]\r
325 } ]\r
326 };\r
327} );\r