/hippo/src/main/webapp/ext/src/widgets/ColorPalette.js

http://hdbc.googlecode.com/ · JavaScript · 148 lines · 66 code · 8 blank · 74 comment · 10 complexity · 2095b113d4ce04dc2550101ef4545be2 MD5 · raw file

  1. /*!
  2. * Ext JS Library 3.0.0
  3. * Copyright(c) 2006-2009 Ext JS, LLC
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /**
  8. * @class Ext.ColorPalette
  9. * @extends Ext.Component
  10. * Simple color palette class for choosing colors. The palette can be rendered to any container.<br />
  11. * Here's an example of typical usage:
  12. * <pre><code>
  13. var cp = new Ext.ColorPalette({value:'993300'}); // initial selected color
  14. cp.render('my-div');
  15. cp.on('select', function(palette, selColor){
  16. // do something with selColor
  17. });
  18. </code></pre>
  19. * @constructor
  20. * Create a new ColorPalette
  21. * @param {Object} config The config object
  22. * @xtype colorpalette
  23. */
  24. Ext.ColorPalette = function(config){
  25. Ext.ColorPalette.superclass.constructor.call(this, config);
  26. this.addEvents(
  27. /**
  28. * @event select
  29. * Fires when a color is selected
  30. * @param {ColorPalette} this
  31. * @param {String} color The 6-digit color hex code (without the # symbol)
  32. */
  33. 'select'
  34. );
  35. if(this.handler){
  36. this.on("select", this.handler, this.scope, true);
  37. }
  38. };
  39. Ext.extend(Ext.ColorPalette, Ext.Component, {
  40. /**
  41. * @cfg {String} tpl An existing XTemplate instance to be used in place of the default template for rendering the component.
  42. */
  43. /**
  44. * @cfg {String} itemCls
  45. * The CSS class to apply to the containing element (defaults to "x-color-palette")
  46. */
  47. itemCls : "x-color-palette",
  48. /**
  49. * @cfg {String} value
  50. * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol). Note that
  51. * the hex codes are case-sensitive.
  52. */
  53. value : null,
  54. clickEvent:'click',
  55. // private
  56. ctype: "Ext.ColorPalette",
  57. /**
  58. * @cfg {Boolean} allowReselect If set to true then reselecting a color that is already selected fires the {@link #select} event
  59. */
  60. allowReselect : false,
  61. /**
  62. * <p>An array of 6-digit color hex code strings (without the # symbol). This array can contain any number
  63. * of colors, and each hex code should be unique. The width of the palette is controlled via CSS by adjusting
  64. * the width property of the 'x-color-palette' class (or assigning a custom class), so you can balance the number
  65. * of colors with the width setting until the box is symmetrical.</p>
  66. * <p>You can override individual colors if needed:</p>
  67. * <pre><code>
  68. var cp = new Ext.ColorPalette();
  69. cp.colors[0] = "FF0000"; // change the first box to red
  70. </code></pre>
  71. Or you can provide a custom array of your own for complete control:
  72. <pre><code>
  73. var cp = new Ext.ColorPalette();
  74. cp.colors = ["000000", "993300", "333300"];
  75. </code></pre>
  76. * @type Array
  77. */
  78. colors : [
  79. "000000", "993300", "333300", "003300", "003366", "000080", "333399", "333333",
  80. "800000", "FF6600", "808000", "008000", "008080", "0000FF", "666699", "808080",
  81. "FF0000", "FF9900", "99CC00", "339966", "33CCCC", "3366FF", "800080", "969696",
  82. "FF00FF", "FFCC00", "FFFF00", "00FF00", "00FFFF", "00CCFF", "993366", "C0C0C0",
  83. "FF99CC", "FFCC99", "FFFF99", "CCFFCC", "CCFFFF", "99CCFF", "CC99FF", "FFFFFF"
  84. ],
  85. // private
  86. onRender : function(container, position){
  87. var t = this.tpl || new Ext.XTemplate(
  88. '<tpl for="."><a href="#" class="color-{.}" hidefocus="on"><em><span style="background:#{.}" unselectable="on">&#160;</span></em></a></tpl>'
  89. );
  90. var el = document.createElement("div");
  91. el.id = this.getId();
  92. el.className = this.itemCls;
  93. t.overwrite(el, this.colors);
  94. container.dom.insertBefore(el, position);
  95. this.el = Ext.get(el);
  96. this.mon(this.el, this.clickEvent, this.handleClick, this, {delegate: 'a'});
  97. if(this.clickEvent != 'click'){
  98. this.mon(this.el, 'click', Ext.emptyFn, this, {delegate: 'a', preventDefault: true});
  99. }
  100. },
  101. // private
  102. afterRender : function(){
  103. Ext.ColorPalette.superclass.afterRender.call(this);
  104. if(this.value){
  105. var s = this.value;
  106. this.value = null;
  107. this.select(s);
  108. }
  109. },
  110. // private
  111. handleClick : function(e, t){
  112. e.preventDefault();
  113. if(!this.disabled){
  114. var c = t.className.match(/(?:^|\s)color-(.{6})(?:\s|$)/)[1];
  115. this.select(c.toUpperCase());
  116. }
  117. },
  118. /**
  119. * Selects the specified color in the palette (fires the {@link #select} event)
  120. * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
  121. */
  122. select : function(color){
  123. color = color.replace("#", "");
  124. if(color != this.value || this.allowReselect){
  125. var el = this.el;
  126. if(this.value){
  127. el.child("a.color-"+this.value).removeClass("x-color-palette-sel");
  128. }
  129. el.child("a.color-"+color).addClass("x-color-palette-sel");
  130. this.value = color;
  131. this.fireEvent("select", this, color);
  132. }
  133. }
  134. /**
  135. * @cfg {String} autoEl @hide
  136. */
  137. });
  138. Ext.reg('colorpalette', Ext.ColorPalette);