Permalink
Browse files

working with jquery plugin

  • Loading branch information...
1 parent 5c693cf commit abfccac3434e73c0db52341ccb29caa7ca546065 @rlemon committed Jan 20, 2012
Showing with 292 additions and 2 deletions.
  1. +182 −2 prettyprint.user.js
  2. +110 −0 print.css
View
184 prettyprint.user.js
@@ -12,9 +12,8 @@
// @include http://*.stackexchange.com/*
// @include http://answers.onstartups.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
-// @require https://raw.github.com/rlemon/PrettyPrint/master/printpreview.jquery.js
-// ==/UserScript==
+// ==/UserScript==
function EmbedCodeOnPage(jcode) {
var script = document.createElement('script');
@@ -27,6 +26,187 @@ function EmbedFunctionOnPageAndExecute(function_contents) {
}
EmbedFunctionOnPageAndExecute(function() {
+
+/*!
+ * jQuery Print Previw Plugin v1.0
+ *
+ * Copyright 2011, Tim Connell
+ * Licensed under the GPL Version 2 license
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Date: Sun Mar 25 00:00:00 2011 -000
+ *
+ * Modified by rlemon:
+ * Date: Fri Jan 20 2012
+ */
+
+(function($) {
+
+ // Initialization
+ $.fn.printPreview = function() {
+ this.each(function() {
+ $(this).bind('click', function(e) {
+ e.preventDefault();
+ if (!$('#print-modal').length) {
+ $.printPreview.loadPrintPreview($(this).parent('div'));
+ }
+ });
+ });
+ return this;
+ };
+
+ // Private functions
+ var mask, size, print_modal, print_controls;
+ $.printPreview = {
+ loadPrintPreview: function(container) {
+ console.log(container);
+ // Declare DOM objects
+ print_modal = $('<div id="print-modal"></div>');
+ print_controls = $('<div id="print-modal-controls">' +
+ '<a href="#" class="print" title="Print page">Print page</a>' +
+ '<a href="#" class="close" title="Close print preview">Close</a>').hide();
+ var print_frame = $('<iframe id="print-modal-content" scrolling="no" border="0" frameborder="0" name="print-frame" />');
+
+ // Raise print preview window from the dead, zooooooombies
+ print_modal
+ .hide()
+ .append(print_controls)
+ .append(print_frame)
+ .appendTo('body');
+
+ // The frame lives
+ var print_frame_ref = window.frames["print-frame"].document;
+ print_frame_ref.open();
+ print_frame_ref.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
+ '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">' +
+ '<head><title>' + document.title + '</title></head>' +
+ '<body></body>' +
+ '</html>');
+ print_frame_ref.close();
+
+ // Grab contents and apply stylesheet
+ $('body', print_frame_ref).html(container.clone());
+ $('head link[media*=print], head link[media=all]').each(function() {
+ $('head', print_frame_ref).append($(this).clone().attr('media', 'all'));
+ });
+
+ // Disable all links
+ $('a', print_frame_ref).bind('click.printPreview', function(e) {
+ e.preventDefault();
+ });
+
+ // Introduce print styles
+ $('head').append('<style type="text/css">' +
+ '@media print {' +
+ '/* -- Print Preview --*/' +
+ '#print-modal-mask,' +
+ '#print-modal {' +
+ 'display: none !important;' +
+ '}' +
+ '}' +
+ '</style>'
+ );
+
+ // Load mask
+ $.printPreview.loadMask();
+
+ // Disable scrolling
+ $('body').css({overflowY: 'hidden', height: '100%'});
+ $('img', print_frame_ref).load(function() {
+ print_frame.height($('body', print_frame.contents())[0].scrollHeight);
+ });
+
+ // Position modal
+ starting_position = $(window).height() + $(window).scrollTop();
+ var css = {
+ top: starting_position,
+ height: '100%',
+ overflowY: 'auto',
+ zIndex: 10000,
+ display: 'block'
+ }
+ print_modal
+ .css(css)
+ .animate({ top: $(window).scrollTop()}, 400, 'linear', function() {
+ print_controls.fadeIn('slow').focus();
+ });
+ print_frame.height($('body', print_frame.contents())[0].scrollHeight);
+
+ // Bind closure
+ $('a', print_controls).bind('click', function(e) {
+ e.preventDefault();
+ if ($(this).hasClass('print')) { window.print(); }
+ else { $.printPreview.distroyPrintPreview(); }
+ });
+ },
+
+ distroyPrintPreview: function() {
+ print_controls.fadeOut(100);
+ print_modal.animate({ top: $(window).scrollTop() - $(window).height(), opacity: 1}, 400, 'linear', function(){
+ print_modal.remove();
+ $('body').css({overflowY: 'auto', height: 'auto'});
+ });
+ mask.fadeOut('slow', function() {
+ mask.remove();
+ });
+
+ $(document).unbind("keydown.printPreview.mask");
+ mask.unbind("click.printPreview.mask");
+ $(window).unbind("resize.printPreview.mask");
+ },
+
+ /* -- Mask Functions --*/
+ loadMask: function() {
+ size = $.printPreview.sizeUpMask();
+ mask = $('<div id="print-modal-mask" />').appendTo($('body'));
+ mask.css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ width: size[0],
+ height: size[1],
+ display: 'none',
+ opacity: 0,
+ zIndex: 9999,
+ backgroundColor: '#000'
+ });
+
+ mask.css({display: 'block'}).fadeTo('400', 0.75);
+
+ $(window).bind("resize..printPreview.mask", function() {
+ $.printPreview.updateMaskSize();
+ });
+
+ mask.bind("click.printPreview.mask", function(e) {
+ $.printPreview.distroyPrintPreview();
+ });
+
+ $(document).bind("keydown.printPreview.mask", function(e) {
+ if (e.keyCode == 27) { $.printPreview.distroyPrintPreview(); }
+ });
+ },
+
+ sizeUpMask: function() {
+ if ($.browser.msie) {
+ // if there are no scrollbars then use window.height
+ var d = $(document).height(), w = $(window).height();
+ return [
+ window.innerWidth || // ie7+
+ document.documentElement.clientWidth || // ie6
+ document.body.clientWidth, // ie6 quirks mode
+ d - w < 20 ? w : d
+ ];
+ } else { return [$(document).width(), $(document).height()]; }
+ },
+
+ updateMaskSize: function() {
+ var size = $.printPreview.sizeUpMask();
+ mask.css({width: size[0], height: size[1]});
+ }
+ }
+})(jQuery);
+
+
$('div.answer, div.question').each(function() {
var print_button = $('<a>', {
text: 'Print Preview',
View
110 print.css
@@ -0,0 +1,110 @@
+/**
+ * Example Print Preview site
+ *
+ * print.css
+ * author: Tim Connell
+ */
+
+/* -- Hide screen specific elements -- */
+#nav,
+#footer .footer-links,
+#feature a.prev,
+#feature a.next,
+a.print-preview {
+ display: none;
+}
+
+
+/* -- Typography -- */
+body {
+ background: #FFF;
+ color: #000;
+ font-family: Georgia, serif;
+ line-height: 1.2;
+}
+
+p, table, ul, ol, dl, blockquote, pre, code, form {
+ margin: 0 0 1em;
+}
+
+h1,h2,h3,h4,h5 {
+ font-weight: normal;
+ margin: 2em 0 0.5em;
+ text-shadow: rgba(0, 0, 0, 0.44) 1px 1px 2px;
+}
+
+h1 { font-size: 2em; margin: 2em 0 0.25em; }
+h2 { font-size:1.7em; }
+h3 { font-size:1.5em; }
+h4 { font-size:1.2em; }
+h5 { font-size:1em; }
+
+
+/* -- Style site heading -- */
+#header strong {
+ color: #000;
+ display: block;
+ font-weight: normal;
+ font-size: 3em;
+ margin: 0 0 1em;
+ padding: 0;
+ text-shadow: rgba(0, 0, 0, 0.44) 1px 1px 2px;
+}
+
+
+/* -- Add message re print -- */
+#content:before {
+ content: "Your Printing Job has been sent! Thankyou for using this service.";
+ display: block;
+ border: 1px solid #ccc;
+ font-style: italic;
+ margin: 0 0 1em;
+ padding: 8px 10px;
+}
+
+a:link:after,
+a:visited:after {
+ content: " (" attr(href) ") ";
+ font-size: 80%;
+ text-decoration: none;
+}
+
+
+/* -- Accommodate JS driven elements --*/
+#feature > div {
+ border: none;
+ overflow: visible;
+ height: auto;
+}
+
+ #feature div.items {
+ left: auto !important;
+ position: relative;
+ overflow: visible;
+ width: auto;
+ }
+
+ #feature div.items div {
+ float: none;
+ margin: 20px 0;
+ position: relative;
+ }
+
+/* -- Photo galleries -- */
+div.gallery {
+ margin: 1em 0;
+ overflow: hidden;
+}
+
+ div.gallery div.items div {
+ float: left;
+ margin-right: 10px;
+ }
+
+/* -- Footer -- */
+#footer {
+ border-top: 1px solid #cecece;
+ font-size: 0.83em;
+ margin: 2em 0 0;
+ padding: 1em 0 0;
+}

0 comments on commit abfccac

Please sign in to comment.