Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ProductViewModel(products){
  2.     var self = this;
  3.     self.ready = ko.observable(false);
  4.  
  5.     self.products = ko.observableArray(products).extend({
  6.         paging: 5
  7.     });
  8.  
  9.     ko.utils.arrayForEach(products, function(product){
  10.         product.visible = ko.observable(true);
  11.         product.ready = ko.observable(true);
  12.         product.addToCart = function(child, b) {
  13.             if (null === self.cart) {
  14.                 throw 'Cart is not defined';
  15.             }
  16.             this.ready(false);
  17.             self.cart.addProduct(child, function(){
  18.                 setTimeout(function(){
  19.                     child.ready(true);
  20.                 }, 1000);
  21.             });
  22.         };
  23.  
  24.         if(product.type_id == 'simple'){
  25.             product.qty = ko.observable(1);
  26.         }
  27.  
  28.         if ('children' in product) {
  29.             ko.utils.arrayForEach(product.children, function(child){
  30.                 child.ready = ko.observable(true);
  31.                 child.qty = ko.observable(1);
  32.             })
  33.         }
  34.  
  35.         if ('fitting_level' in product) {
  36.             if(!jQuery.isArray(product.fitting_level)){
  37.                 product.fitting_level = [product.fitting_level]
  38.             }
  39.         }
  40.     });
  41.  
  42.     self.attributes = ko.observableArray([]);
  43.     self.filterByAttributesAvailable = ko.pureComputed(function(){
  44.         return !!ko.utils.arrayFirst(self.attributes(), function(filter){
  45.             return filter.values().length;
  46.         });
  47.     });
  48.  
  49.     var i = 0;
  50.     var filters = {};
  51.     var finder = function(searchValue, propertyValue){
  52.         if (typeof propertyValue === 'string') {
  53.             return -1 !== searchValue.indexOf(propertyValue)
  54.         } else {
  55.             if (jQuery.isPlainObject(propertyValue)) {
  56.                 propertyValue = Object.values(propertyValue)
  57.             }
  58.  
  59.             if (jQuery.isArray(propertyValue)) {
  60.                 return !!searchValue.filter(function(n) {
  61.                     return propertyValue.indexOf(n) !== -1
  62.                 }).length;
  63.             }
  64.         }
  65.     };
  66.  
  67.     var selectedFilters = {};
  68.     var applyMultiselect = function(name, value) {
  69.         if (!(name in selectedFilters)) {
  70.             selectedFilters[name] = ko.observableArray([]);
  71.         }
  72.  
  73.         var selected = selectedFilters[name];
  74.         if (-1 === selected.indexOf(value)) {
  75.             selected.push(value);
  76.         } else {
  77.             selected.remove(value);
  78.             isFiltersStarted = false;
  79.         }
  80.         return selected();
  81.     };
  82.  
  83.     var attrValue = function(name) {
  84.         this.name = ko.observable(name);
  85.         this.isActive = ko.observable(false);
  86.         this.isDisabled = ko.observable(false);
  87.         this.originalName = name;
  88.     };
  89.  
  90.     ko.utils.arrayForEach(window.filterableAttributes(), function(attribute){
  91.         var isAttrExists = false;
  92.         var attributeValues = ko.observableArray([]);
  93.         var attr = attribute.code;
  94.         ko.utils.arrayForEach(products, function(product){
  95.             if (!product.hasOwnProperty(attr)) {
  96.                 return;
  97.             }
  98.             isAttrExists = true;
  99.             var attributeValue = product[attr];
  100.  
  101.             if (jQuery.isPlainObject(attributeValue)) {
  102.                 attributeValue = Object.values(attributeValue);
  103.             }
  104.             if (jQuery.isArray(attributeValue)) {
  105.                 ko.utils.arrayForEach(attributeValue, function(value){
  106.                     if (!attributeValues.findBy('name', value) && value != '' && value != null) {
  107.                         attributeValues.push(new attrValue(value));
  108.                     }
  109.                 });
  110.             } else if (!attributeValues.findBy('name', attributeValue) && attributeValue != null && attributeValue != '') {
  111.                 attributeValues.push(new attrValue(attributeValue));
  112.             }
  113.  
  114.             if ((attributeValues().length == 1) && (attributeValues()[0].name() == '-1') && (attributeValues()[0].name() === null)) {
  115.                 attributeValues.removeAll();
  116.             }
  117.         });
  118.  
  119.         if (!isAttrExists) {
  120.             return;
  121.         }
  122.  
  123.         var filterAttribute = {
  124.             name: attr,
  125.             label: attribute.label,
  126.             values: attributeValues,
  127.             visible: ko.observable(i++ == 0),
  128.             isBlured: ko.observable(false),
  129.             isAttributesDisabled: ko.observable(false),
  130.             toggleVisible: function() {
  131.                 if (filterAttribute.isAttributesDisabled()) {
  132.                     return ;
  133.                 }
  134.                 ko.utils.arrayForEach(self.attributes(), function(attributeObject){
  135.                     attributeObject.visible(false)
  136.                 });
  137.                 this.visible(true);
  138.             },
  139.             filter: function(value) {
  140.                 if (!value.isDisabled()) {
  141.                     self.filters.set(attr, applyMultiselect(attr, ko.unwrap(value.name))).filter();
  142.                 }
  143.             }
  144.         };
  145.  
  146.         //console.log('attr', attr);
  147.         self.attributes.push(filterAttribute);
  148.         filters[attr] = finder;
  149.  
  150.     });
  151.  
  152.     self.filters = new Filters(filters);
  153.     self.filters.setCollection(products);
  154.  
  155.     self.nothingFound = ko.observable(false);
  156.     self.filteredCount = ko.observable(products.length);
  157.  
  158.     var isFiltersStarted = false;
  159.  
  160.     jQuery(self.filters).on('filter:done', function(e, data) {
  161.         self.nothingFound(data.isEmptySearch());
  162.         self.filteredCount(data.getFilteredItems().length);
  163.         var filtersToBlur = [];
  164.  
  165.         if(isFiltersStarted === false){
  166.             /** @var data FilterEvent */
  167.             isFiltersStarted = true;
  168.         }
  169.  
  170.         var activeFilters = data.getActiveFilters();
  171.  
  172.         jQuery.each(data.getFilterValues(), function(key, value){
  173.             if (!(key in activeFilters)){
  174.                 filtersToBlur.push(key);
  175.             }
  176.         });
  177.  
  178.  
  179.         var products = data.getFilteredItems();
  180.         jQuery.each(filtersToBlur, function(index, filterName){
  181.             var attribute = ko.utils.arrayFirst(self.attributes(), function(item){
  182.                 return item.name === filterName;
  183.             });
  184.  
  185.             var attributesOfVisibleProducts = ko.observableArray([]);
  186.             var isAttributeVisible = false;
  187.             jQuery.each(products, function(productIndex, product){
  188.                 //collect attributes info from products
  189.                 var attributeValues = attribute.values,
  190.                     productAttributeValues = product[filterName];
  191.  
  192.                 try {
  193.                     if(null != productAttributeValues && productAttributeValues != ''){
  194.                         //console.log('productAttributeValues', productAttributeValues, '====>', productAttributeValues.length);
  195.  
  196.                         if (typeof productAttributeValues === 'object') {
  197.                             jQuery.each(productAttributeValues, function (k, v) {
  198.                                 if (!attributesOfVisibleProducts.findBy('name', v)) {
  199.                                     attributesOfVisibleProducts.push(new attrValue(v));
  200.                                 }
  201.                             });
  202.                         } else {
  203.                             if (!attributesOfVisibleProducts.findBy('name', productAttributeValues)) {
  204.                                 attributesOfVisibleProducts.push(new attrValue(productAttributeValues));
  205.                             }
  206.                         }
  207.  
  208.                         var intersect = !productAttributeValues ? false : attributeValues().filter(function (i) {
  209.                             return -1 !== productAttributeValues.indexOf(i.name());
  210.                         });
  211.                         isAttributeVisible = isAttributeVisible || (!intersect);
  212.                     }
  213.                 } catch (e) {
  214.                     isAttributeVisible = true;
  215.                 }
  216.  
  217.             });
  218.             attribute.isBlured(!isAttributeVisible);
  219.  
  220.  
  221.             var allDisabled = false;
  222.             jQuery.each(attribute.values(), function (key, attributeValue) {
  223.                 var isAttrEnabled = attributesOfVisibleProducts.findBy('name', attributeValue.name());
  224.  
  225.                 attributeValue.isDisabled(!isAttrEnabled);
  226.                 allDisabled = isAttrEnabled || allDisabled;
  227.             });
  228.             attribute.isAttributesDisabled(!allDisabled);
  229.         });
  230.  
  231.     });
  232.  
  233.     self.resetFilters = function() {
  234.         self.filters.resetFilters().filter();
  235.         selectedFilters = {};
  236.         isFiltersStarted = false;
  237.         jQuery('.filter-block__items a').removeClass('disabled');
  238.     };
  239.  
  240.     self.ready(true);
  241.  
  242.     self.cart = null;
  243.     self.setCart = function(cart) {
  244.         self.cart = cart;
  245.     };
  246.  
  247.     /*$.ajax('/url').then(function(response){
  248.         ko.utils.arrayForEach(function(product){
  249.         self.products.push(product);
  250.      })
  251.      })*/
  252.  
  253. }
  254.  
  255. ko.extenders.paging = function(target, pageSize) {
  256.     var _pageSize = ko.observable(pageSize || 10),
  257.     // default pageSize to 10
  258.         _currentPage = ko.observable(1); // default current page to 1
  259.  
  260.     target.pageSize = ko.computed({
  261.         read: _pageSize,
  262.         write: function(newValue) {
  263.             if (newValue > 0) {
  264.                 _pageSize(newValue);
  265.             }
  266.             else {
  267.                 _pageSize(10);
  268.             }
  269.         }
  270.     });
  271.  
  272.     target.currentPage = ko.computed({
  273.         read: _currentPage,
  274.         write: function(newValue) {
  275.             if (newValue > target.pageCount()) {
  276.                 _currentPage(target.pageCount());
  277.             }
  278.             else if (newValue <= 0) {
  279.                 _currentPage(1);
  280.             }
  281.             else {
  282.                 _currentPage(newValue);
  283.             }
  284.         }
  285.     });
  286.  
  287.     target.pageCount = ko.computed(function() {
  288.         return Math.ceil(target().length / target.pageSize()) || 1;
  289.     });
  290.  
  291.     target.currentPageData = ko.computed(function() {
  292.         var pageSize = _pageSize(),
  293.             pageIndex = _currentPage(),
  294.             startIndex = pageSize * (pageIndex - 1),
  295.             endIndex = pageSize * pageIndex;
  296.  
  297.         return target().slice(startIndex, endIndex);
  298.     });
  299.  
  300.     target.moveFirst = function() {
  301.         target.currentPage(1);
  302.     };
  303.     target.movePrevious = function() {
  304.         target.currentPage(target.currentPage() - 1);
  305.     };
  306.     target.moveNext = function() {
  307.         target.currentPage(target.currentPage() + 1);
  308.     };
  309.     target.moveLast = function() {
  310.         target.currentPage(target.pageCount());
  311.     };
  312.  
  313.     return target;
  314. };
  315.  
  316.  
  317.  
  318. jQuery(function($){
  319.     $(document).delegate('a.filter-button.enabled', 'click',  function() {
  320.         $(this).toggleClass('active');
  321.     });
  322.     $(document).delegate('a.filter-button.disabled', 'click',  function() {
  323.         $(this).removeClass('active');
  324.     });
  325.     $(document).on( 'click', '.filter-btn', function(){
  326.         $(this).toggleClass('open');
  327.         $('.filter-block-wr').toggle();
  328.     });
  329. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement