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