Created
May 30, 2014 20:44
-
-
Save ghankerson/9e2f0c27f55b8bec4869 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var disqus_shortname; | |
// Module pattern see http://learn.jquery.com/code-organization/concepts/ | |
window.Dapp = function (){ | |
'use strict'; | |
var commentUser; | |
var thread_id; | |
var comment_count; | |
var comments_closed = false; | |
var dconfig = {}; | |
var that = this; | |
var config= { | |
base_url: 'http://www.marketplace.org', | |
childDataRef : "https://****", | |
cacheDataRef : "https://****", | |
commentBox: '#createCommentBox', | |
commentCloseDate: 60 * 60 * 24 * 30, // close after 30 days | |
comments: "#comments", | |
commentsTitle: "#comments h3", | |
commentContainer: "ul#commentList", | |
commentItemContainer: "ul#commentList li", | |
commentCountContainer: '.comment-count', | |
createThreadEndpoint: '/sites/all/libraries/apmdisqusapi/php/create_thread.php', | |
createCommentEndpoint: '/sites/all/libraries/apmdisqusapi/php/create_comment.php', | |
createaccount: '#createaccount', | |
createComment: '#createComment', | |
createCommentWrapper: '#textboxWrapper', | |
dataRef : "https://boiling-fire-4861.firebaseio.com/", | |
disqusdiv : "#disqus_thread", | |
disqusUserEndpoint: '/sites/all/libraries/apmdisqusapi/php/duser.php', | |
DISQUS_PUBLIC : "*****", | |
endpoint: "https://disqus.com/api/3.0/threads/listPosts.json", | |
firebase: 'https://boiling-fire-4861.firebaseio.com/', | |
logout: '#logout', | |
login: '#login', | |
pagerNext: '#comments-next', | |
pagerPrev: '#comments-prev', | |
reminder: '#login-reminder', | |
shortname: "apm-marketplace", | |
sociallogin: "#sociallogin", | |
socialloginOptions: "#sociallogin ul.loginoptions", | |
storyPubDate: Date(), | |
testing: false, | |
title: $(document).find('title'), | |
tos: '#tos', | |
tos_wrapper: "#tos-wrapper", | |
useDateCloseComments: true, | |
user_info: "user_info", | |
warning: 'p.warning' | |
}; | |
var init = function(options, bool) { | |
if(bool) { | |
$.extend(publicApi, { | |
config: config, | |
login: login, | |
doLogin: doLogin, | |
firebaseAuth: firebaseAuth, | |
firebaseAuthSetup: firebaseAuthSetup, | |
checkForSimpleLogin: checkForSimpleLogin, | |
}); | |
} | |
$(config.socialloginOptions).bind('click', login); | |
$.each([config.logout], function(idx, value) { $(value).hide();}); | |
config = $.extend(config, options); | |
window.disqus_shortname = config.shortname; | |
}; | |
var login = function(e) { | |
e.preventDefault(); | |
if($(config.tos).attr('checked') === false) { | |
if($(config.warning).length === 0) { | |
var message = "<p class='warning'>You must agree to the terms of service before logging in to use comments</p>"; | |
$(config.sociallogin).prepend(message); | |
} | |
return; | |
} | |
var provider = e.target.className.replace(/\ssociallogin\ssprite\ssprite--(facebook|twitter|plus)/, ''); | |
checkForSimpleLogin(provider); | |
}; | |
var checkForSimpleLogin = function(provider) { | |
if (typeof(that._auth) === 'undefined') { | |
firebaseAuthSetup(function() { | |
doLogin(provider); | |
}); | |
} | |
else { | |
doLogin(provider); | |
} | |
}; | |
var doLogin = function(provider) { | |
if(provider === 'facebook') { | |
that._auth.login(provider, { | |
rememberMe: true, | |
scope: 'email' | |
}); | |
} | |
else if(provider === 'email') { | |
$(config.tabs).show(); | |
} | |
else { | |
that._auth.login(provider); | |
} | |
}; | |
var firebaseAuthSetup = function (cb) { | |
that._dataRef = new Firebase(config.dataRef); | |
firebaseAuth(cb); | |
}; | |
var firebaseAuth = function (cb) { | |
that._auth = new FirebaseSimpleLogin(that._dataRef, function(error, user) { | |
if (error) { | |
// an error occurred while attempting login | |
} else if (user) { | |
logUserin(user); | |
} else { | |
// user is logged ou | |
$.each([config.commentBox, config.createCommentWrapper, '#' + config.user_info], function(idx, val) { | |
$(val).hide().remove(); | |
}); | |
var numComments = $(config.commentItemContainer).size(); | |
if(numComments === 0 ) { | |
loadDisqus(dconfig); | |
} | |
} | |
}); | |
if(typeof(cb) === 'function') { | |
cb(); | |
} | |
}; | |
var publicApi = { | |
init: init, | |
}; | |
return publicApi; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment