Last active
December 11, 2015 17:28
-
-
Save slickplaid/5595c7ff0b9bee3bbf58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ZoomHandler = (function() { | |
if(typeof require === 'undefined') { | |
return {}; | |
} | |
// Private | |
const defaultZoom = 1; | |
const defaultWidth = 1030; | |
const maxZoom = 2; | |
const maxAutoZoom = 1.5; | |
const minZoom = 0.9; | |
const zoomStep = 0.1; | |
var webFrame = require('web-frame'); | |
function getZoomLevel() { | |
var zoomFactor = webFrame.getZoomFactor(); | |
var width = window.innerWidth * zoomFactor; | |
var zoom = normalizeAuto(width / defaultWidth); | |
return zoom; | |
}; | |
function setZoom() { | |
var zoom = getZoomLevel(); | |
webFrame.setZoomFactor(zoom); | |
localStorage.setItem('zoomLevel', zoom); | |
}; | |
function boundedNormalize(zoom, min, max) { | |
if(typeof zoom === 'undefined') { | |
return defaultZoom; | |
} | |
if(isNaN(zoom)) { | |
return defaultZoom; | |
} | |
if (zoom < min) { | |
return min; | |
} else if (zoom > max) { | |
return max; | |
} else { | |
return zoom; | |
} | |
}; | |
function normalize(zoom) { | |
return boundedNormalize(zoom, minZoom, maxZoom); | |
}; | |
function normalizeAuto(zoom) { | |
return boundedNormalize(zoom, minZoom, maxAutoZoom); | |
} | |
function increaseZoom() { | |
disableAutoResize(); | |
localStorage.setItem('zoomAuto', 0); | |
var zoom = normalize(webFrame.getZoomFactor() + zoomStep); | |
webFrame.setZoomFactor(zoom); | |
localStorage.setItem('zoomLevel', zoom); | |
}; | |
function decreaseZoom() { | |
disableAutoResize(); | |
localStorage.setItem('zoomAuto', 0); | |
var zoom = normalize(webFrame.getZoomFactor() - zoomStep); | |
webFrame.setZoomFactor(zoom); | |
localStorage.setItem('zoomLevel', zoom); | |
}; | |
function resetZoom() { | |
webFrame.setZoomFactor(defaultZoom); | |
localStorage.setItem('zoomLevel', defaultZoom); | |
}; | |
// event handlers | |
function enableAutoResize() { | |
$(window).on('resize.zoomHandler', setZoom); | |
setZoom(); | |
localStorage.setItem('zoomAuto', 1); | |
}; | |
function manualResizeKeybinds(e) { | |
if ((e.which == 107 || e.which == 187 ) && e.ctrlKey) { | |
increaseZoom(); | |
} else if ((e.which == 109 || e.which == 189 ) && e.ctrlKey) { | |
decreaseZoom(); | |
} else if ((e.which == 48 || e.which == 96) && e.ctrlKey) { | |
resetZoom(); | |
} | |
}; | |
function disableAutoResize() { | |
localStorage.setItem('zoomAuto', 0); | |
$(window).off('resize.zoomHandler'); | |
}; | |
function enableManualResize() { | |
$(function() { | |
$(document).on('keydown.zoomHandler', manualResizeKeybinds); | |
}); | |
}; | |
function disableManualResize() { | |
$(document).off('keydown.zoomHandler'); | |
} | |
function init() { | |
var autoResizeEnabled = localStorage.getItem('zoomAuto'); | |
if(autoResizeEnabled === null) { | |
autoResizeEnabled = '1'; | |
localStorage.setItem('zoomAuto', 1); | |
} | |
if(autoResizeEnabled === '1') { | |
enableAutoResize(); | |
} | |
enableManualResize(); | |
var zoom = parseInt(localStorage.getItem('zoomLevel'), 10); | |
zoom = normalize(zoom); | |
webFrame.setZoomFactor(zoom); | |
// Fix odd menu issue | |
var menufix = $('.desktop-close').parent(); | |
menufix.hide(); | |
setTimeout(function() { menufix.show(); }, 100); | |
} | |
// Public | |
var zoomHandler = { | |
getZoom: webFrame.getZoomFactor, | |
increaseZoom, | |
decreaseZoom, | |
resetZoom, | |
enableAutoResize, | |
disableAutoResize, | |
enableManualResize, | |
disableManualResize, | |
init | |
}; | |
return zoomHandler; | |
})(); | |
if(typeof jQuery !== 'undefined') { | |
ZoomHandler.init(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment