No Description

woocommerce_admin.js 15KB

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