Advertisement
Guest User

Untitled

a guest
Nov 1st, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DT = {
  2.     /**
  3.      * Get API
  4.      *
  5.      * Take either a DT instance, CSS handler or API, and return
  6.      * just the API of the DT instance
  7.      *
  8.      * @param {object}  dt       DataTables instance API
  9.      * @return {object} DT Api
  10.      */
  11.     get_api: function ( dt ){
  12.         if( ! $.fn.DataTable.isDataTable( dt ) && ! ( dt instanceof $.fn.dataTable.Api ) )
  13.             throw new Error('Not a table or an API instance');
  14.  
  15.         return new $.fn.dataTable.Api( dt );
  16.     },
  17.  
  18.     /**
  19.      * Monitor AJAX Source
  20.      *
  21.      * Monitor the data source for an AJAX sourced DataTable instance,
  22.      * and update the table if any updates are detected
  23.      *
  24.      * @param {object}      dt              DataTables instance API
  25.      * @param {integer}     update_interval Interval to reload table (Default to 4 sec)
  26.      * @param {function}    pause           Closure to control the loop status
  27.      */
  28.     monitor_ajax: function ( dt, update_interval, pause ){
  29.         dt = this.get_api( dt );
  30.  
  31.         // If no pause function, then always unpaused
  32.         if ( typeof pause !== 'function' )
  33.             pause = function(){ return false; };
  34.  
  35.         // Make sure its ajax
  36.         if( ! dt.init().ajax )
  37.             throw new Error('DT.monitor_ajax() was called on a DT instance that does not use AJAX');
  38.  
  39.         var ajax = false;
  40.         var i = 0;
  41.  
  42.         // Check for updates ever N seconds
  43.         var updates_loop = setInterval(function(){
  44.             i++;
  45.  
  46.             // If theres no ajax request running, and not paused, then check
  47.             if( ! ajax && ! pause()) {
  48.                 ajax = true;
  49.                 $.ajax( {
  50.                     type: "GET",
  51.                     dataType: 'json',
  52.                     url: dt.ajax.url(),
  53.                     success: function ( response ) {
  54.                         // Get the proper data source
  55.                         var new_json = (dt.init().ajax.dataSrc
  56.                             ? response[ dt.init().ajax.dataSrc ]
  57.                             : response);
  58.  
  59.                         // Get the data stored by DataTables to compare
  60.                         var dt_json = (dt.init().ajax.dataSrc
  61.                             ? dt.ajax.json()[ dt.init().ajax.dataSrc ]
  62.                             : dt.ajax.json());
  63.  
  64.                         // If were monitoring something that is partition specific,
  65.                         // and the partition has changed, kill the loop
  66.                         if(typeof response.partition !== 'undefined') {
  67.                             // Check if the partition changed, if so, quit this loop
  68.                             if ( response.partition.partition_id !== dt.ajax.json().partition.partition_id ) {
  69.                                 // Don't alert, an alert will be triggered from the partition selector in the header instead
  70.                                 clearInterval( updates_loop );
  71.                                 return;
  72.                             }
  73.                         }
  74.  
  75.                         // Should reload the table if _anything_ was changed (Timestamps, new/deleted records, and status changes)
  76.                         JSON.stringify(new_json) === JSON.stringify(dt_json) || dt.ajax.reload( null, false );
  77.                     },
  78.                     error: function ( xhr, ajaxOptions, thrownError ) {
  79.                         clearInterval(updates_loop);
  80.                         throw new Error( 'Error: ' + thrownError );
  81.                     }
  82.                 } ).done( function () {
  83.                     ajax = false;
  84.                 } );
  85.             }
  86.         }, update_interval || 4000);
  87.  
  88.         // If the DT instance was terminated, end the loop
  89.         dt.one('destroy.interval', function () {
  90.             console.log('DESTROYED');
  91.             clearInterval(updates_loop);
  92.         } );
  93.     },
  94.  
  95.     /**
  96.      * Get Opened Nodes
  97.      *
  98.      * Get the index numbers of the rows with open child nodes
  99.      *
  100.      * @param   {object}        dt          DataTables instance API
  101.      * @param   {integer}       exclude     What row index to exclude
  102.      * @returns {array|boolean}
  103.      */
  104.     get_opened_nodes: function ( dt, exclude ){
  105.         dt = this.get_api( dt );
  106.  
  107.         return dt
  108.                 .rows()
  109.                 .indexes()
  110.                 .filter( function ( value, index ) {
  111.                     return dt.row( index ).child.isShown() && index !== exclude;
  112.                 } ) || false;
  113.     },
  114.  
  115.     /**
  116.      * Keep Table Conditions
  117.      *
  118.      * Keep track of the page length, order, search string and page # in the hash,
  119.      * as well as draw the table with those conditions on page load
  120.      *
  121.      * @param {object}  dt  DataTables instance API
  122.      */
  123.     keep_conditions: function ( dt ){
  124.         dt = DT.get_api( dt );
  125.  
  126.         // Change the URL Hash for every change
  127.         dt
  128.             .on( 'search.dt', function () {
  129.                 update_hash();
  130.             } )
  131.             .on( 'order.dt', function () {
  132.                 update_hash();
  133.             } )
  134.             .on( 'page.dt', function () {
  135.                 update_hash();
  136.             } )
  137.             .on( 'length.dt', function ( e, settings, len ) {
  138.                 update_hash();
  139.             } );
  140.  
  141.         // Process the hash string on load
  142.         process_hash();
  143.  
  144.         // Use the query_string() function to break down the hash string
  145.         // and re-draw the DT instance with the specified settings
  146.         function process_hash(){
  147.             var hash = query_string();
  148.  
  149.             if( ! hash ) return;
  150.  
  151.             if( hash.order )
  152.                 dt.order( hash.order.split(':') );
  153.  
  154.             if( hash.search )
  155.                 dt.search( hash.search );
  156.  
  157.             if( hash.page )
  158.                 dt.page( parseInt(hash.page ) );
  159.  
  160.             if( hash.length )
  161.                 dt.page.len( parseInt( hash.length ) );
  162.  
  163.             dt.draw();
  164.         }
  165.  
  166.         // Update the URL hash by processing the DT instance settings (page,
  167.         // length, search, etc) and setting the URL hash string value
  168.         function update_hash(){
  169.             var hash = [];
  170.  
  171.             if( dt.order()[ 0 ] )
  172.                 hash.push( 'order=' + dt.order()[ 0 ][ 0 ]+':'+dt.order()[ 0 ][ 1 ] );
  173.  
  174.             if( dt.search() )
  175.                 hash.push( 'search='+dt.search() );
  176.  
  177.             if( dt.page.info() )
  178.                 hash.push( 'page='+dt.page.info().page );
  179.  
  180.             if( dt.page.len() )
  181.                 hash.push( 'length='+dt.page.len() );
  182.  
  183.             window.location.hash = hash.join('&');
  184.         }
  185.  
  186.         // Process the URL hash into an object
  187.         function query_string(){
  188.             var query_string = {};
  189.             var query        = window.location.hash.substring(1);
  190.             var vars         = query.split("&");
  191.  
  192.             for ( var i = 0; i < vars.length; i++)  {
  193.                 var pair = vars[ i ].split ( "=");
  194.                 // If first entry with this name
  195.                 if ( typeof query_string[ pair [ 0 ] ] === "undefined") {
  196.                     query_string[ pair [ 0 ] ] = pair [ 1 ];
  197.                     // If second entry with this name
  198.                 } else if ( typeof query_string [ pair [ 0 ] ] === "string") {
  199.                     query_string[ pair [ 0 ] ] = [ query_string [ pair [ 0 ] ], pair[ 1 ] ];
  200.                     // If third or later entry with this name
  201.                 } else {
  202.                     query_string[ pair [ 0 ] ].push ( pair [1 ]);
  203.                 }
  204.             }
  205.  
  206.             return query_string || false;
  207.         }
  208.     }
  209. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement