CColorPicker.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| BI and PM: prmt
  5. *| (C) Copyright IBM Corp. 2002, 2016
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. /*
  13. CColorPicker.js
  14. This script provides interactivity for the color prompt control.
  15. */
  16. // Constructor to create a new Color Picker prompt
  17. // sPromptId: The id of the prompt control (String)
  18. // sRef: the name of this object
  19. // sCallBackFunction: function to call with the new color value as a parameter
  20. // bAutoReset: automatically reset the color to the default if true
  21. // sDefaultValue: a default CSS RGB value to initialize the controls
  22. // (e.g. '#000000', '#FFFFFF')
  23. // sType: define the whether the control is foreground or background
  24. // (e.g. TYPE_COLOR | TYPE_BACKGROUND_COLOR)
  25. // sTooltip: string to used as a tooltip for the color picker button
  26. // bToolbar: (Optional, Default: true). Is this button in a toolbar?
  27. function CColorPicker(sPromptId, sRef, sCallBackFunction, bAutoReset, sDefaultValue, sType, sToolTip, bToolbar)
  28. {
  29. // set up control
  30. this.m_sSkin = (typeof getPromptSkin != "undefined" ? getPromptSkin() : "../skins/corporate");
  31. this.m_sImgPath = this.m_sSkin + "/prompting/images/";
  32. this.m_sToolTip = sToolTip;
  33. this.m_bToolbar = (bToolbar != false);
  34. if (sType == TYPE_BACKGROUND_COLOR)
  35. {
  36. this.m_sIcon = "bgcolor.gif";
  37. }
  38. else
  39. {
  40. this.m_sIcon = "color.gif";
  41. }
  42. this.m_sPromptId = sPromptId;
  43. this.m_sRef = sRef;
  44. //the parent object
  45. this.m_oParent = document.getElementById('selectColor' + this.m_sPromptId);
  46. //dialog
  47. this.m_sDialogId = "colorDialog" + sPromptId;
  48. this.m_sCaptionId = "colorDialogCaption" + sPromptId;
  49. this.m_sPaletteId = "colorDialogPalette" + sPromptId;
  50. //drawn palettes
  51. this.m_sSelectedPalette = null;
  52. this.m_bNamedPallete = false;
  53. this.m_bWebPallete = false;
  54. this.m_bCustomPallete = false;
  55. //auto reset back to default
  56. this.m_bAutoReset = (bAutoReset != null) ? bAutoReset : false;
  57. //current color
  58. this.m_sColor = ((sDefaultValue != null) && (sDefaultValue != '')) ? parseRGB(sDefaultValue) : '';
  59. this.m_oNamedColor = new CNamedColor();
  60. this.m_oNamedColors = null;
  61. //custom palette textboxes
  62. this.m_sCustomR = "customR" + sPromptId;
  63. this.m_sCustomG = "customG" + sPromptId;
  64. this.m_sCustomB = "customB" + sPromptId;
  65. this.m_sCustomPreview = "customPreview" + sPromptId;
  66. //set callback function
  67. this.m_sCallBackFunction = sCallBackFunction;
  68. this.init();
  69. // update the UI
  70. this.draw();
  71. }
  72. //initialize the control
  73. function CColorPicker_init()
  74. {
  75. //draw the button
  76. var s = (this.m_bToolbar ? '<button ' : '<span ' );
  77. if (this.m_sToolTip != null && this.m_sToolTip != "") {
  78. s += 'title="'+this.m_sToolTip+'" ';
  79. }
  80. if (this.m_bToolbar) {
  81. s += 'class="clsCPButton" onmouseover="this.className=\'clsCPButtonOver\'" onmouseout="this.className=\'clsCPButton\'" type="button" ';
  82. }
  83. s += 'onclick="'+this.m_sRef+'.togglePicker(event)"><img src="'+ this.m_sImgPath + this.m_sIcon +'">';
  84. s += (this.m_bToolbar ? '</button>' : '</span>');
  85. //draw the picker
  86. s += '<br/>';
  87. if (window.ie) { s += '<iframe id="iframe'+this.m_sDialogId+'" src="'+this.m_sImgPath+'spacer.gif" style="display:none;position:absolute;margin-top:1px;"></iframe>'; }
  88. s += '<div id="'+this.m_sDialogId+'" class="clsCPMenuContainer" style="display:none;position:absolute;width:312px;margin-top:1px;" onclick="CColorPicker_preventBubbling(event);">';
  89. s += '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td id="'+ this.m_sCaptionId +'" class="clsCPCaptionBar"></td></tr>';
  90. s += '<tr><td id="'+ this.m_sPaletteId +'" class="clsCPPalette"></td></tr></table>';
  91. s += '</div>';
  92. this.m_oParent.innerHTML = s;
  93. this.m_oDialog = document.getElementById(this.m_sDialogId);
  94. this.m_oDialogCaption = document.getElementById(this.m_sCaptionId);
  95. this.m_oDialogPalette = document.getElementById(this.m_sPaletteId);
  96. }
  97. //render the UI for the control
  98. function CColorPicker_draw()
  99. {
  100. this.drawCaptionBar();
  101. this.setActiveColor('');
  102. }
  103. //render the caption bar for the picker dialog
  104. function CColorPicker_drawCaptionBar()
  105. {
  106. var s = '<select id="'+this.m_sPromptId+'_select" onchange="' +this.m_sRef+ '.setPalette(this.options[this.selectedIndex].value)" onclick="CColorPicker_preventBubbling(event);">';
  107. //named palette
  108. s += '<option ';
  109. s += ' value="'+PALETTE_NAMED+'">' + PMT_CP_NAMED_COLORS + '</option>';
  110. //web palette
  111. s += '<option ';
  112. s += ' value="'+PALETTE_WEB+'">' + PMT_CP_WEB_SAFE_COLORS + '</option>';
  113. //custom
  114. s += '<option ';
  115. s += ' value="'+PALETTE_CUSTOM+'">' + PMT_CP_CUSTOM_COLOR + '</option>';
  116. s += '</select>';
  117. this.m_oDialogCaption.innerHTML = s;
  118. }
  119. function CColorPicker_drawPalette()
  120. {
  121. if (!this.m_bNamedPalette && this.m_sSelectedPalette == PALETTE_NAMED)
  122. {
  123. this.drawNamedPalette();
  124. this.m_bNamedPalette = true;
  125. this.setActiveClass(true, false);
  126. }
  127. else if (!this.m_bWebPalette && this.m_sSelectedPalette == PALETTE_WEB)
  128. {
  129. this.drawWebsafePalette();
  130. this.m_bWebPalette = true;
  131. this.setActiveClass(true, false);
  132. }
  133. else if (!this.m_bCustomPalettte && this.m_sSelectedPalette == PALETTE_CUSTOM)
  134. {
  135. this.drawCustomPalette();
  136. this.m_bCustomPalette = true;
  137. this.setActiveClass(true, false);
  138. }
  139. }
  140. //custom palette dialog
  141. function CColorPicker_drawCustomPalette()
  142. {
  143. var color = this.getColor();
  144. var s = '';
  145. s += '<table id="'+this.m_sPromptId+'_'+PALETTE_CUSTOM+'" cellpadding="0" cellspacing="0" border="0" width="100%" style="display:none" onclick="CColorPicker_preventBubbling(event);">';
  146. //user selection
  147. s += '<tr><td valign="top" class="clsCPCustomTB">';
  148. s += '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
  149. s += '<tr><td class="clsControlLabel pc" colspan=2>' + PMT_CP_RED + '</td></tr>';
  150. s += '<tr><td class="clsReadOnlyText">#</td><td class="clsReadOnlyText"><input type="text" size="2" maxlength="2" value="'+getR(color)+'" name="'+ this.m_sCustomR +'" id="'+ this.m_sCustomR +'" onchange="' + this.m_sRef +'.updateCustomStyle()"></td></tr>';
  151. s += '<tr><td class="clsControlLabel pc" colspan=2>' + PMT_CP_GREEN + '</td></tr>';
  152. s += '<tr><td class="clsReadOnlyText"></td><td class="clsReadOnlyText"><input type="text" size="2" maxlength="2" value="'+getG(color)+'" name="'+ this.m_sCustomG +'" id="'+ this.m_sCustomG +'" onchange="' + this.m_sRef +'.updateCustomStyle()"></td></tr>';
  153. s += '<tr><td class="clsControlLabel pc" colspan=2>' + PMT_CP_BLUE + '</td></tr>';
  154. s += '<tr><td class="clsReadOnlyText"></td><td class="clsReadOnlyText"><input type="text" size="2" maxlength="2" value="'+getB(color)+'" name="'+ this.m_sCustomB +'" id="'+ this.m_sCustomB +'" onchange="' + this.m_sRef +'.updateCustomStyle()"></td></tr>';
  155. s += '</table>';
  156. s += '</td>';
  157. //preview
  158. s += '<td valign="top">';
  159. s += '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
  160. s += '<tr><td valign="top" class="clsControlLabel pc">' + PMT_CP_PREVIEW + '</td></tr>';
  161. s += '<tr><td valign="middle" align="center" class="clsCPPreview"><div class="clsCPPreviewSwatch" id="'+this.m_sCustomPreview+'" style="background-color:' + color +'" onclick="' + this.m_sRef +'.applyCustomStyle()"></div></td></tr>';
  162. s += '</table>';
  163. s += '</tr></td>';
  164. s += '<tr><td colspan="2" align="right"><span class="clsCPLink" onclick="' + this.m_sRef +'.applyCustomStyle()">' + PMT_CP_APPLY_NOW + '</span></td></tr></table>';
  165. this.m_oDialogPalette.innerHTML += s;
  166. }
  167. //named palette dialog
  168. function CColorPicker_drawNamedPalette()
  169. {
  170. //create named palette array if it doesn't already exist
  171. if (this.m_oNamedColors == null)
  172. {
  173. this.m_oNamedColors = this.m_oNamedColor.getNamedColors();
  174. }
  175. var iRowCount = Math.floor(this.m_oNamedColors.length / 2);
  176. var s = '<table id="'+this.m_sPromptId+'_'+PALETTE_NAMED+'" cellpadding="0" cellspacing="0" border="0" width="100%" style="display:none" onclick="CColorPicker_preventBubbling(event);">';
  177. //default
  178. s += '<tr><td><table cellpadding="0" cellspacing="3" border="0" width="100%">';
  179. s += '<tr><td id="'+this.m_sPromptId+'_namedColor0" title="'+ this.m_oNamedColors[0].getName() +'" onclick="'+this.m_sRef +'.setColor(\''+ this.m_oNamedColors[0].getColor() +'\')" class="clsCPItem" onMouseOver="'+this.m_sRef+'.buttonMouseHandler(this, \'over\');" onMouseOut="'+this.m_sRef+'.buttonMouseHandler(this, \'out\');" onMouseDown="'+this.m_sRef+'.buttonMouseHandler(this, \'down\');">';
  180. s += '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
  181. s += '<tr><td valign="top" width="15">';
  182. s += '<table cellpadding="0" cellspacing="0" border="0"><tr>';
  183. s += '<td id="'+this.m_sPromptId+'_namedColorSwatch'+0+'" class="clsCPCell" style="background-color:'+ this.m_oNamedColors[0].getColor() + '">';
  184. s += '<img src="'+ this.m_sImgPath + 'default_color.gif" height="11" width="11" border="0" title="'+ this.m_oNamedColors[0].getName() +'"></td>';
  185. s += '</tr></table></td>';
  186. s += '<td class="clsCPLink" style="padding-left:5px;padding-right:30px;white-space:nowrap;">' + this.m_oNamedColors[0].getName();
  187. s += '</td></tr></table>';
  188. s += '</td><td/></tr></table>';
  189. s += '<tr><td valign="top" width="50%"><table cellpadding="0" cellspacing="3" border="0" width="100%">';
  190. for (var j=1; j < this.m_oNamedColors.length; j++)
  191. {
  192. s += '<tr><td id="'+this.m_sPromptId+'_namedColor'+j+'" title="'+ this.m_oNamedColors[j].getColor() +'" onclick="'+this.m_sRef +'.setColor(\''+ this.m_oNamedColors[j].getColor() +'\')" class="clsCPItem" onMouseOver="'+this.m_sRef+'.buttonMouseHandler(this, \'over\');" onMouseOut="'+this.m_sRef+'.buttonMouseHandler(this, \'out\');" onMouseDown="'+this.m_sRef+'.buttonMouseHandler(this, \'down\');">';
  193. s += '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
  194. s += '<tr><td valign="top" width="15">';
  195. s += '<table cellpadding="0" cellspacing="0" border="0"><tr>';
  196. s += '<td id="'+this.m_sPromptId+'_namedColorSwatch'+j+'" class="clsCPCell" style="background-color:'+ this.m_oNamedColors[j].getColor() + '">';
  197. s += '<img src="'+ this.m_sImgPath + 'spacer.gif" height="11" width="11" border="0" title="'+ this.m_oNamedColors[j].getColor() +'"></td>';
  198. s += '</tr></table></td>';
  199. s += '<td class="clsCPLink" style="padding-left:5px;padding-right:30px;white-space:nowrap;">' + this.m_oNamedColors[j].getName();
  200. s += '</td></tr></table>';
  201. if (j==iRowCount)
  202. {
  203. s += '</td></tr></table></td><td valign="top" width="50%"><table cellpadding="0" cellspacing="3" border="0" width="100%">';
  204. }
  205. else
  206. {
  207. s += '</td></tr>';
  208. }
  209. }
  210. s += '</td></tr></table></td></tr></table>';
  211. this.m_oDialogPalette.innerHTML += s;
  212. }
  213. //web safe palette dialog
  214. function CColorPicker_drawWebsafePalette()
  215. {
  216. var s = '<table id="'+this.m_sPromptId+'_'+PALETTE_WEB+'" cellpadding="0" cellspacing="0" border="0" style="display:none" onclick="CColorPicker_preventBubbling(event);">';
  217. var v_aColorBytes = new Array( "00", "33", "66", "99", "CC", "FF" );
  218. var v_iNumberOfBytes = v_aColorBytes.length;
  219. var iCounter = 1;
  220. s+= '<tr>';
  221. for ( var r = 0; r < v_iNumberOfBytes; r++ )
  222. {
  223. for ( var g = 0; g < v_iNumberOfBytes; g++ )
  224. {
  225. for ( var b = 0; b < v_iNumberOfBytes; b++ )
  226. {
  227. var v_sRGB = "#" + v_aColorBytes[r] + v_aColorBytes[g] + v_aColorBytes[b];
  228. s+= '<td id="'+this.m_sPromptId+'_ws_'+ v_aColorBytes[r] + v_aColorBytes[g] + v_aColorBytes[b] +'"style="background-color:' + v_sRGB + '" class="clsCPCell" onMouseOver="'+this.m_sRef+'.buttonMouseHandler(this, \'over\');" onMouseOut="'+this.m_sRef+'.buttonMouseHandler(this, \'out\');" onMouseDown="'+this.m_sRef+'.buttonMouseHandler(this, \'down\');" onclick="' + this.m_sRef +'.setColor(\''+ v_sRGB +'\');return false"><img src="'+ this.m_sImgPath +'spacer.gif" height="15" width="15" border="0" title="'+ v_sRGB +'">';
  229. s+= '</td>';
  230. if (iCounter % 18 === 0) { s+= '</tr><tr>'; }
  231. iCounter ++;
  232. }
  233. }
  234. }
  235. s+= '</tr>';
  236. s += '</table>';
  237. this.m_oDialogPalette.innerHTML += s;
  238. }
  239. function CColorPicker_setActiveColor(s)
  240. {
  241. this.setActiveClass(false,false);
  242. this.m_sColor = parseRGB(s);
  243. this.setActiveClass(true,true);
  244. }
  245. function CColorPicker_setActiveClass(bActive, bSetPalette)
  246. {
  247. var classSuffix = (bActive ? '_active' : '');
  248. var bNamed = false;
  249. var bIsWebSafe = false;
  250. var idx = this.m_oNamedColor.indexOf(this.m_sColor);
  251. //if it is a named color
  252. if (idx >= 0)
  253. {
  254. if (bSetPalette) {
  255. this.setPalette(PALETTE_NAMED);
  256. }
  257. var nc = document.getElementById(this.m_sPromptId+'_namedColor'+idx);
  258. if (nc != null) {
  259. nc.className = 'clsCPItem' + classSuffix;
  260. }
  261. var ncs = document.getElementById(this.m_sPromptId+'_namedColorSwatch'+idx);
  262. if (ncs != null) {
  263. ncs.className = 'clsCPCell' + classSuffix;
  264. }
  265. bNamed = true;
  266. }
  267. if (this.m_sColor.indexOf('#') === 0 && this.m_sColor.length == 7)
  268. {
  269. for (var i=1; i < this.m_sColor.length; i+=2)
  270. {
  271. var ch1 = this.m_sColor.charAt(i).toLowerCase();
  272. var ch2 = this.m_sColor.charAt(i+1).toLowerCase();
  273. if (ch1 == ch2 && (ch1 == '0' || ch1 == '3' || ch1 == '6' || ch1 == '9' || ch1 == 'c' || ch1 == 'f')) {
  274. bIsWebSafe = true;
  275. }
  276. else
  277. {
  278. bIsWebSafe = false;
  279. break;
  280. }
  281. }
  282. if (bIsWebSafe)
  283. {
  284. //find color on websafe palette and activate it
  285. var wsc = document.getElementById(this.m_sPromptId+'_ws_'+this.m_sColor.substring(1));
  286. if (wsc != null) {
  287. wsc.className = 'clsCPCell' + classSuffix;
  288. }
  289. if (!bNamed && bSetPalette) {
  290. this.setPalette(PALETTE_WEB);
  291. }
  292. }
  293. }
  294. if (!bNamed && !bIsWebSafe && bSetPalette) {
  295. this.setPalette(PALETTE_CUSTOM);
  296. }
  297. //fill in custom color palette
  298. var oR = document.getElementById(this.m_sCustomR);
  299. var oG = document.getElementById(this.m_sCustomG);
  300. var oB = document.getElementById(this.m_sCustomB);
  301. if (oR != null && oG != null && oB != null)
  302. {
  303. if (this.m_sColor.length == 7)
  304. {
  305. oR.value = getR(this.m_sColor);
  306. oG.value = getG(this.m_sColor);
  307. oB.value = getB(this.m_sColor);
  308. }
  309. else
  310. {
  311. oR.value = '';
  312. oG.value = '';
  313. oB.value = '';
  314. }
  315. this.updateCustomStyle();
  316. }
  317. }
  318. //apply the user selected color
  319. function CColorPicker_setColor(s)
  320. {
  321. if (this.m_sColor != s)
  322. {
  323. this.setActiveColor(s);
  324. if (typeof this.m_sCallBackFunction == 'string' && this.m_sCallBackFunction !== null && this.m_sCallBackFunction !== "")
  325. {
  326. var theCommand = this.m_sCallBackFunction + '("' + this.m_sColor + '")';
  327. eval (theCommand);
  328. }
  329. }
  330. //hide the picker
  331. this.hide();
  332. if (this.m_bAutoReset) {
  333. this.setActiveColor('');
  334. }
  335. }
  336. //update the color picker with the new user selection
  337. function CColorPicker_updateCustomStyle()
  338. {
  339. var oR = document.getElementById(this.m_sCustomR);
  340. var oG = document.getElementById(this.m_sCustomG);
  341. var oB = document.getElementById(this.m_sCustomB);
  342. if ((typeof oR != 'undefined') && (typeof oG != 'undefined') && (typeof oB != 'undefined'))
  343. {
  344. var s = parseRGB("#" + oR.value + oG.value + oB.value);
  345. var oCustomPreview = document.getElementById(this.m_sCustomPreview);
  346. if (typeof oCustomPreview != 'undefined' && s.length == 7 || s == '')
  347. {
  348. oCustomPreview.style.backgroundColor = s;
  349. return s;
  350. }
  351. }
  352. return '';
  353. }
  354. //take a user specified custom color and apply it
  355. function CColorPicker_applyCustomStyle()
  356. {
  357. this.setColor(this.updateCustomStyle());
  358. }
  359. //get the current color for the dialog
  360. function CColorPicker_getColor()
  361. {
  362. return this.m_sColor;
  363. }
  364. //set a new palette
  365. function CColorPicker_setPalette(s)
  366. {
  367. if (this.m_sSelectedPalette != s)
  368. {
  369. this.m_sSelectedPalette = s;
  370. this.drawPalette();
  371. var palette = null;
  372. palette = document.getElementById(this.m_sPromptId+'_'+PALETTE_NAMED);
  373. if (palette != null) { palette.style.display = 'none'; }
  374. palette = document.getElementById(this.m_sPromptId+'_'+PALETTE_WEB);
  375. if (palette != null) { palette.style.display = 'none'; }
  376. palette = document.getElementById(this.m_sPromptId+'_'+PALETTE_CUSTOM);
  377. if (palette != null) { palette.style.display = 'none'; }
  378. palette = document.getElementById(this.m_sPromptId+'_'+this.m_sSelectedPalette);
  379. if (palette != null) { palette.style.display = (window.ie ? 'block' : 'table'); }
  380. //set the drop down
  381. var palSel = document.getElementById(this.m_sPromptId+'_select');
  382. if (palSel != null)
  383. {
  384. for (var i=0; i < palSel.options.length; i++)
  385. {
  386. if (palSel.options[i].value == this.m_sSelectedPalette)
  387. {
  388. palSel.selectedIndex = i;
  389. break;
  390. }
  391. }
  392. }
  393. }
  394. }
  395. //switch that will hide and show the picker dialog
  396. function CColorPicker_togglePicker(e)
  397. {
  398. if (this.m_oDialog.style.display == "inline")
  399. {
  400. this.hide();
  401. }
  402. else
  403. {
  404. this.show();
  405. }
  406. this.preventBubbling(e);
  407. }
  408. function CColorPicker_preventBubbling(e)
  409. {
  410. if (typeof e == "object")
  411. {
  412. if (window.ie)
  413. {
  414. e.returnValue = false;
  415. e.cancelBubble = true;
  416. }
  417. else
  418. {
  419. e.preventDefault();
  420. e.stopPropagation();
  421. }
  422. }
  423. }
  424. // show the picker dialog
  425. function CColorPicker_show()
  426. {
  427. //hide any other pickers that might be visible
  428. if ((typeof hidePickers != 'undefined') && (hidePickers))
  429. {
  430. hidePickers();
  431. }
  432. this.m_oDialog.style.zIndex = '3';
  433. this.m_oDialog.style.display = 'inline';
  434. var iframe = document.getElementById('iframe'+this.m_sDialogId);
  435. if (iframe != null)
  436. {
  437. iframe.style.zIndex = '2';
  438. iframe.style.width = this.m_oDialog.offsetWidth + "px";
  439. iframe.style.height = this.m_oDialog.offsetHeight + "px";
  440. iframe.style.display = 'inline';
  441. }
  442. }
  443. //hide the picker dialog
  444. function CColorPicker_hide()
  445. {
  446. var iframe = document.getElementById('iframe'+this.m_sDialogId);
  447. if (iframe != null) {
  448. iframe.style.zIndex = '1';
  449. iframe.style.display = 'none';
  450. }
  451. this.m_oDialog.style.display = 'none';
  452. this.m_oDialog.style.zIndex = '1';
  453. this.setActiveClass(true,true);
  454. }
  455. function CColorPicker_buttonMouseHandler(button, action)
  456. {
  457. var namedColor = 'namedColor';
  458. var idx = button.id.indexOf(namedColor);
  459. if (idx >= 0)
  460. {
  461. idx = button.id.substring(idx+namedColor.length);
  462. if (this.m_oNamedColor.indexOf(this.m_sColor) == parseInt(idx,10))
  463. {
  464. if (action == 'over' || action == 'out' || action == 'down') {
  465. button.className = 'clsCPItem_active';
  466. }
  467. }
  468. else
  469. {
  470. if (action == 'over') {
  471. button.className = 'clsCPItem_hover';
  472. }
  473. else if (action == 'out') {
  474. button.className = 'clsCPItem';
  475. }
  476. else if (action == 'down') {
  477. button.className = 'clsCPItem_active';
  478. }
  479. }
  480. }
  481. var cell = null;
  482. var bNamed = false;
  483. if (idx > -1)
  484. {
  485. cell = document.getElementById(this.m_sPromptId+'_namedColorSwatch'+idx);
  486. bNamed = true;
  487. }
  488. else {
  489. cell = button;
  490. }
  491. if ((bNamed && this.m_oNamedColor.indexOf(this.m_sColor) == idx) || (!bNamed && this.m_sColor != '' && cell.id.indexOf(this.m_sColor.substring(1)) >= 0))
  492. {
  493. if (action == 'over' || action == 'out' || action == 'down') {
  494. cell.className = 'clsCPCell_active';
  495. }
  496. }
  497. else
  498. {
  499. if (action == 'over') {
  500. cell.className = 'clsCPCell_hover';
  501. }
  502. else if (action == 'out') {
  503. cell.className = 'clsCPCell';
  504. }
  505. else if (action == 'down') {
  506. cell.className = 'clsCPCell_active';
  507. }
  508. }
  509. }
  510. //update associated swatch control
  511. function CColorPicker_updateSwatch(s)
  512. {
  513. var oSwatch = document.getElementById('selectColorSwatch' + this.m_sPromptId);
  514. if (oSwatch) {
  515. s = parseRGB(s);
  516. if (s == '') {
  517. oSwatch.className = "colorSwatchDefault";
  518. }
  519. else if (s == "#FFFFFF") {
  520. oSwatch.className = "colorSwatchWhite";
  521. }
  522. else {
  523. oSwatch.className = "";
  524. }
  525. oSwatch.style.backgroundColor = s;
  526. var sNamedColor = s;
  527. var idx = this.m_oNamedColor.indexOf(sNamedColor);
  528. if (idx >= 0 ) {
  529. sNamedColor = this.m_oNamedColor.m_Colors[idx].getName();
  530. }
  531. oSwatch.title = (sNamedColor == '' ? PMT_CP_COLOR_DEFAULT : sNamedColor);
  532. }
  533. // Update the input fields with the same name.
  534. for (var i = 0; i < document.forms.length; i++) {
  535. var o = eval('document.forms[' + i + '].' + this.m_sPromptId);
  536. if (o) {
  537. o.value = s;
  538. }
  539. }
  540. }
  541. CColorPicker.prototype.init = CColorPicker_init;
  542. CColorPicker.prototype.draw = CColorPicker_draw;
  543. CColorPicker.prototype.drawCaptionBar = CColorPicker_drawCaptionBar;
  544. CColorPicker.prototype.drawPalette = CColorPicker_drawPalette;
  545. CColorPicker.prototype.drawNamedPalette = CColorPicker_drawNamedPalette;
  546. CColorPicker.prototype.drawWebsafePalette = CColorPicker_drawWebsafePalette;
  547. CColorPicker.prototype.drawCustomPalette = CColorPicker_drawCustomPalette;
  548. CColorPicker.prototype.setPalette = CColorPicker_setPalette;
  549. CColorPicker.prototype.setActiveColor = CColorPicker_setActiveColor;
  550. CColorPicker.prototype.setActiveClass = CColorPicker_setActiveClass;
  551. CColorPicker.prototype.setColor = CColorPicker_setColor;
  552. CColorPicker.prototype.getColor = CColorPicker_getColor;
  553. CColorPicker.prototype.togglePicker = CColorPicker_togglePicker;
  554. CColorPicker.prototype.hide = CColorPicker_hide;
  555. CColorPicker.prototype.show = CColorPicker_show;
  556. CColorPicker.prototype.applyCustomStyle = CColorPicker_applyCustomStyle;
  557. CColorPicker.prototype.updateCustomStyle = CColorPicker_updateCustomStyle;
  558. CColorPicker.prototype.updateSwatch = CColorPicker_updateSwatch;
  559. CColorPicker.prototype.preventBubbling = CColorPicker_preventBubbling;
  560. CColorPicker.prototype.buttonMouseHandler = CColorPicker_buttonMouseHandler;
  561. //CNamedColor: named colors class
  562. // this class is used to translated between named colors and RGB values
  563. function CNamedColor()
  564. {
  565. this.m_Colors = new Array();
  566. this.init();
  567. }
  568. // initialize the lsit of named colors
  569. function CNamedColor_init()
  570. {
  571. this.add(PMT_CP_COLOR_DEFAULT, '');
  572. this.add(PMT_CP_COLOR_BLACK, '#000000');
  573. this.add(PMT_CP_COLOR_SILVER, '#C0C0C0');
  574. this.add(PMT_CP_COLOR_GRAY, '#808080');
  575. this.add(PMT_CP_COLOR_WHITE, '#FFFFFF');
  576. this.add(PMT_CP_COLOR_MAROON, '#800000');
  577. this.add(PMT_CP_COLOR_RED, '#FF0000');
  578. this.add(PMT_CP_COLOR_PURPLE, '#800080');
  579. this.add(PMT_CP_COLOR_FUSHIA, '#FF00FF');
  580. this.add(PMT_CP_COLOR_GREEN, '#008000');
  581. this.add(PMT_CP_COLOR_LIME, '#00FF00');
  582. this.add(PMT_CP_COLOR_OLIVE, '#808000');
  583. this.add(PMT_CP_COLOR_YELLOW, '#FFFF00');
  584. this.add(PMT_CP_COLOR_NAVY, '#000080');
  585. this.add(PMT_CP_COLOR_BLUE, '#0000FF');
  586. this.add(PMT_CP_COLOR_TEAL, '#008080');
  587. this.add(PMT_CP_COLOR_AQUA, '#00FFFF');
  588. }
  589. // return the array of color objects
  590. function CNamedColor_getNamedColors()
  591. {
  592. return this.m_Colors;
  593. }
  594. //add a new color object
  595. function CNamedColor_add(sName,sColor)
  596. {
  597. var c = new CColor (sName,sColor);
  598. this.m_Colors = this.m_Colors.concat(c);
  599. }
  600. //get the index of a named color
  601. function CNamedColor_indexOf(sColor)
  602. {
  603. for (var i=0; i < this.m_Colors.length; i++)
  604. {
  605. if (sColor == this.m_Colors[i].getColor()) {
  606. return i;
  607. }
  608. }
  609. return -1;
  610. }
  611. CNamedColor.prototype.init = CNamedColor_init;
  612. CNamedColor.prototype.getNamedColors = CNamedColor_getNamedColors;
  613. CNamedColor.prototype.add = CNamedColor_add;
  614. CNamedColor.prototype.indexOf = CNamedColor_indexOf;
  615. //CColor: Color class
  616. // used to store names and color values for a color
  617. function CColor(sName,sColor)
  618. {
  619. this.m_sName = sName;
  620. this.m_sRGBColor = sColor;
  621. }
  622. function CColor_getName()
  623. {
  624. return this.m_sName;
  625. }
  626. function CColor_getColor()
  627. {
  628. return this.m_sRGBColor;
  629. }
  630. CColor.prototype.getName = CColor_getName;
  631. CColor.prototype.getColor = CColor_getColor;
  632. //color utility functions
  633. function parseRGB(s)
  634. {
  635. if (s == null) {
  636. s = '';
  637. }
  638. s = s.toUpperCase();
  639. //take a string and return a valid color string
  640. if (s.length == 7 && s.indexOf('#') === 0)
  641. {
  642. if (!(/[^0-9A-F#]/gi).test(s)) {
  643. return s;
  644. }
  645. }
  646. else if (s.match(/RGB\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/gi)) {
  647. // convert 'rbg(255,255,255)' to '#FFFFFF'
  648. var r = RegExp.$1;
  649. var g = RegExp.$2;
  650. var b = RegExp.$3;
  651. return "#" + numberToHex(r) + numberToHex(g) + numberToHex(b);
  652. }
  653. return '';
  654. }
  655. //return the hex value for a given color
  656. function getR(sColor)
  657. {
  658. var s = parseRGB(sColor);
  659. var sHex = s.slice(1,3);
  660. return sHex;
  661. }
  662. function getG(sColor)
  663. {
  664. var s = parseRGB(sColor);
  665. var sHex = s.slice(3,5);
  666. return sHex;
  667. }
  668. function getB(sColor)
  669. {
  670. var s = parseRGB(sColor);
  671. var sHex = s.slice(5,7);
  672. return sHex;
  673. }
  674. function numberToHex(d)
  675. {
  676. var hex = '';
  677. var i = Math.floor(d / 16);
  678. hex += digitToHex(i);
  679. i = (d % 16);
  680. hex += digitToHex(i);
  681. return hex;
  682. }
  683. function digitToHex(d)
  684. {
  685. switch (d) {
  686. case 0:
  687. case 1:
  688. case 2:
  689. case 3:
  690. case 4:
  691. case 5:
  692. case 6:
  693. case 7:
  694. case 8:
  695. case 9:
  696. return ('' + d);
  697. case 10:
  698. return 'A';
  699. case 11:
  700. return 'B';
  701. case 12:
  702. return 'C';
  703. case 13:
  704. return 'D';
  705. case 14:
  706. return 'E';
  707. case 15:
  708. return 'F';
  709. }
  710. return '0';
  711. }
  712. // CONSTANTS
  713. var PALETTE_NAMED = 'named';
  714. var PALETTE_WEB = 'web';
  715. var PALETTE_CUSTOM = 'custom';
  716. var TYPE_BACKGROUND_COLOR = 'background-color';
  717. var TYPE_COLOR = 'color';