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