// SNIPPET
{% comment %}
Collection template, used on current_collection.liquid and current_collection.image.liquid
{% endcomment %}
<div class="compare-basket">
<button class="action action--button action--compare"><i class="fa fa-check"></i><span class="action__text">Compare</span></button>
</div>
{% assign current_collection = collections['model-1'] %}
<div class="view">
<header class="section-header text-center">
<h1>{{ current_collection.title }}</h1>
<!-- <hr class="hr--small"> -->
{% if current_collection.description != blank %}
<div class="grid">
<div class="grid__item">
<div class="rte">
{{ current_collection.description }}
</div>
</div>
</div>
{% endif %}
<div class="grid--full collection-sorting{% if section.settings.collection_sort_enable %} collection-sorting--enabled{% endif %}">
{% if section.settings.collection_sort_enable %}
{% include 'collection-sorting' %}
{% endif %}
</div>
{% if section.settings.collection_tags_enable %}
{% if current_collection.all_tags.size > 0 %}
{% if section.settings.collection_sort_enable %}
<hr class="hr--small">
{% endif %}
{% comment %}
To provide a 'catch-all' link at the top of the list,
check against the current_collection.handle, product type, and vendor.
{% endcomment %}
<ul class="tags tags--collection inline-list">
<li{% unless current_tags %} class="tag--active"{% endunless %}>
{% comment %}
Good for /collections/all collection and regular collections
{% endcomment %}
{% if current_collection.handle %}
<a href="/collections/{{ current_collection.handle }}">
{{ 'collections.general.all_of_collection' | t }}
</a>
{% comment %}
Good for automatic type collections
{% endcomment %}
{% elsif current_collection.current_type %}
<a href="{{ current_collection.current_type | url_for_type }}">
{{ 'collections.general.all_of_collection' | t }}
</a>
{% comment %}
Good for automatic vendor collections
{% endcomment %}
{% elsif current_collection.current_vendor %}
<a href="{{ current_collection.current_vendor | url_for_vendor }}">
{{ 'collections.general.all_of_collection' | t }}
</a>
{% endif %}
</li>
{% for tag in current_collection.all_tags %}
{% if current_tags contains tag %}
<li class="tag--active">
{{ tag | link_to_remove_tag: tag }}
</li>
{% else %}
<li>
{% comment %}
Use link_to_add_tag if you want to allow filtering
by multiple tags
{% endcomment %}
{{ tag | link_to_tag: tag }}
</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
{% endif %}
{% if section.settings.collection_tags_enable %}
<hr class="hr--small hr--clear">
{% endif %}
</header>
{% if section.settings.collection_products_grid == 'collage' %}
<div class="grid grid-collage product-card-grid">
{% comment %}
Loop through our products in the current current_collection.
See the snippet 'snippets/product-grid-collage.liquid'.
`is_reverse_row__product`, `three_row_index__product`, `collection_product_count`, and `divisible_by_three__product` are
all used by 'snippets/product-grid-collage.liquid'
{% endcomment %}
{% assign is_reverse_row__product = false %}
{% assign three_row_index__product = 0 %}
{% assign collection_product_count__product = current_collection.products.size %}
{% assign divisible_by_three__product = collection_product_count__product | modulo:3 %}
{% for product in current_collection.products %}
{% comment %} {% include 'product-grid-collage' %} {% endcomment %}
{% include 'product-grid-card' %}
{% else %}
{% if shop.products_count == 0 %}
{% comment %}
Add default products to help with onboarding for collections/all only
{% endcomment %}
{% include 'onboarding-featured-products' %}
{% else %}
{% comment %}
If collection exists but is empty, display message
{% endcomment %}
<div class="grid__item text-center">
<p>{{ 'collections.general.no_matches' | t }}</p>
</div>
{% endif %}
{% endfor %}
</div>
{% elsif section.settings.collection_products_grid == 'grid' %}
<div class="grid-uniform">
{% assign grid_item_width = 'medium--one-half large--one-third' %}
{% for product in current_collection.products %}
{% include 'product-grid-card' %}
{% else %}
{% if current_collection.handle == 'all' %}
{% comment %}
Add default products to help with onboarding for collections/all only
{% endcomment %}
{% include 'onboarding-featured-products' %}
{% else %}
{% comment %}
If collection exists but is empty, display message
{% endcomment %}
<div class="grid__item text-center">
<p>{{ 'collections.general.no_matches' | t }}</p>
</div>
{% endif %}
{% endfor %}
</div>
{% endif %}
{% if paginate.pages > 1 %}
{% include 'pagination' %}
{% endif %}
</div>
<section class="compare">
<button class="action action--close"><img src="{{ 'close-white.svg' | asset_url }} " alt='Close Comparison'/><span class="action__text action__text--invisible">Close comparison overlay</span></button>
</section>
<script src="{{ 'classie.js' | asset_url }} "></script>
<script>
(function() {
const viewEl = document.querySelector('.view')
const items = [].slice.call(document.querySelectorAll('.product-card'))
let basket;
// the compare basket
function CompareBasket() {
this.el = document.querySelector('.compare-basket');
this.compareCtrl = this.el.querySelector('.action--compare');
this.compareWrapper = document.querySelector('.compare'),
this.closeCompareCtrl = this.compareWrapper.querySelector('.action--close')
this.itemsAllowed = 3;
this.totalItems = 0;
this.items = [];
// compares items in the compare basket: opens the compare products wrapper
this.compareCtrl.addEventListener('click', this._compareItems.bind(this));
// close the compare products wrapper
var self = this;
this.closeCompareCtrl.addEventListener('click', function() {
// toggle compare basket
classie.add(self.el, 'compare-basket--active');
// animate..
classie.remove(viewEl, 'view--compare');
});
}
CompareBasket.prototype.add = function(item) {
// check limit
if( this.isFull() ) {
return false;
}
classie.add(item, 'product--selected');
// create item preview element
var preview = this._createItemPreview(item);
// prepend it to the basket
this.el.insertBefore(preview, this.el.childNodes[0]);
// insert item
this.items.push(preview);
this.totalItems++;
if( this.isFull() ) {
classie.add(this.el, 'compare-basket--full');
}
classie.add(this.el, 'compare-basket--active');
};
CompareBasket.prototype._createItemPreview = function(item) {
const self = this;
const removeComparisonIcon = document.createElement('img')
const preview = document.createElement('div')
preview.className = 'product-icon';
preview.setAttribute('data-idx', items.indexOf(item));
const removeCtrl = document.createElement('button');
removeCtrl.className = 'action action--remove';
removeCtrl.style.border = 'none'
removeComparisonIcon.src = "{{ 'close.svg' | asset_url }} "
removeCtrl.appendChild(removeComparisonIcon)
removeCtrl.addEventListener('click', function() {
self.remove(item);
});
console.log('item', item)
const productImageEl = item.querySelector('.img img').cloneNode(true);
preview.appendChild(productImageEl);
preview.appendChild(removeCtrl);
const productHandle = item.getAttribute('data-attr-handle');
const productTitle = item.getAttribute('data-attr-title');
const productFeaturedImage = item.getAttribute('data-attr-featured-image');
const productID = item.getAttribute('data-attr-productId');
const productDescription = item.getAttribute('data-attr-product-description');
preview.setAttribute('data-attr-handle', productHandle);
preview.setAttribute('data-attr-title', productTitle);
preview.setAttribute('data-attr-featured-image', productFeaturedImage);
preview.setAttribute('data-attr-productId', productID);
preview.setAttribute('data-attr-product-description', productDescription);
return preview;
};
CompareBasket.prototype.remove = function(item) {
classie.remove(this.el, 'compare-basket--full');
classie.remove(item, 'product--selected');
var preview = this.el.querySelector('[data-idx = "' + items.indexOf(item) + '"]');
this.el.removeChild(preview);
this.totalItems--;
var indexRemove = this.items.indexOf(preview);
this.items.splice(indexRemove, 1);
if( this.totalItems === 0 ) {
classie.remove(this.el, 'compare-basket--active');
}
// checkbox
var checkbox = item.querySelector('.action--compare-add > input[type = "checkbox"]');
if( checkbox.checked ) {
checkbox.checked = false;
}
};
CompareBasket.prototype._compareItems = function() {
var self = this;
// remove all previous items inside the compareWrapper element
[].slice.call(this.compareWrapper.querySelectorAll('div.compare__item')).forEach(function(item) {
self.compareWrapper.removeChild(item);
});
for(var i = 0; i < this.totalItems; ++i) {
const compareItemWrapper = document.createElement('div');
compareItemWrapper.className = 'compare__item';
const theProductID = this.items[i].getAttribute('data-attr-productId');
const compareItemEffectEl = document.createElement('div');
const compareItemTitle = document.createElement('h1');
const compareItemImage = document.createElement('img');
const compareItemDescription = document.createElement('p');
compareItemEffectEl.className = 'compare__effect';
compareItemTitle.innerHTML = this.items[i].getAttribute('data-attr-title');
compareItemDescription.innerHTML = this.items[i].getAttribute('data-attr-product-description');
compareItemImage.src = this.items[i].getAttribute('data-attr-featured-image');
compareItemEffectEl.appendChild(compareItemTitle)
compareItemEffectEl.appendChild(compareItemImage)
compareItemEffectEl.appendChild(compareItemDescription)
console.log(compareItemDescription)
console.log(this.items[i])
compareItemWrapper.appendChild(compareItemEffectEl);
this.compareWrapper.insertBefore(compareItemWrapper, this.compareWrapper.childNodes[0]);
}
setTimeout(function() {
// toggle compare basket
classie.remove(self.el, 'compare-basket--active');
// animate..
classie.add(viewEl, 'view--compare');
}, 25);
};
CompareBasket.prototype.isFull = function() {
return this.totalItems === this.itemsAllowed;
};
function init() {
// initialize an empty basket
basket = new CompareBasket();
initEvents();
}
function initEvents() {
items.forEach(function(item) {
var checkbox = item.querySelector('.action--compare-add > input[type = "checkbox"]');
checkbox.checked = false;
// ctrl to add to the "compare basket"
checkbox.addEventListener('click', function(ev) {
const plusSign = ev.target.parentElement.querySelector('.plus-sign')
plusSign.src = plusSign.src === "{{ 'check.svg' | asset_url }}" ? "{{ 'plus-rectangle.svg' | asset_url }}" : "{{ 'check.svg' | asset_url }}"
if( ev.target.checked ) {
if( basket.isFull() ) {
ev.preventDefault();
return false;
}
basket.add(item);
}
else {
basket.remove(item);
}
});
});
}
init();
})();
</script>