Product = {};
Product.tab = 'show';
Product.items = {};
Product.settings = {
multi: false,
editID: 0,
renderNode: null,
selectedID: []
}
Product.params = [];
Product.values = [];
// Product.DEFAULT = {
// name: 'null',
// description: 'null',
// buy_amount: 'null',
// sell_amount: 'null',
// sale_amount: 'null',
// article: 'null',
// currency: 'RUB',
// type: 'real',
// images: [],
// params: {
// property: [],
// values: [],
// }
// }
Product.DEFAULT_PROPERTIES = {
image: 'null',
// Prices
price: {
buy: 'null',
sell: 'null',
sale: 'null',
},
// CRM
article: 'null',
// Delivery
weight: 'null',
dimensions: {
height: 'null',
width: 'null',
length: 'null'
},
}
Product.DEFAULT = {
version: '2',
// Text
name: 'null',
description: 'null',
full_description: 'null',
type: 'real', // or info
currency: 'RUB',
unit: 'null',
images: [
// "/upload/7833/10842/2facd3e1af.png",
// "/upload/7833/10842/d22fbf0bcf.png",
// "/upload/7833/10842/eb5e179198.png",
// "/upload/7833/10842/ac23b2915c.png",
// "/upload/7833/10842/691cf810fa.png",
// "/upload/7833/10842/1ab840465f.png"
],
// + Product.DEFAULT_PROPERTIES
params: [
// {
// name: 'null',
// control: 'select',
// type: 'price', // price, priceless, additional
// values: [{
// name: 'null',
// value: 'null'
// }]
// }
],
values: [
// {
// id: '',
// // + Product.DEFAULT_PROPERTIES
// }
// id 0_0 - {param_index}_{value_index}#{param_index}_{value_index}
]
}
Product.state = {};
Product.tplObject = {};
$.ajax({
url: '/scr/getWitgetTemplate',
data: { witget: 'product', },
type: 'post',
dataType: 'json',
async: false,
success: function (data) {
Product.tplObject = data;
}
});
Product.create = function() {
var info = {};
info = $.extend(false, {}, Product.DEFAULT);
info.properties = $.extend(true, {}, Product.DEFAULT_PROPERTIES);
return info;
}
Product.get = function(id) {
if(typeof Product.items[id] == 'undefined') {
info = {};
} else {
info = Product.items[id];
}
DEFAULT_PROPERTIES = $.extend(true, {}, Product.DEFAULT_PROPERTIES);
DEFAULT = $.extend(true, {}, Product.DEFAULT);
if(typeof info.properties == 'undefined') info.properties = {};
info.properties = $.extend(true, DEFAULT_PROPERTIES, info.properties);
info = $.extend(false, DEFAULT, info);
if(typeof info.values == 'undefined' || info.values == null) {
info.values = [];
}
return info;
}
Product.getParamInfo = function(id) {
var info = this.get(id);
amount_info = info.properties;
if(typeof info.values == 'undefined' || info.values == null) {
info.values = [];
}
// console.log(id, typeof info.values, info);
image = 'null';
if(info.images.length > 0) {
image = info.images[0];
}
if(info.values.length > 0) {
info = info.values[info.values.length - 1];
amount_info = info;
image = info.image;
}
price = this.getPrice(amount_info);
return $.extend(true, info, {
amount: price.amount,
sell_amount: price.sell_amount,
image: image,
article: amount_info.article,
});
}
Product.getPrice = function(info) {
amount = 0;
sell_amount = 0;
if(typeof info.price == 'undefined') {
info.price = {
buy: 'null',
sell: 'null',
sale: 'null',
};
}
if(info.price.buy != 'null' && info.price.buy != '') {
amount = info.price.buy;
}
if(info.price.sell != 'null' && info.price.sell != '') {
amount = info.price.sell;
}
if(info.price.sale != 'null' && info.price.sale != '') {
sell_amount = amount;
amount = info.price.sale;
}
amount = parseInt(amount) || 0;
sell_amount = parseInt(sell_amount) || 0;
if(amount == sell_amount) {
sell_amount = 0;
}
return {
amount: amount,
sell_amount: sell_amount
};
}
Product.getCurrency = function(currencyName) {
if(currencyName == 'RUB') {
return 'руб.';
} else if(currencyName == 'UAH') {
return 'грн.';
} else if(currencyName == 'EUR') {
return '€';
} else if(currencyName == 'USD') {
return '$';
} else if(currencyName == 'KZT') {
return 'тг.';
} else if(currencyName == 'KGS') {
return 'сом';
}
return 'руб.';
}
Product.getCards = function(choosenProducts) {
if(typeof choosenProducts == 'undefined') {
choosenProducts = [];
}
var html = '';
// button
html += '
\
\
\
Добавить продукт\
\
';
keys = Object.keys(Product.items);
keys.sort();
keys = keys.reverse();
// console.log(keys);
$.each(keys, function(id, v) {
id = v;
v = Product.items[v];
v.id = parseInt(id);
var paramInfo = Product.getParamInfo(id);
// console.log(id, paramInfo, v);
// properties
// if(v.params == null) {
// v.params = [];
// }
// if(v.values == null) {
// v.values = [];
// }
if(Product.settings.selectedID.indexOf(v.id) > -1) {
selectedClass = 'choosen';
buttonText = 'УБРАТЬ';
} else {
selectedClass = '';
buttonText = 'ВЫБРАТЬ';
}
html += '';
html += '
';
// Name and price
html += '
\
\
' + paramInfo.amount + ' ' + Product.getCurrency(v.currency) + '\
';
// Images
html += '
';
if(v.images.length == 0) {
html += '
ИЗОБРАЖЕНИЙ НЕТ
';
}
$.each(v.images, function (i, v) {
if(i == 4) return false;
html += '
';
});
html += '
';
// Params
html += '
';
if(v.params.length == 0) {
html += '';
}
$.each(v.params, function (i, v) {
html += '
'+v.name+'
';
});
html += '
';
// Buttons
html += '
\
';
});
return html;
}
Product.getCard = function(id) {
var html = '',
v = Product.get(id),
paramInfo = Product.getParamInfo(id);
html += '';
html += '
';
// Name and price
html += '
\
\
' + paramInfo.amount + ' ' + Product.getCurrency(v.currency) + '\
';
// Buttons
html += '
';
return html;
}
Product.setMulti = function(multi) {
// alert(typeof multi + ' ' + multi)
if(typeof multi == 'string') {
Product.settings.choosen = [multi];
Product.settings.multi = false;
} else {
Product.settings.choosen = multi;
Product.settings.multi = true;
}
$.each(Product.settings.choosen, function(i, v) {
Product.settings.choosen[i] = parseInt(v);
});
// console.log('-------multi-------', Product.settings.multi, multi.length);
}
Product.tpl = function(id) {
return $(Product.tplObject[id]);
}
Product.change = function(obj, isRender) {
isRender = (typeof isRender == 'undefined') ? true : isRender;
Product.state = $.extend(true, Product.state, obj);
if(isRender) {
Product.autoComplite();
Product.renderTables();
}
}
Product.autoComplite = function() {
var _this = this,
t = function(id) { return _this.tpl(id); },
v = _this.state;
// console.warn(v);
// alert(this.state.name);
$('#product-data-name').val((v.name == 'null') ? '' : v.name);
$('#product-data-description').val((v.description == 'null') ? '' : v.description);
$('#product-data-full_description').val((v.full_description == 'null') ? '' : v.full_description);
$('#product-data-buy_amount').val((v.properties.price.buy == 'null') ? '' : v.properties.price.buy);
$('#product-data-sell_amount').val((v.properties.price.sell == 'null') ? '' : v.properties.price.sell);
$('#product-data-sale_amount').val((v.properties.price.sale == 'null') ? '' : v.properties.price.sale);
$('#product-data-article').val((v.properties.article == 'null') ? '' : v.properties.article);
$('#product-data-unit').val((v.unit == 'null') ? '' : v.unit);
$('#product-data-dimensions-height').val((v.properties.dimensions.height == 'null') ? '' : v.properties.dimensions.height);
$('#product-data-dimensions-width').val((v.properties.dimensions.width == 'null') ? '' : v.properties.dimensions.width);
$('#product-data-dimensions-length').val((v.properties.dimensions.length == 'null') ? '' : v.properties.dimensions.length);
$('#product-data-weight').val((v.properties.weight == 'null') ? '' : v.properties.weight);
currency = $('#product-data-currency').find('li[data-type="'+v.currency+'"]').text();
$('#product-data-currency').find('.button span').html(currency);
type = $('#product-data-type').find('li[data-type="'+v.type+'"]').text();
$('#product-data-type').find('.button span').html(type);
images = $('#product-data-images');
images.html('');
$.each(v.images, function(i, v) {
img = t('params.image');
img.attr('data-index', i);
img.find('img').attr('src', v);
images.append(img);
});
if(v.images.length == 0) {
$('#product-images-controls').fadeOut(200);
$('#product-data-images-null').show();
$('#product-upload-image').removeClass('small');
$('#product-upload-image').parent().css('position', 'relative');
} else {
$('#product-data-images-null').hide();
$('#product-upload-image').addClass('small');
$('#product-upload-image').css('height', 'auto');
$('#product-upload-image').parent().css('position', 'inherit');
if(!$('#product-images-controls').is(':visible')) {
$('#product-images-controls').fadeIn(200);
}
}
if(v.values.length == 0) {
$('#product-params-button span').html('Добавить параметры продукта');
} else {
$('#product-params-button span').html('Добавить или изменить параметры продукта');
}
var length = 0;
$.each(this.state.params, function(i, v) {
if(v.type == 'price') {
length++;
}
});
if(length == 0) {
$('#product-params-empty').show();
$('#product-data-type-table').hide();
} else {
$('#product-params-empty').hide();
$('#product-data-type-table').show();
}
if(Product.settings.editID == null) {
$('#product-actions-save').hide();
$('#product-actions-add').show();
} else {
$('#product-actions-save').show();
$('#product-actions-add').hide();
}
}
Product.start = function(renderNode, selectedProductsID) {
if(typeof renderNode == 'undefined') {
renderNode = null;
}
if(typeof selectedProductsID == 'undefined') {
selectedProductsID = [];
}
if(typeof selectedProductsID == 'object') {
Product.settings.multi = true;
selectedID = selectedProductsID;
} else {
Product.settings.multi = false;
selectedID = [selectedProductsID];
}
Product.settings.renderNode = renderNode;
Product.settings.selectedID = selectedID;
}
Product.addProduct = function() {
Product.settings.editID = null;
this.state = Product.create();
// Product.state = $.extend(true, Product.state, Product.get(null));
// console.log($.extend(true, {}, this.state));
Modal.show({
'class': "paged full",
src: 'products',
multi: false,
siteID: Settings.parentID,
ready: function() {
Product.autoComplite();
Product.renderTables();
}
});
}
Product.cloneProduct = function(id) {
data = $.extend(true, {}, Product.get(id));
data.siteID = Settings.siteID;
if(typeof data.params == 'undefined') {
data.params = [];
}
if(typeof data.values == 'undefined') {
data.values = [];
}
$.ajax({
url: '/scr/cloneProduct',
type: 'post',
data: {
json: JSON.stringify(data),
siteID: Settings.siteID
},
dataType: 'json',
success: function (obj) {
// console.warn(obj);
data.id = obj.items.id;
Product.items[obj.items.id] = data;
if(Product.settings.renderNode != null) {
html = $(''+Product.getCards()+'
');
block = html.find('[data-id="'+obj.items.id+'"]');
block.css({
opacity: 0
});
Product.settings.renderNode.html(html.html());
block = Product.settings.renderNode.find('[data-id="'+obj.items.id+'"]');
block.velocity({
opacity: 1
}, {
duration: 200,
queue: false,
easing: "ease-in-out",
});
}
}
});
}
Product.removeProduct = function(id) {
block = Product.settings.renderNode.children('[data-id="'+id+'"]');
delete Product.items[id];
block.css({
'vertical-align': 'top',
'overflow': 'hidden'
}).velocity({
opacity: 0,
height: 0,
width: 0,
margin: 0
}, {
duration: 200,
queue: false,
easing: "ease-in-out",
complete: function() {
var self = this;
setTimeout(function() {
$(self).remove();
Witgets.fn('removeProducts', [id]);
}, 200);
$.ajax({
url: '/scr/removeProduct',
type: 'post',
data: {
id: id,
siteID: Settings.parentID
},
dataType: 'json',
success: function (data) {
}
});
}
});
}
Product.edit = function(id) {
Product.settings.editID = id;
Product.state = {};
Product.state = $.extend(true, Product.state, Product.get(id));
Modal.show({
'class': "paged full",
src: 'products',
multi: false,
siteID: Settings.parentID,
ready: function() {
Product.autoComplite();
Product.renderTables();
}
});
// $.ajax({
// url: '/scr/editProduct',
// type: 'post',
// data: data,
// dataType: 'json',
// success: function (data) {
// // alert(data)
// // return false;
// $('#products').html(data.products);
// Product.items = data.items;
// Product.changeTab('show');
// Witgets[Witgets.current.type].build();
// // alert(data)
// }
// });
}
Product.cancel = function() {
Modal.hide();
}
Product.save = function() {
data = $.extend(true, {}, Product.state);
data.siteID = Settings.siteID;
if(typeof data.params == 'undefined') {
data.params = [];
}
if(typeof data.values == 'undefined') {
data.values = [];
}
// console.log(data, Product.state);
Product.items[Product.settings.editID] = data;
$.ajax({
url: '/scr/editProduct',
type: 'post',
data: {
json: JSON.stringify(data),
siteID: Settings.siteID
},
dataType: 'json',
success: function (data) {
Witgets.change({});
// alert(data);
// $('#products').html(data.products);
// Product.items = data.items;
// Product.changeTab('show');
// Witgets[Witgets.current.type].build();
// // alert(data)
}
});
if(Product.settings.renderNode != null) {
Product.settings.renderNode.html(Product.getCards());
Modal.hide();
}
}
Product.add = function() {
data = $.extend(true, {}, Product.state);
data.siteID = Settings.siteID;
if(typeof data.params == 'undefined') {
data.params = [];
}
if(typeof data.values == 'undefined') {
data.values = [];
}
// Product.items[id] = data;
$.ajax({
url: '/scr/addProduct',
type: 'post',
data: {
json: JSON.stringify(data),
siteID: Settings.siteID
},
dataType: 'json',
success: function (obj) {
// console.warn(obj);
Product.items[obj.items.id] = data;
if(Product.settings.renderNode != null) {
Product.settings.renderNode.html(Product.getCards());
Modal.hide();
}
}
});
}
Product.addImage = function(files) {
var _this = this;
$.each(files, function(i, params) {
_this.state.images.push(params.path);
});
Product.change({
images: this.state.images
});
}
Product.deleteImage = function(i) {
// clear params
// $.each(this.state.params.values, function(j, v) {
// if(v.image == Product.state.images[i]) {
// v.image = 'null';
// }
// });
this.state.images.splice(i, 1);
Product.change({
images: this.state.images
});
}
/**
* PARAMS
*/
Product.deleteImageFromValue = function(e) {
index = $(e).closest('tr').data('index');
this.state.values[index].image = 'null';
Product.change({
values: this.state.values
});
}
Product.showSelectImage = function(e) {
var index = $(e).closest('tr').data('index'),
_this = this;
Modal.show({
src: 'productImages',
class: 'big',
ready: function() {
block = $('#product-images');
$.each(_this.state.images, function(i, v) {
img = t('params.image.choose');
img.find('img').attr('src', v);
block.append(img);
img.click(function() {
i = $(this).index();
_this.state.values[index].image = _this.state.images[i];
Product.change({});
Modal.hide();
});
});
if(_this.state.images.length == 0) {
block.html('');
}
}
});
}
Product.showSelectBatchImage = function(e) {
var index = $(e).closest('tr').data('index'),
_this = this;
Modal.show({
src: 'productImages',
class: 'big',
ready: function() {
block = $('#product-images');
$.each(_this.state.images, function(i, v) {
img = t('params.image.choose');
img.find('img').attr('src', v);
block.append(img);
img.click(function() {
i = $(this).index();
src = _this.state.images[i];
$('#product-image-thumb').show().find('img').attr('src', src);
$('#product-value-image').val(src);
Modal.hide();
});
});
if(_this.state.images.length == 0) {
block.html('');
}
}
});
}
Product.batchParamsProcessing = function() {
var _this = this;
Modal.show({
src: 'batchParamsProcessing',
class: 'big',
ready: function() {
params = $('#product-params-batch');
params.html('');
$.each(_this.state.params, function(i, v) {
input = $('')
select = input.find('select');
$.each(v.values, function(j, k) {
select.append('');
});
params.append(input);
});
// Product.params = JSON.parse(JSON.stringify(_this.state.params));
// Product.values = JSON.parse(JSON.stringify(_this.state.values));
// Product.renderParams();
}
});
}
Product.batchParams = function() {
var _this = this;
var buy = $('#product-value-buy').val(),
sell = $('#product-value-sell').val(),
sale = $('#product-value-sale').val(),
width = $('#product-value-width').val(),
height = $('#product-value-height').val(),
length = $('#product-value-length').val(),
article = $('#product-value-article').val(),
weight = $('#product-value-weight').val(),
image = $('#product-value-image').val(),
values = [];
$('#product-params-batch .input select').each(function() {
values[$(this).data('index')] = $(this).val();
});
// console.log(buy, sell, sale, width, height, length, article, weight, values);
$.each(this.state.values, function(i, v) {
params = v.id.split('#');
isGood = true;
// console.log(v.id);
$.each(params, function(j, k) {
indexes = k.split('_');
pi = indexes[0];
vi = indexes[1];
if(values[pi] != 'null') {
// console.log(k, pi, values[pi], vi);
if(values[pi] != vi) {
isGood = false;
}
}
});
if(isGood) {
// console.log(v.id, 'YEEEEE')
_this.state.values[i].article = article;
_this.state.values[i].weight = weight;
_this.state.values[i].dimensions.width = width;
_this.state.values[i].dimensions.height = height;
_this.state.values[i].dimensions.length = length;
_this.state.values[i].price.buy = buy;
_this.state.values[i].price.sell = sell;
_this.state.values[i].price.sale = sale;
_this.state.values[i].image = image;
}
// console.log(isGood, '-----');
});
// console.log(_this.state.values);
Product.change({
values: _this.state.values
});
Modal.hide();
}
Product.showParamsSettings = function() {
var _this = this;
Modal.show({
src: 'productParams',
class: 'big',
ready: function() {
Product.params = JSON.parse(JSON.stringify(_this.state.params));
Product.values = JSON.parse(JSON.stringify(_this.state.values));
Product.renderParams();
}
});
}
Product.addParam = function(e) {
type = $(e).data('type');
Product.params.push({
name: 'null',
control: 'select',
type: type, // price, priceless, additional
values: []
});
Product.renderParams();
}
Product.changeParamControl = function(e) {
Product.changeParamSettings(e, {
control: $(e).data('type')
});
Product.renderParams();
}
Product.removeParam = function(e) {
var row = $(e).closest('.row'),
index = row.data('index');
Product.params.splice(index, 1);
Product.renderParams();
}
Product.changeParamSettings = function(e, obj) {
var row = $(e).closest('.row'),
index = row.data('index');
$.each(obj, function(i, v) {
Product.params[index][i] = v;
});
}
Product.changeTableValue = function(e, obj) {
block = $(e).closest('tr');
id = block.data('id');
$.each(this.state.values, function(i, v) {
if(v.id == id) {
v = $.extend(true, v, obj);
}
});
}
Product.renderParams = function() {
result = '';
$.each(Product.params, function(i, v) {
name = (v.name == 'null') ? '' : v.name;
result += '';
if(v.type == 'price') {
result += '';
} else if(v.type == 'priceless') {
result += '';
} else if(v.type == 'additional') {
result += '';
}
if(v.type == 'additional') {
result += '
';
} else {
result += '
';
}
result += '
\
\
\
';
if(v.type != 'additional') {
result += '
';
}
result += '
';
if(v.type != 'additional') {
if(v.control == 'color') {
callback = 'colorParams';
} else {
callback = 'valuesParams';
}
result += '
';
}
result += '
';
result += '
';
result += '
';
if(i != Product.params.length - 1) {
result += '
';
}
});
if(Product.params.length > 0) {
$('#product-params-property').html(result);
} else {
$('#product-params-property').html('');
}
ui.mountAll();
ui.start();
}
Product.renderTables = function() {
var _this = this,
t = function(id) { return Witgets['product'].tpl(id); };
block = $('#product-data-type-table');
block.children('thead').find('.property').remove();
property = $.extend(true, [], this.state.params.property).reverse();
$.each(this.state.params, function(i, v) {
if(v.type !== 'additional') {
first = block.find('thead > tr > td.first_standart_prop');
$('
'+v.name+' | ').insertBefore(first);
}
});
// this.state.params.reverse();
block.find('tbody').html('');
$.each(this.state.values, function(i, v) {
var value = 'null',
tr = $('
|
');
tr.attr({
'data-id': v.id,
'data-index': i,
});
params = v.id.split('#');
$.each(params, function(j, v) {
i = v.split('_');
param = _this.state.params[i[0]];
v = param.values[i[1]];
// console.log(i[0], i[1], _this.state.params);
if(param.type == 'priceless') {
v = {
name: param.values.map(function(v) { return v.name; }).join(', '),
value: i[0]
};
}
td = $('
'+v.name+' | ');
if(param.control == 'color') {
td.prepend('
');
}
tr.append(td);
});
priceBuy = t('params.tableRow');
priceBuy.find(':input').attr({
name: 'price-buy',
value: (v.price.buy == 'null') ? '' : v.price.buy,
onkeyup: 'Product.changeTableValue(this, { price: { buy: this.value }})'
});
tr.append(priceBuy);
priceSell = t('params.tableRow');
priceSell.find(':input').attr({
name: 'price-sell',
value: (v.price.sell == 'null') ? '' : v.price.sell,
onkeyup: 'Product.changeTableValue(this, { price: { sell: this.value }})'
});
tr.append(priceSell);
priceSale = t('params.tableRow');
priceSale.find(':input').attr({
name: 'price-sale',
value: (v.price.sale == 'null') ? '' : v.price.sale,
onkeyup: 'Product.changeTableValue(this, { price: { sale: this.value }})'
});
tr.append(priceSale);
additional = t('params.tableAdditional');
// console.warn(v.image);
if(v.image == 'null') {
additional.children('.image-button').children('.button').show();
additional.children('.image-button').children('.image').hide();
} else {
additional.children('.image-button').children('.button').hide();
additional.children('.image-button').children('.image').show().find('img').attr('src', v.image);
}
additional.find(':input[name="weight"]').val((v.weight == 'null') ? '' : v.weight);
additional.find(':input[name="article"]').val((v.article == 'null') ? '' : v.article);
additional.find(':input[name="dimensions-width"]').val((v.dimensions.width == 'null') ? '' : v.dimensions.width);
additional.find(':input[name="dimensions-height"]').val((v.dimensions.height == 'null') ? '' : v.dimensions.height);
additional.find(':input[name="dimensions-length"]').val((v.dimensions.length == 'null') ? '' : v.dimensions.length);
tr.append(additional);
// article: 'null',
// weight: 'null',
// dimensions: {
block.find('tbody').append(tr);
});
ui.mountAll();
ui.start();
}
Product.generateValues = function(i) {
if(typeof Product.params[i] == 'undefined') {
return [];
}
if(Product.params[i].type == 'additional') {
return [];
}
var values = Product.params[i].values;
if(Product.params[i].type == 'priceless') {
values = [0];
}
// Product.values;
var data = [];
$.each(values, function(j, v) {
var index = i+'_'+j;
ids = Product.generateValues(i+1);
if(ids.length == 0) {
data.push(index);
} else {
$.each(ids, function(k, vk) {
data.push(index + '#' + vk);
});
}
});
return data;
}
Product.saveParams = function() {
var result = [];
$.each(Product.params, function(i, v) {
if(v.type == 'price') {
result = Product.generateValues(i);
return false;
}
});
var table = [];
$.each(result, function(i, v) {
var obj = $.extend(true, {}, Product.DEFAULT_PROPERTIES);
obj.id = v;
$.each(Product.values, function(j, k) {
if(k.id == obj.id) {
obj = $.extend(true, {} , k);
}
});
table.push(obj);
});
this.state.params = Product.params;
this.state.values = table;
Product.change({
params: Product.params,
values: table
});
Modal.hide();
}
// ----------->>>>>
/*
Product.addDefaultParam = function() {
if(Product.state.params.property.length == 0) {
Product.state.params.property.push({
name: '',
values: [],
type: 'select'
});
Product.change({
params: Product.state.params
});
}
}
Product.clearParams = function() {
var indexes = [];
$.each(Product.state.params.property, function(i, v) {
if(v['name'] == '') {
// indexes.push(i);
Product.state.params.property.splice(i, 1);
}
});
// $.each(for (var i = 0; i < indexes.length; i++) {
// Product.state.params.property.splice(indexes[i], 1);
// };)
}
Product.getParams = function() {
currentParams = $("#product-data-params").val();
currentParams = $.parseJSON(currentParams == '' ? '{}' : currentParams);
Product.change({
params: currentParams
});
}
Product.paramRemove = function(e) {
block = $(e).closest('.clear');
i = block.index();
this.state.params.property.splice(i, 1);
Product.change({
params: this.state.params
}, false);
Product.addDefaultParam();
Product.renderProperty();
// block.remove();
}
Product.currentTR = null;
Product.chooseImage = function(e) {
var _this = this,
t = function(id) { return _this.tpl(id); };
block = $(e).closest('tr');
i = block.index();
image = this.state.params.values[i]['image'];
Product.currentTR = this.state.params.values[i];
Modal.show({
src: 'productImages',
ready: function() {
block = $('#product-images');
$.each(_this.state.images, function(i, v) {
img = t('params.image.choose');
img.find('img').attr('src', v);
if(image == v) {
img.addClass('active');
}
block.append(img);
});
}
});
}
Product.changeTableImage = function(e) {
url = $(e).find('img').attr('src');
Product.currentTR['image'] = url;
Modal.hide();
Product.change({
params: this.state.params
});
}
Product.pathTable = function(newProperty, i) {
if(typeof newProperty[i] == 'undefined') {
return [];
}
// if(typeof property[i].values[j] == 'undefined') return [];
var path = [];
$.each(newProperty[i].values, function(j, v) {
var start = j.toString();
// console.log(k, v);
var p = Product.pathTable(newProperty, i+1);
if(p.length > 0) {
$.each(p, function(s, d) {
p = start + '' + d;
path.push(p);
});
} else {
path.push(start);
}
});
return path;
}
Product.createTable = function() {
var params = {
property: [],
values: [],
}
$("#product-params-property").children().each(function() {
name = $(this).find(':input[name="name"]').val();
values = $(this).find(':input[name="values"]').val().split(',');
type = 'select';
for (i = 0; i < values.length; i++) {
values[i] = values[i].trim();
}
if(name.trim() != '') {
params.property.push({
name: name,
values: values,
type: type
});
}
});
p = Product.pathTable(params.property, 0);
console.log(params, p);
$.each(p, function(i, v) {
values = [];
var obj = {
id: v,
buy_amount: '',
sell_amount: '',
sale_amount: '',
article: '',
image: 'null'
};
for (var j = 0; j < params.property.length; j++) {
values.push('');
};
$.each(Product.state.params.values, function(j, k) {
if(k.buy_amount != '') obj.buy_amount = k.buy_amount;
if(k.sell_amount != '') obj.sell_amount = k.sell_amount;
if(k.sale_amount != '' && k.sale_amount != 'NaN') obj.sale_amount = k.sale_amount;
if(k.article != '') obj.article = k.article;
if(k.id == v) {
if(k.image != 'null') obj.image = k.image;
return false;
}
});
console.log($.extend(true, {}, obj));
params.values.push(obj);
});
$("#product-data-params").val(JSON.stringify(params));
Product.change({
params: params
});
}
Product.renderProperty = function() {
var _this = this,
t = function(id) { return _this.tpl(id); };
block = $('#product-params-property');
block.html('');
$.each(this.state.params.property, function(i, v) {
console.log('-VALUES-', typeof v.values, v.values);
if(typeof v.values != 'object') {
v.values = [];
}
html = t('params.add');
html.attr('data-id', i);
html.find(':input[name="name"]').val(v.name);
html.find(':input[name="values"]').val(v.values.join(', '));
block.append(html);
});
}
*/