/javascripts/lib/src/widgets/ColorPalette.js

https://bitbucket.org/ksokmesa/sina-asian · JavaScript · 172 lines · 68 code · 10 blank · 94 comment · 11 complexity · f761173287492acd7462fa99fdb0455a MD5 · raw file

  1. /*!
  2. * Ext JS Library 3.2.1
  3. * Copyright(c) 2006-2010 Ext JS, Inc.
  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 = Ext.extend(Ext.Component, {
  25. /**
  26. * @cfg {String} tpl An existing XTemplate instance to be used in place of the default template for rendering the component.
  27. */
  28. /**
  29. * @cfg {String} itemCls
  30. * The CSS class to apply to the containing element (defaults to 'x-color-palette')
  31. */
  32. itemCls : 'x-color-palette',
  33. /**
  34. * @cfg {String} value
  35. * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol). Note that
  36. * the hex codes are case-sensitive.
  37. */
  38. value : null,
  39. /**
  40. * @cfg {String} clickEvent
  41. * The DOM event that will cause a color to be selected. This can be any valid event name (dblclick, contextmenu).
  42. * Defaults to <tt>'click'</tt>.
  43. */
  44. clickEvent :'click',
  45. // private
  46. ctype : 'Ext.ColorPalette',
  47. /**
  48. * @cfg {Boolean} allowReselect If set to true then reselecting a color that is already selected fires the {@link #select} event
  49. */
  50. allowReselect : false,
  51. /**
  52. * <p>An array of 6-digit color hex code strings (without the # symbol). This array can contain any number
  53. * of colors, and each hex code should be unique. The width of the palette is controlled via CSS by adjusting
  54. * the width property of the 'x-color-palette' class (or assigning a custom class), so you can balance the number
  55. * of colors with the width setting until the box is symmetrical.</p>
  56. * <p>You can override individual colors if needed:</p>
  57. * <pre><code>
  58. var cp = new Ext.ColorPalette();
  59. cp.colors[0] = 'FF0000'; // change the first box to red
  60. </code></pre>
  61. Or you can provide a custom array of your own for complete control:
  62. <pre><code>
  63. var cp = new Ext.ColorPalette();
  64. cp.colors = ['000000', '993300', '333300'];
  65. </code></pre>
  66. * @type Array
  67. */
  68. colors : [
  69. '000000', '993300', '333300', '003300', '003366', '000080', '333399', '333333',
  70. '800000', 'FF6600', '808000', '008000', '008080', '0000FF', '666699', '808080',
  71. 'FF0000', 'FF9900', '99CC00', '339966', '33CCCC', '3366FF', '800080', '969696',
  72. 'FF00FF', 'FFCC00', 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0',
  73. 'FF99CC', 'FFCC99', 'FFFF99', 'CCFFCC', 'CCFFFF', '99CCFF', 'CC99FF', 'FFFFFF'
  74. ],
  75. /**
  76. * @cfg {Function} handler
  77. * Optional. A function that will handle the select event of this palette.
  78. * The handler is passed the following parameters:<div class="mdetail-params"><ul>
  79. * <li><code>palette</code> : ColorPalette<div class="sub-desc">The {@link #palette Ext.ColorPalette}.</div></li>
  80. * <li><code>color</code> : String<div class="sub-desc">The 6-digit color hex code (without the # symbol).</div></li>
  81. * </ul></div>
  82. */
  83. /**
  84. * @cfg {Object} scope
  85. * The scope (<tt><b>this</b></tt> reference) in which the <code>{@link #handler}</code>
  86. * function will be called. Defaults to this ColorPalette instance.
  87. */
  88. // private
  89. initComponent : function(){
  90. Ext.ColorPalette.superclass.initComponent.call(this);
  91. this.addEvents(
  92. /**
  93. * @event select
  94. * Fires when a color is selected
  95. * @param {ColorPalette} this
  96. * @param {String} color The 6-digit color hex code (without the # symbol)
  97. */
  98. 'select'
  99. );
  100. if(this.handler){
  101. this.on('select', this.handler, this.scope, true);
  102. }
  103. },
  104. // private
  105. onRender : function(container, position){
  106. this.autoEl = {
  107. tag: 'div',
  108. cls: this.itemCls
  109. };
  110. Ext.ColorPalette.superclass.onRender.call(this, container, position);
  111. var t = this.tpl || new Ext.XTemplate(
  112. '<tpl for="."><a href="#" class="color-{.}" hidefocus="on"><em><span style="background:#{.}" unselectable="on">&#160;</span></em></a></tpl>'
  113. );
  114. t.overwrite(this.el, this.colors);
  115. this.mon(this.el, this.clickEvent, this.handleClick, this, {delegate: 'a'});
  116. if(this.clickEvent != 'click'){
  117. this.mon(this.el, 'click', Ext.emptyFn, this, {delegate: 'a', preventDefault: true});
  118. }
  119. },
  120. // private
  121. afterRender : function(){
  122. Ext.ColorPalette.superclass.afterRender.call(this);
  123. if(this.value){
  124. var s = this.value;
  125. this.value = null;
  126. this.select(s, true);
  127. }
  128. },
  129. // private
  130. handleClick : function(e, t){
  131. e.preventDefault();
  132. if(!this.disabled){
  133. var c = t.className.match(/(?:^|\s)color-(.{6})(?:\s|$)/)[1];
  134. this.select(c.toUpperCase());
  135. }
  136. },
  137. /**
  138. * Selects the specified color in the palette (fires the {@link #select} event)
  139. * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
  140. * @param {Boolean} suppressEvent (optional) True to stop the select event from firing. Defaults to <tt>false</tt>.
  141. */
  142. select : function(color, suppressEvent){
  143. color = color.replace('#', '');
  144. if(color != this.value || this.allowReselect){
  145. var el = this.el;
  146. if(this.value){
  147. el.child('a.color-'+this.value).removeClass('x-color-palette-sel');
  148. }
  149. el.child('a.color-'+color).addClass('x-color-palette-sel');
  150. this.value = color;
  151. if(suppressEvent !== true){
  152. this.fireEvent('select', this, color);
  153. }
  154. }
  155. }
  156. /**
  157. * @cfg {String} autoEl @hide
  158. */
  159. });
  160. Ext.reg('colorpalette', Ext.ColorPalette);