lib/daddy/formatter/screenshot.js in daddy-0.3.26 vs lib/daddy/formatter/screenshot.js in daddy-0.3.27

- old
+ new

@@ -1,18 +1,38 @@ -$(document).ready(function() { - var viewer = $('<div id="screenshot_viewer" style="display: none;"></div>'); - viewer.css({position: 'absolute'}); - viewer.appendTo(document.body); +if (typeof daddy === "undefined") { + var daddy = {}; +} +daddy.screenshot = {}; - $('.screenshot').click(function() { - var clone= $(this).clone().removeClass('screenshot'); - clone.click(function() { - viewer.hide(); - }); +daddy.screenshot.get_or_create_viewer = function() { + if (!this.viewer) { + this.viewer = $('<div id="screenshot_viewer" style="display: none;"></div>'); + this.viewer.css({position: 'absolute'}).appendTo('body'); + this.viewer.show_image = function(image) { + var clone= $(image).clone().removeClass('screenshot'); - viewer.empty().append(clone); - - viewer.css('left', $(window).scrollLeft()); - viewer.css('top', $(window).scrollTop()); - viewer.show(); + this.empty().append(clone); + this.css('left', $(window).scrollLeft()); + this.css('top', $(window).scrollTop()); + this.show(); + }; + this.viewer.close = function() { + this.hide(); + }; + } + + return this.viewer; +}; + +var viewer = null; + +$(document).ready(function() { + viewer = daddy.screenshot.get_or_create_viewer(); + + $('body').click(function(e) { + viewer.close(); + if ($(e.target).hasClass('screenshot')) { + viewer.show_image(e.target); + } }); + });