forked from mlaa/tags-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
952 lines (644 loc) · 30.3 KB
/
index.html
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=830">
<script type="text/javascript">
/* Configuration */
// Google spreadsheet URL. If this value is false or empty, the page will
// ask the user to supply one. Remember that the spreadsheet must be made
// public or "visible to anyone with the link."
var spreadsheetURL = '';
// The hashtag that the spreadsheet is archiving, e.g, '#conference'. This is
// not strictly necessary but is useful for display purposes and to exclude
// it from the Tags ranking.
var myHashTag = '';
// The version of TAGS you are using, e.g., '3.2'. Specifying this helps
// speed up page load because we don't need to fetch column labels before the
// first request. If you don't know it, just leave it blank.
var TAGSVersion = '';
// That's about it!
</script>
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6/html5shiv.min.js"></script>
<![endif]-->
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body {
margin: 0;
padding: 0;
}
h1, h2, h3, h4, h5, h6 { margin: 0 0 0.75em 0; }
a { color: #258; }
form { margin: 0; }
div.container, div.row-fluid { width: 800px !important; }
div.no-overflow {
position: absolute;
top: 60px;
right: 0;
bottom: 15px;
left: 0;
overflow: hidden;
}
div.row-fluid { margin: 0 auto;}
div.scrollable { overflow-y: auto; }
div.max-height { height: 100%; }
div.navbar-search { line-height: inherit; }
/* Calender */
div.month {
float: left;
margin-bottom: 15px;
padding: 10px;
background-color: #eee;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-o-border-radius: 10px;
border-radius: 10px;
}
div.month h5 { margin-bottom: 5px; }
div.month a { color: #333; }
span.day {
display: inline-block;
width: 22px;
color: #ccc;
text-align: center;
}
/* Miscellaneous */
#main, #error, #config, .loading, .sidebar { display: none; }
#main .twitter-tweet-rendered { margin-bottom: 2em!important; }
#intro { font-size: 1.1em; }
span.badge {
padding: 2px 6px;
background-color: #789;
}
div.loading {
width: 36px;
height: 36px;
background-image: url('http://mlaa.github.com/tags-viewer/loading.gif');
background-repeat: no-repeat;
}
#people p { clear: left; }
#people img {
float: left;
margin: 0 5px 10px 0;
}
</style>
<title>TAGS Viewer</title>
</head>
<body>
<header>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#"><strong>TAGS Viewer</strong></a>
<ul class="nav" id="tabs">
<li><a href="#/dates">Dates</a></li>
<li><a href="#/people">People</a></li>
<li><a href="#/mentions">Mentions</a></li>
<li><a href="#/tags">Tags</a></li>
<li><a href="#/links">Links</a></li>
<li><a href="#/media">Media</a></li>
</ul>
<form class="navbar-search pull-right">
<input type="text" id="searchBox" class="search-query span2" placeholder="Search">
</form>
</div>
</div>
</div>
</header>
<div class="container no-overflow">
<div class="row-fluid max-height">
<div class="span8 max-height scrollable" id="view">
<noscript>
<div class="alert alert-error">
<p><strong>This site requires JavaScript.</strong></p>
</div>
</noscript>
<div id="main" class="view">
<h3 id="summary"></h3>
</div>
<div id="intro" class="view">
<div id="config">
<div class="alert alert-block">
<form class="form-inline">
<fieldset>
<p><strong>No data source.</strong> TAGS Viewer requires a <a href="http://mashe.hawksey.info/2012/01/twitter-archive-tagsv3/">TAGS</a> Google Spreadsheet URL as a data source. Please supply one below.</p>
<input id="key" type="text" placeholder="TAGS Google Spreadsheet URL">
<button id="keyButton" type="button" class="btn">Submit</button>
</fieldset>
</form>
</div>
</div>
<h3>Introduction</h3>
<p><strong>TAGS Viewer</strong> allows users to browse, explore, and search a <em>Twitter</em> archive. As a backend, it requires Martin Hawksey’s <a href="http://mashe.hawksey.info/2012/01/twitter-archive-tagsv3/">Twitter Archive Google Spreadsheet (TAGS)</a>. TAGS provides a free, non-technical method of archiving tweets for a given hashtag, which can be particularly useful for capturing a conference’s <a href="http://en.wikipedia.org/wiki/Backchannel">backchannel</a>.</p>
<p>This application is contained in a single HTML file and has no server dependencies, which makes it easy to host anywhere: just upload a single file (this one!) and you’re done. Or, if you don’t need to share it with anyone, just double-click the file on your hard drive to open it in your Web browser. Configuration is as simple as supplying a Google Spreadsheet URL.</p>
<p>Read more about this project at <a href="https://github.com/mlaa/tags-viewer">Github</a> or download <a href="https://github.com/mlaa/tags-viewer/archive/master.zip">a zip file of the latest version</a>. This work is licensed under a <a href="http://creativecommons.org/licenses/by-nc/3.0/">Creative Commons Attribution-NonCommercial 3.0 Unported License</a>.</p>
<p><img src="http://i.creativecommons.org/l/by-nc/3.0/88x31.png" width="88" height="31" alt="Creative Commons License"></p>
</div>
<div id="error" class="view">
<div class="alert alert-error">
<p><strong>Error:</strong> Sorry, we could not process your request.</p>
</div>
</div>
<div id="view-loading" class="loading"></div>
</div>
<div class="span4 well max-height scrollable" id="sidebar">
<div id="home" class="sidebar">
<h4>Overview</h4>
</div>
<div id="dates" class="sidebar">
<h4>Dates</h4>
</div>
<div id="people" class="sidebar">
<h4>People</h4>
</div>
<div id="mentions" class="sidebar">
<h4>Mentions</h4>
</div>
<div id="tags" class="sidebar">
<h4>Tags</h4>
</div>
<div id="links" class="sidebar">
<h4>Links</h4>
</div>
<div id="media" class="sidebar">
<h4>Media</h4>
</div>
<div id="search" class="sidebar"></div>
<div id="sidebar-loading" class="loading"></div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-hashchange/v1.3/jquery.ba-hashchange.min.js"></script>
<script src="http://mlaa.github.com/tags-viewer/jquery.sheetrock.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/1.7.2/moment.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js"></script>
<script type="text/javascript">
/* Application configuration */
config = {
spreadsheet: {
// Default column labels
labels: ['id_str', 'from_user', 'text', 'created_at', 'time'],
// Default chunk size (number of rows to load at a time)
chunkSize : 20,
// Default number of header rows
headers : 1
},
// All column labels for known TAGS versions
columns: {
'2.4.4': {A:'from_user', B:'text', C:'created_at', D:'time', E:'geo_coordinates', F:'iso_language_code', G:'to_user_id_str', H:'from_user_id_str', I:'id_str', J:'source', K:'profile_image_url', L:'status_url', M:'entities_str'},
'3.1': {A:'id_str', B:'from_user', C:'text', D:'created_at', E:'time', F:'geo_coordinates', G:'iso_language_code', H:'to_user', I:'to_user_id_str', J:'from_user_id_str', K:'source', L:'profile_image_url', M:'status_url', N:'entities_str'},
'3.2': {A:'id_str', B:'from_user', C:'text', D:'created_at', E:'time', F:'geo_coordinates', G:'iso_language_code', H:'to_user', I:'to_user_id_str', J:'from_user_id_str', K:'in_reply_to_status_id_str', L:'source', M:'profile_image_url', N:'status_url', O:'entities_str'}
},
// Our hash tag
myHashTag: (typeof myHashTag === 'undefined') ? '' : myHashTag.toLowerCase().replace(/^#/, ''),
// Debug mode.
debug: true,
// Use Twitter embed -- see comments in "Twitter embed", below.
twitterEmbed : true,
// Define spreadsheet views for valid (non-error) requests. The
// query language is SQL-like. Google provides ocumentation here:
// https://developers.google.com/chart/interactive/docs/querylanguage
views : {
home : { }, // No dynamic content
dates : { sql: "select %id_str%,%from_user%,%text%,%created_at%,%time% where not %text% starts with 'RT' and toDate(%time%) = date '%%%'" },
people : { sql: "select %id_str%,%from_user%,%text%,%created_at%,%time% where lower(%from_user%) = '%%%'" },
mentions : { sql: "select %id_str%,%from_user%,%text%,%created_at%,%time% where lower(%text%) contains '@%%%'" },
tags : { sql: "select %id_str%,%from_user%,%text%,%created_at%,%time% where lower(%text%) contains '#%%%'" },
links : { sql: "select %id_str%,%from_user%,%text%,%created_at%,%time% where lower(%entities_str%) contains '%%%'" },
media : { sql: "select %id_str%,%from_user%,%text%,%created_at%,%time% where lower(%entities_str%) contains '%%%'" },
search : { sql: "select %id_str%,%from_user%,%text%,%created_at%,%time% where not %text% starts with 'RT' and lower(%text%) contains '%%%'" }
},
sidebars: {
home : { sql: "select count(%id_str%),min(%time%),max(%time%)", labels: ['count','first','last'] },
dates : { sql: "select %time%,count(%id_str%) where not %text% starts with 'RT' and year(%time%) is not null group by %time%", labels: ['date', 'count'] },
people : { sql: "select %from_user%,count(%id_str%),max(%profile_image_url%) group by %from_user% order by count(%id_str%) desc", labels: ['user', 'count', 'profile_image_url'] },
mentions : { sql: "select %entities_str%" },
tags : { sql: "select %entities_str%" },
links : { sql: "select %entities_str%" },
media : { sql: "select %entities_str%" },
search : { } // No dynamic content
},
// Define sidebars with entity information (they are loaded together).
entities : ['mentions','tags','links','media'],
summaries: {
dates : '%%%',
people : '@%%%',
mentions : 'Mentions: @%%%',
tags : '#%%%',
links : 'Links: %%%',
media : 'Media: %%%',
search : 'Search results for <span class="label label-info">%%%</label>'
},
// Define a jQuery selector for the top-level content children
// so we can remove them when loading a new view.
templateChildren : 'div, blockquote',
// Certain domains are whitelisted as media object providers.
mediaDomains : ['youtube.com','youtu.be','vimeo.com','twitpic.com','yfrog.com','docs.google.com'],
// Certain file extensions are whitelisted as media objects.
mediaExtensions : ['jpg','jpeg','gif','png','mov','avi']
};
/* Cache */
cache = {
// Cache templates and callback functions. Strings are WAY
// faster than DOM-based <script> templates. Why is that?
templates: {
home : _.template('<p>There are <strong><%= parseInt(cells.count) - 1 %> tweets</strong> in this archive.</p><p>This first tweet was <strong><%= moment(cells.first).fromNow() %></strong>. The most recent tweet was <strong><%= moment(cells.last).fromNow() %></strong>.</p>'),
tweets : _.template('<blockquote class="twitter-tweet"><p><%= cells.text %></p>— @<%= cells.from_user %> <a href="https://twitter.com/<%= cells.from_user %>/status/<%= cells.id_str %>" data-datetime="<%= cells.time %>"><%= cells.created_at %></a></blockquote>'),
dates : loadDates,
months : _.template('<div class="month"><h5><%= month %> <%= year %></h5></div>'),
days : _.template('<span class="day"><% if(date) { %><a href="#/dates/<%= date %>"><%= day %></a><% } else { %><%= day %><% } %></span><% if(week) { %><br><% } %>'),
people : _.template('<p><img src="<%= cells.profile_image_url %>" height="48" width="48" alt="<%= cells.user %>" class="img-rounded"> <a href="#/people/<%= cells.user %>"><%= cells.user %></a><br><span class="badge badge-info"><%= cells.count %></span></p>'),
mentions : _.template('<p><a href="#/mentions/<%= name %>"><%= name %></a> <span class="badge badge-info"><%= count %></span></p>'),
tags : _.template('<p><a href="#/tags/<%= name %>"><%= name %></a> <span class="badge badge-info"><%= count %></span></p>'),
links : _.template('<p><a href="#/links/<%= name %>"><%= name %></a> <span class="badge badge-info"><%= count %></span></p>'),
media : _.template('<p><a href="#/media/<%= name %>"><%= name %></a> <span class="badge badge-info"><%= count %></span></p>')
},
// Cache jQuery selectors.
elements: {
tabs : $('#tabs li'),
view : $('#view'),
views : $('#view .view'),
sidebar : $('#sidebar'),
sidebars : $('#sidebar .sidebar'),
main : $('#main'),
config : $('#config'),
intro : $('#intro'),
error : $('#error'),
summary : $('#summary'),
home : $('#home'),
dates : $('#dates'),
people : $('#people'),
mentions : $('#mentions'),
tags : $('#tags'),
links : $('#links'),
media : $('#media'),
search : $('#search'),
searchBox : $('#searchBox'),
calendar : $('#calendar'),
key : $('#key'),
keyButton : $('#keyButton'),
loading_v : $('#view-loading'),
loading_s : $('#sidebar-loading')
},
// Create a placeholder for an entities cache.
entities: {links: {}, media: {}, tags: {}, mentions: {}},
// Create a placeholder for a dates cache.
dates: {}
};
/* State information */
state = {
previous: { sidebar: '' },
entitiesLoaded: false
};
/* Twitter embed */
// Twitter asks that you use their embed solution when
// displaying tweets on your site. While it makes them
// nice to look at and does nice things like pulling in
// whole conversations, it can slow down a page. Also
// note that it doesn't seem to work on localhost.
if(config.twitterEmbed) $.getScript('https://platform.twitter.com/widgets.js');
/* Application */
$(document).ready(function() {
// If a spreadsheet URL or key has been supplied, throw
// the lever! Otherwise, allow a user to specify one.
if(parseSheet(spreadsheetURL) || parseSheet(window.location.search.substring(1))) {
setup();
} else {
cache.elements.config.show();
cache.elements.key.bind('keydown', addSheetListener);
cache.elements.keyButton.bind('click', addSheet);
}
});
/* Setup */
function setup() {
// Configure Sheetrock.
$.each(config.spreadsheet, function(key, val) {
$.fn.sheetrock.options[key] = val;
});
// Hardcode column labels, if known.
if(_.has(config.columns, TAGSVersion)) {
$.fn.sheetrock.labels = config.columns[TAGSVersion];
}
// Start hashchange router.
$(window).hashchange(hashRouter).trigger('hashchange');
// Listen for tab clicks.
cache.elements.tabs.bind('click', tabClick);
// Listen for searches.
cache.elements.searchBox.bind('keydown', searchWhack);
// Add user-supplied hash tag to page header.
$('#title').html(myHashTag);
}
/* Hashchange router */
function hashRouter(e) {
// Identify requested resource.
var hash = window.location.hash.replace(/^[#\/]*/, '');
var request = hash.split('/', 2);
// Populate state information.
state.sidebar = request[0] || 'home';
state.view = request[1] || false;
// Validate request.
if(!_.has(config.sidebars, state.sidebar)) {
cache.elements.views.hide();
cache.elements.error.show();
return false;
}
// If sidebar has changed, load new sidebar.
if(state.previous.sidebar !== state.sidebar) {
// Hide all sidebars and show requested sidebar.
cache.elements.sidebars.hide();
cache.elements[state.sidebar].show();
// Store new sidebar in state variable.
state.previous.sidebar = state.sidebar;
// Load any dynamic sidebar content.
if(_.has(config.sidebars[state.sidebar], 'sql')) {
if($.inArray(state.sidebar, config.entities) >= 0) {
if(!state.entitiesLoaded) loadEntities();
} else {
loadSidebar();
}
}
// Scroll to the top of the sidebar.
cache.elements.sidebar.scrollTop(0);
}
// If content is requested, load content.
if(state.view) {
// Hide current view.
cache.elements.views.hide();
// Clear previously loaded content.
cache.elements.main.children(config.templateChildren).remove();
$.data(cache.elements.main[0], 'sheetrock-loaded', 0);
$.data(cache.elements.main[0], 'sheetrock-row-offset', 0);
// Load content and start infinite scroll listener, if needed.
if(_.has(config.views[state.sidebar], 'sql')) {
cache.elements.view.scroll(scrollTroll);
loadContent();
}
// Update content summary.
cache.elements.summary.html(config.summaries[state.sidebar].replace(/%%%/g, state.view));
// Show content and scroll to the top of the main window.
cache.elements.main.show();
cache.elements.main.scrollTop(0);
}
// The home page gets special treatment.
if(state.sidebar == 'home') {
cache.elements.views.hide();
cache.elements.intro.show();
}
}
/* Bindings */
function addSheetListener(e) {
if(e.which == 13) addSheet(e);
}
function addSheet(e) {
var key = parseSheet(cache.elements.key.val());
if(key) {
window.location.href = window.location.href + '?' + key;
e.preventDefault();
}
}
function searchWhack(e) {
var searchText = trim($(this).val());
if(e.which == 13 && searchText) {
$(this).blur();
window.location.hash = '#/search/' + encodeURIComponent(searchText);
e.preventDefault();
}
}
function tabClick(e) {
cache.elements.tabs.removeClass('active');
$(this).addClass('active');
}
/* Loading */
function loadSidebar() {
// Build SQL query.
var query = $.extend({}, config.sidebars[state.sidebar]);
query.rowHandler = cache.templates[state.sidebar];
query.chunkSize = 0;
// Debug.
if(config.debug) console.log(query.sql);
// Build calendar for `dates` request.
if(state.sidebar == 'dates') query.userCallback = loadCalendar;
// Pass along loading indicator.
query.loading = cache.elements.loading_s;
// Fetch data.
cache.elements[state.sidebar].sheetrock(query);
}
function loadContent() {
// Build SQL query.
var query = $.extend({}, config.views[state.sidebar]);
query.sql = query.sql.replace(/%%%/, state.view.toLowerCase());
query.rowHandler = cache.templates.tweets;
// Debug.
if(config.debug) console.log(query.sql);
// Schedule Twitter embed agent, if enabled.
if(config.twitterEmbed) query.userCallback = formatTweets;
// Pass along loading indicator.
query.loading = cache.elements.loading_v;
// Fetch data.
cache.elements.main.sheetrock(query);
}
function loadEntities() {
// Build SQL query.
var query = $.extend({}, config.sidebars[state.sidebar]);
query.chunkSize = 0;
// Debug.
if(config.debug) console.log(query.sql);
// Set callback functions.
query.dataHandler = parseEntities;
// Pass along loading indicator.
query.loading = cache.elements.loading_s;
// Fetch data.
$.fn.sheetrock(query);
}
/* Date callbacks */
function loadDates(row) {
if(!_.has(cache.dates, 'start')) cache.dates.start = moment(row.cells.date).format('YYYY-MM-DD');
cache.dates[moment(row.cells.date).format('YYYY-MM-DD')] = row.cells.count;
}
function loadCalendar(options) {
if(cache.dates.start) {
var start = { date: moment(cache.dates.start, 'YYYY-MM-DD') };
var today = { date: moment() };
start.year = parseInt(start.date.format('YYYY'));
today.year = parseInt(today.date.format('YYYY'));
start.month = parseInt(start.date.format('M'));
today.month = parseInt(today.date.format('M'));
for(y=start.year; y<=today.year; y++) {
var minMonth = (y > start.year) ? 1 : start.month;
var maxMonth = (y < today.year) ? 12 : today.month;
for(m=minMonth; m<=maxMonth; m++) {
var first = moment(y + '-' + m, 'YYYY-MM');
var month = $(cache.templates.months({month: first.format('MMMM'), year: y})).appendTo(cache.elements.dates);
var dow = parseInt(first.format('d'));
for(d=0; d<dow; d++) month.append(cache.templates.days({date: '', day: ' ', week: false}));
for(d=1; d<=first.daysInMonth(); d++) {
var date = y + '-' + strpad(m, 2) + '-' + strpad(d, 2);
var week = !((d + dow) % 7);
if(!_.has(cache.dates, date)) date = '';
month.append(cache.templates.days({date: date, day: d, week: week}));
}
}
}
}
}
/* Data callbacks */
function parseEntities(data) {
// Extract entities and populate cache.
_.each(data.table.rows, function(obj, i) {
if(_.has(obj, 'c')) {
var entities = (_.has(obj.c[0], 'v')) ? $.parseJSON(obj.c[0].v) : {};
if(entities) cacheEntities(entities);
}
});
// Sort entities by count (descending order).
_.each(cache.entities, function(entities, i) {
cache.entities[i] = _.sortBy(entities, function(entity, j) { return -entity.count; });
});
// Change state of loaded entities.
state.entitiesLoaded = true;
// Show entities.
showEntities();
}
function cacheEntities(entityGroup) {
// Take a look at the entities (last) column in the Archive sheet.
// I won't attempt to document it here.
// Iterate over the entities and push them into the cache.
$.each(entityGroup, function(i, entities) {
$.each(entities, function(j, entity) {
switch (i) {
case 'urls':
case 'media':
var parsedUri = parseUri(entity.expanded_url.toLowerCase());
var domain = parsedUri.host.replace(/^www\d?\./, '');
// Separate "media" objects (pictures, videos, documents) in a separate cache.
var cacheName = (i == 'media' || isMediaURL(domain, parsedUri.file)) ? 'media' : 'links';
if(_.has(cache.entities[cacheName], domain)) {
// If we've seen the domain before, push it onto the stack and increment the counter.
cache.entities[cacheName][domain].count++;
cache.entities[cacheName][domain].urls.push(entity.expanded_url);
} else {
// Start a list of URLs from this domain.
cache.entities[cacheName][domain] = { count: 1, name: domain, urls: [entity.expanded_url] };
}
break;
case 'hashtags':
var tagName = entity.text.toLowerCase();
// Exclude the hashtag we are all here to see.
if(tagName !== config.myHashTag) {
if(_.has(cache.entities.tags, tagName)) {
// If we've seen the tag before, increment the counter.
cache.entities.tags[tagName].count++;
} else {
// Preserve case of the first instance we encounter.
cache.entities.tags[tagName] = { count: 1, name: entity.text };
}
}
break;
case 'user_mentions':
var screenName = entity.screen_name.toLowerCase();
if(_.has(cache.entities.mentions, screenName)) {
// If we've seen the screen name before, increment the counter.
cache.entities.mentions[screenName].count++;
} else {
// Add information about the mention to the database.
cache.entities.mentions[screenName] = { count: 1, name: screenName, id: entity.id, realName: entity.name };
}
break;
}
});
});
return null;
}
/* Rendering */
function showEntities() {
$.each(config.entities, function(i, sidebar) {
$.each(cache.entities[sidebar], function(j, entity) {
cache.elements[sidebar].append(cache.templates[sidebar](entity));
});
});
}
function formatTweets() {
if(typeof twttr !== 'undefined') twttr.widgets.load();
}
/* Miscellaneous wheel reinventing */
// Homemade vanilla infinite scroll
function scrollTroll() {
// Don't load more data if we are already actively getting some.
if(!$.fn.sheetrock.working) {
// Trigger callback when user scrolls past this number of pixels
// from the bottom of the page.
var pixelBuffer = 100;
// Short reference to main window.
var el = cache.elements.view;
// Determine if there is still more data to get.
var loaded = parseInt($.data(el[0], 'sheetrock-loaded')) || false;
// If there is no more content, unbind the scroll event.
if(loaded) {
el.unbind('scroll');
// Otherwise, load more content.
} else {
if(el[0].scrollHeight - el.scrollTop() - el.height() < pixelBuffer) loadContent();
}
}
}
// Compare URL against whitelist of media Web sites and file types.
function isMediaURL(domain, file) {
if($.inArray(domain, config.mediaDomains) >= 0) return true;
if($.inArray(file.split('.', 2)[1], config.mediaExtensions) >= 0) return true;
return false;
}
// Zero-pad numbers.
function strpad(str, len) {
while((str + '').length < len) str = '0' + str;
return str;
}
// Trim string.
function trim(str) {
return str.toString().replace(/^ +/, '').replace(/ +$/, '');
}
// Parse Google Spreadsheet URL.
function parseSheet(str) {
var url = trim(str).match(/key=([^\&\#]+)(#gid=([0-9]+))?/) || [];
var key = trim(str).match(/[A-z0-9\-\=]+/) || [];
key = url[1] || key[0];
if(key) {
var slug = 'https://docs.google.com/spreadsheet/ccc?key=';
config.spreadsheet.url = slug + key + '#gid=' + (url[3] || '82');
}
return key;
}
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
function parseUri (str) {
var o = parseUri.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14;
while (i--) uri[o.key[i]] = m[i] || "";
uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
});
return uri;
};
parseUri.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
</script>
</body>
</html>