1+ /*
2+ * jQuery Github - v0.2.4
3+ * A jQuery plugin to display your Github Repositories.
4+ * https://github.com/zenorocha/jquery-github/
5+
6+ * Copyright (c) 2013
7+ * MIT License
8+ */
9+
10+ // the semi-colon before function invocation is a safety net against concatenated
11+ // scripts and/or other plugins which may not be closed properly.
12+ ; ( function ( $ , window , undefined ) {
13+
14+ // undefined is used here as the undefined global variable in ECMAScript 3 is
15+ // mutable (ie. it can be changed by someone else). undefined isn't really being
16+ // passed in so we can ensure the value of it is truly undefined. In ES5, undefined
17+ // can no longer be modified.
18+
19+ // window is passed through as local variable rather than global
20+ // as this (slightly) quickens the resolution process and can be more efficiently
21+ // minified (especially when both are regularly referenced in your plugin).
22+
23+ // Create the defaults once
24+ var pluginName = 'github' ,
25+ document = window . document ,
26+ defaults = {
27+ propertyName : "value"
28+ } ;
29+
30+ // The actual plugin constructor
31+ function Plugin ( element , options ) {
32+ this . element = element ;
33+ this . $container = $ ( element ) ;
34+ this . repo = this . $container . attr ( "data-repo" ) ;
35+
36+ // jQuery has an extend method which merges the contents of two or
37+ // more objects, storing the result in the first object. The first object
38+ // is generally empty as we don't want to alter the default options for
39+ // future instances of the plugin
40+ this . options = $ . extend ( { } , defaults , options ) ;
41+
42+ this . _defaults = defaults ;
43+ this . _name = pluginName ;
44+
45+ this . init ( ) ;
46+ }
47+
48+ Plugin . prototype . init = function ( ) {
49+
50+ var self = this ;
51+ var cached ;
52+
53+ // Attempt to get cached repo data
54+ if ( window . sessionStorage ) {
55+ cached = sessionStorage . getItem ( 'gh-repos:' + this . repo ) ;
56+ }
57+
58+ if ( cached !== null ) {
59+ self . applyTemplate ( JSON . parse ( cached ) ) ;
60+ }
61+ else {
62+
63+ $ . ajax ( {
64+ url : 'https://api.github.com/repos/' + this . repo ,
65+ dataType : 'jsonp' ,
66+ success : function ( results ) {
67+
68+ // Handle API failures
69+ if ( results . meta . status >= 400 && results . data . message ) {
70+ console . warn ( results . data . message ) ;
71+ return ;
72+ }
73+ else {
74+
75+ self . applyTemplate ( results . data ) ;
76+
77+ // Cache data
78+ if ( window . sessionStorage ) {
79+ sessionStorage . setItem ( 'gh-repos:' + self . repo , JSON . stringify ( results . data ) ) ;
80+ }
81+
82+ }
83+ }
84+ } ) ;
85+
86+ }
87+
88+ } ;
89+
90+ Plugin . prototype . applyTemplate = function ( repo ) {
91+
92+ var self = this ;
93+ var date = new Date ( repo . pushed_at ) ;
94+ var pushed_at = date . getDate ( ) + '/' + ( date . getMonth ( ) + 1 ) + '/' + date . getFullYear ( ) ;
95+
96+ var $widget = $ ( $ . parseHTML ( ' \
97+ <div class="github-box"> \
98+ <div class="github-box-header"> \
99+ <h3> \
100+ <a href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '">' + repo . name + '</a> \
101+ </h3> \
102+ <div class="github-stats"> \
103+ <a class="repo-watchers" href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '/watchers">' + repo . watchers + '</a> \
104+ <a class="repo-forks" href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '/forks">' + repo . forks + '</a> \
105+ </div> \
106+ </div> \
107+ <div class="github-box-content"> \
108+ <p>' + repo . description + ' — <a href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '#readme">Read More</a></p> \
109+ </div> \
110+ <div class="github-box-download"> \
111+ <p class="repo-update">Latest commit to <strong>master</strong> on ' + pushed_at + '</p> \
112+ <a class="repo-download" href="' + repo . url . replace ( 'api.' , '' ) . replace ( 'repos/' , '' ) + '/zipball/master">Download as zip</a> \
113+ </div> \
114+ </div> \
115+ ' ) ) ;
116+
117+ self . appendTemplate ( $widget ) ;
118+
119+ } ;
120+
121+ Plugin . prototype . appendTemplate = function ( $widget ) {
122+ var self = this ;
123+ $widget . appendTo ( self . $container ) ;
124+ } ;
125+
126+ // A really lightweight plugin wrapper around the constructor,
127+ // preventing against multiple instantiations
128+ $ . fn [ pluginName ] = function ( options ) {
129+ return this . each ( function ( ) {
130+ if ( ! $ . data ( this , 'plugin_' + pluginName ) ) {
131+ $ . data ( this , 'plugin_' + pluginName , new Plugin ( this , options ) ) ;
132+ }
133+ } ) ;
134+ } ;
135+
136+ } ( jQuery , window ) ) ;
0 commit comments