Quand ça marchait c'est parce que je testais sur un cas particulier où les "id" était égaux aux indexs...
var pseManager = (function($){
// cache dom elements
var manager = {};
var $pse = {};
function init(){
$pse = {
"id": $("#pse-id"),
"product": $("#product"),
"name": $("#pse-name"),
"ref": $("#pse-ref"),
"ean": $("#pse-ean"),
"availability": $("#pse-availability"),
"validity": $("#pse-validity"),
"quantity": $("#quantity"),
"promo": $("#pse-promo"),
"new": $("#pse-new"),
"weight": $("#pse-weight"),
"price": $("#pse-price"),
"priceOld": $("#pse-price-old"),
"submit": $("#pse-submit"),
"options": {},
"pseId": null,
"useFallback": false,
"fallback": $("#pse-options .pse-fallback")
};
}
function buildProductForm() {
var pse = null,
combinationId = null,
combinationValue = null,
combinationValueId = null,
combinations = null,
combinationName = [],
i;
// initialization for the first default pse
$pse.pseId = $pse.id.val();
if (PSE_COUNT > 1) {
// Use fallback method ?
$pse.useFallback = useFallback();
if ($pse.useFallback) {
$("#pse-options .option-option").remove();
for (pse in PSE){
combinations = PSE[pse].combinations;
combinationName = [];
if (undefined !== combinations) {
for (i = 0; i < combinations.length; i++){
combinationName.push(PSE_COMBINATIONS_VALUE[combinations[i]][0]);
}
}
$pse.fallback
.append("<option value='" + pse + "'>" + combinationName.join(', ') + "</option>");
}
$("#pse-options .pse-fallback").on("change", function() {
updateProductForm($(this));
});
} else {
$("#pse-options .option-fallback").remove();
// get the select for options
$("#pse-options .pse-option").each(function(){
var $option = $(this);
if ( $option.data("attribute") in PSE_COMBINATIONS){
$pse['options'][$option.data("attribute")] = $option; // jshint ignore:line
$option.on("change", function() {
updateProductForm($(this));
});
} else {
// not affected to this product -> remove
$option.closest(".option").remove();
}
});
// build select
for (combinationValueId in PSE_COMBINATIONS_VALUE) {
combinationValue = PSE_COMBINATIONS_VALUE[combinationValueId];
$pse.options[combinationValue[1]]
.append("<option value='" + combinationValueId + "'>" + combinationValue[0] + "</option>");
}
setPseForm();
}
}
}
function setPseForm(id) {
var pse = null,
combinationValueId;
pse = PSE[id || $pse.pseId];
if (undefined !== pse) {
if ($pse.useFallback) {
$pse.fallbak.val(pse.id);
} else {
for (var i = 0; i < pse.combinations.length; i++) {
combinationValueId = pse.combinations[i];
$pse['options'][PSE_COMBINATIONS_VALUE[combinationValueId][1]].val(pse.combinations[i]) // jshint ignore:line
}
}
}
}
function updateProductForm(obj) {
var pseId = null,
selection;
if (PSE_COUNT > 1) {
if ($pse.useFallback) {
pseId = $pse.fallback.val();
} else {
// get form data
selection = getFormSelection();
// get the pse
pseId = pseExist(selection);
if (!pseId) {
pseId = updateOptions(obj);
}
if ( ! pseId ) {
// not exists, revert
displayNotice();
setPseForm();
} else {
$pse.validity.hide();
}
}
// Trigger a change event to give the modules a change to detect PSE ID change.
$pse.id.val(pseId).trigger('change.pse', pseId);
$pse.pseId = pseId;
}
// Update UI
updateProductUI();
}
function updateOptions(obj) {
if (obj == undefined) {
obj = $pse['options'][Object.keys(PSE_COMBINATIONS)[0]];
}
var combinationIdSelected = obj.attr("data-attribute");
var combinationValueSelected = obj.val();
var indexSelected = Object.keys(PSE_COMBINATIONS).indexOf(combinationIdSelected);
var existingCombinations = [];
var optionsChanged = [];
for (pse in PSE) {
pseId = pse;
combinations = PSE[pse].combinations;
if (undefined !== combinations) {
if (combinations[indexSelected] == combinationValueSelected) {
existingCombinations.push(combinations);
}
}
}
for (pse_combination in PSE_COMBINATIONS) {
if (pse_combination != combinationIdSelected) {
var val = $pse['options'][pse_combination].val();
var index = Object.keys(PSE_COMBINATIONS).indexOf(pse_combination);
find = false;
for (var e = 0; e < existingCombinations.length; e++) {
if (existingCombinations[e][index] == val) {
find = true;
break;
}
}
if (!find) {
$pse['options'][pse_combination].val(existingCombinations[0][index]);
optionsChanged.push($pse['options'][pse_combination]);
for (var e = 1; e < existingCombinations.length; e++) {
if (existingCombinations[e][index] != existingCombinations[0][index]) {
existingCombinations.splice(e, 1);
}
}
}
}
}
$.each(optionsChanged, function(index,option) {
option.addClass('changed');
});
setTimeout(function() {
$.each(optionsChanged, function(index,option) {
option.removeClass('changed');
});
}, 250);
// get form data
selection = getFormSelection();
// get the pse
return pseExist(selection);
}
function displayNotice() {
var $validity = $pse.validity;
$validity.stop().show('fast', function(){
setTimeout(function(){
$validity.stop().hide('fast');
}, 3000);
});
}
function updateProductUI() {
var pse = PSE[$pse.pseId],
name = [],
pseValueId,
i
;
if (undefined !== pse) {
$pse.ref.html(pse.ref);
// $pse.ean.html(pse.ean);
// name
if (PSE_COUNT > 1) {
for (i = 0; i < pse.combinations.length; i++) {
pseValueId = pse.combinations[i];
name.push(
//PSE_COMBINATIONS[PSE_COMBINATIONS_VALUE[pseValueId][1]].name +
//":" +
PSE_COMBINATIONS_VALUE[pseValueId][0]
);
}
$pse.name.html(" - " + name.join(", ") + "");
}
// promo
if (pse.isPromo) {
$pse.product.addClass("product--is-promo");
} else {
$pse.product.removeClass("product--is-promo");
}
// new
if (pse.isNew) {
$pse.product.addClass("product--is-new");
} else {
$pse.product.removeClass("product--is-new");
}
// availability
if (pse.quantity > 0 || !PSE_CHECK_AVAILABILITY) {
setProductAvailable(true);
if (parseInt($pse.quantity.val()) > pse.quantity) {
$pse.quantity.val(pse.quantity);
}
if (PSE_CHECK_AVAILABILITY) {
$pse.quantity.attr("max", pse.quantity);
} else {
$pse.quantity.attr("max", PSE_DEFAULT_AVAILABLE_STOCK);
$pse.quantity.val("1");
}
} else {
setProductAvailable(false);
}
// price
if (pse.isPromo) {
$pse.priceOld.html(pse.price);
$pse.price.html(pse.promo);
} else {
$pse.priceOld.html("");
$pse.price.html(pse.price);
}
}
else {
setProductAvailable(false);
}
}
function setProductAvailable(available) {
if (available) {
$pse.availability
.removeClass("out-of-stock")
.addClass("in-stock")
.attr("href", "http://schema.org/InStock");
$pse.submit.prop("disabled", false);
}
else {
$pse.availability.removeClass("in-stock")
.addClass("out-of-stock")
.attr("href", "http://schema.org/OutOfStock");
$pse.submit.prop("disabled", true);
}
}
function pseExist(selection) {
var pseId,
pse = null,
combinations,
i,
j,
existCombination;
for (pse in PSE){
pseId = pse;
combinations = PSE[pse].combinations;
if (undefined !== combinations) {
for (i = 0; i < selection.length; i++) {
existCombination = false;
for (j = 0; j < combinations.length; j++) {
if (selection[i] == combinations[j]) {
existCombination = true;
break;
}
}
if (existCombination === false) {
break;
}
}
if (existCombination) {
return pseId;
}
}
}
return false;
}
function useFallback() {
var pse = null,
count = -1,
pseCount = 0,
combinations,
i;
for (pse in PSE){
combinations = PSE[pse].combinations;
if (undefined !== combinations) {
pseCount = 0;
for (i = 0; i < combinations.length; i++) {
pseCount += PSE_COMBINATIONS_VALUE[combinations[i]][1];
}
if (count == -1) {
count = pseCount;
} else if (count != pseCount) {
return true;
}
}
}
return (count <= 0);
}
function getFormSelection() {
var selection = [],
combinationId;
for (combinationId in $pse.options){
selection.push($pse.options[combinationId].val());
}
return selection;
}
manager.load = function(){
init();
buildProductForm();
updateProductForm();
};
return manager;
}(jQuery));
!function(){for(var t,o=function(){},i=["assert","clear","count","debug","dir","dirxml","error","exception","group","groupCollapsed","groupEnd","info","log","markTimeline","profile","profileEnd","table","time","timeEnd","timeStamp","trace","warn"],n=i.length,e=window.console=window.console||{};n--;)e[t=i[n]]||(e[t]=o)}();var pseManager=function(t){var o={},i={};function n(){var o,n=null,s=null,r=null,c=null,l=[];if(i.pseId=i.id.val(),PSE_COUNT>1)if(i.useFallback=function(){var t,o,i=null,n=-1,e=0;for(i in PSE)if(void 0!==(t=PSE[i].combinations)){for(e=0,o=0;o<t.length;o++)e+=PSE_COMBINATIONS_VALUE[t[o]][1];if(-1==n)n=e;else if(n!=e)return!0}return n<=0}(),i.useFallback){for(n in t("#pse-options .option-option").remove(),PSE){if(l=[],void 0!==(c=PSE[n].combinations))for(o=0;o<c.length;o++)l.push(PSE_COMBINATIONS_VALUE[c[o]][0]);i.fallback.append("<option value='"+n+"'>"+l.join(", ")+"</option>")}t("#pse-options .pse-fallback").on("change",function(){a(t(this))})}else{for(r in t("#pse-options .option-fallback").remove(),t("#pse-options .pse-option").each(function(){var o=t(this);o.data("attribute")in PSE_COMBINATIONS?(i.options[o.data("attribute")]=o,o.on("change",function(){a(t(this))})):o.closest(".option").remove()}),PSE_COMBINATIONS_VALUE)s=PSE_COMBINATIONS_VALUE[r],i.options[s[1]].append("<option value='"+r+"'>"+s[0]+"</option>");e()}}function e(t){var o,n;if(void 0!==(o=PSE[t||i.pseId]))if(i.useFallback)i.fallbak.val(o.id);else for(var e=0;e<o.combinations.length;e++)n=o.combinations[e],i.options[PSE_COMBINATIONS_VALUE[n][1]].val(o.combinations[e])}function a(t){var o,n=null;PSE_COUNT>1&&(i.useFallback?n=i.fallback.val():((n=c(l()))||(n=s(t)),n?i.validity.hide():((o=i.validity).stop().show("fast",function(){setTimeout(function(){o.stop().hide("fast")},3e3)}),e())),i.id.val(n).trigger("change.pse",n),i.pseId=n),function(){var t,o,n=PSE[i.pseId],e=[];if(void 0!==n){if(i.ref.html(n.ref),PSE_COUNT>1){for(o=0;o<n.combinations.length;o++)t=n.combinations[o],e.push(PSE_COMBINATIONS_VALUE[t][0]);i.name.html(" - "+e.join(", "))}n.isPromo?i.product.addClass("product--is-promo"):i.product.removeClass("product--is-promo"),n.isNew?i.product.addClass("product--is-new"):i.product.removeClass("product--is-new"),n.quantity>0||!PSE_CHECK_AVAILABILITY?(r(!0),parseInt(i.quantity.val())>n.quantity&&i.quantity.val(n.quantity),PSE_CHECK_AVAILABILITY?i.quantity.attr("max",n.quantity):(i.quantity.attr("max",PSE_DEFAULT_AVAILABLE_STOCK),i.quantity.val("1"))):r(!1),n.isPromo?(i.priceOld.html(n.price),i.price.html(n.promo)):(i.priceOld.html(""),i.price.html(n.price))}else r(!1)}()}function s(o){null==o&&(o=i.options[Object.keys(PSE_COMBINATIONS)[0]]);var n=o.attr("data-attribute"),e=o.val(),a=Object.keys(PSE_COMBINATIONS).indexOf(n),s=[],r=[];for(pse in PSE)pseId=pse,combinations=PSE[pse].combinations,void 0!==combinations&&combinations[a]==e&&s.push(combinations);for(pse_combination in PSE_COMBINATIONS)if(pse_combination!=n){var u=i.options[pse_combination].val(),d=Object.keys(PSE_COMBINATIONS).indexOf(pse_combination);find=!1;for(var p=0;p<s.length;p++)if(s[p][d]==u){find=!0;break}if(!find){i.options[pse_combination].val(s[0][d]),r.push(i.options[pse_combination]);for(p=1;p<s.length;p++)s[p][d]!=s[0][d]&&s.splice(p,1)}}return t.each(r,function(t,o){o.addClass("changed")}),setTimeout(function(){t.each(r,function(t,o){o.removeClass("changed")})},250),selection=l(),c(selection)}function r(t){t?(i.availability.removeClass("out-of-stock").addClass("in-stock").attr("href","http://schema.org/InStock"),i.submit.prop("disabled",!1)):(i.availability.removeClass("in-stock").addClass("out-of-stock").attr("href","http://schema.org/OutOfStock"),i.submit.prop("disabled",!0))}function c(t){var o,i,n,e,a,s=null;for(s in PSE)if(o=s,void 0!==(i=PSE[s].combinations)){for(n=0;n<t.length;n++){for(a=!1,e=0;e<i.length;e++)if(t[n]==i[e]){a=!0;break}if(!1===a)break}if(a)return o}return!1}function l(){var t,o=[];for(t in i.options)o.push(i.options[t].val());return o}return o.load=function(){i={id:t("#pse-id"),product:t("#product"),name:t("#pse-name"),ref:t("#pse-ref"),ean:t("#pse-ean"),availability:t("#pse-availability"),validity:t("#pse-validity"),quantity:t("#quantity"),promo:t("#pse-promo"),new:t("#pse-new"),weight:t("#pse-weight"),price:t("#pse-price"),priceOld:t("#pse-price-old"),submit:t("#pse-submit"),options:{},pseId:null,useFallback:!1,fallback:t("#pse-options .pse-fallback")},n(),a()},o}(jQuery);!function(t){var o={"address.delete":function(o){t.post(o.attr("href"),function(t){t.success?o.closest("tr").remove():bootbox.alert(t.message)})}};t(document).ready(function(){var i=t('<div class="loader"></div>');t("body").append(i),t(document).ajaxStart(function(){i.show()}).ajaxStop(function(){i.hide()}).ajaxError(function(){i.hide()});var n=t(window).width()>768;t(".navbar").on("click.subnav","[data-toggle=dropdown]",function(o){if(t(this).parent().hasClass("open")&&t(this).is(o.target))return!1}).on("mouseenter.subnav",".dropdown",function(){t(this).hasClass("open")||t(this).addClass("open")}).on("mouseleave.subnav",".dropdown",function(){var o=t(this);o.hasClass("open")&&(0===o.find(":input:focus").length?o.removeClass("open"):o.find(":input:focus").one("blur",function(){o.trigger("mouseleave.subnav")}))}),t("body").tooltip({selector:"[data-toggle=tooltip]"}),t(document).on("click.confirm","[data-confirm]",function(){var i=t(this),n=i.attr("href"),e=i.attr("data-confirm-callback"),a=""!==i.attr("data-confirm")?i.attr("data-confirm"):"Are you sure?";return bootbox.confirm(a,function(a){if(a)if(e&&t.isFunction(o[e]))o[e](i);else if(n)window.location.href=n;else{var s=i.closest("form");s.size()>0&&s.submit()}}),!1}),t(document).on("click.product-quickview",".product-quickview",function(){if(n)return t.get(this.href,function(o){bootbox.hideAll(),bootbox.dialog({message:t("#product",o),onEscape:function(){bootbox.hideAll()}}),window.pseManager.load()}),!1}),void 0!==window.PSE_FORM&&window.pseManager.load(),t(document).on("submit.form-product",".form-product",function(){if(n){var o=t(this).attr("action"),i=t("input[name$='product_id']",this).val(),e=t("input.pse-id",this).val(),a=t("#quantity",this).val();return t.ajax({type:"POST",data:t(this).serialize(),url:o,success:function(o){t(".cart-container").html(t(o).html()),t.ajax({url:addCartMessageUrl,data:{product_id:i,pse_id:e,quantity:a},success:function(t){bootbox.hideAll(),bootbox.dialog({message:t,onEscape:function(){bootbox.hideAll()}})}})},error:function(t){console.log("Error.",t)}}),!1}});var e=t("#category-products").parent();e.on("click.view-mode","[data-toggle=view]",function(){if(!(t(this).hasClass("btn-grid")&&e.hasClass("grid")||t(this).hasClass("btn-list")&&e.hasClass("list")))return i.show(),setTimeout(function(){e.toggleClass("grid").toggleClass("list"),i.hide()},400),!1});var a=t("#form-login");a.on("change.account",":radio",function(){"0"===t(this).val()?t("#password",a).val("").prop("disabled",!0):t("#password",a).prop("disabled",!1)}).find(":radio:checked").trigger("change.account"),t("#form-newsletter-mini").on("submit.newsletter",function(){return t.ajax({url:t(this).attr("action"),type:t(this).attr("method"),data:t(this).serialize(),dataType:"json",success:function(t){bootbox.alert(t.message)},error:function(o){try{bootbox.alert(t.parseJSON(o.responseText).message)}catch(t){bootbox.alert(o.responseText)}}}),!1}),t("#form-filters").each(function(){var o=t(this);o.on("change.filter",":checkbox",function(){i.show(),o.submit()}).find(".group-btn > .btn").addClass("sr-only")}),t(document).on("click.thumbnails","#product-thumbnails .thumbnail",function(){if(t(this).hasClass("active"))return!1;var o=t(this).closest("#product-gallery");return t(".product-image > img",o).attr("src",t(this).attr("href")),t(".thumbnail",o).removeClass("active"),t(this).addClass("active"),!1}),t("#product-gallery").each(function(){t(".item",this).size()>1&&t("#product-thumbnails",this).carousel({interval:!1}).find(".carousel-control").show()}),t("#payment-method").each(function(){var o=t("label",this);o.on("change",":radio",function(){o.removeClass("active"),o.filter('[for="'+t(this).attr("id")+'"]').addClass("active")}).filter(":has(:checked)").addClass("active")}),t("#form-contact, #form-register, #form-address").validate({highlight:function(o){t(o).closest(".form-group").addClass("has-error")},unhighlight:function(o){t(o).closest(".form-group").removeClass("has-error")},errorElement:"span",errorClass:"help-block"}),t("#content").on("change.toolbarfilter","#limit-top, #sortby-top",function(){window.location=t(this).val()}),a.each(function(){var o=t('input[type="email"]',a),i=t("#account1",a);o.on("keypress",function(){i.click()})})})}(jQuery),function(t){t(document).ready(function(){(initialize=function(o){var i={};i.state=t(o),i.stateId=i.state.val(),i.country=t(i.state.data("thelia-country")),i.countryId=i.country.val(),i.block=t(i.state.data("thelia-toggle")),i.states=i.state.children().clone(),i.state.children().remove();var n=function(){var o=i.country.val(),n=i.state.val(),e=!1;null!==n&&""!==n&&(i.stateId=n),i.state.children().remove(),i.states.each(function(){var n=t(this);n.data("country")==o&&(n.appendTo(i.state),e=!0)}),e?(i.state.val(i.stateId),i.block.removeClass("hidden")):i.block.addClass("hidden")};i.country.on("change",n),n()},{init:function(){t("[data-thelia-state]").each(function(){initialize(this)})}}).init()})}(jQuery);