Skip to content

Commit c21f39c

Browse files
committed
wrapping into a jquery plugin
1 parent da26c39 commit c21f39c

File tree

4 files changed

+47
-49
lines changed

4 files changed

+47
-49
lines changed

demo.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ <h1>Github Project Widget</h1>
3030
<img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub">
3131
</a>
3232

33-
<script src="js/jquery.js"></script>
34-
<script src="js/github.js"></script>
33+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
34+
<script src="js/jquery.github.widget.js"></script>
35+
36+
<script>
37+
$('.github-widget').githubWidget();
38+
</script>
3539

3640
</body>
3741
</html>

js/github.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

js/jquery.github.widget.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// minified (especially when both are regularly referenced in your plugin).
2020

2121
// Create the defaults once
22-
var pluginName = 'defaultPluginName',
22+
var pluginName = 'githubWidget',
2323
document = window.document,
2424
defaults = {
2525
propertyName: "value"
@@ -28,6 +28,8 @@
2828
// The actual plugin constructor
2929
function Plugin( element, options ) {
3030
this.element = element;
31+
this.$container = $(element);
32+
this.repo = this.$container.attr("data-repo");
3133

3234
// jQuery has an extend method which merges the contents of two or
3335
// more objects, storing the result in the first object. The first object
@@ -42,9 +44,44 @@
4244
}
4345

4446
Plugin.prototype.init = function () {
45-
// Place initialization logic here
46-
// You already have access to the DOM element and the options via the instance,
47-
// e.g., this.element and this.options
47+
48+
var self = this;
49+
50+
$.ajax({
51+
url: 'https://api.github.com/repos/' + this.repo,
52+
dataType: 'jsonp',
53+
success: function(results){
54+
55+
var repo = results.data;
56+
var date = new Date(repo.pushed_at);
57+
var pushed_at = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear();
58+
var $widget = $(' \
59+
<div class="github-box repo"> \
60+
<div class="github-box-title"> \
61+
<h3> \
62+
<a class="repo" href="' + repo.url.replace('api.','').replace('repos/','') + '">' + repo.name + '</a> \
63+
</h3> \
64+
<div class="github-stats"> \
65+
<a class="watchers" href="' + repo.url.replace('api.','').replace('repos/','') + '/watchers">' + repo.watchers + '</a> \
66+
<a class="forks" href="' + repo.url.replace('api.','').replace('repos/','') + '/forks">' + repo.forks + '</a> \
67+
</div> \
68+
</div> \
69+
<div class="github-box-content"> \
70+
<p class="description">' + repo.description + ' &mdash; <a href="' + repo.url.replace('api.','').replace('repos/','') + '#readme">Read More</a></p> \
71+
<p class="link"><a href="' + repo.homepage + '">' + repo.homepage + '</a></p> \
72+
</div> \
73+
<div class="github-box-download"> \
74+
<p class="updated">Latest commit to <strong>master</strong> on ' + pushed_at + '</p> \
75+
<a class="download" href="' + repo.url.replace('api.','').replace('repos/','') + '/zipball/master">Download as zip</a> \
76+
</div> \
77+
</div> \
78+
');
79+
80+
$widget.appendTo(self.$container);
81+
82+
}
83+
84+
});
4885
};
4986

5087
// A really lightweight plugin wrapper around the constructor,

js/jquery.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)