From 3f648beb1f398f9e060811521e942eef5ff296d4 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 23 Apr 2015 13:35:27 +0100 Subject: [PATCH] Improve JS and fix decimal validation Fixes #7996 --- assets/js/admin/woocommerce_admin.js | 166 +++++++++-------------- assets/js/admin/woocommerce_admin.min.js | 2 +- 2 files changed, 66 insertions(+), 102 deletions(-) diff --git a/assets/js/admin/woocommerce_admin.js b/assets/js/admin/woocommerce_admin.js index 6d13f19bec1..09641bc6c9e 100644 --- a/assets/js/admin/woocommerce_admin.js +++ b/assets/js/admin/woocommerce_admin.js @@ -5,117 +5,81 @@ */ jQuery( function ( $ ) { - // Price input validation - $( document.body ).on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]', function() { - $('.wc_error_tip').fadeOut('100', function(){ $(this).remove(); } ); - return this; - }); + // Field validation error tips + $( document.body ) + .on( 'wc_add_error_tip', function( e, element, error_type ) { + var offset = element.position(); - $( document.body ).on('keyup change', '.wc_input_price[type=text]', function(){ - var value = $(this).val(); - var regex = new RegExp( "[^\-0-9\%.\\" + woocommerce_admin.mon_decimal_point + "]+", "gi" ); - var newvalue = value.replace( regex, '' ); - - if ( value !== newvalue ) { - $(this).val( newvalue ); - if ( $(this).parent().find('.wc_error_tip').size() == 0 ) { - var offset = $(this).position(); - $(this).after( '
' + woocommerce_admin.i18n_mon_decimal_error + '
' ); - $('.wc_error_tip') - .css('left', offset.left + $(this).width() - ( $(this).width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) ) - .css('top', offset.top + $(this).height() ) + if ( element.parent().find('.wc_error_tip').size() == 0 ) { + element.after( '
' + woocommerce_admin[error_type] + '
' ); + element.parent().find('.wc_error_tip') + .css('left', offset.left + element.width() - ( element.width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) ) + .css('top', offset.top + element.height() ) .fadeIn('100'); } - } - return this; - }); + }) + .on( 'wc_remove_error_tip', function( e, element, error_type ) { + element.parent().find('.wc_error_tip.' + error_type).remove(); + }) + .on( 'click', function() { + $('.wc_error_tip').fadeOut('100', function() { $(this).remove(); } ); + }) + .on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]', function() { + $('.wc_error_tip').fadeOut('100', function() { $(this).remove(); } ); + }) + .on( 'keyup change', '.wc_input_price[type=text]', function() { + var value = $(this).val(); + var regex = new RegExp( "[^\-0-9\%\\" + woocommerce_admin.mon_decimal_point + "]+", "gi" ); + var newvalue = value.replace( regex, '' ); - $( document.body ).on('keyup change', '.wc_input_decimal[type=text]', function(){ - var value = $(this).val(); - var regex = new RegExp( "[^\-0-9\%.\\" + woocommerce_admin.decimal_point + "]+", "gi" ); - var newvalue = value.replace( regex, '' ); - - if ( value !== newvalue ) { - $(this).val( newvalue ); - if ( $(this).parent().find('.wc_error_tip').size() === 0 ) { - var offset = $(this).position(); - $(this).after( '
' + woocommerce_admin.i18n_decimal_error + '
' ); - $('.wc_error_tip') - .css('left', offset.left + $(this).width() - ( $(this).width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) ) - .css('top', offset.top + $(this).height() ) - .fadeIn('100'); + if ( value !== newvalue ) { + $(this).val( newvalue ); + $( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18n_mon_decimal_error' ] ); + } else { + $( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18n_mon_decimal_error' ] ); } - } - return this; - }); + }) + .on( 'keyup change', '.wc_input_decimal[type=text]', function() { + var value = $(this).val(); + var regex = new RegExp( "[^\-0-9\%\\" + woocommerce_admin.decimal_point + "]+", "gi" ); + var newvalue = value.replace( regex, '' ); - $( document.body ).on( 'keyup', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function(){ - var sale_price_field = $(this); - - if( sale_price_field.attr('name').indexOf('variable') !== -1 ) { - var regular_price_field = sale_price_field.parents('.variable_pricing').find('.wc_input_price[name^=variable_regular_price]'); - } else { - var regular_price_field = $('#_regular_price'); - } - - var sale_price = parseFloat( accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point ) ); - var regular_price = parseFloat( accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point ) ); - - if( sale_price >= regular_price ) { - if ( $(this).parent().find('.wc_error_tip').size() === 0 ) { - var offset = $(this).position(); - $(this).after( '
' + woocommerce_admin.i18_sale_less_than_regular_error + '
' ); - $('.wc_error_tip') - .css('left', offset.left + $(this).width() - ( $(this).width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) ) - .css('top', offset.top + $(this).height() ) - .fadeIn('100'); + if ( value !== newvalue ) { + $(this).val( newvalue ); + $( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18n_decimal_error' ] ); + } else { + $( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18n_decimal_error' ] ); } - } else { - $('.wc_error_tip').fadeOut('100', function(){ $(this).remove(); } ); - } - return this; - }); - $( document.body ).on( 'change', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function(){ - var sale_price_field = $(this); + }) + .on( 'keyup change', '.wc_input_country_iso[type=text]', function() { + var value = $(this).val(); + var regex = new RegExp( '^([A-Z])?([A-Z])$' ); - if( sale_price_field.attr('name').indexOf('variable') !== -1 ) { - var regular_price_field = sale_price_field.parents('.variable_pricing').find('.wc_input_price[name^=variable_regular_price]'); - } else { - var regular_price_field = $('#_regular_price'); - } - - var sale_price = parseFloat( accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point ) ); - var regular_price = parseFloat( accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point ) ); - - if( sale_price >= regular_price ) { - sale_price_field.val( regular_price_field.val() ); - } else { - $('.wc_error_tip').fadeOut('100', function(){ $(this).remove(); } ); - } - return this; - }); - - $( document.body ).on('keyup change', '.wc_input_country_iso[type=text]', function(){ - var value = $(this).val(); - var regex = new RegExp( '^([A-Z])?([A-Z])$' ); - - if ( ! regex.test( value ) ) { - $(this).val( '' ); - if ( $(this).parent().find('.wc_error_tip').size() === 0 ) { - var offset = $(this).position(); - $(this).after( '
' + woocommerce_admin.i18n_country_iso_error + '
' ); - $('.wc_error_tip') - .css('left', offset.left + $(this).width() - ( $(this).width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) ) - .css('top', offset.top + $(this).height() ) - .fadeIn('100'); + if ( ! regex.test( value ) ) { + $(this).val( '' ); + $( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18n_country_iso_error' ] ); + } else { + $( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18n_country_iso_error' ] ); } - } - return this; - }); + }) + .on( 'keyup change', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function() { + var sale_price_field = $(this); - $("body").click(function(){ - $('.wc_error_tip').fadeOut('100', function(){ $(this).remove(); } ); - }); + if( sale_price_field.attr('name').indexOf('variable') !== -1 ) { + var regular_price_field = sale_price_field.parents('.variable_pricing').find('.wc_input_price[name^=variable_regular_price]'); + } else { + var regular_price_field = $('#_regular_price'); + } + + var sale_price = parseFloat( accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point ) ); + var regular_price = parseFloat( accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point ) ); + + if ( sale_price >= regular_price ) { + $( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18_sale_less_than_regular_error' ] ); + } else { + $( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18_sale_less_than_regular_error' ] ); + } + }); // Tooltips var tiptip_args = { diff --git a/assets/js/admin/woocommerce_admin.min.js b/assets/js/admin/woocommerce_admin.min.js index 299d211bb03..8c09a250bec 100644 --- a/assets/js/admin/woocommerce_admin.min.js +++ b/assets/js/admin/woocommerce_admin.min.js @@ -1 +1 @@ -jQuery(function(a){a(document.body).on("blur",".wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]",function(){return a(".wc_error_tip").fadeOut("100",function(){a(this).remove()}),this}),a(document.body).on("keyup change",".wc_input_price[type=text]",function(){var b=a(this).val(),c=new RegExp("[^-0-9%.\\"+woocommerce_admin.mon_decimal_point+"]+","gi"),d=b.replace(c,"");if(b!==d&&(a(this).val(d),0==a(this).parent().find(".wc_error_tip").size())){var e=a(this).position();a(this).after('
'+woocommerce_admin.i18n_mon_decimal_error+"
"),a(".wc_error_tip").css("left",e.left+a(this).width()-a(this).width()/2-a(".wc_error_tip").width()/2).css("top",e.top+a(this).height()).fadeIn("100")}return this}),a(document.body).on("keyup change",".wc_input_decimal[type=text]",function(){var b=a(this).val(),c=new RegExp("[^-0-9%.\\"+woocommerce_admin.decimal_point+"]+","gi"),d=b.replace(c,"");if(b!==d&&(a(this).val(d),0===a(this).parent().find(".wc_error_tip").size())){var e=a(this).position();a(this).after('
'+woocommerce_admin.i18n_decimal_error+"
"),a(".wc_error_tip").css("left",e.left+a(this).width()-a(this).width()/2-a(".wc_error_tip").width()/2).css("top",e.top+a(this).height()).fadeIn("100")}return this}),a(document.body).on("keyup","#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]",function(){var b=a(this);if(-1!==b.attr("name").indexOf("variable"))var c=b.parents(".variable_pricing").find(".wc_input_price[name^=variable_regular_price]");else var c=a("#_regular_price");var d=parseFloat(accounting.unformat(b.val(),woocommerce_admin.mon_decimal_point)),e=parseFloat(accounting.unformat(c.val(),woocommerce_admin.mon_decimal_point));if(d>=e){if(0===a(this).parent().find(".wc_error_tip").size()){var f=a(this).position();a(this).after('
'+woocommerce_admin.i18_sale_less_than_regular_error+"
"),a(".wc_error_tip").css("left",f.left+a(this).width()-a(this).width()/2-a(".wc_error_tip").width()/2).css("top",f.top+a(this).height()).fadeIn("100")}}else a(".wc_error_tip").fadeOut("100",function(){a(this).remove()});return this}),a(document.body).on("change","#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]",function(){var b=a(this);if(-1!==b.attr("name").indexOf("variable"))var c=b.parents(".variable_pricing").find(".wc_input_price[name^=variable_regular_price]");else var c=a("#_regular_price");var d=parseFloat(accounting.unformat(b.val(),woocommerce_admin.mon_decimal_point)),e=parseFloat(accounting.unformat(c.val(),woocommerce_admin.mon_decimal_point));return d>=e?b.val(c.val()):a(".wc_error_tip").fadeOut("100",function(){a(this).remove()}),this}),a(document.body).on("keyup change",".wc_input_country_iso[type=text]",function(){var b=a(this).val(),c=new RegExp("^([A-Z])?([A-Z])$");if(!c.test(b)&&(a(this).val(""),0===a(this).parent().find(".wc_error_tip").size())){var d=a(this).position();a(this).after('
'+woocommerce_admin.i18n_country_iso_error+"
"),a(".wc_error_tip").css("left",d.left+a(this).width()-a(this).width()/2-a(".wc_error_tip").width()/2).css("top",d.top+a(this).height()).fadeIn("100")}return this}),a("body").click(function(){a(".wc_error_tip").fadeOut("100",function(){a(this).remove()})});var b={attribute:"data-tip",fadeIn:50,fadeOut:50,delay:200};a(".tips, .help_tip").tipTip(b),a(".parent-tips").each(function(){a(this).closest("a, th").attr("data-tip",a(this).data("tip")).tipTip(b).css("cursor","help")}),a(".wc_input_table.sortable tbody").sortable({items:"tr",cursor:"move",axis:"y",scrollSensitivity:40,forcePlaceholderSize:!0,helper:"clone",opacity:.65,placeholder:"wc-metabox-sortable-placeholder",start:function(a,b){b.item.css("background-color","#f6f6f6")},stop:function(a,b){b.item.removeAttr("style")}}),a(".wc_input_table .remove_rows").click(function(){var b=a(this).closest(".wc_input_table").find("tbody");return b.find("tr.current").size()>0&&($current=b.find("tr.current"),$current.each(function(){a(this).remove()})),!1});var c=!1,d=!1,e=!1;a(document).bind("keyup keydown",function(a){d=a.shiftKey,c=a.ctrlKey||a.metaKey}),a(".wc_input_table").on("focus click","input",function(b){$this_table=a(this).closest("table"),$this_row=a(this).closest("tr"),("focus"==b.type&&e!=$this_row.index()||"click"==b.type&&a(this).is(":focus"))&&(e=$this_row.index(),d||c?d?(a("tr",$this_table).removeClass("current"),$this_row.addClass("selected_now").addClass("current"),a("tr.last_selected",$this_table).size()>0&&($this_row.index()>a("tr.last_selected, $this_table").index()?a("tr",$this_table).slice(a("tr.last_selected",$this_table).index(),$this_row.index()).addClass("current"):a("tr",$this_table).slice($this_row.index(),a("tr.last_selected",$this_table).index()+1).addClass("current")),a("tr",$this_table).removeClass("last_selected"),$this_row.addClass("last_selected")):(a("tr",$this_table).removeClass("last_selected"),c&&a(this).closest("tr").is(".current")?$this_row.removeClass("current"):$this_row.addClass("current").addClass("last_selected")):(a("tr",$this_table).removeClass("current").removeClass("last_selected"),$this_row.addClass("current").addClass("last_selected")),a("tr",$this_table).removeClass("selected_now"))}).on("blur","input",function(){e=!1}),a(".woocommerce_page_wc-settings .shippingrows tbody tr:even").addClass("alternate"),a("select.availability").change(function(){"all"==a(this).val()?a(this).closest("tr").next("tr").hide():a(this).closest("tr").next("tr").show()}).change(),a(document.body).on("click",".show_order_items",function(){return a(this).closest("td").find("table").toggle(),!1}),a(".hide_options_if_checked").each(function(){a(this).find("input:eq(0)").change(function(){a(this).is(":checked")?a(this).closest("fieldset, tr").nextUntil(".hide_options_if_checked, .show_options_if_checked",".hidden_option").hide():a(this).closest("fieldset, tr").nextUntil(".hide_options_if_checked, .show_options_if_checked",".hidden_option").show()}).change()}),a(".show_options_if_checked").each(function(){a(this).find("input:eq(0)").change(function(){a(this).is(":checked")?a(this).closest("fieldset, tr").nextUntil(".hide_options_if_checked, .show_options_if_checked",".hidden_option").show():a(this).closest("fieldset, tr").nextUntil(".hide_options_if_checked, .show_options_if_checked",".hidden_option").hide()}).change()}),a("input#woocommerce_demo_store").change(function(){a(this).is(":checked")?a("#woocommerce_demo_store_notice").closest("tr").show():a("#woocommerce_demo_store_notice").closest("tr").hide()}).change(),a("table.attributes-table tbody tr:nth-child(odd)").addClass("alternate"),"undefined"!=typeof woocommerce_admin&&"undefined"!=typeof woocommerce_admin.qrcode_key&&a("#qrcode_small").qrcode({text:woocommerce_admin.qrcode_key,width:90,height:90})}); \ No newline at end of file +jQuery(function(a){a(document.body).on("wc_add_error_tip",function(b,c,d){var e=c.position();0==c.parent().find(".wc_error_tip").size()&&(c.after('
'+woocommerce_admin[d]+"
"),c.parent().find(".wc_error_tip").css("left",e.left+c.width()-c.width()/2-a(".wc_error_tip").width()/2).css("top",e.top+c.height()).fadeIn("100"))}).on("wc_remove_error_tip",function(a,b,c){b.parent().find(".wc_error_tip."+c).remove()}).on("click",function(){a(".wc_error_tip").fadeOut("100",function(){a(this).remove()})}).on("blur",".wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]",function(){a(".wc_error_tip").fadeOut("100",function(){a(this).remove()})}).on("keyup change",".wc_input_price[type=text]",function(){var b=a(this).val(),c=new RegExp("[^-0-9%\\"+woocommerce_admin.mon_decimal_point+"]+","gi"),d=b.replace(c,"");b!==d?(a(this).val(d),a(document.body).triggerHandler("wc_add_error_tip",[a(this),"i18n_mon_decimal_error"])):a(document.body).triggerHandler("wc_remove_error_tip",[a(this),"i18n_mon_decimal_error"])}).on("keyup change",".wc_input_decimal[type=text]",function(){var b=a(this).val(),c=new RegExp("[^-0-9%\\"+woocommerce_admin.decimal_point+"]+","gi"),d=b.replace(c,"");b!==d?(a(this).val(d),a(document.body).triggerHandler("wc_add_error_tip",[a(this),"i18n_decimal_error"])):a(document.body).triggerHandler("wc_remove_error_tip",[a(this),"i18n_decimal_error"])}).on("keyup change",".wc_input_country_iso[type=text]",function(){var b=a(this).val(),c=new RegExp("^([A-Z])?([A-Z])$");c.test(b)?a(document.body).triggerHandler("wc_remove_error_tip",[a(this),"i18n_country_iso_error"]):(a(this).val(""),a(document.body).triggerHandler("wc_add_error_tip",[a(this),"i18n_country_iso_error"]))}).on("keyup change","#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]",function(){var b=a(this);if(-1!==b.attr("name").indexOf("variable"))var c=b.parents(".variable_pricing").find(".wc_input_price[name^=variable_regular_price]");else var c=a("#_regular_price");var d=parseFloat(accounting.unformat(b.val(),woocommerce_admin.mon_decimal_point)),e=parseFloat(accounting.unformat(c.val(),woocommerce_admin.mon_decimal_point));d>=e?a(document.body).triggerHandler("wc_add_error_tip",[a(this),"i18_sale_less_than_regular_error"]):a(document.body).triggerHandler("wc_remove_error_tip",[a(this),"i18_sale_less_than_regular_error"])});var b={attribute:"data-tip",fadeIn:50,fadeOut:50,delay:200};a(".tips, .help_tip").tipTip(b),a(".parent-tips").each(function(){a(this).closest("a, th").attr("data-tip",a(this).data("tip")).tipTip(b).css("cursor","help")}),a(".wc_input_table.sortable tbody").sortable({items:"tr",cursor:"move",axis:"y",scrollSensitivity:40,forcePlaceholderSize:!0,helper:"clone",opacity:.65,placeholder:"wc-metabox-sortable-placeholder",start:function(a,b){b.item.css("background-color","#f6f6f6")},stop:function(a,b){b.item.removeAttr("style")}}),a(".wc_input_table .remove_rows").click(function(){var b=a(this).closest(".wc_input_table").find("tbody");return b.find("tr.current").size()>0&&($current=b.find("tr.current"),$current.each(function(){a(this).remove()})),!1});var c=!1,d=!1,e=!1;a(document).bind("keyup keydown",function(a){d=a.shiftKey,c=a.ctrlKey||a.metaKey}),a(".wc_input_table").on("focus click","input",function(b){$this_table=a(this).closest("table"),$this_row=a(this).closest("tr"),("focus"==b.type&&e!=$this_row.index()||"click"==b.type&&a(this).is(":focus"))&&(e=$this_row.index(),d||c?d?(a("tr",$this_table).removeClass("current"),$this_row.addClass("selected_now").addClass("current"),a("tr.last_selected",$this_table).size()>0&&($this_row.index()>a("tr.last_selected, $this_table").index()?a("tr",$this_table).slice(a("tr.last_selected",$this_table).index(),$this_row.index()).addClass("current"):a("tr",$this_table).slice($this_row.index(),a("tr.last_selected",$this_table).index()+1).addClass("current")),a("tr",$this_table).removeClass("last_selected"),$this_row.addClass("last_selected")):(a("tr",$this_table).removeClass("last_selected"),c&&a(this).closest("tr").is(".current")?$this_row.removeClass("current"):$this_row.addClass("current").addClass("last_selected")):(a("tr",$this_table).removeClass("current").removeClass("last_selected"),$this_row.addClass("current").addClass("last_selected")),a("tr",$this_table).removeClass("selected_now"))}).on("blur","input",function(){e=!1}),a(".woocommerce_page_wc-settings .shippingrows tbody tr:even").addClass("alternate"),a("select.availability").change(function(){"all"==a(this).val()?a(this).closest("tr").next("tr").hide():a(this).closest("tr").next("tr").show()}).change(),a(document.body).on("click",".show_order_items",function(){return a(this).closest("td").find("table").toggle(),!1}),a(".hide_options_if_checked").each(function(){a(this).find("input:eq(0)").change(function(){a(this).is(":checked")?a(this).closest("fieldset, tr").nextUntil(".hide_options_if_checked, .show_options_if_checked",".hidden_option").hide():a(this).closest("fieldset, tr").nextUntil(".hide_options_if_checked, .show_options_if_checked",".hidden_option").show()}).change()}),a(".show_options_if_checked").each(function(){a(this).find("input:eq(0)").change(function(){a(this).is(":checked")?a(this).closest("fieldset, tr").nextUntil(".hide_options_if_checked, .show_options_if_checked",".hidden_option").show():a(this).closest("fieldset, tr").nextUntil(".hide_options_if_checked, .show_options_if_checked",".hidden_option").hide()}).change()}),a("input#woocommerce_demo_store").change(function(){a(this).is(":checked")?a("#woocommerce_demo_store_notice").closest("tr").show():a("#woocommerce_demo_store_notice").closest("tr").hide()}).change(),a("table.attributes-table tbody tr:nth-child(odd)").addClass("alternate"),"undefined"!=typeof woocommerce_admin&&"undefined"!=typeof woocommerce_admin.qrcode_key&&a("#qrcode_small").qrcode({text:woocommerce_admin.qrcode_key,width:90,height:90})}); \ No newline at end of file