Created
September 18, 2017 13:12
-
-
Save raank/1f473039193db46e34f38a61ed3b4e42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
(function ($) { | |
if( !urlCart.length ) { | |
toastr.error('Url não foi definida', 'Erro'); | |
} | |
$('.md-form label').on('click', function () { | |
$(this).addClass('active'); | |
$(this).parent().find('input').focus(); | |
}); | |
$('.js-checkbox label').on('click', function (e) { | |
e.preventDefault(); | |
var self = $(this), | |
icon = self.find('i'), | |
remove = icon.data('remove'), | |
add = icon.data('add'), | |
input = self.find('input'); | |
if(icon.hasClass(remove)) { | |
icon | |
.removeClass(remove) | |
.addClass(add); | |
input.attr('checked', 'checked'); | |
} else { | |
icon | |
.removeClass(add) | |
.addClass(remove); | |
input.removeAttr('checked'); | |
} | |
}); | |
function money(v, c, d, t) | |
{ | |
var n = v, | |
c = isNaN(c = Math.abs(c)) ? 2 : c, | |
d = d == undefined ? "." : d, | |
t = t == undefined ? "," : t, | |
s = n < 0 ? "-" : "", | |
i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(c))), | |
j = (j = i.length) > 3 ? j % 3 : 0; | |
return 'R$ ' + s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); | |
} | |
var btnDefaultParams = { | |
btnClass: 'btn-success', | |
btnClassActive: 'btn-default', | |
btnClassSuccess: 'btn-success' | |
}, | |
btnDefaultTexts = { | |
init: '<i class="fa fa-refresh fa-spin"></i>', | |
error: '<i class="fa fa-check"></i> No carrinho', | |
success: '<i class="fa fa-check"></i> No carrinho' | |
}; | |
function button($this, type, params, text) { | |
if(type == 'init') { | |
$this | |
.attr('disabled', 'disabled') | |
.removeClass(params.btnClass) | |
.addClass(params.btnClassActive) | |
.html(text.init); | |
} else if(type == 'error') { | |
$this | |
.removeAttr('disabled') | |
.removeClass(params.btnClassActive) | |
.addClass(params.btnClass) | |
.html(text.error); | |
} else if(type == 'success') { | |
$this | |
.attr('disabled', 'disabled') | |
.removeClass(params.btnClassActive) | |
.addClass(params.btnClassSuccess) | |
.html(text.success); | |
} | |
} | |
$('.form-search').submit(function(e) { | |
var $form = $(this); | |
Pace.stop(); | |
Pace.start(); | |
if($form.find('#search').val()){ | |
return; | |
} else { | |
e.preventDefault(); | |
toastr.error('Insira um termo para buscar algo', 'Erro!'); | |
} | |
}); | |
$('.js-add-carts').on('click', function () { | |
var $button = $(this), | |
$id = $button.data('id'); | |
Pace.stop(); | |
Pace.start(); | |
$button | |
.attr('disabled', 'disabled') | |
.removeClass('btn-success') | |
.addClass('btn-default') | |
.html('<i class="fa fa-refresh fa-spin"></i> Processando.'); | |
$.ajax({ | |
url: urlCart, | |
method: 'POST', | |
data: { | |
item: $id, | |
type: $button.data('type') ? $button.data('type') : 'product', | |
qnt: $button.data('qnt') ? $button.data('qnt') : 1, | |
size: null, | |
_method: 'POST' | |
}, | |
success: function (data) { | |
toastr.success(data.message, 'Sucesso!'); | |
$('#cart-total').html(money(data.cart.total, 2, ',', '.')); | |
ga('send', 'event', 'Product Added on Cart', 'click', 'Produto (#' + $id + ') adicionado ao carrinho'); | |
$button | |
.attr('disabled', 'disabled') | |
.html('<i class="fa fa-check"></i> No carrinho'); | |
if(data.otherMessage) { | |
toastr.info(data.otherMessage, 'Atenção!'); | |
} | |
Pace.start(); | |
}, | |
error: function (data) { | |
location.href = data.url; | |
} | |
}); | |
}); | |
$('.js-coupon-discount').on('click', function () { | |
var $button = $(this), | |
$coupon = $('#coupon-discount'); | |
Pace.stop(); | |
Pace.start(); | |
$button | |
.attr('disabled', 'disabled') | |
.removeClass('btn-elegant') | |
.addClass('btn-default') | |
.html('<i class="fa fa-refresh fa-spin"></i> Processando.'); | |
$.ajax({ | |
url: urlCart + '/coupon', | |
method: 'POST', | |
data: { | |
coupon: $coupon.val(), | |
_method: 'POST' | |
}, | |
success: function (data) { | |
console.log(data); | |
toastr.success(data.message, 'Sucesso!'); | |
var discount = data.coupon.value + '% - ', | |
discountOf = money(data.coupon.discounted, 2, ',', '.'); | |
$('.cart-total').html(money(data.cart.total, 2, ',', '.')); | |
$('.coupon-discount').html(discount + discountOf); | |
$button | |
.removeAttr('disabled') | |
.removeClass('btn-default') | |
.addClass('btn-elegant') | |
.html('Calcular'); | |
Pace.stop(); | |
}, | |
error: function (data) { | |
var response = JSON.parse(data.responseText); | |
console.log(response); | |
toastr.error(response.message, 'Ops!') | |
$button | |
.removeAttr('disabled') | |
.removeClass('btn-default') | |
.addClass('btn-elegant') | |
.html('Calcular'); | |
Pace.stop(); | |
} | |
}); | |
}); | |
$('.js-add-size').on('click', function () { | |
var $button = $(this); | |
$('.js-add-cart-wsizes').attr('data-size', $button.data('size')); | |
Pace.stop(); | |
Pace.start(); | |
$('.js-add-size') | |
.removeClass('badge-success') | |
.addClass('badge-primary'); | |
$button | |
.removeClass('badge-primary') | |
.addClass('badge-success'); | |
Pace.stop(); | |
}); | |
$.fn.addCart = function (options) { | |
var settings = $.extend({ | |
id: $(this).data('id'), | |
type: 'product', | |
qnt: 1, | |
url: urlCart, | |
hasSize: false, | |
text: this.data('text'), | |
btnClass: btnDefaultParams, | |
btnTexts: btnDefaultTexts, | |
// cart | |
cartTotal: $('#cart-total'), | |
onSuccess: function (data) {}, | |
onError: function (data) {} | |
}, options); | |
var sizes = function ($this) { | |
if(!$this.data('size')) { | |
toastr.error('Tamanho não foi definido', 'Erro!'); | |
setTimeout(function() { | |
button($this, 'error', settings.btnClass, settings.btnTexts); | |
}, 500); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
var addToCart = function ($this) { | |
$.ajax({ | |
url: urlCart, | |
method: 'POST', | |
data: { | |
id: settings.id, | |
type: settings.type, | |
qnt: settings.qnt, | |
size: $this.data('size'), | |
_method: 'POST' | |
}, | |
success: function (data) { | |
console.log(data); | |
toastr.success(data.message, 'Sucesso!'); | |
settings.onSuccess(data); | |
settings.cartTotal.html(money(data.cart.total, 2, ',', '.')); | |
button($this, 'success', settings.btnClass, settings.btnTexts); | |
if(data.otherMessage) { | |
toastr.info(data.otherMessage, 'Atenção!'); | |
} | |
Pace.start(); | |
}, | |
error: function (data) { | |
var response = $.parseJSON(data.responseText); | |
if( response.messages ) { | |
response.messages.forEach(function (msg) { | |
toastr.error(msg, 'Erro!'); | |
}) | |
} | |
button($this, 'error', settings.btnClass, settings.btnTexts); | |
settings.onError(response); | |
} | |
}); | |
} | |
return this.click(function() { | |
var $this = $(this); | |
button($this, 'init', settings.btnClass, settings.btnTexts); | |
if( settings.hasSize ) { | |
if(sizes($this)) { | |
addToCart($this); | |
} | |
} else { | |
addToCart($this); | |
} | |
}); | |
}; | |
$('.js-add-cart-wsizes').addCart({ | |
hasSize: true, | |
btnTexts: { | |
init: '<i class="fa fa-refresh fa-spin"></i>', | |
error: 'Adicionar ao Carrinho', | |
success: '<i class="fa fa-check"></i> No carrinho' | |
} | |
}); | |
$('.js-set-favorite').on('click', function () { | |
var $this = $(this), | |
$item = $this.data('item'), | |
$user = $this.data('user'); | |
if ($user) { | |
console.log($user); | |
$.ajax({ | |
url: urlApi + '/products/favorite/' + $item, | |
method: 'POST', | |
data: { | |
_item: $item, | |
_user: $user | |
}, | |
success: function (data) { | |
if (data.message) { | |
toastr.success(data.success); | |
} | |
$this.find('.fa').removeClass('text-preto').addClass('text-vermelho animate-red'); | |
}, | |
error: function (err) { | |
var data = $.parseJSON(err.responseText); | |
if (data.message) { | |
toastr.error(data.message); | |
} | |
} | |
}); | |
} else { | |
window.location = url + '/conta/entrar/?favorite=' + $item; | |
} | |
}); | |
$('.js-remove-favorite').on('click', function () { | |
var $this = $(this), | |
$item = $this.data('item'), | |
$remove = $this.data('remove'); | |
$.ajax({ | |
url: urlApi + '/products/favorite/' + $item, | |
method: 'DELETE', | |
data: { | |
_item: $item | |
}, | |
success: function (data) { | |
if (data.message) { | |
toastr.success(data.success); | |
} | |
if ($remove) { | |
$('#product-item-' + $item).remove(); | |
} | |
$this.find('.fa').removeClass('text-vermelho').addClass('text-preto animate-black'); | |
}, | |
error: function (err) { | |
var data = $.parseJSON(err.responseText); | |
if (data.message) { | |
toastr.error(data.message); | |
} | |
} | |
}); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment