Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DT = {
- /**
- * Get API
- *
- * Take either a DT instance, CSS handler or API, and return
- * just the API of the DT instance
- *
- * @param {object} dt DataTables instance API
- * @return {object} DT Api
- */
- get_api: function ( dt ){
- if( ! $.fn.DataTable.isDataTable( dt ) && ! ( dt instanceof $.fn.dataTable.Api ) )
- throw new Error('Not a table or an API instance');
- return new $.fn.dataTable.Api( dt );
- },
- /**
- * Monitor AJAX Source
- *
- * Monitor the data source for an AJAX sourced DataTable instance,
- * and update the table if any updates are detected
- *
- * @param {object} dt DataTables instance API
- * @param {integer} update_interval Interval to reload table (Default to 4 sec)
- * @param {function} pause Closure to control the loop status
- */
- monitor_ajax: function ( dt, update_interval, pause ){
- dt = this.get_api( dt );
- // If no pause function, then always unpaused
- if ( typeof pause !== 'function' )
- pause = function(){ return false; };
- // Make sure its ajax
- if( ! dt.init().ajax )
- throw new Error('DT.monitor_ajax() was called on a DT instance that does not use AJAX');
- var ajax = false;
- var i = 0;
- // Check for updates ever N seconds
- var updates_loop = setInterval(function(){
- i++;
- // If theres no ajax request running, and not paused, then check
- if( ! ajax && ! pause()) {
- ajax = true;
- $.ajax( {
- type: "GET",
- dataType: 'json',
- url: dt.ajax.url(),
- success: function ( response ) {
- // Get the proper data source
- var new_json = (dt.init().ajax.dataSrc
- ? response[ dt.init().ajax.dataSrc ]
- : response);
- // Get the data stored by DataTables to compare
- var dt_json = (dt.init().ajax.dataSrc
- ? dt.ajax.json()[ dt.init().ajax.dataSrc ]
- : dt.ajax.json());
- // If were monitoring something that is partition specific,
- // and the partition has changed, kill the loop
- if(typeof response.partition !== 'undefined') {
- // Check if the partition changed, if so, quit this loop
- if ( response.partition.partition_id !== dt.ajax.json().partition.partition_id ) {
- // Don't alert, an alert will be triggered from the partition selector in the header instead
- clearInterval( updates_loop );
- return;
- }
- }
- // Should reload the table if _anything_ was changed (Timestamps, new/deleted records, and status changes)
- JSON.stringify(new_json) === JSON.stringify(dt_json) || dt.ajax.reload( null, false );
- },
- error: function ( xhr, ajaxOptions, thrownError ) {
- clearInterval(updates_loop);
- throw new Error( 'Error: ' + thrownError );
- }
- } ).done( function () {
- ajax = false;
- } );
- }
- }, update_interval || 4000);
- // If the DT instance was terminated, end the loop
- dt.one('destroy.interval', function () {
- console.log('DESTROYED');
- clearInterval(updates_loop);
- } );
- },
- /**
- * Get Opened Nodes
- *
- * Get the index numbers of the rows with open child nodes
- *
- * @param {object} dt DataTables instance API
- * @param {integer} exclude What row index to exclude
- * @returns {array|boolean}
- */
- get_opened_nodes: function ( dt, exclude ){
- dt = this.get_api( dt );
- return dt
- .rows()
- .indexes()
- .filter( function ( value, index ) {
- return dt.row( index ).child.isShown() && index !== exclude;
- } ) || false;
- },
- /**
- * Keep Table Conditions
- *
- * Keep track of the page length, order, search string and page # in the hash,
- * as well as draw the table with those conditions on page load
- *
- * @param {object} dt DataTables instance API
- */
- keep_conditions: function ( dt ){
- dt = DT.get_api( dt );
- // Change the URL Hash for every change
- dt
- .on( 'search.dt', function () {
- update_hash();
- } )
- .on( 'order.dt', function () {
- update_hash();
- } )
- .on( 'page.dt', function () {
- update_hash();
- } )
- .on( 'length.dt', function ( e, settings, len ) {
- update_hash();
- } );
- // Process the hash string on load
- process_hash();
- // Use the query_string() function to break down the hash string
- // and re-draw the DT instance with the specified settings
- function process_hash(){
- var hash = query_string();
- if( ! hash ) return;
- if( hash.order )
- dt.order( hash.order.split(':') );
- if( hash.search )
- dt.search( hash.search );
- if( hash.page )
- dt.page( parseInt(hash.page ) );
- if( hash.length )
- dt.page.len( parseInt( hash.length ) );
- dt.draw();
- }
- // Update the URL hash by processing the DT instance settings (page,
- // length, search, etc) and setting the URL hash string value
- function update_hash(){
- var hash = [];
- if( dt.order()[ 0 ] )
- hash.push( 'order=' + dt.order()[ 0 ][ 0 ]+':'+dt.order()[ 0 ][ 1 ] );
- if( dt.search() )
- hash.push( 'search='+dt.search() );
- if( dt.page.info() )
- hash.push( 'page='+dt.page.info().page );
- if( dt.page.len() )
- hash.push( 'length='+dt.page.len() );
- window.location.hash = hash.join('&');
- }
- // Process the URL hash into an object
- function query_string(){
- var query_string = {};
- var query = window.location.hash.substring(1);
- var vars = query.split("&");
- for ( var i = 0; i < vars.length; i++) {
- var pair = vars[ i ].split ( "=");
- // If first entry with this name
- if ( typeof query_string[ pair [ 0 ] ] === "undefined") {
- query_string[ pair [ 0 ] ] = pair [ 1 ];
- // If second entry with this name
- } else if ( typeof query_string [ pair [ 0 ] ] === "string") {
- query_string[ pair [ 0 ] ] = [ query_string [ pair [ 0 ] ], pair[ 1 ] ];
- // If third or later entry with this name
- } else {
- query_string[ pair [ 0 ] ].push ( pair [1 ]);
- }
- }
- return query_string || false;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement