Created new js classes for variations media and actions

This commit is contained in:
Claudio Sanches 2015-07-06 20:05:09 -03:00
parent de01133142
commit b819326b2a
2 changed files with 158 additions and 86 deletions

View File

@ -1,6 +1,6 @@
/* global wp, woocommerce_admin_meta_boxes_variations, woocommerce_admin, woocommerce_admin_meta_boxes, accounting */ /* global wp, woocommerce_admin_meta_boxes_variations, woocommerce_admin, woocommerce_admin_meta_boxes, accounting */
/*jshint devel: true */ /*jshint devel: true */
jQuery( function ( $ ) { jQuery( function( $ ) {
$( '.wc-metaboxes-wrapper' ).on( 'click', 'a.bulk_edit', function () { $( '.wc-metaboxes-wrapper' ).on( 'click', 'a.bulk_edit', function () {
var bulk_edit = $( 'select#field_to_edit' ).val(), var bulk_edit = $( 'select#field_to_edit' ).val(),
@ -154,102 +154,172 @@ jQuery( function ( $ ) {
} }
}); });
$( '#variable_product_options' ).on( 'change', 'input.variable_is_downloadable', function () { /**
* Variations actions
*/
var wc_meta_boxes_product_variations_actions = {
/**
* Initialize variations actions
*/
init: function() {
$( '#variable_product_options' )
.on( 'change', 'input.variable_is_downloadable', this.variable_is_downloadable )
.on( 'change', 'input.variable_is_virtual', this.variable_is_virtual )
.on( 'change', 'input.variable_manage_stock', this.variable_manage_stock );
$( 'input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock' ).change();
$( 'a.add_media' ).on( 'click', this.restore_media_post_id );
},
/**
* Check if variation is downloadable and show/hide elements
*/
variable_is_downloadable: function() {
$( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_downloadable' ).hide(); $( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_downloadable' ).hide();
if ( $( this ).is( ':checked' ) ) { if ( $( this ).is( ':checked' ) ) {
$( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_downloadable' ).show(); $( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_downloadable' ).show();
} }
}); },
$( '#variable_product_options' ).on( 'change', 'input.variable_is_virtual', function () { /**
* Check if variation is virtual and show/hide elements
*/
variable_is_virtual: function() {
$( this ).closest( '.woocommerce_variation' ).find( '.hide_if_variation_virtual' ).show(); $( this ).closest( '.woocommerce_variation' ).find( '.hide_if_variation_virtual' ).show();
if ( $( this ).is( ':checked' ) ) { if ( $( this ).is( ':checked' ) ) {
$( this ).closest( '.woocommerce_variation' ).find( '.hide_if_variation_virtual' ).hide(); $( this ).closest( '.woocommerce_variation' ).find( '.hide_if_variation_virtual' ).hide();
} }
}); },
$( '#variable_product_options' ).on( 'change', 'input.variable_manage_stock', function () { /**
* Check if variation manage stock and show/hide elements
*/
variable_manage_stock: function() {
$( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_manage_stock' ).hide(); $( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_manage_stock' ).hide();
if ( $( this ).is( ':checked' ) ) { if ( $( this ).is( ':checked' ) ) {
$( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_manage_stock' ).show(); $( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_manage_stock' ).show();
} }
}); }
};
$( 'input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock' ).change(); /**
* Variations media actions
*/
var wc_meta_boxes_product_variations_media = {
// Uploader /**
var variable_image_frame; * wp.media frame object
var setting_variation_image_id; *
var setting_variation_image; * @type {object}
var wp_media_post_id = wp.media.model.settings.post.id; */
variable_image_frame: null,
$( '#variable_product_options' ).on( 'click', '.upload_image_button', function ( event ) { /**
* Variation image ID
*
* @type {int}
*/
setting_variation_image_id: null,
var $button = $( this ); /**
var post_id = $button.attr( 'rel' ); * Variation image object
var $parent = $button.closest( '.upload_image' ); *
setting_variation_image = $parent; * @type {object}
setting_variation_image_id = post_id; */
setting_variation_image: null,
/**
* wp.media post ID
*
* @type {int}
*/
wp_media_post_id: wp.media.model.settings.post.id,
/**
* Initialize media actions
*/
init: function() {
$( '#variable_product_options' ).on( 'click', '.upload_image_button', this.add_image );
$( 'a.add_media' ).on( 'click', this.restore_wp_media_post_id );
},
/**
* Added new image
*
* @param {object} event
*/
add_image: function( event ) {
var $button = $( this ),
post_id = $button.attr( 'rel' ),
$parent = $button.closest( '.upload_image' );
wc_meta_boxes_product_variations_media.setting_variation_image = $parent;
wc_meta_boxes_product_variations_media.setting_variation_image_id = post_id;
event.preventDefault(); event.preventDefault();
if ( $button.is( '.remove' ) ) { if ( $button.is( '.remove' ) ) {
setting_variation_image.find( '.upload_image_id' ).val( '' ); $( '.upload_image_id', wc_meta_boxes_product_variations_media.setting_variation_image ).val( '' ).change();
setting_variation_image.find( 'img' ).eq( 0 ).attr( 'src', woocommerce_admin_meta_boxes_variations.woocommerce_placeholder_img_src ); wc_meta_boxes_product_variations_media.setting_variation_image.find( 'img' ).eq( 0 ).attr( 'src', woocommerce_admin_meta_boxes_variations.woocommerce_placeholder_img_src );
setting_variation_image.find( '.upload_image_button' ).removeClass( 'remove' ); wc_meta_boxes_product_variations_media.setting_variation_image.find( '.upload_image_button' ).removeClass( 'remove' );
} else { } else {
// If the media frame already exists, reopen it. // If the media frame already exists, reopen it.
if ( variable_image_frame ) { if ( wc_meta_boxes_product_variations_media.variable_image_frame ) {
variable_image_frame.uploader.uploader.param( 'post_id', setting_variation_image_id ); wc_meta_boxes_product_variations_media.variable_image_frame.uploader.uploader.param( 'post_id', wc_meta_boxes_product_variations_media.setting_variation_image_id );
variable_image_frame.open(); wc_meta_boxes_product_variations_media.variable_image_frame.open();
return; return;
} else { } else {
wp.media.model.settings.post.id = setting_variation_image_id; wp.media.model.settings.post.id = wc_meta_boxes_product_variations_media.setting_variation_image_id;
} }
// Create the media frame. // Create the media frame.
variable_image_frame = wp.media.frames.variable_image = wp.media({ wc_meta_boxes_product_variations_media.variable_image_frame = wp.media.frames.variable_image = wp.media({
// Set the title of the modal. // Set the title of the modal.
title: woocommerce_admin_meta_boxes_variations.i18n_choose_image, title: woocommerce_admin_meta_boxes_variations.i18n_choose_image,
button: { button: {
text: woocommerce_admin_meta_boxes_variations.i18n_set_image text: woocommerce_admin_meta_boxes_variations.i18n_set_image
}, },
states : [ states: [
new wp.media.controller.Library({ new wp.media.controller.Library({
title: woocommerce_admin_meta_boxes_variations.i18n_choose_image, title: woocommerce_admin_meta_boxes_variations.i18n_choose_image,
filterable : 'all' filterable: 'all'
}) })
] ]
}); });
// When an image is selected, run a callback. // When an image is selected, run a callback.
variable_image_frame.on( 'select', function () { wc_meta_boxes_product_variations_media.variable_image_frame.on( 'select', function () {
var attachment = variable_image_frame.state().get( 'selection' ).first().toJSON(), var attachment = wc_meta_boxes_product_variations_media.variable_image_frame.state().get( 'selection' ).first().toJSON(),
url = attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url; url = attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url;
setting_variation_image.find( '.upload_image_id' ).val( attachment.id ); $( '.upload_image_id', wc_meta_boxes_product_variations_media.setting_variation_image ).val( attachment.id ).change();
setting_variation_image.find( '.upload_image_button' ).addClass( 'remove' ); wc_meta_boxes_product_variations_media.setting_variation_image.find( '.upload_image_button' ).addClass( 'remove' );
setting_variation_image.find( 'img' ).eq( 0 ).attr( 'src', url ); wc_meta_boxes_product_variations_media.setting_variation_image.find( 'img' ).eq( 0 ).attr( 'src', url );
wp.media.model.settings.post.id = wp_media_post_id; wp.media.model.settings.post.id = wc_meta_boxes_product_variations_media.wp_media_post_id;
}); });
// Finally, open the modal. // Finally, open the modal.
variable_image_frame.open(); wc_meta_boxes_product_variations_media.variable_image_frame.open();
} }
}); },
// Restore ID /**
$( 'a.add_media' ).on( 'click', function() { * Restore wp.media post ID.
wp.media.model.settings.post.id = wp_media_post_id; */
}); restore_wp_media_post_id: function() {
wp.media.model.settings.post.id = wc_meta_boxes_product_variations_media.wp_media_post_id;
}
};
/** /**
* Product variations metabox ajax methods * Product variations metabox ajax methods
@ -688,6 +758,8 @@ jQuery( function ( $ ) {
} }
}; };
wc_meta_boxes_product_variations_actions.init();
wc_meta_boxes_product_variations_media.init();
wc_meta_boxes_product_variations_ajax.init(); wc_meta_boxes_product_variations_ajax.init();
wc_meta_boxes_product_variations_pagenav.init(); wc_meta_boxes_product_variations_pagenav.init();

File diff suppressed because one or more lines are too long