Skip to content

Commit

Permalink
Hide variant images setting (Shopify#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludoboludo authored Aug 19, 2021
1 parent d3b44e7 commit ce0e5ec
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 14 deletions.
21 changes: 15 additions & 6 deletions assets/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,17 @@ class SliderComponent extends HTMLElement {
}

initPages() {
if (!this.sliderItems.length === 0) return;
this.slidesPerPage = Math.floor(this.slider.clientWidth / this.sliderItems[0].clientWidth);
this.totalPages = this.sliderItems.length - this.slidesPerPage + 1;
const sliderItemsToShow = Array.from(this.sliderItems).filter(element => element.clientWidth > 0);
this.sliderLastItem = sliderItemsToShow[sliderItemsToShow.length - 1];
if (sliderItemsToShow.length === 0) return;
this.slidesPerPage = Math.floor(this.slider.clientWidth / sliderItemsToShow[0].clientWidth);
this.totalPages = sliderItemsToShow.length - this.slidesPerPage + 1;
this.update();
}

update() {
if (!this.pageCount || !this.pageTotal) return;
this.currentPage = Math.round(this.slider.scrollLeft / this.sliderItems[0].clientWidth) + 1;
this.currentPage = Math.round(this.slider.scrollLeft / this.sliderLastItem.clientWidth) + 1;

if (this.currentPage === 1) {
this.prevButton.setAttribute('disabled', true);
Expand All @@ -484,7 +486,7 @@ class SliderComponent extends HTMLElement {

onButtonClick(event) {
event.preventDefault();
const slideScrollPosition = event.currentTarget.name === 'next' ? this.slider.scrollLeft + this.sliderItems[0].clientWidth : this.slider.scrollLeft - this.sliderItems[0].clientWidth;
const slideScrollPosition = event.currentTarget.name === 'next' ? this.slider.scrollLeft + this.sliderLastItem.clientWidth : this.slider.scrollLeft - this.sliderLastItem.clientWidth;
this.slider.scrollTo({
left: slideScrollPosition
});
Expand Down Expand Up @@ -533,10 +535,17 @@ class VariantSelects extends HTMLElement {
const newMedia = document.querySelector(
`[data-media-id="${this.dataset.section}-${this.currentVariant.featured_media.id}"]`
);

if (!newMedia) return;
const modalContent = document.querySelector(`#ProductModal-${this.dataset.section} .product-media-modal__content`);
const newMediaModal = modalContent.querySelector( `[data-media-id="${this.currentVariant.featured_media.id}"]`);
const parent = newMedia.parentElement;
if (parent.firstChild == newMedia) return;
modalContent.prepend(newMediaModal);
parent.prepend(newMedia);
window.setTimeout(() => { parent.scroll(0, 0) });
this.stickyHeader = this.stickyHeader || document.querySelector('sticky-header');
this.stickyHeader.dispatchEvent(new Event('preventHeaderReveal'));
window.setTimeout(() => { parent.querySelector('li.product__media-item').scrollIntoView({behavior: "smooth"}); });
}

updateURL() {
Expand Down
10 changes: 10 additions & 0 deletions assets/section-main-product.css
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,16 @@ a.product__text {
}
}

.product__media-item--variant,
.product-media-modal__content > .product__media-item--variant.product__media-item--variant {
display: none;
}

.product__media-item--variant:first-child,
.product-media-modal__content > .product__media-item--variant:first-child {
display: block;
}

.product__media-icon .icon {
width: 1.2rem;
height: 1.4rem;
Expand Down
3 changes: 3 additions & 0 deletions locales/da.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,9 @@
},
"enable_sticky_info": {
"label": "Aktivér fastgjorte produktoplysninger på store skærme"
},
"hide_variants": {
"label": "Skjul andre varianters medier, når du har valgt en variant"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions locales/de.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,9 @@
},
"enable_sticky_info": {
"label": "Fixierte Produktinformationen auf großen Bildschirmen aktivieren"
},
"hide_variants": {
"label": "Medien anderer Varianten ausblenden, sobald eine Variante ausgewählt wurde"
}
},
"name": "Produktinformationen"
Expand Down
3 changes: 3 additions & 0 deletions locales/en.default.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,9 @@
"enable_sticky_info": {
"label": "Enable sticky product information on large screens"
},
"hide_variants": {
"label": "Hide other variants’ media after selecting a variant"
},
"enable_video_looping": {
"label": "Enable video looping"
}
Expand Down
3 changes: 3 additions & 0 deletions locales/fi.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,9 @@
},
"enable_sticky_info": {
"label": "Ota käyttöön kiinteät tuotetiedot suurilla näytöillä"
},
"hide_variants": {
"label": "Piilota muiden versioiden aineisto, kun versio on valittu"
}
},
"name": "Tuotetiedot"
Expand Down
3 changes: 3 additions & 0 deletions locales/fr.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,9 @@
},
"enable_sticky_info": {
"label": "Activer les informations produits collées sur de grands écrans"
},
"hide_variants": {
"label": "Masquer les supports multimédias des autres variantes après la sélection d'une variante"
}
},
"name": "Informations produits"
Expand Down
3 changes: 3 additions & 0 deletions locales/it.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,9 @@
},
"enable_sticky_info": {
"label": "Abilita le informazioni di prodotto fisse su schermi grandi"
},
"hide_variants": {
"label": "Nascondi i contenuti multimediali delle altre varianti dopo averne selezionata una"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions locales/ja.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,9 @@
},
"enable_sticky_info": {
"label": "大画面で商品情報のスティッキー表示を有効にする"
},
"hide_variants": {
"label": "バリエーションを選択した後、他のバリエーションのメディアを非表示にする"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions locales/ko.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,9 @@
},
"enable_sticky_info": {
"label": "큰 화면에 제품 정보 스티커 사용"
},
"hide_variants": {
"label": "이형 상품을 선택한 후 다른 이형 상품의 미디어 숨기기"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions locales/pl.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,9 @@
},
"enable_sticky_info": {
"label": "Włącz przypiętą informację o produkcie na dużych ekranach"
},
"hide_variants": {
"label": "Ukryj multimedia innych wariantów po wybraniu wariantu"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions locales/th.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,9 @@
},
"enable_sticky_info": {
"label": "เปิดใช้ข้อมูลสินค้าแบบยึดตำแหน่งบนหน้าจอใหญ่"
},
"hide_variants": {
"label": "ซ่อนสื่อของตัวเลือกสินค้าอื่นๆ หลังจากเลือกตัวเลือกสินค้า"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions locales/zh-CN.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,9 @@
},
"enable_sticky_info": {
"label": "在较大屏幕上启用粘性产品信息"
},
"hide_variants": {
"label": "在选择多属性后隐藏其他多属性的媒体文件"
}
}
},
Expand Down
24 changes: 19 additions & 5 deletions sections/header.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,19 @@
this.header = document.getElementById('shopify-section-header');
this.headerBounds = {};
this.currentScrollTop = 0;
this.preventReveal = false;

this.onScrollHandler = this.onScroll.bind(this);
this.hideHeaderOnScrollUp = () => this.preventReveal = true;

this.addEventListener('preventHeaderReveal', this.hideHeaderOnScrollUp);
window.addEventListener('scroll', this.onScrollHandler, false);

this.createObserver();
}

disconnectedCallback() {
this.removeEventListener('preventHeaderReveal', this.hideHeaderOnScrollUp);
window.removeEventListener('scroll', this.onScrollHandler);
}

Expand All @@ -461,12 +465,22 @@
onScroll() {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;

if (scrollTop > this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
if (this.preventReveal) {
window.clearTimeout(this.isScrolling);

this.isScrolling = setTimeout(() => {
this.preventReveal = false;
}, 66);

requestAnimationFrame(this.hide.bind(this));
} else if (scrollTop < this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
requestAnimationFrame(this.reveal.bind(this));
} else if (scrollTop <= this.headerBounds.top) {
requestAnimationFrame(this.reset.bind(this));
} else {
if (scrollTop > this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
requestAnimationFrame(this.hide.bind(this));
} else if (scrollTop < this.currentScrollTop && scrollTop > this.headerBounds.bottom) {
requestAnimationFrame(this.reveal.bind(this));
} else if (scrollTop <= this.headerBounds.top) {
requestAnimationFrame(this.reset.bind(this));
}
}

this.currentScrollTop = scrollTop;
Expand Down
14 changes: 11 additions & 3 deletions sections/main-product.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
{{ "accessibility.skip_to_product_info" | t }}
</a>
<ul class="product__media-list grid grid--peek list-unstyled slider slider--mobile" role="list">
{%- assign variant_images = product.images | where: 'attached_to_variant?', true | map: 'src' -%}
{%- if product.selected_or_first_available_variant.featured_media != null -%}
{%- assign media = product.selected_or_first_available_variant.featured_media -%}
<li class="product__media-item grid__item slider__slide{% if media.media_type != 'image' %} product__media-item--full{% endif %}" data-media-id="{{ section.id }}-{{ media.id }}">
<li class="product__media-item grid__item slider__slide{% if media.media_type != 'image' %} product__media-item--full{% endif %}{% if section.settings.hide_variants and variant_images contains media.src %} product__media-item--variant{% endif %}" data-media-id="{{ section.id }}-{{ media.id }}">
{% render 'product-thumbnail', media: media, position: 'featured', loop: section.settings.enable_video_looping, modal_id: section.id %}
</li>
{%- endif -%}
{%- for media in product.media -%}
{%- unless media.id == product.selected_or_first_available_variant.featured_media.id -%}
<li class="product__media-item grid__item slider__slide{% if media.media_type != 'image' %} product__media-item--full{% endif %}" data-media-id="{{ section.id }}-{{ media.id }}">
<li class="product__media-item grid__item slider__slide{% if media.media_type != 'image' %} product__media-item--full{% endif %}{% if section.settings.hide_variants and variant_images contains media.src %} product__media-item--variant{% endif %}" data-media-id="{{ section.id }}-{{ media.id }}">
{% render 'product-thumbnail', media: media, position: forloop.index, loop: section.settings.enable_video_looping, modal_id: section.id %}
</li>
{%- endunless -%}
Expand All @@ -44,7 +45,7 @@
<span class="slider-counter--current">1</span>
<span aria-hidden="true"> / </span>
<span class="visually-hidden">{{ 'accessibility.of' | t }}</span>
<span class="slider-counter--total">{{ product.media.size }}</span>
<span class="slider-counter--total">{% if section.settings.hide_variants %}{{ product.media.size | minus: variant_images.size | plus: 1 }}{% else %}{{ product.media.size }}{% endif %}</span>
</div>
<button type="button" class="slider-button slider-button--prev" name="previous" aria-label="{{ 'accessibility.previous_slide' | t }}">{% render 'icon-caret' %}</button>
<button type="button" class="slider-button slider-button--next" name="next" aria-label="{{ 'accessibility.next_slide' | t }}">{% render 'icon-caret' %}</button>
Expand Down Expand Up @@ -335,6 +336,7 @@
width="1100"
height="{{ 1100 | divided_by: media.preview_image.aspect_ratio | ceil }}"
data-media-id="{{ media.id }}"
{% if section.settings.hide_variants and variant_images contains media.src %}class="product__media-item--variant"{% endif %}
>
{%- else -%}
{%- if media.media_type == 'model' -%}
Expand Down Expand Up @@ -752,6 +754,12 @@
"content": "t:sections.main-product.settings.header.content",
"info": "t:sections.main-product.settings.header.info"
},
{
"type": "checkbox",
"id": "hide_variants",
"default": false,
"label": "t:sections.main-product.settings.hide_variants.label"
},
{
"type": "checkbox",
"id": "enable_video_looping",
Expand Down

0 comments on commit ce0e5ec

Please sign in to comment.