manage-products.js
4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(function($) {
$("#manage-product-details-button").live('click', function() {
$("#product-price-details").find('.loading-area').addClass('small-loading');
url = $(this).attr('href');
$.get(url, function(data){
$("#manage-product-details-button").hide();
$("#display-price-details").hide();
$("#display-manage-price-details").html(data);
$("#product-price-details").find('.loading-area').removeClass('small-loading');
});
return false;
});
$(".cancel-price-details").live('click', function() {
if ( !$(this).hasClass('form-changed') ) {
cancelPriceDetailsEdition();
} else {
if (confirm($(this).attr('data-confirm'))) {
cancelPriceDetailsEdition();
}
}
return false;
});
$("#manage-product-details-form").live('submit', function(data) {
var form = this;
$(form).find('.loading-area').addClass('small-loading');
$(form).css('cursor', 'progress');
$.post(form.action, $(form).serialize(), function(data) {
$("#display-manage-price-details").html(data);
$("#manage-product-details-button").show();
});
if ($('#progressbar-icon').hasClass('ui-icon-check')) {
display_notice($('#price-described-notice').show());
}
return false;
});
$("#add-new-cost").live('click', function() {
$('#display-product-price-details tbody').append($('#new-cost-fields tbody').html());
return false;
});
$(".cancel-new-cost").live('click', function() {
$(this).parents('tr').remove();
return false;
});
$("#product-info-form").live('submit', function(data) {
var form = this;
updatePriceCompositionBar(form);
});
$("form.edit_input").live('submit', function(data) {
var form = this;
updatePriceCompositionBar(form);
inputs_cost_update_url = $(form).find('#inputs-cost-update-url').val();
$.get(inputs_cost_update_url, function(data){
$(".inputs-cost").html(data);
});
return false;
});
$("#manage-product-details-form .price-details-price").live('keydown', function(data) {
$('.cancel-price-details').addClass('form-changed');
var product_price = parseFloat($('form #product_price').val());
var total_cost = parseFloat($('#product_inputs_cost').val());
$('form .price-details-price').each(function() {
total_cost = total_cost + parseFloat($(this).val());
});
enablePriceDetailSubmit();
var described = (product_price - total_cost) == 0;
var percentage = total_cost * 100 / product_price;
priceCompositionBar(percentage, described, total_cost, product_price);
});
function cancelPriceDetailsEdition() {
$("#manage-product-details-button").show();
$("#display-price-details").show();
$("#display-manage-price-details").html('');
};
function updatePriceCompositionBar(form) {
bar_url = $(form).find('.bar-update-url').val();
$.get(bar_url, function(data){
$("#price-composition-bar").html(data);
});
};
function enablePriceDetailSubmit() {
$('#manage-product-details-form input.submit').removeAttr("disabled").removeClass('disabled');
};
})(jQuery);
function productionCostTypeChange(select, url, question, error_msg) {
if (select.value == '') {
var newType = prompt(question);
jQuery.ajax({
url: url + "/" + newType,
dataType: 'json',
success: function(data, status, ajax){
if (data.ok) {
var opt = jQuery('<option value="' + data.id + '">' + newType + '</option>');
opt.insertBefore(jQuery("option:last", select));
select.selectedIndex = select.options.length - 2;
opt.clone().insertBefore('#new-cost-fields .production-cost-selection option:last');
} else {
alert(data.error_msg);
}
},
error: function(ajax, status, error){
alert(error_msg);
}
});
}
}
function priceCompositionBar(value, described, total_cost, price) {
jQuery(function($) {
var bar_area = $('#price-composition-bar');
$(bar_area).find('#progressbar').progressbar({
value: value
});
$(bar_area).find('.production-cost').html(total_cost.toFixed(2));
$(bar_area).find('.product_price').html(price.toFixed(2));
if (described) {
$(bar_area).find('#progressbar-icon').addClass('ui-icon-check');
$(bar_area).find('#progressbar-icon').attr('title', $('#price-described-message').html());
$(bar_area).find('div.ui-progressbar-value').addClass('price-described');
} else {
$(bar_area).find('#progressbar-icon').removeClass('ui-icon-check');
$(bar_area).find('#progressbar-icon').attr('title', $('#price-not-described-message').html());
$(bar_area).find('div.ui-progressbar-value').removeClass('price-described');
}
});
}