Skip to content

Instantly share code, notes, and snippets.

@mattgrill
Created October 30, 2014 00:51
Show Gist options
  • Save mattgrill/95af3b019aac342d8c09 to your computer and use it in GitHub Desktop.
Save mattgrill/95af3b019aac342d8c09 to your computer and use it in GitHub Desktop.
'use strict';
/**
* @file
* Comments JS for EW.com:
* - Attachs comments object to ew_com global.
*/
/* globals window, jQuery, ew_com, DISQUSWIDGETS, DISQUS */
// Only if jQuery is defined
if(typeof jQuery !== 'undefined') {
(function ($, ew_com) {
ew_com.comments = {};
ew_com.comments.disqus = {};
ew_com.comments.disqus.fld = true;
ew_com.comments.disqus.ew_dsq = $('#disqus_thread');
ew_com.comments.disqus.params = {
'shortName' : 'ewm',
'publicAPIKey' : 'HOdGv9alocnzL2wUrX7O9Znjo7ND5pyQ1uoctRr7dTVzYEpntdFsMDwJUm3fhjWk'
};
ew_com.comments.disqus.refresh = function (data) {
/**
* Creates disqus_identifier and disqus_url variables.
* @param {Object} data (required) the JSON representation of the gallery slide.
*/
var timed_disqus;
if (data) {
window.disqus_identifier = data.comments.disqus_identifier === null ?
ew_com.advertising.variables.content_type + '_' + ew_com.advertising.variables.article_id :
data.comments.disqus_identifier;
window.disqus_url = [
window.location.origin.replace('www.ew.local', 'www.ew.com'),
window.location.pathname,
'/',
window.disqus_identifier
].join('');
window.disqus_publicKey = ew_com.comments.disqus.params.publicAPIKey;
window.disqus_shortName = ew_com.comments.disqus.params.shortName;
}
// If user is clicking too fast clear the previous timeout.
clearTimeout(timed_disqus);
// Adding a delay to preventing overloading
timed_disqus = setTimeout(function () {
ew_com.comments.disqus.update(window.disqus_identifier, window.disqus_url, timed_disqus);
}, 2600);
if (typeof DISQUSWIDGETS !== 'undefined'){
// Refresh comment count
DISQUSWIDGETS.getCount();
}
};
ew_com.comments.disqus.update = function (disqus_identifier, disqus_url, timed_disqus) {
/**
* Updates Disqus comment board with new/correct one.
* @param {String} disqus_identifier (required) The Disqus identifier.
* @param {String} disqus_url (required) The URL of the Disqus comments.
* @param {Object} timed_disqus (required) Disqus timer.
*/
$('#disqus_thread')[$('#disqus_thread').hasClass('hide-comments') ? 'hide' : 'show']();
clearTimeout(timed_disqus);
timed_disqus = '';
DISQUS.reset({
reload: true,
config: function () {
this.page.identifier = disqus_identifier;
this.page.url = disqus_url;
}
});
};
ew_com.comments.disqus.commentCount = function (ids) {
/**
* Returns comments count for the passed URL.
* @param {Array} ids (required) An array of disqus_identifer strings to check for comments.
* @return {Object} A jQuery request object.
*/
ids = $.map(ids, function(id) {
return ('ident:' + id);
});
// return $.ajax({
return $.ajax({
type: 'GET',
url: 'https://disqus.com/api/3.0/threads/set.jsonp',
data: {
api_key: ew_com.comments.disqus.params.publicAPIKey,
forum: ew_com.comments.disqus.params.shortName,
thread: ids
},
cache: false,
dataType: 'jsonp',
});
};
ew_com.comments.disqus.renderCommentCounts = function ($selector) {
/**
* Updates Disqus comment count in all instances of .dsq-count.
*/
var $dsqBoxes = $selector.children('.box'),
ids = [],
posts;
// Build array of Disqus identifiers.
$selector.each(function (i) {
ids.push($selector.eq(i).attr('data-disqus-identifier'));
});
ew_com.comments.disqus.commentCount(ids)
.done(function (data) {
if (data.response.length !== 0) {
posts = data.response;
$.each(posts, function (index, value) {
// value.posts is the number of comments.
})
}
});
};
ew_com.comments.disqus.init = function () {
ew_com.comments.disqus.renderCommentCounts($('.dsq-count'));
};
})(jQuery, ew_com);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment