User:Majr/twitterFeed.js

$( function { 'use strict';

var $feeds = $( '.twitter-feed' ), p = 'twitter-feed-tweet-';

$feeds.empty.removeClass( 'twitter-success twitter-failure' );

if ( $feeds.length ) { $feeds.each( function {       var $element = $( this ), $count = 1, feed = {}, promise = [], out = , trim,            $names = $element.data( 'names' ).split( ' ' ),            $showDetails = $element.data( 'show-user-details' );        if ( $element.data( 'count' ) !== undefined ) {            $count = $element.data( 'count' );        }        if ( $showDetails === true || $showDetails === undefined || $showDetails ===  ) {            $element.addClass( 'twitter-feed-name' );            trim = 0;        } else {            $element.removeClass( 'twitter-feed-name' );            trim = 1;        }        $.each( $names, function { var name = this; promise.push(               $.ajax( { url: 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=1&trim_user=' + trim + '&screen_name=' + name + '&count=' + $count, dataType: 'jsonp', timeout: 10000, success: function( resp ) { return feed[name] = parseTweets( resp, name ); }               } )            );        } );        $.when.apply( $, promise ).then( function { $.each( $names, function {               out += feed[this];            } ); $element.addClass( 'twitter-success' ).html( '\n\t\n' + out + '\t\n' ); }, function { $element.addClass( 'twitter-failure' ).html( ' Failed to connect to Twitter ' ); } );   } ); }

function parseTweets( tweets, name ) { var out = '';

$.each( tweets, function {       var t = {}, iu = -1, reg;        t.urls = {}; t.mentions = {}; t.hashtags = {};        if ( this.user.name !== undefined ) {            t.fullName = this.user.name;            t.avatar = this.user.profile_image_url_https;        }        t.name = name;        t.id = this.id_str;        t.source = this.source.replace( 'rel="nofollow"', 'class="' + p + 'ext-link" rel="nofollow"' );        t.date = this.created_at.replace( /\w+ (\w+) 0?(\d+) 0?(\d+):(\d+):\d{2} \+\d+ \d{2}(\d+)/, function( $, $1, $2, $3, $4, $5 ) { var m = ' AM - '; if ( $3 >= 12 ) { m = ' PM - '; if ( $3 > 12 ) { $3 = $3 - 12;                   }                } else if ( $3 === 0 ) { $3 = 12;               }                return $3 + ':' + $4 + m + [$2, $1, $5].join(' '); }       );        if ( this.entities.urls.length ) {            $.each ( this.entities.urls, function( iu ) { t.urls[iu] = {}; t.urls[iu].short = this.url; t.urls[iu].real = this.expanded_url; t.urls[iu].display = this.display_url; } );       }        if ( this.entities.media !== undefined && this.entities.media.length ) {            iu++;            $.each ( this.entities.media, function( i ) { i = i + iu; t.urls[i] = {}; t.urls[i].short = this.url; t.urls[i].real = this.expanded_url; t.urls[i].display = this.display_url; } );       }        if ( this.entities.user_mentions.length ) {            $.each ( this.entities.user_mentions, function( i ) { t.mentions[i] = this.screen_name; } );       }        if ( this.entities.hashtags.length ) {            $.each ( this.entities.hashtags, function( i ) { t.hashtags[i] = this.text; } );       }        t.text = this.text;        if ( !$.isEmptyObject( t.urls ) ) {            $.each ( t.urls, function { t.text = t.text.replace( this.short, '' + this.display + '' ); } );       }        if ( !$.isEmptyObject( t.mentions ) ) {            $.each ( t.mentions, function { reg = new RegExp( '@' + this, 'i' ); t.text = t.text.replace( reg, '@ ' + this + ' ' ); } );       }        if ( !$.isEmptyObject( t.hashtags ) ) {            $.each ( t.hashtags, function { reg = new RegExp( '#' + this, 'i' ); t.text = t.text.replace( reg, '# ' + this + ' ' ); } );       }        out += styleTweet( t );    } ); return out; }

function styleTweet( tweet ) { var out = '\t\t\n'; if ( tweet.fullName !== undefined ) { out += '\t\t\t\n' + '\t\t\t\t ' + tweet.fullName + ' @ ' + tweet.name + ' \n' + '\t\t\t \n'; }   out += '\t\t\t' + tweet.text + ' \n' + '\t\t\t' + tweet.date + '</a> via ' + tweet.source + ' \n' + '\t\t</li>\n' return out; }

} );