aboutsummaryrefslogtreecommitdiffstats
path: root/syte/static/js/components/twitter.js
blob: 4a8bb3018684d63bfcc391370412a91301fe1050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

function setupTwitter(url, el) {
  var href = el.href;

  if ($('#twitter-profile').length > 0) {
    window.location = href;
    return;
  }

  var params = url.attr('path').split('/').filter(function(w) {
      if (w.length)
          return true;
      return false;
  })

  if (params.length == 1) {
     var username = params[0];

     var spinner = new Spinner(spin_opts).spin();
     $('#twitter-link').append(spinner.el);

     require(["json!/twitter/" + username, "text!templates/twitter-view.html"], 
        function(twitter_data, twitter_view) {
            if (twitter_data.error || twitter_data.length == 0) {
                window.location = href;
                return;
            }

            var template = Handlebars.compile(twitter_view);

            var tweets = [];
            $.each(twitter_data, function(i, t) {
              if (i > 3)
                return;

              //'ddd MMM DD HH:mm:ss ZZ YYYY'
              t.formated_date = moment(t.created_at).fromNow();
              t.f_text = twitterLinkify(t.text);
              tweets.push(t);
            });

            var user = twitter_data[0].user;
            user.statuses_count = numberWithCommas(user.statuses_count);
            user.friends_count = numberWithCommas(user.friends_count);
            user.followers_count = numberWithCommas(user.followers_count);
            user.f_description = twitterLinkify(user.description);

            var template_data = {
                "user": user,
                "tweets": tweets
            }

            $(template(template_data)).modal().on('hidden', function () {
                $(this).remove();
                adjustSelection('home-link');
            })

            spinner.stop();
        });

     return;
  }

  window.location = href;
}

function twitterLinkify(text) {
  text = text.replace(/(https?:\/\/\S+)/gi, function (s) {
    return '<a href="' + s + '">' + s + '</a>';
  });

  text = text.replace(/(^|) @(\w+)/gi, function (s) {
    return '<a href="http://twitter.com/' + s + '">' + s + '</a>';
  });

  text = text.replace(/(^|) #(\w+)/gi, function (s) {
    return '<a href="http://search.twitter.com/search?q=' + s.replace(/#/,'%23') + '">' + s + '</a>';
  });

  return text;
}