jquery.layout.browserZoom.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @preserve jquery.layout.browserZoom 1.0
  3. * $Date: 2011-12-29 08:00:00 (Thu, 29 Dec 2011) $
  4. *
  5. * Copyright (c) 2012
  6. * Kevin Dalman (http://allpro.net)
  7. *
  8. * Dual licensed under the GPL (http://www.gnu.org/licenses/gpl.html)
  9. * and MIT (http://www.opensource.org/licenses/mit-license.php) licenses.
  10. *
  11. * @dependancies: UI Layout 1.3.0.rc30.1 or higher
  12. *
  13. * @support: http://groups.google.com/group/jquery-ui-layout
  14. *
  15. * @todo: Extend logic to handle other problematic zooming in browsers
  16. * @todo: Add hotkey/mousewheel bindings to _instantly_ respond to these zoom event
  17. */
  18. ;(function ($) {
  19. var _ = $.layout;
  20. // tell Layout that the plugin is available
  21. _.plugins.browserZoom = true;
  22. _.defaults.browserZoomCheckInterval = 1000;
  23. _.optionsMap.layout.push("browserZoomCheckInterval");
  24. /*
  25. * browserZoom methods
  26. */
  27. _.browserZoom = {
  28. _init: function (inst) {
  29. $.layout.browserZoom._setTimer(inst);
  30. }
  31. , _setTimer: function (inst) {
  32. if (inst.destroyed) return;
  33. var o = inst.options
  34. , s = inst.state
  35. , z = s.browserZoom = $.layout.browserZoom.ratio()
  36. ;
  37. if (o.resizeWithWindow && z !== false) {
  38. setTimeout(function(){
  39. if (inst.destroyed) return;
  40. var d = $.layout.browserZoom.ratio();
  41. if (d !== s.browserZoom) {
  42. s.browserZoom = d;
  43. inst.resizeAll();
  44. }
  45. $.layout.browserZoom._setTimer(inst); // set a NEW timeout
  46. }, Math.max( o.browserZoomCheckInterval, 100 )); // MINIMUM 100ms interval, for performance
  47. }
  48. }
  49. , ratio: function () {
  50. var w = window
  51. , s = screen
  52. , d = document
  53. , dE = d.documentElement || d.body
  54. , b = $.layout.browser
  55. , v = b.version
  56. , r, sW, cW
  57. ;
  58. // we can ignore all browsers that fire window.resize event onZoom
  59. if ((b.msie && v > 8)
  60. || !b.msie
  61. ) return false; // don't need to track zoom
  62. if (s.deviceXDPI)
  63. return calc(s.deviceXDPI, s.systemXDPI);
  64. // everything below is just for future reference!
  65. if (b.webkit && (r = d.body.getBoundingClientRect))
  66. return calc((r.left - r.right), d.body.offsetWidth);
  67. if (b.webkit && (sW = w.outerWidth))
  68. return calc(sW, w.innerWidth);
  69. if ((sW = s.width) && (cW = dE.clientWidth))
  70. return calc(sW, cW);
  71. return false; // no match, so cannot - or don't need to - track zoom
  72. function calc (x,y) { return (parseInt(x,10) / parseInt(y,10) * 100).toFixed(); }
  73. }
  74. };
  75. // add initialization method to Layout's onLoad array of functions
  76. _.onReady.push( $.layout.browserZoom._init );
  77. })( jQuery );