forked from tricknotes/nothub
/
browser_action.coffee
165 lines (138 loc) · 4.68 KB
/
browser_action.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
background = chrome.extension.getBackgroundPage()
# background page might not be prepared yet
# evaluate updateQuery() during callback execution
updateQuery = -> background.updateQuery()
store = new Store(localStorage)
store.on 'add', updateQuery
store.on 'remove', updateQuery
store.on 'update', updateQuery
# user icons
iconCache = new Store(localStorage)
# for gravatar
loadGravatarIcon = (type, name, callback) ->
if info = iconCache.items('usericon')[name]
callback(info.avatar_url || info.owner.avatar_url)
[apiPath, handler] = switch type
when 'username', 'aboutuser'
[
"users/#{name}"
(data) -> callback(data.avatar_url)
]
when 'reponame'
[
"repos/#{name}"
(data) -> callback(data.owner.avatar_url)
]
$.ajax
url: "https://api.github.com/#{apiPath}"
dataType: 'json'
success: (data) ->
iconCache.add('usericon', name, data)
handler(data)
error: ->
callback('../images/404.png')
# for stream query
supportedEventTypes = {
username: _.map(QueryBuilder.userTypes, ({type}) -> type )
reponame: _.map(QueryBuilder.repoTypes, ({type}) -> type )
}
jQuery ($) ->
# setup about user area
$watchAreaAboutUser = $('.watchAreaAboutUser')
background.getUserName (userName) ->
$('.watchAreaContent', $watchAreaAboutUser).hide()
if userName
$('.loggedIn', $watchAreaAboutUser).show()
loadGravatarIcon 'aboutuser', userName, (icon) ->
$('img.icon', $watchAreaAboutUser).attr('src', icon)
$checkbox = $('input[type=checkbox]', $watchAreaAboutUser)
checked = !!store.items('aboutuser')[userName]
$checkbox.attr('checked', checked)
$checkbox.change ->
if $(this).attr('checked')
store.add('aboutuser', userName, true)
else
store.remove('aboutuser', userName)
else # not logged in
$('.loggedOut', $watchAreaAboutUser).show()
$watchArea = $('.watchArea')
areaFromType = (type) ->
areas = $watchArea.filter (i, el) ->
$(el).data('type') == type
areas[0] # nothing to duplicate
# template for watched name
# requires: name, type, eventTypes
toWatchedArea = _.template($('#watchedAreaTemplate').text())
setupWatchedField = (type, name) ->
return unless supportedEventTypes[type]
$place = $('.watchedNames', areaFromType(type))
$field = $(toWatchedArea({
type
name
eventTypes: supportedEventTypes[type]
}))
# check selected event
eventTypes = store.items(type)[name]
$('input[name=eventTypes]', $field).each ->
if eventTypes[$(this).attr('value')]
$(this).attr('checked', true)
# load gravatar icon
$('img.icon', $field).one 'load', ->
$img = $(this)
loadGravatarIcon type, name, (icon) ->
$img.attr('src', icon)
$place.append($field)
store.on 'add', setupWatchedField
removeNameFromWatchedField = (type, name) ->
return unless supportedEventTypes[type]
$place = $('.watchedNames', areaFromType(type))
$('.watchedRow', $place).each (i, el) ->
if $(el).data('name') == name
$(el).remove()
store.on 'remove', removeNameFromWatchedField
$watchArea.each (i, area) ->
type = $(area).data('type')
# setup submit event
$('.watchButton', area).click ->
$field = $('.nameInputField', area)
name = $field.attr('value')
name = name.replace(/^ +| +$/g, '') # trim
return unless name # name is empty or whitespace only
all = {}
all[n] = true for n in supportedEventTypes[type]
store.add(type, name, all)
$field.attr('value', '')
# setup initialize data
for name, _details of store.items(type)
setupWatchedField(type, name)
# setup delete link
$('.deleteWatchedName', $watchArea).live 'click', ->
$row = $(this).parents('.watchedRow')
name = $row.data('name')
type = $row.data('type')
store.remove(type, name)
# setup configure link
$('.configureWatchedName', $watchArea).live 'click', ->
$row = $(this).parents('.watchedRow')
$area = $('.configureArea', $row)
if shown = $(this).data('showArea')
$area.fadeOut(800)
else
$area.fadeIn(1000)
$(this).data('showArea', !shown)
# setup configuration area
$('input[name=eventTypes]', $watchArea).live 'click', ->
$row = $(this).parents('.watchedRow')
$area = $(this).parents('.configureArea')
name = $row.data('name')
type = $row.data('type')
checking = {}
$('input[name=eventTypes]', $area).each ->
eventType = $(this).attr('value')
checked = !!$(this).attr('checked')
checking[eventType] = checked
true
store.update(type, name, checking)
# cancel default submit
$watchArea.submit ->
false