customize-preview.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * File customize-preview.js.
  3. *
  4. * Brings logo resizing technology to the Customizer.
  5. *
  6. * Contains handlers to make Customizer preview changes asynchronously.
  7. */
  8. ( function( $ ) {
  9. var api = wp.customize;
  10. var Logo = new photoBlogLogo();
  11. var initial = null;
  12. var resizeTimer;
  13. api( 'custom_logo', function( value ) {
  14. handleLogoDetection( value() );
  15. value.bind( handleLogoDetection );
  16. } );
  17. api( 'logo_size', function( value ) {
  18. Logo.resize( value() );
  19. value.bind( Logo.resize );
  20. } );
  21. api( 'ready', function() {
  22. initial = api( 'custom_logo' )._value;
  23. } );
  24. function handleLogoDetection( to, initial ) {
  25. if ( '' === to ) {
  26. Logo.remove();
  27. } else if ( undefined === initial ) {
  28. Logo.add();
  29. } else {
  30. Logo.change();
  31. }
  32. initial = to;
  33. }
  34. function photoBlogLogo() {
  35. var intId = 0;
  36. var hasLogo = null;
  37. var min = 48;
  38. var self = {
  39. resize: function( to ) {
  40. if ( hasLogo ) {
  41. var img = new Image();
  42. var logo = $( '.custom-logo' );
  43. var size = {
  44. width: parseInt( logo.attr( 'width' ), 10 ),
  45. height: parseInt( logo.attr( 'height' ), 10 ),
  46. }
  47. var cssMax = {
  48. width: parseInt( logo.css( 'max-width' ), 10 ),
  49. height: parseInt( logo.css( 'max-height' ), 10 ),
  50. }
  51. var max = new Object();
  52. max.width = $.isNumeric( cssMax.width ) ? cssMax.width : size.width;
  53. max.height = $.isNumeric( cssMax.height ) ? cssMax.height : size.height;
  54. img.onload = function() {
  55. var output = new Object();
  56. if ( size.width >= size.height ) {
  57. // landscape or square, calculate height as short side
  58. output = logo_min_max( size.height, size.width, max.height, max.width, to, min );
  59. size = {
  60. height: output.a,
  61. width: output.b,
  62. }
  63. } else if ( size.width < size.height ){
  64. // portrait, calculate height as long side
  65. output = logo_min_max( size.width, size.height, max.width, max.height, to, min );
  66. size = {
  67. height: output.b,
  68. width: output.a,
  69. }
  70. }
  71. logo.css( {
  72. width: size.width,
  73. height: size.height,
  74. } );
  75. }
  76. img.src = logo.attr( 'src' );
  77. clearTimeout( resizeTimer );
  78. resizeTimer = setTimeout( function() {
  79. $( document.body ).resize();
  80. }, 500 );
  81. }
  82. },
  83. add: function() {
  84. intID = setInterval( function() {
  85. var logo = $( '.custom-logo[src]' );
  86. if ( logo.length ) {
  87. clearInterval( intID );
  88. hasLogo = true;
  89. }
  90. }, 500 );
  91. },
  92. change: function() {
  93. var oldlogo = $( '.custom-logo' ).attr( 'src' );
  94. intID = setInterval( function() {
  95. var logo = $( '.custom-logo' ).attr( 'src' );
  96. if ( logo != oldlogo ) {
  97. clearInterval( intID );
  98. hasLogo = true;
  99. self.resize( 50 );
  100. }
  101. }, 100 );
  102. },
  103. remove: function() {
  104. hasLogo = null;
  105. }
  106. }
  107. return self;
  108. }
  109. // a is short side, b is long side
  110. // x is short css max, y is long css max
  111. // p is percent, m is minimum short side
  112. function logo_min_max( a, b, amax, bmax, p, m ){
  113. var ppp,
  114. ratio,
  115. max = new Object(),
  116. size = new Object();
  117. ratio = ( b / a );
  118. max.b = ( bmax >= b ) ? b : bmax;
  119. max.a = ( amax >= ( max.b / ratio ) ) ? Math.floor( max.b / ratio ) : amax;
  120. // number of pixels per percentage point
  121. ppp = ( max.a - m ) / 100;
  122. // at 0%, the minimum is set, scale up from there
  123. size.a = Math.floor( m + ( p * ppp ) );
  124. // long side is calculated from the image ratio
  125. size.b = Math.floor( size.a * ratio );
  126. return size;
  127. }
  128. } )( jQuery );