Stackedit/js/github-helper.js

176 lines
4.4 KiB
JavaScript
Raw Normal View History

2013-04-10 23:13:31 +00:00
define(["jquery", "async-runner"], function($, asyncTaskRunner) {
// Dependencies
var core = undefined;
var connected = undefined;
2013-04-11 22:38:41 +00:00
var github = undefined;
2013-04-10 23:13:31 +00:00
var githubHelper = {};
// Try to connect github by downloading js file
function connect(callback) {
callback = callback || core.doNothing;
var asyncTask = {};
asyncTask.run = function() {
if(core.isOffline === true) {
connected = false;
core.showMessage("Operation not available in offline mode.");
asyncTask.error();
return;
}
if (connected === true) {
asyncTask.success();
return;
}
$.ajax({
url : "lib/github.js",
dataType : "script", timeout : AJAX_TIMEOUT
}).done(function() {
connected = true;
asyncTask.success();
}).fail(function() {
asyncTask.error();
});
};
asyncTask.onSuccess = function() {
callback();
};
asyncTask.onError = function() {
core.setOffline();
callback();
};
asyncTaskRunner.addTask(asyncTask);
}
// Try to authenticate with Oauth
function authenticate(callback, immediate) {
callback = callback || core.doNothing;
connect(function() {
if (connected === false) {
callback();
return;
}
2013-04-11 22:38:41 +00:00
var intervalId = undefined;
var authWindow = undefined;
var token = localStorage["githubToken"];
2013-04-10 23:13:31 +00:00
var asyncTask = {};
asyncTask.run = function() {
2013-04-11 22:38:41 +00:00
if (github !== undefined) {
2013-04-10 23:13:31 +00:00
asyncTask.success();
return;
}
2013-04-11 22:38:41 +00:00
if (token !== undefined) {
github = new Github({
token: token,
auth: "oauth"
});
asyncTask.success();
return;
2013-04-10 23:13:31 +00:00
}
2013-04-11 22:38:41 +00:00
if(immediate === true) {
core.showError("Unable to perform GitHub authenticate.");
asyncTask.error();
return;
}
// We add time for user to enter his credentials
asyncTask.timeout = AUTH_POPUP_TIMEOUT;
2013-04-10 23:13:31 +00:00
core.showMessage("Please make sure the Github authorization popup is not blocked by your browser.");
2013-04-11 22:38:41 +00:00
localStorage.removeItem("githubCode");
authWindow = core.popupWindow(
'github-oauth-client.html?client_id=' + GITHUB_CLIENT_ID,
'stackedit-github-oauth', 960, 600);
authWindow.focus();
intervalId = setInterval(function() {
var code = localStorage["githubCode"];
if(code !== undefined) {
localStorage.removeItem("githubCode");
$.getJSON(GATEKEEPER_URL + "authenticate/" + code, function(data) {
if(data.token !== undefined) {
localStorage["githubToken"] = data.token;
asyncTask.success();
}
else {
core.showError("Error retrieving GitHub Oauth token.");
asyncTask.error();
}
});
}
}, 500);
2013-04-10 23:13:31 +00:00
};
asyncTask.onSuccess = function() {
2013-04-11 22:38:41 +00:00
if(intervalId !== undefined) {
clearInterval(intervalId);
}
if (github !== undefined) {
callback();
return;
}
authenticate(callback, true);
2013-04-10 23:13:31 +00:00
};
asyncTask.onError = function() {
2013-04-11 22:38:41 +00:00
if(intervalId !== undefined) {
clearInterval(intervalId);
}
if(authWindow !== undefined) {
authWindow.close();
2013-04-10 23:13:31 +00:00
}
callback();
};
asyncTaskRunner.addTask(asyncTask);
});
}
2013-04-11 22:38:41 +00:00
githubHelper.upload = function(username, reponame, branch, path, content, commitMsg, callback) {
2013-04-10 23:13:31 +00:00
callback = callback || core.doNothing;
authenticate(function() {
2013-04-11 22:38:41 +00:00
if (github === undefined) {
2013-04-10 23:13:31 +00:00
callback();
return;
}
2013-04-11 22:38:41 +00:00
var error = undefined;
2013-04-10 23:13:31 +00:00
var asyncTask = {};
asyncTask.run = function() {
2013-04-11 22:38:41 +00:00
var repo = github.getRepo(username, reponame);
repo.write(branch, path, content, commitMsg, function(err) {
if(!err) {
2013-04-10 23:13:31 +00:00
asyncTask.success();
return;
}
2013-04-11 22:38:41 +00:00
error = err.error;
asyncTask.error();
2013-04-10 23:13:31 +00:00
});
};
asyncTask.onSuccess = function() {
2013-04-11 22:38:41 +00:00
callback(error);
2013-04-10 23:13:31 +00:00
};
2013-04-11 22:38:41 +00:00
asyncTask.onError = function() {
if(error === 401) {
github = undefined;
// Token must be renewed
localStorage.removeItem("githubToken");
githubHelper.upload(username, reponame, branch, path, content, commitMsg, callback);
return;
}
if(error === 0) {
connected = false;
github = undefined;
core.setOffline();
}
core.showError("Could not publish on GitHub");
callback(error);
2013-04-10 23:13:31 +00:00
};
asyncTaskRunner.addTask(asyncTask);
});
};
2013-04-11 22:38:41 +00:00
githubHelper.init = function(coreModule) {
2013-04-10 23:13:31 +00:00
core = coreModule;
};
return githubHelper;
});