{I"
class:ETI"ProcessedAsset; FI"logical_path; TI"fshop/buy_button.js; FI"
pathname; TI"N/home/nofxx/projects/fshop/app/assets/javascripts/fshop/buy_button.coffee; FI"content_type; TI"application/javascript; TI"
mtime; Tl+ŪTRI"length; TiXI"digest; TI"%57464860cb4e7dffbae2cdac37aa20a8; FI"source; TI"X(function() {
var BuyButton, doc;
doc = $(document);
BuyButton = (function() {
function BuyButton(el) {
this.originalElement = el;
this.$originalElement = $(el);
this.setTargetElement();
this.changeFormIfNeeded();
this.$element.addClass('buy-button-target');
this.makeButtonTexts();
this.statesTimeouts = [];
}
BuyButton.prototype.getText = function(kind) {
var html;
return this.$originalElement.data("" + kind + "-text") || (kind === 'normal' ? this.$originalElement.val() || ((html = this.$originalElement.html()).replace(/\s/g, '') ? html : void 0) : void 0) || this.defaults.texts[kind];
};
BuyButton.prototype.setTargetElement = function() {
var height;
height = this.$originalElement.height();
if (this.originalElement.tagName === 'INPUT') {
this.$element = $('');
this.element = this.$element.get(0);
this.$element.attr('style', this.$originalElement.attr('style'));
this.$element.attr('class', this.$originalElement.attr('class'));
this.$element.addClass('buy-button-dummy');
this.$element.text(this.$originalElement.val());
this.$originalElement.hide();
this.$originalElement.after(this.$element);
return this.$element.data('buy-button', this);
} else {
this.element = this.originalElement;
return this.$element = this.$originalElement;
}
};
BuyButton.prototype.changeFormIfNeeded = function() {
var $form;
$form = this.$element.closest('form');
if ($form.length) {
return $form.addClass('with-buy-button');
}
};
BuyButton.prototype.makeButtonTexts = function() {
var text, _i, _len, _ref;
this.containers = {
fake: $(''),
base: $('')
};
this.texts = {};
_ref = ['normal', 'buy', 'buying', 'buyed', 'cart'];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
text = _ref[_i];
this.texts[text] = this.getText(text);
(this.containers[text] = $("").appendTo(this.containers.base)).html(this.texts[text]);
}
this.texts.error = this.getText('error');
this.containers.fake.css('height', this.$originalElement.height());
this.$element.html(this.containers.fake);
return this.containers.fake.html(this.containers.base);
};
BuyButton.prototype.changeState = function(state, isSystem) {
var timeout, _i, _len, _ref;
console.log('state', arguments);
if (!isSystem) {
_ref = this.statesTimeouts;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
timeout = _ref[_i];
clearTimeout(timeout);
}
this.statesTimeouts = [];
}
if (this.state) {
this.$element.removeClass(this.state);
}
if (state) {
this.$element.addClass(state);
}
return this.state = state;
};
BuyButton.prototype.delayChangeState = function(state, time) {
var _this = this;
console.log('delay', arguments);
return this.statesTimeouts.push(setTimeout((function() {
return _this.changeState(state, true);
}), time));
};
BuyButton.prototype.submited = function() {
return this.send();
};
BuyButton.prototype.clicked = function() {
if (this.state === 'cart' || this.state === 'buyed') {
return Turbolinks.visit('/cart');
} else {
return this.send();
}
};
BuyButton.prototype.send = function() {
var $form;
if (this.state === 'buying') {
return;
}
this.changeState('buying');
$form = this.$originalElement.closest('form');
if ($form) {
return $.ajax($form.attr('action'), {
data: $form.serialize(),
type: 'POST',
dataType: 'json',
complete: this.ajaxComplete,
context: this
});
} else {
return this.$originalElement.click();
}
};
BuyButton.prototype.ajaxComplete = function(e, state) {
if (state === 'success') {
this.$element.find('.state-buyed').html(this.texts.buyed);
this.changeState('buyed');
this.delayChangeState('cart', 3000);
return this.delayChangeState(void 0, 13000);
} else {
this.$element.find('.state-buyed').html(this.texts.error.replace(/:number/g, e.status));
this.changeState('buyed');
return this.delayChangeState(void 0, 5000);
}
};
BuyButton.prototype.defaults = {
texts: {
normal: 'Comprar',
buy: 'Comprar?',
buying: 'Comprando',
buyed: 'Comprado!',
cart: 'Carrinho',
error: 'Erro n::number'
}
};
return BuyButton;
})();
doc.on('created', '.buy-button:not(.buy-button-dummy)', function() {
return $(this).data('buy-button', new BuyButton(this));
});
doc.on('submit', 'form.with-buy-button', function(e) {
var button;
e.preventDefault();
button = $(this).find('.buy-button');
if (button.length) {
if (button.length > 1) {
button = button.filter('.default-buy-button') || button.first();
}
return button.data('buy-button').submited();
}
});
doc.on('click', '.buy-button, .buy-button-target', function(e) {
e.preventDefault();
return $(this).data('buy-button').clicked();
});
doc.on('click', 'form.with-buy-button [type=submit]:not(.buy-button)', function(e) {
return $(this).closest('form').removeClass('with-buy-button');
});
}).call(this);
; TI"dependency_digest; TI"%5fd0e817413d19ac2ba380d3cd3e39ce; FI"required_paths; T[I"N/home/nofxx/projects/fshop/app/assets/javascripts/fshop/buy_button.coffee; FI"dependency_paths; T[{I" path; TI"N/home/nofxx/projects/fshop/app/assets/javascripts/fshop/buy_button.coffee; FI"
mtime; TI"2013-10-09T01:26:43-03:00; TI"digest; TI"%22c0bfb39d2fb506148527cc9af3bfae; FI"
_version; TI"%ac1cd7cf9811f9938e2b8937c60a24e6; F