1. #1
    Sencha User Jad's Avatar
    Join Date
    Feb 2012
    Location
    annecy france
    Posts
    102
    Vote Rating
    3
    Jad is on a distinguished road

      0  

    Default Time field

    Time field


    Hi Guys

    I have developpe a Time field

    Code:
    /** 
     * Time field
     * 
     * @author Jad Haddouch <jad.haddouch@topnetworksa.ch>
     * @docauthor Jad Haddouch <jad.haddouch@topnetworksa.ch>
     * @copyright Copyright 2014 Topnetwork
     */ 
    Ext.define('TnwCore.form.field.Timefield', {
        extend: 'Ext.form.field.Text',
        alias: 'widget.tnwtimefield',
    
    
        fieldLabel: 'Label',
    	
    	stripCharsRe: /[^0-9:.,-]/g,
    	
    	format: 'S', // 'S', 'F'
    	separator: ':', // format S: ':' // format F: ',' '.'
    	
    	defaultValue: false, // format S: (string)'HH:MM' // format F: (float) H.M
    	minValue: false, // format S: (string)'HH:MM' // format F: (float) H.M
    	maxValue: false, // format S: (string)'HH:MM' // format F: (float) H.M
    
    
        initComponent: function () {
            var me = this;
    		
    		if (me.format === 'F' && me.separator === ':') { me.separator = '.'; }
    		
    		me.on({
    			blur: {
    				fn: me.onTextfieldBlur,
    				scope: me
    			},
    			specialkey: {
    				fn: me.onTextfieldSpecialkey,
    				scope: me
    			}
    		});
    		
    		me.addEvents('entryformated');
    
    
            me.callParent(arguments);
        },
    
    
        onTextfieldBlur: function (component, e, eOpts) {
            var me = this;
    		
    		me.formatEntry();
        },
    
    
        onTextfieldSpecialkey: function (field, e, eOpts) {
            var me = this;
            
    		if (e.getKey() == e.ENTER || e.getKey() == e.TAB) {
                me.formatEntry();
            }
        },
    
    
    	// ...( )H( )... :(-)( )M( )M...
        formatStringEntry: function (entry) {
    		var me = this,
                split = entry.split(':'),
                h = Math.abs(parseInt(split[0])), 
                m = split[1] != '' ? parseInt(split[1]) : 0;
    			
    		if (m > 59 || m < 0) { 
    			h += Math.floor(m / 60);
    			m = m % 60;
    			if (m < 0) { m += 60; }
    
    
    			if (h < 0) { 
    				h = 0; 
    				m = 0; 
    			}
    		}
    		
    		return me.format === 'F' ? h + (m / 60) : [h, m];	
    	},
    	
    	// H.M // H,M
    	formatFloatEntry: function (entry) {
    		var me = this,
    			h = 0, 
                m = 0,
                split;
    			
    		if (entry.indexOf(',') > -1) { split = entry.split(','); }
    		else if (entry.indexOf('.') > -1) { split = entry.split('.'); }
    		else { return me.format === 'F' ? 0 : [0, 0]; }
    
    
    		var h1 = split[0];
    
    
    		h = Math.abs(parseInt(h1 == '' ? '0' : h1));
    
    
    		m = Math.ceil(parseFloat('0.' + Math.abs(parseInt(split[1]))) * 60);
    		
    		return me.format === 'F' ? h + (m / 60) : [h, m];
    	},
    	
    	// H
    	formatIntEntry: function (entry) {
    		var me = this,
    			h = Math.abs(parseInt(entry));
    		
    		return me.format === 'F' ? h : [h, 0];
    	},
    	
    	getDefaultValue: function (h_m) {
    		var me = this,
    			defaultValue = me.defaultValue !== false;
    			
    		if (me.format === 'F') { h_m = defaultValue ? me.defaultValue : 0; }
    		else {
    			var split = defaultValue ? me.defaultValue.split(me.separator) : '';
    			if (isNaN(h_m[0])) { h_m[0] = defaultValue ? parseInt(split[0]) : 0; }
    			if (isNaN(h_m[1])) { h_m[1] = defaultValue ? parseInt(split[1]) : 0; }	
    		}
    		
    		return h_m;
    	},
    	
    	getDefaultValue_F: function (h_m) {
    		var me = this;
    			
    		return me.defaultValue !== false ? me.defaultValue : 0;
    	},
    	
    	getDefaultValue_S: function (h_m) {
    		var me = this,
    			defaultValue = me.defaultValue !== false,
    			split = defaultValue ? me.defaultValue.split(me.separator) : '';
    		
    		if (isNaN(h_m[0])) { h_m[0] = defaultValue ? parseInt(split[0]) : 0; }
    		if (isNaN(h_m[1])) { h_m[1] = defaultValue ? parseInt(split[1]) : 0; }
    		
    		return h_m
    	},
    	
    	getMinValue: function (h_m) {
    		var me = this;
    		
    		if (me.format === 'F') { 
    			if (h_m < me.minValue) { h_m = me.minValue }
    		}
    		else {
    			var min = me.minValue.split(me.separator),
    				mh = parseInt(min[0]),
    				mm = parseInt(min[1]);
    	
    			if (h_m[0] < mh || (h_m[0] == mh && h_m[1] < mm)) { 
    				h_m[0] = mh; 
    				h_m[1] = mm; 
    			}
    		}
    		
    		return h_m
    	},
    	
    	getMaxValue: function (h_m) {
    		var me = this;
    		
    		if (me.format === 'F') { 
    			if (h_m > me.maxValue) { h_m = me.maxValue; }
    		}
    		else {
    			var max = me.maxValue.split(me.separator),
    				mh = parseInt(max[0]),
    				mm = parseInt(max[1]);
    
    
                if (h_m[0] > mh || (h_m[0] == mh && h_m[1] > mm)) { 
    				h_m[0] = mh; 
    				h_m[1] = mm; 
    			}
    		}
    		
    		return h_m
    	},
    	
    	formatEntry: function () {
            var me = this,
                entry = me.getValue(), 
                h_m = me.format === 'F' ? 0 : [0, 0];
    		
    		if (entry.indexOf(':') > -1) { h_m = me.formatStringEntry(entry); } // ...( )H( )... :(-)( )M( )M...
    		else if (entry.indexOf('.') > -1 || entry.indexOf(',') > -1) { h_m = me.formatFloatEntry(entry); } // H.M // H,M
    		else { h_m = me.formatIntEntry(entry); } // H
            
    		if ((me.format === 'S' && (isNaN(h_m[0]) || isNaN(h_m[1]))) || (me.format === 'F' && isNaN(h_m))) { h_m = me['getDefaultValue_' + me.format](h_m); }
    		
            if (me.minValue !== false) { h_m = me.getMinValue(h_m); }
    
    
            if (me.maxValue !== false) { h_m = me.getMaxValue(h_m); }
    
    
            me.setFormatedEntry(h_m);
        },
    	
    	setFormatedEntry: function (h_m) {
    		var me = this,
    			value = '';
    		
    		if (me.format === 'S') { value = (h_m[0] < 10 ? '0' : '') + h_m[0] + me.separator + (h_m[1] < 10 ? '0' : '') + h_m[1]; }
    		else if (me.format === 'F') { 
    			var h = parseInt(h_m);
    			value = me.separator !== '.' ? h + me.separator + parseInt((h_m - h) * 100) : h_m;
    		}
    		
    		me.setValue(value);
    		
    		me.fireEvent('entryformated', me, me.getValue());
    	}
    });

  2. #2
    Sencha - Support Team joel.watson's Avatar
    Join Date
    Nov 2014
    Posts
    990
    Vote Rating
    43
    joel.watson has a spectacular aura about joel.watson has a spectacular aura about joel.watson has a spectacular aura about

      0  

    Default


    Thanks for your contribution to the community!

Thread Participants: 1