How do I convert a condition if in a method that can be called in EXTJS6?

1

I have a grid in EXTJS where when adding a row (day), it is filled with schedules and verifies that the sum of all the hours is not greater than X value, and if it is not greater, it allows adding more rows until complete the hours.

There is a section where you check the weekly hours and if they are completed send an alert that no more hours can be added.

This is the code:

onAgregarDiaClick: function(button, e, eOpts) {
    var dias_store      = button.up('grid').getStore().count();
    console.log(dias_store);

    var dif_hora_total = 8.80;
    var cuenta_grid    = Ext.ComponentQuery.query('[itemId=grid_jornadaLaboral]')[0].getStore().getCount();

    if(dias_store < 7)
    {
        if(dias_store == 6)
        button.disable();

        grideditable = button.up('grid');
        var opcion = new form_48.model.JornadaLaboral.mdl_JornadaDiaria({
            'dia_codigo'    : 8,
            'dia_nombre'    : 'Seleccione...',
            'entrada_manana': '08:30',
            'salida_manana' : '13:18',
            'entrada_tarde' : '15:00',
            'salida_tarde'  : '19:00'
        });

        var editor = button.up('grid').editingPlugin;
        //editor.cancelEdit();

        if(cuenta_grid === 0){

            button.up('grid').getStore().insert(dias_store+1, opcion);
            button.up('grid').getStore().sort('dia_codigo', 'ASC');

        }
        //editor.startEditByPosition({row: dias_store+1, column: 0});



    }

    var horas_semanales = Ext.ComponentQuery.query('numberfield[name=num_horasSemanales]')[0].getValue();


    if( dif_hora_total < horas_semanales ){

        if( cuenta_grid > 0 ){


            for (var i = 0; i < cuenta_grid; i++)
            {

                var ent_mnna = Ext.ComponentQuery.query('[itemId=grid_jornadaLaboral]')[0].getStore().data.items[i].data.entrada_manana;
                var sal_mnna = Ext.ComponentQuery.query('[itemId=grid_jornadaLaboral]')[0].getStore().data.items[i].data.salida_manana;
                var ent_tard = Ext.ComponentQuery.query('[itemId=grid_jornadaLaboral]')[0].getStore().data.items[i].data.entrada_tarde;
                var sal_tard = Ext.ComponentQuery.query('[itemId=grid_jornadaLaboral]')[0].getStore().data.items[i].data.salida_tarde;


                var ent_mnna_sum = form_48.app.getController('FormateoNumero').fecha_hora(ent_mnna);
                var sal_mnna_sum = form_48.app.getController('FormateoNumero').fecha_hora(sal_mnna);
                var ent_tard_sum = form_48.app.getController('FormateoNumero').fecha_hora(ent_tard);
                var sal_tard_sum = form_48.app.getController('FormateoNumero').fecha_hora(sal_tard);

                //OBTENEMOS LAS DIFERENCIAS DE HORAS DE LA MANANA Y DE LA TARDE (EJ. 8:30 -13:18 = 04:48)
                dif_hora_manana = form_48.app.getController('FormateoNumero').resta_horas(ent_mnna_sum,sal_mnna_sum);
                dif_hora_tarde  = form_48.app.getController('FormateoNumero').resta_horas(ent_tard_sum,sal_tard_sum);


                //SUMAMOS LAS DIFERENCIAS DE HORAS DE LA MANANA Y DE LA TARDE PARA OBTENER EL TOTAL DE HORAS TRABAJADO AL DIA.
                dif_hora_total = dif_hora_total + form_48.app.getController('FormateoNumero').suma_horas(dif_hora_manana,dif_hora_tarde);

                // Ext.ComponentQuery.query('numberfield[name=num_horas_grid]')[0].setValue(num_horas_grid);
                console.log(dif_hora_total);



            }

            if(dif_hora_total === horas_semanales){

                Ext.ComponentQuery.query('displayfield[name=dsp_cumple_horas]')[0].setValue('SI');

               console.log(horas_semanales);
            }else{

                Ext.ComponentQuery.query('displayfield[name=dsp_cumple_horas]')[0].setValue('NO');
            }

            if(dif_hora_total <= horas_semanales){

                //alert("dif_hora2: "+dif_hora_total);
                button.up('grid').getStore().insert(dias_store+1, opcion);
                button.up('grid').getStore().sort('dia_codigo', 'ASC');

            }else{

                Ext.Msg.show
                ({
                    title:'Advertencia',
                    msg: 'No se puede ingresar la jornada ya que las horas exceden el maximo establecido en el campo <center><b>"Horas Semanales"</b></center>',
                    icon: Ext.MessageBox.WARNING,
                    buttons: Ext.MessageBox.OK
                });
            }



        }//fin if cuentagrid
    }



},

Since the declaration of variable var horas_semanales onwards I want it to be a method that can be called, something like in java place it check () {} and that can be called from within the method onAgregarDiaClick, because at this moment it's just a if condition within onAgregarClick, but I want to make use of that condition from here as well, in the onTimefieldSelect method

onTimefieldSelect: function(combo, record, eOpts) {
    var value = new Date(combo.getValue());


    hora = value.getHours();
    minuto = (value.getMinutes() === 0)?'00':value.getMinutes();
    console.log('hora '+hora+' '+minuto);
    combo.setValue(hora +':'+minuto);
},

What the code does now is that when you add a new row, check the number of hours with the IF condition and my idea is that each time you modify a value in the grid, go to that new method of check (that replaces the if) to add all the hours of the grid and just check whether they are ok or not, because at this time check only when adding a new row.

That's why my idea is to convert that condition if it is onAddDiaClick in a method that can be called onTimefieldSelect, but I do not know how to do that in EXTJS.

How can I do it? Is there a better way yet ??

Thank you very much !!

    
asked by user3565613 09.06.2017 в 18:25
source

0 answers