color_field_widget_box.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * @file
  3. * Color Field jQuery.
  4. */
  5. (function ($) {
  6. jQuery.fn.addColorPicker = function (props) {
  7. 'use strict';
  8. if (!props) {
  9. props = [];
  10. }
  11. props = jQuery.extend({
  12. currentColor:'',
  13. blotchElemType: 'button',
  14. blotchClass:'colorBox',
  15. blotchTransparentClass:'transparentBox',
  16. addTransparentBlotch: true,
  17. clickCallback: function (ignoredColor) {},
  18. iterationCallback: null,
  19. fillString: ' ',
  20. fillStringX: '?',
  21. colors: [
  22. '#AC725E','#D06B64','#F83A22', '#FA573C', '#FF7537', '#FFAD46',
  23. '#42D692','#16A765', '#7BD148','#B3DC6C','#FBE983',
  24. '#92E1C0', '#9FE1E7', '#9FC6E7', '#4986E7','#9A9CFF',
  25. '#B99AFF','#C2C2C2','#CABDBF','#CCA6AC','#F691B2',
  26. '#CD74E6','#A47AE2',
  27. ]
  28. }, props);
  29. this.addBlotchElement = function(color, blotchClass) {
  30. var elem = jQuery('<' + props.blotchElemType + '/>')
  31. .addClass(blotchClass)
  32. .attr('color',color)
  33. .attr('title', color)
  34. .css('background-color',color);
  35. // Jq bug: chaining here fails if color is null b/c .css() returns (new String('transparent'))!
  36. if (props.currentColor == color) {
  37. elem.addClass('active');
  38. }
  39. if (props.clickCallback) {
  40. elem.click(function (event) {
  41. event.preventDefault();
  42. jQuery(this).parent().children().removeClass('active');
  43. jQuery(this).addClass('active');
  44. props.clickCallback(jQuery(this).attr('color'));
  45. });
  46. }
  47. this.append(elem);
  48. if (props.iterationCallback) {
  49. props.iterationCallback(this, elem, color, i);
  50. }
  51. }
  52. for (var i = 0; i < props.colors.length; ++i) {
  53. var color = props.colors[i];
  54. this.addBlotchElement(color, props.blotchClass);
  55. }
  56. if (props.addTransparentBlotch) {
  57. this.addBlotchElement('', props.blotchTransparentClass);
  58. }
  59. return this;
  60. };
  61. })(jQuery);