search.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ( function( $ ) {
  2. $( window ).load( function() {
  3. function searchAddClass() {
  4. $( this ).closest( '.search-form' ).addClass( 'hover' );
  5. }
  6. function searchRemoveClass() {
  7. $( this ).closest( '.search-form' ).removeClass( 'hover' );
  8. }
  9. var searchSubmit = $( '.search-submit' );
  10. searchSubmit.hover( searchAddClass, searchRemoveClass );
  11. searchSubmit.focusin( searchAddClass );
  12. searchSubmit.focusout( searchRemoveClass );
  13. } );
  14. } )( jQuery );
  15. ( function() {
  16. var container, button, form, siteHeaderInner, siteNavigation, div;
  17. var windowWidth = window.innerWidth;
  18. container = document.getElementById( 'search-header' );
  19. if ( ! container ) {
  20. return;
  21. }
  22. button = container.getElementsByTagName( 'button' )[0];
  23. if ( 'undefined' === typeof button ) {
  24. return;
  25. }
  26. form = container.getElementsByTagName( 'form' )[0];
  27. if ( 'undefined' === typeof form ) {
  28. button.style.display = 'none';
  29. return;
  30. }
  31. form.setAttribute( 'aria-expanded', 'false' );
  32. siteHeaderInner = document.getElementsByClassName( 'site-header-inner' )[0];
  33. button.onclick = function() {
  34. if ( -1 !== container.className.indexOf( 'toggled' ) ) {
  35. document.body.className = document.body.className.replace( ' search-toggled', '' );
  36. container.className = container.className.replace( ' toggled', '' );
  37. button.setAttribute( 'aria-expanded', 'false' );
  38. form.setAttribute( 'aria-expanded', 'false' );
  39. } else {
  40. document.body.className += ' search-toggled';
  41. container.className += ' toggled';
  42. button.setAttribute( 'aria-expanded', 'true' );
  43. form.setAttribute( 'aria-expanded', 'true' );
  44. }
  45. };
  46. siteNavigation = document.getElementById( 'site-navigation' );
  47. if ( ! siteNavigation ) {
  48. return;
  49. }
  50. div = siteNavigation.getElementsByTagName( 'div' )[0];
  51. function searchPosition() {
  52. if ( window.innerWidth > 599 ) {
  53. siteHeaderInner.appendChild( container );
  54. document.body.className = document.body.className.replace( ' search-toggled', '' );
  55. container.className = container.className.replace( ' toggled', '' );
  56. button.setAttribute( 'aria-expanded', 'false' );
  57. form.setAttribute( 'aria-expanded', 'false' );
  58. } else {
  59. div.insertBefore( container, div.firstChild );
  60. document.body.className += ' search-toggled';
  61. container.className += ' toggled';
  62. button.setAttribute( 'aria-expanded', 'true' );
  63. form.setAttribute( 'aria-expanded', 'true' );
  64. }
  65. }
  66. window.onload = function() {
  67. searchPosition();
  68. };
  69. window.onresize = function() {
  70. // Prevent the Android keyboard from triggering searchPosition
  71. if ( windowWidth !== window.innerWidth ) {
  72. searchPosition();
  73. }
  74. windowWidth = window.innerWidth;
  75. }
  76. } )();