Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ProductViewModel(products){
- var self = this;
- self.ready = ko.observable(false);
- self.products = ko.observableArray(products).extend({
- paging: 5
- });
- ko.utils.arrayForEach(products, function(product){
- product.visible = ko.observable(true);
- product.ready = ko.observable(true);
- product.addToCart = function(child, b) {
- if (null === self.cart) {
- throw 'Cart is not defined';
- }
- this.ready(false);
- self.cart.addProduct(child, function(){
- setTimeout(function(){
- child.ready(true);
- }, 1000);
- });
- };
- if(product.type_id == 'simple'){
- product.qty = ko.observable(1);
- }
- if ('children' in product) {
- ko.utils.arrayForEach(product.children, function(child){
- child.ready = ko.observable(true);
- child.qty = ko.observable(1);
- })
- }
- if ('fitting_level' in product) {
- if(!jQuery.isArray(product.fitting_level)){
- product.fitting_level = [product.fitting_level]
- }
- }
- });
- self.attributes = ko.observableArray([]);
- self.filterByAttributesAvailable = ko.pureComputed(function(){
- return !!ko.utils.arrayFirst(self.attributes(), function(filter){
- return filter.values().length;
- });
- });
- var i = 0;
- var filters = {};
- var finder = function(searchValue, propertyValue){
- if (typeof propertyValue === 'string') {
- return -1 !== searchValue.indexOf(propertyValue)
- } else {
- if (jQuery.isPlainObject(propertyValue)) {
- propertyValue = Object.values(propertyValue)
- }
- if (jQuery.isArray(propertyValue)) {
- return !!searchValue.filter(function(n) {
- return propertyValue.indexOf(n) !== -1
- }).length;
- }
- }
- };
- var selectedFilters = {};
- var applyMultiselect = function(name, value) {
- if (!(name in selectedFilters)) {
- selectedFilters[name] = ko.observableArray([]);
- }
- var selected = selectedFilters[name];
- if (-1 === selected.indexOf(value)) {
- selected.push(value);
- } else {
- selected.remove(value);
- isFiltersStarted = false;
- }
- return selected();
- };
- var attrValue = function(name) {
- this.name = ko.observable(name);
- this.isActive = ko.observable(false);
- this.isDisabled = ko.observable(false);
- this.originalName = name;
- };
- ko.utils.arrayForEach(window.filterableAttributes(), function(attribute){
- var isAttrExists = false;
- var attributeValues = ko.observableArray([]);
- var attr = attribute.code;
- ko.utils.arrayForEach(products, function(product){
- if (!product.hasOwnProperty(attr)) {
- return;
- }
- isAttrExists = true;
- var attributeValue = product[attr];
- if (jQuery.isPlainObject(attributeValue)) {
- attributeValue = Object.values(attributeValue);
- }
- if (jQuery.isArray(attributeValue)) {
- ko.utils.arrayForEach(attributeValue, function(value){
- if (!attributeValues.findBy('name', value) && value != '' && value != null) {
- attributeValues.push(new attrValue(value));
- }
- });
- } else if (!attributeValues.findBy('name', attributeValue) && attributeValue != null && attributeValue != '') {
- attributeValues.push(new attrValue(attributeValue));
- }
- if ((attributeValues().length == 1) && (attributeValues()[0].name() == '-1') && (attributeValues()[0].name() === null)) {
- attributeValues.removeAll();
- }
- });
- if (!isAttrExists) {
- return;
- }
- var filterAttribute = {
- name: attr,
- label: attribute.label,
- values: attributeValues,
- visible: ko.observable(i++ == 0),
- isBlured: ko.observable(false),
- isAttributesDisabled: ko.observable(false),
- toggleVisible: function() {
- if (filterAttribute.isAttributesDisabled()) {
- return ;
- }
- ko.utils.arrayForEach(self.attributes(), function(attributeObject){
- attributeObject.visible(false)
- });
- this.visible(true);
- },
- filter: function(value) {
- if (!value.isDisabled()) {
- self.filters.set(attr, applyMultiselect(attr, ko.unwrap(value.name))).filter();
- }
- }
- };
- //console.log('attr', attr);
- self.attributes.push(filterAttribute);
- filters[attr] = finder;
- });
- self.filters = new Filters(filters);
- self.filters.setCollection(products);
- self.nothingFound = ko.observable(false);
- self.filteredCount = ko.observable(products.length);
- var isFiltersStarted = false;
- jQuery(self.filters).on('filter:done', function(e, data) {
- self.nothingFound(data.isEmptySearch());
- self.filteredCount(data.getFilteredItems().length);
- var filtersToBlur = [];
- if(isFiltersStarted === false){
- /** @var data FilterEvent */
- isFiltersStarted = true;
- }
- var activeFilters = data.getActiveFilters();
- jQuery.each(data.getFilterValues(), function(key, value){
- if (!(key in activeFilters)){
- filtersToBlur.push(key);
- }
- });
- var products = data.getFilteredItems();
- jQuery.each(filtersToBlur, function(index, filterName){
- var attribute = ko.utils.arrayFirst(self.attributes(), function(item){
- return item.name === filterName;
- });
- var attributesOfVisibleProducts = ko.observableArray([]);
- var isAttributeVisible = false;
- jQuery.each(products, function(productIndex, product){
- //collect attributes info from products
- var attributeValues = attribute.values,
- productAttributeValues = product[filterName];
- try {
- if(null != productAttributeValues && productAttributeValues != ''){
- //console.log('productAttributeValues', productAttributeValues, '====>', productAttributeValues.length);
- if (typeof productAttributeValues === 'object') {
- jQuery.each(productAttributeValues, function (k, v) {
- if (!attributesOfVisibleProducts.findBy('name', v)) {
- attributesOfVisibleProducts.push(new attrValue(v));
- }
- });
- } else {
- if (!attributesOfVisibleProducts.findBy('name', productAttributeValues)) {
- attributesOfVisibleProducts.push(new attrValue(productAttributeValues));
- }
- }
- var intersect = !productAttributeValues ? false : attributeValues().filter(function (i) {
- return -1 !== productAttributeValues.indexOf(i.name());
- });
- isAttributeVisible = isAttributeVisible || (!intersect);
- }
- } catch (e) {
- isAttributeVisible = true;
- }
- });
- attribute.isBlured(!isAttributeVisible);
- var allDisabled = false;
- jQuery.each(attribute.values(), function (key, attributeValue) {
- var isAttrEnabled = attributesOfVisibleProducts.findBy('name', attributeValue.name());
- attributeValue.isDisabled(!isAttrEnabled);
- allDisabled = isAttrEnabled || allDisabled;
- });
- attribute.isAttributesDisabled(!allDisabled);
- });
- });
- self.resetFilters = function() {
- self.filters.resetFilters().filter();
- selectedFilters = {};
- isFiltersStarted = false;
- jQuery('.filter-block__items a').removeClass('disabled');
- };
- self.ready(true);
- self.cart = null;
- self.setCart = function(cart) {
- self.cart = cart;
- };
- /*$.ajax('/url').then(function(response){
- ko.utils.arrayForEach(function(product){
- self.products.push(product);
- })
- })*/
- }
- ko.extenders.paging = function(target, pageSize) {
- var _pageSize = ko.observable(pageSize || 10),
- // default pageSize to 10
- _currentPage = ko.observable(1); // default current page to 1
- target.pageSize = ko.computed({
- read: _pageSize,
- write: function(newValue) {
- if (newValue > 0) {
- _pageSize(newValue);
- }
- else {
- _pageSize(10);
- }
- }
- });
- target.currentPage = ko.computed({
- read: _currentPage,
- write: function(newValue) {
- if (newValue > target.pageCount()) {
- _currentPage(target.pageCount());
- }
- else if (newValue <= 0) {
- _currentPage(1);
- }
- else {
- _currentPage(newValue);
- }
- }
- });
- target.pageCount = ko.computed(function() {
- return Math.ceil(target().length / target.pageSize()) || 1;
- });
- target.currentPageData = ko.computed(function() {
- var pageSize = _pageSize(),
- pageIndex = _currentPage(),
- startIndex = pageSize * (pageIndex - 1),
- endIndex = pageSize * pageIndex;
- return target().slice(startIndex, endIndex);
- });
- target.moveFirst = function() {
- target.currentPage(1);
- };
- target.movePrevious = function() {
- target.currentPage(target.currentPage() - 1);
- };
- target.moveNext = function() {
- target.currentPage(target.currentPage() + 1);
- };
- target.moveLast = function() {
- target.currentPage(target.pageCount());
- };
- return target;
- };
- jQuery(function($){
- $(document).delegate('a.filter-button.enabled', 'click', function() {
- $(this).toggleClass('active');
- });
- $(document).delegate('a.filter-button.disabled', 'click', function() {
- $(this).removeClass('active');
- });
- $(document).on( 'click', '.filter-btn', function(){
- $(this).toggleClass('open');
- $('.filter-block-wr').toggle();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement