cashless2ecash

cashless2ecash: pay with cards for digital cash (experimental)
Log | Files | Refs | README

buttons.print.js (4446B)


      1 /*!
      2  * Print button for Buttons and DataTables.
      3  * 2016 SpryMedia Ltd - datatables.net/license
      4  */
      5 
      6 (function( factory ){
      7 	if ( typeof define === 'function' && define.amd ) {
      8 		// AMD
      9 		define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) {
     10 			return factory( $, window, document );
     11 		} );
     12 	}
     13 	else if ( typeof exports === 'object' ) {
     14 		// CommonJS
     15 		module.exports = function (root, $) {
     16 			if ( ! root ) {
     17 				root = window;
     18 			}
     19 
     20 			if ( ! $ || ! $.fn.dataTable ) {
     21 				$ = require('datatables.net')(root, $).$;
     22 			}
     23 
     24 			if ( ! $.fn.dataTable.Buttons ) {
     25 				require('datatables.net-buttons')(root, $);
     26 			}
     27 
     28 			return factory( $, root, root.document );
     29 		};
     30 	}
     31 	else {
     32 		// Browser
     33 		factory( jQuery, window, document );
     34 	}
     35 }(function( $, window, document, undefined ) {
     36 'use strict';
     37 var DataTable = $.fn.dataTable;
     38 
     39 
     40 var _link = document.createElement( 'a' );
     41 
     42 /**
     43  * Clone link and style tags, taking into account the need to change the source
     44  * path.
     45  *
     46  * @param  {node}     el Element to convert
     47  */
     48 var _styleToAbs = function( el ) {
     49 	var url;
     50 	var clone = $(el).clone()[0];
     51 	var linkHost;
     52 
     53 	if ( clone.nodeName.toLowerCase() === 'link' ) {
     54 		clone.href = _relToAbs( clone.href );
     55 	}
     56 
     57 	return clone.outerHTML;
     58 };
     59 
     60 /**
     61  * Convert a URL from a relative to an absolute address so it will work
     62  * correctly in the popup window which has no base URL.
     63  *
     64  * @param  {string} href URL
     65  */
     66 var _relToAbs = function( href ) {
     67 	// Assign to a link on the original page so the browser will do all the
     68 	// hard work of figuring out where the file actually is
     69 	_link.href = href;
     70 	var linkHost = _link.host;
     71 
     72 	// IE doesn't have a trailing slash on the host
     73 	// Chrome has it on the pathname
     74 	if ( linkHost.indexOf('/') === -1 && _link.pathname.indexOf('/') !== 0) {
     75 		linkHost += '/';
     76 	}
     77 
     78 	return _link.protocol+"//"+linkHost+_link.pathname+_link.search;
     79 };
     80 
     81 
     82 DataTable.ext.buttons.print = {
     83 	className: 'buttons-print',
     84 
     85 	text: function ( dt ) {
     86 		return dt.i18n( 'buttons.print', 'Print' );
     87 	},
     88 
     89 	action: function ( e, dt, button, config ) {
     90 		var data = dt.buttons.exportData( config.exportOptions );
     91 		var addRow = function ( d, tag ) {
     92 			var str = '<tr>';
     93 
     94 			for ( var i=0, ien=d.length ; i<ien ; i++ ) {
     95 				str += '<'+tag+'>'+d[i]+'</'+tag+'>';
     96 			}
     97 
     98 			return str + '</tr>';
     99 		};
    100 
    101 		// Construct a table for printing
    102 		var html = '<table class="'+dt.table().node().className+'">';
    103 
    104 		if ( config.header ) {
    105 			html += '<thead>'+ addRow( data.header, 'th' ) +'</thead>';
    106 		}
    107 
    108 		html += '<tbody>';
    109 		for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
    110 			html += addRow( data.body[i], 'td' );
    111 		}
    112 		html += '</tbody>';
    113 
    114 		if ( config.footer && data.footer ) {
    115 			html += '<tfoot>'+ addRow( data.footer, 'th' ) +'</tfoot>';
    116 		}
    117 
    118 		// Open a new window for the printable table
    119 		var win = window.open( '', '' );
    120 		var title = config.title;
    121 
    122 		if ( typeof title === 'function' ) {
    123 			title = title();
    124 		}
    125 
    126 		if ( title.indexOf( '*' ) !== -1 ) {
    127 			title= title.replace( '*', $('title').text() );
    128 		}
    129 
    130 		win.document.close();
    131 
    132 		// Inject the title and also a copy of the style and link tags from this
    133 		// document so the table can retain its base styling. Note that we have
    134 		// to use string manipulation as IE won't allow elements to be created
    135 		// in the host document and then appended to the new window.
    136 		var head = '<title>'+title+'</title>';
    137 		$('style, link').each( function () {
    138 			head += _styleToAbs( this );
    139 		} );
    140 
    141 		try {
    142 			win.document.head.innerHTML = head; // Work around for Edge
    143 		}
    144 		catch (e) {
    145 			$(win.document.head).html( head ); // Old IE
    146 		}
    147 
    148 		// Inject the table and other surrounding information
    149 		win.document.body.innerHTML =
    150 			'<h1>'+title+'</h1>'+
    151 			'<div>'+
    152 				(typeof config.message === 'function' ?
    153 					config.message( dt, button, config ) :
    154 					config.message
    155 				)+
    156 			'</div>'+
    157 			html;
    158 
    159 		$(win.document.body).addClass('dt-print-view');
    160 
    161 		$('img', win.document.body).each( function ( i, img ) {
    162 			img.setAttribute( 'src', _relToAbs( img.getAttribute('src') ) );
    163 		} );
    164 
    165 		if ( config.customize ) {
    166 			config.customize( win );
    167 		}
    168 
    169 		setTimeout( function () {
    170 			if ( config.autoPrint ) {
    171 				win.print(); // blocking - so close will not
    172 				win.close(); // execute until this is done
    173 			}
    174 		}, 250 );
    175 	},
    176 
    177 	title: '*',
    178 
    179 	message: '',
    180 
    181 	exportOptions: {},
    182 
    183 	header: true,
    184 
    185 	footer: false,
    186 
    187 	autoPrint: true,
    188 
    189 	customize: null
    190 };
    191 
    192 
    193 return DataTable.Buttons;
    194 }));