-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.coffee
142 lines (118 loc) · 4.11 KB
/
app.coffee
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class GithubMon
repositoryTemplate: null
pullRequestTemplate: null
repositoryJSON: null
repositories: []
hiddenPRs: []
constructor: (url) ->
@url = url.replace(/[\?#].*/, '')
_.templateSettings =
interpolate: /\{\{(.+?)\}\}/g
@repositoryTemplate = $('#repository-row').html()
@pullRequestTemplate = $('#pull-request-row').html()
@port = chrome.extension.connect name: 'connection'
@port.onMessage.addListener (msg) =>
if msg.success
@render()
else
debugger
@renderVersion()
@accessToken = localStorage.getItem('accessToken')
@githubHost = if localStorage.getItem('githubHost') then localStorage.getItem('githubHost') else 'https://github.com'
if @accessToken
@render()
@triggerFetch()
else
@renderHelpView()
@promptAddRepo()
@
renderVersion: ->
manifest = chrome.runtime.getManifest()
$('.version').text(manifest.version)
render: ->
@fetchRepositories()
@fetchPullRequests()
@populateRepoList()
@bindEvents()
fetchRepositories: =>
@repositories = JSON.parse(localStorage.getItem('repositories')) or []
fetchPullRequests: =>
@hiddenPRs = JSON.parse(localStorage.getItem('hiddenPRs')) or []
@repositoryJSON = JSON.parse(localStorage.getItem('repos'))
populateRepoList: ->
if @repositories.length > 0
$('.empty').hide()
html = _(@repositoryJSON).map (pullRequests, repo) =>
pullRequests = _(pullRequests).filter (pr) =>
not _(@hiddenPRs).contains pr.id
if pullRequests.length > 0
pullRequestsHTML = _(pullRequests).map (pr) =>
_.template @pullRequestTemplate,
id: pr.id
title: pr.title
html_url: pr.html_url
user: pr.user.login
user_avatar: pr.user.avatar_url
user_url: pr.user.html_url
git_host: @githubHost
created_at: moment.utc(pr.created_at).fromNow()
else
pullRequestsHTML = ["<li><p>No PR's</p></li>"]
_.template @repositoryTemplate,
name: repo
git_host: @githubHost
pullRequests: pullRequestsHTML.join('')
$('#repositories').html html.join('')
else
$('#repositories').html('')
$('.empty').show()
bindEvents: =>
$('.hide').on 'click', @hidePR
$('.remove').on 'click', @removeRepository
promptAddRepo: ->
regexExpression = "^" + @githubHost + "\\/([\\w-\\.]+\\/[\\w-\\.]+)"
regex = new RegExp regexExpression
if match = @url.match(regex)
@currentRepo = match[1]
@showPrompt(@currentRepo) unless _(@repositories).contains @currentRepo
else
@hidePrompt()
showPrompt: (repository) ->
$('.add-repo .title').text(repository)
$('.add-repo').show()
$('.add-repo .add').on 'click', @addCurrentRepo
hidePrompt: ->
$('.add-repo').hide()
addCurrentRepo: =>
@repositories.push @currentRepo
localStorage.setItem('repositories', JSON.stringify(@repositories))
@triggerFetch()
@hidePrompt()
hidePR: (event) =>
id = $(event.target).closest('li').data('id')
@hiddenPRs.push(id)
localStorage.setItem('hiddenPRs', JSON.stringify(@hiddenPRs))
@render()
removeRepository: (event) =>
repo = $(event.target).closest('li').data('id')
@repositories = _(@repositories).without(repo)
localStorage.setItem('repositories', JSON.stringify(@repositories))
@repositoryJSON = JSON.parse(localStorage.getItem('repos'))
delete @repositoryJSON[repo]
localStorage.setItem('repos', JSON.stringify(@repositoryJSON))
@promptAddRepo()
@triggerFetch()
renderHelpView: ->
$('.welcome').show()
$('.save-token').on 'click', =>
if at = $('#access-token').val()
localStorage.setItem('accessToken', at)
localStorage.setItem('githubHost', gh) if gh = $('#github-host').val()
localStorage.setItem('githubApiHost', gah) if gah = $('#github-apihost').val()
$('.welcome').hide()
@triggerFetch()
triggerFetch: ->
@port.postMessage refresh: true
$ ->
chrome.tabs.getSelected null, (tab) ->
mon = new GithubMon(tab.url)