Advertisement
Uno-Dan

Plugins update

Dec 23rd, 2020
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function( $ ) {
  2.     $.fn.config = function(args) {
  3.         var tbl = $( this )[0];
  4.        
  5.         tbl.width = args.width ? args.width : '100%';
  6.         tbl.height = args.height ? args.height : '100%';
  7.        
  8.         $(this).resize( tbl.width, tbl.height);
  9.        
  10.         return $( this );
  11.     };
  12. }) ( jQuery ); // config
  13.  
  14. (function( $ ) {
  15.     $.fn.reindex = function() {
  16.         this.find('tbody tr:not(.details)').each(function(idx) {
  17.             $(this).find('td:first-child input').val((idx+1));
  18.         });
  19.         return $( this );
  20.     };
  21. }) ( jQuery ); // reindex
  22.  
  23. (function( $ ) {
  24.     $.fn.colorize = function( args=0 ) {
  25.         var tbl = $( this );
  26.         var table = tbl[0];
  27.        
  28.         var hdr_fg = args.th && args.th.color ? args.th.color : 0;
  29.         var hdr_bg = args.th && args.th.background ? args.th.background : 0;
  30.         var sash = args.th && args.th.sash ? args.th.sash : 0;
  31.         var odd_fg = args.td && args.td.odd_fg ? args.td.odd_fg : 0;
  32.         var odd_bg = args.td && args.td.odd_bg ? args.td.odd_bg : table.background ? table.background : 0;
  33.         var even_fg = args.td && args.td.even_fg ? args.td.even_fg : 0;
  34.         var even_bg = args.td && args.td.even_bg ? args.td.even_bg : 0;
  35.                
  36.         table.hdr_fg = hdr_fg = hdr_fg ? hdr_fg : table.hdr_fg ? table.hdr_fg : '';
  37.         table.hdr_bg = hdr_bg = hdr_bg ? hdr_bg : table.hdr_bg ? table.hdr_bg : '';
  38.         table.sash = sash = sash ? sash : table.sash ? table.sash : hdr_bg;
  39.         table.odd_fg = odd_fg = odd_fg ? odd_fg : table.odd_fg ? table.odd_fg : '';
  40.         table.odd_bg = odd_bg = odd_bg ? odd_bg : table.odd_bg ? table.odd_bg : '';
  41.         table.even_fg = even_fg = even_fg ? even_fg : table.even_fg ? table.even_fg : '';
  42.         table.even_bg = even_bg = even_bg ? even_bg : table.even_bg ? table.even_bg : '';
  43.        
  44.         tbl.find("tbody>tr:not(.details):odd *").css("color", odd_fg);
  45.         tbl.find("tbody>tr:not(.details):odd").css("background-color", odd_bg);
  46.         tbl.find("tbody>tr:not(.details):odd input").css("background-color", odd_bg);
  47.        
  48.         tbl.find("tbody>tr:not(.details):even *").css("color", even_fg);
  49.         tbl.find("tbody>tr:not(.details):even").css("background-color", even_bg);
  50.         tbl.find("tbody>tr:not(.details):even input").css("background-color", even_bg);
  51.        
  52.         tbl.find("thead>tr *").css( { color: hdr_fg, background: hdr_bg } );
  53.         tbl.find("thead>tr button").css( { "border-right-color": sash } );
  54.        
  55.         return tbl;
  56.     };
  57. }) ( jQuery ); // colorize
  58.  
  59. (function( $ ) {
  60.     $.fn.sortable = function() {
  61.         function sort() {
  62.             value = ( tr, idx ) =>
  63.                 $( tr ).find('td').eq( idx ).find( 'input' ).val();
  64.            
  65.             column = (idx, asc) => (a, b) => ((v1, v2) =>
  66.                 v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
  67.                 ) ( value(asc ? a : b, idx), value(asc ? b : a, idx) );
  68.  
  69.             var table = tbl[0];
  70.             var idx = $(this).closest('th').index();
  71.             var tbl_rows = tbl.find('>tbody>tr:not(.details)');
  72.  
  73.             if (typeof table.asc === 'undefined') {
  74.                 table.asc = 1;
  75.             }
  76.            
  77.             Array.from(tbl_rows).sort( column(idx, table.asc = !table.asc) )
  78.                 .forEach(tr => tbl.find('>tbody')[0].appendChild(tr) );
  79.  
  80.             tbl_rows.each(function() {
  81.                 var elm = $(this);
  82.                 var id = $(this).attr('data-id');
  83.                 elm.parent().
  84.                     find('tr.details[data-id=' + id + ']').insertAfter(elm);
  85.             });
  86.             tbl.colorize();
  87.         }
  88.        
  89.         var tbl = $(this);
  90.         var wrap = $('<div class="table wrap"></div>');
  91.         tbl.parent().append( wrap );
  92.         wrap.append( tbl );
  93.         tbl.find('thead th span').on('click', sort);
  94.         tbl.css('display', 'block');
  95.        
  96.         return tbl;
  97.     };
  98. }) ( jQuery ); // sortable
  99.  
  100. (function( $ ) {
  101.     $.fn.resize = function(width=0, height=0) {
  102.         var tbl = $(this);
  103. //        width = tbl[0].width ? tbl[0].width : width;
  104. //        height = tbl[0].height ? tbl[0].height : height;
  105.         tbl.width( 0 );
  106.        
  107.         // Todo write code for height.
  108.                
  109.         var wrap = $(this).closest('.table.wrap');
  110.         wrap.css( { width: width, overflow: 'auto' } );
  111.        
  112.         var span = $('<span>' +
  113.             tbl.find('td:last-child input').val() + '</span>');
  114.         $('footer').append( span.hide() );
  115.  
  116.         tbl.find('th span').each(function( ) {
  117.             $(this).width( $(this).closest('button').width() );
  118.         });
  119.         tbl.find('th:last-child span').width( span.width() );
  120.  
  121.         wrap.on('mouseup', function() {  
  122.             wrap.off('mousemove');
  123.         });
  124.         wrap.on('mouseleave', function() {  
  125.             wrap.off('mousemove');
  126.         });
  127.  
  128.         tbl.find('thead th button').on('dblclick', function() {
  129.             var width = 0;
  130.             var idx = $(this).closest('th').index();
  131.             tbl.find('tbody tr:not(.details)').each(function() {
  132.                 var input = $(this).find('td').eq( idx ).find('input');
  133.                 span.text( input.val() );
  134.                 width = span.width() > width ? span.width() : width;
  135.             });
  136.             tbl.find('thead th').eq( idx ).width(width);
  137.             tbl.find('thead th span').eq( idx ).width(width);
  138.             return false;
  139.         });
  140.        
  141.         wrap.on('mousedown', function(e) {  
  142.             var deltaX = 0;
  143.             var curX = e.pageX;
  144.             var elm = $(document.elementFromPoint(e.pageX, e.pageY)).closest('th');
  145.  
  146.             if(e.offsetX > elm.outerWidth() - 6) {
  147.                 wrap.on('mousemove', function(e) {
  148.                     deltaX = e.pageX - curX;
  149.                     curX = e.pageX;
  150.                     elm.width( elm.width() + deltaX );
  151.  
  152.                     elm.find('span').width( elm.find('span').width() + deltaX );
  153.                     if ( tbl.width() + deltaX > wrap.width() ) {
  154.                         tbl.width(tbl.width() + deltaX);
  155.                         elm.find('span').width( elm.find('span').width() + deltaX );
  156.                     }
  157.                 });
  158.                 return false;
  159.             }
  160.         });
  161.         return $(this);
  162.     };
  163. }) ( jQuery ); // resize
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement