Skip to content

refactoring #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 127 additions & 71 deletions dist/jquery.github.js
Original file line number Diff line number Diff line change
@@ -1,136 +1,192 @@
/*
* jQuery Github - v0.2.5
* jQuery Github - v0.2.6
* A jQuery plugin to display your Github Repositories.
* https://github.com/zenorocha/jquery-github/

* Copyright (c) 2013
* MIT License
*/

// the semi-colon before function invocation is a safety net against concatenated
// scripts and/or other plugins which may not be closed properly.
;(function ( $, window, undefined ) {

// undefined is used here as the undefined global variable in ECMAScript 3 is
// mutable (ie. it can be changed by someone else). undefined isn't really being
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined
// can no longer be modified.

// window is passed through as local variable rather than global
// as this (slightly) quickens the resolution process and can be more efficiently
// minified (especially when both are regularly referenced in your plugin).

// Create the defaults once
var pluginName = 'github',
document = window.document,
defaults = {
document = window.document,
defaults = {
propertyName: "value"
};

// The actual plugin constructor
function Plugin( element, options ) {
this.element = element;
this.$container = $(element);
this.repo = this.$container.attr("data-repo");
var self = this;

self.element = element;
self.$container = $(element);
self.repo = self.$container.attr("data-repo");

// jQuery has an extend method which merges the contents of two or
// more objects, storing the result in the first object. The first object
// is generally empty as we don't want to alter the default options for
// future instances of the plugin
this.options = $.extend( {}, defaults, options) ;
self.options = $.extend( {}, defaults, options) ;

this._defaults = defaults;
this._name = pluginName;
self._defaults = defaults;
self._name = pluginName;

this.init();
self.init();
}

/**
* Initializer
*
*/
Plugin.prototype.init = function () {

var self = this;
var cached;

// Attempt to get cached repo data
if (window.sessionStorage) {
cached = sessionStorage.getItem('gh-repos:' + this.repo);
}
var self = this,
cached = self.getCache();

if (cached !== null) {
self.applyTemplate(JSON.parse(cached));
}
else {
self.requestData(repo);
}
};

$.ajax({
url: 'https://api.github.com/repos/' + this.repo,
dataType: 'jsonp',
success: function(results){
/**
* Apply results to HTML template
*
*/
Plugin.prototype.applyTemplate = function (repo) {
var self = this,
$widget = self.parseTemplate(repo);

// Handle API failures
if (results.meta.status >= 400 && results.data.message){
console.warn(results.data.message);
return;
}
else {
$widget.appendTo(self.$container);
};

self.applyTemplate(results.data);
/**
* Stores repostories in sessionStorage if available
*
*/
Plugin.prototype.cacheResults = function (result_data) {
var self = this;

// Cache data
if (window.sessionStorage) {
sessionStorage.setItem('gh-repos:' + self.repo, JSON.stringify(results.data));
}
// Cache data
if (window.sessionStorage) {
sessionStorage.setItem('gh-repos:' + self.repo, JSON.stringify(result_data));
}
};

}
}
});
/**
* Grab cached results
*
*/
Plugin.prototype.getCache = function () {
var self = this;

if (window.sessionStorage) {
return sessionStorage.getItem('gh-repos:' + self.repo);
}
else {
return false;
}
};

/**
* Handle Errors requests
*
*/
Plugin.prototype.handlerErrorRequests = function (result_data) {
var self = this;

console.warn(result_data.message);
return;
};

Plugin.prototype.applyTemplate = function (repo) {
/**
* Handle Successful request
*
*/
Plugin.prototype.handlerSuccessfulRequest = function (result_data) {
var self = this;

self.applyTemplate(result_data);
self.cacheResults(result_data);
};

/**
* Parses Pushed date with date format
*
*/
Plugin.prototype.parsePushedDate = function (pushed_at) {
var self = this,
date = new Date(pushed_at);

return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear()
};

/**
* Parses repository URL to be friendly
*
*/
Plugin.prototype.parseRepositoryURL = function (url) {
var self = this;
var date = new Date(repo.pushed_at);
var pushed_at = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();

var $widget = $($.parseHTML(' \
return url.replace('api.','').replace('repos/','');
};

/**
* Parses HTML template
*
*/
Plugin.prototype.parseTemplate = function (repo) {
var self = this,
pushed_at = self.parsePushedDate(repo.pushed_at),
repo_url = self.parseRepositoryURL(repo.url);

return $($.parseHTML(' \
<div class="github-box"> \
<div class="github-box-header"> \
<h3> \
<a href="' + repo.url.replace('api.','').replace('repos/','') + '">' + repo.name + '</a> \
<a href="' + repo_url + '">' + repo.name + '</a> \
</h3> \
<div class="github-stats"> \
<a class="repo-watchers" href="' + repo.url.replace('api.','').replace('repos/','') + '/watchers">' + repo.watchers + '</a> \
<a class="repo-forks" href="' + repo.url.replace('api.','').replace('repos/','') + '/forks">' + repo.forks + '</a> \
<a class="repo-watchers" href="' + repo_url + '/watchers">' + repo.watchers + '</a> \
<a class="repo-forks" href="' + repo_url + '/forks">' + repo.forks + '</a> \
</div> \
</div> \
<div class="github-box-content"> \
<p>' + repo.description + ' &mdash; <a href="' + repo.url.replace('api.','').replace('repos/','') + '#readme">Read More</a></p> \
<p>' + repo.description + ' &mdash; <a href="' + repo_url + '#readme">Read More</a></p> \
</div> \
<div class="github-box-download"> \
<p class="repo-update">Latest commit to <strong>master</strong> on ' + pushed_at + '</p> \
<a class="repo-download" href="' + repo.url.replace('api.','').replace('repos/','') + '/zipball/master">Download as zip</a> \
<a class="repo-download" href="' + repo_url + '/zipball/master">Download as zip</a> \
</div> \
</div> \
'));

self.appendTemplate($widget);

};

Plugin.prototype.appendTemplate = function ($widget) {
/**
* Request repositories from Github
*
*/
Plugin.prototype.requestData = function (repo) {
var self = this;
$widget.appendTo(self.$container);

$.ajax({
url: 'https://api.github.com/repos/' + repo,
dataType: 'jsonp',
success: function(results) {
var result_data = results.data;

// Handle API failures
if (results.meta.status >= 400 && result_data.message) {
self.handlerErrorRequest();
}
else {
self.handlerSuccessfulRequest(result_data);
}
}
});
};

// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
};

}(jQuery, window));
4 changes: 2 additions & 2 deletions dist/jquery.github.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading