Last active
August 29, 2015 14:04
-
-
Save mhui/4c01ac66c2809d35da38 to your computer and use it in GitHub Desktop.
This file contains 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
(function() { | |
var Crane, ajax, deleteCookie, escapeRegExp, getCookie, hide, isIos, jsonp, serialize, setCookie, show, trim, xhr, | |
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | |
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | |
Crane = (function() { | |
Crane.prototype.DOMAIN = "https://localhost:3000"; | |
Crane.prototype.MIN_DISPLAY = 1000 * 2; | |
Crane.prototype.ERROR_MESSAGE = "<h2>Error adding to cart</h4>"; | |
Crane.prototype.CHANNELS = ['instagram', 'pinterest']; | |
function Crane() { | |
this.onError = __bind(this.onError, this); | |
this.onTrackConversion = __bind(this.onTrackConversion, this); | |
this.goToCart = __bind(this.goToCart, this); | |
this.onAddToCartError = __bind(this.onAddToCartError, this); | |
this.onAddToCart = __bind(this.onAddToCart, this); | |
this.addToCart = __bind(this.addToCart, this); | |
this.onGetCartError = __bind(this.onGetCartError, this); | |
this.onGetCart = __bind(this.onGetCart, this); | |
this.onModifyInvoiceSuccess = __bind(this.onModifyInvoiceSuccess, this); | |
this.modifySoldsieInvoice = __bind(this.modifySoldsieInvoice, this); | |
this.updatePrompted = __bind(this.updatePrompted, this); | |
this.onRetrieveInvoiceError = __bind(this.onRetrieveInvoiceError, this); | |
this.onRetrieveInvoice = __bind(this.onRetrieveInvoice, this); | |
this.onSubmitRegister = __bind(this.onSubmitRegister, this); | |
this.onFetchUsername = __bind(this.onFetchUsername, this); | |
this.fetchUsername = __bind(this.fetchUsername, this); | |
var invoiceparam, order_id, regex, result, _ref; | |
this.storeId = document.getElementById('soldsie-crane').getAttribute('data-store-id'); | |
this.addStyles(); | |
regex = new RegExp("[\\?&]soldsieid=([^&#]*)"); | |
invoiceparam = regex.exec(location.search); | |
if (invoiceparam) { | |
this.showModal("<div id='soldsie-spinner'></div>"); | |
this.start = (new Date()).getTime(); | |
this.showSpinner('soldsie-spinner'); | |
ajax({ | |
url: "" + this.DOMAIN + "/invoices/" + invoiceparam[1], | |
data: { | |
'snippet': 1 | |
}, | |
success: this.onRetrieveInvoice, | |
error: this.onRetrieveInvoiceError, | |
jsonp: true | |
}); | |
} else { | |
if (result = /[\\?&]soldsie-reg=([^&]+)/.exec(location.search)) { | |
if (!(this.CHANNELS.indexOf(result[1]) > -1)) { | |
return; | |
} | |
this.channel = result[1]; | |
this.showRegistrationFlow(); | |
} | |
} | |
if (window.location.href.match(/checkout\/onepage\/success/i)) { | |
order_id = (_ref = document.querySelectorAll(getCookie('order_id_selector'))[0]) != null ? _ref.innerHTML.match(/[0-9]+/i)[0] : void 0; | |
this.trackConversion(order_id); | |
} else if (getCookie('cart_url') && window.location.href.match(new RegExp(escapeRegExp(getCookie('cart_url'), "i"))) && getCookie("from_soldsie")) { | |
console.log('Visited cart after redirect'); | |
if (getCookie('empty_cart_text') && document.body.innerHTML.search(getCookie('empty_cart_text')) > 0) { | |
this.notifyError("CouldNotAddToCart", "" + window.location.href + " " + (getCookie('invoice_ids'))); | |
this.showModal(ERROR_MESSAGE); | |
} | |
} | |
deleteCookie('from_soldsie'); | |
} | |
Crane.prototype.showModal = function(html) { | |
var _ref, | |
_this = this; | |
if ((_ref = this.spinner) != null) { | |
_ref.stop(); | |
} | |
if (!this.modal) { | |
this.modal = document.createElement('div'); | |
this.modal.setAttribute('id', 'soldsie'); | |
this.modal.setAttribute('style', 'opacity:0'); | |
document.body.appendChild(this.modal); | |
setTimeout(function() { | |
console.log('make visible', _this.modal); | |
return _this.modal.setAttribute('style', ''); | |
}, 100); | |
} | |
this.modal.innerHTML = " <div id='soldsie-bg'></div> <div id='soldsie-modal-wrapper'> <div id='soldsie-modal'> <div id='soldsie-error' class='soldsie-h3'></div> " + html + " </div> </div>"; | |
return this.modal; | |
}; | |
Crane.prototype.hideModal = function(delay) { | |
var _this = this; | |
if (delay == null) { | |
delay = 3000; | |
} | |
setTimeout(function() { | |
return _this.modal.setAttribute('style', 'opacity:0'); | |
}, delay); | |
return setTimeout(function() { | |
return _this.modal.parentNode.removeChild(_this.modal); | |
}, delay + 500); | |
}; | |
Crane.prototype.showSpinner = function(id, opts) { | |
if (opts == null) { | |
opts = {}; | |
} | |
this.hideSpinner(); | |
opts.lines || (opts.lines = 13); | |
opts.length || (opts.length = 20); | |
opts.width || (opts.width = 10); | |
opts.radius || (opts.radius = 30); | |
opts.corners || (opts.corners = 1); | |
opts.rotate || (opts.rotate = 0); | |
opts.direction || (opts.direction = 1); | |
opts.color || (opts.color = '#333'); | |
opts.speed || (opts.speed = 1); | |
opts.trail || (opts.trail = 60); | |
opts.shadow || (opts.shadow = false); | |
opts.hwaccel || (opts.hwaccel = false); | |
opts.zIndex || (opts.zIndex = 2e9); | |
if (!opts.bottom) { | |
opts.top || (opts.top = '50%'); | |
} | |
if (!opts.right) { | |
opts.left || (opts.left = '50%'); | |
} | |
return this.spinner = new Spinner(opts).spin(document.getElementById(id)); | |
}; | |
Crane.prototype.hideSpinner = function() { | |
var _ref; | |
return (_ref = this.spinner) != null ? _ref.stop() : void 0; | |
}; | |
Crane.prototype.showPromptedOverlay = function(promptedInvoices) { | |
var html, i, invoice, variant, _i, _j, _len, _len1, _ref; | |
html = "<a class='modalCloseImg simplemodal-close' title='Close'></a> <form>"; | |
for (i = _i = 0, _len = promptedInvoices.length; _i < _len; i = ++_i) { | |
invoice = promptedInvoices[i]; | |
if (i > 0) { | |
html += '<br><br><br>'; | |
} | |
html += " <div class='soldsie-h2'>" + invoice.name + "</div> <div class='soldsie-card'><img src='" + invoice.image_url + "' /></div> <br> <select class='soldsie-input' id='soldsie-invoice-" + invoice.invoice_id + "'>"; | |
_ref = invoice.variants; | |
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { | |
variant = _ref[_j]; | |
html += "<option value='" + variant.id + "'>" + variant.value + "</option>"; | |
} | |
html += "</select>"; | |
} | |
if (promptedInvoices.length > 1) { | |
html += "<br><br><input type='submit' value='Choose' id='soldsie-submit' class='soldsie-btn soldsie-btn-large'>"; | |
} else { | |
html += " <input type='submit' value='Choose' id='soldsie-submit' class='soldsie-btn'>"; | |
} | |
html += "</form>"; | |
this.showModal(html); | |
return document.getElementById('soldsie-submit').onclick = this.updatePrompted; | |
}; | |
Crane.prototype.showError = function(message) { | |
var _ref; | |
return (_ref = document.getElementById('soldsie-error')) != null ? _ref.innerHTML = message : void 0; | |
}; | |
Crane.prototype.hideError = function() { | |
var _ref; | |
return (_ref = document.getElementById('soldsie-error')) != null ? _ref.innerHTML = '' : void 0; | |
}; | |
Crane.prototype.addStyles = function() { | |
var css, style; | |
css = " #soldsie {overflow:auto;overflow-y:scroll;position:fixed;top:0;right:0;bottom:0;left:0;z-index:9999999999;transition:opacity 0.35s ease-out;-webkit-transition:opacity 0.35s ease-out;-o-transition:opacity 0.35s ease-out;-moz-transition:opacity 0.35s ease-out} #soldsie form {margin-bottom:10px;} #soldsie .soldsie-h1 {display:block;font-size:28px;margin-bottom:10px;line-height:35px;text-transform:uppercase; letter-spacing: 2px;} #soldsie .soldsie-h2 {display:block;font-size:23px;margin-bottom:10px;line-height:28px;text-transform:uppercase; letter-spacing: 2px;} #soldsie .soldsie-h3 {display:block;font-size:18px;margin-bottom:10px;line-height:25px;text-transform:uppercase; letter-spacing: 2px;} #soldsie .soldsie-h4 {display:block;font-size:15px;margin-bottom:10px;line-height:20px;text-transform:uppercase; letter-spacing: 2px;} #soldsie .soldsie-cardd {display:inline-block;box-sizing:border-box;background:white;box-shadow:0 0 0 1px rgba(220,226,229,0.5),0 0 10px rgba(220,226,229,1);padding:10px;position:relative;margin-bottom:10px;} #soldsie .soldsie-input{display:inline-block;box-sizing:border-box;width:200px;height:34px;padding:6px 12px;margin-bottom:5px;font-size:14px;line-height:14px;background-color:#fff;border:1px solid #ccc;-webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0, .075);box-shadow:inset 0 1px 1px rgba(0, 0, 0, .075);-webkit-transition:border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;-o-transition:border-color ease-in-out .15s, box-shadow ease-in-out .15s;transition:border-color ease-in-out .15s, box-shadow ease-in-out .15s;} #soldsie select.soldsie-input {width:auto;min-width:100px;max-width:200px} #soldsie .soldsie-input:focus{border-color:#66afe9;outline:0;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);box-shadow:inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);} #soldsie .soldsie-input::-moz-placeholder {color:#AAA;opacity:1;} #soldsie .soldsie-input:-ms-input-placeholder {color:#AAA;} #soldsie .soldsie-input::-webkit-input-placeholder {color:#AAA;} #soldsie .soldsie-input-indented {padding-left:30px;} #soldsie #soldsie-customer-img {position:absolute;top:7px;left:7px;width:20px;height:20px;} #soldsie .soldsie-btn {display:inline-block;box-sizing:border-box;text-transform:uppercase;background-color:#000;color:#FFF;padding:8px 12px;height:auto;margin-bottom:5px;font-size:12px;font-weight:bold;line-height:14px;text-align:center;white-space:nowrap;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;background-image:none;border:1px solid transparent;} #soldsie .soldsie-btn:focus {outline:thin dotted;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px;} #soldsie .soldsie-btn:hover,#soldsie .soldsie-btn:focus {background-color:#ff2dff;text-decoration:none;} #soldsie .soldsie-btn:active {background-image:none;outline:0;-webkit-box-shadow:inset 0 3px 5px rgba(0, 0, 0, .125);box-shadow:inset 0 3px 5px rgba(0, 0, 0, .125);} #soldsie .soldsie-btn-large {padding:12px 16px;font-size:15px;line-height:16px;min-width:200px;} #soldsie .soldsie-btn-disabled,#soldsie .soldsie-btn-disabled:hover {color:#FFF;background-color:#AAA;} #soldsie-error {color:#e94e58;} #soldsie-bg {position:fixed;top:0;right:0;bottom:0;left:0;background-color:#000;opacity:0.7;filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=70);-ms-filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=70)} #soldsie-modal-wrapper {position:relative;width:470px;margin:50px auto;background:#FFF;padding:5px;} #soldsie-modal {border:1px solid #b7b7b7;position:relative;padding:30px 25px;box-sizing:border-box;max-width:600px;margin:0 auto;text-align:center;background:#FFF} "; | |
style = document.createElement("style"); | |
style.type = "text/css"; | |
if (style.styleSheet) { | |
style.styleSheet.cssText = css; | |
} else { | |
style.appendChild(document.createTextNode(css)); | |
} | |
return document.getElementsByTagName("head")[0].appendChild(style); | |
}; | |
Crane.prototype.showRegistrationFlow = function() { | |
var html; | |
html = " <a class='modalCloseImg simplemodal-close' title='Close'></a> <div style=\"background-image: url('https://dc8b1d610eee21816eff-159d789f4e9bf2ec1e5eacfcd79db0d8.ssl.cf1.rackcdn.com/logo.png');width: 198px;height: 39px;margin: 0 auto 25px auto;background-size:contain;background-repeat: no-repeat;\"></div> <hr style='height: 5px;border: none;background: #000;width: 140px;margin:0 auto 5px;'> <form accept-charset='UTF-8' action='" + this.DOMAIN + "/stores/" + this.storeId + "/customers' id='soldsie-customer-form' method='post'> <input id='channel' name='channel' type='hidden' value='" + this.channel + "' /> <input id='soldsie-customer-channel' name='customer[channel]' type='hidden' value='" + this.channel + "' /> <input id='soldsie-customer-first-name' name='customer[first_name]' type='hidden' /> <input id='soldsie-customer-last-name' name='customer[last_name]' type='hidden' /> <input id='soldsie-customer-name' name='customer[name]' type='hidden' /> <input id='soldsie-customer-" + this.channel + "-picture-url' name='customer[" + this.channel + "_picture_url]' type='hidden' /> <input id='soldsie-customer-" + this.channel + "-graph-id' name='customer[" + this.channel + "_graph_id]' type='hidden' value='' /> <h3 class='soldsie-h4' style='font-size: 15px;'>Register your " + (this.channel.charAt(0).toUpperCase() + this.channel.substr(1).toLowerCase()) + " Account</h3> <div style='display:inline-block;position:relative;width:205px'> <div id='soldsie-register-spinner'></div> <img src='" + this.DOMAIN + "/assets/dashboard/" + this.channel + "_16x16.png' id='soldsie-customer-img' /> <input id='soldsie-customer-" + this.channel + "-username' name='customer[" + this.channel + "_username]' placeholder='username' required='required' size='30' type='text' class='soldsie-input soldsie-input-indented' /> <br> <div id='soldsie-customer-new'> <input class='soldsie-input' type='email' id='soldsie-customer-email' name='customer[email]'' placeholder='[email protected]' size='30' type='text' value=''> <br> <button name='button' id='soldsie-register-btn' type='submit' class='soldsie-btn soldsie-btn-large soldsie-btn-disabled'>Register</button> </div> <div id='soldsie-customer-existing' style='display:none'> <div id='soldsie-existing-message' class='soldsie-h4'></div> <a id='soldsie-continue' href='" + (isIos() ? 'instagram://' : 'http://instagram.com') + "' class='soldsie-btn soldsie-btn-large'>OK!</a> </div> </div> </form> "; | |
this.showModal(html); | |
document.getElementById("soldsie-customer-" + this.channel + "-username").onblur = this.fetchUsername; | |
return document.getElementById('soldsie-customer-form').onsubmit = this.onSubmitRegister; | |
}; | |
Crane.prototype.fetchUsername = function(event) { | |
var username; | |
username = document.getElementById("soldsie-customer-" + this.channel + "-username").value; | |
if (!username) { | |
return; | |
} | |
if (this.username === username) { | |
return; | |
} | |
this.username = username; | |
this.hideError(); | |
this.showSpinner('soldsie-register-spinner', { | |
length: 3, | |
width: 2, | |
radius: 4, | |
left: '183px', | |
top: '16px' | |
}); | |
document.getElementById('soldsie-customer-img').setAttribute('src', "" + this.DOMAIN + "/assets/dashboard/" + this.channel + "_16x16.png"); | |
document.getElementById('soldsie-register-btn').setAttribute('class', 'soldsie-btn soldsie-btn-large soldsie-btn-disabled'); | |
document.getElementById('soldsie-customer-new').setAttribute('style', 'display: block'); | |
document.getElementById('soldsie-customer-existing').setAttribute('style', 'display: none'); | |
return ajax({ | |
url: "" + this.DOMAIN + "/customers/" + this.channel, | |
data: { | |
'username': username | |
}, | |
success: this.onFetchUsername, | |
error: this.onError, | |
jsonp: true | |
}); | |
}; | |
Crane.prototype.onFetchUsername = function(response) { | |
var data; | |
this.hideSpinner(); | |
data = response; | |
if (data.error) { | |
return this.showError('We couldn\'t find a user with that username'); | |
} else if (data.email) { | |
document.getElementById('soldsie-customer-new').setAttribute('style', 'display: none'); | |
document.getElementById('soldsie-customer-existing').setAttribute('style', 'display: block'); | |
document.getElementById('soldsie-existing-message').innerHTML = "Account already registered! <strong>" + data.email + "</strong>"; | |
if (data.profile_picture) { | |
return document.getElementById('soldsie-customer-img').setAttribute('src', data.profile_picture); | |
} | |
} else { | |
document.getElementById("soldsie-customer-" + this.channel + "-graph-id").value = data.graph_id || ''; | |
document.getElementById('soldsie-customer-name').value = data.full_name || ''; | |
document.getElementById('soldsie-customer-first-name').value = data.first_name || ''; | |
document.getElementById('soldsie-customer-last-name').value = data.last_name || ''; | |
document.getElementById("soldsie-customer-" + this.channel + "-picture-url").value = data.profile_picture || ''; | |
document.getElementById('soldsie-register-btn').setAttribute('class', 'soldsie-btn soldsie-btn-large'); | |
if (data.profile_picture) { | |
return document.getElementById('soldsie-customer-img').setAttribute('src', data.profile_picture); | |
} | |
} | |
}; | |
Crane.prototype.onSubmitRegister = function(event) { | |
var btnClass; | |
btnClass = document.getElementById('soldsie-register-btn').getAttribute('class'); | |
if (btnClass.indexOf('disabled') > -1 || !this.emailIsValid(document.getElementById('soldsie-customer-email').value)) { | |
return false; | |
} | |
}; | |
Crane.prototype.emailIsValid = function(email) { | |
var re; | |
re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
return re.test(email); | |
}; | |
Crane.prototype.onRetrieveInvoice = function(data) { | |
var item, _i, _len, _ref, _ref1, _ref2, _ref3, _results; | |
setCookie('cart_url', data.cart_url); | |
setCookie('add_cart_url', data.cart_add_url); | |
setCookie('order_id_selector', data.order_id_selector); | |
setCookie('invoice_ids', data.invoice_ids); | |
setCookie('empty_cart_text', data.empty_cart_text); | |
this.items = data.items.filter(function(invoice) { | |
return invoice.status !== 'prompted'; | |
}); | |
this.promptedInvoices = data.items.filter(function(invoice) { | |
return invoice.status === 'prompted'; | |
}); | |
if (this.promptedInvoices.length > 0) { | |
this.showPromptedOverlay(this.promptedInvoices); | |
} | |
if (!(((_ref = this.items) != null ? _ref.length : void 0) || ((_ref1 = this.promptedInvoices) != null ? _ref1.length : void 0))) { | |
return this.goToCart(); | |
} | |
if (data.can_check_cart) { | |
return this.getCart(); | |
} else { | |
this.numItemsToAdd = this.items.length + ((_ref2 = this.promptedInvoices) != null ? _ref2.length : void 0); | |
_ref3 = this.items; | |
_results = []; | |
for (_i = 0, _len = _ref3.length; _i < _len; _i++) { | |
item = _ref3[_i]; | |
_results.push(this.addToCart(item)); | |
} | |
return _results; | |
} | |
}; | |
Crane.prototype.onRetrieveInvoiceError = function(response) { | |
this.hideSpinner(); | |
this.showModal('<div class="soldsie-h2">Oh no! We could not find that invoice.</div>Please check your email for the correct invoice. Thanks!'); | |
return this.hideModal(6000); | |
}; | |
Crane.prototype.updatePrompted = function(event) { | |
var e, invoice, _i, _len, _ref; | |
event.preventDefault(); | |
_ref = this.promptedInvoices; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
invoice = _ref[_i]; | |
invoice.quantity = 1; | |
e = document.getElementById("soldsie-invoice-" + invoice.invoice_id); | |
console.log(e.options, e.selectedIndex, e.options[e.selectedIndex].value); | |
this.modifySoldsieInvoice(invoice.invoice_id, e.options[e.selectedIndex].value); | |
} | |
this.showModal("<div id='soldsie-spinner'></div>"); | |
return this.showSpinner('soldsie-spinner'); | |
}; | |
Crane.prototype.modifySoldsieInvoice = function(invoiceId, variantId) { | |
return ajax({ | |
url: "" + this.DOMAIN + "/snippet/i", | |
data: { | |
'invoice_id': invoiceId, | |
'variant_id': variantId | |
}, | |
success: this.onModifyInvoiceSuccess, | |
error: this.onError, | |
jsonp: true | |
}); | |
}; | |
Crane.prototype.onModifyInvoiceSuccess = function(response) { | |
return this.addToCart(response); | |
}; | |
Crane.prototype.getCart = function() { | |
return ajax({ | |
url: '/cart.js', | |
success: this.onGetCart, | |
error: this.onGetCartError | |
}); | |
}; | |
Crane.prototype.onGetCart = function(response) { | |
var cartItem, data, ids, item, itemIndex, itemsToAdd, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _results; | |
data = response; | |
ids = (function() { | |
var _i, _len, _ref, _results; | |
_ref = this.items; | |
_results = []; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
item = _ref[_i]; | |
_results.push(item.id); | |
} | |
return _results; | |
}).call(this); | |
_ref = data.items; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
cartItem = _ref[_i]; | |
itemIndex = ids.indexOf("" + cartItem.id); | |
if (itemIndex > -1) { | |
item = this.items[itemIndex]; | |
item.quantity -= cartItem.quantity; | |
} | |
} | |
itemsToAdd = []; | |
_ref1 = this.items; | |
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | |
item = _ref1[_j]; | |
if (item.quantity > 0) { | |
itemsToAdd.push(item); | |
} | |
} | |
if (!(itemsToAdd.length || this.promptedInvoices.length)) { | |
return this.goToCart(); | |
} | |
this.numItemsToAdd = itemsToAdd.length + ((_ref2 = this.promptedInvoices) != null ? _ref2.length : void 0); | |
_results = []; | |
for (_k = 0, _len2 = itemsToAdd.length; _k < _len2; _k++) { | |
item = itemsToAdd[_k]; | |
_results.push(this.addToCart(item)); | |
} | |
return _results; | |
}; | |
Crane.prototype.onGetCartError = function(response) { | |
return console.log('Error retrieving cart', response); | |
}; | |
Crane.prototype.addToCart = function(invoice) { | |
return ajax({ | |
url: getCookie('add_cart_url'), | |
data: invoice, | |
success: this.onAddToCart, | |
error: this.onAddToCartError, | |
method: 'POST' | |
}); | |
}; | |
Crane.prototype.onAddToCart = function(response) { | |
this.numItemsToAdd--; | |
if (!this.numItemsToAdd) { | |
return this.goToCart(); | |
} | |
}; | |
Crane.prototype.onAddToCartError = function(response) { | |
this.numItemsToAdd--; | |
console.log('Error adding to cart', response); | |
this.notifyError("CouldNotAddToCart", "" + window.location.href + " " + (getCookie('invoice_ids'))); | |
return this.showModal(ERROR_MESSAGE); | |
}; | |
Crane.prototype.goToCart = function() { | |
setCookie("from_soldsie", 1); | |
if (this.start && ((new Date()).getTime() - this.start) < this.MIN_DISPLAY) { | |
return setTimeout(this.goToCart, 500); | |
} else { | |
console.log(getCookie('cart_url')); | |
return window.location = getCookie('cart_url'); | |
} | |
}; | |
Crane.prototype.trackConversion = function(orderId, invoiceIds, quantities) { | |
var data; | |
if (orderId == null) { | |
orderId = ''; | |
} | |
if (invoiceIds == null) { | |
invoiceIds = ''; | |
} | |
if (quantities == null) { | |
quantities = ''; | |
} | |
console.log("Track conversion"); | |
data = { | |
'order_id': orderId | |
}; | |
if (invoiceIds) { | |
data.invoice_ids = invoiceIds; | |
} | |
if (quantities) { | |
data.quantities = quantities; | |
} | |
return ajax({ | |
url: "" + this.DOMAIN + "/snippet/s", | |
data: data, | |
success: this.onTrackConversion, | |
error: this.onTrackConversionError, | |
jsonp: true | |
}); | |
}; | |
Crane.prototype.onTrackConversion = function(response) { | |
return console.log(response); | |
}; | |
Crane.prototype.notifyError = function(errorClass, errorMessage) { | |
return ajax({ | |
url: "" + this.DOMAIN + "/snippet/e", | |
data: { | |
'error_class': errorClass, | |
'error_message': errorMessage | |
}, | |
success: function(response) { | |
return console.log(response); | |
}, | |
error: function() { | |
return console.log(response); | |
}, | |
jsonp: true | |
}); | |
}; | |
Crane.prototype.onError = function(response) { | |
return console.log(response); | |
}; | |
return Crane; | |
})(); | |
isIos = function() { | |
return navigator.userAgent.match(/iPhone|iPad|iPod/i); | |
}; | |
ajax = function(params) { | |
if (params.jsonp) { | |
return jsonp(params); | |
} else { | |
return xhr(params); | |
} | |
}; | |
jsonp = function(params) { | |
var callbackId, script; | |
callbackId = Math.floor(Math.random() * 100000000000000000); | |
window["soldsie" + callbackId] = function(data) { | |
return params.success(data); | |
}; | |
script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.async = true; | |
script.src = "" + params.url + ".js?" + (serialize(params.data)) + "&callback=soldsie" + callbackId; | |
script.onerror = function() { | |
if (params.error != null) { | |
return params.error({}); | |
} | |
}; | |
return document.body.appendChild(script); | |
}; | |
xhr = function(params) { | |
var req; | |
if (window.XMLHttpRequest) { | |
req = new XMLHttpRequest(); | |
} | |
req || (req = new ActiveXObject("Microsoft.XMLHTTP")); | |
req.addEventListener('readystatechange', function() { | |
var successResultCodes, _ref; | |
if (req.readyState === 4) { | |
successResultCodes = [200, 304]; | |
if (_ref = req.status, __indexOf.call(successResultCodes, _ref) >= 0) { | |
if (params.success != null) { | |
return params.success(req.responseText); | |
} | |
} else { | |
if (params.error != null) { | |
return params.error(req); | |
} | |
} | |
} | |
}); | |
req.open(params.method || 'GET', params.url, false); | |
if (params.method === 'POST') { | |
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | |
} | |
return req.send(serialize(params.data)); | |
}; | |
escapeRegExp = function(str) { | |
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | |
}; | |
trim = function(s) { | |
return (s || '').replace(/^\s+|\s+$/g, ''); | |
}; | |
serialize = function(obj) { | |
var p, str, val; | |
str = []; | |
for (p in obj) { | |
val = obj[p]; | |
str.push("" + (encodeURIComponent(p)) + "=" + (encodeURIComponent(val))); | |
} | |
return str.join('&'); | |
}; | |
setCookie = function(name, value, days) { | |
var date, expires; | |
if (days) { | |
date = new Date(); | |
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |
expires = "; expires=" + date.toGMTString(); | |
} else { | |
expires = ""; | |
} | |
return document.cookie = name + "=" + value + expires + "; path=/"; | |
}; | |
getCookie = function(name) { | |
var c, ca, i, nameEQ; | |
nameEQ = name + "="; | |
ca = document.cookie.split(";"); | |
i = 0; | |
while (i < ca.length) { | |
c = ca[i]; | |
while (c.charAt(0) === " ") { | |
c = c.substring(1, c.length); | |
} | |
if (c.indexOf(nameEQ) === 0) { | |
return c.substring(nameEQ.length, c.length); | |
} | |
i++; | |
} | |
return null; | |
}; | |
deleteCookie = function(name) { | |
return setCookie(name, "", -1); | |
}; | |
show = function(selector) { | |
var ele, _i, _len, _ref, _results; | |
_ref = document.querySelectorAll(selector); | |
_results = []; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
ele = _ref[_i]; | |
_results.push(ele.style.display = 'block'); | |
} | |
return _results; | |
}; | |
hide = function(selector) { | |
var ele, _i, _len, _ref, _results; | |
_ref = document.querySelectorAll(selector); | |
_results = []; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
ele = _ref[_i]; | |
_results.push(ele.style.display = 'none'); | |
} | |
return _results; | |
}; | |
//fgnass.github.com/spin.js#v2.0.1 | |
!function(a,b){"object"==typeof exports?module.exports=b():"function"==typeof define&&define.amd?define(b):a.Spinner=b()}(this,function(){"use strict";function a(a,b){var c,d=document.createElement(a||"div");for(c in b)d[c]=b[c];return d}function b(a){for(var b=1,c=arguments.length;c>b;b++)a.appendChild(arguments[b]);return a}function c(a,b,c,d){var e=["opacity",b,~~(100*a),c,d].join("-"),f=.01+c/d*100,g=Math.max(1-(1-a)/b*(100-f),a),h=j.substring(0,j.indexOf("Animation")).toLowerCase(),i=h&&"-"+h+"-"||"";return l[e]||(m.insertRule("@"+i+"keyframes "+e+"{0%{opacity:"+g+"}"+f+"%{opacity:"+a+"}"+(f+.01)+"%{opacity:1}"+(f+b)%100+"%{opacity:"+a+"}100%{opacity:"+g+"}}",m.cssRules.length),l[e]=1),e}function d(a,b){var c,d,e=a.style;for(b=b.charAt(0).toUpperCase()+b.slice(1),d=0;d<k.length;d++)if(c=k[d]+b,void 0!==e[c])return c;return void 0!==e[b]?b:void 0}function e(a,b){for(var c in b)a.style[d(a,c)||c]=b[c];return a}function f(a){for(var b=1;b<arguments.length;b++){var c=arguments[b];for(var d in c)void 0===a[d]&&(a[d]=c[d])}return a}function g(a,b){return"string"==typeof a?a:a[b%a.length]}function h(a){this.opts=f(a||{},h.defaults,n)}function i(){function c(b,c){return a("<"+b+' xmlns="urn:schemas-microsoft.com:vml" class="spin-vml">',c)}m.addRule(".spin-vml","behavior:url(#default#VML)"),h.prototype.lines=function(a,d){function f(){return e(c("group",{coordsize:k+" "+k,coordorigin:-j+" "+-j}),{width:k,height:k})}function h(a,h,i){b(m,b(e(f(),{rotation:360/d.lines*a+"deg",left:~~h}),b(e(c("roundrect",{arcsize:d.corners}),{width:j,height:d.width,left:d.radius,top:-d.width>>1,filter:i}),c("fill",{color:g(d.color,a),opacity:d.opacity}),c("stroke",{opacity:0}))))}var i,j=d.length+d.width,k=2*j,l=2*-(d.width+d.length)+"px",m=e(f(),{position:"absolute",top:l,left:l});if(d.shadow)for(i=1;i<=d.lines;i++)h(i,-2,"progid:DXImageTransform.Microsoft.Blur(pixelradius=2,makeshadow=1,shadowopacity=.3)");for(i=1;i<=d.lines;i++)h(i);return b(a,m)},h.prototype.opacity=function(a,b,c,d){var e=a.firstChild;d=d.shadow&&d.lines||0,e&&b+d<e.childNodes.length&&(e=e.childNodes[b+d],e=e&&e.firstChild,e=e&&e.firstChild,e&&(e.opacity=c))}}var j,k=["webkit","Moz","ms","O"],l={},m=function(){var c=a("style",{type:"text/css"});return b(document.getElementsByTagName("head")[0],c),c.sheet||c.styleSheet}(),n={lines:12,length:7,width:5,radius:10,rotate:0,corners:1,color:"#000",direction:1,speed:1,trail:100,opacity:.25,fps:20,zIndex:2e9,className:"spinner",top:"50%",left:"50%",position:"absolute"};h.defaults={},f(h.prototype,{spin:function(b){this.stop();{var c=this,d=c.opts,f=c.el=e(a(0,{className:d.className}),{position:d.position,width:0,zIndex:d.zIndex});d.radius+d.length+d.width}if(e(f,{left:d.left,top:d.top}),b&&b.insertBefore(f,b.firstChild||null),f.setAttribute("role","progressbar"),c.lines(f,c.opts),!j){var g,h=0,i=(d.lines-1)*(1-d.direction)/2,k=d.fps,l=k/d.speed,m=(1-d.opacity)/(l*d.trail/100),n=l/d.lines;!function o(){h++;for(var a=0;a<d.lines;a++)g=Math.max(1-(h+(d.lines-a)*n)%l*m,d.opacity),c.opacity(f,a*d.direction+i,g,d);c.timeout=c.el&&setTimeout(o,~~(1e3/k))}()}return c},stop:function(){var a=this.el;return a&&(clearTimeout(this.timeout),a.parentNode&&a.parentNode.removeChild(a),this.el=void 0),this},lines:function(d,f){function h(b,c){return e(a(),{position:"absolute",width:f.length+f.width+"px",height:f.width+"px",background:b,boxShadow:c,transformOrigin:"left",transform:"rotate("+~~(360/f.lines*k+f.rotate)+"deg) translate("+f.radius+"px,0)",borderRadius:(f.corners*f.width>>1)+"px"})}for(var i,k=0,l=(f.lines-1)*(1-f.direction)/2;k<f.lines;k++)i=e(a(),{position:"absolute",top:1+~(f.width/2)+"px",transform:f.hwaccel?"translate3d(0,0,0)":"",opacity:f.opacity,animation:j&&c(f.opacity,f.trail,l+k*f.direction,f.lines)+" "+1/f.speed+"s linear infinite"}),f.shadow&&b(i,e(h("#000","0 0 4px #000"),{top:"2px"})),b(d,b(i,h(g(f.color,k),"0 0 1px rgba(0,0,0,.1)")));return d},opacity:function(a,b,c){b<a.childNodes.length&&(a.childNodes[b].style.opacity=c)}});var o=e(a("group"),{behavior:"url(#default#VML)"});return!d(o,"transform")&&o.adj?i():j=d(o,"animation"),h}); | |
; | |
window.soldsie || (window.soldsie = {}); | |
window.soldsie.crane = new Crane(); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment