Code coverage report for taxi/lib/scripts/screenshot.js

Statements: 2.5% (1 / 40)      Branches: 0% (0 / 22)      Functions: 0% (0 / 3)      Lines: 2.5% (1 / 40)      Ignored: none     

All files » taxi/lib/scripts/ » screenshot.js
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 1141                                                                                                                                                                                                                                  
module.exports = {
 
    /**
     * Initializes screenshots and gathers data to revert changes.
     * This function will also gather information required for
     * screenshots like dimensions of document and view-port.
     *
     * @method init
	 * @param {boolean} needsStitching
     */
    init: function (needsStitching) {
 
        var initData = {
                viewPort: {},
                document: {},
                bodyTransform: {}
            },
 
            de = document.documentElement,
            el = document.createElement('div'),
            body = document.body;
 
        // Get current scroll-position
        initData.viewPort.x = window.pageXOffset || body.scrollLeft || de.scrollLeft;
        initData.viewPort.y = window.pageYOffset || body.scrollTop || de.scrollTop;
 
        // Get current view-port size
        el.style.position = "fixed";
        el.style.top = 0;
        el.style.left = 0;
        el.style.bottom = 0;
        el.style.right = 0;
        de.insertBefore(el, de.firstChild);
        initData.viewPort.width = el.offsetWidth;
        initData.viewPort.height = el.offsetHeight;
        de.removeChild(el);
        //initData.viewPort.width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
        //initData.viewPort.height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
 
        // Get document size & state of scrollbar styles
        initData.document.width = Math.max(body.scrollWidth, body.offsetWidth, de.clientWidth, de.scrollWidth, de.offsetWidth);
        initData.document.height = Math.max(body.scrollHeight, body.offsetHeight, de.clientHeight, de.scrollHeight, de.offsetHeight);
        initData.document.cssHeight = body.style.height;
        initData.document.overflow = body.style.overflow;
 
        // See which transformation property to use and what value it has
        // Needed for scroll-translation without the page actually knowing about it
        if (body.style.webkitTransform !== undefined) {
            initData.bodyTransform.property = 'webkitTransform';
        } else if (body.style.mozTransform !== undefined) {
            initData.bodyTransform.property = 'mozTransform';
        } else if (body.style.msTransform !== undefined) {
            initData.bodyTransform.property = 'msTransform';
        } else if (body.style.oTransform !== undefined) {
            initData.bodyTransform.property = 'oTransform';
        } else {
            initData.bodyTransform.property = 'transform';
        }
        initData.bodyTransform.value = body.style[initData.bodyTransform.property];
		initData.needsStitching = needsStitching;
 
        // Change document
 
        // Reset scrolling through translate
		if (needsStitching) {
			body.style[initData.bodyTransform.property] = 'translate(' + initData.viewPort.x + 'px, ' + initData.viewPort.y + 'px)';
		}
 
        return JSON.stringify(initData);
    },
 
    /**
     * Reverts changes done to the document during the screenshot process
     *
     * @method revert
     * @param {object} initData Data gathered during init-phase
     */
    revert: function (initData) {
        var body = document.body;
 
        // Reset document height (if changed at all)
        body.style.height = initData.document.cssHeight;
 
        // Reset document offset
		if (initData.needsStitching) {
			body.style[initData.bodyTransform.property] = initData.bodyTransform.value;
		}
    },
 
 
    /**
     * Moves document to a specified offset (instead of really scrolling)
     *
     * @param {int} x X offset of document, translating the document without scrolling
     * @param {int} y Y offset of document, translating the document without scrolling
     * @param {int|null} height Height of the document
     * @param {object} initData Data gathered during init-phase
     */
    documentOffset: function (x, y, height, initData) {
        var body = document.body,
            property = initData.bodyTransform.property,
            initX = initData.viewPort.x,
            initY = initData.viewPort.y;
 
		if (initData.needsStitching) {
			body.style[property] = 'translate(' + ((x * -1) + initX) + 'px, ' + ((y * -1) + initY) + 'px)';
 
			if (height) {
				body.style.height = height;
			}
		}
	}
};