This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 472
/
shop.js.liquid
executable file
·186 lines (155 loc) · 5.34 KB
/
shop.js.liquid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
window.timber = window.timber || {};
timber.cacheSelectors = function () {
timber.cache = {
// General
$html: $('html'),
$body: $('body'),
// Navigation
$navigation: $('#AccessibleNav'),
// Product Page
$productImage: $('#ProductPhotoImg'),
$thumbImages: $('#ProductThumbs').find('a.product-single__thumbnail')
}
};
timber.init = function () {
timber.cacheSelectors();
timber.accessibleNav();
timber.productImageSwitch();
timber.responsiveVideos();
};
timber.accessibleNav = function () {
var $nav = timber.cache.$navigation,
$allLinks = $nav.find('a'),
$topLevel = $nav.children('li').find('a'),
$parents = $nav.find('.site-nav--has-dropdown'),
$subMenuLinks = $nav.find('.site-nav__dropdown').find('a'),
activeClass = 'nav-hover',
focusClass = 'nav-focus';
// Mouseenter
$parents.on('mouseenter touchstart', function(evt) {
var $el = $(this);
if (!$el.hasClass(activeClass)) {
evt.preventDefault();
}
showDropdown($el);
});
// Mouseout
$parents.on('mouseleave', function() {
hideDropdown($(this));
});
$subMenuLinks.on('touchstart', function(evt) {
// Prevent touchstart on body from firing instead of link
evt.stopImmediatePropagation();
});
$allLinks.focus(function() {
handleFocus($(this));
});
$allLinks.blur(function() {
removeFocus($topLevel);
});
// accessibleNav private methods
function handleFocus ($el) {
var $subMenu = $el.next('ul'),
hasSubMenu = $subMenu.hasClass('sub-nav') ? true : false,
isSubItem = $('.site-nav__dropdown').has($el).length,
$newFocus = null;
// Add focus class for top level items, or keep menu shown
if (!isSubItem) {
removeFocus($topLevel);
addFocus($el);
} else {
$newFocus = $el.closest('.site-nav--has-dropdown').find('a');
addFocus($newFocus);
}
}
function showDropdown ($el) {
$el.addClass(activeClass);
setTimeout(function() {
timber.cache.$body.on('touchstart', function() {
hideDropdown($el);
});
}, 250);
}
function hideDropdown ($el) {
$el.removeClass(activeClass);
timber.cache.$body.off('touchstart');
}
function addFocus ($el) {
$el.addClass(focusClass);
}
function removeFocus ($el) {
$el.removeClass(focusClass);
}
};
timber.productPage = function (options) {
var moneyFormat = options.money_format,
variant = options.variant,
selector = options.selector;
// Selectors
var $productImage = $('#ProductPhotoImg'),
$addToCart = $('#AddToCart'),
$productPrice = $('#ProductPrice'),
$comparePrice = $('#ComparePrice'),
$quantityElements = $('.quantity-selector, label + .js-qty'),
$addToCartText = $('#AddToCartText');
if (variant) {
// Update variant image, if one is set
if (variant.featured_image) {
var newImg = variant.featured_image,
el = $productImage[0];
Shopify.Image.switchImage(newImg, el, timber.switchImage);
}
// Select a valid variant if available
if (variant.available) {
// Available, enable the submit button, change text, show quantity elements
$addToCart.removeClass('disabled').prop('disabled', false);
$addToCartText.html({{ 'products.product.add_to_cart' | t | json }});
$quantityElements.show();
} else {
// Sold out, disable the submit button, change text, hide quantity elements
$addToCart.addClass('disabled').prop('disabled', true);
$addToCartText.html({{ 'products.product.sold_out' | t | json }});
$quantityElements.hide();
}
// Regardless of stock, update the product price
$productPrice.html( Shopify.formatMoney(variant.price, moneyFormat) );
// Also update and show the product's compare price if necessary
if (variant.compare_at_price > variant.price) {
$comparePrice
.html({{ 'products.product.compare_at' | t | json }} + ': ' + Shopify.formatMoney(variant.compare_at_price, moneyFormat))
.show();
} else {
$comparePrice.hide();
}
} else {
// The variant doesn't exist, disable submit button.
// This may be an error or notice that a specific variant is not available.
// To only show available variants, implement linked product options:
// - http://docs.shopify.com/manual/configuration/store-customization/advanced-navigation/linked-product-options
$addToCart.addClass('disabled').prop('disabled', true);
$addToCartText.html({{ 'products.product.unavailable' | t | json }});
$quantityElements.hide();
}
};
timber.productImageSwitch = function () {
if (timber.cache.$thumbImages.length) {
// Switch the main image with one of the thumbnails
// Note: this does not change the variant selected, just the image
timber.cache.$thumbImages.on('click', function(evt) {
evt.preventDefault();
var newImage = $(this).attr('href');
timber.switchImage(newImage, null, timber.cache.$productImage);
});
}
};
timber.switchImage = function (src, imgObject, el) {
// Make sure element is a jquery object
var $el = $(el);
$el.attr('src', src);
};
timber.responsiveVideos = function () {
$('iframe[src*="youtube.com/embed"]').wrap('<div class="video-wrapper"></div>');
$('iframe[src*="player.vimeo"]').wrap('<div class="video-wrapper"></div>');
};
// Initialize Timber's JS on docready
$(timber.init)