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-12 23:09:34 +00:00
|
|
|
var errorMsg = "Access to GitHub is not authorized.";
|
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) {
|
2013-04-12 23:09:34 +00:00
|
|
|
core.showError();
|
2013-04-11 22:38:41 +00:00
|
|
|
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"];
|
2013-04-12 23:09:34 +00:00
|
|
|
if(authWindow.closed === true || code !== undefined) {
|
2013-04-11 22:38:41 +00:00
|
|
|
localStorage.removeItem("githubCode");
|
2013-04-12 23:09:34 +00:00
|
|
|
if(code === undefined) {
|
|
|
|
asyncTask.error();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-11 22:38:41 +00:00
|
|
|
$.getJSON(GATEKEEPER_URL + "authenticate/" + code, function(data) {
|
|
|
|
if(data.token !== undefined) {
|
|
|
|
localStorage["githubToken"] = data.token;
|
|
|
|
asyncTask.success();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
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
|
|
|
}
|
2013-04-12 23:09:34 +00:00
|
|
|
core.showError(errorMsg);
|
2013-04-10 23:13:31 +00:00
|
|
|
callback();
|
|
|
|
};
|
|
|
|
asyncTaskRunner.addTask(asyncTask);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-04-12 23:09:34 +00:00
|
|
|
githubHelper.upload = function(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-12 23:09:34 +00:00
|
|
|
callback("error");
|
2013-04-10 23:13:31 +00:00
|
|
|
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-12 23:09:34 +00:00
|
|
|
var user = github.getUser();
|
|
|
|
user.show(undefined, function(err, result) {
|
|
|
|
if(err) {
|
|
|
|
error = err.error;
|
|
|
|
asyncTask.error();
|
2013-04-10 23:13:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-12 23:09:34 +00:00
|
|
|
var repo = github.getRepo(result.login, reponame);
|
|
|
|
repo.write(branch, path, content, commitMsg, function(err) {
|
|
|
|
if(err) {
|
|
|
|
error = err.error;
|
|
|
|
asyncTask.error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
asyncTask.success();
|
|
|
|
});
|
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() {
|
2013-04-12 23:42:53 +00:00
|
|
|
var errorMsg = "Could not publish on GitHub.";
|
2013-04-12 23:09:34 +00:00
|
|
|
if(error === 401 || error === 403) {
|
2013-04-11 22:38:41 +00:00
|
|
|
github = undefined;
|
|
|
|
// Token must be renewed
|
|
|
|
localStorage.removeItem("githubToken");
|
2013-04-12 23:09:34 +00:00
|
|
|
errorMsg = "Access to GitHub is not authorized.";
|
2013-04-11 22:38:41 +00:00
|
|
|
}
|
2013-04-12 23:09:34 +00:00
|
|
|
else if(error === 0) {
|
2013-04-11 22:38:41 +00:00
|
|
|
connected = false;
|
|
|
|
github = undefined;
|
|
|
|
core.setOffline();
|
|
|
|
}
|
2013-04-12 23:09:34 +00:00
|
|
|
core.showError(errorMsg);
|
2013-04-11 22:38:41 +00:00
|
|
|
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;
|
|
|
|
});
|