Color2.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-picker-Color'>/**
  19. </span> * Color picker provides a simple color palette for choosing colors. The picker can be rendered to any container. The
  20. * available default to a standard 40-color palette; this can be customized with the {@link #colors} config.
  21. *
  22. * Typically you will need to implement a handler function to be notified when the user chooses a color from the picker;
  23. * you can register the handler using the {@link #event-select} event, or by implementing the {@link #handler} method.
  24. *
  25. * @example
  26. * Ext.create('Ext.picker.Color', {
  27. * value: '993300', // initial selected color
  28. * renderTo: Ext.getBody(),
  29. * listeners: {
  30. * select: function(picker, selColor) {
  31. * alert(selColor);
  32. * }
  33. * }
  34. * });
  35. */
  36. Ext.define('Ext.picker.Color', {
  37. extend: 'Ext.Component',
  38. requires: 'Ext.XTemplate',
  39. alias: 'widget.colorpicker',
  40. alternateClassName: 'Ext.ColorPalette',
  41. <span id='Ext-picker-Color-cfg-componentCls'> /**
  42. </span> * @cfg {String} [componentCls='x-color-picker']
  43. * The CSS class to apply to the containing element.
  44. */
  45. componentCls : Ext.baseCSSPrefix + 'color-picker',
  46. <span id='Ext-picker-Color-cfg-selectedCls'> /**
  47. </span> * @cfg {String} [selectedCls='x-color-picker-selected']
  48. * The CSS class to apply to the selected element
  49. */
  50. selectedCls: Ext.baseCSSPrefix + 'color-picker-selected',
  51. <span id='Ext-picker-Color-cfg-value'> /**
  52. </span> * @cfg {String} value
  53. * The initial color to highlight (should be a valid 6-digit color hex code without the # symbol). Note that the hex
  54. * codes are case-sensitive.
  55. */
  56. value : null,
  57. <span id='Ext-picker-Color-cfg-clickEvent'> /**
  58. </span> * @cfg {String} clickEvent
  59. * The DOM event that will cause a color to be selected. This can be any valid event name (dblclick, contextmenu).
  60. */
  61. clickEvent :'click',
  62. <span id='Ext-picker-Color-cfg-allowReselect'> /**
  63. </span> * @cfg {Boolean} allowReselect
  64. * If set to true then reselecting a color that is already selected fires the {@link #event-select} event
  65. */
  66. allowReselect : false,
  67. <span id='Ext-picker-Color-property-colors'> /**
  68. </span> * @property {String[]} colors
  69. * An array of 6-digit color hex code strings (without the # symbol). This array can contain any number of colors,
  70. * and each hex code should be unique. The width of the picker is controlled via CSS by adjusting the width property
  71. * of the 'x-color-picker' class (or assigning a custom class), so you can balance the number of colors with the
  72. * width setting until the box is symmetrical.
  73. *
  74. * You can override individual colors if needed:
  75. *
  76. * var cp = new Ext.picker.Color();
  77. * cp.colors[0] = 'FF0000'; // change the first box to red
  78. *
  79. * Or you can provide a custom array of your own for complete control:
  80. *
  81. * var cp = new Ext.picker.Color();
  82. * cp.colors = ['000000', '993300', '333300'];
  83. */
  84. colors : [
  85. '000000', '993300', '333300', '003300', '003366', '000080', '333399', '333333',
  86. '800000', 'FF6600', '808000', '008000', '008080', '0000FF', '666699', '808080',
  87. 'FF0000', 'FF9900', '99CC00', '339966', '33CCCC', '3366FF', '800080', '969696',
  88. 'FF00FF', 'FFCC00', 'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0',
  89. 'FF99CC', 'FFCC99', 'FFFF99', 'CCFFCC', 'CCFFFF', '99CCFF', 'CC99FF', 'FFFFFF'
  90. ],
  91. <span id='Ext-picker-Color-cfg-handler'> /**
  92. </span> * @cfg {Function} handler
  93. * A function that will handle the select event of this picker. The handler is passed the following parameters:
  94. *
  95. * - `picker` : ColorPicker
  96. *
  97. * The {@link Ext.picker.Color picker}.
  98. *
  99. * - `color` : String
  100. *
  101. * The 6-digit color hex code (without the # symbol).
  102. */
  103. <span id='Ext-picker-Color-cfg-scope'> /**
  104. </span> * @cfg {Object} scope
  105. * The scope (`this` reference) in which the `{@link #handler}` function will be called.
  106. *
  107. * Defaults to this Color picker instance.
  108. */
  109. colorRe: /(?:^|\s)color-(.{6})(?:\s|$)/,
  110. renderTpl: [
  111. '&lt;tpl for=&quot;colors&quot;&gt;',
  112. '&lt;a href=&quot;#&quot; class=&quot;color-{.}&quot; hidefocus=&quot;on&quot;&gt;',
  113. '&lt;em&gt;&lt;span style=&quot;background:#{.}&quot; unselectable=&quot;on&quot;&gt;&amp;#160;&lt;/span&gt;&lt;/em&gt;',
  114. '&lt;/a&gt;',
  115. '&lt;/tpl&gt;'
  116. ],
  117. // private
  118. initComponent : function(){
  119. var me = this;
  120. me.callParent(arguments);
  121. me.addEvents(
  122. <span id='Ext-picker-Color-event-select'> /**
  123. </span> * @event select
  124. * Fires when a color is selected
  125. * @param {Ext.picker.Color} this
  126. * @param {String} color The 6-digit color hex code (without the # symbol)
  127. */
  128. 'select'
  129. );
  130. if (me.handler) {
  131. me.on('select', me.handler, me.scope, true);
  132. }
  133. },
  134. // private
  135. initRenderData : function(){
  136. var me = this;
  137. return Ext.apply(me.callParent(), {
  138. itemCls: me.itemCls,
  139. colors: me.colors
  140. });
  141. },
  142. onRender : function(){
  143. var me = this,
  144. clickEvent = me.clickEvent;
  145. me.callParent(arguments);
  146. me.mon(me.el, clickEvent, me.handleClick, me, {delegate: 'a'});
  147. // always stop following the anchors
  148. if(clickEvent != 'click'){
  149. me.mon(me.el, 'click', Ext.emptyFn, me, {delegate: 'a', stopEvent: true});
  150. }
  151. },
  152. // private
  153. afterRender : function(){
  154. var me = this,
  155. value;
  156. me.callParent(arguments);
  157. if (me.value) {
  158. value = me.value;
  159. me.value = null;
  160. me.select(value, true);
  161. }
  162. },
  163. // private
  164. handleClick : function(event, target){
  165. var me = this,
  166. color;
  167. event.stopEvent();
  168. if (!me.disabled) {
  169. color = target.className.match(me.colorRe)[1];
  170. me.select(color.toUpperCase());
  171. }
  172. },
  173. <span id='Ext-picker-Color-method-select'> /**
  174. </span> * Selects the specified color in the picker (fires the {@link #event-select} event)
  175. * @param {String} color A valid 6-digit color hex code (# will be stripped if included)
  176. * @param {Boolean} [suppressEvent=false] True to stop the select event from firing.
  177. */
  178. select : function(color, suppressEvent){
  179. var me = this,
  180. selectedCls = me.selectedCls,
  181. value = me.value,
  182. el;
  183. color = color.replace('#', '');
  184. if (!me.rendered) {
  185. me.value = color;
  186. return;
  187. }
  188. if (color != value || me.allowReselect) {
  189. el = me.el;
  190. if (me.value) {
  191. el.down('a.color-' + value).removeCls(selectedCls);
  192. }
  193. el.down('a.color-' + color).addCls(selectedCls);
  194. me.value = color;
  195. if (suppressEvent !== true) {
  196. me.fireEvent('select', me, color);
  197. }
  198. }
  199. },
  200. <span id='Ext-picker-Color-method-getValue'> /**
  201. </span> * Get the currently selected color value.
  202. * @return {String} value The selected value. Null if nothing is selected.
  203. */
  204. getValue: function(){
  205. return this.value || null;
  206. }
  207. });
  208. </pre>
  209. </body>
  210. </html>