11/*
2- * jQuery Github - v0.2.5
2+ * jquery-github - v0.3.4
33 * A jQuery plugin to display your Github Repositories.
4- * https://github.com/zenorocha/jquery-github/
5-
6- * Copyright (c) 2013
4+ * https://github.com/zenorocha/jquery-github
5+ *
6+ * Copyright (c) 2014
77 * MIT License
88 */
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 ) ) ;
9+ // -- Github Repository --------------------------------------------------------
10+
11+ function GithubRepo ( repo ) {
12+ this . description = repo . description ;
13+ this . forks = repo . forks_count ;
14+ this . name = repo . name ;
15+ this . open_issues = repo . open_issues ;
16+ this . pushed_at = repo . pushed_at ;
17+ this . url = repo . url ;
18+ this . stargazers = repo . stargazers_count ;
19+ }
20+
21+ // Parses HTML template
22+ GithubRepo . prototype . toHTML = function ( ) {
23+ this . pushed_at = this . _parsePushedDate ( this . pushed_at ) ,
24+ this . url = this . _parseURL ( this . url ) ;
25+
26+ return $ (
27+ "<div class='github-box'>" +
28+ "<div class='github-box-header'>" +
29+ "<h3>" +
30+ "<a href='" + this . url + "'>" + this . name + "</a>" +
31+ "</h3>" +
32+ "<div class='github-stats'>" +
33+ "<a class='repo-stars' title='Stars' data-icon='7' href='" + this . url + "/stargazers'>" + this . stargazers + "</a>" +
34+ "<a class='repo-forks' title='Forks' data-icon='f' href='" + this . url + "/network'>" + this . forks + "</a>" +
35+ "<a class='repo-issues' title='Issues' data-icon='i' href='" + this . url + "/issues'>" + this . open_issues + "</a>" +
36+ "</div>" +
37+ "</div>" +
38+ "<div class='github-box-content'>" +
39+ "<p>" + this . description + " — <a href='" + this . url + "#readme'>Read More</a></p>" +
40+ "</div>" +
41+ "<div class='github-box-download'>" +
42+ "<p class='repo-update'>Latest commit to <strong>master</strong> on " + this . pushed_at + "</p>" +
43+ "<a class='repo-download' title='Download as zip' data-icon='w' href='" + this . url + "/zipball/master'></a>" +
44+ "</div>" +
45+ "</div>" ) ;
46+ } ;
47+
48+ // Parses pushed_at with date format
49+ GithubRepo . prototype . _parsePushedDate = function ( pushed_at ) {
50+ var date = new Date ( pushed_at ) ;
51+
52+ return date . getDate ( ) + "/" + ( date . getMonth ( ) + 1 ) + "/" + date . getFullYear ( ) ;
53+ } ;
54+
55+ // Parses URL to be friendly
56+ GithubRepo . prototype . _parseURL = function ( url ) {
57+ return url . replace ( "api." , "" ) . replace ( "repos/" , "" ) ;
58+ } ;
59+
60+ // -- Github Plugin ------------------------------------------------------------
61+
62+ function Github ( element , options ) {
63+ var defaults = {
64+ iconStars : true ,
65+ iconForks : true ,
66+ iconIssues : false
67+ } ;
68+
69+ this . element = element ;
70+ this . $container = $ ( element ) ;
71+ this . repo = this . $container . attr ( "data-repo" ) ;
72+
73+ this . options = $ . extend ( { } , defaults , options ) ;
74+
75+ this . _defaults = defaults ;
76+
77+ this . init ( ) ;
78+ this . displayIcons ( ) ;
79+ }
80+
81+ // Initializer
82+ Github . prototype . init = function ( ) {
83+ var cached = this . getCache ( ) ;
84+
85+ if ( cached !== null ) {
86+ this . applyTemplate ( JSON . parse ( cached ) ) ;
87+ return ;
88+ }
89+
90+ this . requestData ( this . repo ) ;
91+ } ;
92+
93+ // Display or hide icons
94+ Github . prototype . displayIcons = function ( ) {
95+ var options = this . options ,
96+ $iconStars = $ ( ".repo-stars" ) ,
97+ $iconForks = $ ( ".repo-forks" ) ,
98+ $iconIssues = $ ( ".repo-issues" ) ;
99+
100+ $iconStars . css ( "display" , options . iconStars ? "inline-block" : "none" ) ;
101+ $iconForks . css ( "display" , options . iconForks ? "inline-block" : "none" ) ;
102+ $iconIssues . css ( "display" , options . iconIssues ? "inline-block" : "none" ) ;
103+ } ;
104+
105+ // Request repositories from Github
106+ Github . prototype . requestData = function ( repo ) {
107+ var that = this ;
108+
109+ $ . ajax ( {
110+ url : "https://api.github.com/repos/" + repo ,
111+ dataType : "jsonp" ,
112+ success : function ( results ) {
113+ var result_data = results . data ,
114+ isFailling = results . meta . status >= 400 && result_data . message ;
115+
116+ if ( isFailling ) {
117+ that . handleErrorRequest ( result_data ) ;
118+ return ;
119+ }
120+
121+ that . handleSuccessfulRequest ( result_data ) ;
122+ }
123+ } ) ;
124+ } ;
125+
126+ // Handle Errors requests
127+ Github . prototype . handleErrorRequest = function ( result_data ) {
128+ console . warn ( result_data . message ) ;
129+ return ;
130+ } ;
131+
132+ // Handle Successful request
133+ Github . prototype . handleSuccessfulRequest = function ( result_data ) {
134+ this . applyTemplate ( result_data ) ;
135+ this . setCache ( result_data ) ;
136+ } ;
137+
138+ // Stores repostories in sessionStorage if available
139+ Github . prototype . setCache = function ( result_data ) {
140+ // Cache data
141+ if ( window . sessionStorage ) {
142+ window . sessionStorage . setItem ( "gh-repos:" + this . repo , JSON . stringify ( result_data ) ) ;
143+ }
144+ } ;
145+
146+ // Grab cached results
147+ Github . prototype . getCache = function ( ) {
148+ if ( window . sessionStorage ) {
149+ return window . sessionStorage . getItem ( "gh-repos:" + this . repo ) ;
150+ }
151+ else {
152+ return false ;
153+ }
154+ } ;
155+
156+ // Apply results to HTML template
157+ Github . prototype . applyTemplate = function ( repo ) {
158+ var githubRepo = new GithubRepo ( repo ) ,
159+ $widget = githubRepo . toHTML ( ) ;
160+
161+ $widget . appendTo ( this . $container ) ;
162+ } ;
163+
164+ // -- Attach plugin to jQuery's prototype --------------------------------------
165+
166+ ; ( function ( $ , window , undefined ) {
167+
168+ $ . fn . github = function ( options ) {
169+ return this . each ( function ( ) {
170+ if ( ! $ ( this ) . data ( "plugin_github" ) ) {
171+ $ ( this ) . data ( "plugin_github" , new Github ( this , options ) ) ;
172+ }
173+ } ) ;
174+ } ;
175+
176+ } ( window . jQuery || window . Zepto , window ) ) ;
0 commit comments