Skip to content

Commit

Permalink
Merge pull request #9465 from matks/FF-160_enable-edit-specific-price-2
Browse files Browse the repository at this point in the history
Enable 'edit specific price' button on BO Product page which opens a pop-in
  • Loading branch information
mickaelandrieu committed Aug 28, 2018
2 parents 9525f75 + 1fb2eed commit 96b0adc
Show file tree
Hide file tree
Showing 37 changed files with 1,041 additions and 320 deletions.
228 changes: 0 additions & 228 deletions admin-dev/themes/default/js/bundle/product/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ $(document).ready(function() {
formCategory.init();
stock.init();
supplier.init();
specificPrices.init();
warehouseCombinations.init();
customFieldCollection.init();
virtualProduct.init();
Expand Down Expand Up @@ -183,12 +182,10 @@ var displayFieldsManager = (function() {
if (showVariationsSelector.find('input:checked').val() === '1' || $('#accordion_combinations tr:not(#loading-attribute)').length > 0) {
combinationsBlock.show();

$('#specific-price-combination-selector').removeClass('hide').show();
$('#form-nav a[href="#step3"]').text(translate_javascripts['Combinations']);
$('#product_qty_0_shortcut_div, #quantities').hide();
} else {
combinationsBlock.hide();
$('#specific-price-combination-selector').hide();
$('#product_qty_0_shortcut_div, #quantities').show();
}

Expand Down Expand Up @@ -545,234 +542,9 @@ var nav = (function() {

formNav.find("a").on('shown.bs.tab', function(e) {
if (e.target.hash) {
onTabSwitch(e.target.hash);
window.location.hash = e.target.hash.replace('#', '#' + prefix);
}
});

/** on tab switch */
function onTabSwitch(currentTab) {
if (currentTab === '#step2') {
/** each switch to price tab, reload combinations into specific price form */
specificPrices.refreshCombinationsList();
}
}
}
};
})();

/**
* Specific prices management
*/
var specificPrices = (function() {
var id_product = $('#form_id_product').val();
var elem = $('#js-specific-price-list');
var leaveInitialPrice = $('#form_step2_specific_price_leave_bprice');
var productPriceField = $('#form_step2_specific_price_sp_price');
var discountTypeField = $('#form_step2_specific_price_sp_reduction_type');
var discountTaxField = $('#form_step2_specific_price_sp_reduction_tax');
var initSpecificPriceForm = new Object();

/** Get all specific prices */
function getInitSpecificPriceForm() {
$('#specific_price_form').find('select,input').each(function() {
initSpecificPriceForm[$(this).attr('id')] = $(this).val();
});
$('#specific_price_form').find('input:checkbox').each(function() {
initSpecificPriceForm[$(this).attr('id')] = $(this).prop('checked');
});
}

/** Get all specific prices */
function getAll() {
var url = elem.attr('data').replace(/list\/\d+/, 'list/' + id_product);

$.ajax({
type: 'GET',
url: url,
success: function(specific_prices) {
var tbody = elem.find('tbody');
tbody.find('tr').remove();

if (specific_prices.length > 0) {
elem.removeClass('hide');
} else {
elem.addClass('hide');
}

$.each(specific_prices, function(key, specific_price) {
var row = '<tr>' +
'<td>' + specific_price.rule_name + '</td>' +
'<td>' + specific_price.attributes_name + '</td>' +
'<td>' + specific_price.currency + '</td>' +
'<td>' + specific_price.country + '</td>' +
'<td>' + specific_price.group + '</td>' +
'<td>' + specific_price.customer + '</td>' +
'<td>' + specific_price.fixed_price + '</td>' +
'<td>' + specific_price.impact + '</td>' +
'<td>' + specific_price.period + '</td>' +
'<td>' + specific_price.from_quantity + '</td>' +
'<td>' + (specific_price.can_delete ? '<a href="' + $('#js-specific-price-list').attr('data-action-delete').replace(/delete\/\d+/, 'delete/' + specific_price.id_specific_price) + '" class="js-delete delete btn tooltip-link delete pl-0 pr-0"><i class="material-icons">delete</i></a>' : '') + '</td>' +
'</tr>';

tbody.append(row);
});
}
});
}

/**
* Add a specific price
* @param {object} elem - The clicked link
*/
function add(elem) {
$.ajax({
type: 'POST',
url: $('#specific_price_form').attr('data-action'),
data: $('#specific_price_form input, #specific_price_form select, #form_id_product').serialize(),
beforeSend: function() {
elem.attr('disabled', 'disabled');
},
success: function() {
showSuccessMessage(translate_javascripts['Form update success']);
$('#specific_price_form .js-cancel').click();
getAll();
},
complete: function() {
elem.removeAttr('disabled');
},
error: function(errors) {
showErrorMessage(errors.responseJSON);
}
});
}

/**
* Remove a specific price
* @param {object} elem - The clicked link
*/
function remove(elem) {
modalConfirmation.create(translate_javascripts['This will delete the specific price. Do you wish to proceed?'], null, {
onContinue: function() {
$.ajax({
type: 'GET',
url: elem.attr('href'),
beforeSend: function() {
elem.attr('disabled', 'disabled');
},
success: function(response) {
getAll();
resetForm();
showSuccessMessage(response);
},
error: function(response) {
showErrorMessage(response.responseJSON);
},
complete: function() {
elem.removeAttr('disabled');
}
});
}
}).show();
}

/** refresh combinations list selector for specific price form */
function refreshCombinationsList() {
var elem = $('#form_step2_specific_price_sp_id_product_attribute');
var url = elem.attr('data-action').replace(/product-combinations\/\d+/, 'product-combinations/' + id_product);

$.ajax({
type: 'GET',
url: url,
success: function(combinations) {
/** remove all options except first one */
elem.find('option:gt(0)').remove();

$.each(combinations, function(key, combination) {
elem.append('<option value="' + combination.id + '">' + combination.name + '</option>');
});
}
});
}

/**
* Because all "forms" are encapsulated in a global form, we just can't use reset button
* Reset all subform inputs values
*/
function resetForm() {
$('#specific_price_form').find('input').each(function() {
$(this).val(initSpecificPriceForm[$(this).attr('id')]);
});
$('#specific_price_form').find('select').each(function() {
$(this).val(initSpecificPriceForm[$(this).attr('id')]).change();
});
$('#specific_price_form').find('input:checkbox').each(function() {
$(this).prop("checked", true);
});
}

return {
'init': function() {
this.getAll();

$('#specific-price .add').click(function() {
$(this).hide();
});

$('#specific_price_form .js-cancel').click(function() {
resetForm();
$('#specific-price > a').click();
$('#specific-price .add').click().show();
productPriceField.prop('disabled', true);
});

$('#specific_price_form .js-save').click(function() {
add($(this));
});

$(document).on('click', '#js-specific-price-list .js-delete', function(e) {
e.preventDefault();
remove($(this));
});

$('#form_step2_specific_price_sp_reduction_type').change(function() {
if ($(this).val() === 'percentage') {
$('#form_step2_specific_price_sp_reduction_tax').hide();
} else {
$('#form_step2_specific_price_sp_reduction_tax').show();
}
});

this.refreshCombinationsList();

/* enable price field only when needed */
leaveInitialPrice.on('click', function togglePriceField() {
productPriceField.prop('disabled', $(this).is(':checked'))
.val('')
;
});

/* enable tax type field only when reduction by amount is selected */
discountTypeField.on('change', function toggleDiscountTaxField() {
var uglySelect2Selector = $('#select2-form_step2_specific_price_sp_reduction_tax-container').parent().parent();
if ($(this).val() === 'amount') {
uglySelect2Selector.show();
}else {
uglySelect2Selector.hide();
}
});

this.getInitSpecificPriceForm();

},
'getAll': function() {
getAll();
},
'refreshCombinationsList': function() {
refreshCombinationsList();
},
'getInitSpecificPriceForm' : function() {
getInitSpecificPriceForm();
}
};
})();
Expand Down
32 changes: 32 additions & 0 deletions admin-dev/themes/new-theme/js/pages/catalog/product/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

import SpecificPriceFormHandler from './specific-price-form-handler';

const $ = window.$;

$(() => {
new SpecificPriceFormHandler();
});
Loading

0 comments on commit 96b0adc

Please sign in to comment.