Skip to content

Instantly share code, notes, and snippets.

@lucasferreira
Created February 29, 2012 14:20
Show Gist options
  • Save lucasferreira/1941168 to your computer and use it in GitHub Desktop.
Save lucasferreira/1941168 to your computer and use it in GitHub Desktop.
GitHub TreeSlider for case study
GitHub.TreeSlider = function () {
if (!Modernizr.history) return;
if ($("#slider").length == 0) return;
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) return;
var a = this;
a.enabled = !0, $("#slider a.js-slide-to, .breadcrumb a").live("click", function (b) {
return a.clickHandler(b)
}), $(window).bind("popstate", function (b) {
a.popStateHandler(b.originalEvent)
})
}, GitHub.TreeSlider.prototype = {
enabled: !1,
sliding: !1,
slideSpeed: 400,
frameForPath: function (a) {
return $('.frame[data-path="' + a + '"]')
},
frameForURL: function (a) {
return this.frameForPath(this.pathFromURL(a))
},
pathFromURL: function (a) {
if (!a) return;
var b = $(" .repo-tree").attr("data-ref"),
c = new RegExp("/(tree|blob)/" + (b || "[^/]+") + "/"),
d = a.split(c)[2] || "/";
return d.slice(d.length - 1, d.length) != "/" && (d += "/"), unescape(d)
},
scrollToBreadcrumb: function () {
this.visibleInBrowser(".breadcrumb:visible") || $(".breadcrumb:visible").scrollTo(50)
},
visibleInBrowser: function (a) {
var b = $(window).scrollTop(),
c = b + $(window).height(),
d = $(a).offset().top,
e = d + $(a).height();
return e >= b && d <= c
},
clickHandler: function (a) {
if (a.which == 2 || a.metaKey || a.ctrlKey) return !0;
if (this.sliding) return !1;
var b = a.currentTarget.href,
c = this.pathFromURL(b);
return window.history.pushState({
path: c
}, "", b), typeof _gaq != "undefined" && _gaq.push(["_trackPageview"]), this.slideTo(b), !1
},
popStateHandler: function (a) {
this.slideTo(location.pathname)
},
doneSliding: function () {
$("#slider").trigger("pageUpdate");
if (!this.sliding) return;
this.sliding = !1, $("#slider .frame-center").nextAll(".frame").hide(), $("#slider .frame-center").prevAll(".frame").css("visibility", "hidden");
var a = $(".frame-loading:visible");
a.length ? a.removeClass("frame-loading") : $("#slider").trigger("slid")
},
slideTo: function (a) {
var b = this.pathFromURL(a),
c = this.frameForPath(b),
d = $("#slider .frame-center").attr("data-path") || "";
c.is(".frame-center") || (d == "/" || b.split("/").length > d.split("/").length ? this.slideForwardTo(a) : this.slideBackTo(a))
},
slideForwardTo: function (a) {
debug("Sliding forward to %s", a);
var b = this.frameForURL(a);
if (b.length) this.slideForwardToFrame(b);
else {
var c = this.slideForwardToLoading();
this.loadFrame(a, function (a) {
c.replaceWith(a.find(".frame-center"))
})
}
},
slideForwardToFrame: function (a) {
if (this.sliding) return;
this.sliding = !0;
var b = this;
$("#slider .frame-center").after(a.css("marginLeft", 0)).addClass("frame").removeClass("frame-center").animate({
marginLeft: "-1200px"
}, this.slideSpeed, function () {
b.doneSliding()
}), this.makeCenterFrame(a), this.setFrameTitle(a), this.setFrameCanonicalURL(a)
},
slideForwardToLoading: function () {
var a = $(".frame-loading").clone();
return a.find("img").hide(), setTimeout(function () {
a.find("img").show()
}, 500), $(".frames").append(a), this.slideForwardToFrame(a), a
},
slideBackTo: function (a) {
debug("Sliding back to %s", a);
var b = this.frameForURL(a);
if (b.length) this.slideBackToFrame(b);
else {
var c = this.slideBackToLoading(),
d = this.pathFromURL(a);
this.loadFrame(a, function (a) {
c.replaceWith(a.find(".frame-center"))
})
}
},
slideBackToFrame: function (a) {
if (this.sliding) return;
this.sliding = !0, $("#slider .frame-center").before(a.css("marginLeft", "-1200px")).addClass("frame").removeClass("frame-center");
var b = this;
a.animate({
marginLeft: "0"
}, this.slideSpeed, function () {
b.doneSliding()
}), this.makeCenterFrame(a), this.setFrameTitle(a), this.setFrameCanonicalURL(a)
},
slideBackToLoading: function () {
var a = $(".frame-loading").clone();
return a.find("img").hide(), setTimeout(function () {
a.find("img").show()
}, 350), $(".frames").prepend(a.show()), slider.slideBackToFrame(a), a
},
makeCenterFrame: function (a) {
a.css("visibility", "visible").show().addClass("frame-center"), this.scrollToBreadcrumb(), $(".file-history-tease:visible").hide();
var b = $('.file-history-tease[data-path="' + a.attr("data-path") + '"]');
b.length > 0 && b.show(), $(document.body).attr("data-blob-contribs-enabled") == "yes" && (a.attr("data-path") == "/" ? ($(".last-commit, .commit.commit-tease").show(), $("#repo_details").show(), $(".repo-head .tabs").addClass("with-details-box")) : ($(".last-commit, .commit.commit-tease").hide(), $("#repo_details").hide(), $(".repo-head .tabs").removeClass("with-details-box")));
var c = $('.breadcrumb[data-path="' + a.attr("data-path") + '"]');
c.length > 0 && ($(".breadcrumb:visible").hide(), c.show());
var d = $('.announce[data-path="' + a.attr("data-path") + '"]');
$(".announce").fadeOut(), d.length > 0 && d.fadeIn();
var e = $(".js-ufo[data-path=" + a.attr("data-path") + "]");
$(".js-ufo").fadeOut(), e.length > 0 && e.fadeIn()
},
setFrameTitle: function (a) {
var b = a.attr("data-title");
b && (document.title = b)
},
setFrameCanonicalURL: function (a) {
var b = a.attr("data-permalink-url");
b && $("link[rel=permalink]").attr("href", b)
},
loadFrame: function (a, b) {
debug("Loading " + a + "?slide=1");
var c = this;
$.ajax({
url: a + "?slide=1",
cache: !1,
success: function (d) {
var e = $("<div>" + d + "</div>");
b.call(this, e);
var f = $("#slider");
f.trigger("slid"), f.find(".breadcrumb").hide().last().after(e.find(".breadcrumb")), f.find(".breadcrumb").trigger("pageUpdate");
var g = e.find(".file-history-tease");
f.prevAll(".file-history-tease").hide(), g.length && (f.prevAll(".last-commit, .commit.commit-tease").hide(), f.find(".breadcrumb:visible").after(g), g.trigger("pageUpdate"));
var h = c.frameForURL(a);
h.trigger("pageUpdate"), GitHub.CachedCommitDataPoller(50, h), GitHub.Blob.show(), c.setFrameTitle(h), c.setFrameCanonicalURL(h)
},
error: function () {
$("#slider .frame-center").html("<h3>Something went wrong.</h3>")
},
complete: function () {
c.sliding = !1
}
})
}
}, $(function () {
window.slider = new GitHub.TreeSlider
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment