define([
'mage/adminhtml/grid'
], function () {
‘use strict’;
return function (config) {
var selectedProducts = config.selectedProducts,
categoryProducts = $H(selectedProducts),
gridJsObject = window[config.gridJsObjectName],
tabIndex = 1000;
/**
* Show selected product when edit form in associated product grid
*/
$('bliss_products').value = Object.toJSON(categoryProducts);
/**
* Register Category Product
*
* @param {Object} grid
* @param {Object} element
* @param {Boolean} checked
*/
function registerCategoryProduct(grid, element, checked) {
if (checked) {
if (element.positionElement) {
element.positionElement.disabled = false;
categoryProducts.set(element.value, element.positionElement.value);
}
} else {
if (element.positionElement) {
element.positionElement.disabled = true;
}
categoryProducts.unset(element.value);
}
$('bliss_products').value = Object.toJSON(categoryProducts);
grid.reloadParams = {
'selected_products[]': categoryProducts.keys()
};
}
/**
* Click on product row
*
* @param {Object} grid
* @param {String} event
*/
function categoryProductRowClick(grid, event) {
var trElement = Event.findElement(event, 'tr'),
isInput = Event.element(event).tagName === 'INPUT',
checked = false,
checkbox = null;
if (trElement) {
checkbox = Element.getElementsBySelector(trElement, 'input');
if (checkbox[0]) {
checked = isInput ? checkbox[0].checked : !checkbox[0].checked;
gridJsObject.setCheckboxChecked(checkbox[0], checked);
}
}
}
/**
* Change product position
*
* @param {String} event
*/
function customerpricechange(event) {
var element = Event.element(event);
if (element && element.checkboxElement && element.checkboxElement.checked) {
categoryProducts.set(element.checkboxElement.value, element.value);
$('bliss_products').value = Object.toJSON(categoryProducts);
}
}
function customerspecilpricechange(event) {
var element = Event.element(event);
if (element && element.checkboxElement && element.checkboxElement.checked) {
categoryProducts.set(element.checkboxElement.value, element.value);
$('bliss_products').value = Object.toJSON(categoryProducts);
}
}
/**
* Initialize category product row
*
* @param {Object} grid
* @param {String} row
*/
function categoryProductRowInit(grid, row) {
var checkbox = $(row).getElementsByClassName('checkbox')[0],
customerprice = $(row).getElementsByClassName('input-text')[0];
customerspecilprice = $(row).getElementsByClassName('input-text')[1];
if (checkbox && customerprice) {
checkbox.positionElement = customerprice;
customerprice.checkboxElement = checkbox;
customerprice.disabled = !checkbox.checked;
customerprice.tabIndex = tabIndex++;
Event.observe(customerprice, 'keyup', customerpricechange);
}
if (checkbox && customerspecilprice) {
checkbox.positionElement = customerspecilprice;
customerspecilprice.checkboxElement = checkbox;
customerspecilprice.disabled = !checkbox.checked;
customerspecilprice.tabIndex = tabIndex++;
Event.observe(customerspecilprice, 'keyup', customerspecilpricechange);
}
}
gridJsObject.rowClickCallback = categoryProductRowClick;
gridJsObject.initRowCallback = categoryProductRowInit;
gridJsObject.checkboxCheckCallback = registerCategoryProduct;
if (gridJsObject.rows) {
gridJsObject.rows.each(function (row) {
categoryProductRowInit(gridJsObject, row);
});
}
};
});