Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Easylife_Switcher extension
  3.  *
  4.  * NOTICE OF LICENSE
  5.  *
  6.  * This source file is subject to the MIT License
  7.  * that is bundled with this package in the file LICENSE_EASYLIFE_SWITCHER.txt.
  8.  * It is also available through the world-wide-web at this URL:
  9.  * http://opensource.org/licenses/mit-license.php
  10.  *
  11.  * @category    Easylife
  12.  * @package     Easylife_Switcher
  13.  * @copyright   Copyright (c) 2013
  14.  * @license     http://opensource.org/licenses/mit-license.php MIT License
  15.  */
  16. /**
  17.  * extend Product.Config class to support image switchers and option labels
  18.  *
  19.  * @category    Easylife
  20.  * @package     Easylife_Switcher
  21.  * @author      Marius Strajeru <marius.strajeru@gmail.com>
  22.  */
  23. if(typeof Easylife=='undefined') {
  24.     var Easylife = {};
  25. }
  26. Easylife.Switcher = Class.create(Product.Config, {
  27.     /**
  28.      * rewrite initialize to transform dorpdowns and support default configuration
  29.      * @param $super
  30.      * @param config
  31.      * @author Marius Strajeru <marius.strajeru@gmail.com>
  32.      */
  33.     initialize: function($super, config){
  34.         if (typeof config.defaultValues == 'undefined'){
  35.             var separatorIndex = window.location.href.indexOf('#');
  36.             if (separatorIndex != -1) {
  37.                 var paramsStr = window.location.href.substr(separatorIndex+1);
  38.                 var urlValues = paramsStr.toQueryParams();
  39.                 if (!config.defaultValues) {
  40.                     config.defaultValues = {};
  41.                 }
  42.                 for (var i in urlValues) {
  43.                     if (typeof config.attributes[i] != 'undefined'){
  44.                         config.defaultValues[i] = urlValues[i];
  45.                     }
  46.                 }
  47.             }
  48.             if (config.autoselect_first) {
  49.                 var count = 0;
  50.                 if (typeof config.defaultValues != 'undefined'){
  51.                     for (var k in config.defaultValues) {
  52.                         if (config.defaultValues.hasOwnProperty(k)) {
  53.                             ++count;
  54.                         }
  55.                     }
  56.                 }
  57.                 if (count == 0){
  58.                     config.defaultValues = {};
  59.                     for (var attribute in config.attributes){
  60.                         if (config.attributes.hasOwnProperty(attribute)){
  61.                             var option = config.attributes[attribute].options[0].id;
  62.                             config.defaultValues[attribute] = option;
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         $super(config);
  69.         this.rewritten = true;
  70.         if (this.config.transform_dropdowns){
  71.             this.transformDropdowns();
  72.         }
  73.     },
  74.     /**
  75.      * rewrite fillSelect to transform elements to labels
  76.      * @param $super
  77.      * @param element
  78.      * @author Marius Strajeru <marius.strajeru@gmail.com>
  79.      */
  80.     fillSelect: function($super, element){
  81.         $super(element);
  82.         if (this.config.transform_dropdowns){
  83.             this.transformDropdown(element);
  84.         }
  85.         else {
  86.             if (!this.config.allow_no_stock_select) {
  87.                 var attributeId = element.id.replace(/[a-z]*/, '');
  88.                 var options = this.getAttributeOptions(attributeId);
  89.                 for (var i in options) {
  90.                     if (options.hasOwnProperty(i)){
  91.                         var optval = options[i].id;
  92.                         var inStock = this.isInStock(attributeId, optval)
  93.                         $(element).select('option').each (function(elem){
  94.                             if ($(elem).value == optval && !inStock) {
  95.                                 $(elem).disabled="disabled";
  96.                             }
  97.                         });
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.  
  103.     },
  104.     /**
  105.      * transform dropdowns to labels
  106.      * @author Marius Strajeru <marius.strajeru@gmail.com>
  107.      */
  108.     transformDropdowns: function(){
  109.         for (var i=0; i<this.settings.length;i++){
  110.             this.transformDropdown(this.settings[i]);
  111.         }
  112.     },
  113.     /**
  114.      * transform one dropdown to labels
  115.      * @param selectid
  116.      * @author Marius Strajeru <marius.strajeru@gmail.com>
  117.      */
  118.     transformDropdown: function(selectid){
  119.         var that = this;
  120.         var attributeId = $(selectid).id.replace(/[a-z]*/, '');
  121.         var selectname = $(selectid).name;
  122.         var newId = $(selectid).id +'_switchers';
  123.         //remove previous labels
  124.         if ($(newId)){
  125.             $(newId).remove();
  126.         }
  127.         //hide the select
  128.         //actually move it outside the visible area so the validation will still work
  129.         $(selectid).setStyle({
  130.             left:"-10000px",
  131.             position: "absolute"
  132.         });
  133.         //create a container
  134.         $(selectid).insert({after: '<div class="switcher-field" id="' + newId + '"></div>'});
  135.         //create a label for each element
  136.         $(selectid).childElements().each(function(elem, index){
  137.             //skip first element "Choose..."
  138.             if (index == 0){
  139.                 return;
  140.             }
  141.             var optval = $(elem).value;
  142.             if (optval != ''){
  143.                 var opttext = $(elem).innerHTML;
  144.                 if (typeof that.config.images[attributeId] != 'undefined'){
  145.                     for ( var j=0; j<that.config.attributes[attributeId].options.length;j++){
  146.                         if (that.config.attributes[attributeId].options[j].id != optval){
  147.                             continue;
  148.                         }
  149.                         var product = parseInt(that.config.attributes[attributeId].options[j].allowedProducts[0]);
  150.                         //replace label with image if available
  151.                         if (typeof that.config.images[attributeId][product] != 'undefined'){
  152.                             opttext = '<img src="' + that.config.images[attributeId][product] + '" alt="' + opttext + '" title="' + opttext + '" />';
  153.                         }
  154.                     }
  155.                 }
  156.                 var labelClass = '';
  157.                 if ($(elem).selected){
  158.                     labelClass = ' selected';
  159.                 }
  160.                 var inStock = that.isInStock(attributeId, optval);
  161.                 //check if the combination is in stock
  162.                 if (!inStock){
  163.                     labelClass += ' no-stock';
  164.                     if (that.config.allow_no_stock_select){
  165.                         labelClass += ' allow-select';
  166.                     }
  167.                 }
  168.                 $(newId).insert('<label class="switcher-label' + labelClass + '" id="' + $(selectid).id + '_' + optval + '" value="' + optval + '">'+opttext+'</label></div>')
  169.                 //change the select value on click
  170.                 if (inStock || that.config.allow_no_stock_select){
  171.                     Event.observe($($(selectid).id + '_' + optval), 'click', function() {
  172.                         that.selectValue(this, $(this).readAttribute('value'), selectid);
  173.                     });
  174.                 }
  175.                 // Make IE 7 & 8 behave like browsers - damn you IE
  176.                 if (index == $(selectid).childElements().length - 1){
  177.                     $(newId).insert('<div style="clear:both"></div>');
  178.                 }
  179.             }
  180.         })
  181.     },
  182.     /**
  183.      * select a value when clicking a label
  184.      * @param elem
  185.      * @param value
  186.      * @param selectid
  187.      * @author Marius Strajeru <marius.strajeru@gmail.com>
  188.      */
  189.     selectValue: function(elem, value, selectid){
  190.         if ($(elem)){
  191.             $(elem).up(1).select('label').each (function(el){
  192.                 $(el).removeClassName('selected');
  193.             });
  194.             $(elem).addClassName('selected');
  195.             this.simulateSelect(selectid, $(elem).readAttribute('value'));
  196.         }
  197.     },
  198.     /**
  199.      * simulate onchange event on select
  200.      * @param selectid
  201.      * @param value
  202.      * @author Marius Strajeru <marius.strajeru@gmail.com>
  203.      */
  204.     simulateSelect: function(selectid, value){
  205.         $(selectid).value = value;
  206.         if(document.createEvent) {
  207.             var evt = document.createEvent('HTMLEvents');
  208.             evt.initEvent('change',true,true);
  209.             $(selectid).dispatchEvent(evt);
  210.         }
  211.         else {
  212.             var evt = document.createEventObject();
  213.             $(selectid).fireEvent('onchange',evt)
  214.         }
  215.     },
  216.     /**
  217.      * check if a combination is in stock
  218.      * @param attributeId
  219.      * @param value
  220.      * @author Marius Strajeru <marius.strajeru@gmail.com>
  221.      */
  222.     isInStock: function(attributeId, value){
  223.         var that = this;
  224.         for ( var j=0; j<that.config.attributes[attributeId].options.length;j++){
  225.             if (that.config.attributes[attributeId].options[j].id != value){
  226.                 continue;
  227.             }
  228.             for (var i = 0; i<that.config.attributes[attributeId].options[j].allowedProducts.length;i++){
  229.                 var product = that.config.attributes[attributeId].options[j].allowedProducts[i];
  230.                 if (this.config.stock[product] == 1){
  231.                     return 1;
  232.                 }
  233.             }
  234.             return 0;
  235.         }
  236.         return 1;
  237.     },
  238.     /**
  239.      * rewrite condigureElement to change the main image or media block
  240.      * @param $super
  241.      * @param element
  242.      * @author Marius Strajeru <marius.strajeru@gmail.com>
  243.      */
  244.     configureElement: function($super, element){
  245.         //parent action
  246.         $super(element);
  247.         try{
  248.             var attributeId = $(element).id.replace(/[a-z]*/, '');
  249.             var value = $(element).value;
  250.             var options = this.config.attributes[attributeId].options;
  251.             //if we should switch something
  252.             if (this.config.switch_image_type != 0){
  253.                 for (var id in options){
  254.                     if (options.hasOwnProperty(id)){
  255.                         if (options[id].id == value){
  256.                             var product = options[id].allowedProducts[0];
  257.                             //if we should switch only the image
  258.                             if (parseInt(this.config.switch_image_type) == 1){
  259.                                 //check id an image is available
  260.                                 if (typeof this.config.switch_images[attributeId] != 'undefined'){
  261.                                     //...for the current product
  262.                                     if (typeof this.config.switch_images[attributeId][product] != 'undefined'){
  263.                                         //get the image selector set in system->configuration
  264.                                         var image = eval(this.config.main_image_selector);
  265.                                         //if the image exists
  266.                                         if (image){
  267.                                             //change the src
  268.                                             $(image).src = this.config.switch_images[attributeId][product];
  269.                                             //hack for default theme zoomer
  270.                                             //don't call the callback on the first page load
  271.                                             if (this.fullLoad){
  272.                                                 if (this.config.switch_image_callback){
  273.                                                     //callback - give it 0.1 seconds for the image src to be changed
  274.                                                     //a small flicker might occur
  275.                                                     //if you don't like it you can remove it at your own risk
  276.                                                     eval.delay('0.1', this.config.switch_image_callback)
  277.                                                 }
  278.                                             }
  279.                                         }
  280.                                     }
  281.                                 }
  282.                             }
  283.                             //if the media block should be changed
  284.                             if (parseInt(this.config.switch_image_type) == 2){
  285.                                 //check if there is a media to change
  286.                                 if (typeof this.config.switch_media[attributeId] != 'undefined'){
  287.                                     //...for the current product
  288.                                     if (typeof this.config.switch_media[attributeId][product] != 'undefined'){
  289.                                         //ge the media block selector from system->configuration
  290.                                         var media = eval(this.config.switch_media_selector);
  291.                                         if (media){
  292.                                             //hack for default theme zoom-er that doesn't work if called twice.
  293.                                             //if it's not the page load
  294.                                             if (this.fullLoad){
  295.                                                 $(media).update(this.config.switch_media[attributeId][product]);
  296.                                                 if (this.config.switch_media_callback){
  297.                                                     //give it 0.1 seconds to settle in
  298.                                                     //a small flicker might occur
  299.                                                     //if you don't like it you can remove it at your own risk
  300.                                                     eval.delay('0.1', this.config.switch_media_callback)
  301.                                                 }
  302.                                             }
  303.                                             else{
  304.                                                 //if on page load, just add the html
  305.                                                 $(media).innerHTML = this.config.switch_media[attributeId][product];
  306.                                             }
  307.                                         }
  308.                                     }
  309.                                 }
  310.                             }
  311.                         }
  312.                     }
  313.                 }
  314.             }
  315.         }
  316.         catch (e){
  317.             console.log(e);
  318.         }
  319.     },
  320.     /**
  321.      * rewrite configureForValues to avoid calling the switch callback on first load
  322.      * this may not be necessary if the default theme zoomer is not used
  323.      * @param $super
  324.      * @author Marius Strajeru <marius.strajeru@gmail.com>
  325.      */
  326.     configureForValues: function($super){
  327.         this.fullLoad = false;
  328.         $super();
  329.         this.fullLoad = true;
  330.     },
  331.     getOptionLabel : function($super, option, price) {
  332.         if (this.config.show_added_prices) {
  333.             return $super(option, price);
  334.         }
  335.         return option.label;
  336.     }
  337. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement