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

printliminator.js « js - github.com/CSS-Tricks/The-Printliminator.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 23c4a9048e64415e65ccd43c6491ada96fc5b491 (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
function printlimator() {
    //remove conflicts with other javascript libraries
	var $ = jQuery;
	
	//like the hover function, but for mousedown state
	$.fn.active = function(fn1, fn2) {
		var el = this;
		$(el).mousedown(fn1);
		$(document).mouseup(function() {
			fn2.call(el);
		});
		
		return this;
	};
	
	var history = [];
	
	var dont = false;
	$('body *:not(._print_controls, ._print_controls *)').live('click', function (e) {
	    if (!dont) {
	        e.preventDefault();
	        var done;
	        if(e.altKey) {
	        	done = $("body *").not("._print_controls, ._print_controls *, style")
	        			   .not($(this).parents().andSelf())
	        			   .not($(this).find("*"))
	        	.hide();
	       	}
	        else done = $(this).hide();
	        
	        done.addClass("_print_removed");
	        history.push(done);
	    }
	}).live('mouseover', function () {
	    if (!dont) $(this).css('outline', '3px solid red')
	}).live('mouseout', function () {
	    if (!dont) $(this).css('outline', 'none')
	});
	
	var controls = $('<div>').addClass('_print_controls').css({
	    position: 'fixed',
	    top: 25,
	    right: 25,
	    width: 162,
	    height: 182,
	    background: 'url(http://css-tricks.com/examples/ThePrintliminator/images/printliminator2.png) no-repeat',
	    zIndex: 10000
	}).mouseover(function () {
	    dont = true
	}).mouseout(function () {
	    dont = false
	}).appendTo('body');
	
	//fix IE6, which doesn't support position: fixed
	if (controls.css('position') != 'fixed') {
	    controls.css('position', 'absolute');
	}
	
	//Remove Graphics
	$('<div>').css({
		background: 'url(http://css-tricks.com/examples/ThePrintliminator/images/printliminator2.png) 0 -182px no-repeat',
		position: 'absolute',
		top: 6,
		left: 6,
		width: 74,
		height: 74
    }).click(function () {
    	var done = $('img,iframe,object,embed,input[type=image],ins').hide();
    	var bg = $('body *:not(._print_controls, ._print_controls *)').css('background');
    	var item = $('body *:not(._print_controls, ._print_controls *)').css('background', 'none');
    	
    	done.addClass("_print_removed");
    	history.push(function() {
    		done.show();
    		item.css('background', bg);
    	});
	}).hover(function(){
	   $(this).css({ "background-position": "0 -256px" }) }, function() {
	   $(this).css({ "background-position": "0 -182px" }) 
    }).active(function(){
	   $(this).css({ "background-position": "0 -330px" }) }, function() {
	   $(this).css({ "background-position": "0 -182px" }) 
    }).appendTo(controls);
	
	// Print Stylize
	$('<div>').css({
		background: 'url(http://css-tricks.com/examples/ThePrintliminator/images/printliminator2.png) no-repeat -222px -182px',
		position: 'absolute',
		top: 6,
		left: 83,
		width: 74,
		height: 74
    }).click(function () {
		window.print();
	}).hover(function(){
	   $(this).css({ "background-position": "-222px -256px" }) }, function() {
	   $(this).css({ "background-position": "-222px -182px" }) 
    }).active(function(){
	   $(this).css({ "background-position": "-222px -330px" }) }, function() {
	   $(this).css({ "background-position": "-222px -182px" }) 
    }).appendTo(controls);
	
	//Print
	$('<div>').css({
		background: 'url(http://css-tricks.com/examples/ThePrintliminator/images/printliminator2.png) -74px -182px no-repeat',
		position: 'absolute',
		top: 83,
		left: 83,
		width: 74,
		height: 74
    }).hover(function(){
	   $(this).css({ "background-position": "-74px -256px" }) }, function() {
	   $(this).css({ "background-position": "-74px -182px" }) 
    }).active(function(){
	   $(this).css({ "background-position": "-74px -330px" }) }, function() {
	   $(this).css({ "background-position": "-74px -182px" }) 
    }).click(function () {
		var links = $("link[rel='stylesheet'], style:not(#_print_control_styles)").remove();
		//cache and remove inline styles
		var inline = $('body *:not(._print_controls, ._print_controls > *, ._print_removed)').map(function() {
			var style = $(this).attr("style");
			$(this).attr("style", "");
			return {
				el: this,
				style: style
			};
		});
		var print = $("<link rel='stylesheet' type='text/css' href='http://css-tricks.com/examples/ThePrintliminator/css/printliminator.css'/>").appendTo("head");
		
		history.push(function() {
			print.remove();
			links.appendTo("head");
			inline.each(function() {
				$(this.el).attr("style", this.style);
			});
		});
	}).appendTo(controls);
	
	//Close
	$('<div>').css({
		background: 'url(http://css-tricks.com/examples/ThePrintliminator/images/printliminator2.png) -222px -3px no-repeat',
		position: 'absolute',
		top: -20,
		right: -20,
		width: 33,
		height: 33
	}).hover(function(){
	   $(this).css({ "background-position": "-222px -39px" }) }, function(){
	   $(this).css({ "background-position": "-222px -3px" })
	}).click(function(){
		$('._print_controls').remove();
    }).appendTo(controls);
    
    $("<div>").css({
        background: 'url(http://css-tricks.com/examples/ThePrintliminator/images/printliminator2.png) -148px -182px no-repeat',
		position: 'absolute',
		top: 83,
		left: 6,
		width: 74,
		height: 74
    }).hover(function(){
	   $(this).css({ "background-position": "-148px -256px" }) }, function() {
	   $(this).css({ "background-position": "-148px -182px" }) 
    }).active(function(){
	   $(this).css({ "background-position": "-148px -330px" }) }, function() {
	   $(this).css({ "background-position": "-148px -182px" }) 
    }).click(function() {
    	var last = history.pop();
    	if(last) {
    		if(typeof last != 'function')
    			last.removeClass("_print_removed").show();
    		else last.call();
    	}
    }).appendTo(controls);
	
	//make sure that the controls don't get printed
	$('<style id="_print_control_styles">').text('@media print{._print_controls{display:none;}}').appendTo("head");
}