Skip to content

Instantly share code, notes, and snippets.

@pajtai
Created December 25, 2012 00:27
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 pajtai/4371152 to your computer and use it in GitHub Desktop.
Save pajtai/4371152 to your computer and use it in GitHub Desktop.
DISQUS.addBlocks("theme")(function (d) {
d.blocks.letUsKnow = function (f, c) {
var a = new d.Builder,
h = DISQUS.extend({}, f, c);
with(h) return a.put('<a href="https://www.surveymonkey.com/s/GHK872T" target="_blank">'), a.put(gettext("Let us know.")), a.put("</a>"), a.compile()
};
d.blocks.relatedThread = function (f, c) {
var a = new d.Builder,
h = DISQUS.extend({}, f, c);
with(h) return a.put(' <li class="discovery-unit"> <div class="discovery-heading-wrapper"> <h3 title="'), a.put((a.esc || function (a) {
return a
})(thread.title)),
a.put('"> <a data-role="discovery-thread-title" class="title publisher-anchor-color" '),
function () {
var e = {};
d.extend(e, c);
d.extend(e, {});
a.put(d.renderBlock("linkAttributes", e))
}(), a.put("> "), a.put(thread.title), a.put(' </a> </h3> </div> <ul class="meta"> <li class="comments"> <a '),
function () {
var e = {};
d.extend(e, c);
d.extend(e, {});
a.put(d.renderBlock("linkAttributes", e))
}(), a.put("> "),
thread.posts === 1 ? (a.put(" "), a.put(gettext("1 comment")), a.put(" ")) : thread.posts >= 0 && (a.put(" "), a.put(d.interpolate(gettext("%(numPosts)s comments"), {
numPosts: thread.posts
})), a.put(" ")), a.put(' </a> </li> <li class="time">'), a.put((a.esc || function (a) {
return a
})(thread.createdAgo)), a.put('</li> </ul> <a class="top-comment" '),
function () {
var e = {};
d.extend(e, c);
d.extend(e, {});
a.put(d.renderBlock("linkAttributes",
e))
}(), a.put(' data-role="discovery-top-comment"> <img src="'), a.put((a.esc || function (a) {
return a
})(urls.avatar.generic)), a.put('" alt="'), a.put(gettext("Default user")), a.put('" data-role="discovery-avatar"> <p><span class="user" data-role="discovery-top-comment-author">'), a.put('</span> &#8212; <span data-role="discovery-top-comment-snippet">'), a.put("</span></p> </a> </li>"), a.compile()
};
d.blocks.linkAttributes = function (f, c) {
var a = new d.Builder,
h = DISQUS.extend({},
f, c);
with(h) return a.put('href="'), a.put((a.esc || function (a) {
return a
})(thread.link)), a.put('#disqus_thread" data-redirect="'), a.put((a.esc || function (a) {
return a
})(thread.redirectUrl)), a.put('"'), a.compile()
};
d.blocks.relatedThreads = function (f, c) {
var a = new d.Builder,
h = DISQUS.extend({}, f, c);
with(h) return a.put('<div id="discovery-main"> <div id="discovery-note" style="display:none;"> <div class="alert"> '), a.put(d.interpolate(gettext("The new DISQUS Discovery box helps you find other vibrant discussions on the communities you love. Feedback? %(letUsKnow)s"), {
letUsKnow: d.renderBlock("letUsKnow")
})), a.put(' </div> </div> <header> <div id="discovery-options"> <span class="publisher-anchor-color"><a href="#" id="discovery-help" data-action="discovery-help">'), a.put(gettext("What's this?")), a.put('</a></span> <a href="#" id="discovery-close" data-action="discovery-close" title="'), a.put(gettext("Close this box")), a.put('">\u00d7</a> </div> <h2>'), a.put(d.interpolate(gettext("Also on %(forumName)s"), {
forumName: d.renderBlock("forumName", forum)
})), a.put('</h2> </header> <section> <ul id="discovery-units"> '), a.put(" </ul> </section></div>"), a.compile()
};
d.blocks.forumName = function (f, c) {
var a = new d.Builder,
h = DISQUS.extend({}, f, c);
with(h) return a.put("<strong>"), a.put((a.esc || function (a) {
return a
})(name)), a.put("</strong>"), a.compile()
}
});
(function (d) {
var f = d.document,
c = d.DISQUS;
c.registerActions = function () {
c.define("discovery.views", function () {
return {
load: function (a) {
var d = c.discovery.views,
e = d.RelatedThreadView = Backbone.View.extend({
events: {
"click [data-redirect]": "swapLink"
},
dtplBlock: "relatedThread",
initialize: function (a) {
this.snippetLimit = a.snippetLimit;
this.model.on("change:topComment", this.renderTopComment, this)
},
swapLink: function (a) {
a = a.target || a.srcElement;
a.tagName.toUpperCase() == "A" && a.getAttribute("data-redirect") || (a = $(a).closest("a[data-redirect]")[0]);
var b = a;
b.setAttribute("data-href", b.getAttribute("href"));
b.setAttribute("href", b.getAttribute("data-redirect"));
_.delay(function () {
b.setAttribute("href", b.getAttribute("data-href"))
}, 100)
},
renderAvatar: function (a) {
var b = this.$el.find("[data-role=discovery-avatar]");
b.attr("src", a.avatar.cache);
b.attr("alt", this.authorName(a));
return this
},
authorName: function (a) {
return a.name || a.username
},
renderCommentSnippet: function (j) {
var b = this.$el.find("[data-role=discovery-top-comment]");
if (j.author) {
var c = this.authorName(j.author);
b.find("[data-role=discovery-top-comment-author]").text(c)
}
j = _.strip(j.message);
j = _.truncate(j, this.snippetLimit, "\u2026");
c = b.find("[data-role=discovery-top-comment-snippet]");
c.text(j);
b.css({
display: "block"
});
a.lineTruncate(c, {
lines: 3,
ellipsis: !0
});
return this
},
renderTopComment: function (a, b) {
this.renderAvatar(b.author);
this.renderCommentSnippet(b);
this.trigger("resize");
return this
},
truncateHeading: function () {
var j = this.$el.find("[data-role=discovery-thread-title]");
a.lineTruncate(j, {
lines: 2,
ellipsis: !0
})
},
render: function () {
this.setElement(c.renderBlock(this.dtplBlock, {
thread: _.extend(this.model.toJSON(), {
createdAgo: moment(c.assureOffset(this.model.get("createdAt")), c.ISO_8601).fromNow()
})
}));
return this
}
});
d.RelatedThreadCollectionView = Backbone.View.extend({
events: {
"click [data-action=discovery-close]": "close",
"click [data-action=discovery-help]": "showHelp"
},
dtplBlock: "relatedThreads",
initialize: function (a) {
this.snippetLimit = a.snippetLimit
},
close: function (a) {
a.preventDefault();
this.remove();
this.trigger("resize", 0)
},
showHelp: function (a) {
a.preventDefault();
$(a.currentTarget).hide();
this.$el.find("#discovery-note").show();
this.trigger("resize")
},
getTopComment: function (a) {
var b = this;
c.api.call("discovery/listTopPost.json", {
method: "GET",
data: {
thread: a
},
success: function (a) {
a.response.length !== 0 && _.each(a.response, function (a) {
b.collection.get(a.thread).set("topComment", a)
})
}
});
return b
},
resize: _.debounce(function () {
this.trigger("resize")
}, 500),
render: function () {
this.$el.append(c.renderBlock(this.dtplBlock, {
forum: this.collection.mainThread.get("forum")
}));
var a = [];
this.collection.each(function (b) {
b = new e({
model: b,
snippetLimit: this.snippetLimit
});
a.push(b);
b.on("resize", this.resize, this);
b.render();
this.$el.find("section > ul").append(b.el)
}, this);
_.each(a, function (a) {
a.truncateHeading()
});
this.getTopComment(this.collection.pluck("id"));
return this
}
});
return d
}
}
});
c.define("discovery.collections", function () {
return {
load: function (a, d) {
var e = c.discovery.collections;
e.RelatedThreadCollection = Backbone.Collection.extend({
model: a.RelatedThread,
url: function () {
return c.api.getURL("discovery/listRelated.json")
},
initialize: function (a, b) {
this.mainThread = b.thread;
this.limit = b.limit;
this.variant = b.variant
},
fetch: function (a) {
var b = {};
b.data = {
thread: this.mainThread.get("id")
};
b.parse = !1;
_.extend(b, a);
Backbone.Collection.prototype.fetch.call(this, b)
},
addBandit: function (a) {
if ((a = a.response) && a.bandit) this.bandit = a.bandit
},
getBanditJSON: _.memoize(function () {
return c.JSON.stringify(this.bandit)
}),
parse: function (a) {
this.addBandit(a);
var b = [],
e = a.response || [];
if (e.results) e = e.results;
var f = d.log;
f("Results (", e.length, "):", e);
for (var g = 0, m = e.length; g < m && b.length < this.limit; g++) if (a = e[g].thread || e[g], a.id != this.mainThread.id) if (a.forum.id != this.mainThread.get("forum").id) f("Thread", a.id, "does not have same forum (", a.forum.id, ") as source thread's:", this.mainThread.get("forum").id);
else if (a.posts === 0) f("Thread", a.id, "does not have any comments.");
else if (a.title.indexOf("http://") === 0) f("Thread", a.id, "has a title that starts with http.");
else {
var n = e[g].signedUrl ? "http://redirect.disqus.com/url" : a.link + "#redirect",
r = {
url: e[g].signedUrl,
thread: a.id,
forum: _.isObject(a.forum) ? a.forum.id : a.forum,
zone: "internal_discovery",
imp: c.juggler.impId,
source_thread_id: this.mainThread.id
};
if (this.bandit) r.bandit = this.getBanditJSON();
if (this.variant) r.variant = this.variant;
a.redirectUrl = c.serialize(n, r);
a.score = {
score: e[g].score,
k: e[g].k
};
b.push(a)
}
f("Total results:", b.length);
return b
}
});
return e
}
}
});
c.define("discovery.models", function () {
return {
load: function () {
var a = c.discovery.models,
d = a.Thread = Backbone.Model.extend({
defaults: {
author: null,
category: null,
createdAt: null,
forum: null,
identifiers: [],
ipAddress: null,
isClosed: !1,
isDeleted: !1,
link: null,
message: null,
slug: null,
title: null,
userSubscription: !1,
posts: 0,
reactions: 0,
likes: 0,
dislikes: 0,
userScore: 0
},
url: c.api.getURL("threads/details"),
parse: function (a) {
return a.response
}
});
a.RelatedThread = d.extend({
defaults: _.extend({}, d.prototype.defaults, {
redirectUrl: null,
topComment: null
})
});
return a
}
}
});
c.define("discovery.helpers",
function (a, d) {
var e = null,
j = !1,
b = !1,
i = function () {};
a.console && (i = function () {
b && (a.console.log.apply ? a.console.log.apply(a.console, arguments) : a.console.log(_.toArray(arguments).join(" ")))
});
var l = function (a) {
if (a === d) return b;
b = !! a
}, g = function (a) {
if (a === d) return j;
j = !! a
}, m = function (a, b, c) {
if (e) if (e.length) e.enabled(a) && b();
else {
var d = function () {
e.enabled(a) && b();
c && e.off("reset", d)
};
return void e.on("reset", d)
}
};
return {
config: function (a) {
e = a;
g(!0);
m("discovery_next:logging", function () {
l(!0)
})
},
allowLog: l,
ifSwitch: m,
log: i,
allowLineTruncate: g,
lineTruncate: function (b, d) {
function e() {
return i.scrollHeight - i.offsetHeight > 0.2 * t
}
function g() {
k.lastChild && !_.contains(["...", "\u2026"], k.lastChild.nodeValue) && (l = k.appendChild(a.document.createTextNode(" " + m)), e() && (k.removeChild(l), k.removeChild(k.lastChild), g()))
}
if (j) {
var h = c.logError || function () {};
if (!b.closest("body").length) return void h("lineTruncate called on el not on DOM");
if (b.text().length < 1) return void h("lineTruncated called on empty el");
if (_.any(b.children(),
function (a) {
return a.nodeType !== 3
})) return void h("lineTruncate called on non-flat el");
var k = b[0],
i = k;
if (b.css("display") !== "block") for (; i.parentNode;) if (i = i.parentNode, $(i).css("display") === "block") break;
var t = parseFloat(b.css("font-size"), 10);
if (e()) {
var d = d || {}, h = d.lines || 1,
m = d.ellipsis,
l, o = b.text();
if (o.length) {
var s = b.width() / t,
h = parseInt(s * h, 10),
o = o.split(/\s/),
s = 0;
b.empty();
for (var q = 0, v = o.length; q < v; q++) {
s += o[q].length + 1;
if (s >= h) break;
k.appendChild(f.createTextNode(" " + o[q]))
}
if (e()) {
do l = k.removeChild(k.lastChild);
while (e())
} else {
do l = k.appendChild(f.createTextNode(" " + o[q++]));
while (!e() && q < v);
k.removeChild(l)
}
m && (_.isString(m) || (m = "\u2026"), g())
}
}
}
}
}
});
c.define("discovery", function () {
return this.overwrites({
init: function (a, d) {
function e(d) {
l("discovery_next:dark_promoted", function () {
g("Initiating dark request to discovery/listPromoted");
c.api.call("discovery/listPromoted.json", {
data: {
thread: a.thread.id
}
})
});
p.mainThread = d;
p.fetch({
success: j,
error: b
});
p.on("error", b)
}
function j(c) {
if (c.length < 2) return void b({
display: !1
});
c.length % 2 === 1 && c.pop();
c = new w({
el: f.getElementById(a.containerId),
collection: c,
snippetLimit: m
});
d(c);
b({
display: !0
})
}
function b(b) {
b = b || {};
if (!u) {
u = !0;
var d = c.juggler.client("juggler");
if (d) {
var e = {
major_version: "next_internal",
internal_organic: p.length,
external_organic: 0,
promoted: 0,
display: !! b.display
};
p.bandit && _.extend(e, {
bandit: c.JSON.stringify(p.bandit)
});
a.isExperiment && _.extend(e, {
variant: a.variant
});
g("Juggler init_discovery", e);
d.emit("init_discovery", e);
l("dark_jester",
function () {
c.juggler.client("jester", !0).emit("init_discovery", e)
})
}
}
}
var i = c.discovery.helpers,
l = i.ifSwitch,
g = i.log;
i.config(a.switches);
var m = 100,
n = c.discovery.models.load(),
r = c.discovery.collections.load(n, i),
i = c.discovery.views.load(i),
n = n.Thread,
w = i.RelatedThreadCollectionView,
p = new r.RelatedThreadCollection(null, {
limit: 4,
variant: !! a.isExperiment && a.variant
});
a.threadId ? (g("Discovery: Making call to threads/details"), (new n).fetch({
data: {
thread: a.threadId,
forum: a.forum,
related: "forum"
},
success: e
})) : e(new n(_.extend(a.thread, {
forum: a.forum
})));
var u = !1
}
})
})
};
c.runThemeScript = c.registerActions
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment