Welcome to mirror list, hosted at ThFree Co, Russian Federation.

printliminator.js « src - github.com/CSS-Tricks/The-Printliminator.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91403fb47d6dac54db3d2399b64bb5ce22df33b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Printliminator */
function csstricksPrintliminator( jQ ) {
	// remove conflicts with other javascript libraries
	var $ = jQ.noConflict(),
	history = [],
	flags = {},
	// if local, load local sprite image
	sprite = ( window.location.origin === 'file://' ? '' : '//css-tricks.github.io/The-Printliminator/' ) +
		'printliminator.png',
	// programmically added stylesheets
	styles = '' +
		'._print_controls { position: fixed; top: 25px; right: 25px; width: 162px; height: 182px; z-index: 10000;' +
			'-moz-user-select: none; -webkit-user-select: none; -ms-user-select: none;' +
			'background: url(' + sprite + ') no-repeat; }' +
		'._print_controls_close { position: absolute; top: -20px; right: -20px; width: 33px; height: 33px;' +
			'background: url(' + sprite + ') -222px -3px no-repeat; }' +
		'._print_controls_close:hover { background-position: -222px -39px; }' +
		'._print_controls_remove_graphics, ._print_controls_print, ._print_controls_undo, ._print_controls_stylize {' +
			'position: absolute; height: 74px; width: 74px;' +
			'background: url(' + sprite + ') no-repeat; }' +
		'._print_controls_remove_graphics { top: 6px; left: 6px; background-position: 0px -182px; }' +
		'._print_controls_remove_graphics:hover { background-position: 0 -256px; }' +
		'._print_controls_remove_graphics.active { background-position: 0 -330px; }' +
		'._print_controls_print { top: 83px; left: 83px; background-position: -74px -182px; }' +
		'._print_controls_print:hover { background-position: -74px -256px; }' +
		'._print_controls_print.active { background-position: -74px -330px; }' +
		'._print_controls_undo { top: 83px; left: 6px; background-position: -148px -182px; }' +
		'._print_controls_undo:hover { background-position: -148px -256px; }' +
		'._print_controls_undo.active { background-position: -148px -330px; }' +
		'._print_controls_stylize { top: 6px; left: 83px; background-position: -222px -182px; }' +
		'._print_controls_stylize:hover { background-position: -222px -256px; }' +
		'._print_controls_stylize.active { background-position: -222px -330px; }' +
		'._print_removed { display: none !important; }' +
		'._printliminator_highlight { outline: 3px solid red; }' +
		'@media print{ ._print_controls { display: none; } }',

	printstylesheet = '@media print, screen {' +
		'body { margin:0; padding:0; line-height: 1.4; word-spacing: 1.1pt; letter-spacing: 0.2pt;' +
			'font-family: Garamond, "Times New Roman", serif; color: #000; background: none; font-size: 12pt; }' +
		'h1, h2, h3, h4, h5, h6 { font-family: Helvetica, Arial, sans-serif; }' +
		'h1 { font-size: 19pt; }' +
		'h2 { font-size: 17pt; }' +
		'h3 { font-size: 15pt; }' +
		'h4, h5, h6 { font-size: 12pt; }' +
		'code { font: 10pt Courier, monospace; }' +
		'blockquote { margin: 1.3em; padding: 1em;  font-size: 10pt; }' +
		'hr { background-color: #ccc; }' +
		'img { float: left; margin: 1em 1.5em 1.5em 0; }' +
		'a img { border: none; }' +
		'table { margin: 1px; text-align: left; border-collapse: collapse; }' +
		'th { border: 1px solid #333;  font-weight: bold; }' +
		'td { border: 1px solid #333; }' +
		'th, td { padding: 4px 10px; }' +
		'tfoot { font-style: italic; }' +
		'caption { background: #fff; margin-bottom: 20px; text-align: left; }' +
		'thead {display: table-header-group; }' +
		'tr { page-break-inside: avoid; }' +
		'} @media screen { body { padding: 20px; } }';

	$( '<style id="_print_controls_styles">' )
		.text( styles )
		.appendTo( 'head' );

	$( 'body *:not(._print_controls, ._print_controls *)' )
		.live( 'click', function( e ) {
			e.preventDefault();
			var $done,
				$this = $( this );
			if ( e.altKey ) {
				$done = $( 'body *' )
					.not( '._print_controls, ._print_controls *, style' )
					.not( $this.parents().andSelf() )
					.not( $this.find( '*' ) )
					.addClass( '_print_removed' );
			} else {
				$done = $this;
			}
			$done.addClass( '_print_removed' );
			history.push( $done );
		})
		.live( 'mouseover', function() {
			$(this).addClass( '_printliminator_highlight' );
		})
		.live( 'mouseout', function() {
			$(this).removeClass( '_printliminator_highlight' );
		});

	var $controls = $( '<div class="_print_controls">' )
		.appendTo( 'body' );

	// fix IE6, which doesn't support position: fixed
	if ( $controls.css( 'position' ) !== 'fixed' ) {
		$controls.css( 'position', 'absolute' );
	}

	// Remove Graphics
	$( '<div class="_print_controls_remove_graphics">' )
		.click( function() {
			if ( !flags.removeGraphics ) {
				var indx, $el, bkgd,
					bkgds = [],
					$done = $( 'img, iframe, object, embed, input[type=image], ins' ),
					$item = $( 'body *:not(._print_controls, ._print_controls *)' ),
					len = $item.length;
				for ( indx = 0; indx < len; indx++ ) {
					$el = $item.eq( indx );
					bkgd = $el.css( 'background-image' );
					if ( bkgd !== 'none' ) {
						bkgds.push( [ $el, bkgd ] );
						$el.css( 'background-image', 'none' );
					}
				}
				$done.addClass( '_print_removed' );
				flags.removeGraphics = true;

				history.push( function() {
					flags.removeGraphics = false;
					$done.removeClass( '_print_removed' );
					var $el,
						len = bkgds.length;
					for ( indx = 0; indx < len; indx++ ) {
						$el = bkgds[ indx ][ 0 ];
						$el.css( 'background-image', bkgds[ indx ][ 1 ] );
					}
				});
			}
		})
		.appendTo( $controls );

	// Print Stylize
	$( '<div class="_print_controls_stylize">' )
		.click( function() {
			window.print();
		})
		.appendTo( $controls );

	// Print
	$( '<div class="_print_controls_print">' )
		.click( function() {
			if ( !flags.stylize ) {
				var links = $( 'link[rel="stylesheet"], style:not(#_print_controls_styles)' ).remove(),
				// cache and remove inline styles
				inline = $( 'body *:not(._print_controls, ._print_controls > *, ._print_removed)' ).map( function() {
					var $this = $( this ),
						style = $this.attr( 'style' );
					$this.attr( 'style', '' );
					return {
						el: this,
						style: style
					};
				}),
				print = $( '<style id="_print_controls_printstylesheet">' )
					.text( printstylesheet )
					.appendTo( 'head' );
				flags.stylize = true;

				history.push( function() {
					flags.stylize = false;
					print.remove();
					links.appendTo( 'head' );
					inline.each( function() {
						$( this.el ).attr( 'style', this.style );
					});
				});
			}
		})
		.appendTo( $controls );

	// Close
	$( '<div class="_print_controls_close">' )
		.click( function() {
			$( '._print_controls' ).remove();
		})
		.appendTo( $controls );

	// Undo
	$( '<div class="_print_controls_undo">' )
		.click( function() {
			var last = history.pop();
			if ( last ) {
				if ( typeof last !== 'function' ) {
					last.removeClass( '_print_removed' );
				} else {
					last.call();
				}
			}
		})
		.appendTo( $controls );

	// active state
	$( '._print_controls_remove_graphics, ._print_controls_print, ._print_controls_undo, ._print_controls_stylize' )
		.bind( 'mousedown', function() {
			$( this ).addClass( 'active' );
		})
		.bind( 'mouseleave mouseup', function() {
			$( this ).removeClass( 'active' );
		});

}