Advertisement
andhiirawan

Magento : Product Detail Page : JS-Config : for Colorswicher

Oct 1st, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //js\varien\config.js
  2.  
  3. if(typeof Product=='undefined') {
  4.     var Product = {};
  5. }
  6. if(typeof selectedAssocProducts=='undefined') {
  7.         var selectedAssocProducts = {};
  8.     }
  9. /**************************** CONFIGURABLE PRODUCT **************************/
  10. var _BigImg;
  11. Product.Config = Class.create();
  12. Product.Config.prototype = {
  13.     initialize: function(config){
  14.         this.config     = config;
  15.         this.taxConfig  = this.config.taxConfig;
  16.         this.settings   = $$('.super-attribute-select');
  17.         this.state      = new Hash();
  18.         this.priceTemplate = new Template(this.config.template);
  19.         this.prices     = config.prices;
  20.         this.settings.each(function(element){
  21.             Event.observe(element, 'change', this.configure.bind(this))
  22.         }.bind(this));
  23.  
  24.         // fill state
  25.         this.settings.each(function(element){
  26.             var attributeId = element.id.replace(/[a-z]*/, '');
  27.             if(attributeId && this.config.attributes[attributeId]) {
  28.                 element.config = this.config.attributes[attributeId];
  29.                 element.attributeId = attributeId;
  30.                 this.state[attributeId] = false;
  31.             }
  32.         }.bind(this))
  33.  
  34.         // Init settings dropdown
  35.         var childSettings = [];
  36.         for(var i=this.settings.length-1;i>=0;i--){
  37.             var prevSetting = this.settings[i-1] ? this.settings[i-1] : false;
  38.             var nextSetting = this.settings[i+1] ? this.settings[i+1] : false;
  39.             if(i==0){
  40.                 this.fillSelect(this.settings[i])
  41.             }
  42.             else {
  43.                 this.settings[i].disabled=true;
  44.             }
  45.             $(this.settings[i]).childSettings = childSettings.clone();
  46.             $(this.settings[i]).prevSetting   = prevSetting;
  47.             $(this.settings[i]).nextSetting   = nextSetting;
  48.             childSettings.push(this.settings[i]);
  49.         }
  50.  
  51.         // try retireve options from url
  52.         var separatorIndex = window.location.href.indexOf('#');
  53.         if (separatorIndex!=-1) {
  54.             var paramsStr = window.location.href.substr(separatorIndex+1);
  55.             this.values = paramsStr.toQueryParams();
  56.             this.settings.each(function(element){
  57.                 var attributeId = element.attributeId;
  58.                 element.value = this.values[attributeId];
  59.                 this.configureElement(element);
  60.             }.bind(this));
  61.         }
  62.     },
  63.  
  64.     configure: function(event){
  65.         var element = Event.element(event);
  66.         this.configureElement(element);
  67.     },
  68.  
  69.     configureElement : function(element) {
  70.         this.reloadOptionLabels(element);
  71.         if(element.value){
  72.             this.state[element.config.id] = element.value;
  73.             if(element.nextSetting){
  74.                 element.nextSetting.disabled = false;
  75.                 this.fillSelect(element.nextSetting);
  76.                 this.resetChildren(element.nextSetting);
  77.             }
  78.         }
  79.         else {
  80.             this.resetChildren(element);
  81.         }
  82.         this.reloadPrice();
  83. //      Calculator.updatePrice();
  84.         /***** Load Associated Image : This should come after this.resetChildren is called *****/
  85.      
  86.     // If an option doesnt have a value attribute, it'll take its innerHTML as its value - hence the reason for || element.value.substr(0,6) == 'choose'
  87.     if (!element.value || element.value.substr(0,6) == 'choose') return; // Selected "choose option"
  88.     var attributeId = element.id.replace(/[a-z]*/, '');
  89.     for (var a in this.config.attributes)
  90.     {
  91.         for (i = 0; i < this.config.attributes[a].options.length; i++)
  92.         {
  93.             if (this.config.attributes[a].options[i].id != element.value) continue;
  94.             selectedAssocProducts[a] = this.config.attributes[attributeId].options[i].products;
  95.            
  96.         }
  97.     }
  98.      
  99.         var productNo = selectedAssocProductsImg(selectedAssocProducts) || selectedAssocProducts[attributeId][0];
  100.         var changeImg = document.getElementsByClassName("normalimg");
  101.         var changeBigImg = document.getElementsByClassName("masterzoom");
  102.         var associatedProductImg= assocIMG[productNo] ;
  103.         var associatedProductBigImg= assocBIGIMG[productNo] ;
  104.        
  105.         /*var i__;
  106.         for (i__ = 0; i__ < changeImg.length; i__++) {
  107.             changeImg[i__].src=associatedProductImg;
  108.         }
  109.         for (i__ = 0; i__ < changeBigImg.length; i__++) {
  110.             changeBigImg[i__].href=associatedProductBigImg;
  111.         }*/
  112.         if(typeof _xtemp !== 'undefined' && typeof associatedProductImg !== 'undefined'){
  113.            
  114.             this.appendBigImg(associatedProductBigImg);
  115.             jQuery('.zoomPad').unbind().remove();
  116.            
  117.             _dfi++;
  118.             ne = _xtemp.replace(/xxx/g, _dfi).replace(/xxsmall/g, associatedProductImg).replace(/xxbig/g, associatedProductBigImg);
  119.             net = _xttemp.replace(/xxx/g, _dfi);
  120.             jQuery(".product-image").html(ne);
  121.             jQuery(".more-views").html("<ul id='gal"+_dfi+"'>" + net + "</ul>");
  122.             jQuery('#gal'+_dfi).bxSlider({
  123.                 auto: false,
  124.                 mode: 'vertical',
  125.                 minSlides: 4,
  126.                 maxSlides: 4,
  127.                 nextText: '',
  128.                 prevText: '',
  129.                 pager: false,
  130.                 preloadImages:'all',      
  131.                 hideControlOnEnd: true,
  132.                 infiniteLoop: false    
  133.             });    
  134.        
  135.              setTimeout(function(){
  136.              jQuery('.jqzoom'+_dfi).jqzoom({
  137.                zoomType: 'reverse',
  138.                lens:true,
  139.                preloadImages: false,
  140.                alwaysOn:false,
  141.                zoomWidth: 478,
  142.                zoomHeight: 497,
  143.              });
  144.              }, 100);
  145.         }
  146.     },
  147.    
  148.     appendBigImg:function(url){
  149.         /*jQuery('.jqzoom').removeData('jqzoom');
  150.         jQuery('.jqzoom').unbind();
  151.         jQuery('.zoomPup').unbind();
  152.         jQuery('#gal1').find('img').unbind();
  153.         jQuery('#gal1').find('a').unbind();
  154.        
  155.         _BigImg = new Image();
  156.         _BigImg.style.position = 'absolute';
  157.         _BigImg.style.border = '0px';
  158.         _BigImg.style.display = 'block';
  159.         _BigImg.style.left = '-5000px';
  160.         _BigImg.style.top = '0px';
  161.         document.body.appendChild(_BigImg);
  162.         _BigImg.src = url; // fires off async
  163.         this.fetchBigImg();*/
  164.     },
  165.    
  166.     fetchBigImg:function(){
  167.         _BigImg.style.left = '0px';
  168.         document.body.removeChild(_BigImg);
  169.         document.getElementById('zwi').innerHTML = "";
  170.         document.getElementById('zwi').appendChild( _BigImg);
  171.        
  172.         i = jQuery('#jqzomm-masterimg').clone();
  173.         jQuery('.jqzoom').html( i.prop('outerHTML') ); 
  174.         jQuery('.jqzoom').jqzoom({
  175.            zoomType: 'reverse',
  176.            lens:true,
  177.            preloadImages: false,
  178.            alwaysOn:false,
  179.            zoomWidth: 478,
  180.            zoomHeight: 497,
  181.          });
  182.        
  183.      
  184.     },
  185.  
  186.     reloadOptionLabels: function(element){
  187.         var selectedPrice;
  188.         if(element.options[element.selectedIndex].config){
  189.             selectedPrice = parseFloat(element.options[element.selectedIndex].config.price)
  190.         }
  191.         else{
  192.             selectedPrice = 0;
  193.         }
  194.         for(var i=0;i<element.options.length;i++){
  195.             if(element.options[i].config){
  196.                 element.options[i].text = this.getOptionLabel(element.options[i].config, element.options[i].config.price-selectedPrice);
  197.             }
  198.         }
  199.     },
  200.  
  201.     resetChildren : function(element){
  202.         if(element.childSettings) {
  203.             for(var i=0;i<element.childSettings.length;i++){
  204.                 element.childSettings[i].selectedIndex = 0;
  205.                 element.childSettings[i].disabled = true;
  206.                 if(element.config){
  207.                     this.state[element.config.id] = false;
  208.                 }
  209.             }
  210.         }
  211.     },
  212.  
  213.     fillSelect: function(element){
  214.         var attributeId = element.id.replace(/[a-z]*/, '');
  215.         var options = this.getAttributeOptions(attributeId);
  216.         this.clearSelect(element);
  217.         element.options[0] = new Option(this.config.chooseText, '');
  218.  
  219.         var prevConfig = false;
  220.         if(element.prevSetting){
  221.             prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
  222.         }
  223.  
  224.         if(options) {
  225.             var index = 1;
  226.             for(var i=0;i<options.length;i++){
  227.                 var allowedProducts = [];
  228.                 if(prevConfig) {
  229.                     for(var j=0;j<options[i].products.length;j++){
  230.                         if(prevConfig.config.allowedProducts
  231.                             && prevConfig.config.allowedProducts.indexOf(options[i].products[j])>-1){
  232.                             allowedProducts.push(options[i].products[j]);
  233.                         }
  234.                     }
  235.                 } else {
  236.                     allowedProducts = options[i].products.clone();
  237.                 }
  238.  
  239.                 if(allowedProducts.size()>0){
  240.                     options[i].allowedProducts = allowedProducts;
  241.                     element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
  242.                     element.options[index].config = options[i];
  243.                     index++;
  244.                 }
  245.             }
  246.         }
  247.     },
  248.  
  249.     getOptionLabel: function(option, price){
  250.         var price = parseFloat(price);
  251.         if (this.taxConfig.includeTax) {
  252.             var tax = price / (100 + this.taxConfig.defaultTax) * this.taxConfig.defaultTax;
  253.             var excl = price - tax;
  254.             var incl = excl*(1+(this.taxConfig.currentTax/100));
  255.         } else {
  256.             var tax = price * (this.taxConfig.currentTax / 100);
  257.             var excl = price;
  258.             var incl = excl + tax;
  259.         }
  260.  
  261.         if (this.taxConfig.showIncludeTax || this.taxConfig.showBothPrices) {
  262.             price = incl;
  263.         } else {
  264.             price = excl;
  265.         }
  266.  
  267.         var str = option.label;
  268.         if(price){
  269.             if (this.taxConfig.showBothPrices) {
  270.                 str+= ' ' + this.formatPrice(excl, true) + ' (' + this.formatPrice(price, true) + ' ' + this.taxConfig.inclTaxTitle + ')';
  271.             } else {
  272.                 str+= ' ' + this.formatPrice(price, true);
  273.             }
  274.         }
  275.         return str;
  276.     },
  277.  
  278.     formatPrice: function(price, showSign){
  279.         var str = '';
  280.         price = parseFloat(price);
  281.         if(showSign){
  282.             if(price<0){
  283.                 str+= '-';
  284.                 price = -price;
  285.             }
  286.             else{
  287.                 str+= '+';
  288.             }
  289.         }
  290.  
  291.         var roundedPrice = (Math.round(price*100)/100).toString();
  292.  
  293.         if (this.prices && this.prices[roundedPrice]) {
  294.             str+= this.prices[roundedPrice];
  295.         }
  296.         else {
  297.             str+= this.priceTemplate.evaluate({price:price.toFixed(2)});
  298.         }
  299.         return str;
  300.     },
  301.  
  302.     clearSelect: function(element){
  303.         for(var i=element.options.length-1;i>=0;i--){
  304.             element.remove(i);
  305.         }
  306.     },
  307.  
  308.     getAttributeOptions: function(attributeId){
  309.         if(this.config.attributes[attributeId]){
  310.             return this.config.attributes[attributeId].options;
  311.         }
  312.     },
  313.  
  314.     reloadPrice: function(){
  315.         var price = 0;
  316.         for(var i=this.settings.length-1;i>=0;i--){
  317.             var selected = this.settings[i].options[this.settings[i].selectedIndex];
  318.             if(selected.config){
  319.                 price += parseFloat(selected.config.price);
  320.             }
  321.         }
  322.  
  323.         optionsPrice.changePrice('config', price);
  324.         optionsPrice.reload();
  325.  
  326.         return price;
  327.  
  328.         if($('product-price-'+this.config.productId)){
  329.             $('product-price-'+this.config.productId).innerHTML = price;
  330.         }
  331.         this.reloadOldPrice();
  332.     },
  333.  
  334.     reloadOldPrice: function(){
  335.         if ($('old-price-'+this.config.productId)) {
  336.  
  337.             var price = parseFloat(this.config.oldPrice);
  338.             for(var i=this.settings.length-1;i>=0;i--){
  339.                 var selected = this.settings[i].options[this.settings[i].selectedIndex];
  340.                 if(selected.config){
  341.                     price+= parseFloat(selected.config.price);
  342.                 }
  343.             }
  344.             if (price < 0)
  345.                 price = 0;
  346.             price = this.formatPrice(price);
  347.  
  348.             if($('old-price-'+this.config.productId)){
  349.                 $('old-price-'+this.config.productId).innerHTML = price;
  350.             }
  351.  
  352.         }
  353.     }
  354. }
  355. function selectedAssocProductsImg(selectedAssocProduct) // passing associated image array here
  356.     {
  357.         if (selectedAssocProduct == null) return false;
  358.            
  359.         var newselectedAssocProduct = new Array();
  360.         if (selectedAssocProduct.length == undefined) // push all data in new array if more than one images
  361.         {    
  362.             for (var i in selectedAssocProduct)
  363.              newselectedAssocProduct.push(selectedAssocProduct[i]);      
  364.         }    
  365.         else
  366.          newselectedAssocProduct = selectedAssocProduct;
  367.          
  368.         if (newselectedAssocProduct.length == 1) return false; // return false if array legth 1
  369.       var finalImgArray = new Array();
  370.         function eachAttribute(newselectedAssocProduct, iIndex, jIndex, JnextIndex)
  371.         {              
  372.             if (iIndex == null) iIndex = 0;
  373.             if (jIndex == null) jIndex = 0;
  374.             if (JnextIndex == null) JnextIndex = newselectedAssocProduct[iIndex].length;
  375.             if (iIndex == newselectedAssocProduct.length - 1) return;          
  376.             for (var i = jIndex; i < JnextIndex; i++)
  377.             {
  378.                 if (finalImgArray.indexOf(newselectedAssocProduct[iIndex][i]) != -1) continue;
  379.                 for (var j = 0; j < newselectedAssocProduct[iIndex + 1].length; j++)
  380.                 {
  381.                     if (newselectedAssocProduct[iIndex][i] != newselectedAssocProduct[iIndex+1][j]) continue;                      
  382.                     eachAttribute(newselectedAssocProduct, iIndex + 1, j, j + 1);
  383.                     if (iIndex + 1 == newselectedAssocProduct.length - 1) { finalImgArray.push(newselectedAssocProduct[iIndex][i]); break; }                      
  384.                 }
  385.             }          
  386.         }      
  387.      
  388.         eachAttribute(newselectedAssocProduct);
  389.         return finalImgArray;
  390.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement