From cd97e49c52c68f18f8110472ac39361137574b4f Mon Sep 17 00:00:00 2001 From: Mottie Date: Fri, 21 Aug 2015 22:36:24 -0500 Subject: Add flags to prevent repeated modifications This also makes the undo easier to use; click add print stylesheet multiple times, but only click undo once to remove it --- js/printliminator.js | 93 ++++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/js/printliminator.js b/js/printliminator.js index a58ad18..acce311 100644 --- a/js/printliminator.js +++ b/js/printliminator.js @@ -3,6 +3,7 @@ function csstricksPrintliminator( jQ ) { // remove conflicts with other javascript libraries var $ = jQ.noConflict(), history = [], + flags = {}, dont = false, // programmically added stylesheets root = '', // '//css-tricks.com/examples/ThePrintliminator/'; @@ -96,30 +97,34 @@ function csstricksPrintliminator( jQ ) { // Remove Graphics $( '
' ) .click( function() { - 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' ); - - history.push( function() { - $done.removeClass( '_print_removed' ); - var $el, - len = bkgds.length; + 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 = bkgds[ indx ][ 0 ]; - $el.css( 'background-image', bkgds[ indx ][ 1 ] ); + $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 ); @@ -133,28 +138,32 @@ function csstricksPrintliminator( jQ ) { // Print $( '
' ) .click( function() { - 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 = $( '