Skip to content

Instantly share code, notes, and snippets.

@h2o64
Created October 25, 2015 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h2o64/969de11d9483392c8bb1 to your computer and use it in GitHub Desktop.
Save h2o64/969de11d9483392c8bb1 to your computer and use it in GitHub Desktop.
dedicated-availability-2.5.0.min.js
window.addEvent("domready", function() {
function e(e) {
var t = {
second: -1,
type: "",
value: -1
}
return e && (e.match(/^(\d+)d(-(low|high))?$/i) ? (t.second = 60 * parseInt(RegExp.$1, 10) * 60 * 24, t.type = RegExp.$3 || "") : e.match(/^(\d+)h(-(low|high))?$/i) ? (t.second = 60 * parseInt(RegExp.$1, 10) * 60, t.type = RegExp.$3 || "") : e.match(/^(\d+)m(-(low|high))?$/i) ? (t.second = 60 * parseInt(RegExp.$1, 10), t.type = RegExp.$3 || "") : e.match(/^(\d+)s(-(low|high))?$/i) && (t.second = parseInt(RegExp.$1, 10), t.type = RegExp.$3 || "")), -1 !== t.second && (t.value = t.second, "low" === t.type ? t.value-- : "high" === t.type && t.value++), t
}
function t(e, t) {
var a, n, i, o = e.getProperty("data-extra-params") || null
return s ? (a = e.getProperty("data-cgi") || "dedies", n = "hg" === a || o && o.match(/orderType=b?hg/) ? "config" : "hard", i = l.dedicatedOrderUrl + a + ".cgi?" + n + "=" + t) : i = l.dedicatedOrderUrl + "?reference=" + t, o && (o.match(/^(\?|&)/) && (o = o.substring(1)), i += "&" + o), i
}
function a(e) {
return void 0 !== window.QTYBox ? new QTYBox("input-quantity-" + e, {
initValue: 1,
incValue: 1,
minValue: d,
maxValue: p
}) : new Element("input", {
type: "number",
name: "input-quantity-" + e,
step: 1,
min: d,
max: p,
value: 1
})
}
function n(e, t) {
return new Element("a", {
href: e,
"class": "order-button",
events: {
click: function() {
var e, a = $$("input[name='input-quantity-" + t + "']")
if (a.length > 0 && a[0].get("value").match(/(\d{1,3})/) && (e = parseInt(RegExp.$1, 10), d > e ? e = d : e > p && (e = p)), void 0 !== e) {
var n = this.retrieve("href")
n || this.store("href", this.getProperty("href")), this.setProperty("href", this.retrieve("href") + (s ? "&dedicatedQuantity=" : "&quantity=") + e)
}
return !0
}
}
}).grab(new Element("span", {
html: l.i18n.order
}))
}
function i(i) {
function o(e, t, a) {
var n = {
src: l.imagesFolder + t
}
a && (n.alt = a), e.empty().grab(new Element("img", n))
}
function r(e) {
o(e, l.soonIcon ? l.soonIcon : u)
}
function s(e) {
e.empty().grab(new Element("span", {
html: l.soonText
}))
}
function c(e) {
l.soonText ? s(e) : r(e)
}
function d(e) {
var t, a, n, i, o = new Date,
r = new Date,
s = new Date,
c = l.i18n.dateAt
return r.setTime(r.getTime() - 864e5), s.setTime(s.getTime() - 1e3 * e), o.getDate() === s.getDate() ? c = l.i18n.todayAt : r.getDate() === s.getDate() && (c = l.i18n.yesterdayAt), i = s.getDate(), n = s.getMonth() + 1, t = s.getHours(), a = s.getMinutes(), i = 1 === (i + "").length ? "0" + i : i, n = 1 === (n + "").length ? "0" + n : n, t = 1 === (t + "").length ? "0" + t : t, a = 1 === (a + "").length ? "0" + a : a, c.substitute({
hours: t,
minutes: a,
year: s.getFullYear(),
date: i,
month: n
})
}
function p(t, a) {
var n, i, o = t.getProperty("data-dc"),
r = t.getProperty("data-availability"),
l = []
return o && (l = o.split(/[,;\|\s\t]+/)), a.zones.each(function(t) {
i = e(t.availability), (!r || r.match(/^\d+$/) && i.second === parseInt(r, 10) || r.match(/^(\d+)\+$/) && i.second >= parseInt(RegExp.$1, 10) || r.match(/^(\d+)\-$/) && i.second <= parseInt(RegExp.$1, 10)) && (1 === l.length ? t.zone === l[0] && (t.$object = i, n = t) : (0 === l.length || l.contains(t.zone)) && -1 !== i.value && (!n || i.value < n.$object.value) && (t.$object = i, n = t))
}), n
}
var g = $$(".zone-order-button-dedicated-availability[data-ref]"),
y = {}
i.each(function(e) {
y[e.reference] = e
}), g.length > 0 && g.each(function(e) {
var t = e.getProperty("data-ref")
t && y[t] || c(e)
})
var h = $$(".zone-dedicated-availability")
h.length > 0 && (i.each(function(e) {
"array" === typeOf(e.zones) && h.each(function(i) {
if (i.getProperty("data-ref") === e.reference) {
var r = i.getProperty("data-actions").replace(/\s|\t/g, ""),
s = p(i, e)
if (r = r.split(","), r.contains("icon") && (l.forceGlobalUnavailability !== !0 && s && -1 !== s.$object.second ? o(i, "icn-" + s.$object.second + "s" + ("" !== s.$object.type ? "-" + s.$object.type : "") + ".png") : c(i)), r.contains("flag") && (l.forceGlobalUnavailability !== !0 && s && -1 !== s.$object.second ? o(i, "flag-" + s.zone + ".png", s.zone) : o(i, "flag-unavailable.png", s.zone)), r.contains("class") && (i.store("zoneClass", !0), l.forceGlobalUnavailability !== !0 && s && -1 !== s.$object.second ? i.removeClass("dedicated-not-available").addClass("dedicated-available") : i.removeClass("dedicated-available").addClass("dedicated-not-available")), r.contains("inputQuantity") && (l.forceGlobalUnavailability !== !0 && s ? i.empty().grab(a(e.reference)) : c(i)), r.contains("orderButton") && (l.forceGlobalUnavailability !== !0 && s ? i.empty().grab(n(t(i, e.reference), e.reference)) : c(i)), (l.forceGlobalUnavailability === !0 || !s || s && -1 === s.$object.second) && r.contains("refUnavailable")) {
i.getElements(".show-on-ref-unavailable").each(function(e) {
e.setStyle("display", "")
}), i.getElements(".hide-on-ref-unavailable").each(function(e) {
e.destroy()
})
var u = i.getElement(".elapsed-time-since-last-delivery")
u && new Request.JSONP({
url: "https://ws.ovh." + ("CA" === l.superMasterBillingCountry ? "ca" : "com") + "/dedicated/r2/ws.dispatcher/getElapsedTimeSinceLastDelivery",
data: {
params: JSON.encode({
gamme: e.reference
})
},
onSuccess: function(e) {
var t = parseInt(e.answer, 10)
if (isNaN(t) || 0 > t) u.set("html", u.getElement(".victim-success").get("html"))
else {
var a = u.getElement(".out-of-stock > span")
a.set("html", a.get("html").substitute({
value: d(t)
})).setStyle("display", ""), u.set("html", u.getElement(".out-of-stock").get("html"))
}
}
}).send()
}
}
})
}), h.each(function(e) {
var t = e.getProperty("data-actions").replace(/\s|\t/g, "")
t = t.split(","), t.contains("class") && !e.retrieve("zoneClass") && e.addClass("dedicated-not-available")
}))
}
function o() {
var e = $$(".zone-icon-dedicated-availability[data-ref]"),
i = $$(".zone-order-button-dedicated-availability[data-ref]"),
o = $$(".zone-input-quantity-dedicated-availability[data-ref]")
e.length > 0 && e.each(function(e) {
e.empty().grab(new Element("span", {
"class": "icon-disponibility-temporary-unavailable",
html: "<span>-</span>"
}))
}), o.length > 0 && o.each(function(e) {
e.empty().grab(a(e.getProperty("data-ref")))
}), i.length > 0 && i.each(function(e) {
var a = e.getProperty("data-ref"),
i = t(e, a)
e.empty().grab(n(i, a))
})
var r = new Element("div", {
styles: {
position: "fixed",
top: "15px",
textAlign: "center",
width: "100%",
zIndex: 1e4
}
}).grab(new Element("div", {
styles: {
display: "inline-block",
whiteSpace: "nowrap",
border: "1px solid #ebccd1",
backgroundColor: "#f2dede",
color: "#b94a48",
padding: "5px 10px",
borderRadius: "4px",
cursor: "pointer",
fontSize: "13px",
marginLeft: "auto",
marginRight: "auto",
boxShadow: "1px 1px 12px #555"
},
title: "Click to close",
events: {
click: function() {
r.destroy()
}
},
html: "Unable to load dedicated server disponibilty! If the problem continues, please contact support."
}).grab(new Element("span", {
styles: {
fontWeight: "bold",
color: "#b94a48",
fontSize: "13px",
marginRight: "5px"
},
html: "Warn:"
}), "top"))
$(document.body).grab(r),
function() {
r.destroy()
}.delay(5e3)
}
function r() {
new Request.JSON({
url: l.urlBase + "/js/dedicatedAvailability/availability-data" + ("CA" === l.superMasterBillingCountry ? "-ca" : "") + ".json",
method: "get",
noCache: !0,
onSuccess: function(e) {
e && "array" === typeOf(e.availability) ? i(e.availability) : o()
},
onFailure: o,
onException: o
}).send()
}!window.typeOf && window.$type && (window.typeOf = window.$type)
var l, s, c = $("dedicatedAvailabilityJs"),
d = 1,
p = 5,
u = "icn-soon.png"
c && (l = JSON.decode(c.get("html")), s = l.dedicatedOrderUrl.match(/\.xml$/) ? !1 : !0, l.skipWs ? r() : new Request.JSONP({
url: "https://ws.ovh." + ("CA" === l.superMasterBillingCountry ? "ca" : "com") + "/dedicated/r2/ws.dispatcher/getAvailability2",
noCache: !0,
onSuccess: function(e) {
e && e.answer && "array" === typeOf(e.answer.availability) ? i(e.answer.availability) : r()
},
onFailure: r,
onException: r
}).send())
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment