woocommerce/assets/js/admin/meta-boxes-product-variatio...

1 line
12 KiB
JavaScript

jQuery(function(a){a(".wc-metaboxes-wrapper").on("click","a.bulk_edit",function(){var b,c,d,e=a("select#field_to_edit").val();switch(e){case"toggle_enabled":b=a('input[name^="variable_enabled"]'),b.attr("checked",!b.attr("checked"));break;case"toggle_downloadable":b=a('input[name^="variable_is_downloadable"]'),b.attr("checked",!b.attr("checked")),a("input.variable_is_downloadable").change();break;case"toggle_virtual":b=a('input[name^="variable_is_virtual"]'),b.attr("checked",!b.attr("checked")),a("input.variable_is_virtual").change();break;case"toggle_manage_stock":b=a('input[name^="variable_manage_stock"]'),b.attr("checked",!b.attr("checked")),a("input.variable_manage_stock").change();break;case"delete_all":if(c=window.confirm(woocommerce_admin_meta_boxes_variations.i18n_delete_all_variations),c&&(c=window.confirm(woocommerce_admin_meta_boxes_variations.i18n_last_warning))){var f=[];a(".woocommerce_variations .woocommerce_variation").block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),a(".woocommerce_variations .woocommerce_variation .remove_variation").each(function(){var b=a(this).attr("rel");b>0&&f.push(b)});var g={action:"woocommerce_remove_variations",variation_ids:f,security:woocommerce_admin_meta_boxes_variations.delete_variations_nonce};a.post(woocommerce_admin_meta_boxes_variations.ajax_url,g,function(){a(".woocommerce_variations .woocommerce_variation").fadeOut("300",function(){a(".woocommerce_variations .woocommerce_variation").remove()})})}break;case"variable_regular_price_increase":case"variable_regular_price_decrease":case"variable_sale_price_increase":case"variable_sale_price_decrease":var h;if(h=0===e.lastIndexOf("variable_regular_price",0)?"variable_regular_price":"variable_sale_price",d=window.prompt(woocommerce_admin_meta_boxes_variations.i18n_enter_a_value_fixed_or_percent),null==d)return;d=d.toString(),a(':input[name^="'+h+'"]').not('[name*="dates"]').each(function(){var b,c,f=accounting.unformat(a(this).val(),woocommerce_admin.mon_decimal_point);c=d.indexOf("%")>=0?f/100*accounting.unformat(d.replace(/\%/,""),woocommerce_admin.mon_decimal_point):accounting.unformat(d,woocommerce_admin.mon_decimal_point),b=-1!==e.indexOf("increase")?f+c:f-c,a(this).val(accounting.formatNumber(b,woocommerce_admin_meta_boxes.currency_format_num_decimals,woocommerce_admin_meta_boxes.currency_format_thousand_sep,woocommerce_admin_meta_boxes.currency_format_decimal_sep)).change()});break;case"variable_regular_price":case"variable_sale_price":case"variable_stock":case"variable_weight":case"variable_length":case"variable_width":case"variable_height":case"variable_download_limit":case"variable_download_expiry":d=window.prompt(woocommerce_admin_meta_boxes_variations.i18n_enter_a_value),null!=d&&a(':input[name^="'+e+'"]').not('[name*="dates"]').val(d).change();break;case"variable_sale_schedule":var i=window.prompt(woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_start),j=window.prompt(woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_end),k=!1;null!=i&&""!==i&&(a(".woocommerce_variable_attributes .sale_schedule").click(),a(':input[name^="variable_sale_price_dates_from"]').val(i).change(),k=!0),null!=j&&""!==j&&(a(".woocommerce_variable_attributes .sale_schedule").click(),a(':input[name^="variable_sale_price_dates_to"]').val(j).change(),k=!0),k||a(".woocommerce_variable_attributes .cancel_sale_schedule").click();break;default:a("select#field_to_edit").trigger(e)}}),a("#variable_product_options").on("change","input.variable_is_downloadable",function(){a(this).closest(".woocommerce_variation").find(".show_if_variation_downloadable").hide(),a(this).is(":checked")&&a(this).closest(".woocommerce_variation").find(".show_if_variation_downloadable").show()}),a("#variable_product_options").on("change","input.variable_is_virtual",function(){a(this).closest(".woocommerce_variation").find(".hide_if_variation_virtual").show(),a(this).is(":checked")&&a(this).closest(".woocommerce_variation").find(".hide_if_variation_virtual").hide()}),a("#variable_product_options").on("change","input.variable_manage_stock",function(){a(this).closest(".woocommerce_variation").find(".show_if_variation_manage_stock").hide(),a(this).is(":checked")&&a(this).closest(".woocommerce_variation").find(".show_if_variation_manage_stock").show()}),a("input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock").change();var b,c,d,e=wp.media.model.settings.post.id;a("#variable_product_options").on("click",".upload_image_button",function(f){var g=a(this),h=g.attr("rel"),i=g.closest(".upload_image");if(d=i,c=h,f.preventDefault(),g.is(".remove"))d.find(".upload_image_id").val(""),d.find("img").eq(0).attr("src",woocommerce_admin_meta_boxes_variations.woocommerce_placeholder_img_src),d.find(".upload_image_button").removeClass("remove");else{if(b)return b.uploader.uploader.param("post_id",c),void b.open();wp.media.model.settings.post.id=c,b=wp.media.frames.variable_image=wp.media({title:woocommerce_admin_meta_boxes_variations.i18n_choose_image,button:{text:woocommerce_admin_meta_boxes_variations.i18n_set_image},states:[new wp.media.controller.Library({title:woocommerce_admin_meta_boxes_variations.i18n_choose_image,filterable:"all"})]}),b.on("select",function(){var a=b.state().get("selection").first().toJSON(),c=a.sizes.thumbnail?a.sizes.thumbnail.url:a.url;d.find(".upload_image_id").val(a.id),d.find(".upload_image_button").addClass("remove"),d.find("img").eq(0).attr("src",c),wp.media.model.settings.post.id=e}),b.open()}}),a("a.add_media").on("click",function(){wp.media.model.settings.post.id=e});var f={init:function(){a("li.variations_tab a").on("click",this.initial_load),a("#variable_product_options").on("click","button.save-variation-changes",this.save_variations).on("click","button.add_variation",this.add_variation).on("click","button.remove_variation",this.remove_variation).on("click","button.link_all_variations",this.link_all_variations),a("body").on("change","#variable_product_options .woocommerce_variations :input",this.input_changed)},check_for_editions:function(){var b=a("#variable_product_options .woocommerce_variations .variation-needs-update");if(0<b.legnth){if(!window.confirm(woocommerce_admin_meta_boxes_variations.i18n_edited_variations))return!1;b.removeClass("variation-needs-update"),a("button.save-variation-changes").removeAttr("disabled")}return!0},block:function(){a("#woocommerce-product-data").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){a("#woocommerce-product-data").unblock()},initial_load:function(){0===a("#variable_product_options .woocommerce_variations .woocommerce_variation").length&&(g.set_paginav(0),g.set_page(1))},load_variations:function(b,c){b=b||1,c=c||woocommerce_admin_meta_boxes_variations.variations_per_page;var d=a("#variable_product_options .woocommerce_variations");f.block(),a.ajax({url:woocommerce_admin_meta_boxes_variations.ajax_url,data:{action:"woocommerce_load_variations",security:woocommerce_admin_meta_boxes_variations.load_variations_nonce,product_id:d.data("product_id"),attributes:d.data("attributes"),page:b,per_page:c},type:"POST",success:function(a){d.empty().append(a).attr("data-page",b),f.unblock()}})},save_variations:function(){var b=a(this),c=a("#variable_product_options .woocommerce_variations"),d=a(".variation-needs-update",c);return 0<d.length&&(f.block(),a.ajax({url:woocommerce_admin_meta_boxes_variations.ajax_url,data:{action:"woocommerce_save_variations",security:woocommerce_admin_meta_boxes_variations.save_variations_nonce,product_id:c.data("product_id"),data:a(":input",d).serialize()},type:"POST",success:function(){d.removeClass("variation-needs-update"),b.attr("disabled","disabled"),f.unblock()}})),!1},add_variation:function(){if(!f.check_for_editions())return!1;f.block();var b={action:"woocommerce_add_variation",post_id:woocommerce_admin_meta_boxes_variations.post_id,loop:a(".woocommerce_variation").size(),security:woocommerce_admin_meta_boxes_variations.add_variation_nonce};return a.post(woocommerce_admin_meta_boxes_variations.ajax_url,b,function(){g.set_paginav(1),g.set_page(1),f.unblock(),a("#variable_product_options").trigger("woocommerce_variations_added")}),!1},remove_variation:function(){if(!f.check_for_editions())return!1;if(window.confirm(woocommerce_admin_meta_boxes_variations.i18n_remove_variation)){var b=a(this).attr("rel"),c=[],d={action:"woocommerce_remove_variations"};f.block(),b>0?(c.push(b),d.variation_ids=c,d.security=woocommerce_admin_meta_boxes_variations.delete_variations_nonce,a.post(woocommerce_admin_meta_boxes_variations.ajax_url,d,function(){var b=parseInt(a("#variable_product_options .woocommerce_variations").attr("data-page"),10);g.set_paginav(-1),g.set_page(b)})):f.unblock()}return!1},link_all_variations:function(){if(!f.check_for_editions())return!1;if(window.confirm(woocommerce_admin_meta_boxes_variations.i18n_link_all_variations)){f.block();var b={action:"woocommerce_link_all_variations",post_id:woocommerce_admin_meta_boxes_variations.post_id,security:woocommerce_admin_meta_boxes_variations.link_variation_nonce};a.post(woocommerce_admin_meta_boxes_variations.ajax_url,b,function(b){var c=parseInt(b,10);window.alert(1===c?c+" "+woocommerce_admin_meta_boxes_variations.i18n_variation_added:0===c||c>1?c+" "+woocommerce_admin_meta_boxes_variations.i18n_variations_added:woocommerce_admin_meta_boxes_variations.i18n_no_variations_added),c>0?(g.set_paginav(c),g.set_page(1),a("#variable_product_options").trigger("woocommerce_variations_added")):f.unblock()})}return!1},input_changed:function(){a(this).closest(".woocommerce_variation").addClass("variation-needs-update"),a(".save-variation-changes").removeAttr("disabled")}},g={init:function(){a(".variations-pagenav").on("change",".page-selector",this.page_selector).on("click",".first-page",this.first_page).on("click",".prev-page",this.prev_page).on("click",".next-page",this.next_page).on("click",".last-page",this.last_page)},set_paginav:function(b){var c=a("#variable_product_options .woocommerce_variations"),d=parseInt(c.attr("data-total"),10),e=d+b,f=a(".variations-pagenav .displaying-num"),g=Math.ceil(e/woocommerce_admin_meta_boxes_variations.variations_per_page),h="";c.attr("data-total",e),f.text(1===e?woocommerce_admin_meta_boxes_variations.i18n_item.replace("%qty%",e):woocommerce_admin_meta_boxes_variations.i18n_items.replace("%qty%",e)),c.attr("data-total_pages",g),a(".variations-pagenav .total-pages").text(g);for(var i=1;g>=i;i++)h+='<option value="'+i+'">'+i+"</option>";a(".page-selector").empty().html(h)},check_is_enabled:function(b){return!a(b).hasClass("disabled")&&f.check_for_editions()},change_classes:function(b,c){var d=a(".variations-pagenav .first-page"),e=a(".variations-pagenav .prev-page"),f=a(".variations-pagenav .next-page"),g=a(".variations-pagenav .last-page");1===b?(d.addClass("disabled"),e.addClass("disabled")):(d.removeClass("disabled"),e.removeClass("disabled")),c===b?(f.addClass("disabled"),g.addClass("disabled")):(f.removeClass("disabled"),g.removeClass("disabled"))},set_page:function(b){a(".variations-pagenav .page-selector").val(b).change()},page_selector:function(){var b=parseInt(a(this).val(),10),c=a("#variable_product_options .woocommerce_variations");f.check_for_editions()?(g.change_classes(b,parseInt(c.attr("data-total_pages"),10)),f.load_variations(b)):a(this).val(parseInt(c.data("page"),10))},first_page:function(){return g.check_is_enabled(this)&&g.set_page(1),!1},prev_page:function(){if(g.check_is_enabled(this)){var b=a("#variable_product_options .woocommerce_variations"),c=parseInt(b.data("page"),10)-1,d=c>0?c:1;g.set_page(d)}return!1},next_page:function(){if(g.check_is_enabled(this)){var b=a("#variable_product_options .woocommerce_variations"),c=parseInt(b.data("total_pages"),10),d=parseInt(b.data("page"),10)+1,e=c>=d?d:c;g.set_page(e)}return!1},last_page:function(){if(g.check_is_enabled(this)){var b=a("#variable_product_options .woocommerce_variations").data("total_pages");g.set_page(b)}return!1}};f.init(),g.init()});