customize-preview.js 3.3 KB

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