scripts.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. ( function( $ ) {
  2. 'use strict';
  3. if ( typeof wpcf7 === 'undefined' || wpcf7 === null ) {
  4. return;
  5. }
  6. $( function() {
  7. var welcomePanel = $( '#welcome-panel' );
  8. var updateWelcomePanel;
  9. updateWelcomePanel = function( visible ) {
  10. $.post( ajaxurl, {
  11. action: 'wpcf7-update-welcome-panel',
  12. visible: visible,
  13. welcomepanelnonce: $( '#welcomepanelnonce' ).val()
  14. } );
  15. };
  16. $( 'a.welcome-panel-close', welcomePanel ).click( function( event ) {
  17. event.preventDefault();
  18. welcomePanel.addClass( 'hidden' );
  19. updateWelcomePanel( 0 );
  20. } );
  21. $( '#contact-form-editor' ).tabs( {
  22. active: wpcf7.activeTab,
  23. activate: function( event, ui ) {
  24. $( '#active-tab' ).val( ui.newTab.index() );
  25. }
  26. } );
  27. $( '#contact-form-editor-tabs' ).focusin( function( event ) {
  28. $( '#contact-form-editor .keyboard-interaction' ).css(
  29. 'visibility', 'visible' );
  30. } ).focusout( function( event ) {
  31. $( '#contact-form-editor .keyboard-interaction' ).css(
  32. 'visibility', 'hidden' );
  33. } );
  34. wpcf7.toggleMail2( 'input:checkbox.toggle-form-table' );
  35. $( 'input:checkbox.toggle-form-table' ).click( function( event ) {
  36. wpcf7.toggleMail2( this );
  37. } );
  38. if ( '' === $( '#title' ).val() ) {
  39. $( '#title' ).focus();
  40. }
  41. wpcf7.titleHint();
  42. $( '.contact-form-editor-box-mail span.mailtag' ).click( function( event ) {
  43. var range = document.createRange();
  44. range.selectNodeContents( this );
  45. window.getSelection().addRange( range );
  46. } );
  47. wpcf7.updateConfigErrors();
  48. $( '[data-config-field]' ).change( function() {
  49. var postId = $( '#post_ID' ).val();
  50. if ( ! postId || -1 == postId ) {
  51. return;
  52. }
  53. var data = [];
  54. $( this ).closest( 'form' ).find( '[data-config-field]' ).each( function() {
  55. data.push( {
  56. 'name': $( this ).attr( 'name' ).replace( /^wpcf7-/, '' ).replace( /-/g, '_' ),
  57. 'value': $( this ).val()
  58. } );
  59. } );
  60. data.push( { 'name': 'context', 'value': 'dry-run' } );
  61. $.ajax( {
  62. method: 'POST',
  63. url: wpcf7.apiSettings.getRoute( '/contact-forms/' + postId ),
  64. beforeSend: function( xhr ) {
  65. xhr.setRequestHeader( 'X-WP-Nonce', wpcf7.apiSettings.nonce );
  66. },
  67. data: data
  68. } ).done( function( response ) {
  69. wpcf7.configValidator.errors = response.config_errors;
  70. wpcf7.updateConfigErrors();
  71. } );
  72. } );
  73. $( window ).on( 'beforeunload', function( event ) {
  74. var changed = false;
  75. $( '#wpcf7-admin-form-element :input[type!="hidden"]' ).each( function() {
  76. if ( $( this ).is( ':checkbox, :radio' ) ) {
  77. if ( this.defaultChecked != $( this ).is( ':checked' ) ) {
  78. changed = true;
  79. }
  80. } else if ( $( this ).is( 'select' ) ) {
  81. $( this ).find( 'option' ).each( function() {
  82. if ( this.defaultSelected != $( this ).is( ':selected' ) ) {
  83. changed = true;
  84. }
  85. } );
  86. } else {
  87. if ( this.defaultValue != $( this ).val() ) {
  88. changed = true;
  89. }
  90. }
  91. } );
  92. if ( changed ) {
  93. event.returnValue = wpcf7.saveAlert;
  94. return wpcf7.saveAlert;
  95. }
  96. } );
  97. $( '#wpcf7-admin-form-element' ).submit( function() {
  98. if ( 'copy' != this.action.value ) {
  99. $( window ).off( 'beforeunload' );
  100. }
  101. if ( 'save' == this.action.value ) {
  102. $( '#publishing-action .spinner' ).addClass( 'is-active' );
  103. }
  104. } );
  105. } );
  106. wpcf7.toggleMail2 = function( checkbox ) {
  107. var $checkbox = $( checkbox );
  108. var $fieldset = $( 'fieldset',
  109. $checkbox.closest( '.contact-form-editor-box-mail' ) );
  110. if ( $checkbox.is( ':checked' ) ) {
  111. $fieldset.removeClass( 'hidden' );
  112. } else {
  113. $fieldset.addClass( 'hidden' );
  114. }
  115. };
  116. wpcf7.updateConfigErrors = function() {
  117. var errors = wpcf7.configValidator.errors;
  118. var errorCount = { total: 0 };
  119. $( '[data-config-field]' ).each( function() {
  120. $( this ).removeAttr( 'aria-invalid' );
  121. $( this ).next( 'ul.config-error' ).remove();
  122. var section = $( this ).attr( 'data-config-field' );
  123. if ( errors[ section ] ) {
  124. var $list = $( '<ul></ul>' ).attr( {
  125. 'role': 'alert',
  126. 'class': 'config-error'
  127. } );
  128. $.each( errors[ section ], function( i, val ) {
  129. var $li = $( '<li></li>' ).append(
  130. wpcf7.iconInCircle( '!' )
  131. ).append(
  132. $( '<span class="screen-reader-text"></span>' ).text( wpcf7.configValidator.iconAlt )
  133. ).append( ' ' );
  134. if ( val.link ) {
  135. $li.append(
  136. $( '<a></a>' ).attr( 'href', val.link ).text( val.message )
  137. );
  138. } else {
  139. $li.text( val.message );
  140. }
  141. $li.appendTo( $list );
  142. var tab = section
  143. .replace( /^mail_\d+\./, 'mail.' ).replace( /\..*$/, '' );
  144. if ( ! errorCount[ tab ] ) {
  145. errorCount[ tab ] = 0;
  146. }
  147. errorCount[ tab ] += 1;
  148. errorCount.total += 1;
  149. } );
  150. $( this ).after( $list ).attr( { 'aria-invalid': 'true' } );
  151. }
  152. } );
  153. $( '#contact-form-editor-tabs > li' ).each( function() {
  154. var $item = $( this );
  155. $item.find( '.icon-in-circle' ).remove();
  156. var tab = $item.attr( 'id' ).replace( /-panel-tab$/, '' );
  157. $.each( errors, function( key, val ) {
  158. key = key.replace( /^mail_\d+\./, 'mail.' );
  159. if ( key.replace( /\..*$/, '' ) == tab.replace( '-', '_' ) ) {
  160. var $mark = wpcf7.iconInCircle( '!' );
  161. $item.find( 'a.ui-tabs-anchor' ).first().append( $mark );
  162. return false;
  163. }
  164. } );
  165. var $tabPanelError = $( '#' + tab + '-panel > div.config-error:first' );
  166. $tabPanelError.empty();
  167. if ( errorCount[ tab.replace( '-', '_' ) ] ) {
  168. $tabPanelError.append( wpcf7.iconInCircle( '!' ) );
  169. if ( 1 < errorCount[ tab.replace( '-', '_' ) ] ) {
  170. var manyErrorsInTab = wpcf7.configValidator.manyErrorsInTab
  171. .replace( '%d', errorCount[ tab.replace( '-', '_' ) ] );
  172. $tabPanelError.append( manyErrorsInTab );
  173. } else {
  174. $tabPanelError.append( wpcf7.configValidator.oneErrorInTab );
  175. }
  176. }
  177. } );
  178. $( '#misc-publishing-actions .misc-pub-section.config-error' ).remove();
  179. if ( errorCount.total ) {
  180. var $warning = $( '<div></div>' )
  181. .addClass( 'misc-pub-section config-error' )
  182. .append( wpcf7.iconInCircle( '!' ) );
  183. if ( 1 < errorCount.total ) {
  184. $warning.append(
  185. wpcf7.configValidator.manyErrors.replace( '%d', errorCount.total )
  186. );
  187. } else {
  188. $warning.append( wpcf7.configValidator.oneError );
  189. }
  190. $warning.append( '<br />' ).append(
  191. $( '<a></a>' )
  192. .attr( 'href', wpcf7.configValidator.docUrl )
  193. .text( wpcf7.configValidator.howToCorrect )
  194. );
  195. $( '#misc-publishing-actions' ).append( $warning );
  196. }
  197. };
  198. /**
  199. * Copied from wptitlehint() in wp-admin/js/post.js
  200. */
  201. wpcf7.titleHint = function() {
  202. var $title = $( '#title' );
  203. var $titleprompt = $( '#title-prompt-text' );
  204. if ( '' === $title.val() ) {
  205. $titleprompt.removeClass( 'screen-reader-text' );
  206. }
  207. $titleprompt.click( function() {
  208. $( this ).addClass( 'screen-reader-text' );
  209. $title.focus();
  210. } );
  211. $title.blur( function() {
  212. if ( '' === $(this).val() ) {
  213. $titleprompt.removeClass( 'screen-reader-text' );
  214. }
  215. } ).focus( function() {
  216. $titleprompt.addClass( 'screen-reader-text' );
  217. } ).keydown( function( e ) {
  218. $titleprompt.addClass( 'screen-reader-text' );
  219. $( this ).unbind( e );
  220. } );
  221. };
  222. wpcf7.iconInCircle = function( icon ) {
  223. var $span = $( '<span class="icon-in-circle" aria-hidden="true"></span>' );
  224. return $span.text( icon );
  225. };
  226. wpcf7.apiSettings.getRoute = function( path ) {
  227. var url = wpcf7.apiSettings.root;
  228. url = url.replace(
  229. wpcf7.apiSettings.namespace,
  230. wpcf7.apiSettings.namespace + path );
  231. return url;
  232. };
  233. } )( jQuery );