1. #1
    Sencha User
    Join Date
    Jul 2009
    Posts
    6
    Vote Rating
    0
    slammer is on a distinguished road

      0  

    Default Simple Ext.ux.ColorField plugin

    Simple Ext.ux.ColorField plugin


    I decided to migrate this plugin http://www.sencha.com/learn/legacy/Extension:ColorField to ExtJS 4.

    Code:
    Ext.define('Ext.ux.ColorField', {
        extend: 'Ext.form.field.Trigger',
        alias: 'widget.colorfield',    
        requires: ['Ext.form.field.VTypes', 'Ext.layout.component.field.Text'],
    
        lengthText: "Color hex values must be either 3 or 6 characters.",
        blankText: "Must have a hexidecimal value in the format ABCDEF.",
        
        regex: /^[0-9a-f]{3,6}$/i,
        
        validateValue : function(value){
            if(!this.getEl()) {
                return true;
            }
            if(value.length!=3 && value.length!=6) {
                this.markInvalid(Ext.String.format(this.lengthText, value));
                return false;
            }
            if((value.length < 1 && !this.allowBlank) || !this.regex.test(value)) {
                this.markInvalid(Ext.String.format(this.blankText, value));
                return false;
            }
            
            this.markInvalid();
            this.setColor(value);
            return true;
        },
    
        markInvalid : function( msg ) {
            Ext.ux.ColorField.superclass.markInvalid.call(this, msg);
            this.inputEl.setStyle({
                'background-image': 'url(../resources/themes/images/default/grid/invalid_line.gif)'
            });
        },
        
        setValue : function(hex){
            Ext.ux.ColorField.superclass.setValue.call(this, hex);
            this.setColor(hex);
        },
        
        setColor : function(hex) {
            Ext.ux.ColorField.superclass.setFieldStyle.call(this, {
                'background-color': '#' + hex,
                'background-image': 'none'
            });
        },
    
        menuListeners : {
            select: function(m, d){
                this.setValue(d);
            },
            show : function(){
                this.onFocus();
            },
            hide : function(){
                this.focus();
                var ml = this.menuListeners;
                this.menu.un("select", ml.select,  this);
                this.menu.un("show", ml.show,  this);
                this.menu.un("hide", ml.hide,  this);
            }
        },
        
        onTriggerClick : function(e){
            if(this.disabled){
                return;
            }
            
            this.menu = new Ext.menu.ColorPicker({
                shadow: true,
                autoShow : true
            });
            this.menu.alignTo(this.inputEl, 'tl-bl?');
            this.menu.doLayout();
            
            this.menu.on(Ext.apply({}, this.menuListeners, {
                scope:this
            }));
            
            this.menu.show(this.inputEl);
        }
    });

  2. #2
    Sencha User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    13
    Vote Rating
    -1
    SenchaGuru is an unknown quantity at this point

      0  

    Default


    Thank you, .. this is exactly what I was trying to do, ..

    Maybe someone should update the plugin page, .. this would've saved me hours trying to get that to work

    Excelent work

  3. #3
    Sencha User
    Join Date
    Dec 2008
    Posts
    89
    Vote Rating
    0
    janhov is on a distinguished road

      0  

    Default


    Thanks a lot! I find this component very useful.

  4. #4
    Sencha User Izhaki's Avatar
    Join Date
    Apr 2009
    Location
    London
    Posts
    117
    Vote Rating
    11
    Izhaki will become famous soon enough

      0  

    Default


    Thanks a lot for sharing.

    Shouldn't it be:

    Code:
        validateValue : function(value){
    
    
            if (this.allowBlank && !value)
            {
                return true
            }
    
    
            if(!this.getEl()) {
                return true;
            }
            if(value.length!=3 && value.length!=6) {
                this.markInvalid(Ext.String.format(this.lengthText, value));
                return false;
            }
            if((value.length < 1 && !this.allowBlank) || !this.regex.test(value)) {
                this.markInvalid(Ext.String.format(this.blankText, value));
                return false;
            }
    
    
            return true;
        },

  5. #5
    Sencha User
    Join Date
    Nov 2011
    Posts
    1
    Vote Rating
    0
    apriza_ymail is on a distinguished road

      0  

    Default


    cann't run in ie.. i am using ie 8

  6. #6
    Sencha User
    Join Date
    Oct 2011
    Posts
    1
    Vote Rating
    0
    lastcrown is on a distinguished road

      0  

    Default


    on setColor, remove '#'

    [before]
    background-color': '#' + hex,

    [after]
    'background-color': hex,