Нет описания

wc-setup.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*global wc_setup_params */
  2. /*global wc_setup_currencies */
  3. /*global wc_base_state */
  4. /* @deprecated 4.6.0 */
  5. jQuery( function( $ ) {
  6. function blockWizardUI() {
  7. $('.wc-setup-content').block({
  8. message: null,
  9. overlayCSS: {
  10. background: '#fff',
  11. opacity: 0.6
  12. }
  13. });
  14. }
  15. $( '.button-next' ).on( 'click', function() {
  16. var form = $( this ).parents( 'form' ).get( 0 );
  17. if ( ( 'function' !== typeof form.checkValidity ) || form.checkValidity() ) {
  18. blockWizardUI();
  19. }
  20. return true;
  21. } );
  22. $( 'form.address-step' ).on( 'submit', function( e ) {
  23. var form = $( this );
  24. if ( ( 'function' !== typeof form.checkValidity ) || form.checkValidity() ) {
  25. blockWizardUI();
  26. }
  27. e.preventDefault();
  28. $('.wc-setup-content').unblock();
  29. $( this ).WCBackboneModal( {
  30. template: 'wc-modal-tracking-setup'
  31. } );
  32. $( document.body ).on( 'wc_backbone_modal_response', function() {
  33. form.off( 'submit' ).trigger( 'submit' );
  34. } );
  35. $( '#wc_tracker_checkbox_dialog' ).on( 'change', function( e ) {
  36. var eventTarget = $( e.target );
  37. $( '#wc_tracker_checkbox' ).prop( 'checked', eventTarget.prop( 'checked' ) );
  38. } );
  39. $( '#wc_tracker_submit' ).on( 'click', function () {
  40. form.off( 'submit' ).trigger( 'submit' );
  41. } );
  42. return true;
  43. } );
  44. $( '#store_country' ).on( 'change', function() {
  45. // Prevent if we don't have the metabox data
  46. if ( wc_setup_params.states === null ){
  47. return;
  48. }
  49. var $this = $( this ),
  50. country = $this.val(),
  51. $state_select = $( '#store_state' );
  52. if ( ! $.isEmptyObject( wc_setup_params.states[ country ] ) ) {
  53. var states = wc_setup_params.states[ country ];
  54. $state_select.empty();
  55. $.each( states, function( index ) {
  56. $state_select.append( $( '<option value="' + index + '">' + states[ index ] + '</option>' ) );
  57. } );
  58. $( '.store-state-container' ).show();
  59. $state_select.selectWoo().val( wc_base_state ).trigger( 'change' ).prop( 'required', true );
  60. } else {
  61. $( '.store-state-container' ).hide();
  62. $state_select.empty().val( '' ).trigger( 'change' ).prop( 'required', false );
  63. }
  64. $( '#currency_code' ).val( wc_setup_currencies[ country ] ).trigger( 'change' );
  65. } );
  66. /* Setup postcode field and validations */
  67. $( '#store_country' ).on( 'change', function() {
  68. if ( ! wc_setup_params.postcodes ) {
  69. return;
  70. }
  71. var $this = $( this ),
  72. country = $this.val(),
  73. $store_postcode_input = $( '#store_postcode' ),
  74. country_postcode_obj = wc_setup_params.postcodes[ country ];
  75. // Default to required, if its unknown whether postcode is required or not.
  76. if ( $.isEmptyObject( country_postcode_obj ) || country_postcode_obj.required ) {
  77. $store_postcode_input.attr( 'required', 'true' );
  78. } else {
  79. $store_postcode_input.prop( 'required', false );
  80. }
  81. } );
  82. $( '#store_country' ).trigger( 'change' );
  83. $( '.wc-wizard-services' ).on( 'change', '.wc-wizard-service-enable input', function() {
  84. if ( $( this ).is( ':checked' ) ) {
  85. $( this ).closest( '.wc-wizard-service-toggle' ).removeClass( 'disabled' );
  86. $( this ).closest( '.wc-wizard-service-item' ).addClass( 'checked' );
  87. $( this ).closest( '.wc-wizard-service-item' )
  88. .find( '.wc-wizard-service-settings' ).removeClass( 'hide' );
  89. } else {
  90. $( this ).closest( '.wc-wizard-service-toggle' ).addClass( 'disabled' );
  91. $( this ).closest( '.wc-wizard-service-item' ).removeClass( 'checked' );
  92. $( this ).closest( '.wc-wizard-service-item' )
  93. .find( '.wc-wizard-service-settings' ).addClass( 'hide' );
  94. }
  95. } );
  96. $( '.wc-wizard-services' ).on( 'keyup', function( e ) {
  97. var code = e.keyCode || e.which,
  98. $focused = $( document.activeElement );
  99. if ( $focused.is( '.wc-wizard-service-toggle, .wc-wizard-service-enable' ) && ( 13 === code || 32 === code ) ) {
  100. $focused.find( ':input' ).trigger( 'click' );
  101. }
  102. } );
  103. $( '.wc-wizard-services' ).on( 'click', '.wc-wizard-service-enable', function( e ) {
  104. var eventTarget = $( e.target );
  105. if ( eventTarget.is( 'input' ) ) {
  106. e.stopPropagation();
  107. return;
  108. }
  109. var $checkbox = $( this ).find( 'input[type="checkbox"]' );
  110. $checkbox.prop( 'checked', ! $checkbox.prop( 'checked' ) ).trigger( 'change' );
  111. } );
  112. $( '.wc-wizard-services-list-toggle' ).on( 'click', function() {
  113. var listToggle = $( this ).closest( '.wc-wizard-services-list-toggle' );
  114. if ( listToggle.hasClass( 'closed' ) ) {
  115. listToggle.removeClass( 'closed' );
  116. } else {
  117. listToggle.addClass( 'closed' );
  118. }
  119. $( this ).closest( '.wc-wizard-services' ).find( '.wc-wizard-service-item' )
  120. .slideToggle()
  121. .css( 'display', 'flex' );
  122. } );
  123. $( '.wc-wizard-services' ).on( 'change', '.wc-wizard-shipping-method-select .method', function( e ) {
  124. var zone = $( this ).closest( '.wc-wizard-service-description' );
  125. var selectedMethod = e.target.value;
  126. var description = zone.find( '.shipping-method-descriptions' );
  127. description.find( '.shipping-method-description' ).addClass( 'hide' );
  128. description.find( '.' + selectedMethod ).removeClass( 'hide' );
  129. var $checkbox = zone.parent().find( 'input[type="checkbox"]' );
  130. var settings = zone.find( '.shipping-method-settings' );
  131. settings
  132. .find( '.shipping-method-setting' )
  133. .addClass( 'hide' )
  134. .find( '.shipping-method-required-field' )
  135. .prop( 'required', false );
  136. settings
  137. .find( '.' + selectedMethod )
  138. .removeClass( 'hide' )
  139. .find( '.shipping-method-required-field' )
  140. .prop( 'required', $checkbox.prop( 'checked' ) );
  141. } ).find( '.wc-wizard-shipping-method-select .method' ).trigger( 'change' );
  142. $( '.wc-wizard-services' ).on( 'change', '.wc-wizard-shipping-method-enable', function() {
  143. var checked = $( this ).is( ':checked' );
  144. var selectedMethod = $( this )
  145. .closest( '.wc-wizard-service-item' )
  146. .find( '.wc-wizard-shipping-method-select .method' )
  147. .val();
  148. $( this )
  149. .closest( '.wc-wizard-service-item' )
  150. .find( '.' + selectedMethod )
  151. .find( '.shipping-method-required-field' )
  152. .prop( 'required', checked );
  153. } );
  154. function submitActivateForm() {
  155. $( 'form.activate-jetpack' ).trigger( 'submit' );
  156. }
  157. function waitForJetpackInstall() {
  158. wp.ajax.post( 'setup_wizard_check_jetpack' )
  159. .then( function( result ) {
  160. // If we receive success, or an unexpected result
  161. // let the form submit.
  162. if (
  163. ! result ||
  164. ! result.is_active ||
  165. 'yes' === result.is_active
  166. ) {
  167. return submitActivateForm();
  168. }
  169. // Wait until checking the status again
  170. setTimeout( waitForJetpackInstall, 3000 );
  171. } )
  172. .fail( function() {
  173. // Submit the form as normal if the request fails
  174. submitActivateForm();
  175. } );
  176. }
  177. // Wait for a pending Jetpack install to finish before triggering a "save"
  178. // on the activate step, which launches the Jetpack connection flow.
  179. $( '.activate-jetpack' ).on( 'click', '.button-primary', function( e ) {
  180. blockWizardUI();
  181. if ( 'no' === wc_setup_params.pending_jetpack_install ) {
  182. return true;
  183. }
  184. e.preventDefault();
  185. waitForJetpackInstall();
  186. } );
  187. $( '.activate-new-onboarding' ).on( 'click', '.button-primary', function() {
  188. // Show pending spinner while activate happens.
  189. blockWizardUI();
  190. } );
  191. $( '.wc-wizard-services' ).on( 'change', 'input#stripe_create_account, input#ppec_paypal_reroute_requests', function() {
  192. if ( $( this ).is( ':checked' ) ) {
  193. $( this ).closest( '.wc-wizard-service-settings' )
  194. .find( 'input.payment-email-input' )
  195. .attr( 'type', 'email' )
  196. .prop( 'disabled', false )
  197. .prop( 'required', true );
  198. } else {
  199. $( this ).closest( '.wc-wizard-service-settings' )
  200. .find( 'input.payment-email-input' )
  201. .attr( 'type', null )
  202. .prop( 'disabled', true )
  203. .prop( 'required', false );
  204. }
  205. } ).find( 'input#stripe_create_account, input#ppec_paypal_reroute_requests' ).trigger( 'change' );
  206. function addPlugins( bySlug, $el, hover ) {
  207. var plugins = $el.data( 'plugins' );
  208. for ( var i in Array.isArray( plugins ) ? plugins : [] ) {
  209. var slug = plugins[ i ].slug;
  210. bySlug[ slug ] = bySlug[ slug ] ||
  211. $( '<span class="plugin-install-info-list-item">' )
  212. .append( '<a href="https://wordpress.org/plugins/' + slug + '/" target="_blank">' + plugins[ i ].name + '</a>' );
  213. bySlug[ slug ].find( 'a' )
  214. .on( 'mouseenter mouseleave', ( function( $hover, event ) {
  215. $hover.toggleClass( 'plugin-install-source', 'mouseenter' === event.type );
  216. } ).bind( null, hover ? $el.closest( hover ) : $el ) );
  217. }
  218. }
  219. function updatePluginInfo() {
  220. var pluginLinkBySlug = {};
  221. var extraPlugins = [];
  222. $( '.wc-wizard-service-enable input:checked' ).each( function() {
  223. addPlugins( pluginLinkBySlug, $( this ), '.wc-wizard-service-item' );
  224. var $container = $( this ).closest( '.wc-wizard-service-item' );
  225. $container.find( 'input.payment-checkbox-input:checked' ).each( function() {
  226. extraPlugins.push( $( this ).attr( 'id' ) );
  227. addPlugins( pluginLinkBySlug, $( this ), '.wc-wizard-service-settings' );
  228. } );
  229. $container.find( '.wc-wizard-shipping-method-select .method' ).each( function() {
  230. var $this = $( this );
  231. if ( 'live_rates' === $this.val() ) {
  232. addPlugins( pluginLinkBySlug, $this, '.wc-wizard-service-item' );
  233. }
  234. } );
  235. } );
  236. $( '.recommended-item input:checked' ).each( function() {
  237. addPlugins( pluginLinkBySlug, $( this ), '.recommended-item' );
  238. } );
  239. var $list = $( 'span.plugin-install-info-list' ).empty();
  240. for ( var slug in pluginLinkBySlug ) {
  241. $list.append( pluginLinkBySlug[ slug ] );
  242. }
  243. if (
  244. extraPlugins &&
  245. wc_setup_params.current_step &&
  246. wc_setup_params.i18n.extra_plugins[ wc_setup_params.current_step ] &&
  247. wc_setup_params.i18n.extra_plugins[ wc_setup_params.current_step ][ extraPlugins.join( ',' ) ]
  248. ) {
  249. $list.append(
  250. wc_setup_params.i18n.extra_plugins[ wc_setup_params.current_step ][ extraPlugins.join( ',' ) ]
  251. );
  252. }
  253. $( 'span.plugin-install-info' ).toggle( $list.children().length > 0 );
  254. }
  255. updatePluginInfo();
  256. $( '.wc-setup-content' ).on( 'change', '[data-plugins]', updatePluginInfo );
  257. $( document.body ).on( 'init_tooltips', function() {
  258. $( '.help_tip' ).tipTip( {
  259. 'attribute': 'data-tip',
  260. 'fadeIn': 50,
  261. 'fadeOut': 50,
  262. 'delay': 200,
  263. 'defaultPosition': 'top'
  264. } );
  265. } ).trigger( 'init_tooltips' );
  266. } );