woocommerce_admin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* global woocommerce_admin */
  2. jQuery( function ( $ ) {
  3. if ( 'undefined' === typeof woocommerce_admin ) {
  4. return;
  5. }
  6. // Add buttons to product screen.
  7. var $product_screen = $( '.edit-php.post-type-product' ),
  8. $title_action = $product_screen.find( '.page-title-action:first' ),
  9. $blankslate = $product_screen.find( '.woocommerce-BlankState' );
  10. if ( 0 === $blankslate.length ) {
  11. if ( woocommerce_admin.urls.export_products ) {
  12. $title_action.after('<a href="' + woocommerce_admin.urls.export_products + '" class="page-title-action">' + woocommerce_admin.strings.export_products + '</a>');
  13. }
  14. if ( woocommerce_admin.urls.import_products ) {
  15. $title_action.after( '<a href="' + woocommerce_admin.urls.import_products + '" class="page-title-action">' + woocommerce_admin.strings.import_products + '</a>' );
  16. }
  17. } else {
  18. $title_action.hide();
  19. }
  20. // Progress indicators when showing steps.
  21. $( '.woocommerce-progress-form-wrapper .button-next' ).on( 'click', function() {
  22. $('.wc-progress-form-content').block({
  23. message: null,
  24. overlayCSS: {
  25. background: '#fff',
  26. opacity: 0.6
  27. }
  28. });
  29. return true;
  30. } );
  31. // Field validation error tips
  32. $( document.body )
  33. .on( 'wc_add_error_tip', function( e, element, error_type ) {
  34. var offset = element.position();
  35. if ( element.parent().find( '.wc_error_tip' ).length === 0 ) {
  36. element.after( '<div class="wc_error_tip ' + error_type + '">' + woocommerce_admin[error_type] + '</div>' );
  37. element.parent().find( '.wc_error_tip' )
  38. .css( 'left', offset.left + element.width() - ( element.width() / 2 ) - ( $( '.wc_error_tip' ).width() / 2 ) )
  39. .css( 'top', offset.top + element.height() )
  40. .fadeIn( '100' );
  41. }
  42. })
  43. .on( 'wc_remove_error_tip', function( e, element, error_type ) {
  44. element.parent().find( '.wc_error_tip.' + error_type ).fadeOut( '100', function() { $( this ).remove(); } );
  45. })
  46. .on( 'click', function() {
  47. $( '.wc_error_tip' ).fadeOut( '100', function() { $( this ).remove(); } );
  48. })
  49. .on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]', function() {
  50. $( '.wc_error_tip' ).fadeOut( '100', function() { $( this ).remove(); } );
  51. })
  52. .on( 'change', '.wc_input_price[type=text], .wc_input_decimal[type=text], .wc-order-totals #refund_amount[type=text]', function() {
  53. var regex;
  54. if ( $( this ).is( '.wc_input_price' ) || $( this ).is( '#refund_amount' ) ) {
  55. regex = new RegExp( '[^\-0-9\%\\' + woocommerce_admin.mon_decimal_point + ']+', 'gi' );
  56. } else {
  57. regex = new RegExp( '[^\-0-9\%\\' + woocommerce_admin.decimal_point + ']+', 'gi' );
  58. }
  59. var value = $( this ).val();
  60. var newvalue = value.replace( regex, '' );
  61. if ( value !== newvalue ) {
  62. $( this ).val( newvalue );
  63. }
  64. })
  65. .on( 'keyup', '.wc_input_price[type=text], .wc_input_decimal[type=text], .wc_input_country_iso[type=text], .wc-order-totals #refund_amount[type=text]', function() {
  66. var regex, error;
  67. if ( $( this ).is( '.wc_input_price' ) || $( this ).is( '#refund_amount' ) ) {
  68. regex = new RegExp( '[^\-0-9\%\\' + woocommerce_admin.mon_decimal_point + ']+', 'gi' );
  69. error = 'i18n_mon_decimal_error';
  70. } else if ( $( this ).is( '.wc_input_country_iso' ) ) {
  71. regex = new RegExp( '([^A-Z])+|(.){3,}', 'im' );
  72. error = 'i18n_country_iso_error';
  73. } else {
  74. regex = new RegExp( '[^\-0-9\%\\' + woocommerce_admin.decimal_point + ']+', 'gi' );
  75. error = 'i18n_decimal_error';
  76. }
  77. var value = $( this ).val();
  78. var newvalue = value.replace( regex, '' );
  79. if ( value !== newvalue ) {
  80. $( document.body ).triggerHandler( 'wc_add_error_tip', [ $( this ), error ] );
  81. } else {
  82. $( document.body ).triggerHandler( 'wc_remove_error_tip', [ $( this ), error ] );
  83. }
  84. })
  85. .on( 'change', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function() {
  86. var sale_price_field = $( this ), regular_price_field;
  87. if( sale_price_field.attr( 'name' ).indexOf( 'variable' ) !== -1 ) {
  88. regular_price_field = sale_price_field.parents( '.variable_pricing' ).find( '.wc_input_price[name^=variable_regular_price]' );
  89. } else {
  90. regular_price_field = $( '#_regular_price' );
  91. }
  92. var sale_price = parseFloat( window.accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point ) );
  93. var regular_price = parseFloat( window.accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point ) );
  94. if ( sale_price >= regular_price ) {
  95. $( this ).val( '' );
  96. }
  97. })
  98. .on( 'keyup', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function() {
  99. var sale_price_field = $( this ), regular_price_field;
  100. if( sale_price_field.attr( 'name' ).indexOf( 'variable' ) !== -1 ) {
  101. regular_price_field = sale_price_field.parents( '.variable_pricing' ).find( '.wc_input_price[name^=variable_regular_price]' );
  102. } else {
  103. regular_price_field = $( '#_regular_price' );
  104. }
  105. var sale_price = parseFloat( window.accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point ) );
  106. var regular_price = parseFloat( window.accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point ) );
  107. if ( sale_price >= regular_price ) {
  108. $( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18n_sale_less_than_regular_error' ] );
  109. } else {
  110. $( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18n_sale_less_than_regular_error' ] );
  111. }
  112. })
  113. .on( 'init_tooltips', function() {
  114. $( '.tips, .help_tip, .woocommerce-help-tip' ).tipTip( {
  115. 'attribute': 'data-tip',
  116. 'fadeIn': 50,
  117. 'fadeOut': 50,
  118. 'delay': 200
  119. } );
  120. $( '.column-wc_actions .wc-action-button' ).tipTip( {
  121. 'fadeIn': 50,
  122. 'fadeOut': 50,
  123. 'delay': 200
  124. } );
  125. // Add tiptip to parent element for widefat tables
  126. $( '.parent-tips' ).each( function() {
  127. $( this ).closest( 'a, th' ).attr( 'data-tip', $( this ).data( 'tip' ) ).tipTip( {
  128. 'attribute': 'data-tip',
  129. 'fadeIn': 50,
  130. 'fadeOut': 50,
  131. 'delay': 200
  132. } ).css( 'cursor', 'help' );
  133. });
  134. });
  135. // Tooltips
  136. $( document.body ).trigger( 'init_tooltips' );
  137. // wc_input_table tables
  138. $( '.wc_input_table.sortable tbody' ).sortable({
  139. items: 'tr',
  140. cursor: 'move',
  141. axis: 'y',
  142. scrollSensitivity: 40,
  143. forcePlaceholderSize: true,
  144. helper: 'clone',
  145. opacity: 0.65,
  146. placeholder: 'wc-metabox-sortable-placeholder',
  147. start: function( event, ui ) {
  148. ui.item.css( 'background-color', '#f6f6f6' );
  149. },
  150. stop: function( event, ui ) {
  151. ui.item.removeAttr( 'style' );
  152. }
  153. });
  154. // Focus on inputs within the table if clicked instead of trying to sort.
  155. $( '.wc_input_table.sortable tbody input' ).on( 'click', function() {
  156. $( this ).focus();
  157. } );
  158. $( '.wc_input_table .remove_rows' ).click( function() {
  159. var $tbody = $( this ).closest( '.wc_input_table' ).find( 'tbody' );
  160. if ( $tbody.find( 'tr.current' ).length > 0 ) {
  161. var $current = $tbody.find( 'tr.current' );
  162. $current.each( function() {
  163. $( this ).remove();
  164. });
  165. }
  166. return false;
  167. });
  168. var controlled = false;
  169. var shifted = false;
  170. var hasFocus = false;
  171. $( document.body ).bind( 'keyup keydown', function( e ) {
  172. shifted = e.shiftKey;
  173. controlled = e.ctrlKey || e.metaKey;
  174. });
  175. $( '.wc_input_table' ).on( 'focus click', 'input', function( e ) {
  176. var $this_table = $( this ).closest( 'table, tbody' );
  177. var $this_row = $( this ).closest( 'tr' );
  178. if ( ( e.type === 'focus' && hasFocus !== $this_row.index() ) || ( e.type === 'click' && $( this ).is( ':focus' ) ) ) {
  179. hasFocus = $this_row.index();
  180. if ( ! shifted && ! controlled ) {
  181. $( 'tr', $this_table ).removeClass( 'current' ).removeClass( 'last_selected' );
  182. $this_row.addClass( 'current' ).addClass( 'last_selected' );
  183. } else if ( shifted ) {
  184. $( 'tr', $this_table ).removeClass( 'current' );
  185. $this_row.addClass( 'selected_now' ).addClass( 'current' );
  186. if ( $( 'tr.last_selected', $this_table ).length > 0 ) {
  187. if ( $this_row.index() > $( 'tr.last_selected', $this_table ).index() ) {
  188. $( 'tr', $this_table ).slice( $( 'tr.last_selected', $this_table ).index(), $this_row.index() ).addClass( 'current' );
  189. } else {
  190. $( 'tr', $this_table ).slice( $this_row.index(), $( 'tr.last_selected', $this_table ).index() + 1 ).addClass( 'current' );
  191. }
  192. }
  193. $( 'tr', $this_table ).removeClass( 'last_selected' );
  194. $this_row.addClass( 'last_selected' );
  195. } else {
  196. $( 'tr', $this_table ).removeClass( 'last_selected' );
  197. if ( controlled && $( this ).closest( 'tr' ).is( '.current' ) ) {
  198. $this_row.removeClass( 'current' );
  199. } else {
  200. $this_row.addClass( 'current' ).addClass( 'last_selected' );
  201. }
  202. }
  203. $( 'tr', $this_table ).removeClass( 'selected_now' );
  204. }
  205. }).on( 'blur', 'input', function() {
  206. hasFocus = false;
  207. });
  208. // Additional cost and Attribute term tables
  209. $( '.woocommerce_page_wc-settings .shippingrows tbody tr:even, table.attributes-table tbody tr:nth-child(odd)' ).addClass( 'alternate' );
  210. // Show order items on orders page
  211. $( document.body ).on( 'click', '.show_order_items', function() {
  212. $( this ).closest( 'td' ).find( 'table' ).toggle();
  213. return false;
  214. });
  215. // Select availability
  216. $( 'select.availability' ).change( function() {
  217. if ( $( this ).val() === 'all' ) {
  218. $( this ).closest( 'tr' ).next( 'tr' ).hide();
  219. } else {
  220. $( this ).closest( 'tr' ).next( 'tr' ).show();
  221. }
  222. }).change();
  223. // Hidden options
  224. $( '.hide_options_if_checked' ).each( function() {
  225. $( this ).find( 'input:eq(0)' ).change( function() {
  226. if ( $( this ).is( ':checked' ) ) {
  227. $( this ).closest( 'fieldset, tr' ).nextUntil( '.hide_options_if_checked, .show_options_if_checked', '.hidden_option' ).hide();
  228. } else {
  229. $( this ).closest( 'fieldset, tr' ).nextUntil( '.hide_options_if_checked, .show_options_if_checked', '.hidden_option' ).show();
  230. }
  231. }).change();
  232. });
  233. $( '.show_options_if_checked' ).each( function() {
  234. $( this ).find( 'input:eq(0)' ).change( function() {
  235. if ( $( this ).is( ':checked' ) ) {
  236. $( this ).closest( 'fieldset, tr' ).nextUntil( '.hide_options_if_checked, .show_options_if_checked', '.hidden_option' ).show();
  237. } else {
  238. $( this ).closest( 'fieldset, tr' ).nextUntil( '.hide_options_if_checked, .show_options_if_checked', '.hidden_option' ).hide();
  239. }
  240. }).change();
  241. });
  242. // Reviews.
  243. $( 'input#woocommerce_enable_reviews' ).change(function() {
  244. if ( $( this ).is( ':checked' ) ) {
  245. $( '#woocommerce_enable_review_rating' ).closest( 'tr' ).show();
  246. } else {
  247. $( '#woocommerce_enable_review_rating' ).closest( 'tr' ).hide();
  248. }
  249. }).change();
  250. // Attribute term table
  251. $( 'table.attributes-table tbody tr:nth-child(odd)' ).addClass( 'alternate' );
  252. // Toggle gateway on/off.
  253. $( '.wc_gateways' ).on( 'click', '.wc-payment-gateway-method-toggle-enabled', function() {
  254. var $link = $( this ),
  255. $row = $link.closest( 'tr' ),
  256. $toggle = $link.find( '.woocommerce-input-toggle' );
  257. var data = {
  258. action: 'woocommerce_toggle_gateway_enabled',
  259. security: woocommerce_admin.nonces.gateway_toggle,
  260. gateway_id: $row.data( 'gateway_id' )
  261. };
  262. $toggle.addClass( 'woocommerce-input-toggle--loading' );
  263. $.ajax( {
  264. url: woocommerce_admin.ajax_url,
  265. data: data,
  266. dataType : 'json',
  267. type : 'POST',
  268. success: function( response ) {
  269. if ( true === response.data ) {
  270. $toggle.removeClass( 'woocommerce-input-toggle--enabled, woocommerce-input-toggle--disabled' );
  271. $toggle.addClass( 'woocommerce-input-toggle--enabled' );
  272. $toggle.removeClass( 'woocommerce-input-toggle--loading' );
  273. } else if ( false === response.data ) {
  274. $toggle.removeClass( 'woocommerce-input-toggle--enabled, woocommerce-input-toggle--disabled' );
  275. $toggle.addClass( 'woocommerce-input-toggle--disabled' );
  276. $toggle.removeClass( 'woocommerce-input-toggle--loading' );
  277. } else if ( 'needs_setup' === response.data ) {
  278. window.location.href = $link.attr( 'href' );
  279. }
  280. }
  281. } );
  282. return false;
  283. });
  284. $( '#wpbody' ).on( 'click', '#doaction, #doaction2', function() {
  285. var action = $( this ).is( '#doaction' ) ? $( '#bulk-action-selector-top' ).val() : $( '#bulk-action-selector-bottom' ).val();
  286. if ( 'remove_personal_data' === action ) {
  287. return window.confirm( woocommerce_admin.i18n_remove_personal_data_notice );
  288. }
  289. });
  290. });