Permalink
Browse files

js

  • Loading branch information...
1 parent e65530a commit 0128b90fbebfe5b5bb38f691fe5f38f22c8204bc @benbalter committed Sep 11, 2012
Showing with 243 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +2 −0 _includes/js/commits.js
  3. +20 −0 _includes/js/ga.js
  4. +94 −0 _includes/js/jquery.relatize_date.js
  5. +120 −0 _includes/js/search.js
  6. +5 −0 _includes/js/tweets.js
View
@@ -0,0 +1,2 @@
+.DS_Store
+_site/
View
@@ -0,0 +1,2 @@
+/* recent commits widget */
+jQuery(document).ready(function($){});
View
@@ -0,0 +1,20 @@
+/* Google Analytics virtual event tracking */
+jQuery(document).ready( function($) {
+
+ /* Mailto tracking code */
+ $('a[href^="mailto\:"]').click(function() {
+ _gaq.push(['_trackEvent','Email', 'Click', $(this).attr('href') ]);
+ });
+
+ /* Download Tracking Code */
+ $('a[href$="zip"],a[href$="pdf"],a[href$="doc"],a[href$="docx"],a[href$="xls"],a[href$="xlsx"],a[href$="ppt"],a[href$="pptx"],a[href$="txt"],a[href$="csv"]').click(function() {
+ var u = $(this).attr('href'); _gaq.push(['_trackEvent','Download', u.match(/[^.]+$/), u ]);
+ });
+
+ /* External link tracking code for old site */
+ $('a[href^="http"]').click(function(){
+ if (this.hostname != location.hostname )
+ _gaq.push(['_trackEvent', 'External Link', 'Click', $(this).attr('href') ]);
+ });
+
+});
@@ -0,0 +1,94 @@
+/* All credit goes to Rick Olson. */
+/* http://github.com/github/jquery-relatize_date/blob/master/jquery.relatize_date.js */
+(function($) {
+ $.fn.relatizeDate = function() {
+ return $(this).each(function() {
+ $(this).text( $.relatizeDate(this) );
+ })
+ }
+
+ $.relatizeDate = function(element) {
+ return $.relatizeDate.timeAgoInWords( new Date($(element).text()) );
+ }
+
+ $r = $.relatizeDate;
+
+ $.extend($.relatizeDate, {
+ shortDays: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
+ days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
+ shortMonths: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ],
+ months: [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ],
+
+ /**
+ * Given a formatted string, replace the necessary items and return.
+ * Example: Time.now().strftime("%B %d, %Y") => February 11, 2008
+ * @param {String} format The formatted string used to format the results
+ */
+ strftime: function(date, format) {
+ var day = date.getDay(), month = date.getMonth();
+ var hours = date.getHours(), minutes = date.getMinutes();
+
+ var pad = function(num) {
+ var string = num.toString(10);
+ return new Array((2 - string.length) + 1).join('0') + string;
+ };
+
+ return format.replace(/\%([aAbBcdHImMpSwyY])/g, function(part) {
+ switch(part[1]) {
+ case 'a': return $r.shortDays[day]; break;
+ case 'A': return $r.days[day]; break;
+ case 'b': return $r.shortMonths[month]; break;
+ case 'B': return $r.months[month]; break;
+ case 'c': return date.toString(); break;
+ case 'd': return pad(date.getDate()); break;
+ case 'H': return pad(hours); break;
+ case 'I': return pad((hours + 12) % 12); break;
+ case 'm': return pad(month + 1); break;
+ case 'M': return pad(minutes); break;
+ case 'p': return hours > 12 ? 'PM' : 'AM'; break;
+ case 'S': return pad(date.getSeconds()); break;
+ case 'w': return day; break;
+ case 'y': return pad(date.getFullYear() % 100); break;
+ case 'Y': return date.getFullYear().toString(); break;
+ }
+ })
+ },
+
+ timeAgoInWords: function(targetDate, includeTime) {
+ return $r.distanceOfTimeInWords(targetDate, new Date(), includeTime);
+ },
+
+ /**
+ * Return the distance of time in words between two Date's
+ * Example: '5 days ago', 'about an hour ago'
+ * @param {Date} fromTime The start date to use in the calculation
+ * @param {Date} toTime The end date to use in the calculation
+ * @param {Boolean} Include the time in the output
+ */
+ distanceOfTimeInWords: function(fromTime, toTime, includeTime) {
+ var delta = parseInt((toTime.getTime() - fromTime.getTime()) / 1000);
+ if (delta < 60) {
+ return 'less than a minute ago';
+ } else if (delta < 120) {
+ return 'about a minute ago';
+ } else if (delta < (45*60)) {
+ return (parseInt(delta / 60)).toString() + ' minutes ago';
+ } else if (delta < (120*60)) {
+ return 'about an hour ago';
+ } else if (delta < (24*60*60)) {
+ return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
+ } else if (delta < (48*60*60)) {
+ return '1 day ago';
+ } else {
+ var days = (parseInt(delta / 86400)).toString();
+ if (days > 5) {
+ var fmt = '%B %d, %Y'
+ if (includeTime) fmt += ' %I:%M %p'
+ return $r.strftime(fromTime, fmt);
+ } else {
+ return days + " days ago";
+ }
+ }
+ }
+ })
+})(jQuery);
View
@@ -0,0 +1,120 @@
+/*
+Client-side site search
+Heavily adapted, but inspired by:
+ - http://forrst.com/posts/Static_site_e_g_Jekyll_search_with_JQuery-zL9, and
+ - http://developmentseed.org/blog/2011/09/09/jekyll-github-pages/
+*/
+
+jQuery(document).ready( function($) {
+
+ /* Main filtering logic */
+ function findEntries(q) {
+
+ var matches = [];
+ var rq = new RegExp(q, 'im');
+ $.each( entries.posts, function(k, post ){
+ if ( rq.test( post.title ) || rq.test( post.url ) || rq.test( post.description ) || rq.test( post.content ) || rq.test( post.tags ) || rq.test( post.category ) ) {
+ matches.push({ 'title': post.title, 'url':post.url, 'date': post.date });
+ }
+ });
+
+ $('body').addClass('search');
+ var content = $('#content');
+ content.append( '<h3>Search Results</h3>' );
+
+ if ( matches.length > 0 ) {
+
+ content.append( '<ul class="search-results">' );
+
+ //matches found
+ $.each( matches, function( key, match ){
+
+ content.append( '<li><a href="' + match.url + '">' + match.title + '</a></li>' );
+
+ });
+
+ content.append( '</ul>' );
+
+ } else {
+
+ //no matches
+ content.append( '<div class="no-search-results">No matches found</div>' );
+
+ }
+
+ content.append( '<a href="#" id="back">Back</a>' );
+
+ }
+
+ /* Search form submit */
+ $('#search_form').live( 'submit', function(e) {
+ e.preventDefault();
+ var query = $('#query').val();
+ window.location.hash = 'q='+escape(query.replace(/\s/g, '+'));
+ return false;
+ });
+
+ /* Process hashchange */
+ $(window).bind('hashchange', function(e) {
+
+ // called when the part of the URL after the hash (#) changes
+ var query = window.location.hash; // e.g. "#search=text"
+
+ //if the hash contains search
+ if (/[#?]{1}q=(.*)/.test(query)) {
+
+ //strip search from the hash
+ query = window.location.hash.replace('+', ' ').replace('#q=', '');
+
+ // in case the user browsed to the search
+ $('#query').val(query);
+
+ if (query) {
+
+ if (typeof oldhtml == 'undefined') { // save state!
+ oldhtml = $('#content').html();
+ }
+
+ $('#content').html('<div id="loader"></div>');
+ $('#query').blur().attr('disabled', true);
+
+ if ( typeof entries == 'undefined' ) {
+
+ // lazily load and parse the posts JSON feed
+ $.getJSON( '{{ site.url }}/posts.json', function(data) {
+ entries = data;
+ findEntries( query );
+ });
+
+ } else {
+ // search the pre-loaded data
+ findEntries( query );
+ }
+
+ // disable the search bar until current search is complete
+ $('#query').blur().attr('disabled', false);
+ }
+
+ _gaq.push(['_trackEvent', 'Search', 'Search', query ]);
+
+ } else {
+
+ // revert to original page, hide search results
+ if ( typeof oldhtml == 'undefined' ) {
+ oldhtml = $('#content').html();
+ }
+
+ $('body').removeClass('search');
+ $('#content').html(oldhtml);
+ $('#query').blur().attr('disabled', false).val('');
+
+ _gaq.push(['_trackEvent', 'Search', 'Back', query ]);
+
+ }
+
+ });
+
+ // called in case user browses "into" a search
+ $(window).trigger( 'hashchange' );
+
+});
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 0128b90

Please sign in to comment.