Skip to content

Instantly share code, notes, and snippets.

@dshaw002
Created November 7, 2012 21:32
Show Gist options
  • Save dshaw002/4034631 to your computer and use it in GitHub Desktop.
Save dshaw002/4034631 to your computer and use it in GitHub Desktop.
Javascript for About/Twitter
<script>
$(document).ready(function(){
$(document).on('click','.tour-modal-next', function(e){
e.preventDefault();
var tour_id= parseInt($(this).attr('id'));
tour_id++;
$('.tour-modal').hide();
if(tour_id != 5) {
$('#take-the-tour-'+tour_id).show();
}
if(tour_id == 5) { $('.overlay').hide();}
});
$('.about-menu').click(function(e){
e.preventDefault();
$('.who-we-are').hide();
$('.about-faq').hide();
$('.contact').hide();
$('.team').hide();
$('.about-links-active-triangle').hide();
var link = $(this).attr('id');
console.log(link);
$(this).next().show();
$('.' + link).show();
changeHeaderText(link);
});
$('#about-contact').submit(function(e){
e.preventDefault();
$.ajax({
url: 'mail.php',
type: 'POST',
dataType: 'json',
data: { name: $('.about-name').val(), from: $('.about-email').val(),
subject: $('.about-subject').val(), message: $('.about-message').val() },
success:function(data){
if(data.result=="success"){
$('.about-submit-p').hide();
$('.about-form-result').html('Thank you for your comment. We will be in touch!');
$('.about-form-result').show();
}
}
});
});
$('.btn-tour').click(function(e){
e.preventDefault();
$('.overlay').show();
$('#take-the-tour-1').show();
//console.log("clicked!");
});
$(document).on('click','.tour-close-button', function(e){
e.preventDefault();
$('.tour-modal').hide();
$('.overlay').hide();
});
getTweetsFromDB();
function changeHeaderText(link)
{
if(link == "who-we-are") { $('.about-header h2').text("WHO WE ARE"); }
else if(link == "about-faq") { $('.about-header h2').text("FAQ"); }
else if(link =="team") { $('.about-header h2').text("TEAM"); }
else if(link =="contact") { $('.about-header h2').text("CONTACT"); }
}
function getTweetsFromDB() {
$.ajax({
url: '<?=$this->config->base_url()?>ajax/check_tweets',
type: 'POST',
dataType: 'json',
data: { timestamp: "<?=time('r')?>", action: "fetch" },
success:function(data) {
if(data.result == "no_tweets") {
getTweetsFromTwitter();
}
else if(data.result== "old_tweets") {
getTweetsFromTwitter();
}
else if(data.result=="success") {
printTweets(data.tweets);
}
}
});
}
function getTweetsFromTwitter() {
$.ajax({
url: 'https://api.twitter.com/1/statuses/user_timeline.json',
type: 'GET',
data: { screen_name: 'primecardinc', count:'4' },
dataType: 'jsonp',
success:function(data)
{
console.log(data);
printTweets(data);
for(var i = 0; i < data.length; i ++)
{
tweet_item = new Object();
tweet_item.text = data[i].text;
tweet_item.created_at = data[i].created_at;
saveTweets(tweet_item);
}
}
});
}
function saveTweets(tweets) {
$.ajax({
url: '<?=$this->config->base_url()?>ajax/check_tweets',
type: 'POST',
dataType: 'json',
data: { tweets: tweets, action: 'save', timestamp: "<?=time()?>" },
success:function(data) {
if(data.result=="success") {
console.log("Logged Great!");
}
else {
console.log("We have a problem!");
}
}
});
}
function printTweets(tweets) {
console.log(tweets);
for(var i = 0; i < 2; i++)
{
//append text
$('.primecard-tweet-box').append('<br/><p>'+checkTweetsForLinks(tweets[i].text)+
'<br/><br/><i>'+tweets[i].created_at+' ago</i></p> \
<span class="tweet-line">&nbsp;</span>');
//append line if i==0
}
}
function checkTweetsForLinks(tweet){
if(tweet.indexOf('http')!=-1) {
var url_index = tweet.indexOf('http');
//var url_end_index = tweet.lastIndexOf('/');
var url_end_index = url_index+20;
var url = tweet.substring(url_index, url_end_index);
var new_tweet = tweet.substring(0,url_index);
new_tweet += '<a href="'+url+'">';
new_tweet += tweet.substring(url_index,url_end_index);
new_tweet += '</a>';
new_tweet += tweet.substring(url_end_index);
return new_tweet;
} else { return tweet; }
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment