Skip to content

Instantly share code, notes, and snippets.

@clintecker
Created January 6, 2011 19:01
Show Gist options
  • Save clintecker/768353 to your computer and use it in GitHub Desktop.
Save clintecker/768353 to your computer and use it in GitHub Desktop.
da-storefront-product.jsz
/*
** Copyright (C) 2011 Apple Inc.
** All rights reserved.
*/
function ItsDaScreenshotsController() {}
ItsDaScreenshotsController.prototype.domReady = function domReady() {
this.bindToElements("#screenshots")
};
iTSKit.registerController(ItsDaScreenshotsController);
function ItsDaScreenshots(a) {
this.element = a;
this.init()
}
ItsDaScreenshots.prototype.init = function () {
this.screenshotFrame = this.element.querySelector(".fullsize");
this.thumbs = this.element.querySelectorAll(".thumbs li");
this.altTag = this.element.querySelector(".fullsize img").getAttribute("alt");
this.thumbs.addEventListeners("click", bindAsEventListener(this, "thumbClicked"), false)
};
ItsDaScreenshots.prototype.thumbClicked = function (b) {
if (b.currentTarget.hasClassName("selected")) {
return
}
var c = b.currentTarget.getAttribute("data-fullsize");
var a = this.thumbs.index(b.currentTarget);
this.thumbs.removeClassNames("selected");
b.currentTarget.addClassName("selected");
this.thumbs.each(function (e, d) {
var f = e.querySelector("img");
if (d == a) {
f.setAttribute("aria-checked", "true")
} else {
f.setAttribute("aria-checked", "false")
}
}, this);
if (!b.currentTarget.hasClassName("added")) {
this.appendScreenshot(c, a);
b.currentTarget.addClassName("added")
}
defer(this, function () {
this.makeStatic(a)
})
};
ItsDaScreenshots.prototype.makeStatic = function (a) {
this.element.querySelectorAll(".fullsize img").removeClassNames("top");
this.screenshotFrame.querySelector('img[index="' + a + '"]').toggleClassName("top");
var b = this;
setTimeout(function () {
b.element.querySelectorAll(".fullsize img").removeClassNames("static");
document.querySelector("img.top").addClassName("static")
}, 500)
};
ItsDaScreenshots.prototype.appendScreenshot = function (b, a) {
var c = new Image();
c.src = b;
c.setAttribute("index", a);
c.setAttribute("alt", this.altTag);
var d = this;
this.screenshotFrame.appendChild(c)
};
iTSKit.registerElement(ItsDaScreenshots);
function iTSMoreTextController() {}
iTSMoreTextController.prototype.domReady = function domReady() {
if (!its.client.screenReaderRunning()) {
var a = this;
var b = document.querySelectorAll(".more-text");
b.each(function (e) {
var c = a.bindToElement(e);
if (c) {
var f = e.getAttribute("truncate-style");
var g = parseInt(e.getAttribute("truncate-length"), 10);
var d = e.getAttribute("truncate-override") == "true";
c.setOptions(f, g, d)
}
})
}
};
iTSMoreTextController.prototype.bindWithOptions = function (a, d, c, b) {
document.querySelectorAll(a).each(function (f) {
var e = this.bindToElement(f);
if (e) {
e.setOptions(d, c, b)
}
}, this)
};
iTSKit.registerController(iTSMoreTextController);
function iTSMoreText(a) {
this.element = a
}
iTSMoreText.prototype.setOptions = function (c, b, a) {
this.showType = c;
this.showLines = b;
this.overrideLink = a;
switch (this.showType) {
case "paragraph":
this.truncateForParagraph();
break;
case "same-paragraph":
this.truncateForSameParagraph();
break;
case "inline":
this.truncateForInline();
break;
case "multi-paragraph":
this.truncateMultiParagraph();
break;
case "list":
this.truncateForList();
break;
case "mixed":
this.truncateMixed();
break
}
};
iTSMoreText.prototype.handleEvent = function (a) {
a.stopPropagation();
a.preventDefault();
switch (this.showType) {
case "paragraph":
this.showAllForParagraph();
break;
case "inline":
this.showAllForInline();
break;
case "multi-paragraph":
this.showAllMultiParagraph();
break;
case "list":
this.showAllForList();
break;
case "mixed":
this.showAllForMixed();
break;
default:
this.showAllForParagraph();
break
}
};
iTSMoreText.prototype.truncateForParagraph = function () {
var a = this.paragraphLineClamp();
if (a < 100) {
if (!this.element.parentNode.querySelector("p ~ a.goto-link")) {
this.element.insertAfter(this.createMoreLink(this.createMoreLink()));
this.moreLink = this.element.parentNode.querySelector("a.more-link");
this.moreLink.addEventListener("click", this, false)
}
this.element.style.display = "-webkit-box";
this.element.style.webkitBoxOrient = "vertical";
this.element.style.overflow = "hidden";
this.element.style.webkitLineClamp = a + "%"
}
};
iTSMoreText.prototype.truncateMultiParagraph = function () {
var b = this.element.querySelectorAll("p").length > 1;
if (!b) {
var a = this.paragraphLineClamp()
} else {
var a = 99
}
if (a < 100) {
if (this.element.tagName == "DIV") {
this.element.querySelectorAll("p").each(function (d, c) {
if (c > 0) {
d.hide()
} else {
d.insertAfter(this.createMoreLink(this.createMoreLink()));
this.moreLink = d.parentNode.querySelector("a.more-link");
this.moreLink.addEventListener("click", this, false);
d.style.display = "-webkit-box";
d.style.webkitBoxOrient = "vertical";
d.style.overflow = "hidden";
d.style.webkitLineClamp = a + "%"
}
}, this)
}
}
};
iTSMoreText.prototype.truncateForInline = function () {
var a = this.element.querySelectorAll("p");
if (a.length > 0) {
a.each(function (d, c) {
if (c > 0) {
d.hide()
} else {
if (d.lineCount() > this.showLines) {
this._truncateForInlineHelper(d)
}
}
}, this)
} else {
var b = this.element.querySelector(".truncate");
if (!b) {
b = this.element.firstElementChild
}
if (b && Math.floor(b.lineCount()) > this.showLines) {
this._truncateForInlineHelper(b)
}
}
};
iTSMoreText.prototype._truncateForInlineHelper = function (c) {
if (this.element.getComputedStyleProperty("position") == "static") {
var a = this.element.parentByClassName("wide-user-lockup");
if (!a) {
this.element.style.position = "relative"
}
}
c.insertAfter(this.createMoreLink());
this.moreLink = c.parentNode.querySelector("a.more-link");
this.moreLink.addEventListener("click", this, false);
c.style.display = "-webkit-box";
c.style.webkitBoxOrient = "vertical";
c.style.overflow = "hidden";
c.style.webkitLineClamp = Math.ceil((this.showLines / c.lineCount()) * 100) + "%";
var b = this.createMoreLink();
b.className = "more-link-placeholder";
c.appendChild(b)
};
iTSMoreText.prototype.truncateForSameParagraph = function () {
var a = ((this.showLines) / this.element.lineCount()) * 100;
if (a < 100) {
if (!this.element.parentNode.querySelector("p ~ a.goto-link")) {
this.element.appendChild(this.createMoreLink(this.createMoreLink()));
this.moreLink = this.element.parentNode.querySelector("a.more-link");
this.moreLink.addEventListener("click", this, false)
}
this.element.style.display = "-webkit-box";
this.element.style.webkitBoxOrient = "vertical";
this.element.style.overflow = "hidden";
this.element.style.webkitLineClamp = this.showLines
}
};
iTSMoreText.prototype.paragraphLineClamp = function () {
if (this.element.tagName != "DIV") {
return ((this.showLines + 1) / this.element.lineCount()) * 100
} else {
return ((this.showLines + 1) / this.element.querySelector("p").lineCount()) * 100
}
};
iTSMoreText.prototype.showAllForParagraph = function () {
if (!this.overrideLink) {
this.element.style.webkitLineClamp = "100%"
}
if (!this.overrideLink && this.moreLink) {
this.moreLink.hide()
}
};
iTSMoreText.prototype.showAllForInline = function () {
if (this.element.querySelectorAll("p").length > 0) {
this.showAllMultiParagraph()
} else {
var a = this.element.querySelector(".truncate");
if (!a) {
a = this.element.firstElementChild
}
a.style.webkitLineClamp = "100%";
if (!this.overrideLink && this.moreLink) {
this.moreLink.hide()
}
}
};
iTSMoreText.prototype.showAllMultiParagraph = function () {
if (!this.overrideLink) {
this.element.querySelector("p").style.webkitLineClamp = "100%"
}
if (!this.overrideLink) {
this.element.querySelectorAll("p").each(function (a) {
a.show()
})
}
if (!this.overrideLink && this.moreLink) {
this.moreLink.hide()
}
};
iTSMoreText.prototype.showAllForMixed = function () {
this.moreLink.hide();
var a = this.element.querySelector(".fixed-truncate-area");
if (a) {
a.style.height = "";
a.style.overflow = ""
}
this.element.querySelectorAll("p, li, ul").show();
this.element.querySelectorAll("li").each(function (b) {
b.style.height = ""
})
};
iTSMoreText.prototype.truncateForList = function () {
var b = this.showLines - 1;
if (this.element.children.length > b + 2) {
this.element.children.each(function (d, c) {
if (c > b) {
d.hide()
}
});
var a = document.createElement("li");
a.appendChild(this.createMoreLink());
this.element.children[b].insertAfter(a);
this.moreLink = this.element.querySelector("a.more-link");
this.moreLink.addEventListener("click", this, false)
}
};
iTSMoreText.prototype.adjustHeight = function () {
var d = this.element.querySelectorAll("p, ul");
var a = 0;
var e = false;
if (d.length > 0) {
var n = d[0].getComputedStyleProperty("line-height").toInt();
var j = this.showLines * n
}
for (var h = 0; h < d.length; h++) {
var g = d[h];
var o = g.offsetHeightPlusMargin() + g.paddingHeight();
if (e) {
g.hide()
} else {
if (a + o <= j) {
a += o
} else {
var m = g;
if (g.tagName == "UL") {
g.querySelectorAll("li").each(function (i) {
if (e) {
i.hide()
} else {
var k = i.offsetHeightPlusMargin() + i.paddingHeight();
if (a + k > j) {
m = i;
e = true
} else {
a += k
}
}
})
}
e = true;
a += m.getComputedStyleProperty("margin-top").toInt() + m.getComputedStyleProperty("padding-top").toInt();
var c = 0;
for (var f = 0; f < m.lineCount(); f++) {
if (a + ((f + 1) * n) < j) {
c++
} else {
break
}
}
if (c == 0) {
m.hide()
} else {
m.style.webkitBoxOrient = "vertical";
m.style.overflow = "hidden";
if (m.tagName == "LI") {
m.style.height = (n * c) + "px"
} else {
m.style.webkitLineClamp = ((c / m.lineCount()) * 100) + "%"
}
}
}
}
}
var l = this.element.querySelector(".fixed-truncate-area");
if (l) {
if (!e) {
var b = this.createMoreLinkDiv();
b.style.visibility = "hidden";
this.element.appendChild(b)
}
l.style.height = j + "px";
l.style.overflow = "hidden"
}
return e
};
iTSMoreText.prototype.truncateMixed = function () {
this.showLines++;
var a = this.adjustHeight();
this.element.style.display = "-webkit-box";
this.element.style.webkitBoxOrient = "vertical";
this.element.style.overflow = "hidden";
if (a) {
this.element.appendChild(this.createMoreLinkDiv());
this.moreLink = this.element.querySelector("a.more-link");
this.moreLink.addEventListener("click", this, false)
}
};
iTSMoreText.prototype.showAllForList = function () {
this.moreLink.parentByTagName("li").remove();
this.element.children.show()
};
iTSMoreText.prototype.createMoreLinkDiv = function () {
var a = document.createElement("div");
a.appendChild(this.createMoreLink());
return a
};
iTSMoreText.prototype.createMoreLink = function () {
var a = document.createElement("a");
a.className = "more-link";
a.href = "#more-link";
a.setAttribute("ontouchstart", "");
a.innerText = this.localize("Js.TextTruncation.More");
return a
};
iTSKit.registerElement(iTSMoreText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment