/
sparerib.js
348 lines (296 loc) · 12.2 KB
/
sparerib.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
(function($) {
// Models
var Document = Backbone.Model.extend({ url: function() { return "/api/1.0/document/" + this.id; } });
var DocumentCollection = Backbone.Collection.extend({ model: Document, url: "/api/1.0/document" });
var Docket = Backbone.Model.extend({ url: function() { return "/api/1.0/docket/" + this.id; } });
var DocketCollection = Backbone.Collection.extend({ model: Docket, url: "/api/1.0/docket" });
var Agency = Backbone.Model.extend({ url: function() { return "/api/1.0/agency/" + this.id; } });
var AgencyCollection = Backbone.Collection.extend({ model: Agency, url: "/api/1.0/agency" });
var Entity = Backbone.Model.extend({ url: function() { return "/api/1.0/entity/" + this.id; } });
var EntityCollection = Backbone.Collection.extend({ model: Entity, url: "/api/1.0/entity" });
var SearchResults = Backbone.Model.extend({ idAttribute: "query", url: function() { return "/api/1.0/search/" + (this.get('level') ? this.get('level') + '/' : '') + encodeURIComponent(this.id) + (this.get('in_page') ? "?page=" + this.get('in_page') : ''); } });
var SearchResultsCollection = Backbone.Collection.extend({ model: SearchResults, url: "/api/1.0/search" });
// Template helpers
var helpers = {
'formatDate': function(iso_date) {
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date(iso_date);
return (months[date.getUTCMonth()] + " " + date.getUTCDate() + ", " + date.getUTCFullYear());
},
'capitalize': function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
'prettifyLabel': function(string) {
return _.map(string.split('_'), helpers.capitalize).join(' ');
},
'getIcon': function(file_type) {
var icons = {
'html': 'html',
'xml': 'html',
'crtext': 'html',
'msw': 'msw',
'msw6': 'msw',
'msw8': 'msw',
'msw12': 'msw',
'pdf': 'pdf',
'rtf': 'rtf',
'txt': 'txt',
'wp8': 'wp8',
'?': 'unknown'
}
return '/static/img/icons/64x64/icon_' + (typeof icons[file_type] == "undefined" ? icons['?'] : icons[file_type]) + '.png';
}
}
// Views
var SearchView = Backbone.View.extend({
tagName: 'div',
className: 'search-view',
events: {
'submit form': 'search'
},
template: _.template($('#search-tpl').html()),
render: function() {
$(this.el).html(this.template(this));
return this;
},
search: function(evt) {
evt.preventDefault();
app.navigate('/search/' + encodeURIComponent($(this.el).find('.search-query').val()), {trigger: true});
return false;
}
})
var ResultsView = Backbone.View.extend({
tagName: 'div',
id: 'results-view',
template: _.template($('#results-tpl').html()),
render: function() {
this.model.fetch(
{
'success': $.proxy(function() {
var context = _.extend({}, helpers, this.model.toJSON());
$(this.el).html(this.template(context));
// update the URL for the right type
if (!this.model.get('level')) {
app.navigate('/search-' + this.model.attributes.search.aggregation_level + '/' + encodeURIComponent(this.model.attributes.search.raw_query) + (this.model.get('in_page') ? '/' + this.model.get('in_page') : ''), {trigger: false, replace: true});
}
// populate the search input
this.$el.closest('.search-view').find('form input.search-query').val(context.search.raw_query);
}, this),
'error': function() {
console.log('failed');
}
}
);
return this;
}
})
var AggregatedDetailView = Backbone.View.extend({
tagName: 'div',
id: 'docket-view',
template: _.template($('#aggregated-tpl').html()),
render: function() {
this.model.fetch(
{
'success': $.proxy(function() {
var context = _.extend({}, helpers, this.model.toJSON());
$(this.el).html(this.template(context));
// charts
SpareribCharts.type_breakdown_piechart('type-breakdown', context.stats.type_breakdown);
var timeGranularity = this.model.get('type') == 'docket' ? 'weeks' : 'months';
var timeline_data = [{
'name': 'Submission Timline',
'href': '',
'timeline': _.map(context.stats[timeGranularity], function(period) {
return period.count;
})
}];
SpareribCharts.timeline_chart('submission-timeline', timeline_data);
}, this),
'error': function() {
console.log('failed');
}
}
);
return this;
}
})
var DocumentDetailView = Backbone.View.extend({
tagName: 'div',
id: 'document-view',
events: {
'click .tab': 'switchTab',
'click .attachment-name': 'toggleAttachment'
},
template: _.template($('#document-tpl').html()),
render: function() {
this.model.fetch(
{
'success': $.proxy(function() {
var context = _.extend({}, helpers, this.model.toJSON());
// tweak attachments a bit
context['full_attachments'] = [{'title': 'Main Views', 'attachment': false, 'views': context['views']}].concat(_.map(context['attachments'], function(attachment) {
attachment['attachment'] = true;
return attachment;
}));
$(this.el).html(this.template(context));
// make the first attachment visible
$(this.el).find('.attachment-name').eq(0).click()
}, this),
'error': function() {
console.log('failed');
}
}
);
return this;
},
switchTab: function(evt) {
var $tab = $(evt.target).closest('.tab');
var $this = $(this.el);
var $area = $tab.closest('.tab-area');
if (!$tab.hasClass('active')) {
$area.find('.tab').removeClass('active');
$tab.addClass('active');
var view = $area.find('.tab-view').hide().filter('[data-tab-id=' + $tab.attr('data-tab-id') + ']').show();
var iframe = view.find('iframe');
if (!iframe.attr('src')) {
iframe.attr('src', iframe.attr('data-src'));
}
}
},
toggleAttachment: function(evt) {
var $name = $(evt.target).closest('.attachment-name');
var $attachment = $name.closest('.attachment');
var $area = $attachment.find('.tab-area');
if (!$name.hasClass('active')) {
// first make sure something is visible in the hidden area
var tabs = $area.find('.tab');
if (tabs.filter('.active').length == 0) {
tabs.eq(0).click();
}
// then show the whole thing
$name.addClass('active');
$area.slideDown('fast');
} else {
$name.removeClass('active');
$area.slideUp('fast');
}
}
})
var EntityDetailView = Backbone.View.extend({
tagName: 'div',
id: 'entity-view',
template: _.template($('#entity-tpl').html()),
render: function() {
this.model.fetch(
{
'success': $.proxy(function() {
var context = _.extend({}, helpers, this.model.toJSON());
$(this.el).html(this.template(context));
// charts
_.each(['submitter_mentions', 'text_mentions'], function(submission_type) {
if (context.stats[submission_type].count == 0) {
return;
}
var timeline_data = [{
'name': 'Submission Timline',
'href': '',
'timeline': _.map(context.stats[submission_type].months, function(month) {
return month.count;
})
}];
SpareribCharts.timeline_chart(({'submitter_mentions': 'submission', 'text_mentions': 'mention'})[submission_type] + '-timeline', timeline_data);
});
}, this),
'error': function() {
console.log('failed');
}
}
);
return this;
}
})
// Router
var AppRouter = Backbone.Router.extend({
initialize: function() {
// routes
// resource pages
this.route("document/:id", "documentDetail");
this.route("docket/:id", "docketDetail");
this.route("agency/:id", "agencyDetail");
this.route(/^(organization|individual|politician|entity)\/[a-zA-Z0-9-]*\/([a-z0-9-]*)$/, "entityDetail");
// search
this.route("", "searchLanding");
this.route("search/:term/:page", "defaultSearchResults");
this.route("search/:term", "defaultSearchResults");
this.route("search-:type/:term/:page", "searchResults");
this.route("search-:type/:term", "searchResults");
// load the upper search box at the beginning
var topSearchView = new SearchView({'id': 'top-search-form'});
$('#top-search').html(topSearchView.render().el);
// on all navigation, check to show/hide the search box
this.on('all', function () {
if ($('#main .search-view').length != 0) {
$('#top-search').hide();
} else {
$('#top-search').show().find('input[type=text]').val('');
}
});
},
searchLanding: function() {
var searchView = new SearchView({'id': 'main-search-form'});
$('#main').html(searchView.render().el);
},
defaultSearchResults: function(query, page) {
this.searchResults(null, query, page);
},
searchResults: function(type, query, page) {
console.log(query, page);
// are we on a search page?
var resultSet = $('#main .result-set');
if (resultSet.length == 0) {
this.searchLanding();
resultSet = $('#main .result-set');
}
if (typeof page == "undefined") {
page = null;
}
var results = new SearchResults({'query': query, 'in_page': page, 'level': type});
var resultsView = new ResultsView({model: results});
resultSet.html(resultsView.render().el);
},
documentDetail: function(id) {
var doc = new Document({'id': id});
var view = new DocumentDetailView({model: doc});
$('#main').html(view.render().el);
},
docketDetail: function(id) {
var docket = new Docket({'id': id});
var view = new AggregatedDetailView({model: docket});
$('#main').html(view.render().el);
},
agencyDetail: function(id) {
var agency = new Agency({'id': id});
var view = new AggregatedDetailView({model: agency});
$('#main').html(view.render().el);
},
entityDetail: function(type, id) {
var entity = new Entity({'id': id});
var entityView = new EntityDetailView({model: entity});
$('#main').html(entityView.render().el);
}
});
var app = new AppRouter();
window.app = app;
console.log(app);
Backbone.history.start({pushState: true});
/* assume backbone link handling, from Tim Branyen */
$(document).on("click", "a:not([data-bypass])", function(evt) {
var href = $(this).attr("href");
var protocol = this.protocol + "//";
if (href && href.slice(0, protocol.length) !== protocol &&
href.indexOf("javascript:") !== 0) {
evt.preventDefault();
Backbone.history.navigate(href, true);
}
});
})(jQuery);