home *** CD-ROM | disk | FTP | other *** search
/ Freelog 101 / FreelogNo101-JanvierFevrier2011.iso / Securite / Passter / Passter.exe / flexigrid.js < prev    next >
Text File  |  2010-10-26  |  17KB  |  638 lines

  1. /*
  2.  * Flexigrid for jQuery - New Wave Grid
  3.  *
  4.  * Copyright (c) 2008 Paulo P. Marinas (webplicity.net/flexigrid)
  5.  * Dual licensed under the MIT (MIT-LICENSE.txt)
  6.  * and GPL (GPL-LICENSE.txt) licenses.
  7.  *
  8.  * $Date: 2008-07-14 00:09:43 +0800 (Tue, 14 Jul 2008) $
  9.  */
  10.  
  11. (function($){
  12.           
  13.     $.addFlex = function(t,p)
  14.     {
  15.  
  16.         if (t.grid) return false; //return if already exist    
  17.         
  18.         // apply default properties
  19.         p = $.extend({
  20.              height: 200, //default height
  21.              width: 'auto', //auto width
  22.              striped: true, //apply odd even stripes
  23.              novstripe: false,
  24.              minwidth: 30, //min width of columns
  25.              minheight: 80, //min height of columns
  26.              resizable: true, //resizable table
  27.              url: false, //ajax url
  28.              method: 'POST', // data sending method
  29.              dataType: 'xml', // type of data loaded
  30.              errormsg: 'Connection Error',
  31.              usepager: false, //
  32.              nowrap: true, //
  33.              page: 1, //current page
  34.              total: 1, //total pages
  35.              useRp: true, //use the results per page select box
  36.              rp: 15, // results per page
  37.              rpOptions: [10,15,20,25,40],
  38.              title: false,
  39.              pagestat: 'Displaying {from} to {to} of {total} items',
  40.              pagetext: 'Page',
  41.              outof: 'of',
  42.              findtext: 'Find',
  43.              procmsg: 'Processing, please wait ...',
  44.              query: '',
  45.              qtype: '',
  46.              nomsg: 'No items',
  47.              minColToggle: 1, //minimum allowed column to be hidden
  48.              showToggleBtn: true, //show or hide column toggle popup
  49.              hideOnSubmit: true,
  50.              autoload: true,
  51.              blockOpacity: 0.5,
  52.              onDragCol: false,
  53.              onToggleCol: false,
  54.              onChangeSort: false,
  55.              onSuccess: false,
  56.              onError: false,
  57.              onSubmit: false // using a custom populate function
  58.           }, p);
  59.                   
  60.  
  61.         $(t)
  62.         .show() //show if hidden
  63.         .attr({cellPadding: 0, cellSpacing: 0, border: 0})  //remove padding and spacing
  64.         .removeAttr('width') //remove width properties    
  65.         ;
  66.         
  67.         //create grid class
  68.         var g = {
  69.             hset : {},
  70.             rePosDrag: function () {
  71.  
  72.             var cdleft = 0 - this.hDiv.scrollLeft;
  73.             if (this.hDiv.scrollLeft>0) cdleft -= Math.floor(p.cgwidth/2);
  74.             $(g.cDrag).css({top:g.hDiv.offsetTop+1});
  75.             var cdpad = this.cdpad;
  76.             
  77.             $('div',g.cDrag).hide();
  78.             
  79.             $('thead tr:first th:visible',this.hDiv).each
  80.                 (
  81.                  function ()
  82.                     {
  83.                     var n = $('thead tr:first th:visible',g.hDiv).index(this);
  84.  
  85.                     var cdpos = parseInt($('div',this).width());
  86.                     var ppos = cdpos;
  87.                     if (cdleft==0) 
  88.                             cdleft -= Math.floor(p.cgwidth/2); 
  89.  
  90.                     cdpos = cdpos + cdleft + cdpad;
  91.                     
  92.                     $('div:eq('+n+')',g.cDrag).css({'left':cdpos+'px'}).show();
  93.  
  94.                     cdleft = cdpos;
  95.                     }
  96.                 );
  97.                 
  98.             },
  99.             fixHeight: function (newH) {
  100.                     newH = false;
  101.                     if (!newH) newH = $(g.bDiv).height();
  102.                     var hdHeight = $(this.hDiv).height();
  103.                     $('div',this.cDrag).each(
  104.                         function ()
  105.                             {
  106.                                 $(this).height(newH+hdHeight);
  107.                             }
  108.                     );
  109.                     
  110.                     var nd = parseInt($(g.nDiv).height());
  111.                     
  112.                     if (nd>newH)
  113.                         $(g.nDiv).height(newH).width(200);
  114.                     else
  115.                         $(g.nDiv).height('auto').width('auto');
  116.                     
  117.                     $(g.block).css({height:newH,marginBottom:(newH * -1)});
  118.                     
  119.                     var hrH = g.bDiv.offsetTop + newH;
  120.                     if (p.height != 'auto' && p.resizable) hrH = g.vDiv.offsetTop;
  121.                     $(g.rDiv).css({height: hrH});
  122.                 
  123.             },
  124.             dragStart: function (dragtype,e,obj) { //default drag function start
  125.                 
  126.                 if (dragtype=='colresize') //column resize
  127.                     {
  128.                         $(g.nDiv).hide();$(g.nBtn).hide();
  129.                         var n = $('div',this.cDrag).index(obj);
  130.                         var ow = $('th:visible div:eq('+n+')',this.hDiv).width();
  131.                         $(obj).addClass('dragging').siblings().hide();
  132.                         $(obj).prev().addClass('dragging').show();
  133.                         
  134.                         this.colresize = {startX: e.pageX, ol: parseInt(obj.style.left), ow: ow, n : n };
  135.                         $('body').css('cursor','col-resize');
  136.                     }
  137.                 else if (dragtype=='vresize') //table resize
  138.                     {
  139.                         var hgo = false;
  140.                         $('body').css('cursor','row-resize');
  141.                         if (obj) 
  142.                             {
  143.                             hgo = true;
  144.                             $('body').css('cursor','col-resize');
  145.                             }
  146.                         this.vresize = {h: p.height, sy: e.pageY, w: p.width, sx: e.pageX, hgo: hgo};
  147.                         
  148.                     }
  149.  
  150.                 else if (dragtype=='colMove') //column header drag
  151.                     {
  152.                         $(g.nDiv).hide();$(g.nBtn).hide();
  153.                         this.hset = $(this.hDiv).offset();
  154.                         this.hset.right = this.hset.left + $('table',this.hDiv).width();
  155.                         this.hset.bottom = this.hset.top + $('table',this.hDiv).height();
  156.                         this.dcol = obj;
  157.                         this.dcoln = $('th',this.hDiv).index(obj);
  158.                         
  159.                         this.colCopy = document.createElement("div");
  160.                         this.colCopy.className = "colCopy";
  161.                         this.colCopy.innerHTML = obj.innerHTML;
  162.                         if ($.browser.msie)
  163.                         {
  164.                         this.colCopy.className = "colCopy ie";
  165.                         }
  166.                         
  167.                         
  168.                         $(this.colCopy).css({position:'absolute',float:'left',display:'none', textAlign: obj.align});
  169.                         $('body').append(this.colCopy);
  170.                         $(this.cDrag).hide();
  171.                         
  172.                     }
  173.                                                         
  174.                 $('body').noSelect();
  175.             
  176.             },
  177.             dragMove: function (e) {
  178.             
  179.                 if (this.colresize) //column resize
  180.                     {
  181.                         var n = this.colresize.n;
  182.                         var diff = e.pageX-this.colresize.startX;
  183.                         var nleft = this.colresize.ol + diff;
  184.                         var nw = this.colresize.ow + diff;
  185.                         if (nw > p.minwidth)
  186.                             {
  187.                                 $('div:eq('+n+')',this.cDrag).css('left',nleft);
  188.                                 this.colresize.nw = nw;
  189.                             }
  190.                     }
  191.                 else if (this.vresize) //table resize
  192.                     {
  193.                         var v = this.vresize;
  194.                         var y = e.pageY;
  195.                         var diff = y-v.sy;
  196.                         
  197.                         if (!p.defwidth) p.defwidth = p.width;
  198.                         
  199.                         if (p.width != 'auto' && !p.nohresize && v.hgo)
  200.                         {
  201.                             var x = e.pageX;
  202.                             var xdiff = x - v.sx;
  203.                             var newW = v.w + xdiff;
  204.                             if (newW > p.defwidth)
  205.                                 {
  206.                                     this.gDiv.style.width = newW + 'px';
  207.                                     p.width = newW;
  208.                                 }
  209.                         }
  210.                         
  211.                         var newH = v.h + diff;
  212.                         if ((newH > p.minheight || p.height < p.minheight) && !v.hgo)
  213.                             {
  214.                                 this.bDiv.style.height = newH + 'px';
  215.                                 p.height = newH;
  216.                                 this.fixHeight(newH);
  217.                             }
  218.                         v = null;
  219.                     }
  220.                 else if (this.colCopy) {
  221.                     $(this.dcol).addClass('thMove').removeClass('thOver'); 
  222.                     if (e.pageX > this.hset.right || e.pageX < this.hset.left || e.pageY > this.hset.bottom || e.pageY < this.hset.top)
  223.                     {
  224.                         //this.dragEnd();
  225.                         $('body').css('cursor','move');
  226.                     }
  227.                     else 
  228.                     $('body').css('cursor','pointer');
  229.                     $(this.colCopy).css({top:e.pageY + 10,left:e.pageX + 20, display: 'block'});
  230.                 }                                                    
  231.             
  232.             },
  233.             dragEnd: function () {
  234.  
  235.                 if (this.colresize)
  236.                     {
  237.                         var n = this.colresize.n;
  238.                         var nw = this.colresize.nw;
  239.  
  240.                                 $('th:visible div:eq('+n+')',this.hDiv).css('width',nw);
  241.                                 $('tr',this.bDiv).each (
  242.                                     function ()
  243.                                         {
  244.                                         $('td:visible div:eq('+n+')',this).css('width',nw);
  245.                                         }
  246.                                 );
  247.                                 this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  248.  
  249.  
  250.                         $('div:eq('+n+')',this.cDrag).siblings().show();
  251.                         $('.dragging',this.cDrag).removeClass('dragging');
  252.                         this.rePosDrag();
  253.                         this.fixHeight();
  254.                         this.colresize = false;
  255.                     }
  256.                 else if (this.vresize)
  257.                     {
  258.                         this.vresize = false;
  259.                     }
  260.                 else if (this.colCopy)
  261.                     {
  262.                         $(this.colCopy).remove();
  263.                         if (this.dcolt != null)
  264.                             {
  265.                             
  266.                             
  267.                             if (this.dcoln>this.dcolt)
  268.                                 $('th:eq('+this.dcolt+')',this.hDiv).before(this.dcol);
  269.                             else
  270.                                 $('th:eq('+this.dcolt+')',this.hDiv).after(this.dcol);
  271.                             
  272.                             
  273.                             
  274.                             this.switchCol(this.dcoln,this.dcolt);
  275.                             $(this.cdropleft).remove();
  276.                             $(this.cdropright).remove();
  277.                             this.rePosDrag();
  278.                             
  279.                             if (p.onDragCol) p.onDragCol(this.dcoln, this.dcolt);
  280.                                                                             
  281.                             }
  282.                         
  283.                         this.dcol = null;
  284.                         this.hset = null;
  285.                         this.dcoln = null;
  286.                         this.dcolt = null;
  287.                         this.colCopy = null;
  288.                         
  289.                         $('.thMove',this.hDiv).removeClass('thMove');
  290.                         $(this.cDrag).show();
  291.                     }                                        
  292.                 $('body').css('cursor','default');
  293.                 $('body').noSelect(false);
  294.             },
  295.             toggleCol: function(cid,visible) {
  296.                 
  297.                 var ncol = $("th[axis='col"+cid+"']",this.hDiv)[0];
  298.                 var n = $('thead th',g.hDiv).index(ncol);
  299.                 var cb = $('input[value='+cid+']',g.nDiv)[0];
  300.                 
  301.                 
  302.                 if (visible==null)
  303.                     {
  304.                         visible = ncol.hide;
  305.                     }
  306.                 
  307.                 
  308.                 
  309.                 if ($('input:checked',g.nDiv).length<p.minColToggle&&!visible) return false;
  310.                 
  311.                 if (visible)
  312.                     {
  313.                         ncol.hide = false;
  314.                         $(ncol).show();
  315.                         cb.checked = true;
  316.                     }
  317.                 else
  318.                     {
  319.                         ncol.hide = true;
  320.                         $(ncol).hide();
  321.                         cb.checked = false;
  322.                     }
  323.                     
  324.                         $('tbody tr',t).each
  325.                             (
  326.                                 function ()
  327.                                     {
  328.                                         if (visible)
  329.                                             $('td:eq('+n+')',this).show();
  330.                                         else
  331.                                             $('td:eq('+n+')',this).hide();
  332.                                     }
  333.                             );                            
  334.                 
  335.                 this.rePosDrag();
  336.                 
  337.                 if (p.onToggleCol) p.onToggleCol(cid,visible);
  338.                 
  339.                 return visible;
  340.             },
  341.             switchCol: function(cdrag,cdrop) { //switch columns
  342.                 
  343.                 $('tbody tr',t).each
  344.                     (
  345.                         function ()
  346.                             {
  347.                                 if (cdrag>cdrop)
  348.                                     $('td:eq('+cdrop+')',this).before($('td:eq('+cdrag+')',this));
  349.                                 else
  350.                                     $('td:eq('+cdrop+')',this).after($('td:eq('+cdrag+')',this));
  351.                             }
  352.                     );
  353.                     
  354.                     //switch order in nDiv
  355.                     if (cdrag>cdrop)
  356.                         $('tr:eq('+cdrop+')',this.nDiv).before($('tr:eq('+cdrag+')',this.nDiv));
  357.                     else
  358.                         $('tr:eq('+cdrop+')',this.nDiv).after($('tr:eq('+cdrag+')',this.nDiv));
  359.                         
  360.                     if ($.browser.msie&&$.browser.version<7.0) $('tr:eq('+cdrop+') input',this.nDiv)[0].checked = true;    
  361.                     
  362.                     this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  363.             },            
  364.             scroll: function() {
  365.                     this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  366.                     this.rePosDrag();
  367.             },
  368.             addData: function (data) { //parse data
  369.                 
  370.                 if (p.preProcess)
  371.                     data = p.preProcess(data);
  372.                 
  373.                 $('.pReload',this.pDiv).removeClass('loading');
  374.                 this.loading = false;
  375.  
  376.                 if (!data) 
  377.                     {
  378.                     $('.pPageStat',this.pDiv).html(p.errormsg);    
  379.                     return false;
  380.                     }
  381.  
  382.                 if (p.dataType=='xml')
  383.                     p.total = +$('rows total',data).text();
  384.                 else
  385.                     p.total = data.total;
  386.                     
  387.                 if (p.total==0)
  388.                     {
  389.                     $('tr, a, td, div',t).unbind();
  390.                     $(t).empty();
  391.                     p.pages = 1;
  392.                     p.page = 1;
  393.                     this.buildpager();
  394.                     $('.pPageStat',this.pDiv).html(p.nomsg);
  395.                     return false;
  396.                     }
  397.                 
  398.                 p.pages = Math.ceil(p.total/p.rp);
  399.                 
  400.                 if (p.dataType=='xml')
  401.                     p.page = +$('rows page',data).text();
  402.                 else
  403.                     p.page = data.page;
  404.                 
  405.                 this.buildpager();
  406.  
  407.                 //build new body
  408.                 var tbody = document.createElement('tbody');
  409.                 
  410.                 if (p.dataType=='json')
  411.                 {
  412.                     $.each
  413.                     (
  414.                      data.rows,
  415.                      function(i,row) 
  416.                          {
  417.                             var tr = document.createElement('tr');
  418.                             if (i % 2 && p.striped) tr.className = 'erow';
  419.                             
  420.                             if (row.id) tr.id = 'row' + row.id;
  421.                             
  422.                             //add cell
  423.                             $('thead tr:first th',g.hDiv).each
  424.                             (
  425.                                  function ()
  426.                                     {
  427.                                         
  428.                                         var td = document.createElement('td');
  429.                                         var idx = $(this).attr('axis').substr(3);
  430.                                         td.align = this.align;
  431.                                         td.innerHTML = row.cell[idx];
  432.                                         $(tr).append(td);
  433.                                         td = null;
  434.                                     }
  435.                             ); 
  436.                             
  437.                             
  438.                             if ($('thead',this.gDiv).length<1) //handle if grid has no headers
  439.                             {
  440.  
  441.                                     for (idx=0;idx<cell.length;idx++)
  442.                                         {
  443.                                         var td = document.createElement('td');
  444.                                         td.innerHTML = row.cell[idx];
  445.                                         $(tr).append(td);
  446.                                         td = null;
  447.                                         }
  448.                             }                            
  449.                             
  450.                             $(tbody).append(tr);
  451.                             tr = null;
  452.                         }
  453.                     );                
  454.                     
  455.                 } else if (p.dataType=='xml') {
  456.  
  457.                 i = 1;
  458.  
  459.                 $("rows row",data).each
  460.                 (
  461.                  
  462.                      function ()
  463.                         {
  464.                             
  465.                             i++;
  466.                             
  467.                             var tr = document.createElement('tr');
  468.                             if (i % 2 && p.striped) tr.className = 'erow';
  469.  
  470.                             var nid =$(this).attr('id');
  471.                             if (nid) tr.id = 'row' + nid;
  472.                             
  473.                             nid = null;
  474.                             
  475.                             var robj = this;
  476.  
  477.                             
  478.                             
  479.                             $('thead tr:first th',g.hDiv).each
  480.                             (
  481.                                  function ()
  482.                                     {
  483.                                         
  484.                                         var td = document.createElement('td');
  485.                                         var idx = $(this).attr('axis').substr(3);
  486.                                         td.align = this.align;
  487.                                         td.innerHTML = $("cell:eq("+ idx +")",robj).text();
  488.                                         $(tr).append(td);
  489.                                         td = null;
  490.                                     }
  491.                             );
  492.                             
  493.                             
  494.                             if ($('thead',this.gDiv).length<1) //handle if grid has no headers
  495.                             {
  496.                                 $('cell',this).each
  497.                                 (
  498.                                      function ()
  499.                                         {
  500.                                         var td = document.createElement('td');
  501.                                         td.innerHTML = $(this).text();
  502.                                         $(tr).append(td);
  503.                                         td = null;
  504.                                         }
  505.                                 );
  506.                             }
  507.                             
  508.                             $(tbody).append(tr);
  509.                             tr = null;
  510.                             robj = null;
  511.                         }
  512.                 );
  513.                 
  514.                 }
  515.  
  516.                 $('tr',t).unbind();
  517.                 $(t).empty();
  518.                 
  519.                 $(t).append(tbody);
  520.                 this.addCellProp();
  521.                 this.addRowProp();
  522.                 
  523.                 //this.fixHeight($(this.bDiv).height());
  524.                 
  525.                 this.rePosDrag();
  526.                 
  527.                 tbody = null; data = null; i = null; 
  528.                 
  529.                 if (p.onSuccess) p.onSuccess();
  530.                 if (p.hideOnSubmit) $(g.block).remove();//$(t).show();
  531.                 
  532.                 this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  533.                 if ($.browser.opera) $(t).css('visibility','visible');
  534.                 
  535.             },
  536.             
  537.             getPageNum: function(){ 
  538.                             return p.page; 
  539.                         }, 
  540.             
  541.             getNewPageNum: function(){ 
  542.                             return p.newp; 
  543.                         }, 
  544.                         
  545.             changeSort: function(th) { //change sortorder
  546.             
  547.                 if (this.loading) return true;
  548.                 
  549.                 $(g.nDiv).hide();$(g.nBtn).hide();
  550.                 
  551.                 if (p.sortname == $(th).attr('abbr'))
  552.                     {
  553.                         if (p.sortorder=='asc') p.sortorder = 'desc'; 
  554.                         else p.sortorder = 'asc';                        
  555.                     }
  556.                 
  557.                 $(th).addClass('sorted').siblings().removeClass('sorted');
  558.                 $('.sdesc',this.hDiv).removeClass('sdesc');
  559.                 $('.sasc',this.hDiv).removeClass('sasc');
  560.                 $('div',th).addClass('s'+p.sortorder);
  561.                 p.sortname= $(th).attr('abbr');
  562.                 
  563.                 if (p.onChangeSort)
  564.                     p.onChangeSort(p.sortname,p.sortorder);
  565.                 else
  566.                     this.populate();                
  567.             
  568.             },
  569.             buildpager: function(){ //rebuild pager based on new properties
  570.             
  571.             $('.pcontrol input',this.pDiv).val(p.page);
  572.             $('.pcontrol span',this.pDiv).html(p.pages);
  573.             
  574.             var r1 = (p.page-1) * p.rp + 1; 
  575.             var r2 = r1 + p.rp - 1; 
  576.             
  577.             if (p.total<r2) r2 = p.total;
  578.             
  579.             var stat = p.pagestat;
  580.             
  581.             stat = stat.replace(/{from}/,r1);
  582.             stat = stat.replace(/{to}/,r2);
  583.             stat = stat.replace(/{total}/,p.total);
  584.             
  585.             $('.pPageStat',this.pDiv).html(stat);
  586.             
  587.             },
  588.             populate: function () { //get latest data
  589.  
  590.                 if (this.loading) return true;
  591.  
  592.                 if (p.onSubmit)
  593.                     {
  594.                         var gh = p.onSubmit();
  595.                         if (!gh) return false;
  596.                     }
  597.  
  598.                 this.loading = true;
  599.                 if (!p.url) return false;
  600.                 
  601.                 $('.pPageStat',this.pDiv).html(p.procmsg);
  602.                 
  603.                 $('.pReload',this.pDiv).addClass('loading');
  604.                 
  605.                 $(g.block).css({top:g.bDiv.offsetTop});
  606.                 
  607.                 if (p.hideOnSubmit) $(this.gDiv).prepend(g.block); //$(t).hide();
  608.                 
  609.                 if ($.browser.opera) $(t).css('visibility','hidden');
  610.                 
  611.                 if (!p.newp) p.newp = 1;
  612.                 
  613.                 if (p.page>p.pages) p.page = p.pages;
  614.                 //var param = {page:p.newp, rp: p.rp, sortname: p.sortname, sortorder: p.sortorder, query: p.query, qtype: p.qtype};
  615.                 var param = [
  616.                      { name : 'page', value : p.newp }
  617.                     ,{ name : 'rp', value : p.rp }
  618.                     ,{ name : 'sortname', value : p.sortname}
  619.                     ,{ name : 'sortorder', value : p.sortorder }
  620.                     ,{ name : 'query', value : p.query}
  621.                     ,{ name : 'qtype', value : p.qtype}
  622.                 ];                             
  623.                              
  624.                 if (p.params)
  625.                     {
  626.                         for (var pi = 0; pi < p.params.length; pi++) param[param.length] = p.params[pi];
  627.                     }
  628.                 
  629.                     $.ajax({
  630.                        type: p.method,
  631.                        url: p.url,
  632.                        data: param,
  633.                        dataType: p.dataType,
  634.                        success: function(data){ms[pWms[p                = null;
  635.                     <1) //handle if gris[p    xp.unbind();
  636.                     }null;+.append(tbody);
  637.                 this.addCellP