Skip to content

Instantly share code, notes, and snippets.

@jasonmerino
Last active August 29, 2015 13:58
Show Gist options
  • Save jasonmerino/9944226 to your computer and use it in GitHub Desktop.
Save jasonmerino/9944226 to your computer and use it in GitHub Desktop.
var productView = function() {
this.data = this.data || {};
return {
set: function(options) {
$.extend(this.data, options);
},
get: function(key) {
return this.data[key];
},
hasSelectedFinish: function() {
return this.get('$selectedFinish').length;
},
shouldHideOutOfStockMessage: function() {
return this.get('shouldHideOutOfStockMessage');
},
setSelectedFinishAndStoreData: function($element) {
this.set({
$selectedFinish: $element
stock: parseInt($element.data('stock'), 10),
isAvailable: $element.data('isavailable'),
sku: $element.data('sku')
});
},
selectedSwatchIsAvailable: function() {
return this.get('isAvailable');
},
updateOutOfStockMessage: function() {
if (this.shouldhideoutofstockMessage()) {
$stockZero.hide();
} else {
$stockZero.show();
}
},
selectedSwatchHasData: function() {
return this.get('sku') === undefined;
},
handleGeAndHotpoint: function(stock) {
// accounts for markup inconsistencies...?
if (!this.selectedSwatchHasData()) {
this.setSelectedFinishAndStoreData(this.get('$selectedFinish').parent('li').find('div[data-name]'));
stock = this.get('stock');
}
$availabilityVaries.show();
if (isFinishSelected() || isFinishSelected() && isPostalCodeDefined()) {
// Remove any error state classes
$availabilityVaries.find('label').removeClass('prd_error_highlight');
if (this.selectedSwatchIsAvailable()) {
pageMessaging.hideUnavailableByLocation();
updateStockMessage($stockCount, stock).show();
if (stock === 0) {
$stockCount.hide();
toggleAddToCart(false);
// it is possible that this method would return undefined
if (this.shouldHideOutOfStockMessage() === false) {
$stockZero.show();
}
} else if (stock > 0) {
$stockZero.hide();
$leadTimeText.show();
toggleAddToCart(true);
// it is possible that this method would return undefined
} else if (this.shouldHideOutOfStockMessage() === false) {
$stockCount.text('Out Of Stock').addClass('out-of-stock').show();
}
} else {
pageMessaging.showUnavailableByLocation();
$stockCount.hide();
$stockZero.hide();
$freeShipping.hide();
$leadTimeText.hide();
toggleAddToCart(false);
}
} else {
toggleAddToCart(false);
}
},
handleProducts: function(stock) {
$leadTimeText.show();
if (stock === 0) {
$stockCount.hide();
this.updateOutOfStockMessage();
} else if (stock > 0) {
$stockZero.hide();
updateStockMessage($stockCount, stock).show();
} else {
this.updateOutOfStockMessage();
}
}
};
}();
updatePageMessaging = function($swatch) {
var stock;
productView.setSelectedFinishAndStoreData($swatch || getSelectedFinish());
productView.set({
shouldHideOutOfStockMessage: undefined
});
if (productView.hasSelectedFinish()) {
stock = productView.get('stock');
productView.set({
shouldHideOutOfStockMessage: productView.get('$selectedFinish').data('hidemessage') === true
});
}
if (hasAvailabilityByLocation()) {
productView.handleGeAndHotpoint(stock);
} else {
productView.handleProducts(stock);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment