aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts/tweets.coffee.erb
blob: 13cbb8e92c7fe67a8bf2a1ceafbb4ababe20d684 (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
Views.tweets =
  _: ->
    vm = new Vue
      el: ".statuses"
      data:
        statuses: []
        loading: false
        next: null
        prev: null
      methods:
        failProfileImage: (e) ->
          e.preventDefault()
          e.target.src = '<%= image_path("profile_image_missing.png") %>'
        toggleExpandFavorites: (status, e) ->
          e.preventDefault()
          status.expandFavorites = !status.expandFavorites
        toggleExpandRetweets: (status, e) ->
          e.preventDefault()
          status.expandRetweets = !status.expandRetweets
        openIntent: (e) ->
          e.preventDefault()
          Helpers.openTwitterIntent(e.target.getAttribute("href"))
      filters:
        formatSource: (str) ->
          if /^<a href="([^"]+?)" rel="nofollow">([^<>]+?)<\/a>$/.test(str)
            str
          else
            str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
        formatText: (str) ->
          autolinked = twttr.txt.autoLink(str, { suppressLists: true, usernameIncludeSymbol: true, usernameUrlBase: "/" })
          autolinked.replace(/\r?\n/g, "<br />\n")

    loadJSON = (url) ->
      vm.loading = true
      superagent
        .get url
        .accept "json"
        .end (err, res) ->
          json = res.body
          json.statuses.forEach (status) ->
            status.expandFavorites = status.expandRetweets = false
            if status.allowed && status.reactions_count > 0
              status.loading = true
              superagent
                .get "/i/" + status.id_str + "/responses"
                .accept "json"
                .end (rerr, rres) ->
                  rjson = rres.body
                  status.favorites = rjson.favorites
                  status.retweets = rjson.retweets
                  status.loading = false
          vm.statuses = vm.statuses.concat(json.statuses)
          vm.loading = false
          vm.next = json.next
          vm.prev = json.prev

    loadJSON(window.location.toString())
    
    content = $(".statuses")
    $(window).scroll ->
      if vm.loading || !vm.next
        return
      if (content.offset().top + content.height()) - ($(document).scrollTop() + $(window).height()) < 100
        loadJSON vm.next