Upgrade TinyMCE to v3.4.5
[citadel.git] / webcit / tiny_mce / themes / advanced / js / color_picker.js
1 tinyMCEPopup.requireLangPack();\r
2 \r
3 var detail = 50, strhex = "0123456789ABCDEF", i, isMouseDown = false, isMouseOver = false;\r
4 \r
5 var colors = [\r
6         "#000000","#000033","#000066","#000099","#0000cc","#0000ff","#330000","#330033",\r
7         "#330066","#330099","#3300cc","#3300ff","#660000","#660033","#660066","#660099",\r
8         "#6600cc","#6600ff","#990000","#990033","#990066","#990099","#9900cc","#9900ff",\r
9         "#cc0000","#cc0033","#cc0066","#cc0099","#cc00cc","#cc00ff","#ff0000","#ff0033",\r
10         "#ff0066","#ff0099","#ff00cc","#ff00ff","#003300","#003333","#003366","#003399",\r
11         "#0033cc","#0033ff","#333300","#333333","#333366","#333399","#3333cc","#3333ff",\r
12         "#663300","#663333","#663366","#663399","#6633cc","#6633ff","#993300","#993333",\r
13         "#993366","#993399","#9933cc","#9933ff","#cc3300","#cc3333","#cc3366","#cc3399",\r
14         "#cc33cc","#cc33ff","#ff3300","#ff3333","#ff3366","#ff3399","#ff33cc","#ff33ff",\r
15         "#006600","#006633","#006666","#006699","#0066cc","#0066ff","#336600","#336633",\r
16         "#336666","#336699","#3366cc","#3366ff","#666600","#666633","#666666","#666699",\r
17         "#6666cc","#6666ff","#996600","#996633","#996666","#996699","#9966cc","#9966ff",\r
18         "#cc6600","#cc6633","#cc6666","#cc6699","#cc66cc","#cc66ff","#ff6600","#ff6633",\r
19         "#ff6666","#ff6699","#ff66cc","#ff66ff","#009900","#009933","#009966","#009999",\r
20         "#0099cc","#0099ff","#339900","#339933","#339966","#339999","#3399cc","#3399ff",\r
21         "#669900","#669933","#669966","#669999","#6699cc","#6699ff","#999900","#999933",\r
22         "#999966","#999999","#9999cc","#9999ff","#cc9900","#cc9933","#cc9966","#cc9999",\r
23         "#cc99cc","#cc99ff","#ff9900","#ff9933","#ff9966","#ff9999","#ff99cc","#ff99ff",\r
24         "#00cc00","#00cc33","#00cc66","#00cc99","#00cccc","#00ccff","#33cc00","#33cc33",\r
25         "#33cc66","#33cc99","#33cccc","#33ccff","#66cc00","#66cc33","#66cc66","#66cc99",\r
26         "#66cccc","#66ccff","#99cc00","#99cc33","#99cc66","#99cc99","#99cccc","#99ccff",\r
27         "#cccc00","#cccc33","#cccc66","#cccc99","#cccccc","#ccccff","#ffcc00","#ffcc33",\r
28         "#ffcc66","#ffcc99","#ffcccc","#ffccff","#00ff00","#00ff33","#00ff66","#00ff99",\r
29         "#00ffcc","#00ffff","#33ff00","#33ff33","#33ff66","#33ff99","#33ffcc","#33ffff",\r
30         "#66ff00","#66ff33","#66ff66","#66ff99","#66ffcc","#66ffff","#99ff00","#99ff33",\r
31         "#99ff66","#99ff99","#99ffcc","#99ffff","#ccff00","#ccff33","#ccff66","#ccff99",\r
32         "#ccffcc","#ccffff","#ffff00","#ffff33","#ffff66","#ffff99","#ffffcc","#ffffff"\r
33 ];\r
34 \r
35 var named = {\r
36         '#F0F8FF':'Alice Blue','#FAEBD7':'Antique White','#00FFFF':'Aqua','#7FFFD4':'Aquamarine','#F0FFFF':'Azure','#F5F5DC':'Beige',\r
37         '#FFE4C4':'Bisque','#000000':'Black','#FFEBCD':'Blanched Almond','#0000FF':'Blue','#8A2BE2':'Blue Violet','#A52A2A':'Brown',\r
38         '#DEB887':'Burly Wood','#5F9EA0':'Cadet Blue','#7FFF00':'Chartreuse','#D2691E':'Chocolate','#FF7F50':'Coral','#6495ED':'Cornflower Blue',\r
39         '#FFF8DC':'Cornsilk','#DC143C':'Crimson','#00FFFF':'Cyan','#00008B':'Dark Blue','#008B8B':'Dark Cyan','#B8860B':'Dark Golden Rod',\r
40         '#A9A9A9':'Dark Gray','#A9A9A9':'Dark Grey','#006400':'Dark Green','#BDB76B':'Dark Khaki','#8B008B':'Dark Magenta','#556B2F':'Dark Olive Green',\r
41         '#FF8C00':'Darkorange','#9932CC':'Dark Orchid','#8B0000':'Dark Red','#E9967A':'Dark Salmon','#8FBC8F':'Dark Sea Green','#483D8B':'Dark Slate Blue',\r
42         '#2F4F4F':'Dark Slate Gray','#2F4F4F':'Dark Slate Grey','#00CED1':'Dark Turquoise','#9400D3':'Dark Violet','#FF1493':'Deep Pink','#00BFFF':'Deep Sky Blue',\r
43         '#696969':'Dim Gray','#696969':'Dim Grey','#1E90FF':'Dodger Blue','#B22222':'Fire Brick','#FFFAF0':'Floral White','#228B22':'Forest Green',\r
44         '#FF00FF':'Fuchsia','#DCDCDC':'Gainsboro','#F8F8FF':'Ghost White','#FFD700':'Gold','#DAA520':'Golden Rod','#808080':'Gray','#808080':'Grey',\r
45         '#008000':'Green','#ADFF2F':'Green Yellow','#F0FFF0':'Honey Dew','#FF69B4':'Hot Pink','#CD5C5C':'Indian Red','#4B0082':'Indigo','#FFFFF0':'Ivory',\r
46         '#F0E68C':'Khaki','#E6E6FA':'Lavender','#FFF0F5':'Lavender Blush','#7CFC00':'Lawn Green','#FFFACD':'Lemon Chiffon','#ADD8E6':'Light Blue',\r
47         '#F08080':'Light Coral','#E0FFFF':'Light Cyan','#FAFAD2':'Light Golden Rod Yellow','#D3D3D3':'Light Gray','#D3D3D3':'Light Grey','#90EE90':'Light Green',\r
48         '#FFB6C1':'Light Pink','#FFA07A':'Light Salmon','#20B2AA':'Light Sea Green','#87CEFA':'Light Sky Blue','#778899':'Light Slate Gray','#778899':'Light Slate Grey',\r
49         '#B0C4DE':'Light Steel Blue','#FFFFE0':'Light Yellow','#00FF00':'Lime','#32CD32':'Lime Green','#FAF0E6':'Linen','#FF00FF':'Magenta','#800000':'Maroon',\r
50         '#66CDAA':'Medium Aqua Marine','#0000CD':'Medium Blue','#BA55D3':'Medium Orchid','#9370D8':'Medium Purple','#3CB371':'Medium Sea Green','#7B68EE':'Medium Slate Blue',\r
51         '#00FA9A':'Medium Spring Green','#48D1CC':'Medium Turquoise','#C71585':'Medium Violet Red','#191970':'Midnight Blue','#F5FFFA':'Mint Cream','#FFE4E1':'Misty Rose','#FFE4B5':'Moccasin',\r
52         '#FFDEAD':'Navajo White','#000080':'Navy','#FDF5E6':'Old Lace','#808000':'Olive','#6B8E23':'Olive Drab','#FFA500':'Orange','#FF4500':'Orange Red','#DA70D6':'Orchid',\r
53         '#EEE8AA':'Pale Golden Rod','#98FB98':'Pale Green','#AFEEEE':'Pale Turquoise','#D87093':'Pale Violet Red','#FFEFD5':'Papaya Whip','#FFDAB9':'Peach Puff',\r
54         '#CD853F':'Peru','#FFC0CB':'Pink','#DDA0DD':'Plum','#B0E0E6':'Powder Blue','#800080':'Purple','#FF0000':'Red','#BC8F8F':'Rosy Brown','#4169E1':'Royal Blue',\r
55         '#8B4513':'Saddle Brown','#FA8072':'Salmon','#F4A460':'Sandy Brown','#2E8B57':'Sea Green','#FFF5EE':'Sea Shell','#A0522D':'Sienna','#C0C0C0':'Silver',\r
56         '#87CEEB':'Sky Blue','#6A5ACD':'Slate Blue','#708090':'Slate Gray','#708090':'Slate Grey','#FFFAFA':'Snow','#00FF7F':'Spring Green',\r
57         '#4682B4':'Steel Blue','#D2B48C':'Tan','#008080':'Teal','#D8BFD8':'Thistle','#FF6347':'Tomato','#40E0D0':'Turquoise','#EE82EE':'Violet',\r
58         '#F5DEB3':'Wheat','#FFFFFF':'White','#F5F5F5':'White Smoke','#FFFF00':'Yellow','#9ACD32':'Yellow Green'\r
59 };\r
60 \r
61 var namedLookup = {};\r
62 \r
63 function init() {\r
64         var inputColor = convertRGBToHex(tinyMCEPopup.getWindowArg('input_color')), key, value;\r
65 \r
66         tinyMCEPopup.resizeToInnerSize();\r
67 \r
68         generatePicker();\r
69         generateWebColors();\r
70         generateNamedColors();\r
71 \r
72         if (inputColor) {\r
73                 changeFinalColor(inputColor);\r
74 \r
75                 col = convertHexToRGB(inputColor);\r
76 \r
77                 if (col)\r
78                         updateLight(col.r, col.g, col.b);\r
79         }\r
80         \r
81         for (key in named) {\r
82                 value = named[key];\r
83                 namedLookup[value.replace(/\s+/, '').toLowerCase()] = key.replace(/#/, '').toLowerCase();\r
84         }\r
85 }\r
86 \r
87 function toHexColor(color) {\r
88         var matches, red, green, blue, toInt = parseInt;\r
89 \r
90         function hex(value) {\r
91                 value = parseInt(value).toString(16);\r
92 \r
93                 return value.length > 1 ? value : '0' + value; // Padd with leading zero\r
94         };\r
95 \r
96         color = color.replace(/[\s#]+/g, '').toLowerCase();\r
97         color = namedLookup[color] || color;\r
98         matches = /^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)|([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})|([a-f0-9])([a-f0-9])([a-f0-9])$/.exec(color);\r
99 \r
100         if (matches) {\r
101                 if (matches[1]) {\r
102                         red = toInt(matches[1]);\r
103                         green = toInt(matches[2]);\r
104                         blue = toInt(matches[3]);\r
105                 } else if (matches[4]) {\r
106                         red = toInt(matches[4], 16);\r
107                         green = toInt(matches[5], 16);\r
108                         blue = toInt(matches[6], 16);\r
109                 } else if (matches[7]) {\r
110                         red = toInt(matches[7] + matches[7], 16);\r
111                         green = toInt(matches[8] + matches[8], 16);\r
112                         blue = toInt(matches[9] + matches[9], 16);\r
113                 }\r
114 \r
115                 return '#' + hex(red) + hex(green) + hex(blue);\r
116         }\r
117 \r
118         return '';\r
119 }\r
120 \r
121 function insertAction() {\r
122         var color = document.getElementById("color").value, f = tinyMCEPopup.getWindowArg('func');\r
123 \r
124         tinyMCEPopup.restoreSelection();\r
125 \r
126         if (f)\r
127                 f(toHexColor(color));\r
128 \r
129         tinyMCEPopup.close();\r
130 }\r
131 \r
132 function showColor(color, name) {\r
133         if (name)\r
134                 document.getElementById("colorname").innerHTML = name;\r
135 \r
136         document.getElementById("preview").style.backgroundColor = color;\r
137         document.getElementById("color").value = color.toUpperCase();\r
138 }\r
139 \r
140 function convertRGBToHex(col) {\r
141         var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");\r
142 \r
143         if (!col)\r
144                 return col;\r
145 \r
146         var rgb = col.replace(re, "$1,$2,$3").split(',');\r
147         if (rgb.length == 3) {\r
148                 r = parseInt(rgb[0]).toString(16);\r
149                 g = parseInt(rgb[1]).toString(16);\r
150                 b = parseInt(rgb[2]).toString(16);\r
151 \r
152                 r = r.length == 1 ? '0' + r : r;\r
153                 g = g.length == 1 ? '0' + g : g;\r
154                 b = b.length == 1 ? '0' + b : b;\r
155 \r
156                 return "#" + r + g + b;\r
157         }\r
158 \r
159         return col;\r
160 }\r
161 \r
162 function convertHexToRGB(col) {\r
163         if (col.indexOf('#') != -1) {\r
164                 col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');\r
165 \r
166                 r = parseInt(col.substring(0, 2), 16);\r
167                 g = parseInt(col.substring(2, 4), 16);\r
168                 b = parseInt(col.substring(4, 6), 16);\r
169 \r
170                 return {r : r, g : g, b : b};\r
171         }\r
172 \r
173         return null;\r
174 }\r
175 \r
176 function generatePicker() {\r
177         var el = document.getElementById('light'), h = '', i;\r
178 \r
179         for (i = 0; i < detail; i++){\r
180                 h += '<div id="gs'+i+'" style="background-color:#000000; width:15px; height:3px; border-style:none; border-width:0px;"'\r
181                 + ' onclick="changeFinalColor(this.style.backgroundColor)"'\r
182                 + ' onmousedown="isMouseDown = true; return false;"'\r
183                 + ' onmouseup="isMouseDown = false;"'\r
184                 + ' onmousemove="if (isMouseDown && isMouseOver) changeFinalColor(this.style.backgroundColor); return false;"'\r
185                 + ' onmouseover="isMouseOver = true;"'\r
186                 + ' onmouseout="isMouseOver = false;"'\r
187                 + '></div>';\r
188         }\r
189 \r
190         el.innerHTML = h;\r
191 }\r
192 \r
193 function generateWebColors() {\r
194         var el = document.getElementById('webcolors'), h = '', i;\r
195 \r
196         if (el.className == 'generated')\r
197                 return;\r
198 \r
199         // TODO: VoiceOver doesn't seem to support legend as a label referenced by labelledby.\r
200         h += '<div role="listbox" aria-labelledby="webcolors_title" tabindex="0"><table role="presentation" border="0" cellspacing="1" cellpadding="0">'\r
201                 + '<tr>';\r
202 \r
203         for (i=0; i<colors.length; i++) {\r
204                 h += '<td bgcolor="' + colors[i] + '" width="10" height="10">'\r
205                         + '<a href="javascript:insertAction();" role="option" tabindex="-1" aria-labelledby="web_colors_' + i + '" onfocus="showColor(\'' + colors[i] + '\');" onmouseover="showColor(\'' + colors[i] + '\');" style="display:block;width:10px;height:10px;overflow:hidden;">';\r
206                 if (tinyMCEPopup.editor.forcedHighContrastMode) {\r
207                         h += '<canvas class="mceColorSwatch" height="10" width="10" data-color="' + colors[i] + '"></canvas>';\r
208                 }\r
209                 h += '<span class="mceVoiceLabel" style="display:none;" id="web_colors_' + i + '">' + colors[i].toUpperCase() + '</span>';\r
210                 h += '</a></td>';\r
211                 if ((i+1) % 18 == 0)\r
212                         h += '</tr><tr>';\r
213         }\r
214 \r
215         h += '</table></div>';\r
216 \r
217         el.innerHTML = h;\r
218         el.className = 'generated';\r
219 \r
220         paintCanvas(el);\r
221         enableKeyboardNavigation(el.firstChild);\r
222 }\r
223 \r
224 function paintCanvas(el) {\r
225         tinyMCEPopup.getWin().tinymce.each(tinyMCEPopup.dom.select('canvas.mceColorSwatch', el), function(canvas) {\r
226                 var context;\r
227                 if (canvas.getContext && (context = canvas.getContext("2d"))) {\r
228                         context.fillStyle = canvas.getAttribute('data-color');\r
229                         context.fillRect(0, 0, 10, 10);\r
230                 }\r
231         });\r
232 }\r
233 function generateNamedColors() {\r
234         var el = document.getElementById('namedcolors'), h = '', n, v, i = 0;\r
235 \r
236         if (el.className == 'generated')\r
237                 return;\r
238 \r
239         for (n in named) {\r
240                 v = named[n];\r
241                 h += '<a href="javascript:insertAction();" role="option" tabindex="-1" aria-labelledby="named_colors_' + i + '" onfocus="showColor(\'' + n + '\',\'' + v + '\');" onmouseover="showColor(\'' + n + '\',\'' + v + '\');" style="background-color: ' + n + '">';\r
242                 if (tinyMCEPopup.editor.forcedHighContrastMode) {\r
243                         h += '<canvas class="mceColorSwatch" height="10" width="10" data-color="' + colors[i] + '"></canvas>';\r
244                 }\r
245                 h += '<span class="mceVoiceLabel" style="display:none;" id="named_colors_' + i + '">' + v + '</span>';\r
246                 h += '</a>';\r
247                 i++;\r
248         }\r
249 \r
250         el.innerHTML = h;\r
251         el.className = 'generated';\r
252 \r
253         paintCanvas(el);\r
254         enableKeyboardNavigation(el);\r
255 }\r
256 \r
257 function enableKeyboardNavigation(el) {\r
258         tinyMCEPopup.editor.windowManager.createInstance('tinymce.ui.KeyboardNavigation', {\r
259                 root: el,\r
260                 items: tinyMCEPopup.dom.select('a', el)\r
261         }, tinyMCEPopup.dom);\r
262 }\r
263 \r
264 function dechex(n) {\r
265         return strhex.charAt(Math.floor(n / 16)) + strhex.charAt(n % 16);\r
266 }\r
267 \r
268 function computeColor(e) {\r
269         var x, y, partWidth, partDetail, imHeight, r, g, b, coef, i, finalCoef, finalR, finalG, finalB;\r
270 \r
271         x = e.offsetX ? e.offsetX : (e.target ? e.clientX - e.target.x : 0);\r
272         y = e.offsetY ? e.offsetY : (e.target ? e.clientY - e.target.y : 0);\r
273 \r
274         partWidth = document.getElementById('colors').width / 6;\r
275         partDetail = detail / 2;\r
276         imHeight = document.getElementById('colors').height;\r
277 \r
278         r = (x >= 0)*(x < partWidth)*255 + (x >= partWidth)*(x < 2*partWidth)*(2*255 - x * 255 / partWidth) + (x >= 4*partWidth)*(x < 5*partWidth)*(-4*255 + x * 255 / partWidth) + (x >= 5*partWidth)*(x < 6*partWidth)*255;\r
279         g = (x >= 0)*(x < partWidth)*(x * 255 / partWidth) + (x >= partWidth)*(x < 3*partWidth)*255     + (x >= 3*partWidth)*(x < 4*partWidth)*(4*255 - x * 255 / partWidth);\r
280         b = (x >= 2*partWidth)*(x < 3*partWidth)*(-2*255 + x * 255 / partWidth) + (x >= 3*partWidth)*(x < 5*partWidth)*255 + (x >= 5*partWidth)*(x < 6*partWidth)*(6*255 - x * 255 / partWidth);\r
281 \r
282         coef = (imHeight - y) / imHeight;\r
283         r = 128 + (r - 128) * coef;\r
284         g = 128 + (g - 128) * coef;\r
285         b = 128 + (b - 128) * coef;\r
286 \r
287         changeFinalColor('#' + dechex(r) + dechex(g) + dechex(b));\r
288         updateLight(r, g, b);\r
289 }\r
290 \r
291 function updateLight(r, g, b) {\r
292         var i, partDetail = detail / 2, finalCoef, finalR, finalG, finalB, color;\r
293 \r
294         for (i=0; i<detail; i++) {\r
295                 if ((i>=0) && (i<partDetail)) {\r
296                         finalCoef = i / partDetail;\r
297                         finalR = dechex(255 - (255 - r) * finalCoef);\r
298                         finalG = dechex(255 - (255 - g) * finalCoef);\r
299                         finalB = dechex(255 - (255 - b) * finalCoef);\r
300                 } else {\r
301                         finalCoef = 2 - i / partDetail;\r
302                         finalR = dechex(r * finalCoef);\r
303                         finalG = dechex(g * finalCoef);\r
304                         finalB = dechex(b * finalCoef);\r
305                 }\r
306 \r
307                 color = finalR + finalG + finalB;\r
308 \r
309                 setCol('gs' + i, '#'+color);\r
310         }\r
311 }\r
312 \r
313 function changeFinalColor(color) {\r
314         if (color.indexOf('#') == -1)\r
315                 color = convertRGBToHex(color);\r
316 \r
317         setCol('preview', color);\r
318         document.getElementById('color').value = color;\r
319 }\r
320 \r
321 function setCol(e, c) {\r
322         try {\r
323                 document.getElementById(e).style.backgroundColor = c;\r
324         } catch (ex) {\r
325                 // Ignore IE warning\r
326         }\r
327 }\r
328 \r
329 tinyMCEPopup.onInit.add(init);\r