Advertisement
Uno-Dan

Bindings

Feb 18th, 2021
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2.   bindings_set( elm ) {
  3.     function move_up( e ) {
  4.       let input = $( e.target );
  5.       let td = input.closest( 'td' );
  6.       let tr = td.closest( 'tr' ).prev();
  7.       if ( ! tr.length || tr.prop( 'tagName') !== 'TR' ) return false;
  8.      
  9.       td = tr.find( '>td' ).eq( td.index() );
  10.       if ( attrs.selector ) tm.selector_set( td );
  11.      
  12.       return td.find( '>div :input' ).focus();
  13.     }
  14.    
  15.     function move_down( e ) {
  16.       let input = $( e.target );
  17.       let td = input.closest( 'td' );
  18.       let tr = td.closest( 'tr' ).next();
  19.       if ( ! tr.length || tr.prop( 'tagName') !== 'TR' ) return false;
  20.      
  21.       td = tr.find( '>td' ).eq( td.index() );
  22.       if ( attrs.selector ) tm.selector_set( td );
  23.      
  24.       return td.find( '>div :input' ).focus();
  25.     }
  26.    
  27.     function move_left( e ) {
  28.       let input = $( e.target );
  29.       let td = input.closest( 'td' ).prev();
  30.      
  31.       if ( ! td.length || td.index() - attrs.index < 0 ) return false;
  32.       if ( attrs.selector ) tm.selector_set( td );
  33.      
  34.       return td.find( '>div :input' ).focus();
  35.     }
  36.    
  37.     function move_right( e ) {
  38.       let input = $( e.target );
  39.       let td = input.closest( 'td' ).next();
  40.      
  41.       if ( ! td.length || ! td.index() ) return false;
  42.       if ( attrs.selector ) tm.selector_set( td );
  43.      
  44.       return td.find( '>div :input' ).focus();
  45.     }
  46.    
  47.     function key_events() {
  48.       elm.off( 'keyup' ).on( 'keyup', function( e ) {
  49.         let div = $( e.target ).closest( 'div' );
  50.         let input = $( e.target );
  51.         let type = input.prop( 'tagName' );
  52.        
  53.         switch ( e.keyCode ) {
  54.           case 9: // Tab
  55.             e.preventDefault();  
  56.            
  57.             if ( ! div.hasClass( 'selected' ) ) {
  58.               div.addClass( 'selected' );
  59.               if ( type === 'INPUT' )
  60.                 input[ 0 ].setSelectionRange(
  61.                 elm[0].selectionStart, elm[0].selectionStart );
  62.             }
  63.           break;
  64.           case 13: // Enter
  65.             if ( e.target.defaultValue === e.target.value  
  66.             && ! div.hasClass( 'selected' ) ) {
  67.               div.addClass( 'selected' );
  68.               move_down( e );
  69.               return false;
  70.             }
  71.             if ( e.target.defaultValue === e.target.value )
  72.               return false;
  73.            
  74.             if ( $(e.target ).closest( 'div').attr('class') !== 'ok_cell'
  75.               || ( e.target.defaultValue === e.target.value ) )
  76.               return false;
  77.           case 27: // ESC
  78.             let selected = tm.elm.find( '>tbody>tr>td>div.selected ' );
  79.             if ( selected.length ) {
  80.               tm.selection_remove();
  81.             } else {
  82.               $( e.target ).closest( 'div' ).addClass('selected');
  83.               $( e.target ).focus();
  84.             }
  85.           break;
  86.         }
  87.       });
  88.      
  89.       elm.off( 'keydown' ).on( 'keydown', function( e ) {
  90.         if ( ! $( 'table.magic .selected' ).length ) return;
  91.        
  92.         switch( e.keyCode ) { // Arrow Keys
  93.           case 37: if ( attrs.selector ) move_left( e ); return false;
  94.           case 38: move_up( e ); return false;
  95.           case 39: if ( attrs.selector ) move_right( e ); return false;
  96.           case 40: move_down( e ); return false;
  97.         }
  98.         if ( ! attrs.selector ) return;
  99.        
  100.         switch( e.keyCode ) {
  101.           case 9: // Tab
  102.             e.preventDefault();  
  103.             e.shiftKey ? move_left( e ) : move_right( e );
  104.             return false;
  105.           case 13: // Enter
  106.             e.preventDefault();  
  107.             let elm = e.shiftKey && attrs.selector ?
  108.             move_up( e ) : move_down( e );
  109.             tm.selector_set( elm.closest( 'div' ) );
  110.            
  111.             return false;
  112.           case 113: tm.selection_remove(); break; // F2
  113.  
  114.           case 27: // ESC
  115.           case 33:
  116.           case 34: //Page Up/Down
  117.           case 91:
  118.           case 92: return false; // Meta Keys
  119.          
  120.           default:
  121.             let char = String.fromCharCode(e.keyCode)
  122.             .replace(/[\x00-\x07\x0E-\x1F\x7F-\uFFFF]/g, '');
  123.    
  124.             if ( ! char ) return false;
  125.             tm.selection_remove();
  126.           break;
  127.         }
  128.       } );
  129.     }
  130.      
  131.     let tm = this;
  132.     let self = this;
  133.     let attrs = this.attrs;
  134.     let thead = this.thead;
  135.     let tbody = this.tbody;
  136.    
  137.     switch ( elm.prop( 'tagName' ) ) {
  138.       case 'BODY':
  139.         elm.on( 'click', function() {
  140.           if ( $( '.menu.magic' ).is( ':visible' ) ) {
  141.             $( '.menu.magic' ).hide();
  142.           }
  143.         } );
  144.       break;
  145.      
  146.       case 'THEAD':
  147.         if ( attrs.sortable ) {
  148.           thead.find('>tr>th span').on( 'click', tm.column_sort );
  149.         }
  150.         if ( attrs.column_sizing ) {
  151.           thead.find( 'span' ).on( 'dblclick, mousedown', false );
  152.          
  153.           thead.find( 'button' ).on( 'dblclick', function( e ) {
  154.             tm.column_set_width( $( e.target ).closest( 'th' ).index() );
  155.           } );    
  156.          
  157.           thead.find( '>tr>th button' ).
  158.           on( 'mousedown', { elm: false }, function( e ) {
  159.             thead.find( 'tr *' ).css( 'cursor', 'col-resize' );
  160.             tbody.find( 'tr *' ).css( 'cursor', 'col-resize' );
  161.             e.data.elm = $( this );
  162.             tm.column_sizer( e );
  163.           } );
  164.         }
  165.       break;
  166.      
  167.       case 'TBODY':
  168.         tbody.find( '>tr>td>div, >tr>td>div :input' )
  169.         .on( 'contextmenu', function() {
  170.           let div = $( this ).closest( 'div' );
  171.           tm.selector_set( div );
  172.           tm.menu.init( tbody );
  173.           div.find( ':input' ).trigger( 'click' ).focus();
  174.         } );
  175.       break;
  176.      
  177.       case 'TR':
  178.         if ( attrs.column_sizing ) {
  179.           let div = elm.find( '>td>div' );
  180.           let input = div.find( 'input' );
  181.           let select = div.find( 'select' );
  182.          
  183.           div.on( 'click', tm.selector_set );
  184.           input.on( 'click', tm.selector_set );
  185.           input.on( 'dblclick', tm.cell_edit );
  186.           elm.find( '>td.index input' ).off( 'click' );
  187.  
  188.           input.change( function( e ) {
  189.             let elm = $( e.target );
  190.             let td = elm.closest( 'td' );
  191.             let id = td.closest( 'tr' ).attr( 'data-id' );
  192.             let idx = td.index() - 1;
  193.  
  194.             td.find( '>div ,>div input').addClass( 'ok_cell' );
  195.            
  196.             tm.data.body.rows[ id ][ idx ] = elm.val();
  197.  
  198.             setTimeout( () => {
  199.               td.find( '>div ,>div input').removeClass( 'ok_cell' );
  200.             }, 2000);
  201.            
  202.           } );
  203.  
  204.           select.on( 'focusout', function( e ) {
  205.             let elm = $( e.target );
  206.             let td = elm.closest( 'td' );
  207.            
  208.             let row = parseInt( td.closest( 'tr' ).attr( 'data-id' ) );
  209.             let col = parseInt( td.index() ) - 1;
  210.             let val = parseInt( this.value );
  211.            
  212.             if ( tm.data.body.rows[ row ][ col ] !== val ) {
  213.              
  214.               td.find( '>div ,>div select').addClass( 'ok_cell' );
  215.               tm.data.body.rows[ row ][ col ] = parseInt( val );
  216.  
  217.               setTimeout( () => {
  218.                 td.find( '>div ,>div select').removeClass( 'ok_cell' );
  219.               }, 2000);
  220.             }
  221.           } );
  222.          
  223.           select.on( 'mousedown', function( e ) { tm.selector_set( e ); } );
  224.         }
  225.          
  226.         key_events();
  227.       break
  228.     }
  229.    
  230.     return this;
  231.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement